]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
New internal function _nettle_hmac_set_key.
authorNiels Möller <nisse@lysator.liu.se>
Tue, 29 Oct 2024 19:48:19 +0000 (20:48 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Sun, 22 Jun 2025 19:11:04 +0000 (21:11 +0200)
hmac-internal.c
hmac-internal.h
hmac-sha256.c

index c9f73b8aa0a83c3047e0da4d9aa6bfa57a154a45..51874de9e6bfcaf55795cec39d2813fb9addd284 100644 (file)
@@ -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);
+}
index d0552c03b4135c2bf2233334568384a76e2f0e08..c02c41781729d471ac42810d63532c13bdcc0732 100644 (file)
@@ -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 */
index c1cf0879c069b090ecf575c92118d711ac55e64e..7a5d9630fa54f249c3f14da5fc0f68a4fd9eb8ef 100644 (file)
@@ -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