OLD | NEW |
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/symmetric_key.h" | 5 #include "base/crypto/symmetric_key.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 #if defined(USE_NSS) || defined(OS_MACOSX) | 13 TEST(SymmetricKeyTest, GenerateRandomKey) { |
14 #define MAYBE(name) name | |
15 #else | |
16 #define MAYBE(name) DISABLED_ ## name | |
17 #endif | |
18 | |
19 TEST(SymmetricKeyTest, MAYBE(GenerateRandomKey)) { | |
20 scoped_ptr<base::SymmetricKey> key( | 14 scoped_ptr<base::SymmetricKey> key( |
21 base::SymmetricKey::GenerateRandomKey(base::SymmetricKey::AES, 256)); | 15 base::SymmetricKey::GenerateRandomKey(base::SymmetricKey::AES, 256)); |
22 EXPECT_TRUE(NULL != key.get()); | 16 EXPECT_TRUE(NULL != key.get()); |
23 std::string raw_key; | 17 std::string raw_key; |
24 EXPECT_TRUE(key->GetRawKey(&raw_key)); | 18 EXPECT_TRUE(key->GetRawKey(&raw_key)); |
25 EXPECT_EQ(32U, raw_key.size()); | 19 EXPECT_EQ(32U, raw_key.size()); |
26 | 20 |
27 // Do it again and check that the keys are different. | 21 // Do it again and check that the keys are different. |
28 // (Note: this has a one-in-10^77 chance of failure!) | 22 // (Note: this has a one-in-10^77 chance of failure!) |
29 scoped_ptr<base::SymmetricKey> key2( | 23 scoped_ptr<base::SymmetricKey> key2( |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 50, | 152 50, |
159 160, | 153 160, |
160 { | 154 { |
161 0x6b, 0x9c, 0xf2, 0x6d, 0x45, 0x45, 0x5a, 0x43, | 155 0x6b, 0x9c, 0xf2, 0x6d, 0x45, 0x45, 0x5a, 0x43, |
162 0xa5, 0xb8, 0xbb, 0x27, 0x6a, 0x40, 0x3b, 0x39, | 156 0xa5, 0xb8, 0xbb, 0x27, 0x6a, 0x40, 0x3b, 0x39, |
163 0xe7, 0xfe, 0x37, 0xa0 | 157 0xe7, 0xfe, 0x37, 0xa0 |
164 }, | 158 }, |
165 } | 159 } |
166 }; | 160 }; |
167 | 161 |
168 TEST(SymmetricKeyTest, MAYBE(DeriveKeyFromPassword)) { | 162 TEST(SymmetricKeyTest, DeriveKeyFromPassword) { |
169 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(test_vectors); ++i) { | 163 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(test_vectors); ++i) { |
170 SCOPED_TRACE(StringPrintf("Test[%u]", i)); | 164 SCOPED_TRACE(StringPrintf("Test[%u]", i)); |
171 #if defined(OS_MACOSX) | 165 #if defined(OS_MACOSX) |
172 // The OS X crypto libraries have minimum salt and iteration requirements | 166 // The OS X crypto libraries have minimum salt and iteration requirements |
173 // so some of the above tests will cause them to barf. Skip these. | 167 // so some of the above tests will cause them to barf. Skip these. |
174 if (strlen(test_vectors[i].salt) < 8 || test_vectors[i].rounds < 1000) { | 168 if (strlen(test_vectors[i].salt) < 8 || test_vectors[i].rounds < 1000) { |
175 LOG(INFO) << "Skipped test vector #" << i; | 169 LOG(INFO) << "Skipped test vector #" << i; |
176 continue; | 170 continue; |
177 } | 171 } |
178 #endif // OS_MACOSX | 172 #endif // OS_MACOSX |
179 scoped_ptr<base::SymmetricKey> key( | 173 scoped_ptr<base::SymmetricKey> key( |
180 base::SymmetricKey::DeriveKeyFromPassword( | 174 base::SymmetricKey::DeriveKeyFromPassword( |
181 base::SymmetricKey::HMAC_SHA1, | 175 base::SymmetricKey::HMAC_SHA1, |
182 test_vectors[i].password, test_vectors[i].salt, | 176 test_vectors[i].password, test_vectors[i].salt, |
183 test_vectors[i].rounds, test_vectors[i].key_size_in_bits)); | 177 test_vectors[i].rounds, test_vectors[i].key_size_in_bits)); |
184 ASSERT_TRUE(NULL != key.get()); | 178 ASSERT_TRUE(NULL != key.get()); |
185 | 179 |
186 std::string raw_key; | 180 std::string raw_key; |
187 key->GetRawKey(&raw_key); | 181 key->GetRawKey(&raw_key); |
188 EXPECT_EQ(test_vectors[i].key_size_in_bits / 8, raw_key.size()); | 182 EXPECT_EQ(test_vectors[i].key_size_in_bits / 8, raw_key.size()); |
189 EXPECT_EQ(0, memcmp(test_vectors[i].expected, | 183 EXPECT_EQ(0, memcmp(test_vectors[i].expected, |
190 raw_key.data(), | 184 raw_key.data(), |
191 raw_key.size())); | 185 raw_key.size())); |
192 } | 186 } |
193 } | 187 } |
OLD | NEW |