New multiprecision integer arithmetic suite.
[u/mdw/catacomb] / hmac.h
1 /* -*-c-*-
2 *
3 * $Id: hmac.h,v 1.1 1999/09/03 08:41:12 mdw Exp $
4 *
5 * Generic code for HMAC and NMAC
6 *
7 * (c) 1998 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.h,v $
33 * Revision 1.1 1999/09/03 08:41:12 mdw
34 * Initial import.
35 *
36 */
37
38 /*----- Notes on the HMAC and NMAC constructions --------------------------*
39 *
40 * Designed by Mihir Bellare, Ran Canetti and Hugo Krawczyk, NMAC is a
41 * method for constructing keyed message authentication algorithms from
42 * unkeyed hash functions. HMAC is an alternative formulation which doesn't
43 * require low-level access to the hash function's implementation. NMAC was
44 * designed to allow MD5 has a suitable underlying hash function, even though
45 * doubts were already being raised about its collision resistance.
46 */
47
48 #ifndef HMAC_H
49 #define HMAC_H
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 /*----- Header files ------------------------------------------------------*/
56
57 #include <stdlib.h>
58 #include <string.h>
59
60 #include <mLib/bits.h>
61
62 #ifndef PARANOIA_H
63 # include "paranoia.h"
64 #endif
65
66 /*----- Macros ------------------------------------------------------------*/
67
68 /* --- @HMAC_DECL@ --- *
69 *
70 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
71 *
72 * Use: Creates declarations for the HMAC and NMAC functions.
73 */
74
75 #define HMAC_DECL(PRE, pre) \
76 \
77 typedef struct pre##_mackey { \
78 octet ochain[PRE##_HASHSZ]; /* Chaining for outer hash */ \
79 unsigned long ocount; /* Byte count for outer hash */ \
80 octet ichain[PRE##_HASHSZ]; /* Chaining for inner hash */ \
81 unsigned long icount; /* Byte count for inner hash */ \
82 } pre##_mackey; \
83 \
84 typedef struct pre##_macctx { \
85 pre##_ctx ctx; /* Context for main hashing */ \
86 octet chain[PRE##_HASHSZ]; /* Chaining for outer hash */ \
87 unsigned long count; /* Byte count for outer hash */ \
88 } pre##_macctx; \
89 \
90 extern void pre##_nmac(pre##_mackey */*key*/, \
91 const void */*ok*/, const void */*ik*/); \
92 \
93 extern void pre##_hmac(pre##_mackey */*key*/, \
94 const void */*k*/, size_t /*sz*/); \
95 \
96 extern void pre##_macinit(pre##_macctx */*ctx*/, \
97 const pre##_mackey */*key*/); \
98 \
99 extern void pre##_mac(pre##_macctx */*ctx*/, \
100 const void */*buf*/, size_t /*sz*/); \
101 \
102 extern void pre##_macdone(pre##_macctx */*ctx*/, void */*mac*/);
103
104 /* --- @HMAC_DEF@ --- *
105 *
106 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
107 *
108 * Use: Creates implementations for the HMAC and NMAC functions.
109 */
110
111 #define HMAC_DEF(PRE, pre) \
112 \
113 /* --- @pre_nmac@ --- * \
114 * \
115 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
116 * @const void *ok@ = pointer to outer hash init vector \
117 * @const void *ik@ = pointer to inner hash init vector \
118 * \
119 * Returns: --- \
120 * \
121 * Use: Initializes a MAC key for doing NMAC hashing. \
122 */ \
123 \
124 void pre##_nmac(pre##_mackey *key, const void *ok, const void *ik) \
125 { \
126 memcpy(key->ochain, ok, PRE##_HASHSZ); \
127 memcpy(key->ichain, ik, PRE##_HASHSZ); \
128 key->ocount = key->icount = 0; \
129 } \
130 \
131 /* --- @pre_hmac@ --- * \
132 * \
133 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
134 * @const void *k@ = pointer to key to use \
135 * @size_t sz@ = size of key data \
136 * \
137 * Returns: --- \
138 * \
139 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
140 * longer than the hash function's output size aren't very \
141 * useful, but are accepted. Keys longer than the hash's \
142 * block size are also accepted; they are hashed before \
143 * use, as specified in RFC2104. \
144 */ \
145 \
146 void pre##_hmac(pre##_mackey *key, const void *k, size_t sz) \
147 { \
148 int i; \
149 const octet *kbuf = k; \
150 pre##_ctx ctx; \
151 octet buf[PRE##_HASHSZ]; \
152 \
153 if (sz > PRE##_BUFSZ) { \
154 pre##_init(&ctx); \
155 pre##_hash(&ctx, k, sz); \
156 pre##_done(&ctx, buf); \
157 kbuf = buf; \
158 sz = PRE##_HASHSZ; \
159 } \
160 \
161 pre##_init(&ctx); \
162 memset(ctx.buf, 0x5c, PRE##_BUFSZ); \
163 for (i = 0; i < sz; i++) \
164 ctx.buf[i] ^= kbuf[i]; \
165 pre##_compress(&ctx, ctx.buf); \
166 pre##_state(&ctx, key->ochain); \
167 \
168 pre##_init(&ctx); \
169 memset(ctx.buf, 0x36, PRE##_BUFSZ); \
170 for (i = 0; i < sz; i++) \
171 ctx.buf[i] ^= kbuf[i]; \
172 pre##_compress(&ctx, ctx.buf); \
173 pre##_state(&ctx, key->ichain); \
174 \
175 key->ocount = key->icount = PRE##_BUFSZ; \
176 BURN(ctx); \
177 } \
178 \
179 /* --- @pre_macinit@ --- * \
180 * \
181 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
182 * @const pre_mackey *key@ = pointer to MAC key block \
183 * \
184 * Returns: --- \
185 * \
186 * Use: Instantiates a MAC context from a key block. \
187 */ \
188 \
189 void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \
190 { \
191 memcpy(ctx->chain, key->ochain, PRE##_HASHSZ); \
192 ctx->count = key->ocount; \
193 pre##_set(&ctx->ctx, key->ichain, key->icount); \
194 } \
195 \
196 /* --- @pre_mac@ --- * \
197 * \
198 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
199 * @const void *buf@ = pointer to buffer \
200 * @size_t sz@ = size of the buffer \
201 * \
202 * Returns: --- \
203 * \
204 * Use: Hashes a buffer. \
205 */ \
206 \
207 void pre##_mac(pre##_macctx *ctx, const void *buf, size_t sz) \
208 { \
209 pre##_hash(&ctx->ctx, buf, sz); \
210 } \
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 HMAC_TEST(PRE, pre) \
231
232 /* --- @HMAC_TEST@ --- *
233 *
234 * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions
235 *
236 * Use: Standard test rig for MAC functions.
237 */
238
239 #ifdef TEST_RIG
240
241 #include <mLib/dstr.h>
242 #include <mLib/quis.h>
243 #include <mLib/testrig.h>
244
245 #define HMAC_TEST(PRE, pre) \
246 \
247 static int macverify(dstr *v) \
248 { \
249 pre##_macctx cctx; \
250 pre##_mackey ckey; \
251 int ok = 1; \
252 int i; \
253 octet *p; \
254 int szs[] = { 1, 7, 192, -1, 0 }, *ip; \
255 size_t csz; \
256 dstr d; \
257 \
258 dstr_create(&d); \
259 dstr_ensure(&d, PRE##_HASHSZ); \
260 d.len = PRE##_HASHSZ; \
261 \
262 pre##_hmac(&ckey, v[1].buf, v[1].len); \
263 \
264 for (ip = szs; *ip; ip++) { \
265 i = *ip; \
266 csz = v[0].len; \
267 if (i == -1) \
268 i = csz; \
269 if (i > csz) \
270 continue; \
271 p = (octet *)v[0].buf; \
272 pre##_macinit(&cctx, &ckey); \
273 while (csz) { \
274 if (i > csz) \
275 i = csz; \
276 pre##_mac(&cctx, p, i); \
277 p += i; \
278 csz -= i; \
279 } \
280 pre##_macdone(&cctx, d.buf); \
281 if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \
282 printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \
283 *ip, v[0].buf); \
284 type_hex.dump(&v[1], stdout); \
285 fputs("\n\texpected = ", stdout); \
286 type_hex.dump(&v[2], stdout); \
287 fputs("\n\tcomputed = ", stdout); \
288 type_hex.dump(&d, stdout); \
289 putchar('\n'); \
290 ok = 0; \
291 } \
292 } \
293 \
294 dstr_destroy(&d); \
295 return (ok); \
296 } \
297 \
298 static test_chunk macdefs[] = { \
299 { #pre "-hmac", macverify, \
300 { &type_string, &type_hex, &type_hex, 0 } }, \
301 { 0, 0, { 0 } } \
302 }; \
303 \
304 int main(int argc, char *argv[]) \
305 { \
306 ego(argv[0]); \
307 test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \
308 return (0); \
309 }
310
311 #else
312 # define HMAC_TEST(PRE, pre)
313 #endif
314
315 /*----- That's all, folks -------------------------------------------------*/
316
317 #ifdef __cplusplus
318 }
319 #endif
320
321 #endif