X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/a351d052dda03a2cc1af01dcef4640891f551ada..55fae6a7f7a59ef0b7c0d6065eb8499dd58295de:/hmac-def.h diff --git a/hmac-def.h b/hmac-def.h index 38dd64a..95d7562 100644 --- a/hmac-def.h +++ b/hmac-def.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: hmac-def.h,v 1.3 2000/07/02 18:27:42 mdw Exp $ + * $Id: hmac-def.h,v 1.6 2001/04/03 19:35:45 mdw Exp $ * * Definitions for HMAC and NMAC * @@ -30,6 +30,16 @@ /*----- Revision history --------------------------------------------------* * * $Log: hmac-def.h,v $ + * Revision 1.6 2001/04/03 19:35:45 mdw + * Support the SSL HMAC variant (untested). + * + * Revision 1.5 2000/10/15 19:09:20 mdw + * Support HMAC mode for hash functions which need to store more state than + * the hash output size. + * + * Revision 1.4 2000/07/15 10:00:58 mdw + * New generic hash operation for copying hash contexts. + * * Revision 1.3 2000/07/02 18:27:42 mdw * (ghash->ops->done): Interface change. Passing in a null buffer pointer * uses a buffer internal to the ghash object. The operation returns the @@ -86,7 +96,7 @@ \ /* --- Useful constants --- */ \ \ -const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ }; \ +const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \ \ /* --- @pre_nmacinit@ --- * \ * \ @@ -101,8 +111,8 @@ const octet pre##_mackeysz[] = { KSZ_ANY, PRE##_HASHSZ }; \ \ void pre##_nmacinit(pre##_mackey *key, const void *ok, const void *ik) \ { \ - memcpy(key->ochain, ok, PRE##_HASHSZ); \ - memcpy(key->ichain, ik, PRE##_HASHSZ); \ + memcpy(key->ochain, ok, PRE##_STATESZ); \ + memcpy(key->ichain, ik, PRE##_STATESZ); \ key->ocount = key->icount = 0; \ } \ \ @@ -154,6 +164,49 @@ void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \ BURN(ctx); \ } \ \ +/* --- @pre_sslmacinit@ --- * \ + * \ + * Arguments: @pre_mackey *key@ = pointer to MAC key object \ + * @const void *k@ = pointer to key to use \ + * @size_t sz@ = size of key data \ + * \ + * Returns: --- \ + * \ + * Use: Initializes a MAC key for doing hasing using the SSL3 \ + * variant of HMAC. \ + */ \ + \ +void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \ +{ \ + int i; \ + const octet *kbuf = k; \ + pre##_ctx ctx; \ + octet buf[PRE##_HASHSZ]; \ + \ + if (sz > PRE##_BUFSZ) { \ + pre##_init(&ctx); \ + pre##_hash(&ctx, k, sz); \ + pre##_done(&ctx, buf); \ + kbuf = buf; \ + sz = PRE##_HASHSZ; \ + } \ + \ + pre##_init(&ctx); \ + memcpy(ctx.buf, kbuf, sz); \ + memset(ctx.buf + sz, 0x5c, PRE##_BUFSZ - sz); \ + pre##_compress(&ctx, ctx.buf); \ + pre##_state(&ctx, key->ochain); \ + \ + pre##_init(&ctx); \ + memcpy(ctx.buf, kbuf, sz); \ + memset(ctx.buf + sz, 0x36, PRE##_BUFSZ - sz); \ + pre##_compress(&ctx, ctx.buf); \ + pre##_state(&ctx, key->ichain); \ + \ + key->ocount = key->icount = PRE##_BUFSZ; \ + BURN(ctx); \ +} \ + \ /* --- @pre_macinit@ --- * \ * \ * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ @@ -166,7 +219,7 @@ void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \ \ void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \ { \ - memcpy(ctx->chain, key->ochain, PRE##_HASHSZ); \ + memcpy(ctx->chain, key->ochain, PRE##_STATESZ); \ ctx->count = key->ocount; \ pre##_set(&ctx->ctx, key->ichain, key->icount); \ } \ @@ -238,6 +291,14 @@ static gmac *gkey(const void *k, size_t sz) \ return (&gk->m); \ } \ \ +static gmac *gsslkey(const void *k, size_t sz) \ +{ \ + gkctx *gk = S_CREATE(gkctx); \ + gk->m.ops = &gkops; \ + pre##_sslmacinit(&gk->k, k, sz); \ + return (&gk->m); \ +} \ + \ static void ghhash(ghash *h, const void *p, size_t sz) \ { \ gctx *g = (gctx *)h; \ @@ -253,6 +314,14 @@ static octet *ghdone(ghash *h, void *buf) \ return (buf); \ } \ \ +static ghash *ghcopy(ghash *h) \ +{ \ + gctx *g = (gctx *)h; \ + gctx *gg = S_CREATE(gctx); \ + memcpy(gg, g, sizeof(gctx)); \ + return (&gg->h); \ +} \ + \ static void ghdestroy(ghash *h) \ { \ gctx *g = (gctx *)h; \ @@ -275,10 +344,12 @@ static ghash *ghinit(void) \ \ const gcmac pre##_hmac = \ { #pre "-hmac", PRE##_HASHSZ, pre##_mackeysz, gkey }; \ +const gcmac pre##_sslmac = \ + { #pre "-sslmac", PRE##_HASHSZ, pre##_mackeysz, gsslkey }; \ static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \ static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \ static const ghash_ops gops = \ - { &gch, ghhash, ghdone, ghdestroy }; \ + { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \ \ HMAC_TEST(PRE, pre)