summaryrefslogtreecommitdiffstats
path: root/crypto/encryptor_unittest.cc
diff options
context:
space:
mode:
authorxhwang@chromium.org <xhwang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-13 19:48:01 +0000
committerxhwang@chromium.org <xhwang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-13 19:48:01 +0000
commita3f742697f50cae3ace0a84a3ace1dde9beaef65 (patch)
tree1160547489bf6fb94af068a029d7cf70ae34769f /crypto/encryptor_unittest.cc
parentdf29744db2dfe21c90308089ff70197a5dd710f0 (diff)
downloadchromium_src-a3f742697f50cae3ace0a84a3ace1dde9beaef65.zip
chromium_src-a3f742697f50cae3ace0a84a3ace1dde9beaef65.tar.gz
chromium_src-a3f742697f50cae3ace0a84a3ace1dde9beaef65.tar.bz2
Support CTR-AES in encryptor_openssl.
BUG=163552 TEST=Added unittest to cover CTR-AES encryption/decryption. Also tested AesDecryptorTest in media_unittests. Review URL: https://chromiumcodereview.appspot.com/16654005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@206141 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'crypto/encryptor_unittest.cc')
-rw-r--r--crypto/encryptor_unittest.cc218
1 files changed, 190 insertions, 28 deletions
diff --git a/crypto/encryptor_unittest.cc b/crypto/encryptor_unittest.cc
index 5b234d2..2f569e1 100644
--- a/crypto/encryptor_unittest.cc
+++ b/crypto/encryptor_unittest.cc
@@ -15,7 +15,7 @@ TEST(EncryptorTest, EncryptDecrypt) {
scoped_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPassword(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
- EXPECT_TRUE(NULL != key.get());
+ EXPECT_TRUE(key.get());
crypto::Encryptor encryptor;
// The IV must be exactly as long as the cipher block size.
@@ -39,27 +39,27 @@ TEST(EncryptorTest, DecryptWrongKey) {
scoped_ptr<crypto::SymmetricKey> key(
crypto::SymmetricKey::DeriveKeyFromPassword(
crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
- EXPECT_TRUE(NULL != key.get());
+ EXPECT_TRUE(key.get());
// A wrong key that can be detected by implementations that validate every
// byte in the padding.
scoped_ptr<crypto::SymmetricKey> wrong_key(
crypto::SymmetricKey::DeriveKeyFromPassword(
crypto::SymmetricKey::AES, "wrongword", "sweetest", 1000, 256));
- EXPECT_TRUE(NULL != wrong_key.get());
+ EXPECT_TRUE(wrong_key.get());
// A wrong key that can't be detected by any implementation. The password
// "wrongword;" would also work.
scoped_ptr<crypto::SymmetricKey> wrong_key2(
crypto::SymmetricKey::DeriveKeyFromPassword(
crypto::SymmetricKey::AES, "wrongword+", "sweetest", 1000, 256));
- EXPECT_TRUE(NULL != wrong_key2.get());
+ EXPECT_TRUE(wrong_key2.get());
// A wrong key that can be detected by all implementations.
scoped_ptr<crypto::SymmetricKey> wrong_key3(
crypto::SymmetricKey::DeriveKeyFromPassword(
crypto::SymmetricKey::AES, "wrongwordx", "sweetest", 1000, 256));
- EXPECT_TRUE(NULL != wrong_key3.get());
+ EXPECT_TRUE(wrong_key3.get());
crypto::Encryptor encryptor;
// The IV must be exactly as long as the cipher block size.
@@ -112,15 +112,179 @@ TEST(EncryptorTest, DecryptWrongKey) {
EXPECT_FALSE(decryptor3.Decrypt(ciphertext, &decypted));
}
-// CTR mode encryption is only implemented using NSS.
-#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
+namespace {
+
+// From NIST SP 800-38a test cast:
+// - F.5.1 CTR-AES128.Encrypt
+// - F.5.6 CTR-AES256.Encrypt
+// http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
+const unsigned char kAES128CTRKey[] = {
+ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
+ 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
+};
+
+const unsigned char kAES256CTRKey[] = {
+ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
+ 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
+ 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
+ 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
+};
+
+const unsigned char kAESCTRInitCounter[] = {
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
+ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
+};
+
+const unsigned char kAESCTRPlaintext[] = {
+ // Block #1
+ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
+ 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
+ // Block #2
+ 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
+ 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
+ // Block #3
+ 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
+ 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
+ // Block #4
+ 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
+ 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
+};
+
+const unsigned char kAES128CTRCiphertext[] = {
+ // Block #1
+ 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26,
+ 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce,
+ // Block #2
+ 0x98, 0x06, 0xf6, 0x6b, 0x79, 0x70, 0xfd, 0xff,
+ 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff,
+ // Block #3
+ 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e,
+ 0x5b, 0x4f, 0x09, 0x02, 0x0d, 0xb0, 0x3e, 0xab,
+ // Block #4
+ 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1,
+ 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee
+};
+
+const unsigned char kAES256CTRCiphertext[] = {
+ // Block #1
+ 0x60, 0x1e, 0xc3, 0x13, 0x77, 0x57, 0x89, 0xa5,
+ 0xb7, 0xa7, 0xf5, 0x04, 0xbb, 0xf3, 0xd2, 0x28,
+ // Block #2
+ 0xf4, 0x43, 0xe3, 0xca, 0x4d, 0x62, 0xb5, 0x9a,
+ 0xca, 0x84, 0xe9, 0x90, 0xca, 0xca, 0xf5, 0xc5,
+ // Block #3
+ 0x2b, 0x09, 0x30, 0xda, 0xa2, 0x3d, 0xe9, 0x4c,
+ 0xe8, 0x70, 0x17, 0xba, 0x2d, 0x84, 0x98, 0x8d,
+ // Block #4
+ 0xdf, 0xc9, 0xc5, 0x8d, 0xb6, 0x7a, 0xad, 0xa6,
+ 0x13, 0xc2, 0xdd, 0x08, 0x45, 0x79, 0x41, 0xa6
+};
+
+void TestAESCTREncrypt(
+ const unsigned char* key, size_t key_size,
+ const unsigned char* init_counter, size_t init_counter_size,
+ const unsigned char* plaintext, size_t plaintext_size,
+ const unsigned char* ciphertext, size_t ciphertext_size) {
+ std::string key_str(reinterpret_cast<const char*>(key), key_size);
+ scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
+ crypto::SymmetricKey::AES, key_str));
+ ASSERT_TRUE(sym_key.get());
+
+ crypto::Encryptor encryptor;
+ EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));
+
+ base::StringPiece init_counter_str(
+ reinterpret_cast<const char*>(init_counter), init_counter_size);
+ base::StringPiece plaintext_str(
+ reinterpret_cast<const char*>(plaintext), plaintext_size);
+
+ EXPECT_TRUE(encryptor.SetCounter(init_counter_str));
+ std::string encrypted;
+ EXPECT_TRUE(encryptor.Encrypt(plaintext_str, &encrypted));
+
+ EXPECT_EQ(ciphertext_size, encrypted.size());
+ EXPECT_EQ(0, memcmp(encrypted.data(), ciphertext, encrypted.size()));
+
+ std::string decypted;
+ EXPECT_TRUE(encryptor.SetCounter(init_counter_str));
+ EXPECT_TRUE(encryptor.Decrypt(encrypted, &decypted));
+
+ EXPECT_EQ(plaintext_str, decypted);
+}
+
+void TestAESCTRMultipleDecrypt(
+ const unsigned char* key, size_t key_size,
+ const unsigned char* init_counter, size_t init_counter_size,
+ const unsigned char* plaintext, size_t plaintext_size,
+ const unsigned char* ciphertext, size_t ciphertext_size) {
+ std::string key_str(reinterpret_cast<const char*>(key), key_size);
+ scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
+ crypto::SymmetricKey::AES, key_str));
+ ASSERT_TRUE(sym_key.get());
+
+ crypto::Encryptor encryptor;
+ EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));
+
+ // Counter is set only once.
+ EXPECT_TRUE(encryptor.SetCounter(base::StringPiece(
+ reinterpret_cast<const char*>(init_counter), init_counter_size)));
+
+ std::string ciphertext_str(reinterpret_cast<const char*>(ciphertext),
+ ciphertext_size);
+
+ int kTestDecryptSizes[] = { 32, 16, 8 };
+
+ int offset = 0;
+ for (size_t i = 0; i < arraysize(kTestDecryptSizes); ++i) {
+ std::string decypted;
+ size_t len = kTestDecryptSizes[i];
+ EXPECT_TRUE(
+ encryptor.Decrypt(ciphertext_str.substr(offset, len), &decypted));
+ EXPECT_EQ(len, decypted.size());
+ EXPECT_EQ(0, memcmp(decypted.data(), plaintext + offset, len));
+ offset += len;
+ }
+}
+
+} // namespace
+
+TEST(EncryptorTest, EncryptAES128CTR) {
+ TestAESCTREncrypt(
+ kAES128CTRKey, arraysize(kAES128CTRKey),
+ kAESCTRInitCounter, arraysize(kAESCTRInitCounter),
+ kAESCTRPlaintext, arraysize(kAESCTRPlaintext),
+ kAES128CTRCiphertext, arraysize(kAES128CTRCiphertext));
+}
+
+TEST(EncryptorTest, EncryptAES256CTR) {
+ TestAESCTREncrypt(
+ kAES256CTRKey, arraysize(kAES256CTRKey),
+ kAESCTRInitCounter, arraysize(kAESCTRInitCounter),
+ kAESCTRPlaintext, arraysize(kAESCTRPlaintext),
+ kAES256CTRCiphertext, arraysize(kAES256CTRCiphertext));
+}
+
+TEST(EncryptorTest, EncryptAES128CTR_MultipleDecrypt) {
+ TestAESCTRMultipleDecrypt(
+ kAES128CTRKey, arraysize(kAES128CTRKey),
+ kAESCTRInitCounter, arraysize(kAESCTRInitCounter),
+ kAESCTRPlaintext, arraysize(kAESCTRPlaintext),
+ kAES128CTRCiphertext, arraysize(kAES128CTRCiphertext));
+}
+
+TEST(EncryptorTest, EncryptAES256CTR_MultipleDecrypt) {
+ TestAESCTRMultipleDecrypt(
+ kAES256CTRKey, arraysize(kAES256CTRKey),
+ kAESCTRInitCounter, arraysize(kAESCTRInitCounter),
+ kAESCTRPlaintext, arraysize(kAESCTRPlaintext),
+ kAES256CTRCiphertext, arraysize(kAES256CTRCiphertext));
+}
TEST(EncryptorTest, EncryptDecryptCTR) {
scoped_ptr<crypto::SymmetricKey> key(
- crypto::SymmetricKey::GenerateRandomKey(
- crypto::SymmetricKey::AES, 128));
+ crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 128));
- EXPECT_TRUE(NULL != key.get());
+ EXPECT_TRUE(key.get());
const std::string kInitialCounter = "0000000000000000";
crypto::Encryptor encryptor;
@@ -188,8 +352,6 @@ TEST(EncryptorTest, CTRCounter) {
EXPECT_EQ(0, memcmp(buf, kExpect3, kCounterSize));
}
-#endif
-
// TODO(wtc): add more known-answer tests. Test vectors are available from
// http://www.ietf.org/rfc/rfc3602
// http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
@@ -199,17 +361,17 @@ TEST(EncryptorTest, CTRCounter) {
// NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt.
TEST(EncryptorTest, EncryptAES256CBC) {
// From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt.
- static const unsigned char raw_key[] = {
+ static const unsigned char kRawKey[] = {
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
};
- static const unsigned char raw_iv[] = {
+ static const unsigned char kRawIv[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
- static const unsigned char raw_plaintext[] = {
+ static const unsigned char kRawPlaintext[] = {
// Block #1
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
@@ -223,7 +385,7 @@ TEST(EncryptorTest, EncryptAES256CBC) {
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
};
- static const unsigned char raw_ciphertext[] = {
+ static const unsigned char kRawCiphertext[] = {
// Block #1
0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba,
0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
@@ -241,24 +403,24 @@ TEST(EncryptorTest, EncryptAES256CBC) {
0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44
};
- std::string key(reinterpret_cast<const char*>(raw_key), sizeof(raw_key));
+ std::string key(reinterpret_cast<const char*>(kRawKey), sizeof(kRawKey));
scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
crypto::SymmetricKey::AES, key));
- ASSERT_TRUE(NULL != sym_key.get());
+ ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
// The IV must be exactly as long a the cipher block size.
- std::string iv(reinterpret_cast<const char*>(raw_iv), sizeof(raw_iv));
+ std::string iv(reinterpret_cast<const char*>(kRawIv), sizeof(kRawIv));
EXPECT_EQ(16U, iv.size());
EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
- std::string plaintext(reinterpret_cast<const char*>(raw_plaintext),
- sizeof(raw_plaintext));
+ std::string plaintext(reinterpret_cast<const char*>(kRawPlaintext),
+ sizeof(kRawPlaintext));
std::string ciphertext;
EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
- EXPECT_EQ(sizeof(raw_ciphertext), ciphertext.size());
- EXPECT_EQ(0, memcmp(ciphertext.data(), raw_ciphertext, ciphertext.size()));
+ EXPECT_EQ(sizeof(kRawCiphertext), ciphertext.size());
+ EXPECT_EQ(0, memcmp(ciphertext.data(), kRawCiphertext, ciphertext.size()));
std::string decypted;
EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
@@ -277,7 +439,7 @@ TEST(EncryptorTest, EncryptAES128CBCRegression) {
scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
crypto::SymmetricKey::AES, key));
- ASSERT_TRUE(NULL != sym_key.get());
+ ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
// The IV must be exactly as long a the cipher block size.
@@ -303,7 +465,7 @@ TEST(EncryptorTest, EncryptAES192CBCRegression) {
scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
crypto::SymmetricKey::AES, key));
- ASSERT_TRUE(NULL != sym_key.get());
+ ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
// The IV must be exactly as long a the cipher block size.
@@ -328,7 +490,7 @@ TEST(EncryptorTest, UnsupportedKeySize) {
std::string iv = "Sweet Sixteen IV";
scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
crypto::SymmetricKey::AES, key));
- ASSERT_TRUE(NULL != sym_key.get());
+ ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
// The IV must be exactly as long a the cipher block size.
@@ -342,7 +504,7 @@ TEST(EncryptorTest, UnsupportedIV) {
std::string iv = "OnlyForteen :(";
scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
crypto::SymmetricKey::AES, key));
- ASSERT_TRUE(NULL != sym_key.get());
+ ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
@@ -356,7 +518,7 @@ TEST(EncryptorTest, EmptyEncrypt) {
scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
crypto::SymmetricKey::AES, key));
- ASSERT_TRUE(NULL != sym_key.get());
+ ASSERT_TRUE(sym_key.get());
crypto::Encryptor encryptor;
// The IV must be exactly as long a the cipher block size.