Support HMAC mode for hash functions which need to store more state than
[u/mdw/catacomb] / hmac-def.h
1 /* -*-c-*-
2 *
3 * $Id: hmac-def.h,v 1.5 2000/10/15 19:09:20 mdw Exp $
4 *
5 * Definitions for HMAC and NMAC
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: hmac-def.h,v $
33 * Revision 1.5 2000/10/15 19:09:20 mdw
34 * Support HMAC mode for hash functions which need to store more state than
35 * the hash output size.
36 *
37 * Revision 1.4 2000/07/15 10:00:58 mdw
38 * New generic hash operation for copying hash contexts.
39 *
40 * Revision 1.3 2000/07/02 18:27:42 mdw
41 * (ghash->ops->done): Interface change. Passing in a null buffer pointer
42 * uses a buffer internal to the ghash object. The operation returns the
43 * address of the buffer it used. Clients of generic hashes no longer need
44 * to use dynamically allocated memory for hash results.
45 *
46 * Revision 1.2 2000/06/17 11:23:44 mdw
47 * Use secure arena for memory allocation. Minor changes in the generic
48 * hash interface.
49 *
50 * Revision 1.1 1999/12/10 23:16:40 mdw
51 * Split mode macros into interface and implementation.
52 *
53 */
54
55 #ifndef CATACOMB_HMAC_DEF_H
56 #define CATACOMB_HMAC_DEF_H
57
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61
62 /*----- Header files ------------------------------------------------------*/
63
64 #include <assert.h>
65 #include <stdlib.h>
66 #include <string.h>
67
68 #include <mLib/bits.h>
69 #include <mLib/sub.h>
70
71 #ifndef CATACOMB_ARENA_H
72 # include "arena.h"
73 #endif
74
75 #ifndef CATACOMB_GMAC_H
76 # include "gmac.h"
77 #endif
78
79 #ifndef CATACOMB_PARANOIA_H
80 # include "paranoia.h"
81 #endif
82
83 /*----- Macros ------------------------------------------------------------*/
84
85 /* --- @HMAC_DEF@ --- *
86 *
87 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
88 *
89 * Use: Creates implementations for the HMAC and NMAC functions.
90 */
91
92 #define HMAC_DEF(PRE, pre) \
93 \
94 /* --- Useful constants --- */ \
95 \
96 const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
97 \
98 /* --- @pre_nmacinit@ --- * \
99 * \
100 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
101 * @const void *ok@ = pointer to outer hash init vector \
102 * @const void *ik@ = pointer to inner hash init vector \
103 * \
104 * Returns: --- \
105 * \
106 * Use: Initializes a MAC key for doing NMAC hashing. \
107 */ \
108 \
109 void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
110 { \
111 memcpy(key->ochain, ok, PRE##_STATESZ); \
112 memcpy(key->ichain, ik, PRE##_STATESZ); \
113 key->ocount = key->icount = 0; \
114 } \
115 \
116 /* --- @pre_hmacinit@ --- * \
117 * \
118 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
119 * @const void *k@ = pointer to key to use \
120 * @size_t sz@ = size of key data \
121 * \
122 * Returns: --- \
123 * \
124 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
125 * longer than the hash function's output size aren't very \
126 * useful, but are accepted. Keys longer than the hash's \
127 * block size are also accepted; they are hashed before \
128 * use, as specified in RFC2104. \
129 */ \
130 \
131 void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \
132 { \
133 int i; \
134 const octet *kbuf = k; \
135 pre##_ctx ctx; \
136 octet buf[PRE##_HASHSZ]; \
137 \
138 if (sz > PRE##_BUFSZ) { \
139 pre##_init(&ctx); \
140 pre##_hash(&ctx, k, sz); \
141 pre##_done(&ctx, buf); \
142 kbuf = buf; \
143 sz = PRE##_HASHSZ; \
144 } \
145 \
146 pre##_init(&ctx); \
147 memset(ctx.buf, 0x5c, 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->ochain); \
152 \
153 pre##_init(&ctx); \
154 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
155 for (i = 0; i < sz; i++) \
156 ctx.buf[i] ^= kbuf[i]; \
157 pre##_compress(&ctx, ctx.buf); \
158 pre##_state(&ctx, key->ichain); \
159 \
160 key->ocount = key->icount = PRE##_BUFSZ; \
161 BURN(ctx); \
162 } \
163 \
164 /* --- @pre_macinit@ --- * \
165 * \
166 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
167 * @const pre_mackey *key@ = pointer to MAC key block \
168 * \
169 * Returns: --- \
170 * \
171 * Use: Instantiates a MAC context from a key block. \
172 */ \
173 \
174 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
175 { \
176 memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \
177 ctx->count = key->ocount; \
178 pre##_set(&ctx->ctx, key->ichain, key->icount); \
179 } \
180 \
181 /* --- @pre_machash@ --- * \
182 * \
183 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
184 * @const void *buf@ = pointer to buffer \
185 * @size_t sz@ = size of the buffer \
186 * \
187 * Returns: --- \
188 * \
189 * Use: Hashes a buffer. \
190 */ \
191 \
192 void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \
193 { \
194 pre##_hash(&ctx->ctx, buf, sz); \
195 } \
196 \
197 /* --- @pre_macdone@ --- * \
198 * \
199 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
200 * @void *mac@ = pointer to buffer to receive MAC \
201 * \
202 * Returns: --- \
203 * \
204 * Use: Returns the result of a MAC computation. \
205 */ \
206 \
207 void pre##_macdone(pre##_macctx *ctx, void *mac) \
208 { \
209 pre##_done(&ctx->ctx, mac); \
210 pre##_set(&ctx->ctx, ctx->chain, ctx->count); \
211 pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \
212 pre##_done(&ctx->ctx, mac); \
213 } \
214 \
215 /* --- Generic MAC interface --- */ \
216 \
217 static const gmac_ops gkops; \
218 static const ghash_ops gops; \
219 \
220 typedef struct gkctx { \
221 gmac m; \
222 pre##_mackey k; \
223 } gkctx; \
224 \
225 typedef struct gctx { \
226 ghash h; \
227 pre##_macctx c; \
228 octet buf[PRE##_HASHSZ]; \
229 } gctx; \
230 \
231 static ghash *gkinit(gmac *m) \
232 { \
233 gkctx *gk = (gkctx *)m; \
234 gctx *g = S_CREATE(gctx); \
235 g->h.ops = &gops; \
236 pre##_macinit(&g->c, &gk->k); \
237 return (&g->h); \
238 } \
239 \
240 static gmac *gkey(const void *k, size_t sz) \
241 { \
242 gkctx *gk = S_CREATE(gkctx); \
243 gk->m.ops = &gkops; \
244 pre##_hmacinit(&gk->k, k, sz); \
245 return (&gk->m); \
246 } \
247 \
248 static void ghhash(ghash *h, const void *p, size_t sz) \
249 { \
250 gctx *g = (gctx *)h; \
251 pre##_machash(&g->c, p, sz); \
252 } \
253 \
254 static octet *ghdone(ghash *h, void *buf) \
255 { \
256 gctx *g = (gctx *)h; \
257 if (!buf) \
258 buf = g->buf; \
259 pre##_macdone(&g->c, buf); \
260 return (buf); \
261 } \
262 \
263 static ghash *ghcopy(ghash *h) \
264 { \
265 gctx *g = (gctx *)h; \
266 gctx *gg = S_CREATE(gctx); \
267 memcpy(gg, g, sizeof(gctx)); \
268 return (&gg->h); \
269 } \
270 \
271 static void ghdestroy(ghash *h) \
272 { \
273 gctx *g = (gctx *)h; \
274 BURN(*g); \
275 S_DESTROY(g); \
276 } \
277 \
278 static void gkdestroy(gmac *m) \
279 { \
280 gkctx *gk = (gkctx *)m; \
281 BURN(*gk); \
282 S_DESTROY(gk); \
283 } \
284 \
285 static ghash *ghinit(void) \
286 { \
287 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
288 return (0); \
289 } \
290 \
291 const gcmac pre##_hmac = \
292 { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey }; \
293 static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
294 static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
295 static const ghash_ops gops = \
296 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
297 \
298 HMAC_TEST(PRE, pre)
299
300 /* --- @HMAC_TEST@ --- *
301 *
302 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
303 *
304 * Use: Standard test rig for MAC functions.
305 */
306
307 #ifdef TEST_RIG
308
309 #include <stdio.h>
310
311 #include <mLib/dstr.h>
312 #include <mLib/quis.h>
313 #include <mLib/testrig.h>
314
315 #define HMAC_TEST(PRE, pre) \
316 \
317 static int macverify(dstr *v) \
318 { \
319 pre##_macctx cctx; \
320 pre##_mackey ckey; \
321 int ok = 1; \
322 int i; \
323 octet *p; \
324 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
325 size_t csz; \
326 dstr d; \
327 \
328 dstr_create(&d); \
329 dstr_ensure(&d, PRE##_HASHSZ); \
330 d.len = PRE##_HASHSZ; \
331 \
332 pre##_hmacinit(&ckey, v[1].buf, v[1].len); \
333 \
334 for (ip = szs; *ip; ip++) { \
335 i = *ip; \
336 csz = v[0].len; \
337 if (i == -1) \
338 i = csz; \
339 if (i > csz) \
340 continue; \
341 p = (octet *)v[0].buf; \
342 pre##_macinit(&cctx, &ckey); \
343 while (csz) { \
344 if (i > csz) \
345 i = csz; \
346 pre##_machash(&cctx, p, i); \
347 p += i; \
348 csz -= i; \
349 } \
350 pre##_macdone(&cctx, d.buf); \
351 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
352 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
353 *ip, v[0].buf); \
354 type_hex.dump(&v[1], stdout); \
355 fputs("\n\texpected = ", stdout); \
356 type_hex.dump(&v[2], stdout); \
357 fputs("\n\tcomputed = ", stdout); \
358 type_hex.dump(&d, stdout); \
359 putchar('\n'); \
360 ok = 0; \
361 } \
362 } \
363 \
364 dstr_destroy(&d); \
365 return (ok); \
366 } \
367 \
368 static test_chunk macdefs[] = { \
369 { #pre "-hmac", macverify, \
370 { &type_string, &type_hex, &type_hex, 0 } }, \
371 { 0, 0, { 0 } } \
372 }; \
373 \
374 int main(int argc, char *argv[]) \
375 { \
376 ego(argv[0]); \
377 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
378 return (0); \
379 }
380
381 #else
382 # define HMAC_TEST(PRE, pre)
383 #endif
384
385 /*----- That's all, folks -------------------------------------------------*/
386
387 #ifdef __cplusplus
388 }
389 #endif
390
391 #endif