aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libbb.h4
-rw-r--r--libbb/md5.c76
-rw-r--r--libbb/sha1.c16
3 files changed, 44 insertions, 52 deletions
diff --git a/include/libbb.h b/include/libbb.h
index d05b2d48a..dec4e3af5 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1539,8 +1539,8 @@ typedef struct md5_ctx_t {
uint32_t B;
uint32_t C;
uint32_t D;
- uint64_t total;
- char buffer[64];
+ uint64_t total64;
+ char wbuffer[64];
} md5_ctx_t;
#else
/* libbb/md5prime.c uses a bit different one: */
diff --git a/libbb/md5.c b/libbb/md5.c
index a1f0a923f..f5d083e3f 100644
--- a/libbb/md5.c
+++ b/libbb/md5.c
@@ -33,7 +33,7 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx)
ctx->B = 0xefcdab89;
ctx->C = 0x98badcfe;
ctx->D = 0x10325476;
- ctx->total = 0;
+ ctx->total64 = 0;
}
/* These are the four functions used in the four steps of the MD5 algorithm
@@ -48,8 +48,8 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx)
#define rotl32(w, s) (((w) << (s)) | ((w) >> (32 - (s))))
-/* Hash a single block, 64 bytes long and 4-byte aligned. */
-static void md5_hash_block(md5_ctx_t *ctx)
+/* Hash a single block, 64 bytes long and 4-byte aligned */
+static void md5_process_block64(md5_ctx_t *ctx)
{
#if MD5_SIZE_VS_SPEED > 0
/* Before we start, one word to the strange constants.
@@ -95,7 +95,7 @@ static void md5_hash_block(md5_ctx_t *ctx)
};
# endif
#endif
- const uint32_t *words = (const void*) ctx->buffer;
+ const uint32_t *words = (const void*) ctx->wbuffer;
uint32_t A = ctx->A;
uint32_t B = ctx->B;
@@ -354,29 +354,41 @@ static void md5_hash_block(md5_ctx_t *ctx)
ctx->D = D;
}
-/* The first unused position in ctx->buffer: */
-#define BUFPOS(ctx) (((unsigned)ctx->total) & 63)
-
/* Feed data through a temporary buffer to call md5_hash_aligned_block()
* with chunks of data that are 4-byte aligned and a multiple of 64 bytes.
* This function's internal buffer remembers previous data until it has 64
* bytes worth to pass on. Call md5_end() to flush this buffer. */
void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
{
-#if 1
- /* Tiny bit smaller code */
- unsigned bufpos = BUFPOS(ctx);
+ unsigned bufpos = ctx->total64 & 63;
+ unsigned remaining;
/* RFC 1321 specifies the possible length of the file up to 2^64 bits.
* Here we only track the number of bytes. */
- ctx->total += len;
+ ctx->total64 += len;
+#if 0
+ remaining = 64 - bufpos;
+ /* Hash whole blocks */
+ while (len >= remaining) {
+ memcpy(ctx->wbuffer + bufpos, buffer, remaining);
+ buffer = (const char *)buffer + remaining;
+ len -= remaining;
+ remaining = 64;
+ bufpos = 0;
+ md5_process_block64(ctx);
+ }
+
+ /* Save last, partial blosk */
+ memcpy(ctx->wbuffer + bufpos, buffer, len);
+#else
+ /* Tiny bit smaller code */
while (1) {
- unsigned remaining = 64 - bufpos;
+ remaining = 64 - bufpos;
if (remaining > len)
remaining = len;
- /* Copy data into aligned buffer. */
- memcpy(ctx->buffer + bufpos, buffer, remaining);
+ /* Copy data into aligned buffer */
+ memcpy(ctx->wbuffer + bufpos, buffer, remaining);
len -= remaining;
buffer = (const char *)buffer + remaining;
bufpos += remaining;
@@ -384,30 +396,10 @@ void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
bufpos -= 64;
if (bufpos != 0)
break;
- /* Buffer is filled up, process it. */
- md5_hash_block(ctx);
+ /* Buffer is filled up, process it */
+ md5_process_block64(ctx);
/*bufpos = 0; - already is */
}
-#else
- unsigned bufpos = BUFPOS(ctx);
- unsigned add = 64 - bufpos;
-
- /* RFC 1321 specifies the possible length of the file up to 2^64 bits.
- * Here we only track the number of bytes. */
- ctx->total += len;
-
- /* Hash whole blocks */
- while (len >= add) {
- memcpy(ctx->buffer + bufpos, buffer, add);
- buffer = (const char *)buffer + add;
- len -= add;
- add = 64;
- bufpos = 0;
- md5_hash_block(ctx);
- }
-
- /* Save last, partial blosk */
- memcpy(ctx->buffer + bufpos, buffer, len);
#endif
}
@@ -418,25 +410,25 @@ 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)
{
- unsigned bufpos = BUFPOS(ctx);
+ unsigned bufpos = ctx->total64 & 63;
/* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
- ctx->buffer[bufpos++] = 0x80;
+ ctx->wbuffer[bufpos++] = 0x80;
/* This loop iterates either once or twice, no more, no less */
while (1) {
unsigned remaining = 64 - bufpos;
- memset(ctx->buffer + bufpos, 0, remaining);
+ memset(ctx->wbuffer + 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;
+ uint64_t t = ctx->total64 << 3;
unsigned i;
for (i = 0; i < 8; i++) {
- ctx->buffer[56 + i] = t;
+ ctx->wbuffer[56 + i] = t;
t >>= 8;
}
}
- md5_hash_block(ctx);
+ md5_process_block64(ctx);
if (remaining >= 8)
break;
bufpos = 0;
diff --git a/libbb/sha1.c b/libbb/sha1.c
index 3e61aff6d..d79291148 100644
--- a/libbb/sha1.c
+++ b/libbb/sha1.c
@@ -469,10 +469,10 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
/* This loop iterates either once or twice, no more, no less */
while (1) {
- unsigned pad = 64 - bufpos;
- memset(ctx->wbuffer + bufpos, 0, pad);
+ unsigned remaining = 64 - bufpos;
+ memset(ctx->wbuffer + bufpos, 0, remaining);
/* Do we have enough space for the length count? */
- if (pad >= 8) {
+ if (remaining >= 8) {
/* Store the 64-bit counter of bits in the buffer in BE format */
uint64_t t = ctx->total64 << 3;
t = hton64(t);
@@ -480,7 +480,7 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
*(uint64_t *) (&ctx->wbuffer[64 - 8]) = t;
}
ctx->process_block(ctx);
- if (pad >= 8)
+ if (remaining >= 8)
break;
bufpos = 0;
}
@@ -503,9 +503,9 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
ctx->wbuffer[bufpos++] = 0x80;
while (1) {
- unsigned pad = 128 - bufpos;
- memset(ctx->wbuffer + bufpos, 0, pad);
- if (pad >= 16) {
+ unsigned remaining = 128 - bufpos;
+ memset(ctx->wbuffer + bufpos, 0, remaining);
+ if (remaining >= 16) {
/* Store the 128-bit counter of bits in the buffer in BE format */
uint64_t t;
t = ctx->total64[0] << 3;
@@ -516,7 +516,7 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
*(uint64_t *) (&ctx->wbuffer[128 - 16]) = t;
}
sha512_process_block128(ctx);
- if (pad >= 16)
+ if (remaining >= 16)
break;
bufpos = 0;
}