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