summaryrefslogtreecommitdiff
path: root/chromium/crypto/hmac_unittest.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2020-10-12 14:27:29 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2020-10-13 09:35:20 +0000
commitc30a6232df03e1efbd9f3b226777b07e087a1122 (patch)
treee992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/crypto/hmac_unittest.cc
parent7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff)
downloadqtwebengine-chromium-85-based.tar.gz
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/crypto/hmac_unittest.cc')
-rw-r--r--chromium/crypto/hmac_unittest.cc56
1 files changed, 56 insertions, 0 deletions
diff --git a/chromium/crypto/hmac_unittest.cc b/chromium/crypto/hmac_unittest.cc
index 2d39f038364..7e81f0a3855 100644
--- a/chromium/crypto/hmac_unittest.cc
+++ b/chromium/crypto/hmac_unittest.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include <stddef.h>
+#include <string.h>
#include <string>
@@ -296,3 +297,58 @@ TEST(HMACTest, EmptyKey) {
EXPECT_TRUE(hmac.Verify(
data, base::StringPiece(kExpectedDigest, kSHA1DigestSize)));
}
+
+TEST(HMACTest, TooLong) {
+ // See RFC4231, section 4.7.
+ unsigned char key[131];
+ for (size_t i = 0; i < sizeof(key); ++i)
+ key[i] = 0xaa;
+
+ std::string data = "Test Using Larger Than Block-Size Key - Hash Key First";
+ static uint8_t kKnownHMACSHA256[] = {
+ 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26,
+ 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28,
+ 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54};
+
+ crypto::HMAC hmac(crypto::HMAC::SHA256);
+ ASSERT_TRUE(hmac.Init(key, sizeof(key)));
+
+ // Attempting to write too large of an HMAC is an error.
+ uint8_t calculated_hmac[kSHA256DigestSize + 1];
+ EXPECT_FALSE(hmac.Sign(data, calculated_hmac, sizeof(calculated_hmac)));
+
+ // Attempting to verify too large of an HMAC is an error.
+ memcpy(calculated_hmac, kKnownHMACSHA256, kSHA256DigestSize);
+ calculated_hmac[kSHA256DigestSize] = 0;
+ EXPECT_FALSE(hmac.VerifyTruncated(
+ data,
+ std::string(calculated_hmac, calculated_hmac + sizeof(calculated_hmac))));
+}
+
+TEST(HMACTest, Bytes) {
+ // See RFC4231, section 4.7.
+ std::vector<uint8_t> key(131, 0xaa);
+ std::string data_str =
+ "Test Using Larger Than Block-Size Key - Hash Key First";
+ std::vector<uint8_t> data(data_str.begin(), data_str.end());
+ static uint8_t kKnownHMACSHA256[] = {
+ 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26,
+ 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28,
+ 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54};
+
+ crypto::HMAC hmac(crypto::HMAC::SHA256);
+ ASSERT_TRUE(hmac.Init(key));
+
+ uint8_t calculated_hmac[kSHA256DigestSize];
+ ASSERT_TRUE(hmac.Sign(data, calculated_hmac));
+ EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac, kSHA256DigestSize));
+
+ EXPECT_TRUE(hmac.Verify(data, calculated_hmac));
+ EXPECT_TRUE(hmac.VerifyTruncated(
+ data, base::make_span(calculated_hmac, kSHA256DigestSize / 2)));
+
+ data[0]++;
+ EXPECT_FALSE(hmac.Verify(data, calculated_hmac));
+ EXPECT_FALSE(hmac.VerifyTruncated(
+ data, base::make_span(calculated_hmac, kSHA256DigestSize / 2)));
+}