From 7887edc2b44c840bf2cbe60cb1bf02eceab80dd3 Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Mon, 15 Apr 2019 22:53:29 +0300 Subject: [PATCH] MD5: count bytes, not bits MHD do not add bites, no need to count more precise than bytes --- src/microhttpd/md5.c | 16 +++++++++------- src/microhttpd/md5.h | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/microhttpd/md5.c b/src/microhttpd/md5.c index a9e52935..867e0d3f 100644 --- a/src/microhttpd/md5.c +++ b/src/microhttpd/md5.c @@ -71,21 +71,23 @@ MD5Init (void *ctx_) static void MD5Pad (struct MD5Context *ctx) { - uint8_t count[8]; + uint8_t count_le[8]; size_t padlen; + uint64_t count_bits; mhd_assert (ctx != NULL); /* Convert count to 8 bytes in little endian order. */ - PUT_64BIT_LE(count, ctx->count); + count_bits = ctx->count << 3; + PUT_64BIT_LE(count_le, count_bits); /* Pad out to 56 mod 64. */ padlen = MD5_BLOCK_SIZE - - ((ctx->count >> 3) & (MD5_BLOCK_SIZE - 1)); + ((ctx->count) & (MD5_BLOCK_SIZE - 1)); if (padlen < 1 + 8) padlen += MD5_BLOCK_SIZE; MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ - MD5Update(ctx, count, 8); + MD5Update(ctx, count_le, 8); } @@ -244,11 +246,11 @@ MD5Update (void *ctx_, mhd_assert ((ctx != NULL) || (len == 0)); /* Check how many bytes we already have and how many more we need. */ - have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_SIZE - 1)); + have = (size_t)((ctx->count) & (MD5_BLOCK_SIZE - 1)); need = MD5_BLOCK_SIZE - have; - /* Update bitcount */ - ctx->count += (uint64_t)len << 3; + /* Update bytecount */ + ctx->count += (uint64_t)len; if (len >= need) { diff --git a/src/microhttpd/md5.h b/src/microhttpd/md5.h index 655b9f72..819d515d 100644 --- a/src/microhttpd/md5.h +++ b/src/microhttpd/md5.h @@ -27,7 +27,7 @@ struct MD5Context { uint32_t state[4]; /* state */ - uint64_t count; /* number of bits, mod 2^64 */ + uint64_t count; /* number of bytes, mod 2^64 */ uint8_t buffer[MD5_BLOCK_SIZE]; /* input buffer */ };