diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-16 22:43:34 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-16 22:43:34 +0200 |
commit | 273abcbf664adc92ef3bc1d9752a2b571623ad52 (patch) | |
tree | ae4bdff4451ce36e4bb0568e7020d9cd9f150ba8 | |
parent | 1ac476bb8561f57703b84f090ed7a575fa512823 (diff) | |
download | busybox-w32-273abcbf664adc92ef3bc1d9752a2b571623ad52.tar.gz busybox-w32-273abcbf664adc92ef3bc1d9752a2b571623ad52.tar.bz2 busybox-w32-273abcbf664adc92ef3bc1d9752a2b571623ad52.zip |
shaN: small code shrink
function old new delta
sha512_hash 134 128 -6
sha1_hash 114 106 -8
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/md5.c | 61 | ||||
-rw-r--r-- | libbb/sha1.c | 115 | ||||
-rwxr-xr-x | testsuite/md5sum.tests | 14 |
3 files changed, 129 insertions, 61 deletions
diff --git a/libbb/md5.c b/libbb/md5.c index f192d0e47..cf3825a34 100644 --- a/libbb/md5.c +++ b/libbb/md5.c | |||
@@ -354,8 +354,8 @@ static void md5_hash_block(md5_ctx_t *ctx) | |||
354 | ctx->D = D; | 354 | ctx->D = D; |
355 | } | 355 | } |
356 | 356 | ||
357 | /* The size of filled part of ctx->buffer: */ | 357 | /* The first unused position in ctx->buffer: */ |
358 | #define BUFLEN(ctx) (((unsigned)ctx->total) & 63) | 358 | #define BUFPOS(ctx) (((unsigned)ctx->total) & 63) |
359 | 359 | ||
360 | /* Feed data through a temporary buffer to call md5_hash_aligned_block() | 360 | /* Feed data through a temporary buffer to call md5_hash_aligned_block() |
361 | * with chunks of data that are 4-byte aligned and a multiple of 64 bytes. | 361 | * with chunks of data that are 4-byte aligned and a multiple of 64 bytes. |
@@ -363,31 +363,52 @@ static void md5_hash_block(md5_ctx_t *ctx) | |||
363 | * bytes worth to pass on. Call md5_end() to flush this buffer. */ | 363 | * bytes worth to pass on. Call md5_end() to flush this buffer. */ |
364 | void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) | 364 | void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) |
365 | { | 365 | { |
366 | const char *buf = buffer; | 366 | #if 1 |
367 | unsigned buflen = BUFLEN(ctx); | 367 | /* Tiny bit smaller code */ |
368 | unsigned bufpos = BUFPOS(ctx); | ||
368 | 369 | ||
369 | /* RFC 1321 specifies the possible length of the file up to 2^64 bits. | 370 | /* RFC 1321 specifies the possible length of the file up to 2^64 bits. |
370 | * Here we only track the number of bytes. */ | 371 | * Here we only track the number of bytes. */ |
371 | ctx->total += len; | 372 | ctx->total += len; |
372 | 373 | ||
373 | /* Process all input. */ | ||
374 | while (1) { | 374 | while (1) { |
375 | unsigned i = 64 - buflen; | 375 | unsigned remaining = 64 - bufpos; |
376 | if (i > len) | 376 | if (remaining > len) |
377 | i = len; | 377 | remaining = len; |
378 | /* Copy data into aligned buffer. */ | 378 | /* Copy data into aligned buffer. */ |
379 | memcpy(ctx->buffer + buflen, buf, i); | 379 | memcpy(ctx->buffer + bufpos, buffer, remaining); |
380 | len -= i; | 380 | len -= remaining; |
381 | buf += i; | 381 | buffer = (const char *)buffer + remaining; |
382 | buflen += i; | 382 | bufpos += remaining; |
383 | /* clever way to do "if (buflen != 64) break; ... ; buflen = 0;" */ | 383 | /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */ |
384 | buflen -= 64; | 384 | bufpos -= 64; |
385 | if (buflen != 0) | 385 | if (bufpos != 0) |
386 | break; | 386 | break; |
387 | /* Buffer is filled up, process it. */ | 387 | /* Buffer is filled up, process it. */ |
388 | md5_hash_block(ctx); | 388 | md5_hash_block(ctx); |
389 | /*buflen = 0; - already is */ | 389 | /*bufpos = 0; - already is */ |
390 | } | 390 | } |
391 | #else | ||
392 | unsigned bufpos = BUFPOS(ctx); | ||
393 | unsigned add = 64 - bufpos; | ||
394 | |||
395 | /* RFC 1321 specifies the possible length of the file up to 2^64 bits. | ||
396 | * Here we only track the number of bytes. */ | ||
397 | ctx->total += len; | ||
398 | |||
399 | /* Hash whole blocks */ | ||
400 | while (len >= add) { | ||
401 | memcpy(ctx->buffer + bufpos, buffer, add); | ||
402 | buffer = (const char *)buffer + add; | ||
403 | len -= add; | ||
404 | add = 64; | ||
405 | bufpos = 0; | ||
406 | md5_hash_block(ctx); | ||
407 | } | ||
408 | |||
409 | /* Save last, partial blosk */ | ||
410 | memcpy(ctx->buffer + bufpos, buffer, len); | ||
411 | #endif | ||
391 | } | 412 | } |
392 | 413 | ||
393 | /* Process the remaining bytes in the buffer and put result from CTX | 414 | /* Process the remaining bytes in the buffer and put result from CTX |
@@ -399,13 +420,13 @@ void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) | |||
399 | { | 420 | { |
400 | uint64_t total; | 421 | uint64_t total; |
401 | unsigned i; | 422 | unsigned i; |
402 | unsigned buflen = BUFLEN(ctx); | 423 | unsigned bufpos = BUFPOS(ctx); |
403 | 424 | ||
404 | /* Pad data to block size. */ | 425 | /* Pad data to block size. */ |
405 | ctx->buffer[buflen++] = 0x80; | 426 | ctx->buffer[bufpos++] = 0x80; |
406 | memset(ctx->buffer + buflen, 0, 64 - buflen); | 427 | memset(ctx->buffer + bufpos, 0, 64 - bufpos); |
407 | 428 | ||
408 | if (buflen > 56) { | 429 | if (bufpos > 56) { |
409 | md5_hash_block(ctx); | 430 | md5_hash_block(ctx); |
410 | memset(ctx->buffer, 0, 64); | 431 | memset(ctx->buffer, 0, 64); |
411 | } | 432 | } |
diff --git a/libbb/sha1.c b/libbb/sha1.c index fac23c0ec..8c67d07bc 100644 --- a/libbb/sha1.c +++ b/libbb/sha1.c | |||
@@ -363,62 +363,117 @@ void FAST_FUNC sha512_begin(sha512_ctx_t *ctx) | |||
363 | /* Used also for sha256 */ | 363 | /* Used also for sha256 */ |
364 | void FAST_FUNC sha1_hash(sha1_ctx_t *ctx, const void *buffer, size_t len) | 364 | void FAST_FUNC sha1_hash(sha1_ctx_t *ctx, const void *buffer, size_t len) |
365 | { | 365 | { |
366 | unsigned in_buf = ctx->total64 & 63; | 366 | #if 0 |
367 | unsigned add = 64 - in_buf; | 367 | unsigned bufpos = ctx->total64 & 63; |
368 | unsigned add = 64 - bufpos; | ||
368 | 369 | ||
369 | ctx->total64 += len; | 370 | ctx->total64 += len; |
370 | 371 | ||
371 | while (len >= add) { /* transfer whole blocks while possible */ | 372 | /* Hash whole blocks */ |
372 | memcpy(ctx->wbuffer + in_buf, buffer, add); | 373 | while (len >= add) { |
374 | memcpy(ctx->wbuffer + bufpos, buffer, add); | ||
373 | buffer = (const char *)buffer + add; | 375 | buffer = (const char *)buffer + add; |
374 | len -= add; | 376 | len -= add; |
375 | add = 64; | 377 | add = 64; |
376 | in_buf = 0; | 378 | bufpos = 0; |
377 | ctx->process_block(ctx); | 379 | ctx->process_block(ctx); |
378 | } | 380 | } |
379 | 381 | ||
380 | memcpy(ctx->wbuffer + in_buf, buffer, len); | 382 | /* Save last, partial blosk */ |
383 | memcpy(ctx->wbuffer + bufpos, buffer, len); | ||
384 | #else | ||
385 | /* Tiny bit smaller code */ | ||
386 | unsigned bufpos = ctx->total64 & 63; | ||
387 | |||
388 | ctx->total64 += len; | ||
389 | |||
390 | while (1) { | ||
391 | unsigned remaining = 64 - bufpos; | ||
392 | if (remaining > len) | ||
393 | remaining = len; | ||
394 | /* Copy data into aligned buffer */ | ||
395 | memcpy(ctx->wbuffer + bufpos, buffer, remaining); | ||
396 | len -= remaining; | ||
397 | buffer = (const char *)buffer + remaining; | ||
398 | bufpos += remaining; | ||
399 | /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */ | ||
400 | bufpos -= 64; | ||
401 | if (bufpos != 0) | ||
402 | break; | ||
403 | /* Buffer is filled up, process it */ | ||
404 | ctx->process_block(ctx); | ||
405 | /*bufpos = 0; - already is */ | ||
406 | } | ||
407 | #endif | ||
381 | } | 408 | } |
382 | 409 | ||
383 | void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) | 410 | void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) |
384 | { | 411 | { |
385 | unsigned in_buf = ctx->total64[0] & 127; | 412 | #if 0 |
386 | unsigned add = 128 - in_buf; | 413 | unsigned bufpos = ctx->total64[0] & 127; |
414 | unsigned add = 128 - bufpos; | ||
387 | 415 | ||
388 | /* First increment the byte count. FIPS 180-2 specifies the possible | ||
389 | length of the file up to 2^128 _bits_. | ||
390 | We compute the number of _bytes_ and convert to bits later. */ | ||
391 | ctx->total64[0] += len; | 416 | ctx->total64[0] += len; |
392 | if (ctx->total64[0] < len) | 417 | if (ctx->total64[0] < len) |
393 | ctx->total64[1]++; | 418 | ctx->total64[1]++; |
394 | 419 | ||
395 | while (len >= add) { /* transfer whole blocks while possible */ | 420 | /* Hash whole blocks */ |
396 | memcpy(ctx->wbuffer + in_buf, buffer, add); | 421 | while (len >= add) { |
422 | memcpy(ctx->wbuffer + bufpos, buffer, add); | ||
397 | buffer = (const char *)buffer + add; | 423 | buffer = (const char *)buffer + add; |
398 | len -= add; | 424 | len -= add; |
399 | add = 128; | 425 | add = 128; |
400 | in_buf = 0; | 426 | bufpos = 0; |
401 | sha512_process_block128(ctx); | 427 | sha512_process_block128(ctx); |
402 | } | 428 | } |
403 | 429 | ||
404 | memcpy(ctx->wbuffer + in_buf, buffer, len); | 430 | /* Save last, partial blosk */ |
431 | memcpy(ctx->wbuffer + bufpos, buffer, len); | ||
432 | #else | ||
433 | unsigned bufpos = ctx->total64[0] & 127; | ||
434 | |||
435 | /* First increment the byte count. FIPS 180-2 specifies the possible | ||
436 | length of the file up to 2^128 _bits_. | ||
437 | We compute the number of _bytes_ and convert to bits later. */ | ||
438 | ctx->total64[0] += len; | ||
439 | if (ctx->total64[0] < len) | ||
440 | ctx->total64[1]++; | ||
441 | |||
442 | while (1) { | ||
443 | unsigned remaining = 128 - bufpos; | ||
444 | if (remaining > len) | ||
445 | remaining = len; | ||
446 | /* Copy data into aligned buffer. */ | ||
447 | memcpy(ctx->wbuffer + bufpos, buffer, remaining); | ||
448 | len -= remaining; | ||
449 | buffer = (const char *)buffer + remaining; | ||
450 | bufpos += remaining; | ||
451 | /* clever way to do "if (bufpos != 128) break; ... ; bufpos = 0;" */ | ||
452 | bufpos -= 128; | ||
453 | if (bufpos != 0) | ||
454 | break; | ||
455 | /* Buffer is filled up, process it. */ | ||
456 | sha512_process_block128(ctx); | ||
457 | /*bufpos = 0; - already is */ | ||
458 | } | ||
459 | #endif | ||
405 | } | 460 | } |
406 | 461 | ||
407 | 462 | ||
408 | /* Used also for sha256 */ | 463 | /* Used also for sha256 */ |
409 | void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) | 464 | void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) |
410 | { | 465 | { |
411 | unsigned pad, in_buf; | 466 | unsigned pad, bufpos; |
412 | 467 | ||
413 | in_buf = ctx->total64 & 63; | 468 | bufpos = ctx->total64 & 63; |
414 | /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ | 469 | /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ |
415 | ctx->wbuffer[in_buf++] = 0x80; | 470 | ctx->wbuffer[bufpos++] = 0x80; |
416 | 471 | ||
417 | /* This loop iterates either once or twice, no more, no less */ | 472 | /* This loop iterates either once or twice, no more, no less */ |
418 | while (1) { | 473 | while (1) { |
419 | pad = 64 - in_buf; | 474 | pad = 64 - bufpos; |
420 | memset(ctx->wbuffer + in_buf, 0, pad); | 475 | memset(ctx->wbuffer + bufpos, 0, pad); |
421 | in_buf = 0; | 476 | bufpos = 0; |
422 | /* Do we have enough space for the length count? */ | 477 | /* Do we have enough space for the length count? */ |
423 | if (pad >= 8) { | 478 | if (pad >= 8) { |
424 | /* Store the 64-bit counter of bits in the buffer in BE format */ | 479 | /* Store the 64-bit counter of bits in the buffer in BE format */ |
@@ -432,30 +487,30 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) | |||
432 | break; | 487 | break; |
433 | } | 488 | } |
434 | 489 | ||
435 | in_buf = (ctx->process_block == sha1_process_block64) ? 5 : 8; | 490 | bufpos = (ctx->process_block == sha1_process_block64) ? 5 : 8; |
436 | /* This way we do not impose alignment constraints on resbuf: */ | 491 | /* This way we do not impose alignment constraints on resbuf: */ |
437 | if (BB_LITTLE_ENDIAN) { | 492 | if (BB_LITTLE_ENDIAN) { |
438 | unsigned i; | 493 | unsigned i; |
439 | for (i = 0; i < in_buf; ++i) | 494 | for (i = 0; i < bufpos; ++i) |
440 | ctx->hash[i] = htonl(ctx->hash[i]); | 495 | ctx->hash[i] = htonl(ctx->hash[i]); |
441 | } | 496 | } |
442 | memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * in_buf); | 497 | memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * bufpos); |
443 | } | 498 | } |
444 | 499 | ||
445 | void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) | 500 | void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) |
446 | { | 501 | { |
447 | unsigned pad, in_buf; | 502 | unsigned pad, bufpos; |
448 | 503 | ||
449 | in_buf = ctx->total64[0] & 127; | 504 | bufpos = ctx->total64[0] & 127; |
450 | /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... | 505 | /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... |
451 | * (FIPS 180-2:5.1.2) | 506 | * (FIPS 180-2:5.1.2) |
452 | */ | 507 | */ |
453 | ctx->wbuffer[in_buf++] = 0x80; | 508 | ctx->wbuffer[bufpos++] = 0x80; |
454 | 509 | ||
455 | while (1) { | 510 | while (1) { |
456 | pad = 128 - in_buf; | 511 | pad = 128 - bufpos; |
457 | memset(ctx->wbuffer + in_buf, 0, pad); | 512 | memset(ctx->wbuffer + bufpos, 0, pad); |
458 | in_buf = 0; | 513 | bufpos = 0; |
459 | if (pad >= 16) { | 514 | if (pad >= 16) { |
460 | /* Store the 128-bit counter of bits in the buffer in BE format */ | 515 | /* Store the 128-bit counter of bits in the buffer in BE format */ |
461 | uint64_t t; | 516 | uint64_t t; |
diff --git a/testsuite/md5sum.tests b/testsuite/md5sum.tests index 5bbdb3b58..35ec67cb4 100755 --- a/testsuite/md5sum.tests +++ b/testsuite/md5sum.tests | |||
@@ -18,25 +18,17 @@ fi | |||
18 | sum="$1" | 18 | sum="$1" |
19 | expected="$2" | 19 | expected="$2" |
20 | 20 | ||
21 | mkdir testdir 2>/dev/null | ||
22 | |||
23 | result=`( | ||
24 | cd testdir || { echo "cannot cd testdir!" >&2; exit 1; } | ||
25 | |||
26 | text="The quick brown fox jumps over the lazy dog" | 21 | text="The quick brown fox jumps over the lazy dog" |
22 | text=`yes "$text" | head -c 9999` | ||
27 | 23 | ||
24 | result=`( | ||
28 | n=0 | 25 | n=0 |
29 | while test $n -le 999; do | 26 | while test $n -le 999; do |
30 | yes "$text" | head -c $n | "$sum" | 27 | echo "$text" | head -c $n | "$sum" |
31 | : $((n++)) | 28 | : $((n++)) |
32 | done | "$sum" | 29 | done | "$sum" |
33 | |||
34 | )` | 30 | )` |
35 | 31 | ||
36 | rm -rf testdir | ||
37 | |||
38 | FAILCOUNT=0 | ||
39 | |||
40 | if test x"$result" = x"$expected -"; then | 32 | if test x"$result" = x"$expected -"; then |
41 | echo "PASS: $sum" | 33 | echo "PASS: $sum" |
42 | exit 0 | 34 | exit 0 |