MD5: count bytes, not bits

MHD do not add bites, no need to count more precise than bytes
This commit is contained in:
Evgeny Grin (Karlson2k)
2019-04-16 10:59:02 +03:00
parent 5194dd1615
commit 7887edc2b4
2 changed files with 10 additions and 8 deletions
+9 -7
View File
@@ -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)
{
+1 -1
View File
@@ -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 */
};