1 /* From RFC4231 "Identifiers and Test Vectors for HMAC-SHA-224, HMAC-SHA-256,
2 * HMAC-SHA-384, and HMAC-SHA-512"
4 * https://tools.ietf.org/html/rfc4231
6 #include <ccan/crypto/hmac_sha256/hmac_sha256.h>
7 #include <ccan/tap/tap.h>
8 #include <ccan/str/hex/hex.h>
13 const char *key, *data, *hmac;
16 static struct test tests[] = { {
18 "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", /* (20 bytes) */
19 "4869205468657265", /* ("Hi There") */
20 "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
23 Test with a key shorter than the length of the HMAC output. */
25 "4a656665", /* ("Jefe") */
26 /* ("what do ya want for nothing?") */
27 "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
28 "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
33 Test with a combined length of key and data that is larger than 64
34 bytes (= block-size of SHA-224 and SHA-256).
36 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", /* (20 bytes) */
37 "dddddddddddddddddddddddddddddddd"
38 "dddddddddddddddddddddddddddddddd"
39 "dddddddddddddddddddddddddddddddd"
40 "dddd", /* (50 bytes) */
41 "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe"
46 Test with a combined length of key and data that is larger than 64
47 bytes (= block-size of SHA-224 and SHA-256).
49 "0102030405060708090a0b0c0d0e0f10111213141516171819", /* (25 bytes) */
50 "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
51 "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
52 "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
53 "cdcd", /* (50 bytes) */
54 "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"
60 Test with a truncation of output to 128 bits.
62 "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c", /* (20 bytes) */
63 "546573742057697468205472756e636174696f6e", /* ("Test With Truncation") */
64 "a3b6167473100ee06e0c796c2955552b"
70 Test with a key larger than 128 bytes (= block-size of SHA-384 and
73 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
74 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
75 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
76 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
77 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
78 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
79 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
80 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
81 "aaaaaa", /* (131 bytes) */
82 "54657374205573696e67204c61726765" /* ("Test Using Large") */
83 "72205468616e20426c6f636b2d53697a" /* ("r Than Block-Siz") */
84 "65204b6579202d2048617368204b6579" /* ("e Key - Hash Key") */
85 "204669727374", /* (" First") */
86 "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"
91 Test with a key and data that is larger than 128 bytes (= block-size
92 of SHA-384 and SHA-512). */
93 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
94 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
95 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
96 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
97 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
98 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
99 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
100 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
101 "aaaaaa", /* (131 bytes) */
102 "54686973206973206120746573742075" /* ("This is a test u") */
103 "73696e672061206c6172676572207468" /* ("sing a larger th") */
104 "616e20626c6f636b2d73697a65206b65" /* ("an block-size ke") */
105 "7920616e642061206c61726765722074" /* ("y and a larger t") */
106 "68616e20626c6f636b2d73697a652064" /* ("han block-size d") */
107 "6174612e20546865206b6579206e6565" /* ("ata. The key nee") */
108 "647320746f2062652068617368656420" /* ("ds to be hashed ") */
109 "6265666f7265206265696e6720757365" /* ("before being use") */
110 "642062792074686520484d414320616c" /* ("d by the HMAC al") */
111 "676f726974686d2e", /* ("gorithm.") */
112 "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2"
116 static void *fromhex(const char *str, size_t *len)
120 *len = hex_data_size(strlen(str));
122 if (!hex_decode(str, strlen(str), p, *len))
130 struct hmac_sha256 hmac;
132 plan_tests(sizeof(tests) / sizeof(tests[0]));
134 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
135 size_t ksize, dsize, hmacsize;
136 void *k, *d, *expect;
138 k = fromhex(tests[i].key, &ksize);
139 d = fromhex(tests[i].data, &dsize);
140 expect = fromhex(tests[i].hmac, &hmacsize);
141 assert(hmacsize == sizeof(hmac));
142 hmac_sha256(&hmac, k, ksize, d, dsize);
143 ok1(memcmp(&hmac, expect, hmacsize) == 0);
149 return exit_status();