bring md5 and sha1 names closer. no code changes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-10-17 03:21:51 +02:00
parent 36ab585f68
commit f6dacc23ff
3 changed files with 44 additions and 52 deletions

View File

@ -1539,8 +1539,8 @@ typedef struct md5_ctx_t {
uint32_t B; uint32_t B;
uint32_t C; uint32_t C;
uint32_t D; uint32_t D;
uint64_t total; uint64_t total64;
char buffer[64]; char wbuffer[64];
} md5_ctx_t; } md5_ctx_t;
#else #else
/* libbb/md5prime.c uses a bit different one: */ /* libbb/md5prime.c uses a bit different one: */

View File

@ -33,7 +33,7 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx)
ctx->B = 0xefcdab89; ctx->B = 0xefcdab89;
ctx->C = 0x98badcfe; ctx->C = 0x98badcfe;
ctx->D = 0x10325476; ctx->D = 0x10325476;
ctx->total = 0; ctx->total64 = 0;
} }
/* These are the four functions used in the four steps of the MD5 algorithm /* 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)))) #define rotl32(w, s) (((w) << (s)) | ((w) >> (32 - (s))))
/* Hash a single block, 64 bytes long and 4-byte aligned. */ /* Hash a single block, 64 bytes long and 4-byte aligned */
static void md5_hash_block(md5_ctx_t *ctx) static void md5_process_block64(md5_ctx_t *ctx)
{ {
#if MD5_SIZE_VS_SPEED > 0 #if MD5_SIZE_VS_SPEED > 0
/* Before we start, one word to the strange constants. /* Before we start, one word to the strange constants.
@ -95,7 +95,7 @@ static void md5_hash_block(md5_ctx_t *ctx)
}; };
# endif # endif
#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 A = ctx->A;
uint32_t B = ctx->B; uint32_t B = ctx->B;
@ -354,29 +354,41 @@ static void md5_hash_block(md5_ctx_t *ctx)
ctx->D = D; 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() /* 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. * 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 * This function's internal buffer remembers previous data until it has 64
* bytes worth to pass on. Call md5_end() to flush this buffer. */ * 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) void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
{ {
#if 1 unsigned bufpos = ctx->total64 & 63;
/* Tiny bit smaller code */ unsigned remaining;
unsigned bufpos = BUFPOS(ctx);
/* RFC 1321 specifies the possible length of the file up to 2^64 bits. /* RFC 1321 specifies the possible length of the file up to 2^64 bits.
* Here we only track the number of bytes. */ * 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) { while (1) {
unsigned remaining = 64 - bufpos; remaining = 64 - bufpos;
if (remaining > len) if (remaining > len)
remaining = len; remaining = len;
/* Copy data into aligned buffer. */ /* Copy data into aligned buffer */
memcpy(ctx->buffer + bufpos, buffer, remaining); memcpy(ctx->wbuffer + bufpos, buffer, remaining);
len -= remaining; len -= remaining;
buffer = (const char *)buffer + remaining; buffer = (const char *)buffer + remaining;
bufpos += remaining; bufpos += remaining;
@ -384,30 +396,10 @@ void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
bufpos -= 64; bufpos -= 64;
if (bufpos != 0) if (bufpos != 0)
break; break;
/* Buffer is filled up, process it. */ /* Buffer is filled up, process it */
md5_hash_block(ctx); md5_process_block64(ctx);
/*bufpos = 0; - already is */ /*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 #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) 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... */ /* 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 */ /* This loop iterates either once or twice, no more, no less */
while (1) { while (1) {
unsigned remaining = 64 - bufpos; 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? */ /* Do we have enough space for the length count? */
if (remaining >= 8) { if (remaining >= 8) {
/* Store the 64-bit counter of bits in the buffer in BE format */ /* 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; unsigned i;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
ctx->buffer[56 + i] = t; ctx->wbuffer[56 + i] = t;
t >>= 8; t >>= 8;
} }
} }
md5_hash_block(ctx); md5_process_block64(ctx);
if (remaining >= 8) if (remaining >= 8)
break; break;
bufpos = 0; bufpos = 0;

View File

@ -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 */ /* This loop iterates either once or twice, no more, no less */
while (1) { while (1) {
unsigned pad = 64 - bufpos; unsigned remaining = 64 - bufpos;
memset(ctx->wbuffer + bufpos, 0, pad); memset(ctx->wbuffer + bufpos, 0, remaining);
/* Do we have enough space for the length count? */ /* 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 */ /* Store the 64-bit counter of bits in the buffer in BE format */
uint64_t t = ctx->total64 << 3; uint64_t t = ctx->total64 << 3;
t = hton64(t); 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; *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t;
} }
ctx->process_block(ctx); ctx->process_block(ctx);
if (pad >= 8) if (remaining >= 8)
break; break;
bufpos = 0; bufpos = 0;
} }
@ -503,9 +503,9 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
ctx->wbuffer[bufpos++] = 0x80; ctx->wbuffer[bufpos++] = 0x80;
while (1) { while (1) {
unsigned pad = 128 - bufpos; unsigned remaining = 128 - bufpos;
memset(ctx->wbuffer + bufpos, 0, pad); memset(ctx->wbuffer + bufpos, 0, remaining);
if (pad >= 16) { if (remaining >= 16) {
/* Store the 128-bit counter of bits in the buffer in BE format */ /* Store the 128-bit counter of bits in the buffer in BE format */
uint64_t t; uint64_t t;
t = ctx->total64[0] << 3; 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; *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t;
} }
sha512_process_block128(ctx); sha512_process_block128(ctx);
if (pad >= 16) if (remaining >= 16)
break; break;
bufpos = 0; bufpos = 0;
} }