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 "base/crypto/encryptor.h" | 5 #include "crypto/encryptor.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/crypto/symmetric_key.h" | |
10 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
11 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "crypto/symmetric_key.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 TEST(EncryptorTest, EncryptDecrypt) { | 14 TEST(EncryptorTest, EncryptDecrypt) { |
15 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::DeriveKeyFromPassword( | 15 scoped_ptr<crypto::SymmetricKey> key( |
16 base::SymmetricKey::AES, "password", "saltiest", 1000, 256)); | 16 crypto::SymmetricKey::DeriveKeyFromPassword( |
| 17 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)); |
17 EXPECT_TRUE(NULL != key.get()); | 18 EXPECT_TRUE(NULL != key.get()); |
18 | 19 |
19 base::Encryptor encryptor; | 20 crypto::Encryptor encryptor; |
20 // The IV must be exactly as long as the cipher block size. | 21 // The IV must be exactly as long as the cipher block size. |
21 std::string iv("the iv: 16 bytes"); | 22 std::string iv("the iv: 16 bytes"); |
22 EXPECT_EQ(16U, iv.size()); | 23 EXPECT_EQ(16U, iv.size()); |
23 EXPECT_TRUE(encryptor.Init(key.get(), base::Encryptor::CBC, iv)); | 24 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv)); |
24 | 25 |
25 std::string plaintext("this is the plaintext"); | 26 std::string plaintext("this is the plaintext"); |
26 std::string ciphertext; | 27 std::string ciphertext; |
27 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
28 | 29 |
29 EXPECT_LT(0U, ciphertext.size()); | 30 EXPECT_LT(0U, ciphertext.size()); |
30 | 31 |
31 std::string decypted; | 32 std::string decypted; |
32 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | 33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
33 | 34 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61, | 80 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61, |
80 // Block #4 | 81 // Block #4 |
81 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, | 82 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, |
82 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b, | 83 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b, |
83 // PKCS #5 padding, encrypted. | 84 // PKCS #5 padding, encrypted. |
84 0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2, | 85 0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2, |
85 0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44 | 86 0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44 |
86 }; | 87 }; |
87 | 88 |
88 std::string key(reinterpret_cast<const char*>(raw_key), sizeof(raw_key)); | 89 std::string key(reinterpret_cast<const char*>(raw_key), sizeof(raw_key)); |
89 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | 90 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
90 base::SymmetricKey::AES, key)); | 91 crypto::SymmetricKey::AES, key)); |
91 ASSERT_TRUE(NULL != sym_key.get()); | 92 ASSERT_TRUE(NULL != sym_key.get()); |
92 | 93 |
93 base::Encryptor encryptor; | 94 crypto::Encryptor encryptor; |
94 // The IV must be exactly as long a the cipher block size. | 95 // The IV must be exactly as long a the cipher block size. |
95 std::string iv(reinterpret_cast<const char*>(raw_iv), sizeof(raw_iv)); | 96 std::string iv(reinterpret_cast<const char*>(raw_iv), sizeof(raw_iv)); |
96 EXPECT_EQ(16U, iv.size()); | 97 EXPECT_EQ(16U, iv.size()); |
97 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | 98 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
98 | 99 |
99 std::string plaintext(reinterpret_cast<const char*>(raw_plaintext), | 100 std::string plaintext(reinterpret_cast<const char*>(raw_plaintext), |
100 sizeof(raw_plaintext)); | 101 sizeof(raw_plaintext)); |
101 std::string ciphertext; | 102 std::string ciphertext; |
102 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 103 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
103 | 104 |
104 EXPECT_EQ(sizeof(raw_ciphertext), ciphertext.size()); | 105 EXPECT_EQ(sizeof(raw_ciphertext), ciphertext.size()); |
105 EXPECT_EQ(0, memcmp(ciphertext.data(), raw_ciphertext, ciphertext.size())); | 106 EXPECT_EQ(0, memcmp(ciphertext.data(), raw_ciphertext, ciphertext.size())); |
106 | 107 |
107 std::string decypted; | 108 std::string decypted; |
108 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | 109 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
109 | 110 |
110 EXPECT_EQ(plaintext, decypted); | 111 EXPECT_EQ(plaintext, decypted); |
111 } | 112 } |
112 | 113 |
113 // Expected output derived from the NSS implementation. | 114 // Expected output derived from the NSS implementation. |
114 TEST(EncryptorTest, EncryptAES128CBCRegression) { | 115 TEST(EncryptorTest, EncryptAES128CBCRegression) { |
115 std::string key = "128=SixteenBytes"; | 116 std::string key = "128=SixteenBytes"; |
116 std::string iv = "Sweet Sixteen IV"; | 117 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 plaintext = "Plain text with a g-clef U+1D11E \360\235\204\236"; |
118 std::string expected_ciphertext_hex = | 119 std::string expected_ciphertext_hex = |
119 "D4A67A0BA33C30F207344D81D1E944BBE65587C3D7D9939A" | 120 "D4A67A0BA33C30F207344D81D1E944BBE65587C3D7D9939A" |
120 "C070C62B9C15A3EA312EA4AD1BC7929F4D3C16B03AD5ADA8"; | 121 "C070C62B9C15A3EA312EA4AD1BC7929F4D3C16B03AD5ADA8"; |
121 | 122 |
122 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | 123 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
123 base::SymmetricKey::AES, key)); | 124 crypto::SymmetricKey::AES, key)); |
124 ASSERT_TRUE(NULL != sym_key.get()); | 125 ASSERT_TRUE(NULL != sym_key.get()); |
125 | 126 |
126 base::Encryptor encryptor; | 127 crypto::Encryptor encryptor; |
127 // The IV must be exactly as long a the cipher block size. | 128 // The IV must be exactly as long a the cipher block size. |
128 EXPECT_EQ(16U, iv.size()); | 129 EXPECT_EQ(16U, iv.size()); |
129 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | 130 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
130 | 131 |
131 std::string ciphertext; | 132 std::string ciphertext; |
132 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 133 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
133 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | 134 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
134 ciphertext.size())); | 135 ciphertext.size())); |
135 | 136 |
136 std::string decypted; | 137 std::string decypted; |
137 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | 138 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
138 EXPECT_EQ(plaintext, decypted); | 139 EXPECT_EQ(plaintext, decypted); |
139 } | 140 } |
140 | 141 |
141 // Expected output derived from the NSS implementation. | 142 // Expected output derived from the NSS implementation. |
142 TEST(EncryptorTest, EncryptAES192CBCRegression) { | 143 TEST(EncryptorTest, EncryptAES192CBCRegression) { |
143 std::string key = "192bitsIsTwentyFourByte!"; | 144 std::string key = "192bitsIsTwentyFourByte!"; |
144 std::string iv = "Sweet Sixteen IV"; | 145 std::string iv = "Sweet Sixteen IV"; |
145 std::string plaintext = "Small text"; | 146 std::string plaintext = "Small text"; |
146 std::string expected_ciphertext_hex = "78DE5D7C2714FC5C61346C5416F6C89A"; | 147 std::string expected_ciphertext_hex = "78DE5D7C2714FC5C61346C5416F6C89A"; |
147 | 148 |
148 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | 149 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
149 base::SymmetricKey::AES, key)); | 150 crypto::SymmetricKey::AES, key)); |
150 ASSERT_TRUE(NULL != sym_key.get()); | 151 ASSERT_TRUE(NULL != sym_key.get()); |
151 | 152 |
152 base::Encryptor encryptor; | 153 crypto::Encryptor encryptor; |
153 // The IV must be exactly as long a the cipher block size. | 154 // The IV must be exactly as long a the cipher block size. |
154 EXPECT_EQ(16U, iv.size()); | 155 EXPECT_EQ(16U, iv.size()); |
155 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | 156 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
156 | 157 |
157 std::string ciphertext; | 158 std::string ciphertext; |
158 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 159 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
159 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | 160 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
160 ciphertext.size())); | 161 ciphertext.size())); |
161 | 162 |
162 std::string decypted; | 163 std::string decypted; |
163 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | 164 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
164 EXPECT_EQ(plaintext, decypted); | 165 EXPECT_EQ(plaintext, decypted); |
165 } | 166 } |
166 | 167 |
167 // Not all platforms allow import/generation of symmetric keys with an | 168 // Not all platforms allow import/generation of symmetric keys with an |
168 // unsupported size. | 169 // unsupported size. |
169 #if !defined(OS_WIN) && !defined(USE_NSS) | 170 #if !defined(OS_WIN) && !defined(USE_NSS) |
170 TEST(EncryptorTest, UnsupportedKeySize) { | 171 TEST(EncryptorTest, UnsupportedKeySize) { |
171 std::string key = "7 = bad"; | 172 std::string key = "7 = bad"; |
172 std::string iv = "Sweet Sixteen IV"; | 173 std::string iv = "Sweet Sixteen IV"; |
173 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | 174 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
174 base::SymmetricKey::AES, key)); | 175 crypto::SymmetricKey::AES, key)); |
175 ASSERT_TRUE(NULL != sym_key.get()); | 176 ASSERT_TRUE(NULL != sym_key.get()); |
176 | 177 |
177 base::Encryptor encryptor; | 178 crypto::Encryptor encryptor; |
178 // The IV must be exactly as long a the cipher block size. | 179 // The IV must be exactly as long a the cipher block size. |
179 EXPECT_EQ(16U, iv.size()); | 180 EXPECT_EQ(16U, iv.size()); |
180 EXPECT_FALSE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | 181 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
181 } | 182 } |
182 #endif // unsupported platforms. | 183 #endif // unsupported platforms. |
183 | 184 |
184 TEST(EncryptorTest, UnsupportedIV) { | 185 TEST(EncryptorTest, UnsupportedIV) { |
185 std::string key = "128=SixteenBytes"; | 186 std::string key = "128=SixteenBytes"; |
186 std::string iv = "OnlyForteen :("; | 187 std::string iv = "OnlyForteen :("; |
187 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | 188 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
188 base::SymmetricKey::AES, key)); | 189 crypto::SymmetricKey::AES, key)); |
189 ASSERT_TRUE(NULL != sym_key.get()); | 190 ASSERT_TRUE(NULL != sym_key.get()); |
190 | 191 |
191 base::Encryptor encryptor; | 192 crypto::Encryptor encryptor; |
192 EXPECT_FALSE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | 193 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
193 } | 194 } |
194 | 195 |
195 TEST(EncryptorTest, EmptyEncrypt) { | 196 TEST(EncryptorTest, EmptyEncrypt) { |
196 std::string key = "128=SixteenBytes"; | 197 std::string key = "128=SixteenBytes"; |
197 std::string iv = "Sweet Sixteen IV"; | 198 std::string iv = "Sweet Sixteen IV"; |
198 std::string plaintext; | 199 std::string plaintext; |
199 std::string expected_ciphertext_hex = "8518B8878D34E7185E300D0FCC426396"; | 200 std::string expected_ciphertext_hex = "8518B8878D34E7185E300D0FCC426396"; |
200 | 201 |
201 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | 202 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
202 base::SymmetricKey::AES, key)); | 203 crypto::SymmetricKey::AES, key)); |
203 ASSERT_TRUE(NULL != sym_key.get()); | 204 ASSERT_TRUE(NULL != sym_key.get()); |
204 | 205 |
205 base::Encryptor encryptor; | 206 crypto::Encryptor encryptor; |
206 // The IV must be exactly as long a the cipher block size. | 207 // The IV must be exactly as long a the cipher block size. |
207 EXPECT_EQ(16U, iv.size()); | 208 EXPECT_EQ(16U, iv.size()); |
208 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | 209 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
209 | 210 |
210 std::string ciphertext; | 211 std::string ciphertext; |
211 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 212 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
212 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | 213 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
213 ciphertext.size())); | 214 ciphertext.size())); |
214 } | 215 } |
215 | 216 |
216 TEST(EncryptorTest, EmptyDecrypt) { | 217 TEST(EncryptorTest, EmptyDecrypt) { |
217 std::string key = "128=SixteenBytes"; | 218 std::string key = "128=SixteenBytes"; |
218 std::string iv = "Sweet Sixteen IV"; | 219 std::string iv = "Sweet Sixteen IV"; |
219 | 220 |
220 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | 221 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import( |
221 base::SymmetricKey::AES, key)); | 222 crypto::SymmetricKey::AES, key)); |
222 ASSERT_TRUE(NULL != sym_key.get()); | 223 ASSERT_TRUE(NULL != sym_key.get()); |
223 | 224 |
224 base::Encryptor encryptor; | 225 crypto::Encryptor encryptor; |
225 // The IV must be exactly as long a the cipher block size. | 226 // The IV must be exactly as long a the cipher block size. |
226 EXPECT_EQ(16U, iv.size()); | 227 EXPECT_EQ(16U, iv.size()); |
227 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | 228 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
228 | 229 |
229 std::string decrypted; | 230 std::string decrypted; |
230 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); | 231 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); |
231 EXPECT_EQ("", decrypted); | 232 EXPECT_EQ("", decrypted); |
232 } | 233 } |
OLD | NEW |