3 * $Id: hmac-def.h,v 1.3 2000/07/02 18:27:42 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.3 2000/07/02 18:27:42 mdw
34 * (ghash->ops->done): Interface change. Passing in a null buffer pointer
35 * uses a buffer internal to the ghash object. The operation returns the
36 * address of the buffer it used. Clients of generic hashes no longer need
37 * to use dynamically allocated memory for hash results.
39 * Revision 1.2 2000/06/17 11:23:44 mdw
40 * Use secure arena for memory allocation. Minor changes in the generic
43 * Revision 1.1 1999/12/10 23:16:40 mdw
44 * Split mode macros into interface and implementation.
48 #ifndef CATACOMB_HMAC_DEF_H
49 #define CATACOMB_HMAC_DEF_H
55 /*----- Header files ------------------------------------------------------*/
61 #include <mLib/bits.h>
64 #ifndef CATACOMB_ARENA_H
68 #ifndef CATACOMB_GMAC_H
72 #ifndef CATACOMB_PARANOIA_H
73 # include "paranoia.h"
76 /*----- Macros ------------------------------------------------------------*/
78 /* --- @HMAC_DEF@ --- *
80 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
82 * Use: Creates implementations for the HMAC and NMAC functions.
85 #define HMAC_DEF(PRE, pre) \
87 /* --- Useful constants --- */ \
89 const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ }; \
91 /* --- @pre_nmacinit@ --- * \
93 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
94 * @const void *ok@ = pointer to outer hash init vector \
95 * @const void *ik@ = pointer to inner hash init vector \
99 * Use: Initializes a MAC key for doing NMAC hashing. \
102 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
104 memcpy(key->ochain, ok, PRE##_HASHSZ); \
105 memcpy(key->ichain, ik, PRE##_HASHSZ); \
106 key->ocount = key->icount = 0; \
109 /* --- @pre_hmacinit@ --- * \
111 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
112 * @const void *k@ = pointer to key to use \
113 * @size_t sz@ = size of key data \
117 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
118 * longer than the hash function's output size aren't very \
119 * useful, but are accepted. Keys longer than the hash's \
120 * block size are also accepted; they are hashed before \
121 * use, as specified in RFC2104. \
124 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
127 const octet *kbuf = k; \
129 octet buf[PRE##_HASHSZ]; \
131 if (sz > PRE##_BUFSZ) { \
133 pre##_hash(&ctx, k, sz); \
134 pre##_done(&ctx, buf); \
140 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
141 for (i = 0; i < sz; i++) \
142 ctx.buf[i] ^= kbuf[i]; \
143 pre##_compress(&ctx, ctx.buf); \
144 pre##_state(&ctx, key->ochain); \
147 memset(ctx.buf, 0x36, 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->ichain); \
153 key->ocount = key->icount = PRE##_BUFSZ; \
157 /* --- @pre_macinit@ --- * \
159 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
160 * @const pre_mackey *key@ = pointer to MAC key block \
164 * Use: Instantiates a MAC context from a key block. \
167 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
169 memcpy(ctx->chain, key->ochain, PRE##_HASHSZ); \
170 ctx->count = key->ocount; \
171 pre##_set(&ctx->ctx, key->ichain, key->icount); \
174 /* --- @pre_machash@ --- * \
176 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
177 * @const void *buf@ = pointer to buffer \
178 * @size_t sz@ = size of the buffer \
182 * Use: Hashes a buffer. \
185 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
187 pre##_hash(&ctx->ctx, buf, sz); \
190 /* --- @pre_macdone@ --- * \
192 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
193 * @void *mac@ = pointer to buffer to receive MAC \
197 * Use: Returns the result of a MAC computation. \
200 void pre##_macdone(pre##_macctx *ctx, void *mac) \
202 pre##_done(&ctx->ctx, mac); \
203 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
204 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
205 pre##_done(&ctx->ctx, mac); \
208 /* --- Generic MAC interface --- */ \
210 static const gmac_ops gkops; \
211 static const ghash_ops gops; \
213 typedef struct gkctx { \
218 typedef struct gctx { \
221 octet buf[PRE##_HASHSZ]; \
224 static ghash *gkinit(gmac *m) \
226 gkctx *gk = (gkctx *)m; \
227 gctx *g = S_CREATE(gctx); \
229 pre##_macinit(&g->c, &gk->k); \
233 static gmac *gkey(const void *k, size_t sz) \
235 gkctx *gk = S_CREATE(gkctx); \
236 gk->m.ops = &gkops; \
237 pre##_hmacinit(&gk->k, k, sz); \
241 static void ghhash(ghash *h, const void *p, size_t sz) \
243 gctx *g = (gctx *)h; \
244 pre##_machash(&g->c, p, sz); \
247 static octet *ghdone(ghash *h, void *buf) \
249 gctx *g = (gctx *)h; \
252 pre##_macdone(&g->c, buf); \
256 static void ghdestroy(ghash *h) \
258 gctx *g = (gctx *)h; \
263 static void gkdestroy(gmac *m) \
265 gkctx *gk = (gkctx *)m; \
270 static ghash *ghinit(void) \
272 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
276 const gcmac pre##_hmac = \
277 { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey }; \
278 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
279 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
280 static const ghash_ops gops = \
281 { &gch, ghhash, ghdone, ghdestroy }; \
285 /* --- @HMAC_TEST@ --- *
287 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
289 * Use: Standard test rig for MAC functions.
296 #include <mLib/dstr.h>
297 #include <mLib/quis.h>
298 #include <mLib/testrig.h>
300 #define HMAC_TEST(PRE, pre) \
302 static int macverify(dstr *v) \
309 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
314 dstr_ensure(&d, PRE##_HASHSZ); \
315 d.len = PRE##_HASHSZ; \
317 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
319 for (ip = szs; *ip; ip++) { \
326 p = (octet *)v[0].buf; \
327 pre##_macinit(&cctx, &ckey); \
331 pre##_machash(&cctx, p, i); \
335 pre##_macdone(&cctx, d.buf); \
336 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
337 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
339 type_hex.dump(&v[1], stdout); \
340 fputs("\n\texpected = ", stdout); \
341 type_hex.dump(&v[2], stdout); \
342 fputs("\n\tcomputed = ", stdout); \
343 type_hex.dump(&d, stdout); \
353 static test_chunk macdefs[] = { \
354 { #pre "-hmac", macverify, \
355 { &type_string, &type_hex, &type_hex, 0 } }, \
359 int main(int argc, char *argv[]) \
362 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
367 # define HMAC_TEST(PRE, pre)
370 /*----- That's all, folks -------------------------------------------------*/