summaryrefslogtreecommitdiffstats
path: root/base/hmac_unittest.cc
diff options
context:
space:
mode:
authormmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-04 19:00:37 +0000
committermmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-04 19:00:37 +0000
commit301415ea99531af12cf34568c908fdbdd128bcda (patch)
treebeb76fa0ee445fa569e212dcfbe8222528fe739c /base/hmac_unittest.cc
parent261d17188f9c63f82ea326f510c2fddb477c9c97 (diff)
downloadchromium_src-301415ea99531af12cf34568c908fdbdd128bcda.zip
chromium_src-301415ea99531af12cf34568c908fdbdd128bcda.tar.gz
chromium_src-301415ea99531af12cf34568c908fdbdd128bcda.tar.bz2
HMAC-SHA1 implementation for Mac based on CommonCrypto;
allow Windows HMAC-SHA1 to use keys longer than 16 bytes. Review URL: http://codereview.chromium.org/218 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1724 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/hmac_unittest.cc')
-rw-r--r--base/hmac_unittest.cc131
1 files changed, 118 insertions, 13 deletions
diff --git a/base/hmac_unittest.cc b/base/hmac_unittest.cc
index 3ff2f9f..9881369 100644
--- a/base/hmac_unittest.cc
+++ b/base/hmac_unittest.cc
@@ -7,21 +7,23 @@
#include "base/hmac.h"
#include "testing/gtest/include/gtest/gtest.h"
-static const int kKeySize = 16;
static const int kDigestSize = 20;
-// Client key.
-const unsigned char kClientKey[kKeySize] =
- { 0xbf, 0xf6, 0x83, 0x4b, 0x3e, 0xa3, 0x23, 0xdd,
- 0x96, 0x78, 0x70, 0x8e, 0xa1, 0x9d, 0x3b, 0x40 };
+TEST(HMACTest, HmacSafeBrowsingResponseTest) {
+ const int kKeySize = 16;
+
+ // Client key.
+ const unsigned char kClientKey[kKeySize] =
+ { 0xbf, 0xf6, 0x83, 0x4b, 0x3e, 0xa3, 0x23, 0xdd,
+ 0x96, 0x78, 0x70, 0x8e, 0xa1, 0x9d, 0x3b, 0x40 };
-// Expected HMAC result using kMessage and kClientKey.
-const unsigned char kReceivedHmac[kDigestSize] =
- { 0xb9, 0x3c, 0xd6, 0xf0, 0x49, 0x47, 0xe2, 0x52,
- 0x59, 0x7a, 0xbd, 0x1f, 0x2b, 0x4c, 0x83, 0xad,
- 0x86, 0xd2, 0x48, 0x85 };
+ // Expected HMAC result using kMessage and kClientKey.
+ const unsigned char kReceivedHmac[kDigestSize] =
+ { 0xb9, 0x3c, 0xd6, 0xf0, 0x49, 0x47, 0xe2, 0x52,
+ 0x59, 0x7a, 0xbd, 0x1f, 0x2b, 0x4c, 0x83, 0xad,
+ 0x86, 0xd2, 0x48, 0x85 };
-const char kMessage[] =
+ const char kMessage[] =
"n:1896\ni:goog-malware-shavar\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shav"
"ar_s_445-450\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_439-444\nu:s"
".ytimg.com/safebrowsing/rd/goog-malware-shavar_s_437\nu:s.ytimg.com/safebrowsi"
@@ -47,12 +49,115 @@ const char kMessage[] =
"-phish-shavar_a_2629-2631\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2"
"626-2628\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2625\n";
-TEST(HMACTest, HmacSafeBrowsingResponseTest) {
std::string message_data(kMessage);
- HMAC hmac(HMAC::SHA1, kClientKey, kKeySize);
+ base::HMAC hmac(base::HMAC::SHA1, kClientKey, kKeySize);
unsigned char calculated_hmac[kDigestSize];
EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kDigestSize));
EXPECT_EQ(memcmp(kReceivedHmac, calculated_hmac, kDigestSize), 0);
}
+
+// Test cases from RFC 2202 section 3
+TEST(HMACTest, RFC2202TestCases) {
+ const struct {
+ const char *key;
+ const int key_len;
+ const char *data;
+ const int data_len;
+ const char *digest;
+ } cases[] = {
+ { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0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xAA\xAA\xAA\xAA\xAA\xAA",
+ 80,
+ "Test Using Larger Than Block-Size Key and Larger "
+ "Than One Block-Size Data", 73,
+ "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08"
+ "\xBB\xFF\x1A\x91" }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ base::HMAC hmac(base::HMAC::SHA1,
+ reinterpret_cast<const unsigned char*>(cases[i].key),
+ cases[i].key_len);
+ std::string data_string(cases[i].data, cases[i].data_len);
+ unsigned char digest[kDigestSize];
+ EXPECT_TRUE(hmac.Sign(data_string, digest, kDigestSize));
+ EXPECT_EQ(memcmp(cases[i].digest, digest, kDigestSize), 0);
+ }
+}
+
+TEST(HMACTest, HMACObjectReuse) {
+ const char *key =
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
+ const int key_len = 80;
+
+ const struct {
+ const char *data;
+ const int data_len;
+ const char *digest;
+ } cases[] = {
+ { "Test Using Larger Than Block-Size Key - Hash Key First", 54,
+ "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55"
+ "\xED\x40\x21\x12" },
+ { "Test Using Larger Than Block-Size Key and Larger "
+ "Than One Block-Size Data", 73,
+ "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08"
+ "\xBB\xFF\x1A\x91" }
+ };
+
+ base::HMAC hmac(base::HMAC::SHA1,
+ reinterpret_cast<const unsigned char*>(key), key_len);
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ std::string data_string(cases[i].data, cases[i].data_len);
+ unsigned char digest[kDigestSize];
+ EXPECT_TRUE(hmac.Sign(data_string, digest, kDigestSize));
+ EXPECT_EQ(memcmp(cases[i].digest, digest, kDigestSize), 0);
+ }
+}