Signed-off-by: Dmitry Eremin-Solenikov dbaryshkov@gmail.com --- md5-meta.c | 3 +++ md5.c | 52 +++++++++++++++++++++++++++++++++++++-------------- md5.h | 29 +++++++++++++++++++++++++--- nettle-meta.h | 2 ++ 4 files changed, 69 insertions(+), 17 deletions(-)
diff --git a/md5-meta.c b/md5-meta.c index e4013edfd233..63b4a7438774 100644 --- a/md5-meta.c +++ b/md5-meta.c @@ -39,3 +39,6 @@
const struct nettle_hash nettle_md5 = _NETTLE_HASH(md5, MD5); + +const struct nettle_bctx_hash nettle_bctx_md5 += _NETTLE_BLOCK_HASH(md5, MD5); diff --git a/md5.c b/md5.c index cc009b4a8dc8..2876a3105108 100644 --- a/md5.c +++ b/md5.c @@ -47,7 +47,7 @@ #include "nettle-write.h"
void -md5_init(struct md5_ctx *ctx) +md5_block_init(struct md5_state *state, struct block_ctx *bctx) { const uint32_t iv[_MD5_DIGEST_LENGTH] = { @@ -56,9 +56,15 @@ md5_init(struct md5_ctx *ctx) 0x98badcfe, 0x10325476, }; - 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 +md5_init(struct md5_ctx *ctx) +{ + return md5_block_init(&ctx->state, (struct block_ctx *)&ctx->block); }
#define COMPRESS(ctx, data) (nettle_md5_compress((ctx)->state, (data))) @@ -68,26 +74,44 @@ md5_update(struct md5_ctx *ctx, size_t length, const uint8_t *data) { - MD_UPDATE(ctx, length, data, COMPRESS, ctx->count++); + MD_BLOCK_UPDATE(&ctx->state, &ctx->block, MD5_BLOCK_SIZE, length, data, COMPRESS, ctx->state.count++); }
void -md5_digest(struct md5_ctx *ctx, - size_t length, - uint8_t *digest) +md5_block_update(struct md5_state *state, + struct block_ctx *bctx, + size_t length, + const uint8_t *data) +{ + MD_BLOCK_UPDATE(state, bctx, MD5_BLOCK_SIZE, length, data, COMPRESS, state->count++); +} + +void +md5_block_digest(struct md5_state *state, + struct block_ctx *bctx, + size_t length, + uint8_t *digest) { uint64_t bit_count;
assert(length <= MD5_DIGEST_SIZE);
- MD_PAD(ctx, 8, COMPRESS); + MD_BLOCK_PAD(state, bctx, MD5_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);
- LE_WRITE_UINT64(ctx->block + (MD5_BLOCK_SIZE - 8), bit_count); - nettle_md5_compress(ctx->state, ctx->block); + LE_WRITE_UINT64(bctx->buffer + (MD5_BLOCK_SIZE - 8), bit_count); + nettle_md5_compress(state->state, bctx->buffer); + + _nettle_write_le32(length, digest, state->state); + md5_block_init(state, bctx); +}
- _nettle_write_le32(length, digest, ctx->state); - md5_init(ctx); +void +md5_digest(struct md5_ctx *ctx, + size_t length, + uint8_t *digest) +{ + return md5_block_digest(&ctx->state, (struct block_ctx *)&ctx->block, length, digest); } diff --git a/md5.h b/md5.h index e5ae228e8217..23738a8532c5 100644 --- a/md5.h +++ b/md5.h @@ -44,6 +44,9 @@ extern "C" { #define md5_init nettle_md5_init #define md5_update nettle_md5_update #define md5_digest nettle_md5_digest +#define md5_block_init nettle_md5_block_init +#define md5_block_update nettle_md5_block_update +#define md5_block_digest nettle_md5_block_digest
#define MD5_DIGEST_SIZE 16 #define MD5_BLOCK_SIZE 64 @@ -53,12 +56,16 @@ extern "C" { /* Digest is kept internally as 4 32-bit words. */ #define _MD5_DIGEST_LENGTH 4
-struct md5_ctx +struct md5_state { uint32_t state[_MD5_DIGEST_LENGTH]; uint64_t count; /* Block count */ - unsigned index; /* Into buffer */ - uint8_t block[MD5_BLOCK_SIZE]; /* Block buffer */ +}; + +struct md5_ctx +{ + struct md5_state state; + BLOCK_CTX(MD5_BLOCK_SIZE); };
void @@ -74,6 +81,22 @@ md5_digest(struct md5_ctx *ctx, size_t length, uint8_t *digest);
+void +md5_block_init(struct md5_state *state, + struct block_ctx *bctx); + +void +md5_block_update(struct md5_state *state, + struct block_ctx *bctx, + size_t length, + const uint8_t *data); + +void +md5_block_digest(struct md5_state *state, + struct block_ctx *bctx, + size_t length, + uint8_t *digest); + /* Internal compression function. STATE points to 4 uint32_t words, and DATA points to 64 bytes of input data, possibly unaligned. */ void diff --git a/nettle-meta.h b/nettle-meta.h index f21dcd9b344d..770b1953ca5d 100644 --- a/nettle-meta.h +++ b/nettle-meta.h @@ -181,6 +181,8 @@ extern const struct nettle_hash nettle_sha3_256; 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; + struct nettle_aead { const char *name;