{
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);
+}
#define NETTLE_HMAC_INTERNAL_H_INCLUDED
#include "nettle-types.h"
+#include "nettle-meta.h"
#define IPAD 0x36
#define OPAD 0x5c
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 */
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