Signed-off-by: Dmitry Eremin-Solenikov <[email protected]>
---
nettle-meta.h | 1 +
sha1-meta.c | 3 +++
sha1.c | 56 ++++++++++++++++++++++++++++++++++++---------------
sha1.h | 29 +++++++++++++++++++++++---
4 files changed, 70 insertions(+), 19 deletions(-)
diff --git a/nettle-meta.h b/nettle-meta.h
index 79a9900cbdfa..95aaaf0fcc8c 100644
--- a/nettle-meta.h
+++ b/nettle-meta.h
@@ -183,6 +183,7 @@ extern const struct nettle_hash nettle_sha3_512;
extern const struct nettle_bctx_hash nettle_bctx_md5;
extern const struct nettle_bctx_hash nettle_bctx_ripemd160;
+extern const struct nettle_bctx_hash nettle_bctx_sha1;
struct nettle_aead
{
diff --git a/sha1-meta.c b/sha1-meta.c
index dde990360a8f..def2704c40ee 100644
--- a/sha1-meta.c
+++ b/sha1-meta.c
@@ -39,3 +39,6 @@
const struct nettle_hash nettle_sha1
= _NETTLE_HASH(sha1, SHA1);
+
+const struct nettle_bctx_hash nettle_bctx_sha1
+= _NETTLE_BLOCK_HASH(sha1, SHA1);
diff --git a/sha1.c b/sha1.c
index af73096c9016..7dca34b71ef7 100644
--- a/sha1.c
+++ b/sha1.c
@@ -47,7 +47,7 @@
/* Initialize the SHA values */
void
-sha1_init(struct sha1_ctx *ctx)
+sha1_block_init(struct sha1_state *state, struct block_ctx *bctx)
{
/* FIXME: Put the buffer last in the struct, and arrange so that we
can initialize with a single memcpy. */
@@ -61,11 +61,17 @@ sha1_init(struct sha1_ctx *ctx)
0xC3D2E1F0L,
};
- memcpy(ctx->state, iv, sizeof(ctx->state));
- ctx->count = 0;
-
+ memcpy(state->state, iv, sizeof(state->state));
+ state->count = 0;
+
/* Initialize buffer */
- ctx->index = 0;
+ bctx->index = 0;
+}
+
+void
+sha1_init(struct sha1_ctx *ctx)
+{
+ return sha1_block_init(&ctx->state, (struct block_ctx *)&ctx->block);
}
#define COMPRESS(ctx, data) (nettle_sha1_compress((ctx)->state, data))
@@ -74,27 +80,45 @@ void
sha1_update(struct sha1_ctx *ctx,
size_t length, const uint8_t *data)
{
- MD_UPDATE (ctx, length, data, COMPRESS, ctx->count++);
+ MD_BLOCK_UPDATE(&ctx->state, &ctx->block, SHA1_BLOCK_SIZE, length, data,
COMPRESS, ctx->state.count++);
}
-
+
void
-sha1_digest(struct sha1_ctx *ctx,
- size_t length,
- uint8_t *digest)
+sha1_block_update(struct sha1_state *state,
+ struct block_ctx *bctx,
+ size_t length,
+ const uint8_t *data)
+{
+ MD_BLOCK_UPDATE(state, bctx, SHA1_BLOCK_SIZE, length, data, COMPRESS,
state->count++);
+}
+
+void
+sha1_block_digest(struct sha1_state *state,
+ struct block_ctx *bctx,
+ size_t length,
+ uint8_t *digest)
{
uint64_t bit_count;
assert(length <= SHA1_DIGEST_SIZE);
- MD_PAD(ctx, 8, COMPRESS);
+ MD_BLOCK_PAD(state, bctx, SHA1_BLOCK_SIZE, 8, COMPRESS);
/* There are 512 = 2^9 bits in one block */
- bit_count = (ctx->count << 9) | (ctx->index << 3);
+ bit_count = (state->count << 9) | (bctx->index << 3);
/* append the 64 bit count */
- WRITE_UINT64(ctx->block + (SHA1_BLOCK_SIZE - 8), bit_count);
- nettle_sha1_compress(ctx->state, ctx->block);
+ WRITE_UINT64(bctx->buffer + (SHA1_BLOCK_SIZE - 8), bit_count);
+ nettle_sha1_compress(state->state, bctx->buffer);
+
+ _nettle_write_be32(length, digest, state->state);
+ sha1_block_init(state, bctx);
+}
- _nettle_write_be32(length, digest, ctx->state);
- sha1_init(ctx);
+void
+sha1_digest(struct sha1_ctx *ctx,
+ size_t length,
+ uint8_t *digest)
+{
+ return sha1_block_digest(&ctx->state, (struct block_ctx *)&ctx->block,
length, digest);
}
diff --git a/sha1.h b/sha1.h
index 0f4964f8f61c..d5f336eaab04 100644
--- a/sha1.h
+++ b/sha1.h
@@ -44,6 +44,9 @@ extern "C" {
#define sha1_init nettle_sha1_init
#define sha1_update nettle_sha1_update
#define sha1_digest nettle_sha1_digest
+#define sha1_block_init nettle_sha1_block_init
+#define sha1_block_update nettle_sha1_block_update
+#define sha1_block_digest nettle_sha1_block_digest
/* SHA1 */
@@ -55,12 +58,16 @@ extern "C" {
/* Digest is kept internally as 5 32-bit words. */
#define _SHA1_DIGEST_LENGTH 5
-struct sha1_ctx
+struct sha1_state
{
uint32_t state[_SHA1_DIGEST_LENGTH]; /* State variables */
uint64_t count; /* 64-bit block count */
- unsigned int index; /* index into buffer */
- uint8_t block[SHA1_BLOCK_SIZE]; /* SHA1 data buffer */
+};
+
+struct sha1_ctx
+{
+ struct sha1_state state;
+ BLOCK_CTX(SHA1_BLOCK_SIZE);
};
void
@@ -76,6 +83,22 @@ sha1_digest(struct sha1_ctx *ctx,
size_t length,
uint8_t *digest);
+void
+sha1_block_init(struct sha1_state *state,
+ struct block_ctx *bctx);
+
+void
+sha1_block_update(struct sha1_state *state,
+ struct block_ctx *bctx,
+ size_t length,
+ const uint8_t *data);
+
+void
+sha1_block_digest(struct sha1_state *state,
+ struct block_ctx *bctx,
+ size_t length,
+ uint8_t *digest);
+
/* Internal compression function. STATE points to 5 uint32_t words,
and DATA points to 64 bytes of input data, possibly unaligned. */
void
--
2.20.1
_______________________________________________
nettle-bugs mailing list
[email protected]
http://lists.lysator.liu.se/mailman/listinfo/nettle-bugs