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 <string> | 5 #include <string> |
6 | 6 |
7 #include "crypto/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 size_t kSHA1DigestSize = 20; | 10 static const size_t kSHA1DigestSize = 20; |
11 static const size_t kSHA256DigestSize = 32; | 11 static const size_t kSHA256DigestSize = 32; |
12 | 12 |
| 13 static const char* kSimpleKey = |
| 14 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
| 15 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
| 16 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
| 17 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
| 18 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"; |
| 19 static const int kSimpleKeyLength = 80; |
| 20 |
| 21 static const struct { |
| 22 const char *data; |
| 23 const int data_len; |
| 24 const char *digest; |
| 25 } kSimpleHmacCases[] = { |
| 26 { "Test Using Larger Than Block-Size Key - Hash Key First", 54, |
| 27 "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55" |
| 28 "\xED\x40\x21\x12" }, |
| 29 { "Test Using Larger Than Block-Size Key and Larger " |
| 30 "Than One Block-Size Data", 73, |
| 31 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" |
| 32 "\xBB\xFF\x1A\x91" } |
| 33 }; |
| 34 |
13 TEST(HMACTest, HmacSafeBrowsingResponseTest) { | 35 TEST(HMACTest, HmacSafeBrowsingResponseTest) { |
14 const int kKeySize = 16; | 36 const int kKeySize = 16; |
15 | 37 |
16 // Client key. | 38 // Client key. |
17 const unsigned char kClientKey[kKeySize] = | 39 const unsigned char kClientKey[kKeySize] = |
18 { 0xbf, 0xf6, 0x83, 0x4b, 0x3e, 0xa3, 0x23, 0xdd, | 40 { 0xbf, 0xf6, 0x83, 0x4b, 0x3e, 0xa3, 0x23, 0xdd, |
19 0x96, 0x78, 0x70, 0x8e, 0xa1, 0x9d, 0x3b, 0x40 }; | 41 0x96, 0x78, 0x70, 0x8e, 0xa1, 0x9d, 0x3b, 0x40 }; |
20 | 42 |
21 // Expected HMAC result using kMessage and kClientKey. | 43 // Expected HMAC result using kMessage and kClientKey. |
22 const unsigned char kReceivedHmac[kSHA1DigestSize] = | 44 const unsigned char kReceivedHmac[kSHA1DigestSize] = |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 | 210 |
189 std::string message_data(kKnownMessage); | 211 std::string message_data(kKnownMessage); |
190 | 212 |
191 crypto::HMAC hmac(crypto::HMAC::SHA1); | 213 crypto::HMAC hmac(crypto::HMAC::SHA1); |
192 ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize)); | 214 ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize)); |
193 unsigned char calculated_hmac[kSHA1DigestSize]; | 215 unsigned char calculated_hmac[kSHA1DigestSize]; |
194 | 216 |
195 EXPECT_EQ(kSHA1DigestSize, hmac.DigestLength()); | 217 EXPECT_EQ(kSHA1DigestSize, hmac.DigestLength()); |
196 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize)); | 218 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize)); |
197 EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize)); | 219 EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize)); |
| 220 EXPECT_TRUE(hmac.Verify( |
| 221 message_data, |
| 222 base::StringPiece(reinterpret_cast<const char*>(kKnownHMACSHA1), |
| 223 kSHA1DigestSize))); |
198 | 224 |
199 crypto::HMAC hmac2(crypto::HMAC::SHA256); | 225 crypto::HMAC hmac2(crypto::HMAC::SHA256); |
200 ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize)); | 226 ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize)); |
201 unsigned char calculated_hmac2[kSHA256DigestSize]; | 227 unsigned char calculated_hmac2[kSHA256DigestSize]; |
202 | 228 |
203 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize)); | 229 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize)); |
204 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize)); | 230 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize)); |
205 } | 231 } |
206 | 232 |
207 TEST(HMACTest, HMACObjectReuse) { | 233 TEST(HMACTest, HMACObjectReuse) { |
208 const char *key = | |
209 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | |
210 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | |
211 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | |
212 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | |
213 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"; | |
214 const int key_len = 80; | |
215 | |
216 const struct { | |
217 const char *data; | |
218 const int data_len; | |
219 const char *digest; | |
220 } cases[] = { | |
221 { "Test Using Larger Than Block-Size Key - Hash Key First", 54, | |
222 "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55" | |
223 "\xED\x40\x21\x12" }, | |
224 { "Test Using Larger Than Block-Size Key and Larger " | |
225 "Than One Block-Size Data", 73, | |
226 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" | |
227 "\xBB\xFF\x1A\x91" } | |
228 }; | |
229 | |
230 crypto::HMAC hmac(crypto::HMAC::SHA1); | 234 crypto::HMAC hmac(crypto::HMAC::SHA1); |
231 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(key), key_len)); | 235 ASSERT_TRUE( |
232 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 236 hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey), |
233 std::string data_string(cases[i].data, cases[i].data_len); | 237 kSimpleKeyLength)); |
| 238 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSimpleHmacCases); ++i) { |
| 239 std::string data_string(kSimpleHmacCases[i].data, |
| 240 kSimpleHmacCases[i].data_len); |
234 unsigned char digest[kSHA1DigestSize]; | 241 unsigned char digest[kSHA1DigestSize]; |
235 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); | 242 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); |
236 EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize)); | 243 EXPECT_EQ(0, memcmp(kSimpleHmacCases[i].digest, digest, kSHA1DigestSize)); |
237 } | 244 } |
238 } | 245 } |
| 246 |
| 247 TEST(HMACTest, Verify) { |
| 248 crypto::HMAC hmac(crypto::HMAC::SHA1); |
| 249 ASSERT_TRUE( |
| 250 hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey), |
| 251 kSimpleKeyLength)); |
| 252 const char empty_digest[kSHA1DigestSize] = { 0 }; |
| 253 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSimpleHmacCases); ++i) { |
| 254 // Expected results |
| 255 EXPECT_TRUE(hmac.Verify( |
| 256 base::StringPiece(kSimpleHmacCases[i].data, |
| 257 kSimpleHmacCases[i].data_len), |
| 258 base::StringPiece(kSimpleHmacCases[i].digest, |
| 259 kSHA1DigestSize))); |
| 260 // Mismatched size |
| 261 EXPECT_FALSE(hmac.Verify( |
| 262 base::StringPiece(kSimpleHmacCases[i].data, |
| 263 kSimpleHmacCases[i].data_len), |
| 264 base::StringPiece(kSimpleHmacCases[i].data, |
| 265 kSimpleHmacCases[i].data_len))); |
| 266 |
| 267 // Expected size, mismatched data |
| 268 EXPECT_FALSE(hmac.Verify( |
| 269 base::StringPiece(kSimpleHmacCases[i].data, |
| 270 kSimpleHmacCases[i].data_len), |
| 271 base::StringPiece(empty_digest, kSHA1DigestSize))); |
| 272 } |
| 273 } |
OLD | NEW |