3 * $Id: hmac-def.h,v 1.5 2000/10/15 19:09:20 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.5 2000/10/15 19:09:20 mdw
34 * Support HMAC mode for hash functions which need to store more state than
35 * the hash output size.
37 * Revision 1.4 2000/07/15 10:00:58 mdw
38 * New generic hash operation for copying hash contexts.
40 * Revision 1.3 2000/07/02 18:27:42 mdw
41 * (ghash->ops->done): Interface change. Passing in a null buffer pointer
42 * uses a buffer internal to the ghash object. The operation returns the
43 * address of the buffer it used. Clients of generic hashes no longer need
44 * to use dynamically allocated memory for hash results.
46 * Revision 1.2 2000/06/17 11:23:44 mdw
47 * Use secure arena for memory allocation. Minor changes in the generic
50 * Revision 1.1 1999/12/10 23:16:40 mdw
51 * Split mode macros into interface and implementation.
55 #ifndef CATACOMB_HMAC_DEF_H
56 #define CATACOMB_HMAC_DEF_H
62 /*----- Header files ------------------------------------------------------*/
68 #include <mLib/bits.h>
71 #ifndef CATACOMB_ARENA_H
75 #ifndef CATACOMB_GMAC_H
79 #ifndef CATACOMB_PARANOIA_H
80 # include "paranoia.h"
83 /*----- Macros ------------------------------------------------------------*/
85 /* --- @HMAC_DEF@ --- *
87 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
89 * Use: Creates implementations for the HMAC and NMAC functions.
92 #define HMAC_DEF(PRE, pre) \
94 /* --- Useful constants --- */ \
96 const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
98 /* --- @pre_nmacinit@ --- * \
100 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
101 * @const void *ok@ = pointer to outer hash init vector \
102 * @const void *ik@ = pointer to inner hash init vector \
106 * Use: Initializes a MAC key for doing NMAC hashing. \
109 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
111 memcpy(key->ochain, ok, PRE##_STATESZ); \
112 memcpy(key->ichain, ik, PRE##_STATESZ); \
113 key->ocount = key->icount = 0; \
116 /* --- @pre_hmacinit@ --- * \
118 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
119 * @const void *k@ = pointer to key to use \
120 * @size_t sz@ = size of key data \
124 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
125 * longer than the hash function's output size aren't very \
126 * useful, but are accepted. Keys longer than the hash's \
127 * block size are also accepted; they are hashed before \
128 * use, as specified in RFC2104. \
131 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
134 const octet *kbuf = k; \
136 octet buf[PRE##_HASHSZ]; \
138 if (sz > PRE##_BUFSZ) { \
140 pre##_hash(&ctx, k, sz); \
141 pre##_done(&ctx, buf); \
147 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
148 for (i = 0; i < sz; i++) \
149 ctx.buf[i] ^= kbuf[i]; \
150 pre##_compress(&ctx, ctx.buf); \
151 pre##_state(&ctx, key->ochain); \
154 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
155 for (i = 0; i < sz; i++) \
156 ctx.buf[i] ^= kbuf[i]; \
157 pre##_compress(&ctx, ctx.buf); \
158 pre##_state(&ctx, key->ichain); \
160 key->ocount = key->icount = PRE##_BUFSZ; \
164 /* --- @pre_macinit@ --- * \
166 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
167 * @const pre_mackey *key@ = pointer to MAC key block \
171 * Use: Instantiates a MAC context from a key block. \
174 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
176 memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \
177 ctx->count = key->ocount; \
178 pre##_set(&ctx->ctx, key->ichain, key->icount); \
181 /* --- @pre_machash@ --- * \
183 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
184 * @const void *buf@ = pointer to buffer \
185 * @size_t sz@ = size of the buffer \
189 * Use: Hashes a buffer. \
192 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
194 pre##_hash(&ctx->ctx, buf, sz); \
197 /* --- @pre_macdone@ --- * \
199 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
200 * @void *mac@ = pointer to buffer to receive MAC \
204 * Use: Returns the result of a MAC computation. \
207 void pre##_macdone(pre##_macctx *ctx, void *mac) \
209 pre##_done(&ctx->ctx, mac); \
210 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
211 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
212 pre##_done(&ctx->ctx, mac); \
215 /* --- Generic MAC interface --- */ \
217 static const gmac_ops gkops; \
218 static const ghash_ops gops; \
220 typedef struct gkctx { \
225 typedef struct gctx { \
228 octet buf[PRE##_HASHSZ]; \
231 static ghash *gkinit(gmac *m) \
233 gkctx *gk = (gkctx *)m; \
234 gctx *g = S_CREATE(gctx); \
236 pre##_macinit(&g->c, &gk->k); \
240 static gmac *gkey(const void *k, size_t sz) \
242 gkctx *gk = S_CREATE(gkctx); \
243 gk->m.ops = &gkops; \
244 pre##_hmacinit(&gk->k, k, sz); \
248 static void ghhash(ghash *h, const void *p, size_t sz) \
250 gctx *g = (gctx *)h; \
251 pre##_machash(&g->c, p, sz); \
254 static octet *ghdone(ghash *h, void *buf) \
256 gctx *g = (gctx *)h; \
259 pre##_macdone(&g->c, buf); \
263 static ghash *ghcopy(ghash *h) \
265 gctx *g = (gctx *)h; \
266 gctx *gg = S_CREATE(gctx); \
267 memcpy(gg, g, sizeof(gctx)); \
271 static void ghdestroy(ghash *h) \
273 gctx *g = (gctx *)h; \
278 static void gkdestroy(gmac *m) \
280 gkctx *gk = (gkctx *)m; \
285 static ghash *ghinit(void) \
287 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
291 const gcmac pre##_hmac = \
292 { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey }; \
293 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
294 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
295 static const ghash_ops gops = \
296 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
300 /* --- @HMAC_TEST@ --- *
302 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
304 * Use: Standard test rig for MAC functions.
311 #include <mLib/dstr.h>
312 #include <mLib/quis.h>
313 #include <mLib/testrig.h>
315 #define HMAC_TEST(PRE, pre) \
317 static int macverify(dstr *v) \
324 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
329 dstr_ensure(&d, PRE##_HASHSZ); \
330 d.len = PRE##_HASHSZ; \
332 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
334 for (ip = szs; *ip; ip++) { \
341 p = (octet *)v[0].buf; \
342 pre##_macinit(&cctx, &ckey); \
346 pre##_machash(&cctx, p, i); \
350 pre##_macdone(&cctx, d.buf); \
351 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
352 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
354 type_hex.dump(&v[1], stdout); \
355 fputs("\n\texpected = ", stdout); \
356 type_hex.dump(&v[2], stdout); \
357 fputs("\n\tcomputed = ", stdout); \
358 type_hex.dump(&d, stdout); \
368 static test_chunk macdefs[] = { \
369 { #pre "-hmac", macverify, \
370 { &type_string, &type_hex, &type_hex, 0 } }, \
374 int main(int argc, char *argv[]) \
377 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
382 # define HMAC_TEST(PRE, pre)
385 /*----- That's all, folks -------------------------------------------------*/