diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-17 03:00:36 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-17 03:00:36 +0200 |
commit | 36ab585f68295487a0973bde86bcb0ab7577a8ff (patch) | |
tree | 86faf83217ec48ff6135104994313ee03ec3e27d | |
parent | a971a192e8af4279fb384be9ff0f0e8387b229cb (diff) | |
download | busybox-w32-36ab585f68295487a0973bde86bcb0ab7577a8ff.tar.gz busybox-w32-36ab585f68295487a0973bde86bcb0ab7577a8ff.tar.bz2 busybox-w32-36ab585f68295487a0973bde86bcb0ab7577a8ff.zip |
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.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 cf3825a34..a1f0a923f 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) | |||
418 | */ | 418 | */ |
419 | void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) | 419 | void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) |
420 | { | 420 | { |
421 | uint64_t total; | ||
422 | unsigned i; | ||
423 | unsigned bufpos = BUFPOS(ctx); | 421 | unsigned bufpos = BUFPOS(ctx); |
424 | 422 | /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ | |
425 | /* Pad data to block size. */ | ||
426 | ctx->buffer[bufpos++] = 0x80; | 423 | ctx->buffer[bufpos++] = 0x80; |
427 | memset(ctx->buffer + bufpos, 0, 64 - bufpos); | ||
428 | 424 | ||
429 | if (bufpos > 56) { | 425 | /* This loop iterates either once or twice, no more, no less */ |
426 | while (1) { | ||
427 | unsigned remaining = 64 - bufpos; | ||
428 | memset(ctx->buffer + bufpos, 0, remaining); | ||
429 | /* Do we have enough space for the length count? */ | ||
430 | if (remaining >= 8) { | ||
431 | /* Store the 64-bit counter of bits in the buffer in BE format */ | ||
432 | uint64_t t = ctx->total << 3; | ||
433 | unsigned i; | ||
434 | for (i = 0; i < 8; i++) { | ||
435 | ctx->buffer[56 + i] = t; | ||
436 | t >>= 8; | ||
437 | } | ||
438 | } | ||
430 | md5_hash_block(ctx); | 439 | md5_hash_block(ctx); |
431 | memset(ctx->buffer, 0, 64); | 440 | if (remaining >= 8) |
432 | } | 441 | break; |
433 | 442 | bufpos = 0; | |
434 | /* Put the 64-bit file length, expressed in *bits*, | ||
435 | * at the end of the buffer. | ||
436 | */ | ||
437 | total = ctx->total << 3; | ||
438 | for (i = 0; i < 8; i++) { | ||
439 | ctx->buffer[56 + i] = total; | ||
440 | total >>= 8; | ||
441 | } | 443 | } |
442 | 444 | ||
443 | /* Process last bytes. */ | ||
444 | md5_hash_block(ctx); | ||
445 | |||
446 | /* The MD5 result is in little endian byte order. | 445 | /* The MD5 result is in little endian byte order. |
447 | * We (ab)use the fact that A-D are consecutive in memory. | 446 | * We (ab)use the fact that A-D are consecutive in memory. |
448 | */ | 447 | */ |
diff --git a/libbb/sha1.c b/libbb/sha1.c index 70efd581b..3e61aff6d 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) | |||
462 | /* Used also for sha256 */ | 462 | /* Used also for sha256 */ |
463 | void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) | 463 | void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) |
464 | { | 464 | { |
465 | unsigned pad, bufpos; | 465 | unsigned bufpos = ctx->total64 & 63; |
466 | 466 | ||
467 | bufpos = ctx->total64 & 63; | ||
468 | /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ | 467 | /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ |
469 | ctx->wbuffer[bufpos++] = 0x80; | 468 | ctx->wbuffer[bufpos++] = 0x80; |
470 | 469 | ||
471 | /* This loop iterates either once or twice, no more, no less */ | 470 | /* This loop iterates either once or twice, no more, no less */ |
472 | while (1) { | 471 | while (1) { |
473 | pad = 64 - bufpos; | 472 | unsigned pad = 64 - bufpos; |
474 | memset(ctx->wbuffer + bufpos, 0, pad); | 473 | memset(ctx->wbuffer + bufpos, 0, pad); |
475 | bufpos = 0; | ||
476 | /* Do we have enough space for the length count? */ | 474 | /* Do we have enough space for the length count? */ |
477 | if (pad >= 8) { | 475 | if (pad >= 8) { |
478 | /* Store the 64-bit counter of bits in the buffer in BE format */ | 476 | /* 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) | |||
484 | ctx->process_block(ctx); | 482 | ctx->process_block(ctx); |
485 | if (pad >= 8) | 483 | if (pad >= 8) |
486 | break; | 484 | break; |
485 | bufpos = 0; | ||
487 | } | 486 | } |
488 | 487 | ||
489 | bufpos = (ctx->process_block == sha1_process_block64) ? 5 : 8; | 488 | bufpos = (ctx->process_block == sha1_process_block64) ? 5 : 8; |
@@ -498,18 +497,14 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) | |||
498 | 497 | ||
499 | void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) | 498 | void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) |
500 | { | 499 | { |
501 | unsigned pad, bufpos; | 500 | unsigned bufpos = ctx->total64[0] & 127; |
502 | 501 | ||
503 | bufpos = ctx->total64[0] & 127; | 502 | /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... */ |
504 | /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... | ||
505 | * (FIPS 180-2:5.1.2) | ||
506 | */ | ||
507 | ctx->wbuffer[bufpos++] = 0x80; | 503 | ctx->wbuffer[bufpos++] = 0x80; |
508 | 504 | ||
509 | while (1) { | 505 | while (1) { |
510 | pad = 128 - bufpos; | 506 | unsigned pad = 128 - bufpos; |
511 | memset(ctx->wbuffer + bufpos, 0, pad); | 507 | memset(ctx->wbuffer + bufpos, 0, pad); |
512 | bufpos = 0; | ||
513 | if (pad >= 16) { | 508 | if (pad >= 16) { |
514 | /* Store the 128-bit counter of bits in the buffer in BE format */ | 509 | /* Store the 128-bit counter of bits in the buffer in BE format */ |
515 | uint64_t t; | 510 | uint64_t t; |
@@ -523,6 +518,7 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) | |||
523 | sha512_process_block128(ctx); | 518 | sha512_process_block128(ctx); |
524 | if (pad >= 16) | 519 | if (pad >= 16) |
525 | break; | 520 | break; |
521 | bufpos = 0; | ||
526 | } | 522 | } |
527 | 523 | ||
528 | if (BB_LITTLE_ENDIAN) { | 524 | if (BB_LITTLE_ENDIAN) { |