3 * $Id: hmac-def.h,v 1.6 2001/04/03 19:35: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.6 2001/04/03 19:35:45 mdw
34 * Support the SSL HMAC variant (untested).
36 * Revision 1.5 2000/10/15 19:09:20 mdw
37 * Support HMAC mode for hash functions which need to store more state than
38 * the hash output size.
40 * Revision 1.4 2000/07/15 10:00:58 mdw
41 * New generic hash operation for copying hash contexts.
43 * Revision 1.3 2000/07/02 18:27:42 mdw
44 * (ghash->ops->done): Interface change. Passing in a null buffer pointer
45 * uses a buffer internal to the ghash object. The operation returns the
46 * address of the buffer it used. Clients of generic hashes no longer need
47 * to use dynamically allocated memory for hash results.
49 * Revision 1.2 2000/06/17 11:23:44 mdw
50 * Use secure arena for memory allocation. Minor changes in the generic
53 * Revision 1.1 1999/12/10 23:16:40 mdw
54 * Split mode macros into interface and implementation.
58 #ifndef CATACOMB_HMAC_DEF_H
59 #define CATACOMB_HMAC_DEF_H
65 /*----- Header files ------------------------------------------------------*/
71 #include <mLib/bits.h>
74 #ifndef CATACOMB_ARENA_H
78 #ifndef CATACOMB_GMAC_H
82 #ifndef CATACOMB_PARANOIA_H
83 # include "paranoia.h"
86 /*----- Macros ------------------------------------------------------------*/
88 /* --- @HMAC_DEF@ --- *
90 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
92 * Use: Creates implementations for the HMAC and NMAC functions.
95 #define HMAC_DEF(PRE, pre) \
97 /* --- Useful constants --- */ \
99 const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
101 /* --- @pre_nmacinit@ --- * \
103 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
104 * @const void *ok@ = pointer to outer hash init vector \
105 * @const void *ik@ = pointer to inner hash init vector \
109 * Use: Initializes a MAC key for doing NMAC hashing. \
112 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
114 memcpy(key->ochain, ok, PRE##_STATESZ); \
115 memcpy(key->ichain, ik, PRE##_STATESZ); \
116 key->ocount = key->icount = 0; \
119 /* --- @pre_hmacinit@ --- * \
121 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
122 * @const void *k@ = pointer to key to use \
123 * @size_t sz@ = size of key data \
127 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
128 * longer than the hash function's output size aren't very \
129 * useful, but are accepted. Keys longer than the hash's \
130 * block size are also accepted; they are hashed before \
131 * use, as specified in RFC2104. \
134 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
137 const octet *kbuf = k; \
139 octet buf[PRE##_HASHSZ]; \
141 if (sz > PRE##_BUFSZ) { \
143 pre##_hash(&ctx, k, sz); \
144 pre##_done(&ctx, buf); \
150 memset(ctx.buf, 0x5c, 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->ochain); \
157 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
158 for (i = 0; i < sz; i++) \
159 ctx.buf[i] ^= kbuf[i]; \
160 pre##_compress(&ctx, ctx.buf); \
161 pre##_state(&ctx, key->ichain); \
163 key->ocount = key->icount = PRE##_BUFSZ; \
167 /* --- @pre_sslmacinit@ --- * \
169 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
170 * @const void *k@ = pointer to key to use \
171 * @size_t sz@ = size of key data \
175 * Use: Initializes a MAC key for doing hasing using the SSL3 \
179 void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \
182 const octet *kbuf = k; \
184 octet buf[PRE##_HASHSZ]; \
186 if (sz > PRE##_BUFSZ) { \
188 pre##_hash(&ctx, k, sz); \
189 pre##_done(&ctx, buf); \
195 memcpy(ctx.buf, kbuf, sz); \
196 memset(ctx.buf + sz, 0x5c, PRE##_BUFSZ - sz); \
197 pre##_compress(&ctx, ctx.buf); \
198 pre##_state(&ctx, key->ochain); \
201 memcpy(ctx.buf, kbuf, sz); \
202 memset(ctx.buf + sz, 0x36, PRE##_BUFSZ - sz); \
203 pre##_compress(&ctx, ctx.buf); \
204 pre##_state(&ctx, key->ichain); \
206 key->ocount = key->icount = PRE##_BUFSZ; \
210 /* --- @pre_macinit@ --- * \
212 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
213 * @const pre_mackey *key@ = pointer to MAC key block \
217 * Use: Instantiates a MAC context from a key block. \
220 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
222 memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \
223 ctx->count = key->ocount; \
224 pre##_set(&ctx->ctx, key->ichain, key->icount); \
227 /* --- @pre_machash@ --- * \
229 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
230 * @const void *buf@ = pointer to buffer \
231 * @size_t sz@ = size of the buffer \
235 * Use: Hashes a buffer. \
238 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
240 pre##_hash(&ctx->ctx, buf, sz); \
243 /* --- @pre_macdone@ --- * \
245 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
246 * @void *mac@ = pointer to buffer to receive MAC \
250 * Use: Returns the result of a MAC computation. \
253 void pre##_macdone(pre##_macctx *ctx, void *mac) \
255 pre##_done(&ctx->ctx, mac); \
256 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
257 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
258 pre##_done(&ctx->ctx, mac); \
261 /* --- Generic MAC interface --- */ \
263 static const gmac_ops gkops; \
264 static const ghash_ops gops; \
266 typedef struct gkctx { \
271 typedef struct gctx { \
274 octet buf[PRE##_HASHSZ]; \
277 static ghash *gkinit(gmac *m) \
279 gkctx *gk = (gkctx *)m; \
280 gctx *g = S_CREATE(gctx); \
282 pre##_macinit(&g->c, &gk->k); \
286 static gmac *gkey(const void *k, size_t sz) \
288 gkctx *gk = S_CREATE(gkctx); \
289 gk->m.ops = &gkops; \
290 pre##_hmacinit(&gk->k, k, sz); \
294 static gmac *gsslkey(const void *k, size_t sz) \
296 gkctx *gk = S_CREATE(gkctx); \
297 gk->m.ops = &gkops; \
298 pre##_sslmacinit(&gk->k, k, sz); \
302 static void ghhash(ghash *h, const void *p, size_t sz) \
304 gctx *g = (gctx *)h; \
305 pre##_machash(&g->c, p, sz); \
308 static octet *ghdone(ghash *h, void *buf) \
310 gctx *g = (gctx *)h; \
313 pre##_macdone(&g->c, buf); \
317 static ghash *ghcopy(ghash *h) \
319 gctx *g = (gctx *)h; \
320 gctx *gg = S_CREATE(gctx); \
321 memcpy(gg, g, sizeof(gctx)); \
325 static void ghdestroy(ghash *h) \
327 gctx *g = (gctx *)h; \
332 static void gkdestroy(gmac *m) \
334 gkctx *gk = (gkctx *)m; \
339 static ghash *ghinit(void) \
341 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
345 const gcmac pre##_hmac = \
346 { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey }; \
347 const gcmac pre##_sslmac = \
348 { #pre "-sslmac", PRE##_HASHSZ, pre##_mackeysz, gsslkey }; \
349 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
350 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
351 static const ghash_ops gops = \
352 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
356 /* --- @HMAC_TEST@ --- *
358 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
360 * Use: Standard test rig for MAC functions.
367 #include <mLib/dstr.h>
368 #include <mLib/quis.h>
369 #include <mLib/testrig.h>
371 #define HMAC_TEST(PRE, pre) \
373 static int macverify(dstr *v) \
380 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
385 dstr_ensure(&d, PRE##_HASHSZ); \
386 d.len = PRE##_HASHSZ; \
388 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
390 for (ip = szs; *ip; ip++) { \
397 p = (octet *)v[0].buf; \
398 pre##_macinit(&cctx, &ckey); \
402 pre##_machash(&cctx, p, i); \
406 pre##_macdone(&cctx, d.buf); \
407 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
408 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
410 type_hex.dump(&v[1], stdout); \
411 fputs("\n\texpected = ", stdout); \
412 type_hex.dump(&v[2], stdout); \
413 fputs("\n\tcomputed = ", stdout); \
414 type_hex.dump(&d, stdout); \
424 static test_chunk macdefs[] = { \
425 { #pre "-hmac", macverify, \
426 { &type_string, &type_hex, &type_hex, 0 } }, \
430 int main(int argc, char *argv[]) \
433 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
438 # define HMAC_TEST(PRE, pre)
441 /*----- That's all, folks -------------------------------------------------*/