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