]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
New macro _NETTLE_HMAC_DIGEST
authorNiels Möller <nisse@lysator.liu.se>
Wed, 30 Oct 2024 08:43:52 +0000 (09:43 +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 51874de9e6bfcaf55795cec39d2813fb9addd284..691fc38f62d7c4e5ac4a3dcc92bca1f16f7ae478 100644 (file)
@@ -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);
 }
index c02c41781729d471ac42810d63532c13bdcc0732..2861e730bd567e42da1e860d9973b72173fb5746 100644 (file)
 #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 */
index 7a5d9630fa54f249c3f14da5fc0f68a4fd9eb8ef..bcb7e60b2e9f0a6d39a3ef081b920cf4c6c2c0c3 100644 (file)
 #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);
 }