From 910c782b2fd6e3390ca2910c36561842058caff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sat, 12 Jul 2014 22:10:57 +0000 Subject: [PATCH] In the md4 code, use a 64-bit counter to avoid a manual carry operation. --- include/cryb/md4.h | 2 +- lib/digest/md4.c | 21 +++++---------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/include/cryb/md4.h b/include/cryb/md4.h index 33eb936..32cab6c 100644 --- a/include/cryb/md4.h +++ b/include/cryb/md4.h @@ -51,7 +51,7 @@ extern digest_algorithm md4_digest; */ typedef struct { - uint32_t total[2]; /*!< number of bytes processed */ + uint64_t total; /*!< number of bytes processed */ uint32_t state[4]; /*!< intermediate digest state */ uint8_t buffer[64]; /*!< data block being processed */ } diff --git a/lib/digest/md4.c b/lib/digest/md4.c index 806d7a6..6bf3b38 100644 --- a/lib/digest/md4.c +++ b/lib/digest/md4.c @@ -67,8 +67,7 @@ */ void md4_init( md4_ctx *ctx ) { - ctx->total[0] = 0; - ctx->total[1] = 0; + ctx->total = 0; ctx->state[0] = 0x67452301; ctx->state[1] = 0xEFCDAB89; @@ -190,14 +189,10 @@ void md4_update( md4_ctx *ctx, const void *input, int ilen ) if( ilen <= 0 ) return; - left = ctx->total[0] & 0x3F; + left = ctx->total & 0x3F; fill = 64 - left; - ctx->total[0] += ilen; - ctx->total[0] &= 0xFFFFFFFF; - - if( ctx->total[0] < (uint32_t) ilen ) - ctx->total[1]++; + ctx->total += ilen; if( left && ilen >= fill ) { @@ -235,17 +230,11 @@ static const uint8_t md4_padding[64] = void md4_final( md4_ctx *ctx, uint8_t *output ) { uint32_t last, padn; - uint32_t high, low; uint8_t msglen[8]; - high = ( ctx->total[0] >> 29 ) - | ( ctx->total[1] << 3 ); - low = ( ctx->total[0] << 3 ); + le64enc(msglen, ctx->total << 3); - PUT_ULONG_LE( low, msglen, 0 ); - PUT_ULONG_LE( high, msglen, 4 ); - - last = ctx->total[0] & 0x3F; + last = ctx->total & 0x3F; padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); md4_update( ctx, md4_padding, padn );