auth/hmac: complete overhaul

Now that there are dedicated struct for initial parameters, make the
HMAC params provide a lc_hash_ctx to be used internally. This makes HMAC
less special and allows it to work with any hash, instead of needing
dedicated inits and ctx_news. As an upside, now it's possible to have a
one-pass HMAC.
This commit is contained in:
Lucas Gabriel Vuotto 2024-06-10 17:29:29 +00:00
parent f6bddfcd70
commit 666b833b98
4 changed files with 72 additions and 171 deletions

View File

@ -20,8 +20,6 @@
#include "auth.h" #include "auth.h"
#include "hash.h" #include "hash.h"
#include "impl_hmac.h" #include "impl_hmac.h"
#include "impl_sha256.h"
#include "impl_sha512.h"
#include "util.h" #include "util.h"
@ -31,52 +29,34 @@
static int static int
hmac_common_init(void *arg, const uint8_t *key, size_t keylen) hmac_init(void *arg, void *initparams)
{ {
struct hmac_ctx *ctx = arg; struct hmac_ctx *ctx = arg;
struct lc_hmac_params *params = initparams;
uint8_t ikeypad[HMAC_BLOCKLEN_MAX]; uint8_t ikeypad[HMAC_BLOCKLEN_MAX];
size_t i, olen; size_t i, olen, blen, keylen;
if (keylen > ctx->blocksz) { ctx->hash = params->hash;
if (!lc_hash_init(ctx->hctx) || keylen = params->keylen;
!lc_hash_update(ctx->hctx, key, keylen) || blen = params->hash->impl->blocklen;
!lc_hash_final(ctx->hctx, ctx->key, &olen))
if (keylen > blen) {
if (!lc_hash(ctx->hash->impl, ctx->key, &olen, params->key,
keylen))
return 0; return 0;
keylen = olen; keylen = olen;
} else } else
for (i = 0; i < keylen; i++) for (i = 0; i < keylen; i++)
ctx->key[i] = key[i]; ctx->key[i] = params->key[i];
for (i = keylen; i < ctx->blocksz; i++) for (i = keylen; i < blen; i++)
ctx->key[i] = 0; ctx->key[i] = 0;
for (i = 0; i < ctx->blocksz; i++) for (i = 0; i < blen; i++)
ikeypad[i] = ctx->key[i] ^ HMAC_IPAD; ikeypad[i] = ctx->key[i] ^ HMAC_IPAD;
return lc_hash_init(ctx->hctx) && return lc_hash_init(ctx->hash) &&
lc_hash_update(ctx->hctx, ikeypad, ctx->blocksz); lc_hash_update(ctx->hash, ikeypad, blen);
}
static int
hmac_sha224_sha256_init(void *arg, void *initparams)
{
struct lc_hmac_params *params = initparams;
struct hmac_ctx *ctx = arg;
ctx->blocksz = LC_SHA256_BLOCKLEN;
return hmac_common_init(ctx, params->key, params->keylen);
}
static int
hmac_sha384_sha512_init(void *arg, void *initparams)
{
struct lc_hmac_params *params = initparams;
struct hmac_ctx *ctx = arg;
ctx->blocksz = LC_SHA512_BLOCKLEN;
return hmac_common_init(ctx, params->key, params->keylen);
} }
static int static int
@ -84,153 +64,78 @@ hmac_update(void *arg, const uint8_t *in, size_t inlen)
{ {
struct hmac_ctx *ctx = arg; struct hmac_ctx *ctx = arg;
return lc_hash_update(ctx->hctx, in, inlen); return lc_hash_update(ctx->hash, in, inlen);
} }
static int static int
hmac_final(void *arg, uint8_t *out, size_t *outlen) hmac_final(void *arg, uint8_t *out, size_t *outlen)
{ {
struct hmac_ctx *ctx = arg; struct hmac_ctx *ctx = arg;
struct lc_hash_ctx *hctx; uint8_t m[HMAC_BLOCKLEN_MAX], okeypad[HMAC_BLOCKLEN_MAX];
uint8_t m[HMAC_BLOCKLEN_MAX], size_t i, olen, blen;
okeypad[HMAC_BLOCKLEN_MAX];
size_t i, olen;
int rc; int rc;
if (out == NULL) { if (out == NULL) {
(void)lc_hash_final(ctx->hctx, NULL, outlen); *outlen = ctx->hash->impl->hashlen;
return 1; return 1;
} }
hctx = ctx->hctx;
*outlen = 0; *outlen = 0;
for (i = 0; i < ctx->blocksz; i++) blen = ctx->hash->impl->blocklen;
for (i = 0; i < blen; i++)
okeypad[i] = ctx->key[i] ^ HMAC_OPAD; okeypad[i] = ctx->key[i] ^ HMAC_OPAD;
rc = lc_hash_final(ctx->hctx, m, &olen) && rc = lc_hash_final(ctx->hash, m, &olen) &&
lc_hash_init(ctx->hctx) && lc_hash_init(ctx->hash) &&
lc_hash_update(ctx->hctx, okeypad, ctx->blocksz) && lc_hash_update(ctx->hash, okeypad, blen) &&
lc_hash_update(ctx->hctx, m, olen) && lc_hash_update(ctx->hash, m, olen) &&
lc_hash_final(ctx->hctx, out, outlen); lc_hash_final(ctx->hash, out, outlen);
lc_scrub(ctx, sizeof(*ctx)); lc_scrub(ctx, sizeof(*ctx));
ctx->hctx = hctx;
return rc; return rc;
} }
static void * static int
hmac_common_ctx_new(const struct lc_hash_impl *impl) hmac_auth(uint8_t *out, size_t *outlen, void *initparams, const uint8_t *in,
size_t inlen)
{ {
struct hmac_ctx *ctx; struct lc_hmac_params *params = initparams;
struct hmac_ctx ctx;
ctx = malloc(sizeof(*ctx)); if (out == NULL) {
if (ctx == NULL) *outlen = params->hash->impl->hashlen;
return NULL; return 1;
ctx->hctx = lc_hash_ctx_new(impl);
if (ctx->hctx == NULL) {
free(ctx);
return NULL;
} }
return ctx; return hmac_init(&ctx, initparams) &&
hmac_update(&ctx, in, inlen) &&
hmac_final(&ctx, out, outlen);
} }
static void * static void *
hmac_sha224_ctx_new(void) hmac_ctx_new(void)
{ {
return hmac_common_ctx_new(lc_hash_impl_sha224()); return malloc(sizeof(struct hmac_ctx));
}
static void *
hmac_sha256_ctx_new(void)
{
return hmac_common_ctx_new(lc_hash_impl_sha256());
}
static void *
hmac_sha384_ctx_new(void)
{
return hmac_common_ctx_new(lc_hash_impl_sha384());
}
static void *
hmac_sha512_ctx_new(void)
{
return hmac_common_ctx_new(lc_hash_impl_sha512());
}
static void
hmac_ctx_free(void *arg)
{
struct hmac_ctx *ctx = arg;
if (ctx != NULL)
lc_hash_ctx_free(ctx->hctx);
} }
static struct lc_auth_impl hmac_sha224_impl = { static struct lc_auth_impl hmac_impl = {
.init = &hmac_sha224_sha256_init, .init = &hmac_init,
.update = &hmac_update, .update = &hmac_update,
.final = &hmac_final, .final = &hmac_final,
.auth = NULL, .auth = &hmac_auth,
.ctx_new = &hmac_sha224_ctx_new, .ctx_new = &hmac_ctx_new,
.ctx_free = &hmac_ctx_free, .ctx_free = NULL,
};
static struct lc_auth_impl hmac_sha256_impl = { .blocklen = 0,
.init = &hmac_sha224_sha256_init, .taglen = 0,
.update = &hmac_update,
.final = &hmac_final,
.auth = NULL,
.ctx_new = &hmac_sha256_ctx_new,
.ctx_free = &hmac_ctx_free,
};
static struct lc_auth_impl hmac_sha384_impl = {
.init = &hmac_sha384_sha512_init,
.update = &hmac_update,
.final = &hmac_final,
.auth = NULL,
.ctx_new = &hmac_sha384_ctx_new,
.ctx_free = &hmac_ctx_free,
};
static struct lc_auth_impl hmac_sha512_impl = {
.init = &hmac_sha384_sha512_init,
.update = &hmac_update,
.final = &hmac_final,
.auth = NULL,
.ctx_new = &hmac_sha512_ctx_new,
.ctx_free = &hmac_ctx_free,
}; };
const struct lc_auth_impl * const struct lc_auth_impl *
lc_auth_impl_hmac_sha224(void) lc_auth_impl_hmac(void)
{ {
return &hmac_sha224_impl; return &hmac_impl;
}
const struct lc_auth_impl *
lc_auth_impl_hmac_sha256(void)
{
return &hmac_sha256_impl;
}
const struct lc_auth_impl *
lc_auth_impl_hmac_sha384(void)
{
return &hmac_sha384_impl;
}
const struct lc_auth_impl *
lc_auth_impl_hmac_sha512(void)
{
return &hmac_sha512_impl;
} }

View File

@ -21,7 +21,6 @@
struct hmac_ctx { struct hmac_ctx {
struct lc_hash_ctx *hctx; struct lc_hash_ctx *hash;
size_t blocksz;
uint8_t key[HMAC_BLOCKLEN_MAX]; uint8_t key[HMAC_BLOCKLEN_MAX];
}; };

View File

@ -72,6 +72,7 @@ struct lc_hash_impl;
/* Authentication. */ /* Authentication. */
struct lc_hmac_params { struct lc_hmac_params {
struct lc_hash_ctx *hash;
size_t keylen; size_t keylen;
uint8_t *key; uint8_t *key;
}; };
@ -146,11 +147,8 @@ int lc_auth(const struct lc_auth_impl *, uint8_t *, size_t *, void *,
struct lc_auth_ctx *lc_auth_ctx_new(const struct lc_auth_impl *); struct lc_auth_ctx *lc_auth_ctx_new(const struct lc_auth_impl *);
void lc_auth_ctx_free(struct lc_auth_ctx *); void lc_auth_ctx_free(struct lc_auth_ctx *);
const struct lc_auth_impl *lc_auth_impl_hmac(void);
const struct lc_auth_impl *lc_auth_impl_poly1305(void); const struct lc_auth_impl *lc_auth_impl_poly1305(void);
const struct lc_auth_impl *lc_auth_impl_hmac_sha224(void);
const struct lc_auth_impl *lc_auth_impl_hmac_sha256(void);
const struct lc_auth_impl *lc_auth_impl_hmac_sha384(void);
const struct lc_auth_impl *lc_auth_impl_hmac_sha512(void);
/* /*

View File

@ -45,7 +45,7 @@ struct kwrunner {
}; };
static int hmac_sha2_runner(const struct lc_auth_impl *, static int hmac_sha2_runner(const struct lc_hash_impl *,
const struct testcase *, int); const struct testcase *, int);
static int hmac_sha224_runner(const struct testcase *, int); static int hmac_sha224_runner(const struct testcase *, int);
static int hmac_sha256_runner(const struct testcase *, int); static int hmac_sha256_runner(const struct testcase *, int);
@ -220,11 +220,10 @@ main(int argc, char *argv[])
} }
static int static int
hmac_sha2_runner(const struct lc_auth_impl *impl, const struct testcase *c, hmac_sha2_runner(const struct lc_hash_impl *impl, const struct testcase *c,
int verbose) int verbose)
{ {
struct lc_hmac_params params; struct lc_hmac_params params;
struct lc_auth_ctx *ctx;
uint8_t *buf; uint8_t *buf;
size_t olen; size_t olen;
@ -233,20 +232,20 @@ hmac_sha2_runner(const struct lc_auth_impl *impl, const struct testcase *c,
params.key = c->key; params.key = c->key;
params.keylen = c->keylen; params.keylen = c->keylen;
ctx = lc_auth_ctx_new(impl); params.hash = lc_hash_ctx_new(impl);
if (ctx == NULL) if (params.hash == NULL)
errx(1, "can't allocate ctx"); return 0;
if (!lc_auth_init(ctx, &params) || if (!lc_auth(lc_auth_impl_hmac(), NULL, &olen, &params, c->msg,
!lc_auth_update(ctx, c->msg, c->msglen) || c->msglen))
!lc_auth_final(ctx, NULL, &olen))
return 0; return 0;
buf = malloc(olen); buf = malloc(olen);
if (buf == NULL) if (buf == NULL)
err(1, "out of memory"); err(1, "out of memory");
if (!lc_auth_final(ctx, buf, &olen)) if (!lc_auth(lc_auth_impl_hmac(), buf, &olen, &params, c->msg,
c->msglen))
return 0; return 0;
/* /*
@ -267,7 +266,7 @@ hmac_sha2_runner(const struct lc_auth_impl *impl, const struct testcase *c,
} }
free(buf); free(buf);
lc_auth_ctx_free(ctx); lc_hash_ctx_free(params.hash);
return 1; return 1;
} }
@ -275,23 +274,23 @@ hmac_sha2_runner(const struct lc_auth_impl *impl, const struct testcase *c,
static int static int
hmac_sha224_runner(const struct testcase *c, int verbose) hmac_sha224_runner(const struct testcase *c, int verbose)
{ {
return hmac_sha2_runner(lc_auth_impl_hmac_sha224(), c, verbose); return hmac_sha2_runner(lc_hash_impl_sha224(), c, verbose);
} }
static int static int
hmac_sha256_runner(const struct testcase *c, int verbose) hmac_sha256_runner(const struct testcase *c, int verbose)
{ {
return hmac_sha2_runner(lc_auth_impl_hmac_sha256(), c, verbose); return hmac_sha2_runner(lc_hash_impl_sha256(), c, verbose);
} }
static int static int
hmac_sha384_runner(const struct testcase *c, int verbose) hmac_sha384_runner(const struct testcase *c, int verbose)
{ {
return hmac_sha2_runner(lc_auth_impl_hmac_sha384(), c, verbose); return hmac_sha2_runner(lc_hash_impl_sha384(), c, verbose);
} }
static int static int
hmac_sha512_runner(const struct testcase *c, int verbose) hmac_sha512_runner(const struct testcase *c, int verbose)
{ {
return hmac_sha2_runner(lc_auth_impl_hmac_sha512(), c, verbose); return hmac_sha2_runner(lc_hash_impl_sha512(), c, verbose);
} }