Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(850)

Side by Side Diff: crypto/encryptor_unittest.cc

Issue 1870233002: Convert crypto to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/encryptor.h ('k') | crypto/hkdf.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "crypto/encryptor.h" 5 #include "crypto/encryptor.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory>
9 #include <string> 10 #include <string>
10 11
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "crypto/symmetric_key.h" 14 #include "crypto/symmetric_key.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 TEST(EncryptorTest, EncryptDecrypt) { 17 TEST(EncryptorTest, EncryptDecrypt) {
18 scoped_ptr<crypto::SymmetricKey> key( 18 std::unique_ptr<crypto::SymmetricKey> key(
19 crypto::SymmetricKey::DeriveKeyFromPassword( 19 crypto::SymmetricKey::DeriveKeyFromPassword(
20 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)); 20 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
21 EXPECT_TRUE(key.get()); 21 EXPECT_TRUE(key.get());
22 22
23 crypto::Encryptor encryptor; 23 crypto::Encryptor encryptor;
24 // The IV must be exactly as long as the cipher block size. 24 // The IV must be exactly as long as the cipher block size.
25 std::string iv("the iv: 16 bytes"); 25 std::string iv("the iv: 16 bytes");
26 EXPECT_EQ(16U, iv.size()); 26 EXPECT_EQ(16U, iv.size());
27 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv)); 27 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv));
28 28
29 std::string plaintext("this is the plaintext"); 29 std::string plaintext("this is the plaintext");
30 std::string ciphertext; 30 std::string ciphertext;
31 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); 31 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
32 32
33 EXPECT_LT(0U, ciphertext.size()); 33 EXPECT_LT(0U, ciphertext.size());
34 34
35 std::string decrypted; 35 std::string decrypted;
36 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); 36 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
37 37
38 EXPECT_EQ(plaintext, decrypted); 38 EXPECT_EQ(plaintext, decrypted);
39 } 39 }
40 40
41 TEST(EncryptorTest, DecryptWrongKey) { 41 TEST(EncryptorTest, DecryptWrongKey) {
42 scoped_ptr<crypto::SymmetricKey> key( 42 std::unique_ptr<crypto::SymmetricKey> key(
43 crypto::SymmetricKey::DeriveKeyFromPassword( 43 crypto::SymmetricKey::DeriveKeyFromPassword(
44 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)); 44 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
45 EXPECT_TRUE(key.get()); 45 EXPECT_TRUE(key.get());
46 46
47 // A wrong key that can be detected by implementations that validate every 47 // A wrong key that can be detected by implementations that validate every
48 // byte in the padding. 48 // byte in the padding.
49 scoped_ptr<crypto::SymmetricKey> wrong_key( 49 std::unique_ptr<crypto::SymmetricKey> wrong_key(
50 crypto::SymmetricKey::DeriveKeyFromPassword( 50 crypto::SymmetricKey::DeriveKeyFromPassword(
51 crypto::SymmetricKey::AES, "wrongword", "sweetest", 1000, 256)); 51 crypto::SymmetricKey::AES, "wrongword", "sweetest", 1000, 256));
52 EXPECT_TRUE(wrong_key.get()); 52 EXPECT_TRUE(wrong_key.get());
53 53
54 // A wrong key that can't be detected by any implementation. The password 54 // A wrong key that can't be detected by any implementation. The password
55 // "wrongword;" would also work. 55 // "wrongword;" would also work.
56 scoped_ptr<crypto::SymmetricKey> wrong_key2( 56 std::unique_ptr<crypto::SymmetricKey> wrong_key2(
57 crypto::SymmetricKey::DeriveKeyFromPassword( 57 crypto::SymmetricKey::DeriveKeyFromPassword(
58 crypto::SymmetricKey::AES, "wrongword+", "sweetest", 1000, 256)); 58 crypto::SymmetricKey::AES, "wrongword+", "sweetest", 1000, 256));
59 EXPECT_TRUE(wrong_key2.get()); 59 EXPECT_TRUE(wrong_key2.get());
60 60
61 // A wrong key that can be detected by all implementations. 61 // A wrong key that can be detected by all implementations.
62 scoped_ptr<crypto::SymmetricKey> wrong_key3( 62 std::unique_ptr<crypto::SymmetricKey> wrong_key3(
63 crypto::SymmetricKey::DeriveKeyFromPassword( 63 crypto::SymmetricKey::DeriveKeyFromPassword(
64 crypto::SymmetricKey::AES, "wrongwordx", "sweetest", 1000, 256)); 64 crypto::SymmetricKey::AES, "wrongwordx", "sweetest", 1000, 256));
65 EXPECT_TRUE(wrong_key3.get()); 65 EXPECT_TRUE(wrong_key3.get());
66 66
67 crypto::Encryptor encryptor; 67 crypto::Encryptor encryptor;
68 // The IV must be exactly as long as the cipher block size. 68 // The IV must be exactly as long as the cipher block size.
69 std::string iv("the iv: 16 bytes"); 69 std::string iv("the iv: 16 bytes");
70 EXPECT_EQ(16U, iv.size()); 70 EXPECT_EQ(16U, iv.size());
71 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv)); 71 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv));
72 72
73 std::string plaintext("this is the plaintext"); 73 std::string plaintext("this is the plaintext");
74 std::string ciphertext; 74 std::string ciphertext;
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 0xdf, 0xc9, 0xc5, 0x8d, 0xb6, 0x7a, 0xad, 0xa6, 182 0xdf, 0xc9, 0xc5, 0x8d, 0xb6, 0x7a, 0xad, 0xa6,
183 0x13, 0xc2, 0xdd, 0x08, 0x45, 0x79, 0x41, 0xa6 183 0x13, 0xc2, 0xdd, 0x08, 0x45, 0x79, 0x41, 0xa6
184 }; 184 };
185 185
186 void TestAESCTREncrypt( 186 void TestAESCTREncrypt(
187 const unsigned char* key, size_t key_size, 187 const unsigned char* key, size_t key_size,
188 const unsigned char* init_counter, size_t init_counter_size, 188 const unsigned char* init_counter, size_t init_counter_size,
189 const unsigned char* plaintext, size_t plaintext_size, 189 const unsigned char* plaintext, size_t plaintext_size,
190 const unsigned char* ciphertext, size_t ciphertext_size) { 190 const unsigned char* ciphertext, size_t ciphertext_size) {
191 std::string key_str(reinterpret_cast<const char*>(key), key_size); 191 std::string key_str(reinterpret_cast<const char*>(key), key_size);
192 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( 192 std::unique_ptr<crypto::SymmetricKey> sym_key(
193 crypto::SymmetricKey::AES, key_str)); 193 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key_str));
194 ASSERT_TRUE(sym_key.get()); 194 ASSERT_TRUE(sym_key.get());
195 195
196 crypto::Encryptor encryptor; 196 crypto::Encryptor encryptor;
197 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, "")); 197 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));
198 198
199 base::StringPiece init_counter_str( 199 base::StringPiece init_counter_str(
200 reinterpret_cast<const char*>(init_counter), init_counter_size); 200 reinterpret_cast<const char*>(init_counter), init_counter_size);
201 base::StringPiece plaintext_str( 201 base::StringPiece plaintext_str(
202 reinterpret_cast<const char*>(plaintext), plaintext_size); 202 reinterpret_cast<const char*>(plaintext), plaintext_size);
203 203
(...skipping 10 matching lines...) Expand all
214 214
215 EXPECT_EQ(plaintext_str, decrypted); 215 EXPECT_EQ(plaintext_str, decrypted);
216 } 216 }
217 217
218 void TestAESCTRMultipleDecrypt( 218 void TestAESCTRMultipleDecrypt(
219 const unsigned char* key, size_t key_size, 219 const unsigned char* key, size_t key_size,
220 const unsigned char* init_counter, size_t init_counter_size, 220 const unsigned char* init_counter, size_t init_counter_size,
221 const unsigned char* plaintext, size_t plaintext_size, 221 const unsigned char* plaintext, size_t plaintext_size,
222 const unsigned char* ciphertext, size_t ciphertext_size) { 222 const unsigned char* ciphertext, size_t ciphertext_size) {
223 std::string key_str(reinterpret_cast<const char*>(key), key_size); 223 std::string key_str(reinterpret_cast<const char*>(key), key_size);
224 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( 224 std::unique_ptr<crypto::SymmetricKey> sym_key(
225 crypto::SymmetricKey::AES, key_str)); 225 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key_str));
226 ASSERT_TRUE(sym_key.get()); 226 ASSERT_TRUE(sym_key.get());
227 227
228 crypto::Encryptor encryptor; 228 crypto::Encryptor encryptor;
229 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, "")); 229 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, ""));
230 230
231 // Counter is set only once. 231 // Counter is set only once.
232 EXPECT_TRUE(encryptor.SetCounter(base::StringPiece( 232 EXPECT_TRUE(encryptor.SetCounter(base::StringPiece(
233 reinterpret_cast<const char*>(init_counter), init_counter_size))); 233 reinterpret_cast<const char*>(init_counter), init_counter_size)));
234 234
235 std::string ciphertext_str(reinterpret_cast<const char*>(ciphertext), 235 std::string ciphertext_str(reinterpret_cast<const char*>(ciphertext),
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 277
278 TEST(EncryptorTest, EncryptAES256CTR_MultipleDecrypt) { 278 TEST(EncryptorTest, EncryptAES256CTR_MultipleDecrypt) {
279 TestAESCTRMultipleDecrypt( 279 TestAESCTRMultipleDecrypt(
280 kAES256CTRKey, arraysize(kAES256CTRKey), 280 kAES256CTRKey, arraysize(kAES256CTRKey),
281 kAESCTRInitCounter, arraysize(kAESCTRInitCounter), 281 kAESCTRInitCounter, arraysize(kAESCTRInitCounter),
282 kAESCTRPlaintext, arraysize(kAESCTRPlaintext), 282 kAESCTRPlaintext, arraysize(kAESCTRPlaintext),
283 kAES256CTRCiphertext, arraysize(kAES256CTRCiphertext)); 283 kAES256CTRCiphertext, arraysize(kAES256CTRCiphertext));
284 } 284 }
285 285
286 TEST(EncryptorTest, EncryptDecryptCTR) { 286 TEST(EncryptorTest, EncryptDecryptCTR) {
287 scoped_ptr<crypto::SymmetricKey> key( 287 std::unique_ptr<crypto::SymmetricKey> key(
288 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 128)); 288 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 128));
289 289
290 EXPECT_TRUE(key.get()); 290 EXPECT_TRUE(key.get());
291 const std::string kInitialCounter = "0000000000000000"; 291 const std::string kInitialCounter = "0000000000000000";
292 292
293 crypto::Encryptor encryptor; 293 crypto::Encryptor encryptor;
294 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CTR, "")); 294 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CTR, ""));
295 EXPECT_TRUE(encryptor.SetCounter(kInitialCounter)); 295 EXPECT_TRUE(encryptor.SetCounter(kInitialCounter));
296 296
297 std::string plaintext("normal plaintext of random length"); 297 std::string plaintext("normal plaintext of random length");
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61, 400 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
401 // Block #4 401 // Block #4
402 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, 402 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc,
403 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b, 403 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b,
404 // PKCS #5 padding, encrypted. 404 // PKCS #5 padding, encrypted.
405 0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2, 405 0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2,
406 0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44 406 0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44
407 }; 407 };
408 408
409 std::string key(reinterpret_cast<const char*>(kRawKey), sizeof(kRawKey)); 409 std::string key(reinterpret_cast<const char*>(kRawKey), sizeof(kRawKey));
410 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( 410 std::unique_ptr<crypto::SymmetricKey> sym_key(
411 crypto::SymmetricKey::AES, key)); 411 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
412 ASSERT_TRUE(sym_key.get()); 412 ASSERT_TRUE(sym_key.get());
413 413
414 crypto::Encryptor encryptor; 414 crypto::Encryptor encryptor;
415 // The IV must be exactly as long a the cipher block size. 415 // The IV must be exactly as long a the cipher block size.
416 std::string iv(reinterpret_cast<const char*>(kRawIv), sizeof(kRawIv)); 416 std::string iv(reinterpret_cast<const char*>(kRawIv), sizeof(kRawIv));
417 EXPECT_EQ(16U, iv.size()); 417 EXPECT_EQ(16U, iv.size());
418 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); 418 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
419 419
420 std::string plaintext(reinterpret_cast<const char*>(kRawPlaintext), 420 std::string plaintext(reinterpret_cast<const char*>(kRawPlaintext),
421 sizeof(kRawPlaintext)); 421 sizeof(kRawPlaintext));
(...skipping 11 matching lines...) Expand all
433 433
434 // Expected output derived from the NSS implementation. 434 // Expected output derived from the NSS implementation.
435 TEST(EncryptorTest, EncryptAES128CBCRegression) { 435 TEST(EncryptorTest, EncryptAES128CBCRegression) {
436 std::string key = "128=SixteenBytes"; 436 std::string key = "128=SixteenBytes";
437 std::string iv = "Sweet Sixteen IV"; 437 std::string iv = "Sweet Sixteen IV";
438 std::string plaintext = "Plain text with a g-clef U+1D11E \360\235\204\236"; 438 std::string plaintext = "Plain text with a g-clef U+1D11E \360\235\204\236";
439 std::string expected_ciphertext_hex = 439 std::string expected_ciphertext_hex =
440 "D4A67A0BA33C30F207344D81D1E944BBE65587C3D7D9939A" 440 "D4A67A0BA33C30F207344D81D1E944BBE65587C3D7D9939A"
441 "C070C62B9C15A3EA312EA4AD1BC7929F4D3C16B03AD5ADA8"; 441 "C070C62B9C15A3EA312EA4AD1BC7929F4D3C16B03AD5ADA8";
442 442
443 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( 443 std::unique_ptr<crypto::SymmetricKey> sym_key(
444 crypto::SymmetricKey::AES, key)); 444 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
445 ASSERT_TRUE(sym_key.get()); 445 ASSERT_TRUE(sym_key.get());
446 446
447 crypto::Encryptor encryptor; 447 crypto::Encryptor encryptor;
448 // The IV must be exactly as long a the cipher block size. 448 // The IV must be exactly as long a the cipher block size.
449 EXPECT_EQ(16U, iv.size()); 449 EXPECT_EQ(16U, iv.size());
450 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); 450 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
451 451
452 std::string ciphertext; 452 std::string ciphertext;
453 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); 453 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
454 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), 454 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(),
455 ciphertext.size())); 455 ciphertext.size()));
456 456
457 std::string decrypted; 457 std::string decrypted;
458 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); 458 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted));
459 EXPECT_EQ(plaintext, decrypted); 459 EXPECT_EQ(plaintext, decrypted);
460 } 460 }
461 461
462 // Symmetric keys with an unsupported size should be rejected. Whether they are 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. 463 // rejected by SymmetricKey::Import or Encryptor::Init depends on the platform.
464 TEST(EncryptorTest, UnsupportedKeySize) { 464 TEST(EncryptorTest, UnsupportedKeySize) {
465 std::string key = "7 = bad"; 465 std::string key = "7 = bad";
466 std::string iv = "Sweet Sixteen IV"; 466 std::string iv = "Sweet Sixteen IV";
467 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( 467 std::unique_ptr<crypto::SymmetricKey> sym_key(
468 crypto::SymmetricKey::AES, key)); 468 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
469 if (!sym_key.get()) 469 if (!sym_key.get())
470 return; 470 return;
471 471
472 crypto::Encryptor encryptor; 472 crypto::Encryptor encryptor;
473 // The IV must be exactly as long as the cipher block size. 473 // The IV must be exactly as long as the cipher block size.
474 EXPECT_EQ(16U, iv.size()); 474 EXPECT_EQ(16U, iv.size());
475 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); 475 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
476 } 476 }
477 477
478 TEST(EncryptorTest, UnsupportedIV) { 478 TEST(EncryptorTest, UnsupportedIV) {
479 std::string key = "128=SixteenBytes"; 479 std::string key = "128=SixteenBytes";
480 std::string iv = "OnlyForteen :("; 480 std::string iv = "OnlyForteen :(";
481 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( 481 std::unique_ptr<crypto::SymmetricKey> sym_key(
482 crypto::SymmetricKey::AES, key)); 482 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
483 ASSERT_TRUE(sym_key.get()); 483 ASSERT_TRUE(sym_key.get());
484 484
485 crypto::Encryptor encryptor; 485 crypto::Encryptor encryptor;
486 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); 486 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
487 } 487 }
488 488
489 TEST(EncryptorTest, EmptyEncrypt) { 489 TEST(EncryptorTest, EmptyEncrypt) {
490 std::string key = "128=SixteenBytes"; 490 std::string key = "128=SixteenBytes";
491 std::string iv = "Sweet Sixteen IV"; 491 std::string iv = "Sweet Sixteen IV";
492 std::string plaintext; 492 std::string plaintext;
493 std::string expected_ciphertext_hex = "8518B8878D34E7185E300D0FCC426396"; 493 std::string expected_ciphertext_hex = "8518B8878D34E7185E300D0FCC426396";
494 494
495 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( 495 std::unique_ptr<crypto::SymmetricKey> sym_key(
496 crypto::SymmetricKey::AES, key)); 496 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
497 ASSERT_TRUE(sym_key.get()); 497 ASSERT_TRUE(sym_key.get());
498 498
499 crypto::Encryptor encryptor; 499 crypto::Encryptor encryptor;
500 // The IV must be exactly as long a the cipher block size. 500 // The IV must be exactly as long a the cipher block size.
501 EXPECT_EQ(16U, iv.size()); 501 EXPECT_EQ(16U, iv.size());
502 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); 502 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
503 503
504 std::string ciphertext; 504 std::string ciphertext;
505 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); 505 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
506 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), 506 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(),
507 ciphertext.size())); 507 ciphertext.size()));
508 } 508 }
509 509
510 TEST(EncryptorTest, CipherTextNotMultipleOfBlockSize) { 510 TEST(EncryptorTest, CipherTextNotMultipleOfBlockSize) {
511 std::string key = "128=SixteenBytes"; 511 std::string key = "128=SixteenBytes";
512 std::string iv = "Sweet Sixteen IV"; 512 std::string iv = "Sweet Sixteen IV";
513 513
514 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( 514 std::unique_ptr<crypto::SymmetricKey> sym_key(
515 crypto::SymmetricKey::AES, key)); 515 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key));
516 ASSERT_TRUE(sym_key.get()); 516 ASSERT_TRUE(sym_key.get());
517 517
518 crypto::Encryptor encryptor; 518 crypto::Encryptor encryptor;
519 // The IV must be exactly as long a the cipher block size. 519 // The IV must be exactly as long a the cipher block size.
520 EXPECT_EQ(16U, iv.size()); 520 EXPECT_EQ(16U, iv.size());
521 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); 521 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
522 522
523 // Use a separately allocated array to improve the odds of the memory tools 523 // Use a separately allocated array to improve the odds of the memory tools
524 // catching invalid accesses. 524 // catching invalid accesses.
525 // 525 //
526 // Otherwise when using std::string as the other tests do, accesses several 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 527 // bytes off the end of the buffer may fall inside the reservation of
528 // the string and not be detected. 528 // the string and not be detected.
529 scoped_ptr<char[]> ciphertext(new char[1]); 529 std::unique_ptr<char[]> ciphertext(new char[1]);
530 530
531 std::string plaintext; 531 std::string plaintext;
532 EXPECT_FALSE( 532 EXPECT_FALSE(
533 encryptor.Decrypt(base::StringPiece(ciphertext.get(), 1), &plaintext)); 533 encryptor.Decrypt(base::StringPiece(ciphertext.get(), 1), &plaintext));
534 } 534 }
OLDNEW
« no previous file with comments | « crypto/encryptor.h ('k') | crypto/hkdf.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698