X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/4dafd519dbbdc17887f2de4f6cabe33ac4667ab0..HEAD:/symm/hmac-def.h diff --git a/symm/hmac-def.h b/symm/hmac-def.h index 893c6868..76d578d8 100644 --- a/symm/hmac-def.h +++ b/symm/hmac-def.h @@ -62,13 +62,17 @@ * Use: Creates implementations for the HMAC and NMAC functions. */ -#define HMAC_DEF(PRE, pre) \ +#define HMAC_DEF(PRE, pre) HMAC_DEFX(PRE, pre, #pre, #pre) +#define HMAC_DEFX(PRE, pre, name, fname) \ \ /* --- Useful constants --- */ \ \ -const octet pre##_hmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \ -const octet pre##_sslmackeysz[] = { KSZ_ANY, PRE##_STATESZ }; \ -const octet pre##_nmackeysz[] = { KSZ_SET, 2 * PRE##_STATESZ, 0 }; \ +const octet pre##_hmackeysz[] = \ + { KSZ_ANY | KSZ_16BIT, PRE##_HASHSZ/256, PRE##_HASHSZ%256 }; \ +const octet pre##_sslmackeysz[] = \ + { KSZ_ANY | KSZ_16BIT, PRE##_HASHSZ/256, PRE##_HASHSZ%256 }; \ +const octet pre##_nmackeysz[] = \ + { KSZ_SET | KSZ_16BIT, 2*PRE##_HASHSZ/256, 2*PRE##_HASHSZ%256, 0, 0 }; \ \ /* --- @pre_nmacinit@ --- * \ * \ @@ -108,31 +112,28 @@ void pre##_hmacinit(pre##_mackey *key, const void *k, size_t sz) \ int i; \ const octet *kbuf = k; \ pre##_ctx ctx; \ - octet buf[PRE##_HASHSZ]; \ + octet hbuf[PRE##_HASHSZ], buf[PRE##_BUFSZ]; \ \ if (sz > PRE##_BUFSZ) { \ pre##_init(&ctx); \ pre##_hash(&ctx, k, sz); \ - pre##_done(&ctx, buf); \ - kbuf = buf; \ + pre##_done(&ctx, hbuf); \ + kbuf = hbuf; \ sz = PRE##_HASHSZ; \ } \ \ pre##_init(&ctx); \ - memset(ctx.buf, 0x5c, PRE##_BUFSZ); \ - for (i = 0; i < sz; i++) \ - ctx.buf[i] ^= kbuf[i]; \ - pre##_compress(&ctx, ctx.buf); \ - pre##_state(&ctx, key->ochain); \ + memset(buf, 0x5c, PRE##_BUFSZ); \ + for (i = 0; i < sz; i++) buf[i] ^= kbuf[i]; \ + pre##_hash(&ctx, buf, PRE##_BUFSZ); \ + key->ocount = pre##_state(&ctx, key->ochain); \ \ pre##_init(&ctx); \ - memset(ctx.buf, 0x36, PRE##_BUFSZ); \ - for (i = 0; i < sz; i++) \ - ctx.buf[i] ^= kbuf[i]; \ - pre##_compress(&ctx, ctx.buf); \ - pre##_state(&ctx, key->ichain); \ + memset(buf, 0x36, PRE##_BUFSZ); \ + for (i = 0; i < sz; i++) buf[i] ^= kbuf[i]; \ + pre##_hash(&ctx, buf, PRE##_BUFSZ); \ + key->icount = pre##_state(&ctx, key->ichain); \ \ - key->ocount = key->icount = PRE##_BUFSZ; \ BURN(ctx); \ } \ \ @@ -152,29 +153,28 @@ void pre##_sslmacinit(pre##_mackey *key, const void *k, size_t sz) \ { \ const octet *kbuf = k; \ pre##_ctx ctx; \ - octet buf[PRE##_HASHSZ]; \ + octet hbuf[PRE##_HASHSZ], buf[PRE##_BUFSZ]; \ \ if (sz > PRE##_BUFSZ) { \ pre##_init(&ctx); \ pre##_hash(&ctx, k, sz); \ - pre##_done(&ctx, buf); \ - kbuf = buf; \ + pre##_done(&ctx, hbuf); \ + kbuf = hbuf; \ 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); \ + memcpy(buf, kbuf, sz); \ + memset(buf + sz, 0x5c, PRE##_BUFSZ - sz); \ + pre##_hash(&ctx, buf, PRE##_BUFSZ); \ + key->ocount = 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); \ + memcpy(buf, kbuf, sz); \ + memset(buf + sz, 0x36, PRE##_BUFSZ - sz); \ + pre##_hash(&ctx, buf, PRE##_BUFSZ); \ + key->icount = pre##_state(&ctx, key->ichain); \ \ - key->ocount = key->icount = PRE##_BUFSZ; \ BURN(ctx); \ } \ \ @@ -207,9 +207,7 @@ void pre##_macinit(pre##_macctx *ctx, const pre##_mackey *key) \ */ \ \ void pre##_machash(pre##_macctx *ctx, const void *buf, size_t sz) \ -{ \ - pre##_hash(&ctx->ctx, buf, sz); \ -} \ + { pre##_hash(&ctx->ctx, buf, sz); } \ \ /* --- @pre_macdone@ --- * \ * \ @@ -328,25 +326,27 @@ static ghash *ghinit(void) \ } \ \ const gcmac pre##_nmac = \ - { #pre "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey }; \ + { name "-nmac", PRE##_HASHSZ, pre##_nmackeysz, gnkey }; \ const gcmac pre##_hmac = \ - { #pre "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey }; \ + { name "-hmac", PRE##_HASHSZ, pre##_hmackeysz, gkey }; \ const gcmac pre##_sslmac = \ - { #pre "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey }; \ + { name "-sslmac", PRE##_HASHSZ, pre##_sslmackeysz, gsslkey }; \ static const gmac_ops gkops = { &pre##_hmac, gkinit, gkdestroy }; \ static const gmac_ops gnkops = { &pre##_nmac, gkinit, gkdestroy }; \ static const gmac_ops gsslkops = { &pre##_sslmac, gkinit, gkdestroy }; \ -static const gchash gch = { #pre "-hmac", PRE##_HASHSZ, ghinit }; \ +static const gchash gch = { name "-hmac", PRE##_HASHSZ, ghinit }; \ static const ghash_ops gops = \ { &gch, ghhash, ghdone, ghdestroy, ghcopy }; \ -static const gchash gnch = { #pre "-nmac", PRE##_HASHSZ, ghinit }; \ +static const gchash gnch = { name "-nmac", PRE##_HASHSZ, ghinit }; \ static const ghash_ops gnops = \ { &gnch, ghhash, ghdone, ghdestroy, ghcopy }; \ -static const gchash gsslch = { #pre "-sslmac", PRE##_HASHSZ, ghinit }; \ +static const gchash gsslch = { name "-sslmac", PRE##_HASHSZ, ghinit }; \ static const ghash_ops gsslops = \ { &gsslch, ghhash, ghdone, ghdestroy, ghcopy }; \ \ -HMAC_TEST(PRE, pre) +HMAC_TESTX(PRE, pre, name, fname) + +#define HMAC_TEST(PRE, pre) HMAC_TESTX(PRE, pre, #pre, #pre) /* --- @HMAC_TEST@ --- * * @@ -359,11 +359,12 @@ HMAC_TEST(PRE, pre) #include +#include #include #include #include -#define HMAC_TEST(PRE, pre) \ +#define HMAC_TESTX(PRE, pre, name, fname) \ \ static int macverify(dstr *v) \ { \ @@ -399,7 +400,7 @@ static int macverify(dstr *v) \ csz -= i; \ } \ pre##_macdone(&cctx, d.buf); \ - if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \ + if (MEMCMP(d.buf, !=, v[2].buf, PRE##_HASHSZ)) { \ printf("\nfail:\n\tstep = %i\n\tinput = `%s'\n\tkey = ", \ *ip, v[0].buf); \ type_hex.dump(&v[1], stdout); \ @@ -417,7 +418,7 @@ static int macverify(dstr *v) \ } \ \ static test_chunk macdefs[] = { \ - { #pre "-hmac", macverify, \ + { name "-hmac", macverify, \ { &type_string, &type_hex, &type_hex, 0 } }, \ { 0, 0, { 0 } } \ }; \ @@ -425,12 +426,12 @@ static test_chunk macdefs[] = { \ int main(int argc, char *argv[]) \ { \ ego(argv[0]); \ - test_run(argc, argv, macdefs, SRCDIR"/t/" #pre); \ + test_run(argc, argv, macdefs, SRCDIR"/t/" fname); \ return (0); \ } #else -# define HMAC_TEST(PRE, pre) +# define HMAC_TESTX(PRE, pre, name, fname) #endif /*----- That's all, folks -------------------------------------------------*/