3 * $Id: hmac-def.h,v 1.4 2000/07/15 10:00:58 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.4 2000/07/15 10:00:58 mdw
34 * New generic hash operation for copying hash contexts.
36 * Revision 1.3 2000/07/02 18:27:42 mdw
37 * (ghash->ops->done): Interface change. Passing in a null buffer pointer
38 * uses a buffer internal to the ghash object. The operation returns the
39 * address of the buffer it used. Clients of generic hashes no longer need
40 * to use dynamically allocated memory for hash results.
42 * Revision 1.2 2000/06/17 11:23:44 mdw
43 * Use secure arena for memory allocation. Minor changes in the generic
46 * Revision 1.1 1999/12/10 23:16:40 mdw
47 * Split mode macros into interface and implementation.
51 #ifndef CATACOMB_HMAC_DEF_H
52 #define CATACOMB_HMAC_DEF_H
58 /*----- Header files ------------------------------------------------------*/
64 #include <mLib/bits.h>
67 #ifndef CATACOMB_ARENA_H
71 #ifndef CATACOMB_GMAC_H
75 #ifndef CATACOMB_PARANOIA_H
76 # include "paranoia.h"
79 /*----- Macros ------------------------------------------------------------*/
81 /* --- @HMAC_DEF@ --- *
83 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
85 * Use: Creates implementations for the HMAC and NMAC functions.
88 #define HMAC_DEF(PRE, pre) \
90 /* --- Useful constants --- */ \
92 const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ }; \
94 /* --- @pre_nmacinit@ --- * \
96 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
97 * @const void *ok@ = pointer to outer hash init vector \
98 * @const void *ik@ = pointer to inner hash init vector \
102 * Use: Initializes a MAC key for doing NMAC hashing. \
105 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
107 memcpy(key->ochain, ok, PRE##_HASHSZ); \
108 memcpy(key->ichain, ik, PRE##_HASHSZ); \
109 key->ocount = key->icount = 0; \
112 /* --- @pre_hmacinit@ --- * \
114 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
115 * @const void *k@ = pointer to key to use \
116 * @size_t sz@ = size of key data \
120 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
121 * longer than the hash function's output size aren't very \
122 * useful, but are accepted. Keys longer than the hash's \
123 * block size are also accepted; they are hashed before \
124 * use, as specified in RFC2104. \
127 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
130 const octet *kbuf = k; \
132 octet buf[PRE##_HASHSZ]; \
134 if (sz > PRE##_BUFSZ) { \
136 pre##_hash(&ctx, k, sz); \
137 pre##_done(&ctx, buf); \
143 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
144 for (i = 0; i < sz; i++) \
145 ctx.buf[i] ^= kbuf[i]; \
146 pre##_compress(&ctx, ctx.buf); \
147 pre##_state(&ctx, key->ochain); \
150 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
151 for (i = 0; i < sz; i++) \
152 ctx.buf[i] ^= kbuf[i]; \
153 pre##_compress(&ctx, ctx.buf); \
154 pre##_state(&ctx, key->ichain); \
156 key->ocount = key->icount = PRE##_BUFSZ; \
160 /* --- @pre_macinit@ --- * \
162 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
163 * @const pre_mackey *key@ = pointer to MAC key block \
167 * Use: Instantiates a MAC context from a key block. \
170 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
172 memcpy(ctx->chain, key->ochain, PRE##_HASHSZ); \
173 ctx->count = key->ocount; \
174 pre##_set(&ctx->ctx, key->ichain, key->icount); \
177 /* --- @pre_machash@ --- * \
179 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
180 * @const void *buf@ = pointer to buffer \
181 * @size_t sz@ = size of the buffer \
185 * Use: Hashes a buffer. \
188 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
190 pre##_hash(&ctx->ctx, buf, sz); \
193 /* --- @pre_macdone@ --- * \
195 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
196 * @void *mac@ = pointer to buffer to receive MAC \
200 * Use: Returns the result of a MAC computation. \
203 void pre##_macdone(pre##_macctx *ctx, void *mac) \
205 pre##_done(&ctx->ctx, mac); \
206 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
207 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
208 pre##_done(&ctx->ctx, mac); \
211 /* --- Generic MAC interface --- */ \
213 static const gmac_ops gkops; \
214 static const ghash_ops gops; \
216 typedef struct gkctx { \
221 typedef struct gctx { \
224 octet buf[PRE##_HASHSZ]; \
227 static ghash *gkinit(gmac *m) \
229 gkctx *gk = (gkctx *)m; \
230 gctx *g = S_CREATE(gctx); \
232 pre##_macinit(&g->c, &gk->k); \
236 static gmac *gkey(const void *k, size_t sz) \
238 gkctx *gk = S_CREATE(gkctx); \
239 gk->m.ops = &gkops; \
240 pre##_hmacinit(&gk->k, k, sz); \
244 static void ghhash(ghash *h, const void *p, size_t sz) \
246 gctx *g = (gctx *)h; \
247 pre##_machash(&g->c, p, sz); \
250 static octet *ghdone(ghash *h, void *buf) \
252 gctx *g = (gctx *)h; \
255 pre##_macdone(&g->c, buf); \
259 static ghash *ghcopy(ghash *h) \
261 gctx *g = (gctx *)h; \
262 gctx *gg = S_CREATE(gctx); \
263 memcpy(gg, g, sizeof(gctx)); \
267 static void ghdestroy(ghash *h) \
269 gctx *g = (gctx *)h; \
274 static void gkdestroy(gmac *m) \
276 gkctx *gk = (gkctx *)m; \
281 static ghash *ghinit(void) \
283 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
287 const gcmac pre##_hmac = \
288 { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey }; \
289 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
290 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
291 static const ghash_ops gops = \
292 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
296 /* --- @HMAC_TEST@ --- *
298 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
300 * Use: Standard test rig for MAC functions.
307 #include <mLib/dstr.h>
308 #include <mLib/quis.h>
309 #include <mLib/testrig.h>
311 #define HMAC_TEST(PRE, pre) \
313 static int macverify(dstr *v) \
320 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
325 dstr_ensure(&d, PRE##_HASHSZ); \
326 d.len = PRE##_HASHSZ; \
328 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
330 for (ip = szs; *ip; ip++) { \
337 p = (octet *)v[0].buf; \
338 pre##_macinit(&cctx, &ckey); \
342 pre##_machash(&cctx, p, i); \
346 pre##_macdone(&cctx, d.buf); \
347 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
348 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
350 type_hex.dump(&v[1], stdout); \
351 fputs("\n\texpected = ", stdout); \
352 type_hex.dump(&v[2], stdout); \
353 fputs("\n\tcomputed = ", stdout); \
354 type_hex.dump(&d, stdout); \
364 static test_chunk macdefs[] = { \
365 { #pre "-hmac", macverify, \
366 { &type_string, &type_hex, &type_hex, 0 } }, \
370 int main(int argc, char *argv[]) \
373 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
378 # define HMAC_TEST(PRE, pre)
381 /*----- That's all, folks -------------------------------------------------*/