Signed-off-by: Dmitry Eremin-Solenikov dbaryshkov@gmail.com --- nettle-meta.h | 1 + ripemd160-meta.c | 3 +++ ripemd160.c | 50 ++++++++++++++++++++++++++++++++++++------------ ripemd160.h | 29 +++++++++++++++++++++++++--- 4 files changed, 68 insertions(+), 15 deletions(-)
diff --git a/nettle-meta.h b/nettle-meta.h index 770b1953ca5d..79a9900cbdfa 100644 --- a/nettle-meta.h +++ b/nettle-meta.h @@ -182,6 +182,7 @@ extern const struct nettle_hash nettle_sha3_384; 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;
struct nettle_aead { diff --git a/ripemd160-meta.c b/ripemd160-meta.c index c1860b728599..bfa0b080767e 100644 --- a/ripemd160-meta.c +++ b/ripemd160-meta.c @@ -39,3 +39,6 @@
const struct nettle_hash nettle_ripemd160 = _NETTLE_HASH(ripemd160, RIPEMD160); + +const struct nettle_bctx_hash nettle_bctx_ripemd160 += _NETTLE_BLOCK_HASH(ripemd160, RIPEMD160); diff --git a/ripemd160.c b/ripemd160.c index 56ffee33cd6d..ee59684bd834 100644 --- a/ripemd160.c +++ b/ripemd160.c @@ -156,7 +156,7 @@ */
void -ripemd160_init(struct ripemd160_ctx *ctx) +ripemd160_block_init(struct ripemd160_state *state, struct block_ctx *bctx) { static const uint32_t iv[_RIPEMD160_DIGEST_LENGTH] = { @@ -166,9 +166,15 @@ ripemd160_init(struct ripemd160_ctx *ctx) 0x10325476, 0xC3D2E1F0, }; - memcpy(ctx->state, iv, sizeof(ctx->state)); - ctx->count = 0; - ctx->index = 0; + memcpy(state->state, iv, sizeof(state->state)); + state->count = 0; + bctx->index = 0; +} + +void +ripemd160_init(struct ripemd160_ctx *ctx) +{ + return ripemd160_block_init(&ctx->state, (struct block_ctx *)&ctx->block); }
#define COMPRESS(ctx, data) (_nettle_ripemd160_compress((ctx)->state, (data))) @@ -179,25 +185,45 @@ ripemd160_init(struct ripemd160_ctx *ctx) void ripemd160_update(struct ripemd160_ctx *ctx, size_t length, const uint8_t *data) { - MD_UPDATE(ctx, length, data, COMPRESS, ctx->count++); + MD_BLOCK_UPDATE(&ctx->state, &ctx->block, RIPEMD160_BLOCK_SIZE, length, data, COMPRESS, ctx->state.count++); +} + +void +ripemd160_block_update(struct ripemd160_state *state, + struct block_ctx *bctx, + size_t length, + const uint8_t *data) +{ + MD_BLOCK_UPDATE(state, bctx, RIPEMD160_BLOCK_SIZE, length, data, COMPRESS, state->count++); }
void -ripemd160_digest(struct ripemd160_ctx *ctx, size_t length, uint8_t *digest) +ripemd160_block_digest(struct ripemd160_state *state, + struct block_ctx *bctx, + size_t length, + uint8_t *digest) { uint64_t bit_count;
assert(length <= RIPEMD160_DIGEST_SIZE);
- MD_PAD(ctx, 8, COMPRESS); + MD_BLOCK_PAD(state, bctx, RIPEMD160_BLOCK_SIZE, 8, COMPRESS);
/* There are 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 */ - LE_WRITE_UINT64(ctx->block + 56, bit_count); - _nettle_ripemd160_compress(ctx->state, ctx->block); + LE_WRITE_UINT64(bctx->buffer + 56, bit_count); + _nettle_ripemd160_compress(state->state, bctx->buffer);
- _nettle_write_le32(length, digest, ctx->state); - ripemd160_init(ctx); + _nettle_write_le32(length, digest, state->state); + ripemd160_block_init(state, bctx); +} + +void +ripemd160_digest(struct ripemd160_ctx *ctx, + size_t length, + uint8_t *digest) +{ + return ripemd160_block_digest(&ctx->state, (struct block_ctx *)&ctx->block, length, digest); } diff --git a/ripemd160.h b/ripemd160.h index 238c5ee7a44c..40671a29a5a6 100644 --- a/ripemd160.h +++ b/ripemd160.h @@ -44,6 +44,9 @@ extern "C" { #define ripemd160_init nettle_ripemd160_init #define ripemd160_update nettle_ripemd160_update #define ripemd160_digest nettle_ripemd160_digest +#define ripemd160_block_init nettle_ripemd160_block_init +#define ripemd160_block_update nettle_ripemd160_block_update +#define ripemd160_block_digest nettle_ripemd160_block_digest
/* RIPEMD160 */
@@ -55,12 +58,16 @@ extern "C" { /* Digest is kept internally as 5 32-bit words. */ #define _RIPEMD160_DIGEST_LENGTH 5
-struct ripemd160_ctx +struct ripemd160_state { uint32_t state[_RIPEMD160_DIGEST_LENGTH]; uint64_t count; /* 64-bit block count */ - unsigned int index; - uint8_t block[RIPEMD160_BLOCK_SIZE]; +}; + +struct ripemd160_ctx +{ + struct ripemd160_state state; + BLOCK_CTX(RIPEMD160_BLOCK_SIZE); };
void @@ -76,6 +83,22 @@ ripemd160_digest(struct ripemd160_ctx *ctx, size_t length, uint8_t *digest);
+void +ripemd160_block_init(struct ripemd160_state *state, + struct block_ctx *bctx); + +void +ripemd160_block_update(struct ripemd160_state *state, + struct block_ctx *bctx, + size_t length, + const uint8_t *data); + +void +ripemd160_block_digest(struct ripemd160_state *state, + struct block_ctx *bctx, + size_t length, + uint8_t *digest); + #ifdef __cplusplus } #endif