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

Side by Side Diff: crypto/symmetric_key_unittest.cc

Issue 2934893003: Deleted redundant SymmetricKey::GetRawKey(). (Closed)
Patch Set: Rebased to fix conflict with ToT. Created 3 years, 6 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
« no previous file with comments | « crypto/symmetric_key.cc ('k') | no next file » | 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) 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 <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 TEST(SymmetricKeyTest, GenerateRandomKey) { 14 TEST(SymmetricKeyTest, GenerateRandomKey) {
15 std::unique_ptr<crypto::SymmetricKey> key( 15 std::unique_ptr<crypto::SymmetricKey> key(
16 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); 16 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256));
17 ASSERT_TRUE(key); 17 ASSERT_TRUE(key);
18 std::string raw_key; 18 EXPECT_EQ(32U, key->key().size());
19 EXPECT_TRUE(key->GetRawKey(&raw_key));
20 EXPECT_EQ(32U, raw_key.size());
21 19
22 // Do it again and check that the keys are different. 20 // Do it again and check that the keys are different.
23 // (Note: this has a one-in-10^77 chance of failure!) 21 // (Note: this has a one-in-10^77 chance of failure!)
24 std::unique_ptr<crypto::SymmetricKey> key2( 22 std::unique_ptr<crypto::SymmetricKey> key2(
25 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); 23 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256));
26 ASSERT_TRUE(key2); 24 ASSERT_TRUE(key2);
27 std::string raw_key2; 25 EXPECT_EQ(32U, key2->key().size());
28 EXPECT_TRUE(key2->GetRawKey(&raw_key2)); 26 EXPECT_NE(key->key(), key2->key());
29 EXPECT_EQ(32U, raw_key2.size());
30 EXPECT_NE(raw_key, raw_key2);
31 } 27 }
32 28
33 TEST(SymmetricKeyTest, ImportGeneratedKey) { 29 TEST(SymmetricKeyTest, ImportGeneratedKey) {
34 std::unique_ptr<crypto::SymmetricKey> key1( 30 std::unique_ptr<crypto::SymmetricKey> key1(
35 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256)); 31 crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES, 256));
36 ASSERT_TRUE(key1); 32 ASSERT_TRUE(key1);
37 std::string raw_key1;
38 EXPECT_TRUE(key1->GetRawKey(&raw_key1));
39 33
40 std::unique_ptr<crypto::SymmetricKey> key2( 34 std::unique_ptr<crypto::SymmetricKey> key2(
41 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, raw_key1)); 35 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, key1->key()));
42 ASSERT_TRUE(key2); 36 ASSERT_TRUE(key2);
43 37
44 std::string raw_key2; 38 EXPECT_EQ(key1->key(), key2->key());
45 EXPECT_TRUE(key2->GetRawKey(&raw_key2));
46
47 EXPECT_EQ(raw_key1, raw_key2);
48 } 39 }
49 40
50 TEST(SymmetricKeyTest, ImportDerivedKey) { 41 TEST(SymmetricKeyTest, ImportDerivedKey) {
51 std::unique_ptr<crypto::SymmetricKey> key1( 42 std::unique_ptr<crypto::SymmetricKey> key1(
52 crypto::SymmetricKey::DeriveKeyFromPassword( 43 crypto::SymmetricKey::DeriveKeyFromPassword(
53 crypto::SymmetricKey::HMAC_SHA1, "password", "somesalt", 1024, 160)); 44 crypto::SymmetricKey::HMAC_SHA1, "password", "somesalt", 1024, 160));
54 ASSERT_TRUE(key1); 45 ASSERT_TRUE(key1);
55 std::string raw_key1;
56 EXPECT_TRUE(key1->GetRawKey(&raw_key1));
57 46
58 std::unique_ptr<crypto::SymmetricKey> key2( 47 std::unique_ptr<crypto::SymmetricKey> key2(crypto::SymmetricKey::Import(
59 crypto::SymmetricKey::Import(crypto::SymmetricKey::HMAC_SHA1, raw_key1)); 48 crypto::SymmetricKey::HMAC_SHA1, key1->key()));
60 ASSERT_TRUE(key2); 49 ASSERT_TRUE(key2);
61 50
62 std::string raw_key2; 51 EXPECT_EQ(key1->key(), key2->key());
63 EXPECT_TRUE(key2->GetRawKey(&raw_key2));
64
65 EXPECT_EQ(raw_key1, raw_key2);
66 } 52 }
67 53
68 struct PBKDF2TestVector { 54 struct PBKDF2TestVector {
69 crypto::SymmetricKey::Algorithm algorithm; 55 crypto::SymmetricKey::Algorithm algorithm;
70 const char* password; 56 const char* password;
71 const char* salt; 57 const char* salt;
72 unsigned int rounds; 58 unsigned int rounds;
73 unsigned int key_size_in_bits; 59 unsigned int key_size_in_bits;
74 const char* expected; // ASCII encoded hex bytes 60 const char* expected; // ASCII encoded hex bytes
75 }; 61 };
76 62
77 class SymmetricKeyDeriveKeyFromPasswordTest 63 class SymmetricKeyDeriveKeyFromPasswordTest
78 : public testing::TestWithParam<PBKDF2TestVector> { 64 : public testing::TestWithParam<PBKDF2TestVector> {
79 }; 65 };
80 66
81 TEST_P(SymmetricKeyDeriveKeyFromPasswordTest, DeriveKeyFromPassword) { 67 TEST_P(SymmetricKeyDeriveKeyFromPasswordTest, DeriveKeyFromPassword) {
82 PBKDF2TestVector test_data(GetParam()); 68 PBKDF2TestVector test_data(GetParam());
83 std::unique_ptr<crypto::SymmetricKey> key( 69 std::unique_ptr<crypto::SymmetricKey> key(
84 crypto::SymmetricKey::DeriveKeyFromPassword( 70 crypto::SymmetricKey::DeriveKeyFromPassword(
85 test_data.algorithm, test_data.password, test_data.salt, 71 test_data.algorithm, test_data.password, test_data.salt,
86 test_data.rounds, test_data.key_size_in_bits)); 72 test_data.rounds, test_data.key_size_in_bits));
87 ASSERT_TRUE(key); 73 ASSERT_TRUE(key);
88 74
89 std::string raw_key; 75 const std::string& raw_key = key->key();
90 key->GetRawKey(&raw_key);
91 EXPECT_EQ(test_data.key_size_in_bits / 8, raw_key.size()); 76 EXPECT_EQ(test_data.key_size_in_bits / 8, raw_key.size());
92 EXPECT_EQ(test_data.expected, 77 EXPECT_EQ(test_data.expected,
93 base::ToLowerASCII(base::HexEncode(raw_key.data(), 78 base::ToLowerASCII(base::HexEncode(raw_key.data(),
94 raw_key.size()))); 79 raw_key.size())));
95 } 80 }
96 81
97 static const PBKDF2TestVector kTestVectors[] = { 82 static const PBKDF2TestVector kTestVectors[] = {
98 // These tests come from 83 // These tests come from
99 // http://www.ietf.org/id/draft-josefsson-pbkdf2-test-vectors-00.txt 84 // http://www.ietf.org/id/draft-josefsson-pbkdf2-test-vectors-00.txt
100 { 85 {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 191 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
207 "pass phrase exceeds block size", 192 "pass phrase exceeds block size",
208 20, 193 20,
209 256, 194 256,
210 "e0739745dc28b8721ba402e05214d2ac1eab54cf72bee1fba388297a09eb493c", 195 "e0739745dc28b8721ba402e05214d2ac1eab54cf72bee1fba388297a09eb493c",
211 }, 196 },
212 }; 197 };
213 198
214 INSTANTIATE_TEST_CASE_P(, SymmetricKeyDeriveKeyFromPasswordTest, 199 INSTANTIATE_TEST_CASE_P(, SymmetricKeyDeriveKeyFromPasswordTest,
215 testing::ValuesIn(kTestVectors)); 200 testing::ValuesIn(kTestVectors));
OLDNEW
« no previous file with comments | « crypto/symmetric_key.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698