79ba130c |
1 | /* -*-c-*- |
2 | * |
28ef1f45 |
3 | * $Id: hmac-def.h,v 1.2 2000/06/17 11:23:44 mdw Exp $ |
79ba130c |
4 | * |
5 | * Definitions for HMAC and NMAC |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
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. |
18 | * |
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. |
23 | * |
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 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: hmac-def.h,v $ |
28ef1f45 |
33 | * Revision 1.2 2000/06/17 11:23:44 mdw |
34 | * Use secure arena for memory allocation. Minor changes in the generic |
35 | * hash interface. |
36 | * |
79ba130c |
37 | * Revision 1.1 1999/12/10 23:16:40 mdw |
38 | * Split mode macros into interface and implementation. |
39 | * |
40 | */ |
41 | |
42 | #ifndef CATACOMB_HMAC_DEF_H |
43 | #define CATACOMB_HMAC_DEF_H |
44 | |
45 | #ifdef __cplusplus |
46 | extern "C" { |
47 | #endif |
48 | |
49 | /*----- Header files ------------------------------------------------------*/ |
50 | |
28ef1f45 |
51 | #include <assert.h> |
79ba130c |
52 | #include <stdlib.h> |
53 | #include <string.h> |
54 | |
55 | #include <mLib/bits.h> |
56 | #include <mLib/sub.h> |
57 | |
28ef1f45 |
58 | #ifndef CATACOMB_ARENA_H |
59 | # include "arena.h" |
60 | #endif |
61 | |
79ba130c |
62 | #ifndef CATACOMB_GMAC_H |
63 | # include "gmac.h" |
64 | #endif |
65 | |
66 | #ifndef CATACOMB_PARANOIA_H |
67 | # include "paranoia.h" |
68 | #endif |
69 | |
70 | /*----- Macros ------------------------------------------------------------*/ |
71 | |
72 | /* --- @HMAC_DEF@ --- * |
73 | * |
74 | * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function |
75 | * |
76 | * Use: Creates implementations for the HMAC and NMAC functions. |
77 | */ |
78 | |
79 | #define HMAC_DEF(PRE, pre) \ |
80 | \ |
28ef1f45 |
81 | /* --- Useful constants --- */ \ |
82 | \ |
83 | const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ }; \ |
84 | \ |
79ba130c |
85 | /* --- @pre_nmacinit@ --- * \ |
86 | * \ |
87 | * Arguments: @pre_macctx *key@ = pointer to a MAC key object \ |
88 | * @const void *ok@ = pointer to outer hash init vector \ |
89 | * @const void *ik@ = pointer to inner hash init vector \ |
90 | * \ |
91 | * Returns: --- \ |
92 | * \ |
93 | * Use: Initializes a MAC key for doing NMAC hashing. \ |
94 | */ \ |
95 | \ |
96 | void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \ |
97 | { \ |
98 | memcpy(key->ochain, ok, PRE##_HASHSZ); \ |
99 | memcpy(key->ichain, ik, PRE##_HASHSZ); \ |
100 | key->ocount = key->icount = 0; \ |
101 | } \ |
102 | \ |
103 | /* --- @pre_hmacinit@ --- * \ |
104 | * \ |
105 | * Arguments: @pre_mackey *key@ = pointer to MAC key object \ |
106 | * @const void *k@ = pointer to key to use \ |
107 | * @size_t sz@ = size of key data \ |
108 | * \ |
109 | * Returns: --- \ |
110 | * \ |
111 | * Use: Initializes a MAC key for doing HMAC hashing. Keys \ |
112 | * longer than the hash function's output size aren't very \ |
113 | * useful, but are accepted. Keys longer than the hash's \ |
114 | * block size are also accepted; they are hashed before \ |
115 | * use, as specified in RFC2104. \ |
116 | */ \ |
117 | \ |
118 | void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \ |
119 | { \ |
120 | int i; \ |
121 | const octet *kbuf = k; \ |
122 | pre##_ctx ctx; \ |
123 | octet buf[PRE##_HASHSZ]; \ |
124 | \ |
125 | if (sz > PRE##_BUFSZ) { \ |
126 | pre##_init(&ctx); \ |
127 | pre##_hash(&ctx, k, sz); \ |
128 | pre##_done(&ctx, buf); \ |
129 | kbuf = buf; \ |
130 | sz = PRE##_HASHSZ; \ |
131 | } \ |
132 | \ |
133 | pre##_init(&ctx); \ |
134 | memset(ctx.buf, 0x5c, PRE##_BUFSZ); \ |
135 | for (i = 0; i < sz; i++) \ |
136 | ctx.buf[i] ^= kbuf[i]; \ |
137 | pre##_compress(&ctx, ctx.buf); \ |
138 | pre##_state(&ctx, key->ochain); \ |
139 | \ |
140 | pre##_init(&ctx); \ |
141 | memset(ctx.buf, 0x36, PRE##_BUFSZ); \ |
142 | for (i = 0; i < sz; i++) \ |
143 | ctx.buf[i] ^= kbuf[i]; \ |
144 | pre##_compress(&ctx, ctx.buf); \ |
145 | pre##_state(&ctx, key->ichain); \ |
146 | \ |
147 | key->ocount = key->icount = PRE##_BUFSZ; \ |
148 | BURN(ctx); \ |
149 | } \ |
150 | \ |
151 | /* --- @pre_macinit@ --- * \ |
152 | * \ |
153 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
154 | * @const pre_mackey *key@ = pointer to MAC key block \ |
155 | * \ |
156 | * Returns: --- \ |
157 | * \ |
158 | * Use: Instantiates a MAC context from a key block. \ |
159 | */ \ |
160 | \ |
161 | void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \ |
162 | { \ |
163 | memcpy(ctx->chain, key->ochain, PRE##_HASHSZ); \ |
164 | ctx->count = key->ocount; \ |
165 | pre##_set(&ctx->ctx, key->ichain, key->icount); \ |
166 | } \ |
167 | \ |
168 | /* --- @pre_machash@ --- * \ |
169 | * \ |
170 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
171 | * @const void *buf@ = pointer to buffer \ |
172 | * @size_t sz@ = size of the buffer \ |
173 | * \ |
174 | * Returns: --- \ |
175 | * \ |
176 | * Use: Hashes a buffer. \ |
177 | */ \ |
178 | \ |
179 | void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \ |
180 | { \ |
181 | pre##_hash(&ctx->ctx, buf, sz); \ |
182 | } \ |
183 | \ |
184 | /* --- @pre_macdone@ --- * \ |
185 | * \ |
186 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
187 | * @void *mac@ = pointer to buffer to receive MAC \ |
188 | * \ |
189 | * Returns: --- \ |
190 | * \ |
191 | * Use: Returns the result of a MAC computation. \ |
192 | */ \ |
193 | \ |
194 | void pre##_macdone(pre##_macctx *ctx, void *mac) \ |
195 | { \ |
196 | pre##_done(&ctx->ctx, mac); \ |
197 | pre##_set(&ctx->ctx, ctx->chain, ctx->count); \ |
198 | pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \ |
199 | pre##_done(&ctx->ctx, mac); \ |
200 | } \ |
201 | \ |
202 | /* --- Generic MAC interface --- */ \ |
203 | \ |
204 | static const gmac_ops gkops; \ |
205 | static const ghash_ops gops; \ |
206 | \ |
207 | typedef struct gkctx { \ |
208 | gmac m; \ |
209 | pre##_mackey k; \ |
210 | } gkctx; \ |
211 | \ |
212 | typedef struct gctx { \ |
213 | ghash h; \ |
214 | pre##_macctx c; \ |
215 | } gctx; \ |
216 | \ |
217 | static ghash *gkinit(gmac *m) \ |
218 | { \ |
219 | gkctx *gk = (gkctx *)m; \ |
28ef1f45 |
220 | gctx *g = S_CREATE(gctx); \ |
79ba130c |
221 | g->h.ops = &gops; \ |
222 | pre##_macinit(&g->c, &gk->k); \ |
223 | return (&g->h); \ |
224 | } \ |
225 | \ |
226 | static gmac *gkey(const void *k, size_t sz) \ |
227 | { \ |
28ef1f45 |
228 | gkctx *gk = S_CREATE(gkctx); \ |
79ba130c |
229 | gk->m.ops = &gkops; \ |
230 | pre##_hmacinit(&gk->k, k, sz); \ |
231 | return (&gk->m); \ |
232 | } \ |
233 | \ |
234 | static void ghhash(ghash *h, const void *p, size_t sz) \ |
235 | { \ |
236 | gctx *g = (gctx *)h; \ |
237 | pre##_machash(&g->c, p, sz); \ |
238 | } \ |
239 | \ |
240 | static void ghdone(ghash *h, void *buf) \ |
241 | { \ |
242 | gctx *g = (gctx *)h; \ |
243 | pre##_macdone(&g->c, buf); \ |
244 | } \ |
245 | \ |
246 | static void ghdestroy(ghash *h) \ |
247 | { \ |
248 | gctx *g = (gctx *)h; \ |
28ef1f45 |
249 | BURN(*g); \ |
250 | S_DESTROY(g); \ |
79ba130c |
251 | } \ |
252 | \ |
253 | static void gkdestroy(gmac *m) \ |
254 | { \ |
255 | gkctx *gk = (gkctx *)m; \ |
28ef1f45 |
256 | BURN(*gk); \ |
257 | S_DESTROY(gk); \ |
258 | } \ |
259 | \ |
260 | static ghash *ghinit(void) \ |
261 | { \ |
262 | assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \ |
263 | return (0); \ |
79ba130c |
264 | } \ |
265 | \ |
28ef1f45 |
266 | const gcmac pre##_hmac = \ |
267 | { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey }; \ |
268 | static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \ |
269 | static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \ |
79ba130c |
270 | static const ghash_ops gops = \ |
28ef1f45 |
271 | { &gch, ghhash, ghdone, ghdestroy }; \ |
79ba130c |
272 | \ |
273 | HMAC_TEST(PRE, pre) |
274 | |
275 | /* --- @HMAC_TEST@ --- * |
276 | * |
277 | * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions |
278 | * |
279 | * Use: Standard test rig for MAC functions. |
280 | */ |
281 | |
282 | #ifdef TEST_RIG |
283 | |
284 | #include <stdio.h> |
285 | |
286 | #include <mLib/dstr.h> |
287 | #include <mLib/quis.h> |
288 | #include <mLib/testrig.h> |
289 | |
290 | #define HMAC_TEST(PRE, pre) \ |
291 | \ |
292 | static int macverify(dstr *v) \ |
293 | { \ |
294 | pre##_macctx cctx; \ |
295 | pre##_mackey ckey; \ |
296 | int ok = 1; \ |
297 | int i; \ |
298 | octet *p; \ |
299 | int szs[] = { 1, 7, 192, -1, 0 }, *ip; \ |
300 | size_t csz; \ |
301 | dstr d; \ |
302 | \ |
303 | dstr_create(&d); \ |
304 | dstr_ensure(&d, PRE##_HASHSZ); \ |
305 | d.len = PRE##_HASHSZ; \ |
306 | \ |
307 | pre##_hmacinit(&ckey, v[1].buf, v[1].len); \ |
308 | \ |
309 | for (ip = szs; *ip; ip++) { \ |
310 | i = *ip; \ |
311 | csz = v[0].len; \ |
312 | if (i == -1) \ |
313 | i = csz; \ |
314 | if (i > csz) \ |
315 | continue; \ |
316 | p = (octet *)v[0].buf; \ |
317 | pre##_macinit(&cctx, &ckey); \ |
318 | while (csz) { \ |
319 | if (i > csz) \ |
320 | i = csz; \ |
321 | pre##_machash(&cctx, p, i); \ |
322 | p += i; \ |
323 | csz -= i; \ |
324 | } \ |
325 | pre##_macdone(&cctx, d.buf); \ |
326 | if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \ |
327 | printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \ |
328 | *ip, v[0].buf); \ |
329 | type_hex.dump(&v[1], stdout); \ |
330 | fputs("\n\texpected = ", stdout); \ |
331 | type_hex.dump(&v[2], stdout); \ |
332 | fputs("\n\tcomputed = ", stdout); \ |
333 | type_hex.dump(&d, stdout); \ |
334 | putchar('\n'); \ |
335 | ok = 0; \ |
336 | } \ |
337 | } \ |
338 | \ |
339 | dstr_destroy(&d); \ |
340 | return (ok); \ |
341 | } \ |
342 | \ |
343 | static test_chunk macdefs[] = { \ |
344 | { #pre "-hmac", macverify, \ |
345 | { &type_string, &type_hex, &type_hex, 0 } }, \ |
346 | { 0, 0, { 0 } } \ |
347 | }; \ |
348 | \ |
349 | int main(int argc, char *argv[]) \ |
350 | { \ |
351 | ego(argv[0]); \ |
352 | test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \ |
353 | return (0); \ |
354 | } |
355 | |
356 | #else |
357 | # define HMAC_TEST(PRE, pre) |
358 | #endif |
359 | |
360 | /*----- That's all, folks -------------------------------------------------*/ |
361 | |
362 | #ifdef __cplusplus |
363 | } |
364 | #endif |
365 | |
366 | #endif |