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