| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/hmac.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 static const int kDigestSize = 20; | 10 static const int kDigestSize = 20; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 "wsing/rd/goog-malware-shavar_a_937-972\nu:s.ytimg.com/safebrowsing/rd/goog-mal" | 44 "wsing/rd/goog-malware-shavar_a_937-972\nu:s.ytimg.com/safebrowsing/rd/goog-mal" |
| 45 "ware-shavar_a_931-936\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_925" | 45 "ware-shavar_a_931-936\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_925" |
| 46 "-930\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_919-924\ni:goog-phis" | 46 "-930\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_919-924\ni:goog-phis" |
| 47 "h-shavar\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2633\nu:s.ytimg.co" | 47 "h-shavar\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2633\nu:s.ytimg.co" |
| 48 "m/safebrowsing/rd/goog-phish-shavar_a_2632\nu:s.ytimg.com/safebrowsing/rd/goog" | 48 "m/safebrowsing/rd/goog-phish-shavar_a_2632\nu:s.ytimg.com/safebrowsing/rd/goog" |
| 49 "-phish-shavar_a_2629-2631\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2" | 49 "-phish-shavar_a_2629-2631\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2" |
| 50 "626-2628\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2625\n"; | 50 "626-2628\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2625\n"; |
| 51 | 51 |
| 52 std::string message_data(kMessage); | 52 std::string message_data(kMessage); |
| 53 | 53 |
| 54 base::HMAC hmac(base::HMAC::SHA1, kClientKey, kKeySize); | 54 base::HMAC hmac(base::HMAC::SHA1); |
| 55 ASSERT_TRUE(hmac.Init(kClientKey, kKeySize)); |
| 55 unsigned char calculated_hmac[kDigestSize]; | 56 unsigned char calculated_hmac[kDigestSize]; |
| 56 | 57 |
| 57 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kDigestSize)); | 58 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kDigestSize)); |
| 58 EXPECT_EQ(memcmp(kReceivedHmac, calculated_hmac, kDigestSize), 0); | 59 EXPECT_EQ(memcmp(kReceivedHmac, calculated_hmac, kDigestSize), 0); |
| 59 } | 60 } |
| 60 | 61 |
| 61 // Test cases from RFC 2202 section 3 | 62 // Test cases from RFC 2202 section 3 |
| 62 TEST(HMACTest, RFC2202TestCases) { | 63 TEST(HMACTest, RFC2202TestCases) { |
| 63 const struct { | 64 const struct { |
| 64 const char *key; | 65 const char *key; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | 113 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
| 113 "\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", |
| 114 80, | 115 80, |
| 115 "Test Using Larger Than Block-Size Key and Larger " | 116 "Test Using Larger Than Block-Size Key and Larger " |
| 116 "Than One Block-Size Data", 73, | 117 "Than One Block-Size Data", 73, |
| 117 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" | 118 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" |
| 118 "\xBB\xFF\x1A\x91" } | 119 "\xBB\xFF\x1A\x91" } |
| 119 }; | 120 }; |
| 120 | 121 |
| 121 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 122 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 122 base::HMAC hmac(base::HMAC::SHA1, | 123 base::HMAC hmac(base::HMAC::SHA1); |
| 123 reinterpret_cast<const unsigned char*>(cases[i].key), | 124 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(cases[i].key), |
| 124 cases[i].key_len); | 125 cases[i].key_len)); |
| 125 std::string data_string(cases[i].data, cases[i].data_len); | 126 std::string data_string(cases[i].data, cases[i].data_len); |
| 126 unsigned char digest[kDigestSize]; | 127 unsigned char digest[kDigestSize]; |
| 127 EXPECT_TRUE(hmac.Sign(data_string, digest, kDigestSize)); | 128 EXPECT_TRUE(hmac.Sign(data_string, digest, kDigestSize)); |
| 128 EXPECT_EQ(memcmp(cases[i].digest, digest, kDigestSize), 0); | 129 EXPECT_EQ(memcmp(cases[i].digest, digest, kDigestSize), 0); |
| 129 } | 130 } |
| 130 } | 131 } |
| 131 | 132 |
| 132 TEST(HMACTest, HMACObjectReuse) { | 133 TEST(HMACTest, HMACObjectReuse) { |
| 133 const char *key = | 134 const char *key = |
| 134 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | 135 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 145 } cases[] = { | 146 } cases[] = { |
| 146 { "Test Using Larger Than Block-Size Key - Hash Key First", 54, | 147 { "Test Using Larger Than Block-Size Key - Hash Key First", 54, |
| 147 "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55" | 148 "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55" |
| 148 "\xED\x40\x21\x12" }, | 149 "\xED\x40\x21\x12" }, |
| 149 { "Test Using Larger Than Block-Size Key and Larger " | 150 { "Test Using Larger Than Block-Size Key and Larger " |
| 150 "Than One Block-Size Data", 73, | 151 "Than One Block-Size Data", 73, |
| 151 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" | 152 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" |
| 152 "\xBB\xFF\x1A\x91" } | 153 "\xBB\xFF\x1A\x91" } |
| 153 }; | 154 }; |
| 154 | 155 |
| 155 base::HMAC hmac(base::HMAC::SHA1, | 156 base::HMAC hmac(base::HMAC::SHA1); |
| 156 reinterpret_cast<const unsigned char*>(key), key_len); | 157 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(key), key_len)); |
| 157 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 158 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 158 std::string data_string(cases[i].data, cases[i].data_len); | 159 std::string data_string(cases[i].data, cases[i].data_len); |
| 159 unsigned char digest[kDigestSize]; | 160 unsigned char digest[kDigestSize]; |
| 160 EXPECT_TRUE(hmac.Sign(data_string, digest, kDigestSize)); | 161 EXPECT_TRUE(hmac.Sign(data_string, digest, kDigestSize)); |
| 161 EXPECT_EQ(memcmp(cases[i].digest, digest, kDigestSize), 0); | 162 EXPECT_EQ(memcmp(cases[i].digest, digest, kDigestSize), 0); |
| 162 } | 163 } |
| 163 } | 164 } |
| OLD | NEW |