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

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

Issue 1347002: Add Mac implementations of new SymmetricKey and Encryptor classes. (Closed)
Patch Set: Responding to feedback Created 10 years, 9 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
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/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) 13 #if defined(USE_NSS) || defined(OS_MACOSX)
14 #define MAYBE(name) name 14 #define MAYBE(name) name
15 #else 15 #else
16 #define MAYBE(name) DISABLED_ ## name 16 #define MAYBE(name) DISABLED_ ## name
17 #endif 17 #endif
18 18
19 TEST(SymmetricKeyTest, MAYBE(GenerateRandomKey)) { 19 TEST(SymmetricKeyTest, MAYBE(GenerateRandomKey)) {
20 scoped_ptr<base::SymmetricKey> key( 20 scoped_ptr<base::SymmetricKey> key(
21 base::SymmetricKey::GenerateRandomKey(base::SymmetricKey::AES, 32)); 21 base::SymmetricKey::GenerateRandomKey(base::SymmetricKey::AES, 256));
22 EXPECT_TRUE(NULL != key.get()); 22 EXPECT_TRUE(NULL != key.get());
23 std::string raw_key;
24 EXPECT_TRUE(key->GetRawKey(&raw_key));
25 EXPECT_EQ(32U, raw_key.size());
26
27 // Do it again and check that the keys are different.
28 // (Note: this has a one-in-10^77 chance of failure!)
29 scoped_ptr<base::SymmetricKey> key2(
30 base::SymmetricKey::GenerateRandomKey(base::SymmetricKey::AES, 256));
31 EXPECT_TRUE(NULL != key2.get());
32 std::string raw_key2;
33 EXPECT_TRUE(key2->GetRawKey(&raw_key2));
34 EXPECT_EQ(32U, raw_key2.size());
35 EXPECT_NE(raw_key, raw_key2);
23 } 36 }
24 37
25 struct PBKDF2TestVector { 38 struct PBKDF2TestVector {
26 const char* password; 39 const char* password;
27 const char* salt; 40 const char* salt;
28 unsigned int rounds; 41 unsigned int rounds;
29 unsigned int key_size; 42 unsigned int key_size_in_bits;
30 const char* expected; 43 const uint8 expected[21]; // string literals need 1 extra NUL byte
31 }; 44 };
32 45
33 // These are the test vectors suggested in: 46 // These are the test vectors suggested in:
34 // http://www.ietf.org/id/draft-josefsson-pbkdf2-test-vectors-00.txt 47 // http://www.ietf.org/id/draft-josefsson-pbkdf2-test-vectors-00.txt
35 static const PBKDF2TestVector test_vectors[] = { 48 static const PBKDF2TestVector test_vectors[] = {
49 // These tests come from
50 // http://www.ietf.org/id/draft-josefsson-pbkdf2-test-vectors-00.txt
36 { 51 {
37 "password", 52 "password",
38 "salt", 53 "salt",
39 1, 54 1,
40 20, 55 160,
41 "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9" 56 "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9"
42 "\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6", 57 "\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6",
43 }, 58 },
44 { 59 {
45 "password", 60 "password",
46 "salt", 61 "salt",
47 2, 62 2,
48 20, 63 160,
49 "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e" 64 "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e"
50 "\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57", 65 "\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57",
51 }, 66 },
52 { 67 {
53 "password", 68 "password",
54 "salt", 69 "salt",
55 4096, 70 4096,
56 20, 71 160,
57 "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad" 72 "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad"
58 "\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1", 73 "\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1",
59 }, 74 },
60 // This test takes over 30s to run on the trybots. 75 // This test takes over 30s to run on the trybots.
61 #if 0 76 #if 0
62 { 77 {
63 "password", 78 "password",
64 "salt", 79 "salt",
65 16777216, 80 16777216,
66 20, 81 160,
67 "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94" 82 "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94"
68 "\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84", 83 "\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84",
69 }, 84 },
70 #endif 85 #endif
86
87 // These tests come from RFC 3962, via BSD source code at
88 // http://www.openbsd.org/cgi-bin/cvsweb/src/sbin/bioctl/pbkdf2.c?rev=HEAD&con tent-type=text/plain
89 {
90 "password",
91 "ATHENA.MIT.EDUraeburn",
92 1,
93 160,
94 {
95 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01,
96 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15,
97 0x0a, 0xd1, 0xf7, 0xa0
98 },
99 },
100 {
101 "password",
102 "ATHENA.MIT.EDUraeburn",
103 2,
104 160,
105 {
106 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e,
107 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d,
108 0xa0, 0x53, 0x78, 0xb9
109 },
110 },
111 {
112 "password",
113 "ATHENA.MIT.EDUraeburn",
114 1200,
115 160,
116 {
117 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e,
118 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b,
119 0xa7, 0xe5, 0x2d, 0xdb
120 },
121 },
122 {
123 "password",
124 "\0224VxxV4\022", /* 0x1234567878563412 */
125 5,
126 160,
127 {
128 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6,
129 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49,
130 0x3f, 0x98, 0xd2, 0x03
131 },
132 },
133 {
134 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
135 "pass phrase equals block size",
136 1200,
137 160,
138 {
139 0x13, 0x9c, 0x30, 0xc0, 0x96, 0x6b, 0xc3, 0x2b,
140 0xa5, 0x5f, 0xdb, 0xf2, 0x12, 0x53, 0x0a, 0xc9,
141 0xc5, 0xec, 0x59, 0xf1
142 },
143 },
144 {
145 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
146 "pass phrase exceeds block size",
147 1200,
148 160,
149 {
150 0x9c, 0xca, 0xd6, 0xd4, 0x68, 0x77, 0x0c, 0xd5,
151 0x1b, 0x10, 0xe6, 0xa6, 0x87, 0x21, 0xbe, 0x61,
152 0x1a, 0x8b, 0x4d, 0x28
153 },
154 },
155 {
156 "\360\235\204\236", /* g-clef (0xf09d849e) */
157 "EXAMPLE.COMpianist",
158 50,
159 160,
160 {
161 0x6b, 0x9c, 0xf2, 0x6d, 0x45, 0x45, 0x5a, 0x43,
162 0xa5, 0xb8, 0xbb, 0x27, 0x6a, 0x40, 0x3b, 0x39,
163 0xe7, 0xfe, 0x37, 0xa0
164 },
165 }
71 }; 166 };
72 167
73 TEST(SymmetricKeyTest, MAYBE(DeriveKeyFromPassword)) { 168 TEST(SymmetricKeyTest, MAYBE(DeriveKeyFromPassword)) {
74 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(test_vectors); ++i) { 169 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(test_vectors); ++i) {
75 SCOPED_TRACE(StringPrintf("Test[%u]", i)); 170 SCOPED_TRACE(StringPrintf("Test[%u]", i));
171 #if defined(OS_MACOSX)
172 // 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.
174 if (strlen(test_vectors[i].salt) < 8 || test_vectors[i].rounds < 1000) {
175 LOG(INFO) << "Skipped test vector #" << i;
176 continue;
177 }
178 #endif // OS_MACOSX
76 scoped_ptr<base::SymmetricKey> key( 179 scoped_ptr<base::SymmetricKey> key(
77 base::SymmetricKey::DeriveKeyFromPassword( 180 base::SymmetricKey::DeriveKeyFromPassword(
78 base::SymmetricKey::HMAC_SHA1, 181 base::SymmetricKey::HMAC_SHA1,
79 test_vectors[i].password, test_vectors[i].salt, 182 test_vectors[i].password, test_vectors[i].salt,
80 test_vectors[i].rounds, test_vectors[i].key_size)); 183 test_vectors[i].rounds, test_vectors[i].key_size_in_bits));
81 EXPECT_TRUE(NULL != key.get()); 184 ASSERT_TRUE(NULL != key.get());
82 185
83 std::string raw_key; 186 std::string raw_key;
84 key->GetRawKey(&raw_key); 187 key->GetRawKey(&raw_key);
85 EXPECT_EQ(test_vectors[i].key_size, raw_key.size()); 188 EXPECT_EQ(test_vectors[i].key_size_in_bits / 8, raw_key.size());
86 EXPECT_STREQ(test_vectors[i].expected, raw_key.c_str()); 189 EXPECT_EQ(0, memcmp(test_vectors[i].expected,
190 raw_key.data(),
191 raw_key.size()));
87 } 192 }
88 } 193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698