3 * Definitions for HMAC and NMAC
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_HMAC_DEF_H
29 #define CATACOMB_HMAC_DEF_H
35 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
44 #ifndef CATACOMB_ARENA_H
48 #ifndef CATACOMB_GMAC_H
52 #ifndef CATACOMB_PARANOIA_H
53 # include "paranoia.h"
56 /*----- Macros ------------------------------------------------------------*/
58 /* --- @HMAC_DEF@ --- *
60 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
62 * Use: Creates implementations for the HMAC and NMAC functions.
65 #define HMAC_DEF(PRE, pre) \
67 /* --- Useful constants --- */ \
69 const octet pre##_hmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
70 const octet pre##_sslmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
71 const octet pre##_nmackeysz[] = { KSZ_SET, 2 * PRE##_STATESZ, 0 }; \
73 /* --- @pre_nmacinit@ --- * \
75 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
76 * @const void *ok@ = pointer to outer hash init vector \
77 * @const void *ik@ = pointer to inner hash init vector \
81 * Use: Initializes a MAC key for doing NMAC hashing. \
84 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
86 memcpy(key->ochain, ok, PRE##_STATESZ); \
87 memcpy(key->ichain, ik, PRE##_STATESZ); \
88 key->ocount = key->icount = 0; \
91 /* --- @pre_hmacinit@ --- * \
93 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
94 * @const void *k@ = pointer to key to use \
95 * @size_t sz@ = size of key data \
99 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
100 * longer than the hash function's output size aren't very \
101 * useful, but are accepted. Keys longer than the hash's \
102 * block size are also accepted; they are hashed before \
103 * use, as specified in RFC2104. \
106 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
109 const octet *kbuf = k; \
111 octet buf[PRE##_HASHSZ]; \
113 if (sz > PRE##_BUFSZ) { \
115 pre##_hash(&ctx, k, sz); \
116 pre##_done(&ctx, buf); \
122 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
123 for (i = 0; i < sz; i++) \
124 ctx.buf[i] ^= kbuf[i]; \
125 pre##_compress(&ctx, ctx.buf); \
126 pre##_state(&ctx, key->ochain); \
129 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
130 for (i = 0; i < sz; i++) \
131 ctx.buf[i] ^= kbuf[i]; \
132 pre##_compress(&ctx, ctx.buf); \
133 pre##_state(&ctx, key->ichain); \
135 key->ocount = key->icount = PRE##_BUFSZ; \
139 /* --- @pre_sslmacinit@ --- * \
141 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
142 * @const void *k@ = pointer to key to use \
143 * @size_t sz@ = size of key data \
147 * Use: Initializes a MAC key for doing hasing using the SSL3 \
151 void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \
153 const octet *kbuf = k; \
155 octet buf[PRE##_HASHSZ]; \
157 if (sz > PRE##_BUFSZ) { \
159 pre##_hash(&ctx, k, sz); \
160 pre##_done(&ctx, buf); \
166 memcpy(ctx.buf, kbuf, sz); \
167 memset(ctx.buf + sz, 0x5c, PRE##_BUFSZ - sz); \
168 pre##_compress(&ctx, ctx.buf); \
169 pre##_state(&ctx, key->ochain); \
172 memcpy(ctx.buf, kbuf, sz); \
173 memset(ctx.buf + sz, 0x36, PRE##_BUFSZ - sz); \
174 pre##_compress(&ctx, ctx.buf); \
175 pre##_state(&ctx, key->ichain); \
177 key->ocount = key->icount = PRE##_BUFSZ; \
181 /* --- @pre_macinit@ --- * \
183 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
184 * @const pre_mackey *key@ = pointer to MAC key block \
188 * Use: Instantiates a MAC context from a key block. \
191 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
193 memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \
194 ctx->count = key->ocount; \
195 pre##_set(&ctx->ctx, key->ichain, key->icount); \
198 /* --- @pre_machash@ --- * \
200 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
201 * @const void *buf@ = pointer to buffer \
202 * @size_t sz@ = size of the buffer \
206 * Use: Hashes a buffer. \
209 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
211 pre##_hash(&ctx->ctx, buf, sz); \
214 /* --- @pre_macdone@ --- * \
216 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
217 * @void *mac@ = pointer to buffer to receive MAC \
221 * Use: Returns the result of a MAC computation. \
224 void pre##_macdone(pre##_macctx *ctx, void *mac) \
226 pre##_done(&ctx->ctx, mac); \
227 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
228 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
229 pre##_done(&ctx->ctx, mac); \
232 /* --- Generic MAC interface --- */ \
234 static const gmac_ops gkops; \
235 static const ghash_ops gops, gnops, gsslops; \
237 typedef struct gkctx { \
239 const ghash_ops *gops; \
243 typedef struct gctx { \
246 octet buf[PRE##_HASHSZ]; \
249 static ghash *gkinit(gmac *m) \
251 gkctx *gk = (gkctx *)m; \
252 gctx *g = S_CREATE(gctx); \
253 g->h.ops = gk->gops; \
254 pre##_macinit(&g->c, &gk->k); \
258 static gmac *gkey(const void *k, size_t sz) \
260 gkctx *gk = S_CREATE(gkctx); \
261 gk->m.ops = &gkops; \
263 pre##_hmacinit(&gk->k, k, sz); \
267 static gmac *gnkey(const void *k, size_t sz) \
269 gkctx *gk = S_CREATE(gkctx); \
270 const octet *kk = k; \
271 assert(keysz(sz, pre##_nmackeysz) == sz); \
272 gk->m.ops = &gkops; \
274 pre##_nmacinit(&gk->k, kk, kk + PRE##_STATESZ); \
278 static gmac *gsslkey(const void *k, size_t sz) \
280 gkctx *gk = S_CREATE(gkctx); \
281 gk->m.ops = &gkops; \
282 gk->gops = &gsslops; \
283 pre##_sslmacinit(&gk->k, k, sz); \
287 static void ghhash(ghash *h, const void *p, size_t sz) \
289 gctx *g = (gctx *)h; \
290 pre##_machash(&g->c, p, sz); \
293 static octet *ghdone(ghash *h, void *buf) \
295 gctx *g = (gctx *)h; \
298 pre##_macdone(&g->c, buf); \
302 static ghash *ghcopy(ghash *h) \
304 gctx *g = (gctx *)h; \
305 gctx *gg = S_CREATE(gctx); \
306 memcpy(gg, g, sizeof(gctx)); \
310 static void ghdestroy(ghash *h) \
312 gctx *g = (gctx *)h; \
317 static void gkdestroy(gmac *m) \
319 gkctx *gk = (gkctx *)m; \
324 static ghash *ghinit(void) \
326 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
330 const gcmac pre##_nmac = \
331 { #pre "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey }; \
332 const gcmac pre##_hmac = \
333 { #pre "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey }; \
334 const gcmac pre##_sslmac = \
335 { #pre "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey }; \
336 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
337 static const gmac_ops gnkops = { &pre##_nmac, gkinit, gkdestroy }; \
338 static const gmac_ops gsslkops = { &pre##_sslmac, gkinit, gkdestroy }; \
339 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
340 static const ghash_ops gops = \
341 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
342 static const gchash gnch = { #pre "-nmac", PRE##_HASHSZ, ghinit }; \
343 static const ghash_ops gnops = \
344 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
345 static const gchash gsslch = { #pre "-sslmac", PRE##_HASHSZ, ghinit }; \
346 static const ghash_ops gsslops = \
347 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
351 /* --- @HMAC_TEST@ --- *
353 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
355 * Use: Standard test rig for MAC functions.
362 #include <mLib/dstr.h>
363 #include <mLib/quis.h>
364 #include <mLib/testrig.h>
366 #define HMAC_TEST(PRE, pre) \
368 static int macverify(dstr *v) \
375 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
380 dstr_ensure(&d, PRE##_HASHSZ); \
381 d.len = PRE##_HASHSZ; \
383 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
385 for (ip = szs; *ip; ip++) { \
392 p = (octet *)v[0].buf; \
393 pre##_macinit(&cctx, &ckey); \
397 pre##_machash(&cctx, p, i); \
401 pre##_macdone(&cctx, d.buf); \
402 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
403 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
405 type_hex.dump(&v[1], stdout); \
406 fputs("\n\texpected = ", stdout); \
407 type_hex.dump(&v[2], stdout); \
408 fputs("\n\tcomputed = ", stdout); \
409 type_hex.dump(&d, stdout); \
419 static test_chunk macdefs[] = { \
420 { #pre "-hmac", macverify, \
421 { &type_string, &type_hex, &type_hex, 0 } }, \
425 int main(int argc, char *argv[]) \
428 test_run(argc, argv, macdefs, SRCDIR"/t/" #pre); \
433 # define HMAC_TEST(PRE, pre)
436 /*----- That's all, folks -------------------------------------------------*/