aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-09-30 22:22:04 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2020-09-30 22:22:04 +0200
commitabefc3c276ac2450f756c5232745a145bd14dbdf (patch)
tree8d219423dc6480bfc7a28be9b83fc5a14712e1e6
parentbd202a5ec15b82ba9562cdd81157e703349d8459 (diff)
downloadbusybox-w32-abefc3c276ac2450f756c5232745a145bd14dbdf.tar.gz
busybox-w32-abefc3c276ac2450f756c5232745a145bd14dbdf.tar.bz2
busybox-w32-abefc3c276ac2450f756c5232745a145bd14dbdf.zip
libbb: fold common64_hash() into its only user
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/hash_md5_sha.c75
1 files changed, 28 insertions, 47 deletions
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c
index 9db79ea8b..d8f210173 100644
--- a/libbb/hash_md5_sha.c
+++ b/libbb/hash_md5_sha.c
@@ -38,35 +38,6 @@ static ALWAYS_INLINE uint64_t rotl64(uint64_t x, unsigned n)
38 return (x << n) | (x >> (64 - n)); 38 return (x << n) | (x >> (64 - n));
39} 39}
40 40
41/* Feed data through a temporary buffer.
42 * The internal buffer remembers previous data until it has 64
43 * bytes worth to pass on.
44 */
45static void FAST_FUNC common64_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
46{
47 unsigned bufpos = ctx->total64 & 63;
48
49 ctx->total64 += len;
50
51 while (1) {
52 unsigned remaining = 64 - bufpos;
53 if (remaining > len)
54 remaining = len;
55 /* Copy data into aligned buffer */
56 memcpy(ctx->wbuffer + bufpos, buffer, remaining);
57 len -= remaining;
58 buffer = (const char *)buffer + remaining;
59 bufpos += remaining;
60 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
61 bufpos -= 64;
62 if (bufpos != 0)
63 break;
64 /* Buffer is filled up, process it */
65 ctx->process_block(ctx);
66 /*bufpos = 0; - already is */
67 }
68}
69
70/* Process the remaining bytes in the buffer */ 41/* Process the remaining bytes in the buffer */
71static void FAST_FUNC common64_end(md5_ctx_t *ctx, int swap_needed) 42static void FAST_FUNC common64_end(md5_ctx_t *ctx, int swap_needed)
72{ 43{
@@ -449,7 +420,29 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx)
449/* Used also for sha1 and sha256 */ 420/* Used also for sha1 and sha256 */
450void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) 421void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
451{ 422{
452 common64_hash(ctx, buffer, len); 423 unsigned bufpos = ctx->total64 & 63;
424
425 ctx->total64 += len;
426
427 while (1) {
428 unsigned remaining = 64 - bufpos;
429 if (remaining > len)
430 remaining = len;
431 /* Copy data into aligned buffer */
432 memcpy(ctx->wbuffer + bufpos, buffer, remaining);
433 len -= remaining;
434 buffer = (const char *)buffer + remaining;
435 bufpos += remaining;
436
437 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
438 bufpos -= 64;
439 if (bufpos != 0)
440 break;
441
442 /* Buffer is filled up, process it */
443 ctx->process_block(ctx);
444 /*bufpos = 0; - already is */
445 }
453} 446}
454 447
455/* Process the remaining bytes in the buffer and put result from CTX 448/* Process the remaining bytes in the buffer and put result from CTX
@@ -816,7 +809,7 @@ void FAST_FUNC sha512_begin(sha512_ctx_t *ctx)
816 int i; 809 int i;
817 /* Two extra iterations zero out ctx->total64[2] */ 810 /* Two extra iterations zero out ctx->total64[2] */
818 uint64_t *tp = ctx->total64; 811 uint64_t *tp = ctx->total64;
819 for (i = 0; i < 2+8; i++) 812 for (i = 0; i < 8 + 2; i++)
820 tp[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i]; 813 tp[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i];
821 /*ctx->total64[0] = ctx->total64[1] = 0; - already done */ 814 /*ctx->total64[0] = ctx->total64[1] = 0; - already done */
822} 815}
@@ -832,22 +825,7 @@ void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len)
832 ctx->total64[0] += len; 825 ctx->total64[0] += len;
833 if (ctx->total64[0] < len) 826 if (ctx->total64[0] < len)
834 ctx->total64[1]++; 827 ctx->total64[1]++;
835# if 0
836 remaining = 128 - bufpos;
837 828
838 /* Hash whole blocks */
839 while (len >= remaining) {
840 memcpy(ctx->wbuffer + bufpos, buffer, remaining);
841 buffer = (const char *)buffer + remaining;
842 len -= remaining;
843 remaining = 128;
844 bufpos = 0;
845 sha512_process_block128(ctx);
846 }
847
848 /* Save last, partial blosk */
849 memcpy(ctx->wbuffer + bufpos, buffer, len);
850# else
851 while (1) { 829 while (1) {
852 remaining = 128 - bufpos; 830 remaining = 128 - bufpos;
853 if (remaining > len) 831 if (remaining > len)
@@ -857,15 +835,16 @@ void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len)
857 len -= remaining; 835 len -= remaining;
858 buffer = (const char *)buffer + remaining; 836 buffer = (const char *)buffer + remaining;
859 bufpos += remaining; 837 bufpos += remaining;
838
860 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */ 839 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
861 bufpos -= 128; 840 bufpos -= 128;
862 if (bufpos != 0) 841 if (bufpos != 0)
863 break; 842 break;
843
864 /* Buffer is filled up, process it */ 844 /* Buffer is filled up, process it */
865 sha512_process_block128(ctx); 845 sha512_process_block128(ctx);
866 /*bufpos = 0; - already is */ 846 /*bufpos = 0; - already is */
867 } 847 }
868# endif
869} 848}
870#endif /* NEED_SHA512 */ 849#endif /* NEED_SHA512 */
871 850
@@ -1398,10 +1377,12 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len)
1398 bufpos++; 1377 bufpos++;
1399 remaining--; 1378 remaining--;
1400 } 1379 }
1380
1401 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */ 1381 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
1402 bufpos -= ctx->input_block_bytes; 1382 bufpos -= ctx->input_block_bytes;
1403 if (bufpos != 0) 1383 if (bufpos != 0)
1404 break; 1384 break;
1385
1405 /* Buffer is filled up, process it */ 1386 /* Buffer is filled up, process it */
1406 sha3_process_block72(ctx->state); 1387 sha3_process_block72(ctx->state);
1407 /*bufpos = 0; - already is */ 1388 /*bufpos = 0; - already is */