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