Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: crypto/hmac_unittest.cc

Issue 7277024: Add a Verify routine for HMAC (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« crypto/hmac.cc ('K') | « crypto/hmac.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/string_piece.h"
wtc 2011/07/14 00:00:22 "base/string_piece.h" doesn't need to be included.
7 #include "crypto/hmac.h" 8 #include "crypto/hmac.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 10
11 namespace {
12
10 static const size_t kSHA1DigestSize = 20; 13 static const size_t kSHA1DigestSize = 20;
11 static const size_t kSHA256DigestSize = 32; 14 static const size_t kSHA256DigestSize = 32;
12 15
16 static const char* kSimpleKey =
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 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
20 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
21 "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
22 static const int kSimpleKeyLength = 80;
23
24 static const struct {
25 const char *data;
26 const int data_len;
27 const char *digest;
28 } kSimpleHmacCases[] = {
29 { "Test Using Larger Than Block-Size Key - Hash Key First", 54,
30 "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55"
31 "\xED\x40\x21\x12" },
32 { "Test Using Larger Than Block-Size Key and Larger "
33 "Than One Block-Size Data", 73,
34 "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08"
35 "\xBB\xFF\x1A\x91" }
36 };
37 }
wtc 2011/07/14 00:00:22 Add a blank line before this line. Add a namespac
38
13 TEST(HMACTest, HmacSafeBrowsingResponseTest) { 39 TEST(HMACTest, HmacSafeBrowsingResponseTest) {
14 const int kKeySize = 16; 40 const int kKeySize = 16;
15 41
16 // Client key. 42 // Client key.
17 const unsigned char kClientKey[kKeySize] = 43 const unsigned char kClientKey[kKeySize] =
18 { 0xbf, 0xf6, 0x83, 0x4b, 0x3e, 0xa3, 0x23, 0xdd, 44 { 0xbf, 0xf6, 0x83, 0x4b, 0x3e, 0xa3, 0x23, 0xdd,
19 0x96, 0x78, 0x70, 0x8e, 0xa1, 0x9d, 0x3b, 0x40 }; 45 0x96, 0x78, 0x70, 0x8e, 0xa1, 0x9d, 0x3b, 0x40 };
20 46
21 // Expected HMAC result using kMessage and kClientKey. 47 // Expected HMAC result using kMessage and kClientKey.
22 const unsigned char kReceivedHmac[kSHA1DigestSize] = 48 const unsigned char kReceivedHmac[kSHA1DigestSize] =
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 214
189 std::string message_data(kKnownMessage); 215 std::string message_data(kKnownMessage);
190 216
191 crypto::HMAC hmac(crypto::HMAC::SHA1); 217 crypto::HMAC hmac(crypto::HMAC::SHA1);
192 ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize)); 218 ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize));
193 unsigned char calculated_hmac[kSHA1DigestSize]; 219 unsigned char calculated_hmac[kSHA1DigestSize];
194 220
195 EXPECT_EQ(kSHA1DigestSize, hmac.DigestLength()); 221 EXPECT_EQ(kSHA1DigestSize, hmac.DigestLength());
196 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize)); 222 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize));
197 EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize)); 223 EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize));
224 EXPECT_TRUE(hmac.Verify(
225 message_data,
226 base::StringPiece(reinterpret_cast<const char*>(kKnownHMACSHA1),
227 kSHA1DigestSize)));
198 228
199 crypto::HMAC hmac2(crypto::HMAC::SHA256); 229 crypto::HMAC hmac2(crypto::HMAC::SHA256);
200 ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize)); 230 ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize));
201 unsigned char calculated_hmac2[kSHA256DigestSize]; 231 unsigned char calculated_hmac2[kSHA256DigestSize];
202 232
203 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize)); 233 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize));
204 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize)); 234 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize));
205 } 235 }
206 236
207 TEST(HMACTest, HMACObjectReuse) { 237 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); 238 crypto::HMAC hmac(crypto::HMAC::SHA1);
231 ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(key), key_len)); 239 ASSERT_TRUE(
232 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { 240 hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey),
233 std::string data_string(cases[i].data, cases[i].data_len); 241 kSimpleKeyLength));
242 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSimpleHmacCases); ++i) {
243 std::string data_string(kSimpleHmacCases[i].data,
244 kSimpleHmacCases[i].data_len);
234 unsigned char digest[kSHA1DigestSize]; 245 unsigned char digest[kSHA1DigestSize];
235 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize)); 246 EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize));
236 EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize)); 247 EXPECT_EQ(0, memcmp(kSimpleHmacCases[i].digest, digest, kSHA1DigestSize));
237 } 248 }
238 } 249 }
250
251 TEST(HMACTest, Verify) {
252 crypto::HMAC hmac(crypto::HMAC::SHA1);
253 ASSERT_TRUE(
254 hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey),
255 kSimpleKeyLength));
256 const char empty_digest[kSHA1DigestSize] = { 0 };
257 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSimpleHmacCases); ++i) {
258 // Expected results
259 EXPECT_TRUE(hmac.Verify(
260 base::StringPiece(kSimpleHmacCases[i].data,
261 kSimpleHmacCases[i].data_len),
262 base::StringPiece(kSimpleHmacCases[i].digest,
263 kSHA1DigestSize)));
264 // Mismatched size
265 EXPECT_FALSE(hmac.Verify(
266 base::StringPiece(kSimpleHmacCases[i].data,
267 kSimpleHmacCases[i].data_len),
268 base::StringPiece(kSimpleHmacCases[i].data,
269 kSimpleHmacCases[i].data_len)));
270
271 // Expected size, mismatched data
272 EXPECT_FALSE(hmac.Verify(
273 base::StringPiece(kSimpleHmacCases[i].data,
274 kSimpleHmacCases[i].data_len),
275 base::StringPiece(empty_digest, kSHA1DigestSize)));
276 }
277 }
OLDNEW
« crypto/hmac.cc ('K') | « crypto/hmac.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698