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