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

Side by Side Diff: base/crypto/encryptor_unittest.cc

Issue 4777001: Implements encryptor_openssl.cc (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix linux & win Created 10 years, 1 month 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
« no previous file with comments | « base/crypto/encryptor_openssl.cc ('k') | base/crypto/encryptor_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
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 }
OLDNEW
« no previous file with comments | « base/crypto/encryptor_openssl.cc ('k') | base/crypto/encryptor_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698