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 "crypto/symmetric_key.h" | 5 #include "crypto/symmetric_key.h" |
6 | 6 |
| 7 #include <algorithm> |
7 #include <string> | 8 #include <string> |
8 | 9 |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 TEST(SymmetricKeyTest, GenerateRandomKey) { | 15 TEST(SymmetricKeyTest, GenerateRandomKey) { |
15 scoped_ptr<crypto::SymmetricKey> key( | 16 scoped_ptr<crypto::SymmetricKey> key( |
16 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); | 17 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); |
17 ASSERT_TRUE(NULL != key.get()); | 18 ASSERT_TRUE(NULL != key.get()); |
18 std::string raw_key; | 19 std::string raw_key; |
19 EXPECT_TRUE(key->GetRawKey(&raw_key)); | 20 EXPECT_TRUE(key->GetRawKey(&raw_key)); |
20 EXPECT_EQ(32U, raw_key.size()); | 21 EXPECT_EQ(32U, raw_key.size()); |
21 | 22 |
22 // Do it again and check that the keys are different. | 23 // Do it again and check that the keys are different. |
23 // (Note: this has a one-in-10^77 chance of failure!) | 24 // (Note: this has a one-in-10^77 chance of failure!) |
24 scoped_ptr<crypto::SymmetricKey> key2( | 25 scoped_ptr<crypto::SymmetricKey> key2( |
25 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); | 26 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); |
26 ASSERT_TRUE(NULL != key2.get()); | 27 ASSERT_TRUE(NULL != key2.get()); |
27 std::string raw_key2; | 28 std::string raw_key2; |
28 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); | 29 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); |
29 EXPECT_EQ(32U, raw_key2.size()); | 30 EXPECT_EQ(32U, raw_key2.size()); |
30 EXPECT_NE(raw_key, raw_key2); | 31 EXPECT_NE(raw_key, raw_key2); |
31 } | 32 } |
32 | 33 |
| 34 TEST(SymmetricKeyTest, GenerateRandomBytes) { |
| 35 std::vector<uint8> bytes(50); |
| 36 ASSERT_TRUE(crypto::SymmetricKey::GenerateRandomBytes( |
| 37 bytes.size(), &bytes[0])); |
| 38 // Probability of occurrence of less than 25 unique bytes in 50 random bytes |
| 39 // is below 10^-25. |
| 40 std::sort(bytes.begin(), bytes.end()); |
| 41 ASSERT_GT(std::unique(bytes.begin(), bytes.end()) - bytes.begin(), 25); |
| 42 } |
| 43 |
33 TEST(SymmetricKeyTest, ImportGeneratedKey) { | 44 TEST(SymmetricKeyTest, ImportGeneratedKey) { |
34 scoped_ptr<crypto::SymmetricKey> key1( | 45 scoped_ptr<crypto::SymmetricKey> key1( |
35 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); | 46 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); |
36 ASSERT_TRUE(NULL != key1.get()); | 47 ASSERT_TRUE(NULL != key1.get()); |
37 std::string raw_key1; | 48 std::string raw_key1; |
38 EXPECT_TRUE(key1->GetRawKey(&raw_key1)); | 49 EXPECT_TRUE(key1->GetRawKey(&raw_key1)); |
39 | 50 |
40 scoped_ptr<crypto::SymmetricKey> key2( | 51 scoped_ptr<crypto::SymmetricKey> key2( |
41 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, raw_key1)); | 52 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, raw_key1)); |
42 ASSERT_TRUE(NULL != key2.get()); | 53 ASSERT_TRUE(NULL != key2.get()); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", | 227 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", |
217 "pass phrase exceeds block size", | 228 "pass phrase exceeds block size", |
218 20, | 229 20, |
219 256, | 230 256, |
220 "e0739745dc28b8721ba402e05214d2ac1eab54cf72bee1fba388297a09eb493c", | 231 "e0739745dc28b8721ba402e05214d2ac1eab54cf72bee1fba388297a09eb493c", |
221 }, | 232 }, |
222 }; | 233 }; |
223 | 234 |
224 INSTANTIATE_TEST_CASE_P(, SymmetricKeyDeriveKeyFromPasswordTest, | 235 INSTANTIATE_TEST_CASE_P(, SymmetricKeyDeriveKeyFromPasswordTest, |
225 testing::ValuesIn(kTestVectors)); | 236 testing::ValuesIn(kTestVectors)); |
OLD | NEW |