[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/encryptor.h" |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 6 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 9 | #include <memory> |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 10 | #include <string> |
| 11 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 0d8db08 | 2013-06-11 07:27:01 | [diff] [blame] | 13 | #include "base/strings/string_number_conversions.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 14 | #include "crypto/symmetric_key.h" |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 17 | TEST(EncryptorTest, EncryptDecrypt) { |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 18 | std::unique_ptr<crypto::SymmetricKey> key( |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 19 | crypto::SymmetricKey::DeriveKeyFromPassword( |
| 20 | crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 21 | EXPECT_TRUE(key.get()); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 22 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 23 | crypto::Encryptor encryptor; |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 24 | // The IV must be exactly as long as the cipher block size. |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 25 | std::string iv("the iv: 16 bytes"); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 26 | EXPECT_EQ(16U, iv.size()); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 27 | EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 28 | |
| 29 | std::string plaintext("this is the plaintext"); |
| 30 | std::string ciphertext; |
| 31 | EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 32 | |
| 33 | EXPECT_LT(0U, ciphertext.size()); |
| 34 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 35 | std::string decrypted; |
| 36 | EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 37 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 38 | EXPECT_EQ(plaintext, decrypted); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 39 | } |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 40 | |
[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 41 | TEST(EncryptorTest, DecryptWrongKey) { |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 42 | std::unique_ptr<crypto::SymmetricKey> key( |
[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 43 | crypto::SymmetricKey::DeriveKeyFromPassword( |
| 44 | crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 45 | EXPECT_TRUE(key.get()); |
[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 46 | |
[email protected] | 4eac15e | 2012-05-11 03:17:44 | [diff] [blame] | 47 | // A wrong key that can be detected by implementations that validate every |
| 48 | // byte in the padding. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 49 | std::unique_ptr<crypto::SymmetricKey> wrong_key( |
| 50 | crypto::SymmetricKey::DeriveKeyFromPassword( |
| 51 | crypto::SymmetricKey::AES, "wrongword", "sweetest", 1000, 256)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 52 | EXPECT_TRUE(wrong_key.get()); |
[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 53 | |
[email protected] | 4eac15e | 2012-05-11 03:17:44 | [diff] [blame] | 54 | // A wrong key that can't be detected by any implementation. The password |
[email protected] | 31ab866 | 2012-04-27 03:01:09 | [diff] [blame] | 55 | // "wrongword;" would also work. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 56 | std::unique_ptr<crypto::SymmetricKey> wrong_key2( |
| 57 | crypto::SymmetricKey::DeriveKeyFromPassword( |
| 58 | crypto::SymmetricKey::AES, "wrongword+", "sweetest", 1000, 256)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 59 | EXPECT_TRUE(wrong_key2.get()); |
[email protected] | 31ab866 | 2012-04-27 03:01:09 | [diff] [blame] | 60 | |
[email protected] | 4eac15e | 2012-05-11 03:17:44 | [diff] [blame] | 61 | // A wrong key that can be detected by all implementations. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 62 | std::unique_ptr<crypto::SymmetricKey> wrong_key3( |
| 63 | crypto::SymmetricKey::DeriveKeyFromPassword( |
| 64 | crypto::SymmetricKey::AES, "wrongwordx", "sweetest", 1000, 256)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 65 | EXPECT_TRUE(wrong_key3.get()); |
[email protected] | 4eac15e | 2012-05-11 03:17:44 | [diff] [blame] | 66 | |
[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 67 | crypto::Encryptor encryptor; |
| 68 | // The IV must be exactly as long as the cipher block size. |
| 69 | std::string iv("the iv: 16 bytes"); |
| 70 | EXPECT_EQ(16U, iv.size()); |
| 71 | EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv)); |
| 72 | |
| 73 | std::string plaintext("this is the plaintext"); |
| 74 | std::string ciphertext; |
| 75 | EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 76 | |
| 77 | static const unsigned char expected_ciphertext[] = { |
| 78 | 0x7D, 0x67, 0x5B, 0x53, 0xE6, 0xD8, 0x0F, 0x27, |
| 79 | 0x74, 0xB1, 0x90, 0xFE, 0x6E, 0x58, 0x4A, 0xA0, |
| 80 | 0x0E, 0x35, 0xE3, 0x01, 0xC0, 0xFE, 0x9A, 0xD8, |
| 81 | 0x48, 0x1D, 0x42, 0xB0, 0xBA, 0x21, 0xB2, 0x0C |
| 82 | }; |
| 83 | |
| 84 | ASSERT_EQ(arraysize(expected_ciphertext), ciphertext.size()); |
| 85 | for (size_t i = 0; i < ciphertext.size(); ++i) { |
| 86 | ASSERT_EQ(expected_ciphertext[i], |
| 87 | static_cast<unsigned char>(ciphertext[i])); |
| 88 | } |
| 89 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 90 | std::string decrypted; |
[email protected] | 4eac15e | 2012-05-11 03:17:44 | [diff] [blame] | 91 | |
| 92 | // This wrong key causes the last padding byte to be 5, which is a valid |
| 93 | // padding length, and the second to last padding byte to be 137, which is |
| 94 | // invalid. If an implementation simply uses the last padding byte to |
| 95 | // determine the padding length without checking every padding byte, |
| 96 | // Encryptor::Decrypt() will still return true. This is the case for NSS |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 97 | // (crbug.com/124434). |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 98 | #if !defined(USE_NSS_CERTS) && !defined(OS_WIN) && !defined(OS_MACOSX) |
[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 99 | crypto::Encryptor decryptor; |
| 100 | EXPECT_TRUE(decryptor.Init(wrong_key.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 101 | EXPECT_FALSE(decryptor.Decrypt(ciphertext, &decrypted)); |
[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 102 | #endif |
[email protected] | 31ab866 | 2012-04-27 03:01:09 | [diff] [blame] | 103 | |
| 104 | // This demonstrates that not all wrong keys can be detected by padding |
| 105 | // error. This wrong key causes the last padding byte to be 1, which is |
| 106 | // a valid padding block of length 1. |
| 107 | crypto::Encryptor decryptor2; |
| 108 | EXPECT_TRUE(decryptor2.Init(wrong_key2.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 109 | EXPECT_TRUE(decryptor2.Decrypt(ciphertext, &decrypted)); |
[email protected] | 4eac15e | 2012-05-11 03:17:44 | [diff] [blame] | 110 | |
| 111 | // This wrong key causes the last padding byte to be 253, which should be |
| 112 | // rejected by all implementations. |
| 113 | crypto::Encryptor decryptor3; |
| 114 | EXPECT_TRUE(decryptor3.Init(wrong_key3.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 115 | EXPECT_FALSE(decryptor3.Decrypt(ciphertext, &decrypted)); |
[email protected] | de03988 | 2012-04-23 23:51:43 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 118 | namespace { |
| 119 | |
| 120 | // From NIST SP 800-38a test cast: |
| 121 | // - F.5.1 CTR-AES128.Encrypt |
| 122 | // - F.5.6 CTR-AES256.Encrypt |
| 123 | // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 124 | const unsigned char kAES128CTRKey[] = { |
| 125 | 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
| 126 | 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c |
| 127 | }; |
| 128 | |
| 129 | const unsigned char kAES256CTRKey[] = { |
| 130 | 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, |
| 131 | 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, |
| 132 | 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, |
| 133 | 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 |
| 134 | }; |
| 135 | |
| 136 | const unsigned char kAESCTRInitCounter[] = { |
| 137 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
| 138 | 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff |
| 139 | }; |
| 140 | |
| 141 | const unsigned char kAESCTRPlaintext[] = { |
| 142 | // Block #1 |
| 143 | 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
| 144 | 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
| 145 | // Block #2 |
| 146 | 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, |
| 147 | 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, |
| 148 | // Block #3 |
| 149 | 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, |
| 150 | 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, |
| 151 | // Block #4 |
| 152 | 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, |
| 153 | 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 |
| 154 | }; |
| 155 | |
| 156 | const unsigned char kAES128CTRCiphertext[] = { |
| 157 | // Block #1 |
| 158 | 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, |
| 159 | 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, |
| 160 | // Block #2 |
| 161 | 0x98, 0x06, 0xf6, 0x6b, 0x79, 0x70, 0xfd, 0xff, |
| 162 | 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff, |
| 163 | // Block #3 |
| 164 | 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, |
| 165 | 0x5b, 0x4f, 0x09, 0x02, 0x0d, 0xb0, 0x3e, 0xab, |
| 166 | // Block #4 |
| 167 | 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1, |
| 168 | 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee |
| 169 | }; |
| 170 | |
| 171 | const unsigned char kAES256CTRCiphertext[] = { |
| 172 | // Block #1 |
| 173 | 0x60, 0x1e, 0xc3, 0x13, 0x77, 0x57, 0x89, 0xa5, |
| 174 | 0xb7, 0xa7, 0xf5, 0x04, 0xbb, 0xf3, 0xd2, 0x28, |
| 175 | // Block #2 |
| 176 | 0xf4, 0x43, 0xe3, 0xca, 0x4d, 0x62, 0xb5, 0x9a, |
| 177 | 0xca, 0x84, 0xe9, 0x90, 0xca, 0xca, 0xf5, 0xc5, |
| 178 | // Block #3 |
| 179 | 0x2b, 0x09, 0x30, 0xda, 0xa2, 0x3d, 0xe9, 0x4c, |
| 180 | 0xe8, 0x70, 0x17, 0xba, 0x2d, 0x84, 0x98, 0x8d, |
| 181 | // Block #4 |
| 182 | 0xdf, 0xc9, 0xc5, 0x8d, 0xb6, 0x7a, 0xad, 0xa6, |
| 183 | 0x13, 0xc2, 0xdd, 0x08, 0x45, 0x79, 0x41, 0xa6 |
| 184 | }; |
| 185 | |
| 186 | void TestAESCTREncrypt( |
| 187 | const unsigned char* key, size_t key_size, |
| 188 | const unsigned char* init_counter, size_t init_counter_size, |
| 189 | const unsigned char* plaintext, size_t plaintext_size, |
| 190 | const unsigned char* ciphertext, size_t ciphertext_size) { |
| 191 | std::string key_str(reinterpret_cast<const char*>(key), key_size); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 192 | std::unique_ptr<crypto::SymmetricKey> sym_key( |
| 193 | crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key_str)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 194 | ASSERT_TRUE(sym_key.get()); |
| 195 | |
| 196 | crypto::Encryptor encryptor; |
| 197 | EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, "")); |
| 198 | |
| 199 | base::StringPiece init_counter_str( |
| 200 | reinterpret_cast<const char*>(init_counter), init_counter_size); |
| 201 | base::StringPiece plaintext_str( |
| 202 | reinterpret_cast<const char*>(plaintext), plaintext_size); |
| 203 | |
| 204 | EXPECT_TRUE(encryptor.SetCounter(init_counter_str)); |
| 205 | std::string encrypted; |
| 206 | EXPECT_TRUE(encryptor.Encrypt(plaintext_str, &encrypted)); |
| 207 | |
| 208 | EXPECT_EQ(ciphertext_size, encrypted.size()); |
| 209 | EXPECT_EQ(0, memcmp(encrypted.data(), ciphertext, encrypted.size())); |
| 210 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 211 | std::string decrypted; |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 212 | EXPECT_TRUE(encryptor.SetCounter(init_counter_str)); |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 213 | EXPECT_TRUE(encryptor.Decrypt(encrypted, &decrypted)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 214 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 215 | EXPECT_EQ(plaintext_str, decrypted); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void TestAESCTRMultipleDecrypt( |
| 219 | const unsigned char* key, size_t key_size, |
| 220 | const unsigned char* init_counter, size_t init_counter_size, |
| 221 | const unsigned char* plaintext, size_t plaintext_size, |
| 222 | const unsigned char* ciphertext, size_t ciphertext_size) { |
| 223 | std::string key_str(reinterpret_cast<const char*>(key), key_size); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 224 | std::unique_ptr<crypto::SymmetricKey> sym_key( |
| 225 | crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key_str)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 226 | ASSERT_TRUE(sym_key.get()); |
| 227 | |
| 228 | crypto::Encryptor encryptor; |
| 229 | EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, "")); |
| 230 | |
| 231 | // Counter is set only once. |
| 232 | EXPECT_TRUE(encryptor.SetCounter(base::StringPiece( |
| 233 | reinterpret_cast<const char*>(init_counter), init_counter_size))); |
| 234 | |
| 235 | std::string ciphertext_str(reinterpret_cast<const char*>(ciphertext), |
| 236 | ciphertext_size); |
| 237 | |
| 238 | int kTestDecryptSizes[] = { 32, 16, 8 }; |
| 239 | |
| 240 | int offset = 0; |
| 241 | for (size_t i = 0; i < arraysize(kTestDecryptSizes); ++i) { |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 242 | std::string decrypted; |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 243 | size_t len = kTestDecryptSizes[i]; |
| 244 | EXPECT_TRUE( |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 245 | encryptor.Decrypt(ciphertext_str.substr(offset, len), &decrypted)); |
| 246 | EXPECT_EQ(len, decrypted.size()); |
| 247 | EXPECT_EQ(0, memcmp(decrypted.data(), plaintext + offset, len)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 248 | offset += len; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | } // namespace |
| 253 | |
| 254 | TEST(EncryptorTest, EncryptAES128CTR) { |
| 255 | TestAESCTREncrypt( |
| 256 | kAES128CTRKey, arraysize(kAES128CTRKey), |
| 257 | kAESCTRInitCounter, arraysize(kAESCTRInitCounter), |
| 258 | kAESCTRPlaintext, arraysize(kAESCTRPlaintext), |
| 259 | kAES128CTRCiphertext, arraysize(kAES128CTRCiphertext)); |
| 260 | } |
| 261 | |
| 262 | TEST(EncryptorTest, EncryptAES256CTR) { |
| 263 | TestAESCTREncrypt( |
| 264 | kAES256CTRKey, arraysize(kAES256CTRKey), |
| 265 | kAESCTRInitCounter, arraysize(kAESCTRInitCounter), |
| 266 | kAESCTRPlaintext, arraysize(kAESCTRPlaintext), |
| 267 | kAES256CTRCiphertext, arraysize(kAES256CTRCiphertext)); |
| 268 | } |
| 269 | |
| 270 | TEST(EncryptorTest, EncryptAES128CTR_MultipleDecrypt) { |
| 271 | TestAESCTRMultipleDecrypt( |
| 272 | kAES128CTRKey, arraysize(kAES128CTRKey), |
| 273 | kAESCTRInitCounter, arraysize(kAESCTRInitCounter), |
| 274 | kAESCTRPlaintext, arraysize(kAESCTRPlaintext), |
| 275 | kAES128CTRCiphertext, arraysize(kAES128CTRCiphertext)); |
| 276 | } |
| 277 | |
| 278 | TEST(EncryptorTest, EncryptAES256CTR_MultipleDecrypt) { |
| 279 | TestAESCTRMultipleDecrypt( |
| 280 | kAES256CTRKey, arraysize(kAES256CTRKey), |
| 281 | kAESCTRInitCounter, arraysize(kAESCTRInitCounter), |
| 282 | kAESCTRPlaintext, arraysize(kAESCTRPlaintext), |
| 283 | kAES256CTRCiphertext, arraysize(kAES256CTRCiphertext)); |
| 284 | } |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 285 | |
| 286 | TEST(EncryptorTest, EncryptDecryptCTR) { |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 287 | std::unique_ptr<crypto::SymmetricKey> key( |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 288 | crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 128)); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 289 | |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 290 | EXPECT_TRUE(key.get()); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 291 | const std::string kInitialCounter = "0000000000000000"; |
| 292 | |
| 293 | crypto::Encryptor encryptor; |
| 294 | EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CTR, "")); |
| 295 | EXPECT_TRUE(encryptor.SetCounter(kInitialCounter)); |
| 296 | |
| 297 | std::string plaintext("normal plaintext of random length"); |
| 298 | std::string ciphertext; |
| 299 | EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 300 | EXPECT_LT(0U, ciphertext.size()); |
| 301 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 302 | std::string decrypted; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 303 | EXPECT_TRUE(encryptor.SetCounter(kInitialCounter)); |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 304 | EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); |
| 305 | EXPECT_EQ(plaintext, decrypted); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 306 | |
| 307 | plaintext = "0123456789012345"; |
| 308 | EXPECT_TRUE(encryptor.SetCounter(kInitialCounter)); |
| 309 | EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 310 | EXPECT_LT(0U, ciphertext.size()); |
| 311 | |
| 312 | EXPECT_TRUE(encryptor.SetCounter(kInitialCounter)); |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 313 | EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); |
| 314 | EXPECT_EQ(plaintext, decrypted); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | TEST(EncryptorTest, CTRCounter) { |
| 318 | const int kCounterSize = 16; |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 319 | const unsigned char kTest1[] = |
| 320 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 321 | unsigned char buf[16]; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 322 | |
| 323 | // Increment 10 times. |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 324 | crypto::Encryptor::Counter counter1( |
| 325 | std::string(reinterpret_cast<const char*>(kTest1), kCounterSize)); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 326 | for (int i = 0; i < 10; ++i) |
| 327 | counter1.Increment(); |
| 328 | counter1.Write(buf); |
| 329 | EXPECT_EQ(0, memcmp(buf, kTest1, 15)); |
| 330 | EXPECT_TRUE(buf[15] == 10); |
| 331 | |
| 332 | // Check corner cases. |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 333 | const unsigned char kTest2[] = { |
| 334 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 335 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
| 336 | }; |
| 337 | const unsigned char kExpect2[] = |
| 338 | {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 339 | crypto::Encryptor::Counter counter2( |
| 340 | std::string(reinterpret_cast<const char*>(kTest2), kCounterSize)); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 341 | counter2.Increment(); |
| 342 | counter2.Write(buf); |
| 343 | EXPECT_EQ(0, memcmp(buf, kExpect2, kCounterSize)); |
| 344 | |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 345 | const unsigned char kTest3[] = { |
| 346 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 347 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
| 348 | }; |
| 349 | const unsigned char kExpect3[] = |
| 350 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 351 | crypto::Encryptor::Counter counter3( |
| 352 | std::string(reinterpret_cast<const char*>(kTest3), kCounterSize)); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 353 | counter3.Increment(); |
| 354 | counter3.Write(buf); |
| 355 | EXPECT_EQ(0, memcmp(buf, kExpect3, kCounterSize)); |
| 356 | } |
| 357 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 358 | // TODO(wtc): add more known-answer tests. Test vectors are available from |
| 359 | // http://www.ietf.org/rfc/rfc3602 |
| 360 | // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 361 | // http://gladman.plushost.co.uk/oldsite/AES/index.php |
| 362 | // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip |
| 363 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 364 | // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt. |
| 365 | TEST(EncryptorTest, EncryptAES256CBC) { |
| 366 | // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt. |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 367 | static const unsigned char kRawKey[] = { |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 368 | 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, |
| 369 | 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, |
| 370 | 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, |
| 371 | 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 |
| 372 | }; |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 373 | static const unsigned char kRawIv[] = { |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 374 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 375 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 376 | }; |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 377 | static const unsigned char kRawPlaintext[] = { |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 378 | // Block #1 |
| 379 | 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
| 380 | 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
| 381 | // Block #2 |
| 382 | 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, |
| 383 | 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, |
| 384 | // Block #3 |
| 385 | 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, |
| 386 | 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, |
| 387 | // Block #4 |
| 388 | 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, |
| 389 | 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10, |
| 390 | }; |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 391 | static const unsigned char kRawCiphertext[] = { |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 392 | // Block #1 |
| 393 | 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, |
| 394 | 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6, |
| 395 | // Block #2 |
| 396 | 0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, |
| 397 | 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d, |
| 398 | // Block #3 |
| 399 | 0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, |
| 400 | 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61, |
| 401 | // Block #4 |
| 402 | 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, |
| 403 | 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b, |
| 404 | // PKCS #5 padding, encrypted. |
| 405 | 0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2, |
| 406 | 0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44 |
| 407 | }; |
| 408 | |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 409 | std::string key(reinterpret_cast<const char*>(kRawKey), sizeof(kRawKey)); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 410 | std::unique_ptr<crypto::SymmetricKey> sym_key( |
| 411 | crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 412 | ASSERT_TRUE(sym_key.get()); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 413 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 414 | crypto::Encryptor encryptor; |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 415 | // The IV must be exactly as long a the cipher block size. |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 416 | std::string iv(reinterpret_cast<const char*>(kRawIv), sizeof(kRawIv)); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 417 | EXPECT_EQ(16U, iv.size()); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 418 | EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 419 | |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 420 | std::string plaintext(reinterpret_cast<const char*>(kRawPlaintext), |
| 421 | sizeof(kRawPlaintext)); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 422 | std::string ciphertext; |
| 423 | EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 424 | |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 425 | EXPECT_EQ(sizeof(kRawCiphertext), ciphertext.size()); |
| 426 | EXPECT_EQ(0, memcmp(ciphertext.data(), kRawCiphertext, ciphertext.size())); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 427 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 428 | std::string decrypted; |
| 429 | EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 430 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 431 | EXPECT_EQ(plaintext, decrypted); |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 432 | } |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 433 | |
| 434 | // Expected output derived from the NSS implementation. |
| 435 | TEST(EncryptorTest, EncryptAES128CBCRegression) { |
| 436 | std::string key = "128=SixteenBytes"; |
| 437 | std::string iv = "Sweet Sixteen IV"; |
| 438 | std::string plaintext = "Plain text with a g-clef U+1D11E \360\235\204\236"; |
| 439 | std::string expected_ciphertext_hex = |
| 440 | "D4A67A0BA33C30F207344D81D1E944BBE65587C3D7D9939A" |
| 441 | "C070C62B9C15A3EA312EA4AD1BC7929F4D3C16B03AD5ADA8"; |
| 442 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 443 | std::unique_ptr<crypto::SymmetricKey> sym_key( |
| 444 | crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 445 | ASSERT_TRUE(sym_key.get()); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 446 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 447 | crypto::Encryptor encryptor; |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 448 | // The IV must be exactly as long a the cipher block size. |
| 449 | EXPECT_EQ(16U, iv.size()); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 450 | EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 451 | |
| 452 | std::string ciphertext; |
| 453 | EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 454 | EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
| 455 | ciphertext.size())); |
| 456 | |
[email protected] | bd2d54c | 2013-09-30 09:08:47 | [diff] [blame] | 457 | std::string decrypted; |
| 458 | EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); |
| 459 | EXPECT_EQ(plaintext, decrypted); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 460 | } |
| 461 | |
[email protected] | 1970276 | 2014-07-31 01:03:05 | [diff] [blame] | 462 | // Symmetric keys with an unsupported size should be rejected. Whether they are |
| 463 | // rejected by SymmetricKey::Import or Encryptor::Init depends on the platform. |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 464 | TEST(EncryptorTest, UnsupportedKeySize) { |
| 465 | std::string key = "7 = bad"; |
| 466 | std::string iv = "Sweet Sixteen IV"; |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 467 | std::unique_ptr<crypto::SymmetricKey> sym_key( |
| 468 | crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key)); |
[email protected] | 1970276 | 2014-07-31 01:03:05 | [diff] [blame] | 469 | if (!sym_key.get()) |
| 470 | return; |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 471 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 472 | crypto::Encryptor encryptor; |
[email protected] | 1970276 | 2014-07-31 01:03:05 | [diff] [blame] | 473 | // The IV must be exactly as long as the cipher block size. |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 474 | EXPECT_EQ(16U, iv.size()); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 475 | EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 476 | } |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 477 | |
| 478 | TEST(EncryptorTest, UnsupportedIV) { |
| 479 | std::string key = "128=SixteenBytes"; |
| 480 | std::string iv = "OnlyForteen :("; |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 481 | std::unique_ptr<crypto::SymmetricKey> sym_key( |
| 482 | crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 483 | ASSERT_TRUE(sym_key.get()); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 484 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 485 | crypto::Encryptor encryptor; |
| 486 | EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | TEST(EncryptorTest, EmptyEncrypt) { |
| 490 | std::string key = "128=SixteenBytes"; |
| 491 | std::string iv = "Sweet Sixteen IV"; |
| 492 | std::string plaintext; |
| 493 | std::string expected_ciphertext_hex = "8518B8878D34E7185E300D0FCC426396"; |
| 494 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 495 | std::unique_ptr<crypto::SymmetricKey> sym_key( |
| 496 | crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key)); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 497 | ASSERT_TRUE(sym_key.get()); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 498 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 499 | crypto::Encryptor encryptor; |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 500 | // The IV must be exactly as long a the cipher block size. |
| 501 | EXPECT_EQ(16U, iv.size()); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 502 | EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 503 | |
| 504 | std::string ciphertext; |
| 505 | EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 506 | EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
| 507 | ciphertext.size())); |
| 508 | } |
[email protected] | 1a39c7b | 2013-10-01 10:34:27 | [diff] [blame] | 509 | |
| 510 | TEST(EncryptorTest, CipherTextNotMultipleOfBlockSize) { |
| 511 | std::string key = "128=SixteenBytes"; |
| 512 | std::string iv = "Sweet Sixteen IV"; |
| 513 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 514 | std::unique_ptr<crypto::SymmetricKey> sym_key( |
| 515 | crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key)); |
[email protected] | 1a39c7b | 2013-10-01 10:34:27 | [diff] [blame] | 516 | ASSERT_TRUE(sym_key.get()); |
| 517 | |
| 518 | crypto::Encryptor encryptor; |
| 519 | // The IV must be exactly as long a the cipher block size. |
| 520 | EXPECT_EQ(16U, iv.size()); |
| 521 | EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
| 522 | |
| 523 | // Use a separately allocated array to improve the odds of the memory tools |
| 524 | // catching invalid accesses. |
| 525 | // |
| 526 | // Otherwise when using std::string as the other tests do, accesses several |
| 527 | // bytes off the end of the buffer may fall inside the reservation of |
| 528 | // the string and not be detected. |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame^] | 529 | std::unique_ptr<char[]> ciphertext(new char[1]); |
[email protected] | 1a39c7b | 2013-10-01 10:34:27 | [diff] [blame] | 530 | |
| 531 | std::string plaintext; |
| 532 | EXPECT_FALSE( |
| 533 | encryptor.Decrypt(base::StringPiece(ciphertext.get(), 1), &plaintext)); |
| 534 | } |