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; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 "\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" |
136 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" | 136 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" |
137 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", | 137 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", |
138 80, | 138 80, |
139 "Test Using Larger Than Block-Size Key and Larger " | 139 "Test Using Larger Than Block-Size Key and Larger " |
140 "Than One Block-Size Data", 73, | 140 "Than One Block-Size Data", 73, |
141 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" | 141 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08" |
142 "\xBB\xFF\x1A\x91" } | 142 "\xBB\xFF\x1A\x91" } |
143 }; | 143 }; |
144 | 144 |
145 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 145 for (size_t i = 0; i < arraysize(cases); ++i) { |
146 crypto::HMAC hmac(crypto::HMAC::SHA1); | 146 crypto::HMAC hmac(crypto::HMAC::SHA1); |
147 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(cases[i].key), | 147 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(cases[i].key), |
148 cases[i].key_len)); | 148 cases[i].key_len)); |
149 std::string data_string(cases[i].data, cases[i].data_len); | 149 std::string data_string(cases[i].data, cases[i].data_len); |
150 unsigned char digest[kSHA1DigestSize]; | 150 unsigned char digest[kSHA1DigestSize]; |
151 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); | 151 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); |
152 EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize)); | 152 EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize)); |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 232 |
233 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize)); | 233 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize)); |
234 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize)); | 234 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize)); |
235 } | 235 } |
236 | 236 |
237 TEST(HMACTest, HMACObjectReuse) { | 237 TEST(HMACTest, HMACObjectReuse) { |
238 crypto::HMAC hmac(crypto::HMAC::SHA1); | 238 crypto::HMAC hmac(crypto::HMAC::SHA1); |
239 ASSERT_TRUE( | 239 ASSERT_TRUE( |
240 hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey), | 240 hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey), |
241 kSimpleKeyLength)); | 241 kSimpleKeyLength)); |
242 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSimpleHmacCases); ++i) { | 242 for (size_t i = 0; i < arraysize(kSimpleHmacCases); ++i) { |
243 std::string data_string(kSimpleHmacCases[i].data, | 243 std::string data_string(kSimpleHmacCases[i].data, |
244 kSimpleHmacCases[i].data_len); | 244 kSimpleHmacCases[i].data_len); |
245 unsigned char digest[kSHA1DigestSize]; | 245 unsigned char digest[kSHA1DigestSize]; |
246 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); | 246 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); |
247 EXPECT_EQ(0, memcmp(kSimpleHmacCases[i].digest, digest, kSHA1DigestSize)); | 247 EXPECT_EQ(0, memcmp(kSimpleHmacCases[i].digest, digest, kSHA1DigestSize)); |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 TEST(HMACTest, Verify) { | 251 TEST(HMACTest, Verify) { |
252 crypto::HMAC hmac(crypto::HMAC::SHA1); | 252 crypto::HMAC hmac(crypto::HMAC::SHA1); |
253 ASSERT_TRUE( | 253 ASSERT_TRUE( |
254 hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey), | 254 hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey), |
255 kSimpleKeyLength)); | 255 kSimpleKeyLength)); |
256 const char empty_digest[kSHA1DigestSize] = { 0 }; | 256 const char empty_digest[kSHA1DigestSize] = { 0 }; |
257 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSimpleHmacCases); ++i) { | 257 for (size_t i = 0; i < arraysize(kSimpleHmacCases); ++i) { |
258 // Expected results | 258 // Expected results |
259 EXPECT_TRUE(hmac.Verify( | 259 EXPECT_TRUE(hmac.Verify( |
260 base::StringPiece(kSimpleHmacCases[i].data, | 260 base::StringPiece(kSimpleHmacCases[i].data, |
261 kSimpleHmacCases[i].data_len), | 261 kSimpleHmacCases[i].data_len), |
262 base::StringPiece(kSimpleHmacCases[i].digest, | 262 base::StringPiece(kSimpleHmacCases[i].digest, |
263 kSHA1DigestSize))); | 263 kSHA1DigestSize))); |
264 // Mismatched size | 264 // Mismatched size |
265 EXPECT_FALSE(hmac.Verify( | 265 EXPECT_FALSE(hmac.Verify( |
266 base::StringPiece(kSimpleHmacCases[i].data, | 266 base::StringPiece(kSimpleHmacCases[i].data, |
267 kSimpleHmacCases[i].data_len), | 267 kSimpleHmacCases[i].data_len), |
(...skipping 18 matching lines...) Expand all Loading... |
286 crypto::HMAC hmac(crypto::HMAC::SHA1); | 286 crypto::HMAC hmac(crypto::HMAC::SHA1); |
287 ASSERT_TRUE(hmac.Init(NULL, 0)); | 287 ASSERT_TRUE(hmac.Init(NULL, 0)); |
288 | 288 |
289 unsigned char digest[kSHA1DigestSize]; | 289 unsigned char digest[kSHA1DigestSize]; |
290 EXPECT_TRUE(hmac.Sign(data, digest, kSHA1DigestSize)); | 290 EXPECT_TRUE(hmac.Sign(data, digest, kSHA1DigestSize)); |
291 EXPECT_EQ(0, memcmp(kExpectedDigest, digest, kSHA1DigestSize)); | 291 EXPECT_EQ(0, memcmp(kExpectedDigest, digest, kSHA1DigestSize)); |
292 | 292 |
293 EXPECT_TRUE(hmac.Verify( | 293 EXPECT_TRUE(hmac.Verify( |
294 data, base::StringPiece(kExpectedDigest, kSHA1DigestSize))); | 294 data, base::StringPiece(kExpectedDigest, kSHA1DigestSize))); |
295 } | 295 } |
OLD | NEW |