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