]> git.ozlabs.org Git - ccan/blob - ccan/crypto/hmac_sha256/test/api-rfc4231.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / crypto / hmac_sha256 / test / api-rfc4231.c
1 /* From RFC4231 "Identifiers and Test Vectors for HMAC-SHA-224, HMAC-SHA-256,
2  * HMAC-SHA-384, and HMAC-SHA-512"
3  *
4  * https://tools.ietf.org/html/rfc4231
5  */
6 #include <ccan/crypto/hmac_sha256/hmac_sha256.h>
7 #include <ccan/tap/tap.h>
8 #include <ccan/str/hex/hex.h>
9 #include <string.h>
10 #include <assert.h>
11
12 struct test {
13         const char *key, *data, *hmac;
14 };
15
16 static struct test tests[] = { {
17         /* Test Case 1 */
18         "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",     /* (20 bytes) */
19         "4869205468657265",                             /* ("Hi There") */
20         "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
21         },
22         /* Test Case 2:
23            Test with a key shorter than the length of the HMAC output. */
24         {
25         "4a656665",                             /* ("Jefe") */
26         /*  ("what do ya want for nothing?") */
27         "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
28         "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
29         },
30         {
31         /* Test Case 3
32
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).
35         */
36         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", /* (20 bytes) */
37         "dddddddddddddddddddddddddddddddd"
38         "dddddddddddddddddddddddddddddddd"
39         "dddddddddddddddddddddddddddddddd"
40         "dddd", /* (50 bytes) */
41         "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe"
42         },
43         {
44         /* Test Case 4
45
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).
48         */
49         "0102030405060708090a0b0c0d0e0f10111213141516171819", /* (25 bytes) */
50         "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
51         "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
52         "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
53         "cdcd", /* (50 bytes) */
54         "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"
55         },
56 #if 0
57         {
58         /* Test Case 5
59
60            Test with a truncation of output to 128 bits.
61         */
62         "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c", /* (20 bytes) */
63         "546573742057697468205472756e636174696f6e", /* ("Test With Truncation") */
64         "a3b6167473100ee06e0c796c2955552b"
65         },
66 #endif
67         {
68         /* Test Case 6
69
70            Test with a key larger than 128 bytes (= block-size of SHA-384 and
71            SHA-512).
72         */
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"
87         },
88         {
89         /* Test Case 7
90
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"
113         }
114 };
115
116 static void *fromhex(const char *str, size_t *len)
117 {
118         void *p;
119
120         *len = hex_data_size(strlen(str));
121         p = malloc(*len);
122         if (!hex_decode(str, strlen(str), p, *len))
123                 abort();
124         return p;
125 }
126
127 int main(void)
128 {
129         size_t i;
130         struct hmac_sha256 hmac;
131
132         plan_tests(sizeof(tests) / sizeof(tests[0]) * 2);
133
134         for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
135                 size_t ksize, dsize, hmacsize;
136                 void *k, *d, *expect;
137                 struct hmac_sha256_ctx ctx;
138
139                 k = fromhex(tests[i].key, &ksize);
140                 d = fromhex(tests[i].data, &dsize);
141                 expect = fromhex(tests[i].hmac, &hmacsize);
142                 assert(hmacsize == sizeof(hmac));
143                 hmac_sha256(&hmac, k, ksize, d, dsize);
144                 ok1(memcmp(&hmac, expect, hmacsize) == 0);
145
146                 /* Now test partial API. */
147                 hmac_sha256_init(&ctx, k, ksize);
148                 hmac_sha256_update(&ctx, d, dsize / 2);
149                 hmac_sha256_update(&ctx, (char *)d + dsize/2, dsize - dsize/2);
150                 hmac_sha256_done(&ctx, &hmac);
151                 ok1(memcmp(&hmac, expect, hmacsize) == 0);
152
153                 free(k);
154                 free(d);
155                 free(expect);
156         }
157
158         return exit_status();
159 }