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);
}
#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 */
#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;
}
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);
}