3 * Definitions for HMAC and NMAC
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_HMAC_DEF_H
29 #define CATACOMB_HMAC_DEF_H
35 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
44 #ifndef CATACOMB_ARENA_H
48 #ifndef CATACOMB_GMAC_H
52 #ifndef CATACOMB_PARANOIA_H
53 # include "paranoia.h"
56 /*----- Macros ------------------------------------------------------------*/
58 /* --- @HMAC_DEF@ --- *
60 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
62 * Use: Creates implementations for the HMAC and NMAC functions.
65 #define HMAC_DEF(PRE, pre) HMAC_DEFX(PRE, pre, #pre, #pre)
66 #define HMAC_DEFX(PRE, pre, name, fname) \
68 /* --- Useful constants --- */ \
70 const octet pre##_hmackeysz[] = \
71 { KSZ_ANY | KSZ_16BIT, PRE##_HASHSZ/256, PRE##_HASHSZ%256 }; \
72 const octet pre##_sslmackeysz[] = \
73 { KSZ_ANY | KSZ_16BIT, PRE##_HASHSZ/256, PRE##_HASHSZ%256 }; \
74 const octet pre##_nmackeysz[] = \
75 { KSZ_SET | KSZ_16BIT, 2*PRE##_HASHSZ/256, 2*PRE##_HASHSZ%256, 0, 0 }; \
77 /* --- @pre_nmacinit@ --- * \
79 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
80 * @const void *ok@ = pointer to outer hash init vector \
81 * @const void *ik@ = pointer to inner hash init vector \
85 * Use: Initializes a MAC key for doing NMAC hashing. \
88 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
90 memcpy(key->ochain, ok, PRE##_STATESZ); \
91 memcpy(key->ichain, ik, PRE##_STATESZ); \
92 key->ocount = key->icount = 0; \
95 /* --- @pre_hmacinit@ --- * \
97 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
98 * @const void *k@ = pointer to key to use \
99 * @size_t sz@ = size of key data \
103 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
104 * longer than the hash function's output size aren't very \
105 * useful, but are accepted. Keys longer than the hash's \
106 * block size are also accepted; they are hashed before \
107 * use, as specified in RFC2104. \
110 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
113 const octet *kbuf = k; \
115 octet hbuf[PRE##_HASHSZ], buf[PRE##_BUFSZ]; \
117 if (sz > PRE##_BUFSZ) { \
119 pre##_hash(&ctx, k, sz); \
120 pre##_done(&ctx, hbuf); \
126 memset(buf, 0x5c, PRE##_BUFSZ); \
127 for (i = 0; i < sz; i++) buf[i] ^= kbuf[i]; \
128 pre##_hash(&ctx, buf, PRE##_BUFSZ); \
129 key->ocount = pre##_state(&ctx, key->ochain); \
132 memset(buf, 0x36, PRE##_BUFSZ); \
133 for (i = 0; i < sz; i++) buf[i] ^= kbuf[i]; \
134 pre##_hash(&ctx, buf, PRE##_BUFSZ); \
135 key->icount = pre##_state(&ctx, key->ichain); \
140 /* --- @pre_sslmacinit@ --- * \
142 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
143 * @const void *k@ = pointer to key to use \
144 * @size_t sz@ = size of key data \
148 * Use: Initializes a MAC key for doing hasing using the SSL3 \
152 void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \
154 const octet *kbuf = k; \
156 octet hbuf[PRE##_HASHSZ], buf[PRE##_BUFSZ]; \
158 if (sz > PRE##_BUFSZ) { \
160 pre##_hash(&ctx, k, sz); \
161 pre##_done(&ctx, hbuf); \
167 memcpy(buf, kbuf, sz); \
168 memset(buf + sz, 0x5c, PRE##_BUFSZ - sz); \
169 pre##_hash(&ctx, buf, PRE##_BUFSZ); \
170 key->ocount = pre##_state(&ctx, key->ochain); \
173 memcpy(buf, kbuf, sz); \
174 memset(buf + sz, 0x36, PRE##_BUFSZ - sz); \
175 pre##_hash(&ctx, buf, PRE##_BUFSZ); \
176 key->icount = pre##_state(&ctx, key->ichain); \
181 /* --- @pre_macinit@ --- * \
183 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
184 * @const pre_mackey *key@ = pointer to MAC key block \
188 * Use: Instantiates a MAC context from a key block. \
191 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
193 memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \
194 ctx->count = key->ocount; \
195 pre##_set(&ctx->ctx, key->ichain, key->icount); \
198 /* --- @pre_machash@ --- * \
200 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
201 * @const void *buf@ = pointer to buffer \
202 * @size_t sz@ = size of the buffer \
206 * Use: Hashes a buffer. \
209 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
210 { pre##_hash(&ctx->ctx, buf, sz); } \
212 /* --- @pre_macdone@ --- * \
214 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
215 * @void *mac@ = pointer to buffer to receive MAC \
219 * Use: Returns the result of a MAC computation. \
222 void pre##_macdone(pre##_macctx *ctx, void *mac) \
224 pre##_done(&ctx->ctx, mac); \
225 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
226 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
227 pre##_done(&ctx->ctx, mac); \
230 /* --- Generic MAC interface --- */ \
232 static const gmac_ops gkops, gnkops, gsslkops; \
233 static const ghash_ops gops, gnops, gsslops; \
235 typedef struct gkctx { \
237 const ghash_ops *gops; \
241 typedef struct gctx { \
244 octet buf[PRE##_HASHSZ]; \
247 static ghash *gkinit(gmac *m) \
249 gkctx *gk = (gkctx *)m; \
250 gctx *g = S_CREATE(gctx); \
251 g->h.ops = gk->gops; \
252 pre##_macinit(&g->c, &gk->k); \
256 static gmac *gkey(const void *k, size_t sz) \
258 gkctx *gk = S_CREATE(gkctx); \
259 gk->m.ops = &gkops; \
261 pre##_hmacinit(&gk->k, k, sz); \
265 static gmac *gnkey(const void *k, size_t sz) \
267 gkctx *gk = S_CREATE(gkctx); \
268 const octet *kk = k; \
269 assert(keysz(sz, pre##_nmackeysz) == sz); \
270 gk->m.ops = &gnkops; \
272 pre##_nmacinit(&gk->k, kk, kk + PRE##_STATESZ); \
276 static gmac *gsslkey(const void *k, size_t sz) \
278 gkctx *gk = S_CREATE(gkctx); \
279 gk->m.ops = &gsslkops; \
280 gk->gops = &gsslops; \
281 pre##_sslmacinit(&gk->k, k, sz); \
285 static void ghhash(ghash *h, const void *p, size_t sz) \
287 gctx *g = (gctx *)h; \
288 pre##_machash(&g->c, p, sz); \
291 static octet *ghdone(ghash *h, void *buf) \
293 gctx *g = (gctx *)h; \
296 pre##_macdone(&g->c, buf); \
300 static ghash *ghcopy(ghash *h) \
302 gctx *g = (gctx *)h; \
303 gctx *gg = S_CREATE(gctx); \
304 memcpy(gg, g, sizeof(gctx)); \
308 static void ghdestroy(ghash *h) \
310 gctx *g = (gctx *)h; \
315 static void gkdestroy(gmac *m) \
317 gkctx *gk = (gkctx *)m; \
322 static ghash *ghinit(void) \
324 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
328 const gcmac pre##_nmac = \
329 { name "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey }; \
330 const gcmac pre##_hmac = \
331 { name "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey }; \
332 const gcmac pre##_sslmac = \
333 { name "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey }; \
334 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
335 static const gmac_ops gnkops = { &pre##_nmac, gkinit, gkdestroy }; \
336 static const gmac_ops gsslkops = { &pre##_sslmac, gkinit, gkdestroy }; \
337 static const gchash gch = { name "-hmac", PRE##_HASHSZ, ghinit }; \
338 static const ghash_ops gops = \
339 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
340 static const gchash gnch = { name "-nmac", PRE##_HASHSZ, ghinit }; \
341 static const ghash_ops gnops = \
342 { &gnch, ghhash, ghdone, ghdestroy, ghcopy }; \
343 static const gchash gsslch = { name "-sslmac", PRE##_HASHSZ, ghinit }; \
344 static const ghash_ops gsslops = \
345 { &gsslch, ghhash, ghdone, ghdestroy, ghcopy }; \
347 HMAC_TESTX(PRE, pre, name, fname)
349 #define HMAC_TEST(PRE, pre) HMAC_TESTX(PRE, pre, #pre, #pre)
351 /* --- @HMAC_TEST@ --- *
353 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
355 * Use: Standard test rig for MAC functions.
362 #include <mLib/macros.h>
363 #include <mLib/dstr.h>
364 #include <mLib/quis.h>
365 #include <mLib/testrig.h>
367 #define HMAC_TESTX(PRE, pre, name, fname) \
369 static int macverify(dstr *v) \
376 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
381 dstr_ensure(&d, PRE##_HASHSZ); \
382 d.len = PRE##_HASHSZ; \
384 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
386 for (ip = szs; *ip; ip++) { \
393 p = (octet *)v[0].buf; \
394 pre##_macinit(&cctx, &ckey); \
398 pre##_machash(&cctx, p, i); \
402 pre##_macdone(&cctx, d.buf); \
403 if (MEMCMP(d.buf, !=, v[2].buf, PRE##_HASHSZ)) { \
404 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
406 type_hex.dump(&v[1], stdout); \
407 fputs("\n\texpected = ", stdout); \
408 type_hex.dump(&v[2], stdout); \
409 fputs("\n\tcomputed = ", stdout); \
410 type_hex.dump(&d, stdout); \
420 static test_chunk macdefs[] = { \
421 { name "-hmac", macverify, \
422 { &type_string, &type_hex, &type_hex, 0 } }, \
426 int main(int argc, char *argv[]) \
429 test_run(argc, argv, macdefs, SRCDIR"/t/" fname); \
434 # define HMAC_TESTX(PRE, pre, name, fname)
437 /*----- That's all, folks -------------------------------------------------*/