From e9b15bb5cace789408ee8b2668210a647cf04350 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niels=20M=C3=B6ller?= Date: Wed, 30 Oct 2024 09:43:52 +0100 Subject: [PATCH] New macro _NETTLE_HMAC_DIGEST --- hmac-internal.c | 57 +++++++++++++++++++------------------------------ hmac-internal.h | 30 ++++++++++++-------------- hmac-sha256.c | 20 ++--------------- 3 files changed, 38 insertions(+), 69 deletions(-) diff --git a/hmac-internal.c b/hmac-internal.c index 51874de9..691fc38f 100644 --- a/hmac-internal.c +++ b/hmac-internal.c @@ -47,53 +47,40 @@ memxor_byte (uint8_t *p, uint8_t b, size_t n) p[i] ^= b; } -void -_nettle_hmac_outer_block (size_t block_size, uint8_t *block, size_t key_size, const uint8_t *key) -{ - assert (key_size <= block_size); - memset (block, OPAD, block_size); - memxor (block, key, key_size); -} - -void -_nettle_hmac_outer_block_digest (size_t block_size, uint8_t *block, size_t key_size) -{ - assert (key_size <= block_size); - - memxor_byte (block, OPAD, key_size); - memset (block + key_size, OPAD, block_size - key_size); -} - -void -_nettle_hmac_inner_block (size_t block_size, uint8_t *block) +static void +memxor_byte4 (uint8_t *dst, const uint8_t *src, uint8_t b, size_t n) { - memxor_byte (block, OPAD ^ IPAD, block_size); + size_t i; + for (i = 0; i < n; i++) + dst[i] = src[i] ^ b; } void -_nettle_hmac_set_key(size_t state_size, void *outer, void *inner, - void *ctx, uint8_t *block, - const struct nettle_hash *hash, - compress_func *compress, - size_t key_length, const uint8_t *key) +_nettle_hmac_set_key (size_t state_size, void *outer, void *inner, + void *ctx, uint8_t *block, + const struct nettle_hash *hash, + size_t key_size, const uint8_t *key) { hash->init (ctx); - memcpy (outer, ctx, state_size); - memcpy (inner, ctx, state_size); - if (key_length > hash->block_size) + if (key_size > hash->block_size) { - hash->update (ctx, key_length, key); + hash->update (ctx, key_size, key); hash->digest (ctx, block); - _nettle_hmac_outer_block_digest (hash->block_size, block, hash->digest_size); + key_size = hash->digest_size; + memxor_byte (block, OPAD, key_size); } else - _nettle_hmac_outer_block (hash->block_size, block, key_length, key); + memxor_byte4 (block, key, OPAD, key_size); - compress (outer, block); + memset (block + key_size, OPAD, hash->block_size - key_size); - _nettle_hmac_inner_block (hash->block_size, block); - compress (inner, block); + hash->update (ctx, hash->block_size, block); + memcpy (outer, ctx, state_size); + + memxor_byte (block, OPAD ^ IPAD, hash->block_size); - memcpy (ctx, inner, state_size); + hash->init (ctx); + hash->update (ctx, hash->block_size, block); + memcpy (inner, ctx, state_size); } diff --git a/hmac-internal.h b/hmac-internal.h index c02c4178..2861e730 100644 --- a/hmac-internal.h +++ b/hmac-internal.h @@ -38,22 +38,20 @@ #define IPAD 0x36 #define OPAD 0x5c -/* Initialize BLOCK from KEY, with KEY_SIZE <= BLOCK_SIZE. If KEY == - NULL, key is already written at the start of the block. */ -void _nettle_hmac_outer_block (size_t block_size, uint8_t *block, size_t key_size, const uint8_t *key); - -void -_nettle_hmac_outer_block_digest (size_t block_size, uint8_t *block, size_t key_size); - -void _nettle_hmac_inner_block (size_t block_size, uint8_t *block); - -typedef void compress_func (void *state, const uint8_t *block); - void -_nettle_hmac_set_key(size_t state_size, void *outer, void *inner, - void *ctx, uint8_t *block, - const struct nettle_hash *hash, - compress_func *compress, - size_t key_length, const uint8_t *key); +_nettle_hmac_set_key (size_t state_size, void *outer, void *inner, + void *ctx, uint8_t *block, + const struct nettle_hash *hash, + size_t key_size, const uint8_t *key); + +#define _NETTLE_HMAC_DIGEST(outer, inner, ctx, digest_size, f, digest) do { \ + f ((ctx), (ctx)->block); \ + memcpy (&(ctx)->state, (outer), sizeof ((ctx)->state)); \ + (ctx)->count = 1; \ + (ctx)->index = (digest_size); \ + f ((ctx), (digest)); \ + memcpy (&(ctx)->state, (inner), sizeof ((ctx)->state)); \ + (ctx)->count = 1; \ + } while (0) #endif /* NETTLE_HMAC_INTERNAL_H_INCLUDED */ diff --git a/hmac-sha256.c b/hmac-sha256.c index 7a5d9630..bcb7e60b 100644 --- a/hmac-sha256.c +++ b/hmac-sha256.c @@ -42,21 +42,12 @@ #include "memxor.h" #include "sha2-internal.h" -/* Initialize for processing a new message.*/ -static void -hmac_sha256_init (struct hmac_sha256_ctx *ctx) -{ - memcpy (ctx->state.state, ctx->inner, sizeof (ctx->state.state)); - ctx->state.count = 1; - /* index should already be zero, from previous call to sha256_init or sha256_digest. */ -} - void hmac_sha256_set_key(struct hmac_sha256_ctx *ctx, size_t key_length, const uint8_t *key) { _nettle_hmac_set_key (sizeof(ctx->outer), ctx->outer, ctx->inner, &ctx->state, - ctx->state.block, &nettle_sha256, (compress_func *) sha256_compress, key_length, key); + ctx->state.block, &nettle_sha256, key_length, key); ctx->state.count = 1; } @@ -71,12 +62,5 @@ void hmac_sha256_digest(struct hmac_sha256_ctx *ctx, uint8_t *digest) { - sha256_digest (&ctx->state, ctx->state.block); - - memcpy (ctx->state.state, ctx->outer, sizeof (ctx->state.state)); - ctx->state.count = 1; - ctx->state.index = SHA256_DIGEST_SIZE; - sha256_digest (&ctx->state, digest); - - hmac_sha256_init (ctx); + _NETTLE_HMAC_DIGEST (ctx->outer, ctx->inner, &ctx->state, SHA256_DIGEST_SIZE, sha256_digest, digest); } -- 2.47.2