summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-10-17 01:00:36 (GMT)
committer Denys Vlasenko <vda.linux@googlemail.com>2010-10-17 01:00:36 (GMT)
commit36ab585f68295487a0973bde86bcb0ab7577a8ff (patch)
tree86faf83217ec48ff6135104994313ee03ec3e27d
parenta971a192e8af4279fb384be9ff0f0e8387b229cb (diff)
downloadbusybox-36ab585f68295487a0973bde86bcb0ab7577a8ff.tar.gz
busybox-36ab585f68295487a0973bde86bcb0ab7577a8ff.tar.bz2
md5: code shrink
function old new delta md5_end 125 104 -21 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/md5.c37
-rw-r--r--libbb/sha1.c18
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) {