91 lines
3.4 KiB
C++
91 lines
3.4 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
// Copyright 2016 Google LLC.
|
|
//
|
|
// This software is licensed under the terms defined in the Widevine Master
|
|
// License Agreement. For a copy of this agreement, please contact
|
|
// widevine-licensing@google.com.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Unit tests for the ecb_util functions.
|
|
|
|
#include <string>
|
|
|
|
#include "testing/gmock.h"
|
|
#include "testing/gunit.h"
|
|
#include "absl/strings/escaping.h"
|
|
#include "absl/strings/string_view.h"
|
|
#include "common/ecb_util.h"
|
|
|
|
namespace widevine {
|
|
namespace crypto_util {
|
|
|
|
TEST(CryptoUtilTest, TestEncrypt3DesEcb) {
|
|
// Test vector generated by (Python):
|
|
// c = M2Crypto.EVP.Cipher('des_ede3_ecb', '89abcdef0123456789abcdef',
|
|
// iv='', op=1, padding=False)
|
|
// (c.update('This is a test message. ')+c.final()).encode('hex')
|
|
// TODO(user): find some Widevine test vectors and use those
|
|
std::string key = "0123456789abcdef";
|
|
std::string plain = "This is a test message. ";
|
|
std::string cipher;
|
|
std::string decrypted;
|
|
EXPECT_TRUE(Encrypt3DesEcb(key, plain, &cipher));
|
|
EXPECT_EQ("ae7c7accaf99a973e7f89bb7f6dc61a9aa9e226a5ba17376",
|
|
absl::BytesToHexString(cipher));
|
|
EXPECT_TRUE(Decrypt3DesEcb(key, cipher, &decrypted));
|
|
EXPECT_EQ(plain, decrypted);
|
|
}
|
|
|
|
TEST(CryptoUtilTest, TestEncrypt3DesEcbFail) {
|
|
// Verify that 3DES fails with invalid key or data size
|
|
std::string badkey = "0123456789abcde"; // 15 bytes
|
|
std::string badplain = "This is a test message."; // 23 bytes
|
|
std::string goodkey = "0123456789abcdef"; // 16 bytes
|
|
std::string goodplain = "This is a test message. "; // 24 bytes
|
|
|
|
// Encryption failure should leave 'decrypted' as empty std::string
|
|
std::string out = "Not empty";
|
|
EXPECT_FALSE(Encrypt3DesEcb(badkey, goodplain, &out));
|
|
EXPECT_TRUE(out.empty());
|
|
|
|
out = "Not empty";
|
|
EXPECT_FALSE(Decrypt3DesEcb(goodkey, badplain, &out));
|
|
EXPECT_TRUE(out.empty());
|
|
}
|
|
|
|
TEST(CryptoUtilTest, TestEncryptAesEcb) {
|
|
// Test vector generated by (Python):
|
|
// c = M2Crypto.EVP.Cipher('aes_128_ecb', key, '', 1, padding=False)
|
|
// encrypted = c.update(data) + c.final();
|
|
// TODO(user): find some Widevine test vectors and use those
|
|
std::string key = "0123456789abcdef";
|
|
std::string plaintext = "This message has 32 bytes in it.";
|
|
std::string encrypted;
|
|
std::string decrypted;
|
|
EXPECT_TRUE(EncryptAesEcb(key, plaintext, &encrypted));
|
|
EXPECT_EQ("1f6a7d63e0645de25c56c6b39ba7723d640129d65f41e96b87be812bc94ad8a9",
|
|
absl::BytesToHexString(encrypted));
|
|
EXPECT_TRUE(DecryptAesEcb(key, encrypted, &decrypted));
|
|
EXPECT_EQ(plaintext, decrypted);
|
|
}
|
|
|
|
TEST(CryptoUtilTest, TestEncryptAesEcbFail) {
|
|
// Verify that EncryptAesEcb fails with invalid key or data size
|
|
std::string badkey = "0123456789abcde"; // 15 bytes
|
|
std::string badplain = "This message has 31 bytes in it";
|
|
std::string goodkey = "0123456789abcdef"; // 16 bytes
|
|
std::string goodplain = "This message has 32 bytes in it.";
|
|
|
|
// Encryption failure should leave 'decrypted' as empty std::string
|
|
std::string out = "Not empty";
|
|
EXPECT_FALSE(EncryptAesEcb(badkey, goodplain, &out));
|
|
EXPECT_TRUE(out.empty());
|
|
|
|
out = "Not empty";
|
|
EXPECT_FALSE(EncryptAesEcb(goodkey, badplain, &out));
|
|
EXPECT_TRUE(out.empty());
|
|
}
|
|
|
|
} // namespace crypto_util
|
|
} // namespace widevine
|