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

Side by Side Diff: crypto/hmac_unittest.cc

Issue 7532020: Add a routine for truncated HMAC verification (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 4 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
« no previous file with comments | « 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 "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 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize)); 214 ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize));
215 unsigned char calculated_hmac[kSHA1DigestSize]; 215 unsigned char calculated_hmac[kSHA1DigestSize];
216 216
217 EXPECT_EQ(kSHA1DigestSize, hmac.DigestLength()); 217 EXPECT_EQ(kSHA1DigestSize, hmac.DigestLength());
218 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize)); 218 EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize));
219 EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize)); 219 EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize));
220 EXPECT_TRUE(hmac.Verify( 220 EXPECT_TRUE(hmac.Verify(
221 message_data, 221 message_data,
222 base::StringPiece(reinterpret_cast<const char*>(kKnownHMACSHA1), 222 base::StringPiece(reinterpret_cast<const char*>(kKnownHMACSHA1),
223 kSHA1DigestSize))); 223 kSHA1DigestSize)));
224 EXPECT_TRUE(hmac.VerifyTruncated(
225 message_data,
226 base::StringPiece(reinterpret_cast<const char*>(kKnownHMACSHA1),
227 kSHA1DigestSize / 2)));
224 228
225 crypto::HMAC hmac2(crypto::HMAC::SHA256); 229 crypto::HMAC hmac2(crypto::HMAC::SHA256);
226 ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize)); 230 ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize));
227 unsigned char calculated_hmac2[kSHA256DigestSize]; 231 unsigned char calculated_hmac2[kSHA256DigestSize];
228 232
229 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize)); 233 EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize));
230 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize)); 234 EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize));
231 } 235 }
232 236
233 TEST(HMACTest, HMACObjectReuse) { 237 TEST(HMACTest, HMACObjectReuse) {
(...skipping 30 matching lines...) Expand all
264 base::StringPiece(kSimpleHmacCases[i].data, 268 base::StringPiece(kSimpleHmacCases[i].data,
265 kSimpleHmacCases[i].data_len))); 269 kSimpleHmacCases[i].data_len)));
266 270
267 // Expected size, mismatched data 271 // Expected size, mismatched data
268 EXPECT_FALSE(hmac.Verify( 272 EXPECT_FALSE(hmac.Verify(
269 base::StringPiece(kSimpleHmacCases[i].data, 273 base::StringPiece(kSimpleHmacCases[i].data,
270 kSimpleHmacCases[i].data_len), 274 kSimpleHmacCases[i].data_len),
271 base::StringPiece(empty_digest, kSHA1DigestSize))); 275 base::StringPiece(empty_digest, kSHA1DigestSize)));
272 } 276 }
273 } 277 }
OLDNEW
« no previous file with comments | « crypto/hmac.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698