* 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@ --- * \
* \
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); \
} \
\
{ \
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); \
} \
\
*/ \
\
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@ --- * \
* \
} \
\
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@ --- *
*
#include <stdio.h>
+#include <mLib/macros.h>
#include <mLib/dstr.h>
#include <mLib/quis.h>
#include <mLib/testrig.h>
-#define HMAC_TEST(PRE, pre) \
+#define HMAC_TESTX(PRE, pre, name, fname) \
\
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); \
} \
\
static test_chunk macdefs[] = { \
- { #pre "-hmac", macverify, \
+ { name "-hmac", macverify, \
{ &type_string, &type_hex, &type_hex, 0 } }, \
{ 0, 0, { 0 } } \
}; \
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 -------------------------------------------------*/