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