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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/hmac.h" | 7 #include "crypto/hmac.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 static const int kSHA1DigestSize = 20; | 10 static const int kSHA1DigestSize = 20; |
11 static const int kSHA256DigestSize = 32; | 11 static const int kSHA256DigestSize = 32; |
12 | 12 |
13 TEST(HMACTest, HmacSafeBrowsingResponseTest) { | 13 TEST(HMACTest, HmacSafeBrowsingResponseTest) { |
14 const int kKeySize = 16; | 14 const int kKeySize = 16; |
15 | 15 |
16 // Client key. | 16 // Client key. |
17 const unsigned char kClientKey[kKeySize] = | 17 const unsigned char kClientKey[kKeySize] = |
(...skipping 27 matching lines...) Expand all Loading... |
45 "wsing/rd/goog-malware-shavar_a_937-972\nu:s.ytimg.com/safebrowsing/rd/goog-mal" | 45 "wsing/rd/goog-malware-shavar_a_937-972\nu:s.ytimg.com/safebrowsing/rd/goog-mal" |
46 "ware-shavar_a_931-936\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_925" | 46 "ware-shavar_a_931-936\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_925" |
47 "-930\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_919-924\ni:goog-phis" | 47 "-930\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_919-924\ni:goog-phis" |
48 "h-shavar\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2633\nu:s.ytimg.co" | 48 "h-shavar\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2633\nu:s.ytimg.co" |
49 "m/safebrowsing/rd/goog-phish-shavar_a_2632\nu:s.ytimg.com/safebrowsing/rd/goog" | 49 "m/safebrowsing/rd/goog-phish-shavar_a_2632\nu:s.ytimg.com/safebrowsing/rd/goog" |
50 "-phish-shavar_a_2629-2631\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2" | 50 "-phish-shavar_a_2629-2631\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2" |
51 "626-2628\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2625\n"; | 51 "626-2628\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2625\n"; |
52 | 52 |
53 std::string message_data(kMessage); | 53 std::string message_data(kMessage); |
54 | 54 |
55 base::HMAC hmac(base::HMAC::SHA1); | 55 crypto::HMAC hmac(crypto::HMAC::SHA1); |
56 ASSERT_TRUE(hmac.Init(kClientKey, kKeySize)); | 56 ASSERT_TRUE(hmac.Init(kClientKey, kKeySize)); |
57 unsigned char calculated_hmac[kSHA1DigestSize]; | 57 unsigned char calculated_hmac[kSHA1DigestSize]; |
58 | 58 |
59 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize)); | 59 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize)); |
60 EXPECT_EQ(0, memcmp(kReceivedHmac, calculated_hmac, kSHA1DigestSize)); | 60 EXPECT_EQ(0, memcmp(kReceivedHmac, calculated_hmac, kSHA1DigestSize)); |
61 } | 61 } |
62 | 62 |
63 // Test cases from RFC 2202 section 3 | 63 // Test cases from RFC 2202 section 3 |
64 TEST(HMACTest, RFC2202TestCases) { | 64 TEST(HMACTest, RFC2202TestCases) { |
65 const struct { | 65 const struct { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | 114 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
115 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", | 115 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", |
116 80, | 116 80, |
117 "Test Using Larger Than Block-Size Key and Larger " | 117 "Test Using Larger Than Block-Size Key and Larger " |
118 "Than One Block-Size Data", 73, | 118 "Than One Block-Size Data", 73, |
119 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" | 119 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" |
120 "\xBB\xFF\x1A\x91" } | 120 "\xBB\xFF\x1A\x91" } |
121 }; | 121 }; |
122 | 122 |
123 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 123 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
124 base::HMAC hmac(base::HMAC::SHA1); | 124 crypto::HMAC hmac(crypto::HMAC::SHA1); |
125 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(cases[i].key), | 125 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(cases[i].key), |
126 cases[i].key_len)); | 126 cases[i].key_len)); |
127 std::string data_string(cases[i].data, cases[i].data_len); | 127 std::string data_string(cases[i].data, cases[i].data_len); |
128 unsigned char digest[kSHA1DigestSize]; | 128 unsigned char digest[kSHA1DigestSize]; |
129 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); | 129 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); |
130 EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize)); | 130 EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize)); |
131 } | 131 } |
132 } | 132 } |
133 | 133 |
134 // TODO(wtc): add other test vectors from RFC 4231. | 134 // TODO(wtc): add other test vectors from RFC 4231. |
135 TEST(HMACTest, RFC4231TestCase6) { | 135 TEST(HMACTest, RFC4231TestCase6) { |
136 unsigned char key[131]; | 136 unsigned char key[131]; |
137 for (size_t i = 0; i < sizeof(key); ++i) | 137 for (size_t i = 0; i < sizeof(key); ++i) |
138 key[i] = 0xaa; | 138 key[i] = 0xaa; |
139 | 139 |
140 std::string data = "Test Using Larger Than Block-Size Key - Hash Key First"; | 140 std::string data = "Test Using Larger Than Block-Size Key - Hash Key First"; |
141 ASSERT_EQ(54U, data.size()); | 141 ASSERT_EQ(54U, data.size()); |
142 | 142 |
143 static unsigned char kKnownHMACSHA256[] = { | 143 static unsigned char kKnownHMACSHA256[] = { |
144 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, | 144 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, |
145 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, | 145 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, |
146 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, | 146 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, |
147 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 | 147 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 |
148 }; | 148 }; |
149 | 149 |
150 base::HMAC hmac(base::HMAC::SHA256); | 150 crypto::HMAC hmac(crypto::HMAC::SHA256); |
151 ASSERT_TRUE(hmac.Init(key, sizeof(key))); | 151 ASSERT_TRUE(hmac.Init(key, sizeof(key))); |
152 unsigned char calculated_hmac[kSHA256DigestSize]; | 152 unsigned char calculated_hmac[kSHA256DigestSize]; |
153 | 153 |
154 EXPECT_TRUE(hmac.Sign(data, calculated_hmac, kSHA256DigestSize)); | 154 EXPECT_TRUE(hmac.Sign(data, calculated_hmac, kSHA256DigestSize)); |
155 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac, kSHA256DigestSize)); | 155 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac, kSHA256DigestSize)); |
156 } | 156 } |
157 | 157 |
158 // Based on NSS's FIPS HMAC power-up self-test. | 158 // Based on NSS's FIPS HMAC power-up self-test. |
159 TEST(HMACTest, NSSFIPSPowerUpSelfTest) { | 159 TEST(HMACTest, NSSFIPSPowerUpSelfTest) { |
160 static const char kKnownMessage[] = | 160 static const char kKnownMessage[] = |
(...skipping 19 matching lines...) Expand all Loading... |
180 // HMAC-SHA-256 known answer (32 bytes). | 180 // HMAC-SHA-256 known answer (32 bytes). |
181 static const unsigned char kKnownHMACSHA256[] = { | 181 static const unsigned char kKnownHMACSHA256[] = { |
182 0x05, 0x75, 0x9a, 0x9e, 0x70, 0x5e, 0xe7, 0x44, | 182 0x05, 0x75, 0x9a, 0x9e, 0x70, 0x5e, 0xe7, 0x44, |
183 0xe2, 0x46, 0x4b, 0x92, 0x22, 0x14, 0x22, 0xe0, | 183 0xe2, 0x46, 0x4b, 0x92, 0x22, 0x14, 0x22, 0xe0, |
184 0x1b, 0x92, 0x8a, 0x0c, 0xfe, 0xf5, 0x49, 0xe9, | 184 0x1b, 0x92, 0x8a, 0x0c, 0xfe, 0xf5, 0x49, 0xe9, |
185 0xa7, 0x1b, 0x56, 0x7d, 0x1d, 0x29, 0x40, 0x48 | 185 0xa7, 0x1b, 0x56, 0x7d, 0x1d, 0x29, 0x40, 0x48 |
186 }; | 186 }; |
187 | 187 |
188 std::string message_data(kKnownMessage); | 188 std::string message_data(kKnownMessage); |
189 | 189 |
190 base::HMAC hmac(base::HMAC::SHA1); | 190 crypto::HMAC hmac(crypto::HMAC::SHA1); |
191 ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize)); | 191 ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize)); |
192 unsigned char calculated_hmac[kSHA1DigestSize]; | 192 unsigned char calculated_hmac[kSHA1DigestSize]; |
193 | 193 |
194 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize)); | 194 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize)); |
195 EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize)); | 195 EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize)); |
196 | 196 |
197 base::HMAC hmac2(base::HMAC::SHA256); | 197 crypto::HMAC hmac2(crypto::HMAC::SHA256); |
198 ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize)); | 198 ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize)); |
199 unsigned char calculated_hmac2[kSHA256DigestSize]; | 199 unsigned char calculated_hmac2[kSHA256DigestSize]; |
200 | 200 |
201 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize)); | 201 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize)); |
202 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize)); | 202 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize)); |
203 } | 203 } |
204 | 204 |
205 TEST(HMACTest, HMACObjectReuse) { | 205 TEST(HMACTest, HMACObjectReuse) { |
206 const char *key = | 206 const char *key = |
207 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | 207 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
(...skipping 10 matching lines...) Expand all Loading... |
218 } cases[] = { | 218 } cases[] = { |
219 { "Test Using Larger Than Block-Size Key - Hash Key First", 54, | 219 { "Test Using Larger Than Block-Size Key - Hash Key First", 54, |
220 "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55" | 220 "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55" |
221 "\xED\x40\x21\x12" }, | 221 "\xED\x40\x21\x12" }, |
222 { "Test Using Larger Than Block-Size Key and Larger " | 222 { "Test Using Larger Than Block-Size Key and Larger " |
223 "Than One Block-Size Data", 73, | 223 "Than One Block-Size Data", 73, |
224 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" | 224 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" |
225 "\xBB\xFF\x1A\x91" } | 225 "\xBB\xFF\x1A\x91" } |
226 }; | 226 }; |
227 | 227 |
228 base::HMAC hmac(base::HMAC::SHA1); | 228 crypto::HMAC hmac(crypto::HMAC::SHA1); |
229 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(key), key_len)); | 229 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(key), key_len)); |
230 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 230 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
231 std::string data_string(cases[i].data, cases[i].data_len); | 231 std::string data_string(cases[i].data, cases[i].data_len); |
232 unsigned char digest[kSHA1DigestSize]; | 232 unsigned char digest[kSHA1DigestSize]; |
233 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); | 233 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); |
234 EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize)); | 234 EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize)); |
235 } | 235 } |
236 } | 236 } |
OLD | NEW |