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