3 * $Id: hmac.h,v 1.1 1999/09/03 08:41:12 mdw Exp $
5 * Generic code for HMAC and NMAC
7 * (c) 1998 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 --------------------------------------------------*
33 * Revision 1.1 1999/09/03 08:41:12 mdw
38 /*----- Notes on the HMAC and NMAC constructions --------------------------*
40 * Designed by Mihir Bellare, Ran Canetti and Hugo Krawczyk, NMAC is a
41 * method for constructing keyed message authentication algorithms from
42 * unkeyed hash functions. HMAC is an alternative formulation which doesn't
43 * require low-level access to the hash function's implementation. NMAC was
44 * designed to allow MD5 has a suitable underlying hash function, even though
45 * doubts were already being raised about its collision resistance.
55 /*----- Header files ------------------------------------------------------*/
60 #include <mLib/bits.h>
63 # include "paranoia.h"
66 /*----- Macros ------------------------------------------------------------*/
68 /* --- @HMAC_DECL@ --- *
70 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
72 * Use: Creates declarations for the HMAC and NMAC functions.
75 #define HMAC_DECL(PRE, pre) \
77 typedef struct pre##_mackey { \
78 octet ochain[PRE##_HASHSZ]; /* Chaining for outer hash */ \
79 unsigned long ocount; /* Byte count for outer hash */ \
80 octet ichain[PRE##_HASHSZ]; /* Chaining for inner hash */ \
81 unsigned long icount; /* Byte count for inner hash */ \
84 typedef struct pre##_macctx { \
85 pre##_ctx ctx; /* Context for main hashing */ \
86 octet chain[PRE##_HASHSZ]; /* Chaining for outer hash */ \
87 unsigned long count; /* Byte count for outer hash */ \
90 extern void pre##_nmac(pre##_mackey */*key*/, \
91 const void */*ok*/, const void */*ik*/); \
93 extern void pre##_hmac(pre##_mackey */*key*/, \
94 const void */*k*/, size_t /*sz*/); \
96 extern void pre##_macinit(pre##_macctx */*ctx*/, \
97 const pre##_mackey */*key*/); \
99 extern void pre##_mac(pre##_macctx */*ctx*/, \
100 const void */*buf*/, size_t /*sz*/); \
102 extern void pre##_macdone(pre##_macctx */*ctx*/, void */*mac*/);
104 /* --- @HMAC_DEF@ --- *
106 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
108 * Use: Creates implementations for the HMAC and NMAC functions.
111 #define HMAC_DEF(PRE, pre) \
113 /* --- @pre_nmac@ --- * \
115 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
116 * @const void *ok@ = pointer to outer hash init vector \
117 * @const void *ik@ = pointer to inner hash init vector \
121 * Use: Initializes a MAC key for doing NMAC hashing. \
124 void pre##_nmac(pre##_mackey *key, const void *ok, const void *ik) \
126 memcpy(key->ochain, ok, PRE##_HASHSZ); \
127 memcpy(key->ichain, ik, PRE##_HASHSZ); \
128 key->ocount = key->icount = 0; \
131 /* --- @pre_hmac@ --- * \
133 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
134 * @const void *k@ = pointer to key to use \
135 * @size_t sz@ = size of key data \
139 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
140 * longer than the hash function's output size aren't very \
141 * useful, but are accepted. Keys longer than the hash's \
142 * block size are also accepted; they are hashed before \
143 * use, as specified in RFC2104. \
146 void pre##_hmac(pre##_mackey *key, const void *k, size_t sz) \
149 const octet *kbuf = k; \
151 octet buf[PRE##_HASHSZ]; \
153 if (sz > PRE##_BUFSZ) { \
155 pre##_hash(&ctx, k, sz); \
156 pre##_done(&ctx, buf); \
162 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
163 for (i = 0; i < sz; i++) \
164 ctx.buf[i] ^= kbuf[i]; \
165 pre##_compress(&ctx, ctx.buf); \
166 pre##_state(&ctx, key->ochain); \
169 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
170 for (i = 0; i < sz; i++) \
171 ctx.buf[i] ^= kbuf[i]; \
172 pre##_compress(&ctx, ctx.buf); \
173 pre##_state(&ctx, key->ichain); \
175 key->ocount = key->icount = PRE##_BUFSZ; \
179 /* --- @pre_macinit@ --- * \
181 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
182 * @const pre_mackey *key@ = pointer to MAC key block \
186 * Use: Instantiates a MAC context from a key block. \
189 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
191 memcpy(ctx->chain, key->ochain, PRE##_HASHSZ); \
192 ctx->count = key->ocount; \
193 pre##_set(&ctx->ctx, key->ichain, key->icount); \
196 /* --- @pre_mac@ --- * \
198 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
199 * @const void *buf@ = pointer to buffer \
200 * @size_t sz@ = size of the buffer \
204 * Use: Hashes a buffer. \
207 void pre##_mac(pre##_macctx *ctx, const void *buf, size_t sz) \
209 pre##_hash(&ctx->ctx, buf, sz); \
212 /* --- @pre_macdone@ --- * \
214 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
215 * @void *mac@ = pointer to buffer to receive MAC \
219 * Use: Returns the result of a MAC computation. \
222 void pre##_macdone(pre##_macctx *ctx, void *mac) \
224 pre##_done(&ctx->ctx, mac); \
225 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
226 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
227 pre##_done(&ctx->ctx, mac); \
230 HMAC_TEST(PRE, pre) \
232 /* --- @HMAC_TEST@ --- *
234 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
236 * Use: Standard test rig for MAC functions.
241 #include <mLib/dstr.h>
242 #include <mLib/quis.h>
243 #include <mLib/testrig.h>
245 #define HMAC_TEST(PRE, pre) \
247 static int macverify(dstr *v) \
254 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
259 dstr_ensure(&d, PRE##_HASHSZ); \
260 d.len = PRE##_HASHSZ; \
262 pre##_hmac(&ckey, v[1].buf, v[1].len); \
264 for (ip = szs; *ip; ip++) { \
271 p = (octet *)v[0].buf; \
272 pre##_macinit(&cctx, &ckey); \
276 pre##_mac(&cctx, p, i); \
280 pre##_macdone(&cctx, d.buf); \
281 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
282 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
284 type_hex.dump(&v[1], stdout); \
285 fputs("\n\texpected = ", stdout); \
286 type_hex.dump(&v[2], stdout); \
287 fputs("\n\tcomputed = ", stdout); \
288 type_hex.dump(&d, stdout); \
298 static test_chunk macdefs[] = { \
299 { #pre "-hmac", macverify, \
300 { &type_string, &type_hex, &type_hex, 0 } }, \
304 int main(int argc, char *argv[]) \
307 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
312 # define HMAC_TEST(PRE, pre)
315 /*----- That's all, folks -------------------------------------------------*/