OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/crypto/encryptor.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/crypto/symmetric_key.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/string_number_conversions.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 TEST(EncryptorTest, EncryptDecrypt) { | |
15 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::DeriveKeyFromPassword( | |
16 base::SymmetricKey::AES, "password", "saltiest", 1000, 256)); | |
17 EXPECT_TRUE(NULL != key.get()); | |
18 | |
19 base::Encryptor encryptor; | |
20 // The IV must be exactly as long as the cipher block size. | |
21 std::string iv("the iv: 16 bytes"); | |
22 EXPECT_EQ(16U, iv.size()); | |
23 EXPECT_TRUE(encryptor.Init(key.get(), base::Encryptor::CBC, iv)); | |
24 | |
25 std::string plaintext("this is the plaintext"); | |
26 std::string ciphertext; | |
27 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | |
28 | |
29 EXPECT_LT(0U, ciphertext.size()); | |
30 | |
31 std::string decypted; | |
32 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | |
33 | |
34 EXPECT_EQ(plaintext, decypted); | |
35 } | |
36 | |
37 // TODO(wtc): add more known-answer tests. Test vectors are available from | |
38 // http://www.ietf.org/rfc/rfc3602 | |
39 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf | |
40 // http://gladman.plushost.co.uk/oldsite/AES/index.php | |
41 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip | |
42 | |
43 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt. | |
44 TEST(EncryptorTest, EncryptAES256CBC) { | |
45 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt. | |
46 static const unsigned char raw_key[] = { | |
47 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, | |
48 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, | |
49 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, | |
50 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 | |
51 }; | |
52 static const unsigned char raw_iv[] = { | |
53 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
54 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | |
55 }; | |
56 static const unsigned char raw_plaintext[] = { | |
57 // Block #1 | |
58 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, | |
59 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, | |
60 // Block #2 | |
61 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, | |
62 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, | |
63 // Block #3 | |
64 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, | |
65 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, | |
66 // Block #4 | |
67 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, | |
68 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10, | |
69 }; | |
70 static const unsigned char raw_ciphertext[] = { | |
71 // Block #1 | |
72 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba, | |
73 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6, | |
74 // Block #2 | |
75 0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, | |
76 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d, | |
77 // Block #3 | |
78 0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf, | |
79 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61, | |
80 // Block #4 | |
81 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc, | |
82 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b, | |
83 // PKCS #5 padding, encrypted. | |
84 0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2, | |
85 0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44 | |
86 }; | |
87 | |
88 std::string key(reinterpret_cast<const char*>(raw_key), sizeof(raw_key)); | |
89 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | |
90 base::SymmetricKey::AES, key)); | |
91 ASSERT_TRUE(NULL != sym_key.get()); | |
92 | |
93 base::Encryptor encryptor; | |
94 // 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 EXPECT_EQ(16U, iv.size()); | |
97 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | |
98 | |
99 std::string plaintext(reinterpret_cast<const char*>(raw_plaintext), | |
100 sizeof(raw_plaintext)); | |
101 std::string ciphertext; | |
102 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | |
103 | |
104 EXPECT_EQ(sizeof(raw_ciphertext), ciphertext.size()); | |
105 EXPECT_EQ(0, memcmp(ciphertext.data(), raw_ciphertext, ciphertext.size())); | |
106 | |
107 std::string decypted; | |
108 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | |
109 | |
110 EXPECT_EQ(plaintext, decypted); | |
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 } | |
140 | |
141 // Expected output derived from the NSS implementation. | |
142 TEST(EncryptorTest, EncryptAES192CBCRegression) { | |
143 std::string key = "192bitsIsTwentyFourByte!"; | |
144 std::string iv = "Sweet Sixteen IV"; | |
145 std::string plaintext = "Small text"; | |
146 std::string expected_ciphertext_hex = "78DE5D7C2714FC5C61346C5416F6C89A"; | |
147 | |
148 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | |
149 base::SymmetricKey::AES, key)); | |
150 ASSERT_TRUE(NULL != sym_key.get()); | |
151 | |
152 base::Encryptor encryptor; | |
153 // The IV must be exactly as long a the cipher block size. | |
154 EXPECT_EQ(16U, iv.size()); | |
155 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | |
156 | |
157 std::string ciphertext; | |
158 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | |
159 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | |
160 ciphertext.size())); | |
161 | |
162 std::string decypted; | |
163 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | |
164 EXPECT_EQ(plaintext, decypted); | |
165 } | |
166 | |
167 // Not all platforms allow import/generation of symmetric keys with an | |
168 // unsupported size. | |
169 #if !defined(OS_WIN) && !defined(USE_NSS) | |
170 TEST(EncryptorTest, UnsupportedKeySize) { | |
171 std::string key = "7 = bad"; | |
172 std::string iv = "Sweet Sixteen IV"; | |
173 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | |
174 base::SymmetricKey::AES, key)); | |
175 ASSERT_TRUE(NULL != sym_key.get()); | |
176 | |
177 base::Encryptor encryptor; | |
178 // The IV must be exactly as long a the cipher block size. | |
179 EXPECT_EQ(16U, iv.size()); | |
180 EXPECT_FALSE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | |
181 } | |
182 #endif // unsupported platforms. | |
183 | |
184 TEST(EncryptorTest, UnsupportedIV) { | |
185 std::string key = "128=SixteenBytes"; | |
186 std::string iv = "OnlyForteen :("; | |
187 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | |
188 base::SymmetricKey::AES, key)); | |
189 ASSERT_TRUE(NULL != sym_key.get()); | |
190 | |
191 base::Encryptor encryptor; | |
192 EXPECT_FALSE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | |
193 } | |
194 | |
195 TEST(EncryptorTest, EmptyEncrypt) { | |
196 std::string key = "128=SixteenBytes"; | |
197 std::string iv = "Sweet Sixteen IV"; | |
198 std::string plaintext; | |
199 std::string expected_ciphertext_hex = "8518B8878D34E7185E300D0FCC426396"; | |
200 | |
201 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | |
202 base::SymmetricKey::AES, key)); | |
203 ASSERT_TRUE(NULL != sym_key.get()); | |
204 | |
205 base::Encryptor encryptor; | |
206 // The IV must be exactly as long a the cipher block size. | |
207 EXPECT_EQ(16U, iv.size()); | |
208 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | |
209 | |
210 std::string ciphertext; | |
211 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | |
212 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | |
213 ciphertext.size())); | |
214 } | |
215 | |
216 TEST(EncryptorTest, EmptyDecrypt) { | |
217 std::string key = "128=SixteenBytes"; | |
218 std::string iv = "Sweet Sixteen IV"; | |
219 | |
220 scoped_ptr<base::SymmetricKey> sym_key(base::SymmetricKey::Import( | |
221 base::SymmetricKey::AES, key)); | |
222 ASSERT_TRUE(NULL != sym_key.get()); | |
223 | |
224 base::Encryptor encryptor; | |
225 // The IV must be exactly as long a the cipher block size. | |
226 EXPECT_EQ(16U, iv.size()); | |
227 EXPECT_TRUE(encryptor.Init(sym_key.get(), base::Encryptor::CBC, iv)); | |
228 | |
229 std::string decrypted; | |
230 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); | |
231 EXPECT_EQ("", decrypted); | |
232 } | |
OLD | NEW |