Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 29 | 29 |
| 30 EXPECT_LT(0U, ciphertext.size()); | 30 EXPECT_LT(0U, ciphertext.size()); |
| 31 | 31 |
| 32 std::string decypted; | 32 std::string decypted; |
| 33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | 33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
| 34 | 34 |
| 35 EXPECT_EQ(plaintext, decypted); | 35 EXPECT_EQ(plaintext, decypted); |
| 36 } | 36 } |
| 37 | 37 |
| 38 TEST(EncryptorTest, DecryptWrongKey) { | |
| 39 scoped_ptr<crypto::SymmetricKey> key( | |
| 40 crypto::SymmetricKey::DeriveKeyFromPassword( | |
| 41 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)); | |
| 42 EXPECT_TRUE(NULL != key.get()); | |
| 43 | |
| 44 scoped_ptr<crypto::SymmetricKey> wrong_key( | |
| 45 crypto::SymmetricKey::DeriveKeyFromPassword( | |
| 46 crypto::SymmetricKey::AES, "wrongword", "sweetest", 1000, 256)); | |
| 47 EXPECT_TRUE(NULL != wrong_key.get()); | |
| 48 | |
| 49 crypto::Encryptor encryptor; | |
| 50 // The IV must be exactly as long as the cipher block size. | |
| 51 std::string iv("the iv: 16 bytes"); | |
| 52 EXPECT_EQ(16U, iv.size()); | |
| 53 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv)); | |
| 54 | |
| 55 std::string plaintext("this is the plaintext"); | |
| 56 std::string ciphertext; | |
| 57 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | |
| 58 | |
| 59 EXPECT_LT(0U, ciphertext.size()); | |
|
wtc
2012/04/23 21:46:41
I suggest removing this line because the new test
xhwang
2012/04/23 21:54:05
Done.
| |
| 60 | |
| 61 static const unsigned char expected_ciphertext[] = { | |
| 62 0x7D, 0x67, 0x5B, 0x53, 0xE6, 0xD8, 0x0F, 0x27, | |
| 63 0x74, 0xB1, 0x90, 0xFE, 0x6E, 0x58, 0x4A, 0xA0, | |
| 64 0x0E, 0x35, 0xE3, 0x01, 0xC0, 0xFE, 0x9A, 0xD8, | |
| 65 0x48, 0x1D, 0x42, 0xB0, 0xBA, 0x21, 0xB2, 0x0C | |
| 66 }; | |
| 67 | |
| 68 ASSERT_EQ(arraysize(expected_ciphertext), ciphertext.size()); | |
| 69 for (size_t i = 0; i < ciphertext.size(); ++i) { | |
| 70 ASSERT_EQ(expected_ciphertext[i], | |
| 71 static_cast<unsigned char>(ciphertext[i])); | |
| 72 } | |
| 73 | |
| 74 crypto::Encryptor decryptor; | |
| 75 EXPECT_TRUE(decryptor.Init(wrong_key.get(), crypto::Encryptor::CBC, iv)); | |
| 76 std::string decypted; | |
| 77 // TODO(wtc): On Linux, Encryptor::Decrypt() doesn't always return false when | |
| 78 // wrong key is provided. See crbug.com/124434. Remove #if when bug is fixed. | |
| 79 #if !defined(USE_NSS) | |
| 80 EXPECT_FALSE(decryptor.Decrypt(ciphertext, &decypted)); | |
| 81 #endif | |
| 82 } | |
| 83 | |
| 38 // CTR mode encryption is only implemented using NSS. | 84 // CTR mode encryption is only implemented using NSS. |
| 39 #if defined(USE_NSS) | 85 #if defined(USE_NSS) |
| 40 | 86 |
| 41 TEST(EncryptorTest, EncryptDecryptCTR) { | 87 TEST(EncryptorTest, EncryptDecryptCTR) { |
| 42 scoped_ptr<crypto::SymmetricKey> key( | 88 scoped_ptr<crypto::SymmetricKey> key( |
| 43 crypto::SymmetricKey::GenerateRandomKey( | 89 crypto::SymmetricKey::GenerateRandomKey( |
| 44 crypto::SymmetricKey::AES, 128)); | 90 crypto::SymmetricKey::AES, 128)); |
| 45 | 91 |
| 46 EXPECT_TRUE(NULL != key.get()); | 92 EXPECT_TRUE(NULL != key.get()); |
| 47 const std::string kInitialCounter = "0000000000000000"; | 93 const std::string kInitialCounter = "0000000000000000"; |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 crypto::Encryptor encryptor; | 320 crypto::Encryptor encryptor; |
| 275 // The IV must be exactly as long a the cipher block size. | 321 // The IV must be exactly as long a the cipher block size. |
| 276 EXPECT_EQ(16U, iv.size()); | 322 EXPECT_EQ(16U, iv.size()); |
| 277 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); | 323 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
| 278 | 324 |
| 279 std::string ciphertext; | 325 std::string ciphertext; |
| 280 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 326 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 281 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | 327 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
| 282 ciphertext.size())); | 328 ciphertext.size())); |
| 283 } | 329 } |
| OLD | NEW |