3 * $Id: hmac-def.h,v 1.1 1999/12/10 23:16:40 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.1 1999/12/10 23:16:40 mdw
34 * Split mode macros into interface and implementation.
38 #ifndef CATACOMB_HMAC_DEF_H
39 #define CATACOMB_HMAC_DEF_H
45 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/bits.h>
53 #ifndef CATACOMB_GMAC_H
57 #ifndef CATACOMB_PARANOIA_H
58 # include "paranoia.h"
61 /*----- Macros ------------------------------------------------------------*/
63 /* --- @HMAC_DEF@ --- *
65 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
67 * Use: Creates implementations for the HMAC and NMAC functions.
70 #define HMAC_DEF(PRE, pre) \
72 /* --- @pre_nmacinit@ --- * \
74 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
75 * @const void *ok@ = pointer to outer hash init vector \
76 * @const void *ik@ = pointer to inner hash init vector \
80 * Use: Initializes a MAC key for doing NMAC hashing. \
83 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
85 memcpy(key->ochain, ok, PRE##_HASHSZ); \
86 memcpy(key->ichain, ik, PRE##_HASHSZ); \
87 key->ocount = key->icount = 0; \
90 /* --- @pre_hmacinit@ --- * \
92 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
93 * @const void *k@ = pointer to key to use \
94 * @size_t sz@ = size of key data \
98 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
99 * longer than the hash function's output size aren't very \
100 * useful, but are accepted. Keys longer than the hash's \
101 * block size are also accepted; they are hashed before \
102 * use, as specified in RFC2104. \
105 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
108 const octet *kbuf = k; \
110 octet buf[PRE##_HASHSZ]; \
112 if (sz > PRE##_BUFSZ) { \
114 pre##_hash(&ctx, k, sz); \
115 pre##_done(&ctx, buf); \
121 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
122 for (i = 0; i < sz; i++) \
123 ctx.buf[i] ^= kbuf[i]; \
124 pre##_compress(&ctx, ctx.buf); \
125 pre##_state(&ctx, key->ochain); \
128 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
129 for (i = 0; i < sz; i++) \
130 ctx.buf[i] ^= kbuf[i]; \
131 pre##_compress(&ctx, ctx.buf); \
132 pre##_state(&ctx, key->ichain); \
134 key->ocount = key->icount = PRE##_BUFSZ; \
138 /* --- @pre_macinit@ --- * \
140 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
141 * @const pre_mackey *key@ = pointer to MAC key block \
145 * Use: Instantiates a MAC context from a key block. \
148 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
150 memcpy(ctx->chain, key->ochain, PRE##_HASHSZ); \
151 ctx->count = key->ocount; \
152 pre##_set(&ctx->ctx, key->ichain, key->icount); \
155 /* --- @pre_machash@ --- * \
157 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
158 * @const void *buf@ = pointer to buffer \
159 * @size_t sz@ = size of the buffer \
163 * Use: Hashes a buffer. \
166 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
168 pre##_hash(&ctx->ctx, buf, sz); \
171 /* --- @pre_macdone@ --- * \
173 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
174 * @void *mac@ = pointer to buffer to receive MAC \
178 * Use: Returns the result of a MAC computation. \
181 void pre##_macdone(pre##_macctx *ctx, void *mac) \
183 pre##_done(&ctx->ctx, mac); \
184 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
185 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
186 pre##_done(&ctx->ctx, mac); \
189 /* --- Generic MAC interface --- */ \
191 static const gmac_ops gkops; \
192 static const ghash_ops gops; \
194 typedef struct gkctx { \
199 typedef struct gctx { \
204 static ghash *gkinit(gmac *m) \
206 gkctx *gk = (gkctx *)m; \
207 gctx *g = CREATE(gctx); \
209 pre##_macinit(&g->c, &gk->k); \
213 static gmac *gkey(const void *k, size_t sz) \
215 gkctx *gk = CREATE(gkctx); \
216 gk->m.ops = &gkops; \
217 pre##_hmacinit(&gk->k, k, sz); \
221 static void ghhash(ghash *h, const void *p, size_t sz) \
223 gctx *g = (gctx *)h; \
224 pre##_machash(&g->c, p, sz); \
227 static void ghdone(ghash *h, void *buf) \
229 gctx *g = (gctx *)h; \
230 pre##_macdone(&g->c, buf); \
233 static void ghdestroy(ghash *h) \
235 gctx *g = (gctx *)h; \
239 static void gkdestroy(gmac *m) \
241 gkctx *gk = (gkctx *)m; \
245 const gcmac pre##_hmac = { { #pre "-hmac", PRE##_HASHSZ }, gkey }; \
246 static const gmac_ops gkops = { &pre##_hmac.b, gkinit, gkdestroy }; \
247 static const ghash_ops gops = \
248 { &pre##_hmac.b, ghhash, ghdone, ghdestroy }; \
252 /* --- @HMAC_TEST@ --- *
254 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
256 * Use: Standard test rig for MAC functions.
263 #include <mLib/dstr.h>
264 #include <mLib/quis.h>
265 #include <mLib/testrig.h>
267 #define HMAC_TEST(PRE, pre) \
269 static int macverify(dstr *v) \
276 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
281 dstr_ensure(&d, PRE##_HASHSZ); \
282 d.len = PRE##_HASHSZ; \
284 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
286 for (ip = szs; *ip; ip++) { \
293 p = (octet *)v[0].buf; \
294 pre##_macinit(&cctx, &ckey); \
298 pre##_machash(&cctx, p, i); \
302 pre##_macdone(&cctx, d.buf); \
303 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
304 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
306 type_hex.dump(&v[1], stdout); \
307 fputs("\n\texpected = ", stdout); \
308 type_hex.dump(&v[2], stdout); \
309 fputs("\n\tcomputed = ", stdout); \
310 type_hex.dump(&d, stdout); \
320 static test_chunk macdefs[] = { \
321 { #pre "-hmac", macverify, \
322 { &type_string, &type_hex, &type_hex, 0 } }, \
326 int main(int argc, char *argv[]) \
329 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
334 # define HMAC_TEST(PRE, pre)
337 /*----- That's all, folks -------------------------------------------------*/