3 * $Id: hmac-def.h,v 1.2 2000/06/17 11:23:44 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.2 2000/06/17 11:23:44 mdw
34 * Use secure arena for memory allocation. Minor changes in the generic
37 * Revision 1.1 1999/12/10 23:16:40 mdw
38 * Split mode macros into interface and implementation.
42 #ifndef CATACOMB_HMAC_DEF_H
43 #define CATACOMB_HMAC_DEF_H
49 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/bits.h>
58 #ifndef CATACOMB_ARENA_H
62 #ifndef CATACOMB_GMAC_H
66 #ifndef CATACOMB_PARANOIA_H
67 # include "paranoia.h"
70 /*----- Macros ------------------------------------------------------------*/
72 /* --- @HMAC_DEF@ --- *
74 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
76 * Use: Creates implementations for the HMAC and NMAC functions.
79 #define HMAC_DEF(PRE, pre) \
81 /* --- Useful constants --- */ \
83 const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ }; \
85 /* --- @pre_nmacinit@ --- * \
87 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
88 * @const void *ok@ = pointer to outer hash init vector \
89 * @const void *ik@ = pointer to inner hash init vector \
93 * Use: Initializes a MAC key for doing NMAC hashing. \
96 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
98 memcpy(key->ochain, ok, PRE##_HASHSZ); \
99 memcpy(key->ichain, ik, PRE##_HASHSZ); \
100 key->ocount = key->icount = 0; \
103 /* --- @pre_hmacinit@ --- * \
105 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
106 * @const void *k@ = pointer to key to use \
107 * @size_t sz@ = size of key data \
111 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
112 * longer than the hash function's output size aren't very \
113 * useful, but are accepted. Keys longer than the hash's \
114 * block size are also accepted; they are hashed before \
115 * use, as specified in RFC2104. \
118 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
121 const octet *kbuf = k; \
123 octet buf[PRE##_HASHSZ]; \
125 if (sz > PRE##_BUFSZ) { \
127 pre##_hash(&ctx, k, sz); \
128 pre##_done(&ctx, buf); \
134 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
135 for (i = 0; i < sz; i++) \
136 ctx.buf[i] ^= kbuf[i]; \
137 pre##_compress(&ctx, ctx.buf); \
138 pre##_state(&ctx, key->ochain); \
141 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
142 for (i = 0; i < sz; i++) \
143 ctx.buf[i] ^= kbuf[i]; \
144 pre##_compress(&ctx, ctx.buf); \
145 pre##_state(&ctx, key->ichain); \
147 key->ocount = key->icount = PRE##_BUFSZ; \
151 /* --- @pre_macinit@ --- * \
153 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
154 * @const pre_mackey *key@ = pointer to MAC key block \
158 * Use: Instantiates a MAC context from a key block. \
161 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
163 memcpy(ctx->chain, key->ochain, PRE##_HASHSZ); \
164 ctx->count = key->ocount; \
165 pre##_set(&ctx->ctx, key->ichain, key->icount); \
168 /* --- @pre_machash@ --- * \
170 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
171 * @const void *buf@ = pointer to buffer \
172 * @size_t sz@ = size of the buffer \
176 * Use: Hashes a buffer. \
179 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
181 pre##_hash(&ctx->ctx, buf, sz); \
184 /* --- @pre_macdone@ --- * \
186 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
187 * @void *mac@ = pointer to buffer to receive MAC \
191 * Use: Returns the result of a MAC computation. \
194 void pre##_macdone(pre##_macctx *ctx, void *mac) \
196 pre##_done(&ctx->ctx, mac); \
197 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
198 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
199 pre##_done(&ctx->ctx, mac); \
202 /* --- Generic MAC interface --- */ \
204 static const gmac_ops gkops; \
205 static const ghash_ops gops; \
207 typedef struct gkctx { \
212 typedef struct gctx { \
217 static ghash *gkinit(gmac *m) \
219 gkctx *gk = (gkctx *)m; \
220 gctx *g = S_CREATE(gctx); \
222 pre##_macinit(&g->c, &gk->k); \
226 static gmac *gkey(const void *k, size_t sz) \
228 gkctx *gk = S_CREATE(gkctx); \
229 gk->m.ops = &gkops; \
230 pre##_hmacinit(&gk->k, k, sz); \
234 static void ghhash(ghash *h, const void *p, size_t sz) \
236 gctx *g = (gctx *)h; \
237 pre##_machash(&g->c, p, sz); \
240 static void ghdone(ghash *h, void *buf) \
242 gctx *g = (gctx *)h; \
243 pre##_macdone(&g->c, buf); \
246 static void ghdestroy(ghash *h) \
248 gctx *g = (gctx *)h; \
253 static void gkdestroy(gmac *m) \
255 gkctx *gk = (gkctx *)m; \
260 static ghash *ghinit(void) \
262 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
266 const gcmac pre##_hmac = \
267 { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey }; \
268 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
269 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
270 static const ghash_ops gops = \
271 { &gch, ghhash, ghdone, ghdestroy }; \
275 /* --- @HMAC_TEST@ --- *
277 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
279 * Use: Standard test rig for MAC functions.
286 #include <mLib/dstr.h>
287 #include <mLib/quis.h>
288 #include <mLib/testrig.h>
290 #define HMAC_TEST(PRE, pre) \
292 static int macverify(dstr *v) \
299 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
304 dstr_ensure(&d, PRE##_HASHSZ); \
305 d.len = PRE##_HASHSZ; \
307 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
309 for (ip = szs; *ip; ip++) { \
316 p = (octet *)v[0].buf; \
317 pre##_macinit(&cctx, &ckey); \
321 pre##_machash(&cctx, p, i); \
325 pre##_macdone(&cctx, d.buf); \
326 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
327 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
329 type_hex.dump(&v[1], stdout); \
330 fputs("\n\texpected = ", stdout); \
331 type_hex.dump(&v[2], stdout); \
332 fputs("\n\tcomputed = ", stdout); \
333 type_hex.dump(&d, stdout); \
343 static test_chunk macdefs[] = { \
344 { #pre "-hmac", macverify, \
345 { &type_string, &type_hex, &type_hex, 0 } }, \
349 int main(int argc, char *argv[]) \
352 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
357 # define HMAC_TEST(PRE, pre)
360 /*----- That's all, folks -------------------------------------------------*/