OLD | NEW |
---|---|
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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 std::string ciphertext; | 449 std::string ciphertext; |
450 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 450 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
451 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | 451 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
452 ciphertext.size())); | 452 ciphertext.size())); |
453 | 453 |
454 std::string decrypted; | 454 std::string decrypted; |
455 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); | 455 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decrypted)); |
456 EXPECT_EQ(plaintext, decrypted); | 456 EXPECT_EQ(plaintext, decrypted); |
457 } | 457 } |
458 | 458 |
459 // Not all platforms allow import/generation of symmetric keys with an | 459 // Symmetric keys with an unsupported size should be rejected. Whether they are |
460 // unsupported size. | 460 // rejected by SymmetricKey::Import or Encryptor::Init depends on the platform. |
461 #if !defined(USE_NSS) && !defined(OS_WIN) && !defined(OS_MACOSX) | |
462 TEST(EncryptorTest, UnsupportedKeySize) { | 461 TEST(EncryptorTest, UnsupportedKeySize) { |
463 std::string key = "7 = bad"; | 462 std::string key = "7 = bad"; |
464 std::string iv = "Sweet Sixteen IV"; | 463 std::string iv = "Sweet Sixteen IV"; |
465 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( | 464 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
466 crypto::SymmetricKey::AES, key)); | 465 crypto::SymmetricKey::AES, key)); |
467 ASSERT_TRUE(sym_key.get()); | 466 if (!sym_key.get()) |
wtc
2014/07/30 22:33:12
David, it just occurred to me that perhaps we shou
davidben
2014/07/31 16:12:15
Hrm, maybe. I didn't add one already. That probabl
| |
467 return; | |
468 | 468 |
469 crypto::Encryptor encryptor; | 469 crypto::Encryptor encryptor; |
470 // The IV must be exactly as long a the cipher block size. | 470 // The IV must be exactly as long as the cipher block size. |
471 EXPECT_EQ(16U, iv.size()); | 471 EXPECT_EQ(16U, iv.size()); |
472 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); | 472 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
473 } | 473 } |
474 #endif // unsupported platforms. | |
475 | 474 |
476 TEST(EncryptorTest, UnsupportedIV) { | 475 TEST(EncryptorTest, UnsupportedIV) { |
477 std::string key = "128=SixteenBytes"; | 476 std::string key = "128=SixteenBytes"; |
478 std::string iv = "OnlyForteen :("; | 477 std::string iv = "OnlyForteen :("; |
479 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( | 478 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
480 crypto::SymmetricKey::AES, key)); | 479 crypto::SymmetricKey::AES, key)); |
481 ASSERT_TRUE(sym_key.get()); | 480 ASSERT_TRUE(sym_key.get()); |
482 | 481 |
483 crypto::Encryptor encryptor; | 482 crypto::Encryptor encryptor; |
484 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); | 483 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
523 // | 522 // |
524 // Otherwise when using std::string as the other tests do, accesses several | 523 // Otherwise when using std::string as the other tests do, accesses several |
525 // bytes off the end of the buffer may fall inside the reservation of | 524 // bytes off the end of the buffer may fall inside the reservation of |
526 // the string and not be detected. | 525 // the string and not be detected. |
527 scoped_ptr<char[]> ciphertext(new char[1]); | 526 scoped_ptr<char[]> ciphertext(new char[1]); |
528 | 527 |
529 std::string plaintext; | 528 std::string plaintext; |
530 EXPECT_FALSE( | 529 EXPECT_FALSE( |
531 encryptor.Decrypt(base::StringPiece(ciphertext.get(), 1), &plaintext)); | 530 encryptor.Decrypt(base::StringPiece(ciphertext.get(), 1), &plaintext)); |
532 } | 531 } |
OLD | NEW |