math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / symm / hmac-def.h
CommitLineData
79ba130c 1/* -*-c-*-
2 *
79ba130c 3 * Definitions for HMAC and NMAC
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
79ba130c 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.
45c0fd36 16 *
79ba130c 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.
45c0fd36 21 *
79ba130c 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
79ba130c 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
28ef1f45 37#include <assert.h>
79ba130c 38#include <stdlib.h>
39#include <string.h>
40
41#include <mLib/bits.h>
42#include <mLib/sub.h>
43
28ef1f45 44#ifndef CATACOMB_ARENA_H
45# include "arena.h"
46#endif
47
79ba130c 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 \
28ef1f45 67/* --- Useful constants --- */ \
68 \
2a62e96d 69const octet pre##_hmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
70const octet pre##_sslmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \
71const octet pre##_nmackeysz[] = { KSZ_SET, 2 * PRE##_STATESZ, 0 }; \
28ef1f45 72 \
79ba130c 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 \
84void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \
85{ \
c850c0da 86 memcpy(key->ochain, ok, PRE##_STATESZ); \
87 memcpy(key->ichain, ik, PRE##_STATESZ); \
79ba130c 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 \
106void 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 \
36c67859 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 \
151void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \
152{ \
36c67859 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 \
79ba130c 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 \
191void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
192{ \
c850c0da 193 memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \
79ba130c 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 \
209void 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 \
224void 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 \
234static const gmac_ops gkops; \
2a62e96d 235static const ghash_ops gops, gnops, gsslops; \
79ba130c 236 \
237typedef struct gkctx { \
238 gmac m; \
2a62e96d 239 const ghash_ops *gops; \
79ba130c 240 pre##_mackey k; \
241} gkctx; \
242 \
243typedef struct gctx { \
244 ghash h; \
245 pre##_macctx c; \
a351d052 246 octet buf[PRE##_HASHSZ]; \
79ba130c 247} gctx; \
248 \
249static ghash *gkinit(gmac *m) \
250{ \
251 gkctx *gk = (gkctx *)m; \
28ef1f45 252 gctx *g = S_CREATE(gctx); \
2a62e96d 253 g->h.ops = gk->gops; \
79ba130c 254 pre##_macinit(&g->c, &gk->k); \
255 return (&g->h); \
256} \
257 \
258static gmac *gkey(const void *k, size_t sz) \
259{ \
28ef1f45 260 gkctx *gk = S_CREATE(gkctx); \
79ba130c 261 gk->m.ops = &gkops; \
2a62e96d 262 gk->gops = &gops; \
79ba130c 263 pre##_hmacinit(&gk->k, k, sz); \
264 return (&gk->m); \
265} \
266 \
2a62e96d 267static 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 \
36c67859 278static gmac *gsslkey(const void *k, size_t sz) \
279{ \
280 gkctx *gk = S_CREATE(gkctx); \
281 gk->m.ops = &gkops; \
2a62e96d 282 gk->gops = &gsslops; \
36c67859 283 pre##_sslmacinit(&gk->k, k, sz); \
284 return (&gk->m); \
285} \
286 \
79ba130c 287static 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 \
a351d052 293static octet *ghdone(ghash *h, void *buf) \
79ba130c 294{ \
295 gctx *g = (gctx *)h; \
a351d052 296 if (!buf) \
297 buf = g->buf; \
79ba130c 298 pre##_macdone(&g->c, buf); \
a351d052 299 return (buf); \
79ba130c 300} \
301 \
d2fdbc2c 302static 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 \
79ba130c 310static void ghdestroy(ghash *h) \
311{ \
312 gctx *g = (gctx *)h; \
28ef1f45 313 BURN(*g); \
314 S_DESTROY(g); \
79ba130c 315} \
316 \
317static void gkdestroy(gmac *m) \
318{ \
319 gkctx *gk = (gkctx *)m; \
28ef1f45 320 BURN(*gk); \
321 S_DESTROY(gk); \
322} \
323 \
324static ghash *ghinit(void) \
325{ \
326 assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \
327 return (0); \
79ba130c 328} \
329 \
2a62e96d 330const gcmac pre##_nmac = \
331 { #pre "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey }; \
28ef1f45 332const gcmac pre##_hmac = \
2a62e96d 333 { #pre "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey }; \
36c67859 334const gcmac pre##_sslmac = \
2a62e96d 335 { #pre "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey }; \
28ef1f45 336static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \
2a62e96d 337static const gmac_ops gnkops = { &pre##_nmac, gkinit, gkdestroy }; \
338static const gmac_ops gsslkops = { &pre##_sslmac, gkinit, gkdestroy }; \
28ef1f45 339static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \
79ba130c 340static const ghash_ops gops = \
d2fdbc2c 341 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
2a62e96d 342static const gchash gnch = { #pre "-nmac", PRE##_HASHSZ, ghinit }; \
343static const ghash_ops gnops = \
344 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
345static const gchash gsslch = { #pre "-sslmac", PRE##_HASHSZ, ghinit }; \
346static const ghash_ops gsslops = \
347 { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \
79ba130c 348 \
349HMAC_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 \
368static 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 \
419static test_chunk macdefs[] = { \
420 { #pre "-hmac", macverify, \
421 { &type_string, &type_hex, &type_hex, 0 } }, \
422 { 0, 0, { 0 } } \
423}; \
424 \
425int main(int argc, char *argv[]) \
426{ \
427 ego(argv[0]); \
0f00dc4c 428 test_run(argc, argv, macdefs, SRCDIR"/t/" #pre); \
79ba130c 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