OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/crypto/encryptor.h" | 5 #include "base/crypto/encryptor.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/crypto/symmetric_key.h" | 9 #include "base/crypto/symmetric_key.h" |
10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| 11 #include "base/string_number_conversions.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 TEST(EncryptorTest, EncryptDecrypt) { | 14 TEST(EncryptorTest, EncryptDecrypt) { |
14 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::DeriveKeyFromPassword( | 15 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::DeriveKeyFromPassword( |
15 base::SymmetricKey::AES, "password", "saltiest", 1000, 256)); | 16 base::SymmetricKey::AES, "password", "saltiest", 1000, 256)); |
16 EXPECT_TRUE(NULL != key.get()); | 17 EXPECT_TRUE(NULL != key.get()); |
17 | 18 |
18 base::Encryptor encryptor; | 19 base::Encryptor encryptor; |
19 // The IV must be exactly as long as the cipher block size. | 20 // The IV must be exactly as long as the cipher block size. |
20 std::string iv("the iv: 16 bytes"); | 21 std::string iv("the iv: 16 bytes"); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 102 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
102 | 103 |
103 EXPECT_EQ(sizeof(raw_ciphertext), ciphertext.size()); | 104 EXPECT_EQ(sizeof(raw_ciphertext), ciphertext.size()); |
104 EXPECT_EQ(0, memcmp(ciphertext.data(), raw_ciphertext, ciphertext.size())); | 105 EXPECT_EQ(0, memcmp(ciphertext.data(), raw_ciphertext, ciphertext.size())); |
105 | 106 |
106 std::string decypted; | 107 std::string decypted; |
107 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | 108 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
108 | 109 |
109 EXPECT_EQ(plaintext, decypted); | 110 EXPECT_EQ(plaintext, decypted); |
110 } | 111 } |
| 112 |
| 113 // Expected output derived from the NSS implementation. |
| 114 TEST(EncryptorTest, EncryptAES128CBCRegression) { |
| 115 std::string key = "128=SixteenBytes"; |
| 116 std::string iv = "Sweet Sixteen IV"; |
| 117 std::string plaintext = "Plain text with a g-clef U+1D11E \360\235\204\236"; |
| 118 std::string expected_ciphertext_hex = |
| 119 "D4A67A0BA33C30F207344D81D1E944BBE65587C3D7D9939A" |
| 120 "C070C62B9C15A3EA312EA4AD1BC7929F4D3C16B03AD5ADA8"; |
| 121 |
| 122 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( |
| 123 base::SymmetricKey::AES, key)); |
| 124 ASSERT_TRUE(NULL != sym_key.get()); |
| 125 |
| 126 base::Encryptor encryptor; |
| 127 // The IV must be exactly as long a the cipher block size. |
| 128 EXPECT_EQ(16U, iv.size()); |
| 129 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); |
| 130 |
| 131 std::string ciphertext; |
| 132 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 133 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
| 134 ciphertext.size())); |
| 135 |
| 136 std::string decypted; |
| 137 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
| 138 EXPECT_EQ(plaintext, decypted); |
| 139 } |
OLD | NEW |