| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: hmac-def.h,v 1.7 2001/04/19 18:24:45 mdw Exp $ |
| 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 $ |
| 33 | * Revision 1.7 2001/04/19 18:24:45 mdw |
| 34 | * Provide correct key sizes for NMAC, HMAC and SSLMAC. |
| 35 | * |
| 36 | * Revision 1.6 2001/04/03 19:35:45 mdw |
| 37 | * Support the SSL HMAC variant (untested). |
| 38 | * |
| 39 | * Revision 1.5 2000/10/15 19:09:20 mdw |
| 40 | * Support HMAC mode for hash functions which need to store more state than |
| 41 | * the hash output size. |
| 42 | * |
| 43 | * Revision 1.4 2000/07/15 10:00:58 mdw |
| 44 | * New generic hash operation for copying hash contexts. |
| 45 | * |
| 46 | * Revision 1.3 2000/07/02 18:27:42 mdw |
| 47 | * (ghash->ops->done): Interface change. Passing in a null buffer pointer |
| 48 | * uses a buffer internal to the ghash object. The operation returns the |
| 49 | * address of the buffer it used. Clients of generic hashes no longer need |
| 50 | * to use dynamically allocated memory for hash results. |
| 51 | * |
| 52 | * Revision 1.2 2000/06/17 11:23:44 mdw |
| 53 | * Use secure arena for memory allocation. Minor changes in the generic |
| 54 | * hash interface. |
| 55 | * |
| 56 | * Revision 1.1 1999/12/10 23:16:40 mdw |
| 57 | * Split mode macros into interface and implementation. |
| 58 | * |
| 59 | */ |
| 60 | |
| 61 | #ifndef CATACOMB_HMAC_DEF_H |
| 62 | #define CATACOMB_HMAC_DEF_H |
| 63 | |
| 64 | #ifdef __cplusplus |
| 65 | extern "C" { |
| 66 | #endif |
| 67 | |
| 68 | /*----- Header files ------------------------------------------------------*/ |
| 69 | |
| 70 | #include <assert.h> |
| 71 | #include <stdlib.h> |
| 72 | #include <string.h> |
| 73 | |
| 74 | #include <mLib/bits.h> |
| 75 | #include <mLib/sub.h> |
| 76 | |
| 77 | #ifndef CATACOMB_ARENA_H |
| 78 | # include "arena.h" |
| 79 | #endif |
| 80 | |
| 81 | #ifndef CATACOMB_GMAC_H |
| 82 | # include "gmac.h" |
| 83 | #endif |
| 84 | |
| 85 | #ifndef CATACOMB_PARANOIA_H |
| 86 | # include "paranoia.h" |
| 87 | #endif |
| 88 | |
| 89 | /*----- Macros ------------------------------------------------------------*/ |
| 90 | |
| 91 | /* --- @HMAC_DEF@ --- * |
| 92 | * |
| 93 | * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function |
| 94 | * |
| 95 | * Use: Creates implementations for the HMAC and NMAC functions. |
| 96 | */ |
| 97 | |
| 98 | #define HMAC_DEF(PRE, pre) \ |
| 99 | \ |
| 100 | /* --- Useful constants --- */ \ |
| 101 | \ |
| 102 | const octet pre##_hmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \ |
| 103 | const octet pre##_sslmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \ |
| 104 | const octet pre##_nmackeysz[] = { KSZ_SET, 2 * PRE##_STATESZ, 0 }; \ |
| 105 | \ |
| 106 | /* --- @pre_nmacinit@ --- * \ |
| 107 | * \ |
| 108 | * Arguments: @pre_macctx *key@ = pointer to a MAC key object \ |
| 109 | * @const void *ok@ = pointer to outer hash init vector \ |
| 110 | * @const void *ik@ = pointer to inner hash init vector \ |
| 111 | * \ |
| 112 | * Returns: --- \ |
| 113 | * \ |
| 114 | * Use: Initializes a MAC key for doing NMAC hashing. \ |
| 115 | */ \ |
| 116 | \ |
| 117 | void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \ |
| 118 | { \ |
| 119 | memcpy(key->ochain, ok, PRE##_STATESZ); \ |
| 120 | memcpy(key->ichain, ik, PRE##_STATESZ); \ |
| 121 | key->ocount = key->icount = 0; \ |
| 122 | } \ |
| 123 | \ |
| 124 | /* --- @pre_hmacinit@ --- * \ |
| 125 | * \ |
| 126 | * Arguments: @pre_mackey *key@ = pointer to MAC key object \ |
| 127 | * @const void *k@ = pointer to key to use \ |
| 128 | * @size_t sz@ = size of key data \ |
| 129 | * \ |
| 130 | * Returns: --- \ |
| 131 | * \ |
| 132 | * Use: Initializes a MAC key for doing HMAC hashing. Keys \ |
| 133 | * longer than the hash function's output size aren't very \ |
| 134 | * useful, but are accepted. Keys longer than the hash's \ |
| 135 | * block size are also accepted; they are hashed before \ |
| 136 | * use, as specified in RFC2104. \ |
| 137 | */ \ |
| 138 | \ |
| 139 | void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \ |
| 140 | { \ |
| 141 | int i; \ |
| 142 | const octet *kbuf = k; \ |
| 143 | pre##_ctx ctx; \ |
| 144 | octet buf[PRE##_HASHSZ]; \ |
| 145 | \ |
| 146 | if (sz > PRE##_BUFSZ) { \ |
| 147 | pre##_init(&ctx); \ |
| 148 | pre##_hash(&ctx, k, sz); \ |
| 149 | pre##_done(&ctx, buf); \ |
| 150 | kbuf = buf; \ |
| 151 | sz = PRE##_HASHSZ; \ |
| 152 | } \ |
| 153 | \ |
| 154 | pre##_init(&ctx); \ |
| 155 | memset(ctx.buf, 0x5c, PRE##_BUFSZ); \ |
| 156 | for (i = 0; i < sz; i++) \ |
| 157 | ctx.buf[i] ^= kbuf[i]; \ |
| 158 | pre##_compress(&ctx, ctx.buf); \ |
| 159 | pre##_state(&ctx, key->ochain); \ |
| 160 | \ |
| 161 | pre##_init(&ctx); \ |
| 162 | memset(ctx.buf, 0x36, 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->ichain); \ |
| 167 | \ |
| 168 | key->ocount = key->icount = PRE##_BUFSZ; \ |
| 169 | BURN(ctx); \ |
| 170 | } \ |
| 171 | \ |
| 172 | /* --- @pre_sslmacinit@ --- * \ |
| 173 | * \ |
| 174 | * Arguments: @pre_mackey *key@ = pointer to MAC key object \ |
| 175 | * @const void *k@ = pointer to key to use \ |
| 176 | * @size_t sz@ = size of key data \ |
| 177 | * \ |
| 178 | * Returns: --- \ |
| 179 | * \ |
| 180 | * Use: Initializes a MAC key for doing hasing using the SSL3 \ |
| 181 | * variant of HMAC. \ |
| 182 | */ \ |
| 183 | \ |
| 184 | void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \ |
| 185 | { \ |
| 186 | const octet *kbuf = k; \ |
| 187 | pre##_ctx ctx; \ |
| 188 | octet buf[PRE##_HASHSZ]; \ |
| 189 | \ |
| 190 | if (sz > PRE##_BUFSZ) { \ |
| 191 | pre##_init(&ctx); \ |
| 192 | pre##_hash(&ctx, k, sz); \ |
| 193 | pre##_done(&ctx, buf); \ |
| 194 | kbuf = buf; \ |
| 195 | sz = PRE##_HASHSZ; \ |
| 196 | } \ |
| 197 | \ |
| 198 | pre##_init(&ctx); \ |
| 199 | memcpy(ctx.buf, kbuf, sz); \ |
| 200 | memset(ctx.buf + sz, 0x5c, PRE##_BUFSZ - sz); \ |
| 201 | pre##_compress(&ctx, ctx.buf); \ |
| 202 | pre##_state(&ctx, key->ochain); \ |
| 203 | \ |
| 204 | pre##_init(&ctx); \ |
| 205 | memcpy(ctx.buf, kbuf, sz); \ |
| 206 | memset(ctx.buf + sz, 0x36, PRE##_BUFSZ - sz); \ |
| 207 | pre##_compress(&ctx, ctx.buf); \ |
| 208 | pre##_state(&ctx, key->ichain); \ |
| 209 | \ |
| 210 | key->ocount = key->icount = PRE##_BUFSZ; \ |
| 211 | BURN(ctx); \ |
| 212 | } \ |
| 213 | \ |
| 214 | /* --- @pre_macinit@ --- * \ |
| 215 | * \ |
| 216 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
| 217 | * @const pre_mackey *key@ = pointer to MAC key block \ |
| 218 | * \ |
| 219 | * Returns: --- \ |
| 220 | * \ |
| 221 | * Use: Instantiates a MAC context from a key block. \ |
| 222 | */ \ |
| 223 | \ |
| 224 | void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \ |
| 225 | { \ |
| 226 | memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \ |
| 227 | ctx->count = key->ocount; \ |
| 228 | pre##_set(&ctx->ctx, key->ichain, key->icount); \ |
| 229 | } \ |
| 230 | \ |
| 231 | /* --- @pre_machash@ --- * \ |
| 232 | * \ |
| 233 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
| 234 | * @const void *buf@ = pointer to buffer \ |
| 235 | * @size_t sz@ = size of the buffer \ |
| 236 | * \ |
| 237 | * Returns: --- \ |
| 238 | * \ |
| 239 | * Use: Hashes a buffer. \ |
| 240 | */ \ |
| 241 | \ |
| 242 | void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \ |
| 243 | { \ |
| 244 | pre##_hash(&ctx->ctx, buf, sz); \ |
| 245 | } \ |
| 246 | \ |
| 247 | /* --- @pre_macdone@ --- * \ |
| 248 | * \ |
| 249 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
| 250 | * @void *mac@ = pointer to buffer to receive MAC \ |
| 251 | * \ |
| 252 | * Returns: --- \ |
| 253 | * \ |
| 254 | * Use: Returns the result of a MAC computation. \ |
| 255 | */ \ |
| 256 | \ |
| 257 | void pre##_macdone(pre##_macctx *ctx, void *mac) \ |
| 258 | { \ |
| 259 | pre##_done(&ctx->ctx, mac); \ |
| 260 | pre##_set(&ctx->ctx, ctx->chain, ctx->count); \ |
| 261 | pre##_hash(&ctx->ctx, mac, PRE##_HASHSZ); \ |
| 262 | pre##_done(&ctx->ctx, mac); \ |
| 263 | } \ |
| 264 | \ |
| 265 | /* --- Generic MAC interface --- */ \ |
| 266 | \ |
| 267 | static const gmac_ops gkops; \ |
| 268 | static const ghash_ops gops, gnops, gsslops; \ |
| 269 | \ |
| 270 | typedef struct gkctx { \ |
| 271 | gmac m; \ |
| 272 | const ghash_ops *gops; \ |
| 273 | pre##_mackey k; \ |
| 274 | } gkctx; \ |
| 275 | \ |
| 276 | typedef struct gctx { \ |
| 277 | ghash h; \ |
| 278 | pre##_macctx c; \ |
| 279 | octet buf[PRE##_HASHSZ]; \ |
| 280 | } gctx; \ |
| 281 | \ |
| 282 | static ghash *gkinit(gmac *m) \ |
| 283 | { \ |
| 284 | gkctx *gk = (gkctx *)m; \ |
| 285 | gctx *g = S_CREATE(gctx); \ |
| 286 | g->h.ops = gk->gops; \ |
| 287 | pre##_macinit(&g->c, &gk->k); \ |
| 288 | return (&g->h); \ |
| 289 | } \ |
| 290 | \ |
| 291 | static gmac *gkey(const void *k, size_t sz) \ |
| 292 | { \ |
| 293 | gkctx *gk = S_CREATE(gkctx); \ |
| 294 | gk->m.ops = &gkops; \ |
| 295 | gk->gops = &gops; \ |
| 296 | pre##_hmacinit(&gk->k, k, sz); \ |
| 297 | return (&gk->m); \ |
| 298 | } \ |
| 299 | \ |
| 300 | static gmac *gnkey(const void *k, size_t sz) \ |
| 301 | { \ |
| 302 | gkctx *gk = S_CREATE(gkctx); \ |
| 303 | const octet *kk = k; \ |
| 304 | assert(keysz(sz, pre##_nmackeysz) == sz); \ |
| 305 | gk->m.ops = &gkops; \ |
| 306 | gk->gops = &gnops; \ |
| 307 | pre##_nmacinit(&gk->k, kk, kk + PRE##_STATESZ); \ |
| 308 | return (&gk->m); \ |
| 309 | } \ |
| 310 | \ |
| 311 | static gmac *gsslkey(const void *k, size_t sz) \ |
| 312 | { \ |
| 313 | gkctx *gk = S_CREATE(gkctx); \ |
| 314 | gk->m.ops = &gkops; \ |
| 315 | gk->gops = &gsslops; \ |
| 316 | pre##_sslmacinit(&gk->k, k, sz); \ |
| 317 | return (&gk->m); \ |
| 318 | } \ |
| 319 | \ |
| 320 | static void ghhash(ghash *h, const void *p, size_t sz) \ |
| 321 | { \ |
| 322 | gctx *g = (gctx *)h; \ |
| 323 | pre##_machash(&g->c, p, sz); \ |
| 324 | } \ |
| 325 | \ |
| 326 | static octet *ghdone(ghash *h, void *buf) \ |
| 327 | { \ |
| 328 | gctx *g = (gctx *)h; \ |
| 329 | if (!buf) \ |
| 330 | buf = g->buf; \ |
| 331 | pre##_macdone(&g->c, buf); \ |
| 332 | return (buf); \ |
| 333 | } \ |
| 334 | \ |
| 335 | static ghash *ghcopy(ghash *h) \ |
| 336 | { \ |
| 337 | gctx *g = (gctx *)h; \ |
| 338 | gctx *gg = S_CREATE(gctx); \ |
| 339 | memcpy(gg, g, sizeof(gctx)); \ |
| 340 | return (&gg->h); \ |
| 341 | } \ |
| 342 | \ |
| 343 | static void ghdestroy(ghash *h) \ |
| 344 | { \ |
| 345 | gctx *g = (gctx *)h; \ |
| 346 | BURN(*g); \ |
| 347 | S_DESTROY(g); \ |
| 348 | } \ |
| 349 | \ |
| 350 | static void gkdestroy(gmac *m) \ |
| 351 | { \ |
| 352 | gkctx *gk = (gkctx *)m; \ |
| 353 | BURN(*gk); \ |
| 354 | S_DESTROY(gk); \ |
| 355 | } \ |
| 356 | \ |
| 357 | static ghash *ghinit(void) \ |
| 358 | { \ |
| 359 | assert(((void)"Attempt to instantiate an unkeyed MAC", 0)); \ |
| 360 | return (0); \ |
| 361 | } \ |
| 362 | \ |
| 363 | const gcmac pre##_nmac = \ |
| 364 | { #pre "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey }; \ |
| 365 | const gcmac pre##_hmac = \ |
| 366 | { #pre "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey }; \ |
| 367 | const gcmac pre##_sslmac = \ |
| 368 | { #pre "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey }; \ |
| 369 | static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \ |
| 370 | static const gmac_ops gnkops = { &pre##_nmac, gkinit, gkdestroy }; \ |
| 371 | static const gmac_ops gsslkops = { &pre##_sslmac, gkinit, gkdestroy }; \ |
| 372 | static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \ |
| 373 | static const ghash_ops gops = \ |
| 374 | { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \ |
| 375 | static const gchash gnch = { #pre "-nmac", PRE##_HASHSZ, ghinit }; \ |
| 376 | static const ghash_ops gnops = \ |
| 377 | { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \ |
| 378 | static const gchash gsslch = { #pre "-sslmac", PRE##_HASHSZ, ghinit }; \ |
| 379 | static const ghash_ops gsslops = \ |
| 380 | { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \ |
| 381 | \ |
| 382 | HMAC_TEST(PRE, pre) |
| 383 | |
| 384 | /* --- @HMAC_TEST@ --- * |
| 385 | * |
| 386 | * Arguments: @PRE@, @pre@ = prefixes for hash-specfic definitions |
| 387 | * |
| 388 | * Use: Standard test rig for MAC functions. |
| 389 | */ |
| 390 | |
| 391 | #ifdef TEST_RIG |
| 392 | |
| 393 | #include <stdio.h> |
| 394 | |
| 395 | #include <mLib/dstr.h> |
| 396 | #include <mLib/quis.h> |
| 397 | #include <mLib/testrig.h> |
| 398 | |
| 399 | #define HMAC_TEST(PRE, pre) \ |
| 400 | \ |
| 401 | static int macverify(dstr *v) \ |
| 402 | { \ |
| 403 | pre##_macctx cctx; \ |
| 404 | pre##_mackey ckey; \ |
| 405 | int ok = 1; \ |
| 406 | int i; \ |
| 407 | octet *p; \ |
| 408 | int szs[] = { 1, 7, 192, -1, 0 }, *ip; \ |
| 409 | size_t csz; \ |
| 410 | dstr d; \ |
| 411 | \ |
| 412 | dstr_create(&d); \ |
| 413 | dstr_ensure(&d, PRE##_HASHSZ); \ |
| 414 | d.len = PRE##_HASHSZ; \ |
| 415 | \ |
| 416 | pre##_hmacinit(&ckey, v[1].buf, v[1].len); \ |
| 417 | \ |
| 418 | for (ip = szs; *ip; ip++) { \ |
| 419 | i = *ip; \ |
| 420 | csz = v[0].len; \ |
| 421 | if (i == -1) \ |
| 422 | i = csz; \ |
| 423 | if (i > csz) \ |
| 424 | continue; \ |
| 425 | p = (octet *)v[0].buf; \ |
| 426 | pre##_macinit(&cctx, &ckey); \ |
| 427 | while (csz) { \ |
| 428 | if (i > csz) \ |
| 429 | i = csz; \ |
| 430 | pre##_machash(&cctx, p, i); \ |
| 431 | p += i; \ |
| 432 | csz -= i; \ |
| 433 | } \ |
| 434 | pre##_macdone(&cctx, d.buf); \ |
| 435 | if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \ |
| 436 | printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \ |
| 437 | *ip, v[0].buf); \ |
| 438 | type_hex.dump(&v[1], stdout); \ |
| 439 | fputs("\n\texpected = ", stdout); \ |
| 440 | type_hex.dump(&v[2], stdout); \ |
| 441 | fputs("\n\tcomputed = ", stdout); \ |
| 442 | type_hex.dump(&d, stdout); \ |
| 443 | putchar('\n'); \ |
| 444 | ok = 0; \ |
| 445 | } \ |
| 446 | } \ |
| 447 | \ |
| 448 | dstr_destroy(&d); \ |
| 449 | return (ok); \ |
| 450 | } \ |
| 451 | \ |
| 452 | static test_chunk macdefs[] = { \ |
| 453 | { #pre "-hmac", macverify, \ |
| 454 | { &type_string, &type_hex, &type_hex, 0 } }, \ |
| 455 | { 0, 0, { 0 } } \ |
| 456 | }; \ |
| 457 | \ |
| 458 | int main(int argc, char *argv[]) \ |
| 459 | { \ |
| 460 | ego(argv[0]); \ |
| 461 | test_run(argc, argv, macdefs, SRCDIR"/tests/" #pre); \ |
| 462 | return (0); \ |
| 463 | } |
| 464 | |
| 465 | #else |
| 466 | # define HMAC_TEST(PRE, pre) |
| 467 | #endif |
| 468 | |
| 469 | /*----- That's all, folks -------------------------------------------------*/ |
| 470 | |
| 471 | #ifdef __cplusplus |
| 472 | } |
| 473 | #endif |
| 474 | |
| 475 | #endif |