summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Eremin-Solenikov <dbaryshkov@gmail.com>2019-07-11 21:43:13 +0300
committerNiels Möller <nisse@lysator.liu.se>2019-09-14 12:17:43 +0200
commit5c4be62a7ddc59e4c4fe8c7a3a2c39e472732e1b (patch)
tree7fa4e43003bc3262a0e27e55740d507ef2912fcc
parentb708992f386eeff076879b0a077d9e917925bd46 (diff)
downloadnettle-5c4be62a7ddc59e4c4fe8c7a3a2c39e472732e1b.tar.gz
gosthash94: switch to using MD_UPDATE() macro
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
-rw-r--r--gosthash94.c43
-rw-r--r--gosthash94.h5
2 files changed, 12 insertions, 36 deletions
diff --git a/gosthash94.c b/gosthash94.c
index 954130f7..b1ea9873 100644
--- a/gosthash94.c
+++ b/gosthash94.c
@@ -284,6 +284,8 @@ gost_compute_sum_and_hash (struct gosthash94_ctx *ctx, const uint8_t *block,
gost_block_compress (ctx, block_le, sbox);
}
+#define COMPRESS(ctx, block) gost_compute_sum_and_hash((ctx), (block), sbox);
+
/**
* Calculate message hash.
* Can be called repeatedly with chunks of the message to be hashed.
@@ -297,33 +299,7 @@ gosthash94_update_int (struct gosthash94_ctx *ctx,
size_t length, const uint8_t *msg,
const uint32_t sbox[4][256])
{
- unsigned index = (unsigned) ctx->length & 31;
- ctx->length += length;
-
- /* fill partial block */
- if (index)
- {
- unsigned left = GOSTHASH94_BLOCK_SIZE - index;
- memcpy (ctx->message + index, msg, (length < left ? length : left));
- if (length < left)
- return;
-
- /* process partial block */
- gost_compute_sum_and_hash (ctx, ctx->message, sbox);
- msg += left;
- length -= left;
- }
- while (length >= GOSTHASH94_BLOCK_SIZE)
- {
- gost_compute_sum_and_hash (ctx, msg, sbox);
- msg += GOSTHASH94_BLOCK_SIZE;
- length -= GOSTHASH94_BLOCK_SIZE;
- }
- if (length)
- {
- /* save leftovers */
- memcpy (ctx->message, msg, length);
- }
+ MD_UPDATE(ctx, length, msg, COMPRESS, ctx->count++);
}
/**
@@ -369,21 +345,20 @@ gosthash94_write_digest (struct gosthash94_ctx *ctx,
size_t length, uint8_t *result,
const uint32_t sbox[4][256])
{
- unsigned index = ctx->length & 31;
- uint32_t msg32[8];
+ uint32_t msg32[GOSTHASH94_BLOCK_SIZE / 4];
assert(length <= GOSTHASH94_DIGEST_SIZE);
/* pad the last block with zeroes and hash it */
- if (index > 0)
+ if (ctx->index > 0)
{
- memset (ctx->message + index, 0, 32 - index);
- gost_compute_sum_and_hash (ctx, ctx->message, sbox);
+ memset (ctx->block + ctx->index, 0, GOSTHASH94_BLOCK_SIZE - ctx->index);
+ gost_compute_sum_and_hash (ctx, ctx->block, sbox);
}
/* hash the message length and the sum */
- msg32[0] = ctx->length << 3;
- msg32[1] = ctx->length >> 29;
+ msg32[0] = (ctx->count << 8) | (ctx->index << 3);
+ msg32[1] = ctx->count >> 24;
memset (msg32 + 2, 0, sizeof (uint32_t) * 6);
gost_block_compress (ctx, msg32, sbox);
diff --git a/gosthash94.h b/gosthash94.h
index dfa97f61..0efd6412 100644
--- a/gosthash94.h
+++ b/gosthash94.h
@@ -87,8 +87,9 @@ struct gosthash94_ctx
{
uint32_t hash[8]; /* algorithm 256-bit state */
uint32_t sum[8]; /* sum of processed message blocks */
- uint64_t length; /* number of processed bytes */
- uint8_t message[GOSTHASH94_BLOCK_SIZE]; /* 256-bit buffer for leftovers */
+ uint64_t count; /* Block count */
+ unsigned index; /* Into buffer */
+ uint8_t block[GOSTHASH94_BLOCK_SIZE]; /* 256-bit buffer for leftovers */
};
#define gosthash94cp_ctx gosthash94_ctx