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/symmetric_key.h" | |
wtc
2011/04/07 05:35:53
It is correct to include "crypto/symmetric_key.h"
| |
6 | |
7 #include <string> | 5 #include <string> |
8 | 6 |
9 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
10 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
11 #include "base/string_util.h" | 9 #include "base/string_util.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(SymmetricKeyTest, GenerateRandomKey) { | 13 TEST(SymmetricKeyTest, GenerateRandomKey) { |
15 scoped_ptr<base::SymmetricKey> key( | 14 scoped_ptr<crypto::SymmetricKey> key( |
16 base::SymmetricKey::GenerateRandomKey(base::SymmetricKey::AES, 256)); | 15 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); |
17 ASSERT_TRUE(NULL != key.get()); | 16 ASSERT_TRUE(NULL != key.get()); |
18 std::string raw_key; | 17 std::string raw_key; |
19 EXPECT_TRUE(key->GetRawKey(&raw_key)); | 18 EXPECT_TRUE(key->GetRawKey(&raw_key)); |
20 EXPECT_EQ(32U, raw_key.size()); | 19 EXPECT_EQ(32U, raw_key.size()); |
21 | 20 |
22 // Do it again and check that the keys are different. | 21 // Do it again and check that the keys are different. |
23 // (Note: this has a one-in-10^77 chance of failure!) | 22 // (Note: this has a one-in-10^77 chance of failure!) |
24 scoped_ptr<base::SymmetricKey> key2( | 23 scoped_ptr<crypto::SymmetricKey> key2( |
25 base::SymmetricKey::GenerateRandomKey(base::SymmetricKey::AES, 256)); | 24 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); |
26 ASSERT_TRUE(NULL != key2.get()); | 25 ASSERT_TRUE(NULL != key2.get()); |
27 std::string raw_key2; | 26 std::string raw_key2; |
28 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); | 27 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); |
29 EXPECT_EQ(32U, raw_key2.size()); | 28 EXPECT_EQ(32U, raw_key2.size()); |
30 EXPECT_NE(raw_key, raw_key2); | 29 EXPECT_NE(raw_key, raw_key2); |
31 } | 30 } |
32 | 31 |
33 TEST(SymmetricKeyTest, ImportGeneratedKey) { | 32 TEST(SymmetricKeyTest, ImportGeneratedKey) { |
34 scoped_ptr<base::SymmetricKey> key1( | 33 scoped_ptr<crypto::SymmetricKey> key1( |
35 base::SymmetricKey::GenerateRandomKey(base::SymmetricKey::AES, 256)); | 34 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); |
36 ASSERT_TRUE(NULL != key1.get()); | 35 ASSERT_TRUE(NULL != key1.get()); |
37 std::string raw_key1; | 36 std::string raw_key1; |
38 EXPECT_TRUE(key1->GetRawKey(&raw_key1)); | 37 EXPECT_TRUE(key1->GetRawKey(&raw_key1)); |
39 | 38 |
40 scoped_ptr<base::SymmetricKey> key2( | 39 scoped_ptr<crypto::SymmetricKey> key2( |
41 base::SymmetricKey::Import(base::SymmetricKey::AES, raw_key1)); | 40 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, raw_key1)); |
42 ASSERT_TRUE(NULL != key2.get()); | 41 ASSERT_TRUE(NULL != key2.get()); |
43 | 42 |
44 std::string raw_key2; | 43 std::string raw_key2; |
45 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); | 44 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); |
46 | 45 |
47 EXPECT_EQ(raw_key1, raw_key2); | 46 EXPECT_EQ(raw_key1, raw_key2); |
48 } | 47 } |
49 | 48 |
50 TEST(SymmetricKeyTest, ImportDerivedKey) { | 49 TEST(SymmetricKeyTest, ImportDerivedKey) { |
51 scoped_ptr<base::SymmetricKey> key1( | 50 scoped_ptr<crypto::SymmetricKey> key1( |
52 base::SymmetricKey::DeriveKeyFromPassword(base::SymmetricKey::HMAC_SHA1, | 51 crypto::SymmetricKey::DeriveKeyFromPassword( |
53 "password", "somesalt", 1024, | 52 crypto::SymmetricKey::HMAC_SHA1, "password", "somesalt", 1024, 160)); |
54 160)); | |
55 ASSERT_TRUE(NULL != key1.get()); | 53 ASSERT_TRUE(NULL != key1.get()); |
56 std::string raw_key1; | 54 std::string raw_key1; |
57 EXPECT_TRUE(key1->GetRawKey(&raw_key1)); | 55 EXPECT_TRUE(key1->GetRawKey(&raw_key1)); |
58 | 56 |
59 scoped_ptr<base::SymmetricKey> key2( | 57 scoped_ptr<crypto::SymmetricKey> key2( |
60 base::SymmetricKey::Import(base::SymmetricKey::HMAC_SHA1, raw_key1)); | 58 crypto::SymmetricKey::Import(crypto::SymmetricKey::HMAC_SHA1, raw_key1)); |
61 ASSERT_TRUE(NULL != key2.get()); | 59 ASSERT_TRUE(NULL != key2.get()); |
62 | 60 |
63 std::string raw_key2; | 61 std::string raw_key2; |
64 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); | 62 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); |
65 | 63 |
66 EXPECT_EQ(raw_key1, raw_key2); | 64 EXPECT_EQ(raw_key1, raw_key2); |
67 } | 65 } |
68 | 66 |
69 struct PBKDF2TestVector { | 67 struct PBKDF2TestVector { |
70 base::SymmetricKey::Algorithm algorithm; | 68 crypto::SymmetricKey::Algorithm algorithm; |
71 const char* password; | 69 const char* password; |
72 const char* salt; | 70 const char* salt; |
73 unsigned int rounds; | 71 unsigned int rounds; |
74 unsigned int key_size_in_bits; | 72 unsigned int key_size_in_bits; |
75 const char* expected; // ASCII encoded hex bytes | 73 const char* expected; // ASCII encoded hex bytes |
76 }; | 74 }; |
77 | 75 |
78 class SymmetricKeyDeriveKeyFromPasswordTest | 76 class SymmetricKeyDeriveKeyFromPasswordTest |
79 : public testing::TestWithParam<PBKDF2TestVector> { | 77 : public testing::TestWithParam<PBKDF2TestVector> { |
80 }; | 78 }; |
81 | 79 |
82 TEST_P(SymmetricKeyDeriveKeyFromPasswordTest, DeriveKeyFromPassword) { | 80 TEST_P(SymmetricKeyDeriveKeyFromPasswordTest, DeriveKeyFromPassword) { |
83 PBKDF2TestVector test_data(GetParam()); | 81 PBKDF2TestVector test_data(GetParam()); |
84 #if defined(OS_MACOSX) | 82 #if defined(OS_MACOSX) |
85 // The OS X crypto libraries have minimum salt and iteration requirements | 83 // The OS X crypto libraries have minimum salt and iteration requirements |
86 // so some of the tests below will cause them to barf. Skip these. | 84 // so some of the tests below will cause them to barf. Skip these. |
87 if (strlen(test_data.salt) < 8 || test_data.rounds < 1000) { | 85 if (strlen(test_data.salt) < 8 || test_data.rounds < 1000) { |
88 VLOG(1) << "Skipped test vector for " << test_data.expected; | 86 VLOG(1) << "Skipped test vector for " << test_data.expected; |
89 return; | 87 return; |
90 } | 88 } |
91 #endif // OS_MACOSX | 89 #endif // OS_MACOSX |
92 | 90 |
93 scoped_ptr<base::SymmetricKey> key( | 91 scoped_ptr<crypto::SymmetricKey> key( |
94 base::SymmetricKey::DeriveKeyFromPassword( | 92 crypto::SymmetricKey::DeriveKeyFromPassword( |
95 test_data.algorithm, | 93 test_data.algorithm, |
96 test_data.password, test_data.salt, | 94 test_data.password, test_data.salt, |
97 test_data.rounds, test_data.key_size_in_bits)); | 95 test_data.rounds, test_data.key_size_in_bits)); |
98 ASSERT_TRUE(NULL != key.get()); | 96 ASSERT_TRUE(NULL != key.get()); |
99 | 97 |
100 std::string raw_key; | 98 std::string raw_key; |
101 key->GetRawKey(&raw_key); | 99 key->GetRawKey(&raw_key); |
102 EXPECT_EQ(test_data.key_size_in_bits / 8, raw_key.size()); | 100 EXPECT_EQ(test_data.key_size_in_bits / 8, raw_key.size()); |
103 EXPECT_EQ(test_data.expected, | 101 EXPECT_EQ(test_data.expected, |
104 StringToLowerASCII(base::HexEncode(raw_key.data(), | 102 StringToLowerASCII(base::HexEncode(raw_key.data(), |
105 raw_key.size()))); | 103 raw_key.size()))); |
106 } | 104 } |
107 | 105 |
108 static const PBKDF2TestVector kTestVectors[] = { | 106 static const PBKDF2TestVector kTestVectors[] = { |
109 // These tests come from | 107 // These tests come from |
110 // http://www.ietf.org/id/draft-josefsson-pbkdf2-test-vectors-00.txt | 108 // http://www.ietf.org/id/draft-josefsson-pbkdf2-test-vectors-00.txt |
111 { | 109 { |
112 base::SymmetricKey::HMAC_SHA1, | 110 crypto::SymmetricKey::HMAC_SHA1, |
113 "password", | 111 "password", |
114 "salt", | 112 "salt", |
115 1, | 113 1, |
116 160, | 114 160, |
117 "0c60c80f961f0e71f3a9b524af6012062fe037a6", | 115 "0c60c80f961f0e71f3a9b524af6012062fe037a6", |
118 }, | 116 }, |
119 { | 117 { |
120 base::SymmetricKey::HMAC_SHA1, | 118 crypto::SymmetricKey::HMAC_SHA1, |
121 "password", | 119 "password", |
122 "salt", | 120 "salt", |
123 2, | 121 2, |
124 160, | 122 160, |
125 "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957", | 123 "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957", |
126 }, | 124 }, |
127 { | 125 { |
128 base::SymmetricKey::HMAC_SHA1, | 126 crypto::SymmetricKey::HMAC_SHA1, |
129 "password", | 127 "password", |
130 "salt", | 128 "salt", |
131 4096, | 129 4096, |
132 160, | 130 160, |
133 "4b007901b765489abead49d926f721d065a429c1", | 131 "4b007901b765489abead49d926f721d065a429c1", |
134 }, | 132 }, |
135 // This test takes over 30s to run on the trybots. | 133 // This test takes over 30s to run on the trybots. |
136 #if 0 | 134 #if 0 |
137 { | 135 { |
138 base::SymmetricKey::HMAC_SHA1, | 136 crypto::SymmetricKey::HMAC_SHA1, |
139 "password", | 137 "password", |
140 "salt", | 138 "salt", |
141 16777216, | 139 16777216, |
142 160, | 140 160, |
143 "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984", | 141 "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984", |
144 }, | 142 }, |
145 #endif | 143 #endif |
146 | 144 |
147 // These tests come from RFC 3962, via BSD source code at | 145 // These tests come from RFC 3962, via BSD source code at |
148 // http://www.openbsd.org/cgi-bin/cvsweb/src/sbin/bioctl/pbkdf2.c?rev=HEAD&con tent-type=text/plain | 146 // http://www.openbsd.org/cgi-bin/cvsweb/src/sbin/bioctl/pbkdf2.c?rev=HEAD&con tent-type=text/plain |
149 { | 147 { |
150 base::SymmetricKey::HMAC_SHA1, | 148 crypto::SymmetricKey::HMAC_SHA1, |
151 "password", | 149 "password", |
152 "ATHENA.MIT.EDUraeburn", | 150 "ATHENA.MIT.EDUraeburn", |
153 1, | 151 1, |
154 160, | 152 160, |
155 "cdedb5281bb2f801565a1122b25635150ad1f7a0", | 153 "cdedb5281bb2f801565a1122b25635150ad1f7a0", |
156 }, | 154 }, |
157 { | 155 { |
158 base::SymmetricKey::HMAC_SHA1, | 156 crypto::SymmetricKey::HMAC_SHA1, |
159 "password", | 157 "password", |
160 "ATHENA.MIT.EDUraeburn", | 158 "ATHENA.MIT.EDUraeburn", |
161 2, | 159 2, |
162 160, | 160 160, |
163 "01dbee7f4a9e243e988b62c73cda935da05378b9", | 161 "01dbee7f4a9e243e988b62c73cda935da05378b9", |
164 }, | 162 }, |
165 { | 163 { |
166 base::SymmetricKey::HMAC_SHA1, | 164 crypto::SymmetricKey::HMAC_SHA1, |
167 "password", | 165 "password", |
168 "ATHENA.MIT.EDUraeburn", | 166 "ATHENA.MIT.EDUraeburn", |
169 1200, | 167 1200, |
170 160, | 168 160, |
171 "5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddb", | 169 "5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddb", |
172 }, | 170 }, |
173 { | 171 { |
174 base::SymmetricKey::HMAC_SHA1, | 172 crypto::SymmetricKey::HMAC_SHA1, |
175 "password", | 173 "password", |
176 "\0224VxxV4\022", /* 0x1234567878563412 */ | 174 "\0224VxxV4\022", /* 0x1234567878563412 */ |
177 5, | 175 5, |
178 160, | 176 160, |
179 "d1daa78615f287e6a1c8b120d7062a493f98d203", | 177 "d1daa78615f287e6a1c8b120d7062a493f98d203", |
180 }, | 178 }, |
181 { | 179 { |
182 base::SymmetricKey::HMAC_SHA1, | 180 crypto::SymmetricKey::HMAC_SHA1, |
183 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", | 181 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", |
184 "pass phrase equals block size", | 182 "pass phrase equals block size", |
185 1200, | 183 1200, |
186 160, | 184 160, |
187 "139c30c0966bc32ba55fdbf212530ac9c5ec59f1", | 185 "139c30c0966bc32ba55fdbf212530ac9c5ec59f1", |
188 }, | 186 }, |
189 { | 187 { |
190 base::SymmetricKey::HMAC_SHA1, | 188 crypto::SymmetricKey::HMAC_SHA1, |
191 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", | 189 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", |
192 "pass phrase exceeds block size", | 190 "pass phrase exceeds block size", |
193 1200, | 191 1200, |
194 160, | 192 160, |
195 "9ccad6d468770cd51b10e6a68721be611a8b4d28", | 193 "9ccad6d468770cd51b10e6a68721be611a8b4d28", |
196 }, | 194 }, |
197 { | 195 { |
198 base::SymmetricKey::HMAC_SHA1, | 196 crypto::SymmetricKey::HMAC_SHA1, |
199 "\360\235\204\236", /* g-clef (0xf09d849e) */ | 197 "\360\235\204\236", /* g-clef (0xf09d849e) */ |
200 "EXAMPLE.COMpianist", | 198 "EXAMPLE.COMpianist", |
201 50, | 199 50, |
202 160, | 200 160, |
203 "6b9cf26d45455a43a5b8bb276a403b39e7fe37a0", | 201 "6b9cf26d45455a43a5b8bb276a403b39e7fe37a0", |
204 }, | 202 }, |
205 | 203 |
206 // Regression tests for AES keys, derived from the Linux NSS implementation. | 204 // Regression tests for AES keys, derived from the Linux NSS implementation. |
207 { | 205 { |
208 base::SymmetricKey::AES, | 206 crypto::SymmetricKey::AES, |
209 "A test password", | 207 "A test password", |
210 "saltsalt", | 208 "saltsalt", |
211 1, | 209 1, |
212 256, | 210 256, |
213 "44899a7777f0e6e8b752f875f02044b8ac593de146de896f2e8a816e315a36de", | 211 "44899a7777f0e6e8b752f875f02044b8ac593de146de896f2e8a816e315a36de", |
214 }, | 212 }, |
215 { | 213 { |
216 base::SymmetricKey::AES, | 214 crypto::SymmetricKey::AES, |
217 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", | 215 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", |
218 "pass phrase exceeds block size", | 216 "pass phrase exceeds block size", |
219 20, | 217 20, |
220 256, | 218 256, |
221 "e0739745dc28b8721ba402e05214d2ac1eab54cf72bee1fba388297a09eb493c", | 219 "e0739745dc28b8721ba402e05214d2ac1eab54cf72bee1fba388297a09eb493c", |
222 }, | 220 }, |
223 }; | 221 }; |
224 | 222 |
225 INSTANTIATE_TEST_CASE_P(, SymmetricKeyDeriveKeyFromPasswordTest, | 223 INSTANTIATE_TEST_CASE_P(, SymmetricKeyDeriveKeyFromPasswordTest, |
226 testing::ValuesIn(kTestVectors)); | 224 testing::ValuesIn(kTestVectors)); |
OLD | NEW |