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 static const unsigned char expected_ciphertext[] = { |
| 60 0x7D, 0x67, 0x5B, 0x53, 0xE6, 0xD8, 0x0F, 0x27, |
| 61 0x74, 0xB1, 0x90, 0xFE, 0x6E, 0x58, 0x4A, 0xA0, |
| 62 0x0E, 0x35, 0xE3, 0x01, 0xC0, 0xFE, 0x9A, 0xD8, |
| 63 0x48, 0x1D, 0x42, 0xB0, 0xBA, 0x21, 0xB2, 0x0C |
| 64 }; |
| 65 |
| 66 ASSERT_EQ(arraysize(expected_ciphertext), ciphertext.size()); |
| 67 for (size_t i = 0; i < ciphertext.size(); ++i) { |
| 68 ASSERT_EQ(expected_ciphertext[i], |
| 69 static_cast<unsigned char>(ciphertext[i])); |
| 70 } |
| 71 |
| 72 crypto::Encryptor decryptor; |
| 73 EXPECT_TRUE(decryptor.Init(wrong_key.get(), crypto::Encryptor::CBC, iv)); |
| 74 std::string decypted; |
| 75 // TODO(wtc): On Linux, Encryptor::Decrypt() doesn't always return false when |
| 76 // wrong key is provided. See crbug.com/124434. Remove #if when bug is fixed. |
| 77 #if !defined(USE_NSS) |
| 78 EXPECT_FALSE(decryptor.Decrypt(ciphertext, &decypted)); |
| 79 #endif |
| 80 } |
| 81 |
38 // CTR mode encryption is only implemented using NSS. | 82 // CTR mode encryption is only implemented using NSS. |
39 #if defined(USE_NSS) | 83 #if defined(USE_NSS) |
40 | 84 |
41 TEST(EncryptorTest, EncryptDecryptCTR) { | 85 TEST(EncryptorTest, EncryptDecryptCTR) { |
42 scoped_ptr<crypto::SymmetricKey> key( | 86 scoped_ptr<crypto::SymmetricKey> key( |
43 crypto::SymmetricKey::GenerateRandomKey( | 87 crypto::SymmetricKey::GenerateRandomKey( |
44 crypto::SymmetricKey::AES, 128)); | 88 crypto::SymmetricKey::AES, 128)); |
45 | 89 |
46 EXPECT_TRUE(NULL != key.get()); | 90 EXPECT_TRUE(NULL != key.get()); |
47 const std::string kInitialCounter = "0000000000000000"; | 91 const std::string kInitialCounter = "0000000000000000"; |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 crypto::Encryptor encryptor; | 318 crypto::Encryptor encryptor; |
275 // The IV must be exactly as long a the cipher block size. | 319 // The IV must be exactly as long a the cipher block size. |
276 EXPECT_EQ(16U, iv.size()); | 320 EXPECT_EQ(16U, iv.size()); |
277 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); | 321 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
278 | 322 |
279 std::string ciphertext; | 323 std::string ciphertext; |
280 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 324 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
281 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | 325 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
282 ciphertext.size())); | 326 ciphertext.size())); |
283 } | 327 } |
OLD | NEW |