diff options
-rw-r--r-- | libbb/md5.c | 37 | ||||
-rw-r--r-- | libbb/sha1.c | 18 |
2 files changed, 25 insertions, 30 deletions
diff --git a/libbb/md5.c b/libbb/md5.c index cf3825a..a1f0a92 100644 --- a/libbb/md5.c +++ b/libbb/md5.c @@ -418,31 +418,30 @@ void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) */ void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) { - uint64_t total; - unsigned i; unsigned bufpos = BUFPOS(ctx); - - /* Pad data to block size. */ + /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ ctx->buffer[bufpos++] = 0x80; - memset(ctx->buffer + bufpos, 0, 64 - bufpos); - if (bufpos > 56) { + /* This loop iterates either once or twice, no more, no less */ + while (1) { + unsigned remaining = 64 - bufpos; + memset(ctx->buffer + bufpos, 0, remaining); + /* Do we have enough space for the length count? */ + if (remaining >= 8) { + /* Store the 64-bit counter of bits in the buffer in BE format */ + uint64_t t = ctx->total << 3; + unsigned i; + for (i = 0; i < 8; i++) { + ctx->buffer[56 + i] = t; + t >>= 8; + } + } md5_hash_block(ctx); - memset(ctx->buffer, 0, 64); - } - - /* Put the 64-bit file length, expressed in *bits*, - * at the end of the buffer. - */ - total = ctx->total << 3; - for (i = 0; i < 8; i++) { - ctx->buffer[56 + i] = total; - total >>= 8; + if (remaining >= 8) + break; + bufpos = 0; } - /* Process last bytes. */ - md5_hash_block(ctx); - /* The MD5 result is in little endian byte order. * We (ab)use the fact that A-D are consecutive in memory. */ diff --git a/libbb/sha1.c b/libbb/sha1.c index 70efd58..3e61aff 100644 --- a/libbb/sha1.c +++ b/libbb/sha1.c @@ -462,17 +462,15 @@ void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) /* Used also for sha256 */ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) { - unsigned pad, bufpos; + unsigned bufpos = ctx->total64 & 63; - bufpos = ctx->total64 & 63; /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ ctx->wbuffer[bufpos++] = 0x80; /* This loop iterates either once or twice, no more, no less */ while (1) { - pad = 64 - bufpos; + unsigned pad = 64 - bufpos; memset(ctx->wbuffer + bufpos, 0, pad); - bufpos = 0; /* Do we have enough space for the length count? */ if (pad >= 8) { /* Store the 64-bit counter of bits in the buffer in BE format */ @@ -484,6 +482,7 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) ctx->process_block(ctx); if (pad >= 8) break; + bufpos = 0; } bufpos = (ctx->process_block == sha1_process_block64) ? 5 : 8; @@ -498,18 +497,14 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) { - unsigned pad, bufpos; + unsigned bufpos = ctx->total64[0] & 127; - bufpos = ctx->total64[0] & 127; - /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... - * (FIPS 180-2:5.1.2) - */ + /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... */ ctx->wbuffer[bufpos++] = 0x80; while (1) { - pad = 128 - bufpos; + unsigned pad = 128 - bufpos; memset(ctx->wbuffer + bufpos, 0, pad); - bufpos = 0; if (pad >= 16) { /* Store the 128-bit counter of bits in the buffer in BE format */ uint64_t t; @@ -523,6 +518,7 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) sha512_process_block128(ctx); if (pad >= 16) break; + bufpos = 0; } if (BB_LITTLE_ENDIAN) { |