From: Niels Möller Date: Tue, 29 Oct 2024 19:48:19 +0000 (+0100) Subject: New internal function _nettle_hmac_set_key. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d97cb110201976a690d1a277a32002f32982271;p=thirdparty%2Fnettle.git New internal function _nettle_hmac_set_key. --- diff --git a/hmac-internal.c b/hmac-internal.c index c9f73b8a..51874de9 100644 --- a/hmac-internal.c +++ b/hmac-internal.c @@ -69,3 +69,31 @@ _nettle_hmac_inner_block (size_t block_size, uint8_t *block) { memxor_byte (block, OPAD ^ IPAD, block_size); } + +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) +{ + hash->init (ctx); + memcpy (outer, ctx, state_size); + memcpy (inner, ctx, state_size); + + if (key_length > hash->block_size) + { + hash->update (ctx, key_length, key); + hash->digest (ctx, block); + _nettle_hmac_outer_block_digest (hash->block_size, block, hash->digest_size); + } + else + _nettle_hmac_outer_block (hash->block_size, block, key_length, key); + + compress (outer, block); + + _nettle_hmac_inner_block (hash->block_size, block); + compress (inner, block); + + memcpy (ctx, inner, state_size); +} diff --git a/hmac-internal.h b/hmac-internal.h index d0552c03..c02c4178 100644 --- a/hmac-internal.h +++ b/hmac-internal.h @@ -33,6 +33,7 @@ #define NETTLE_HMAC_INTERNAL_H_INCLUDED #include "nettle-types.h" +#include "nettle-meta.h" #define IPAD 0x36 #define OPAD 0x5c @@ -46,4 +47,13 @@ _nettle_hmac_outer_block_digest (size_t block_size, uint8_t *block, size_t key_s 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); + #endif /* NETTLE_HMAC_INTERNAL_H_INCLUDED */ diff --git a/hmac-sha256.c b/hmac-sha256.c index c1cf0879..7a5d9630 100644 --- a/hmac-sha256.c +++ b/hmac-sha256.c @@ -55,25 +55,9 @@ void hmac_sha256_set_key(struct hmac_sha256_ctx *ctx, size_t key_length, const uint8_t *key) { - sha256_init (&ctx->state); - if (key_length > SHA256_BLOCK_SIZE) - { - sha256_update (&ctx->state, key_length, key); - sha256_digest (&ctx->state, ctx->state.block); - _nettle_hmac_outer_block_digest (SHA256_BLOCK_SIZE, ctx->state.block, SHA256_DIGEST_SIZE); - } - else - _nettle_hmac_outer_block (SHA256_BLOCK_SIZE, ctx->state.block, - key_length, key); - - memcpy (ctx->outer, _nettle_sha256_iv, sizeof(ctx->outer)); - sha256_compress (ctx->outer, ctx->state.block); - - _nettle_hmac_inner_block (SHA256_BLOCK_SIZE, ctx->state.block); - memcpy (ctx->inner, _nettle_sha256_iv, sizeof(ctx->inner)); - sha256_compress (ctx->inner, ctx->state.block); - - hmac_sha256_init (ctx); + _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.count = 1; } void