hmac_unittest.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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
  3. // found in the LICENSE file.
  4. #include "crypto/hmac.h"
  5. #include <stddef.h>
  6. #include <string.h>
  7. #include <string>
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. static const size_t kSHA1DigestSize = 20;
  10. static const size_t kSHA256DigestSize = 32;
  11. static const char* kSimpleKey =
  12. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  15. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  16. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
  17. static const size_t kSimpleKeyLength = 80;
  18. static const struct {
  19. const char *data;
  20. const int data_len;
  21. const char *digest;
  22. } kSimpleHmacCases[] = {
  23. { "Test Using Larger Than Block-Size Key - Hash Key First", 54,
  24. "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55"
  25. "\xED\x40\x21\x12" },
  26. { "Test Using Larger Than Block-Size Key and Larger "
  27. "Than One Block-Size Data", 73,
  28. "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08"
  29. "\xBB\xFF\x1A\x91" }
  30. };
  31. TEST(HMACTest, HmacSafeBrowsingResponseTest) {
  32. const int kKeySize = 16;
  33. // Client key.
  34. const unsigned char kClientKey[kKeySize] =
  35. { 0xbf, 0xf6, 0x83, 0x4b, 0x3e, 0xa3, 0x23, 0xdd,
  36. 0x96, 0x78, 0x70, 0x8e, 0xa1, 0x9d, 0x3b, 0x40 };
  37. // Expected HMAC result using kMessage and kClientKey.
  38. const unsigned char kReceivedHmac[kSHA1DigestSize] =
  39. { 0xb9, 0x3c, 0xd6, 0xf0, 0x49, 0x47, 0xe2, 0x52,
  40. 0x59, 0x7a, 0xbd, 0x1f, 0x2b, 0x4c, 0x83, 0xad,
  41. 0x86, 0xd2, 0x48, 0x85 };
  42. const char kMessage[] =
  43. "n:1896\ni:goog-malware-shavar\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shav"
  44. "ar_s_445-450\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_439-444\nu:s"
  45. ".ytimg.com/safebrowsing/rd/goog-malware-shavar_s_437\nu:s.ytimg.com/safebrowsi"
  46. "ng/rd/goog-malware-shavar_s_436\nu:s.ytimg.com/safebrowsing/rd/goog-malware-sh"
  47. "avar_s_433-435\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_431\nu:s.y"
  48. "timg.com/safebrowsing/rd/goog-malware-shavar_s_430\nu:s.ytimg.com/safebrowsing"
  49. "/rd/goog-malware-shavar_s_429\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shav"
  50. "ar_s_428\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_426\nu:s.ytimg.c"
  51. "om/safebrowsing/rd/goog-malware-shavar_s_424\nu:s.ytimg.com/safebrowsing/rd/go"
  52. "og-malware-shavar_s_423\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_4"
  53. "22\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_420\nu:s.ytimg.com/saf"
  54. "ebrowsing/rd/goog-malware-shavar_s_419\nu:s.ytimg.com/safebrowsing/rd/goog-mal"
  55. "ware-shavar_s_414\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_409-411"
  56. "\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_405\nu:s.ytimg.com/safeb"
  57. "rowsing/rd/goog-malware-shavar_s_404\nu:s.ytimg.com/safebrowsing/rd/goog-malwa"
  58. "re-shavar_s_402\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_s_401\nu:s."
  59. "ytimg.com/safebrowsing/rd/goog-malware-shavar_a_973-978\nu:s.ytimg.com/safebro"
  60. "wsing/rd/goog-malware-shavar_a_937-972\nu:s.ytimg.com/safebrowsing/rd/goog-mal"
  61. "ware-shavar_a_931-936\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_925"
  62. "-930\nu:s.ytimg.com/safebrowsing/rd/goog-malware-shavar_a_919-924\ni:goog-phis"
  63. "h-shavar\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2633\nu:s.ytimg.co"
  64. "m/safebrowsing/rd/goog-phish-shavar_a_2632\nu:s.ytimg.com/safebrowsing/rd/goog"
  65. "-phish-shavar_a_2629-2631\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2"
  66. "626-2628\nu:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_2625\n";
  67. std::string message_data(kMessage);
  68. crypto::HMAC hmac(crypto::HMAC::SHA1);
  69. ASSERT_TRUE(hmac.Init(kClientKey, kKeySize));
  70. unsigned char calculated_hmac[kSHA1DigestSize];
  71. EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize));
  72. EXPECT_EQ(0, memcmp(kReceivedHmac, calculated_hmac, kSHA1DigestSize));
  73. }
  74. // Test cases from RFC 2202 section 3
  75. TEST(HMACTest, RFC2202TestCases) {
  76. const struct {
  77. const char *key;
  78. const int key_len;
  79. const char *data;
  80. const int data_len;
  81. const char *digest;
  82. } cases[] = {
  83. { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
  84. "\x0B\x0B\x0B\x0B", 20,
  85. "Hi There", 8,
  86. "\xB6\x17\x31\x86\x55\x05\x72\x64\xE2\x8B\xC0\xB6\xFB\x37\x8C\x8E"
  87. "\xF1\x46\xBE\x00" },
  88. { "Jefe", 4,
  89. "what do ya want for nothing?", 28,
  90. "\xEF\xFC\xDF\x6A\xE5\xEB\x2F\xA2\xD2\x74\x16\xD5\xF1\x84\xDF\x9C"
  91. "\x25\x9A\x7C\x79" },
  92. { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  93. "\xAA\xAA\xAA\xAA", 20,
  94. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  95. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  96. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  97. "\xDD\xDD", 50,
  98. "\x12\x5D\x73\x42\xB9\xAC\x11\xCD\x91\xA3\x9A\xF4\x8A\xA1\x7B\x4F"
  99. "\x63\xF1\x75\xD3" },
  100. { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
  101. "\x11\x12\x13\x14\x15\x16\x17\x18\x19", 25,
  102. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  103. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  104. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  105. "\xCD\xCD", 50,
  106. "\x4C\x90\x07\xF4\x02\x62\x50\xC6\xBC\x84\x14\xF9\xBF\x50\xC8\x6C"
  107. "\x2D\x72\x35\xDA" },
  108. { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
  109. "\x0C\x0C\x0C\x0C", 20,
  110. "Test With Truncation", 20,
  111. "\x4C\x1A\x03\x42\x4B\x55\xE0\x7F\xE7\xF2\x7B\xE1\xD5\x8B\xB9\x32"
  112. "\x4A\x9A\x5A\x04" },
  113. { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  114. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  115. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  116. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  117. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  118. 80,
  119. "Test Using Larger Than Block-Size Key - Hash Key First", 54,
  120. "\xAA\x4A\xE5\xE1\x52\x72\xD0\x0E\x95\x70\x56\x37\xCE\x8A\x3B\x55"
  121. "\xED\x40\x21\x12" },
  122. { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  123. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  124. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  125. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  126. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
  127. 80,
  128. "Test Using Larger Than Block-Size Key and Larger "
  129. "Than One Block-Size Data", 73,
  130. "\xE8\xE9\x9D\x0F\x45\x23\x7D\x78\x6D\x6B\xBA\xA7\x96\x5C\x78\x08"
  131. "\xBB\xFF\x1A\x91" }
  132. };
  133. for (size_t i = 0; i < std::size(cases); ++i) {
  134. crypto::HMAC hmac(crypto::HMAC::SHA1);
  135. ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(cases[i].key),
  136. cases[i].key_len));
  137. std::string data_string(cases[i].data, cases[i].data_len);
  138. unsigned char digest[kSHA1DigestSize];
  139. EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize));
  140. EXPECT_EQ(0, memcmp(cases[i].digest, digest, kSHA1DigestSize));
  141. }
  142. }
  143. // TODO(wtc): add other test vectors from RFC 4231.
  144. TEST(HMACTest, RFC4231TestCase6) {
  145. unsigned char key[131];
  146. for (size_t i = 0; i < sizeof(key); ++i)
  147. key[i] = 0xaa;
  148. std::string data = "Test Using Larger Than Block-Size Key - Hash Key First";
  149. ASSERT_EQ(54U, data.size());
  150. static unsigned char kKnownHMACSHA256[] = {
  151. 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f,
  152. 0x0d, 0x8a, 0x26, 0xaa, 0xcb, 0xf5, 0xb7, 0x7f,
  153. 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14,
  154. 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54
  155. };
  156. crypto::HMAC hmac(crypto::HMAC::SHA256);
  157. ASSERT_TRUE(hmac.Init(key, sizeof(key)));
  158. unsigned char calculated_hmac[kSHA256DigestSize];
  159. EXPECT_EQ(kSHA256DigestSize, hmac.DigestLength());
  160. EXPECT_TRUE(hmac.Sign(data, calculated_hmac, kSHA256DigestSize));
  161. EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac, kSHA256DigestSize));
  162. }
  163. // Based on NSS's FIPS HMAC power-up self-test.
  164. TEST(HMACTest, NSSFIPSPowerUpSelfTest) {
  165. static const char kKnownMessage[] =
  166. "The test message for the MD2, MD5, and SHA-1 hashing algorithms.";
  167. static const unsigned char kKnownSecretKey[] = {
  168. 0x46, 0x69, 0x72, 0x65, 0x66, 0x6f, 0x78, 0x20,
  169. 0x61, 0x6e, 0x64, 0x20, 0x54, 0x68, 0x75, 0x6e,
  170. 0x64, 0x65, 0x72, 0x42, 0x69, 0x72, 0x64, 0x20,
  171. 0x61, 0x72, 0x65, 0x20, 0x61, 0x77, 0x65, 0x73,
  172. 0x6f, 0x6d, 0x65, 0x21, 0x00
  173. };
  174. static const size_t kKnownSecretKeySize = sizeof(kKnownSecretKey);
  175. // HMAC-SHA-1 known answer (20 bytes).
  176. static const unsigned char kKnownHMACSHA1[] = {
  177. 0xd5, 0x85, 0xf6, 0x5b, 0x39, 0xfa, 0xb9, 0x05,
  178. 0x3b, 0x57, 0x1d, 0x61, 0xe7, 0xb8, 0x84, 0x1e,
  179. 0x5d, 0x0e, 0x1e, 0x11
  180. };
  181. // HMAC-SHA-256 known answer (32 bytes).
  182. static const unsigned char kKnownHMACSHA256[] = {
  183. 0x05, 0x75, 0x9a, 0x9e, 0x70, 0x5e, 0xe7, 0x44,
  184. 0xe2, 0x46, 0x4b, 0x92, 0x22, 0x14, 0x22, 0xe0,
  185. 0x1b, 0x92, 0x8a, 0x0c, 0xfe, 0xf5, 0x49, 0xe9,
  186. 0xa7, 0x1b, 0x56, 0x7d, 0x1d, 0x29, 0x40, 0x48
  187. };
  188. std::string message_data(kKnownMessage);
  189. crypto::HMAC hmac(crypto::HMAC::SHA1);
  190. ASSERT_TRUE(hmac.Init(kKnownSecretKey, kKnownSecretKeySize));
  191. unsigned char calculated_hmac[kSHA1DigestSize];
  192. EXPECT_EQ(kSHA1DigestSize, hmac.DigestLength());
  193. EXPECT_TRUE(hmac.Sign(message_data, calculated_hmac, kSHA1DigestSize));
  194. EXPECT_EQ(0, memcmp(kKnownHMACSHA1, calculated_hmac, kSHA1DigestSize));
  195. EXPECT_TRUE(hmac.Verify(
  196. message_data,
  197. base::StringPiece(reinterpret_cast<const char*>(kKnownHMACSHA1),
  198. kSHA1DigestSize)));
  199. EXPECT_TRUE(hmac.VerifyTruncated(
  200. message_data,
  201. base::StringPiece(reinterpret_cast<const char*>(kKnownHMACSHA1),
  202. kSHA1DigestSize / 2)));
  203. crypto::HMAC hmac2(crypto::HMAC::SHA256);
  204. ASSERT_TRUE(hmac2.Init(kKnownSecretKey, kKnownSecretKeySize));
  205. unsigned char calculated_hmac2[kSHA256DigestSize];
  206. EXPECT_TRUE(hmac2.Sign(message_data, calculated_hmac2, kSHA256DigestSize));
  207. EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac2, kSHA256DigestSize));
  208. }
  209. TEST(HMACTest, HMACObjectReuse) {
  210. crypto::HMAC hmac(crypto::HMAC::SHA1);
  211. ASSERT_TRUE(
  212. hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey),
  213. kSimpleKeyLength));
  214. for (size_t i = 0; i < std::size(kSimpleHmacCases); ++i) {
  215. std::string data_string(kSimpleHmacCases[i].data,
  216. kSimpleHmacCases[i].data_len);
  217. unsigned char digest[kSHA1DigestSize];
  218. EXPECT_TRUE(hmac.Sign(data_string, digest, kSHA1DigestSize));
  219. EXPECT_EQ(0, memcmp(kSimpleHmacCases[i].digest, digest, kSHA1DigestSize));
  220. }
  221. }
  222. TEST(HMACTest, Verify) {
  223. crypto::HMAC hmac(crypto::HMAC::SHA1);
  224. ASSERT_TRUE(
  225. hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey),
  226. kSimpleKeyLength));
  227. const char empty_digest[kSHA1DigestSize] = { 0 };
  228. for (size_t i = 0; i < std::size(kSimpleHmacCases); ++i) {
  229. // Expected results
  230. EXPECT_TRUE(hmac.Verify(
  231. base::StringPiece(kSimpleHmacCases[i].data,
  232. kSimpleHmacCases[i].data_len),
  233. base::StringPiece(kSimpleHmacCases[i].digest,
  234. kSHA1DigestSize)));
  235. // Mismatched size
  236. EXPECT_FALSE(hmac.Verify(
  237. base::StringPiece(kSimpleHmacCases[i].data,
  238. kSimpleHmacCases[i].data_len),
  239. base::StringPiece(kSimpleHmacCases[i].data,
  240. kSimpleHmacCases[i].data_len)));
  241. // Expected size, mismatched data
  242. EXPECT_FALSE(hmac.Verify(
  243. base::StringPiece(kSimpleHmacCases[i].data,
  244. kSimpleHmacCases[i].data_len),
  245. base::StringPiece(empty_digest, kSHA1DigestSize)));
  246. }
  247. }
  248. TEST(HMACTest, EmptyKey) {
  249. // Test vector from https://en.wikipedia.org/wiki/HMAC
  250. const char* kExpectedDigest =
  251. "\xFB\xDB\x1D\x1B\x18\xAA\x6C\x08\x32\x4B\x7D\x64\xB7\x1F\xB7\x63"
  252. "\x70\x69\x0E\x1D";
  253. base::StringPiece data("");
  254. crypto::HMAC hmac(crypto::HMAC::SHA1);
  255. ASSERT_TRUE(hmac.Init(nullptr, 0));
  256. unsigned char digest[kSHA1DigestSize];
  257. EXPECT_TRUE(hmac.Sign(data, digest, kSHA1DigestSize));
  258. EXPECT_EQ(0, memcmp(kExpectedDigest, digest, kSHA1DigestSize));
  259. EXPECT_TRUE(hmac.Verify(
  260. data, base::StringPiece(kExpectedDigest, kSHA1DigestSize)));
  261. }
  262. TEST(HMACTest, TooLong) {
  263. // See RFC4231, section 4.7.
  264. unsigned char key[131];
  265. for (size_t i = 0; i < sizeof(key); ++i)
  266. key[i] = 0xaa;
  267. std::string data = "Test Using Larger Than Block-Size Key - Hash Key First";
  268. static uint8_t kKnownHMACSHA256[] = {
  269. 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26,
  270. 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28,
  271. 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54};
  272. crypto::HMAC hmac(crypto::HMAC::SHA256);
  273. ASSERT_TRUE(hmac.Init(key, sizeof(key)));
  274. // Attempting to write too large of an HMAC is an error.
  275. uint8_t calculated_hmac[kSHA256DigestSize + 1];
  276. EXPECT_FALSE(hmac.Sign(data, calculated_hmac, sizeof(calculated_hmac)));
  277. // Attempting to verify too large of an HMAC is an error.
  278. memcpy(calculated_hmac, kKnownHMACSHA256, kSHA256DigestSize);
  279. calculated_hmac[kSHA256DigestSize] = 0;
  280. EXPECT_FALSE(hmac.VerifyTruncated(
  281. data,
  282. std::string(calculated_hmac, calculated_hmac + sizeof(calculated_hmac))));
  283. }
  284. TEST(HMACTest, Bytes) {
  285. // See RFC4231, section 4.7.
  286. std::vector<uint8_t> key(131, 0xaa);
  287. std::string data_str =
  288. "Test Using Larger Than Block-Size Key - Hash Key First";
  289. std::vector<uint8_t> data(data_str.begin(), data_str.end());
  290. static uint8_t kKnownHMACSHA256[] = {
  291. 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26,
  292. 0xaa, 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28,
  293. 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54};
  294. crypto::HMAC hmac(crypto::HMAC::SHA256);
  295. ASSERT_TRUE(hmac.Init(key));
  296. uint8_t calculated_hmac[kSHA256DigestSize];
  297. ASSERT_TRUE(hmac.Sign(data, calculated_hmac));
  298. EXPECT_EQ(0, memcmp(kKnownHMACSHA256, calculated_hmac, kSHA256DigestSize));
  299. EXPECT_TRUE(hmac.Verify(data, calculated_hmac));
  300. EXPECT_TRUE(hmac.VerifyTruncated(
  301. data, base::make_span(calculated_hmac, kSHA256DigestSize / 2)));
  302. data[0]++;
  303. EXPECT_FALSE(hmac.Verify(data, calculated_hmac));
  304. EXPECT_FALSE(hmac.VerifyTruncated(
  305. data, base::make_span(calculated_hmac, kSHA256DigestSize / 2)));
  306. }