encryptor_unittest.cc 19 KB

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