primeiter: New functions for iterating over small primes.
[u/mdw/catacomb] / hmac-def.h
CommitLineData
79ba130c 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: hmac-def.h,v 1.8 2004/04/08 01:36:15 mdw Exp $
79ba130c 4 *
5 * Definitions for HMAC and NMAC
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
79ba130c 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.
45c0fd36 18 *
79ba130c 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.
45c0fd36 23 *
79ba130c 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
79ba130c 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
28ef1f45 39#include <assert.h>
79ba130c 40#include <stdlib.h>
41#include <string.h>
42
43#include <mLib/bits.h>
44#include <mLib/sub.h>
45
28ef1f45 46#ifndef CATACOMB_ARENA_H
47# include "arena.h"
48#endif
49
79ba130c 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 \
28ef1f45 69/* --- Useful constants --- */ \
70 \
2a62e96d 71const octet pre##_hmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
72const octet pre##_sslmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
73const octet pre##_nmackeysz[] = { KSZ_SET, 2 * PRE##_STATESZ, 0 }; \
28ef1f45 74 \
79ba130c 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 \
86void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
87{ \
c850c0da 88 memcpy(key->ochain, ok, PRE##_STATESZ); \
89 memcpy(key->ichain, ik, PRE##_STATESZ); \
79ba130c 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 \
108void 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 \
36c67859 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 \
153void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \
154{ \
36c67859 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 \
79ba130c 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 \
193void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
194{ \
c850c0da 195 memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \
79ba130c 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 \
211void 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 \
226void 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 \
236static const gmac_ops gkops; \
2a62e96d 237static const ghash_ops gops, gnops, gsslops; \
79ba130c 238 \
239typedef struct gkctx { \
240 gmac m; \
2a62e96d 241 const ghash_ops *gops; \
79ba130c 242 pre##_mackey k; \
243} gkctx; \
244 \
245typedef struct gctx { \
246 ghash h; \
247 pre##_macctx c; \
a351d052 248 octet buf[PRE##_HASHSZ]; \
79ba130c 249} gctx; \
250 \
251static ghash *gkinit(gmac *m) \
252{ \
253 gkctx *gk = (gkctx *)m; \
28ef1f45 254 gctx *g = S_CREATE(gctx); \
2a62e96d 255 g->h.ops = gk->gops; \
79ba130c 256 pre##_macinit(&g->c, &gk->k); \
257 return (&g->h); \
258} \
259 \
260static gmac *gkey(const void *k, size_t sz) \
261{ \
28ef1f45 262 gkctx *gk = S_CREATE(gkctx); \
79ba130c 263 gk->m.ops = &gkops; \
2a62e96d 264 gk->gops = &gops; \
79ba130c 265 pre##_hmacinit(&gk->k, k, sz); \
266 return (&gk->m); \
267} \
268 \
2a62e96d 269static 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 \
36c67859 280static gmac *gsslkey(const void *k, size_t sz) \
281{ \
282 gkctx *gk = S_CREATE(gkctx); \
283 gk->m.ops = &gkops; \
2a62e96d 284 gk->gops = &gsslops; \
36c67859 285 pre##_sslmacinit(&gk->k, k, sz); \
286 return (&gk->m); \
287} \
288 \
79ba130c 289static 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 \
a351d052 295static octet *ghdone(ghash *h, void *buf) \
79ba130c 296{ \
297 gctx *g = (gctx *)h; \
a351d052 298 if (!buf) \
299 buf = g->buf; \
79ba130c 300 pre##_macdone(&g->c, buf); \
a351d052 301 return (buf); \
79ba130c 302} \
303 \
d2fdbc2c 304static 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 \
79ba130c 312static void ghdestroy(ghash *h) \
313{ \
314 gctx *g = (gctx *)h; \
28ef1f45 315 BURN(*g); \
316 S_DESTROY(g); \
79ba130c 317} \
318 \
319static void gkdestroy(gmac *m) \
320{ \
321 gkctx *gk = (gkctx *)m; \
28ef1f45 322 BURN(*gk); \
323 S_DESTROY(gk); \
324} \
325 \
326static ghash *ghinit(void) \
327{ \
328 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
329 return (0); \
79ba130c 330} \
331 \
2a62e96d 332const gcmac pre##_nmac = \
333 { #pre "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey }; \
28ef1f45 334const gcmac pre##_hmac = \
2a62e96d 335 { #pre "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey }; \
36c67859 336const gcmac pre##_sslmac = \
2a62e96d 337 { #pre "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey }; \
28ef1f45 338static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
2a62e96d 339static const gmac_ops gnkops = { &pre##_nmac, gkinit, gkdestroy }; \
340static const gmac_ops gsslkops = { &pre##_sslmac, gkinit, gkdestroy }; \
28ef1f45 341static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
79ba130c 342static const ghash_ops gops = \
d2fdbc2c 343 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
2a62e96d 344static const gchash gnch = { #pre "-nmac", PRE##_HASHSZ, ghinit }; \
345static const ghash_ops gnops = \
346 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
347static const gchash gsslch = { #pre "-sslmac", PRE##_HASHSZ, ghinit }; \
348static const ghash_ops gsslops = \
349 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
79ba130c 350 \
351HMAC_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 \
370static 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 \
421static test_chunk macdefs[] = { \
422 { #pre "-hmac", macverify, \
423 { &type_string, &type_hex, &type_hex, 0 } }, \
424 { 0, 0, { 0 } } \
425}; \
426 \
427int 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