Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Side by Side Diff: crypto/encryptor_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698