encryptor_unittest.cc 21 KB

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