3 * $Id: hmac-def.h,v 1.7 2001/04/19 18:24:45 mdw Exp $
5 * Definitions for HMAC and NMAC
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: hmac-def.h,v $
33 * Revision 1.7 2001/04/19 18:24:45 mdw
34 * Provide correct key sizes for NMAC, HMAC and SSLMAC.
36 * Revision 1.6 2001/04/03 19:35:45 mdw
37 * Support the SSL HMAC variant (untested).
39 * Revision 1.5 2000/10/15 19:09:20 mdw
40 * Support HMAC mode for hash functions which need to store more state than
41 * the hash output size.
43 * Revision 1.4 2000/07/15 10:00:58 mdw
44 * New generic hash operation for copying hash contexts.
46 * Revision 1.3 2000/07/02 18:27:42 mdw
47 * (ghash->ops->done): Interface change. Passing in a null buffer pointer
48 * uses a buffer internal to the ghash object. The operation returns the
49 * address of the buffer it used. Clients of generic hashes no longer need
50 * to use dynamically allocated memory for hash results.
52 * Revision 1.2 2000/06/17 11:23:44 mdw
53 * Use secure arena for memory allocation. Minor changes in the generic
56 * Revision 1.1 1999/12/10 23:16:40 mdw
57 * Split mode macros into interface and implementation.
61 #ifndef CATACOMB_HMAC_DEF_H
62 #define CATACOMB_HMAC_DEF_H
68 /*----- Header files ------------------------------------------------------*/
74 #include <mLib/bits.h>
77 #ifndef CATACOMB_ARENA_H
81 #ifndef CATACOMB_GMAC_H
85 #ifndef CATACOMB_PARANOIA_H
86 # include "paranoia.h"
89 /*----- Macros ------------------------------------------------------------*/
91 /* --- @HMAC_DEF@ --- *
93 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
95 * Use: Creates implementations for the HMAC and NMAC functions.
98 #define HMAC_DEF(PRE, pre) \
100 /* --- Useful constants --- */ \
102 const octet pre##_hmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
103 const octet pre##_sslmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
104 const octet pre##_nmackeysz[] = { KSZ_SET, 2 * PRE##_STATESZ, 0 }; \
106 /* --- @pre_nmacinit@ --- * \
108 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
109 * @const void *ok@ = pointer to outer hash init vector \
110 * @const void *ik@ = pointer to inner hash init vector \
114 * Use: Initializes a MAC key for doing NMAC hashing. \
117 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
119 memcpy(key->ochain, ok, PRE##_STATESZ); \
120 memcpy(key->ichain, ik, PRE##_STATESZ); \
121 key->ocount = key->icount = 0; \
124 /* --- @pre_hmacinit@ --- * \
126 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
127 * @const void *k@ = pointer to key to use \
128 * @size_t sz@ = size of key data \
132 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
133 * longer than the hash function's output size aren't very \
134 * useful, but are accepted. Keys longer than the hash's \
135 * block size are also accepted; they are hashed before \
136 * use, as specified in RFC2104. \
139 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
142 const octet *kbuf = k; \
144 octet buf[PRE##_HASHSZ]; \
146 if (sz > PRE##_BUFSZ) { \
148 pre##_hash(&ctx, k, sz); \
149 pre##_done(&ctx, buf); \
155 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
156 for (i = 0; i < sz; i++) \
157 ctx.buf[i] ^= kbuf[i]; \
158 pre##_compress(&ctx, ctx.buf); \
159 pre##_state(&ctx, key->ochain); \
162 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
163 for (i = 0; i < sz; i++) \
164 ctx.buf[i] ^= kbuf[i]; \
165 pre##_compress(&ctx, ctx.buf); \
166 pre##_state(&ctx, key->ichain); \
168 key->ocount = key->icount = PRE##_BUFSZ; \
172 /* --- @pre_sslmacinit@ --- * \
174 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
175 * @const void *k@ = pointer to key to use \
176 * @size_t sz@ = size of key data \
180 * Use: Initializes a MAC key for doing hasing using the SSL3 \
184 void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \
186 const octet *kbuf = k; \
188 octet buf[PRE##_HASHSZ]; \
190 if (sz > PRE##_BUFSZ) { \
192 pre##_hash(&ctx, k, sz); \
193 pre##_done(&ctx, buf); \
199 memcpy(ctx.buf, kbuf, sz); \
200 memset(ctx.buf + sz, 0x5c, PRE##_BUFSZ - sz); \
201 pre##_compress(&ctx, ctx.buf); \
202 pre##_state(&ctx, key->ochain); \
205 memcpy(ctx.buf, kbuf, sz); \
206 memset(ctx.buf + sz, 0x36, PRE##_BUFSZ - sz); \
207 pre##_compress(&ctx, ctx.buf); \
208 pre##_state(&ctx, key->ichain); \
210 key->ocount = key->icount = PRE##_BUFSZ; \
214 /* --- @pre_macinit@ --- * \
216 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
217 * @const pre_mackey *key@ = pointer to MAC key block \
221 * Use: Instantiates a MAC context from a key block. \
224 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
226 memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \
227 ctx->count = key->ocount; \
228 pre##_set(&ctx->ctx, key->ichain, key->icount); \
231 /* --- @pre_machash@ --- * \
233 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
234 * @const void *buf@ = pointer to buffer \
235 * @size_t sz@ = size of the buffer \
239 * Use: Hashes a buffer. \
242 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
244 pre##_hash(&ctx->ctx, buf, sz); \
247 /* --- @pre_macdone@ --- * \
249 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
250 * @void *mac@ = pointer to buffer to receive MAC \
254 * Use: Returns the result of a MAC computation. \
257 void pre##_macdone(pre##_macctx *ctx, void *mac) \
259 pre##_done(&ctx->ctx, mac); \
260 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
261 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
262 pre##_done(&ctx->ctx, mac); \
265 /* --- Generic MAC interface --- */ \
267 static const gmac_ops gkops; \
268 static const ghash_ops gops, gnops, gsslops; \
270 typedef struct gkctx { \
272 const ghash_ops *gops; \
276 typedef struct gctx { \
279 octet buf[PRE##_HASHSZ]; \
282 static ghash *gkinit(gmac *m) \
284 gkctx *gk = (gkctx *)m; \
285 gctx *g = S_CREATE(gctx); \
286 g->h.ops = gk->gops; \
287 pre##_macinit(&g->c, &gk->k); \
291 static gmac *gkey(const void *k, size_t sz) \
293 gkctx *gk = S_CREATE(gkctx); \
294 gk->m.ops = &gkops; \
296 pre##_hmacinit(&gk->k, k, sz); \
300 static gmac *gnkey(const void *k, size_t sz) \
302 gkctx *gk = S_CREATE(gkctx); \
303 const octet *kk = k; \
304 assert(keysz(sz, pre##_nmackeysz) == sz); \
305 gk->m.ops = &gkops; \
307 pre##_nmacinit(&gk->k, kk, kk + PRE##_STATESZ); \
311 static gmac *gsslkey(const void *k, size_t sz) \
313 gkctx *gk = S_CREATE(gkctx); \
314 gk->m.ops = &gkops; \
315 gk->gops = &gsslops; \
316 pre##_sslmacinit(&gk->k, k, sz); \
320 static void ghhash(ghash *h, const void *p, size_t sz) \
322 gctx *g = (gctx *)h; \
323 pre##_machash(&g->c, p, sz); \
326 static octet *ghdone(ghash *h, void *buf) \
328 gctx *g = (gctx *)h; \
331 pre##_macdone(&g->c, buf); \
335 static ghash *ghcopy(ghash *h) \
337 gctx *g = (gctx *)h; \
338 gctx *gg = S_CREATE(gctx); \
339 memcpy(gg, g, sizeof(gctx)); \
343 static void ghdestroy(ghash *h) \
345 gctx *g = (gctx *)h; \
350 static void gkdestroy(gmac *m) \
352 gkctx *gk = (gkctx *)m; \
357 static ghash *ghinit(void) \
359 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
363 const gcmac pre##_nmac = \
364 { #pre "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey }; \
365 const gcmac pre##_hmac = \
366 { #pre "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey }; \
367 const gcmac pre##_sslmac = \
368 { #pre "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey }; \
369 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
370 static const gmac_ops gnkops = { &pre##_nmac, gkinit, gkdestroy }; \
371 static const gmac_ops gsslkops = { &pre##_sslmac, gkinit, gkdestroy }; \
372 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
373 static const ghash_ops gops = \
374 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
375 static const gchash gnch = { #pre "-nmac", PRE##_HASHSZ, ghinit }; \
376 static const ghash_ops gnops = \
377 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
378 static const gchash gsslch = { #pre "-sslmac", PRE##_HASHSZ, ghinit }; \
379 static const ghash_ops gsslops = \
380 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
384 /* --- @HMAC_TEST@ --- *
386 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
388 * Use: Standard test rig for MAC functions.
395 #include <mLib/dstr.h>
396 #include <mLib/quis.h>
397 #include <mLib/testrig.h>
399 #define HMAC_TEST(PRE, pre) \
401 static int macverify(dstr *v) \
408 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
413 dstr_ensure(&d, PRE##_HASHSZ); \
414 d.len = PRE##_HASHSZ; \
416 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
418 for (ip = szs; *ip; ip++) { \
425 p = (octet *)v[0].buf; \
426 pre##_macinit(&cctx, &ckey); \
430 pre##_machash(&cctx, p, i); \
434 pre##_macdone(&cctx, d.buf); \
435 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
436 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
438 type_hex.dump(&v[1], stdout); \
439 fputs("\n\texpected = ", stdout); \
440 type_hex.dump(&v[2], stdout); \
441 fputs("\n\tcomputed = ", stdout); \
442 type_hex.dump(&d, stdout); \
452 static test_chunk macdefs[] = { \
453 { #pre "-hmac", macverify, \
454 { &type_string, &type_hex, &type_hex, 0 } }, \
458 int main(int argc, char *argv[]) \
461 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
466 # define HMAC_TEST(PRE, pre)
469 /*----- That's all, folks -------------------------------------------------*/