aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2013-01-20 00:38:09 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2013-01-20 00:38:09 +0100
commit2cfcc9e9d74447cb770255d1d8cb6f3722df22ba (patch)
tree889818c8786569ad380a40cbe6839123141841f5
parent9980707efc3735574f89ca3fbc686374c6225e3e (diff)
downloadbusybox-w32-2cfcc9e9d74447cb770255d1d8cb6f3722df22ba.tar.gz
busybox-w32-2cfcc9e9d74447cb770255d1d8cb6f3722df22ba.tar.bz2
busybox-w32-2cfcc9e9d74447cb770255d1d8cb6f3722df22ba.zip
sha3: code shrink
function old new delta sha3_hash 155 101 -54 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/libbb.h4
-rw-r--r--libbb/hash_md5_sha.c91
2 files changed, 61 insertions, 34 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 606db7d0d..e52006020 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1641,7 +1641,7 @@ typedef struct sha3_ctx_t {
1641 unsigned bytes_queued; 1641 unsigned bytes_queued;
1642} sha3_ctx_t; 1642} sha3_ctx_t;
1643void md5_begin(md5_ctx_t *ctx) FAST_FUNC; 1643void md5_begin(md5_ctx_t *ctx) FAST_FUNC;
1644void md5_hash(md5_ctx_t *ctx, const void *data, size_t length) FAST_FUNC; 1644void md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
1645void md5_end(md5_ctx_t *ctx, void *resbuf) FAST_FUNC; 1645void md5_end(md5_ctx_t *ctx, void *resbuf) FAST_FUNC;
1646void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC; 1646void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC;
1647#define sha1_hash md5_hash 1647#define sha1_hash md5_hash
@@ -1654,7 +1654,7 @@ void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
1654void sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC; 1654void sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC;
1655void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC; 1655void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC;
1656void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; 1656void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
1657void sha3_end(sha3_ctx_t *ctx, uint8_t *resbuf) FAST_FUNC; 1657void sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC;
1658 1658
1659extern uint32_t *global_crc32_table; 1659extern uint32_t *global_crc32_table;
1660uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC; 1660uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c
index d23d4f639..b4d955e5a 100644
--- a/libbb/hash_md5_sha.c
+++ b/libbb/hash_md5_sha.c
@@ -56,7 +56,7 @@ static void FAST_FUNC common64_hash(md5_ctx_t *ctx, const void *buffer, size_t l
56 len -= remaining; 56 len -= remaining;
57 buffer = (const char *)buffer + remaining; 57 buffer = (const char *)buffer + remaining;
58 bufpos += remaining; 58 bufpos += remaining;
59 /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */ 59 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
60 bufpos -= 64; 60 bufpos -= 64;
61 if (bufpos != 0) 61 if (bufpos != 0)
62 break; 62 break;
@@ -839,7 +839,7 @@ void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len)
839 len -= remaining; 839 len -= remaining;
840 buffer = (const char *)buffer + remaining; 840 buffer = (const char *)buffer + remaining;
841 bufpos += remaining; 841 bufpos += remaining;
842 /* clever way to do "if (bufpos != 128) break; ... ; bufpos = 0;" */ 842 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
843 bufpos -= 128; 843 bufpos -= 128;
844 if (bufpos != 0) 844 if (bufpos != 0)
845 break; 845 break;
@@ -1079,63 +1079,90 @@ void FAST_FUNC sha3_begin(sha3_ctx_t *ctx)
1079 memset(ctx, 0, sizeof(*ctx)); 1079 memset(ctx, 0, sizeof(*ctx));
1080} 1080}
1081 1081
1082void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buf, size_t bytes) 1082void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len)
1083{ 1083{
1084 const uint8_t *data = buf; 1084#if SHA3_SMALL
1085 unsigned bytes_queued = ctx->bytes_queued; 1085 const uint8_t *data = buffer;
1086 unsigned bufpos = ctx->bytes_queued;
1087
1088 while (1) {
1089 unsigned remaining = SHA3_IBLK_BYTES - bufpos;
1090 if (remaining > len)
1091 remaining = len;
1092 len -= remaining;
1093 /* XOR data into buffer */
1094 while (remaining != 0) {
1095 uint8_t *buf = (uint8_t*)ctx->state;
1096 buf[bufpos] ^= *data++;
1097 bufpos++;
1098 remaining--;
1099 }
1100 /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
1101 bufpos -= SHA3_IBLK_BYTES;
1102 if (bufpos != 0)
1103 break;
1104 /* Buffer is filled up, process it */
1105 sha3_process_block72(ctx->state);
1106 /*bufpos = 0; - already is */
1107 }
1108 ctx->bytes_queued = bufpos + SHA3_IBLK_BYTES;
1109#else
1110 /* +50 bytes code size, but a bit faster because of long-sized XORs */
1111 const uint8_t *data = buffer;
1112 unsigned bufpos = ctx->bytes_queued;
1086 1113
1087 /* If already data in queue, continue queuing first */ 1114 /* If already data in queue, continue queuing first */
1088 while (bytes != 0 && bytes_queued != 0) { 1115 while (len != 0 && bufpos != 0) {
1089 uint8_t *buffer = (uint8_t*)ctx->state; 1116 uint8_t *buf = (uint8_t*)ctx->state;
1090 buffer[bytes_queued] ^= *data++; 1117 buf[bufpos] ^= *data++;
1091 bytes--; 1118 len--;
1092 bytes_queued++; 1119 bufpos++;
1093 if (bytes_queued == SHA3_IBLK_BYTES) { 1120 if (bufpos == SHA3_IBLK_BYTES) {
1094 sha3_process_block72(ctx->state); 1121 bufpos = 0;
1095 bytes_queued = 0; 1122 goto do_block;
1096 } 1123 }
1097 } 1124 }
1098 1125
1099 /* Absorb complete blocks */ 1126 /* Absorb complete blocks */
1100 while (bytes >= SHA3_IBLK_BYTES) { 1127 while (len >= SHA3_IBLK_BYTES) {
1101 /* XOR data onto beginning of state[]. 1128 /* XOR data onto beginning of state[].
1102 * We try to be efficient - operate on word at a time, not byte. 1129 * We try to be efficient - operate one word at a time, not byte.
1103 * Yet safe wrt unaligned access: can't just use "*(long*)data"... 1130 * Careful wrt unaligned access: can't just use "*(long*)data"!
1104 */ 1131 */
1105 unsigned count = SHA3_IBLK_BYTES / sizeof(long); 1132 unsigned count = SHA3_IBLK_BYTES / sizeof(long);
1106 long *buffer = (long*)ctx->state; 1133 long *buf = (long*)ctx->state;
1107 do { 1134 do {
1108 long v; 1135 long v;
1109 move_from_unaligned_long(v, (long*)data); 1136 move_from_unaligned_long(v, (long*)data);
1110 *buffer++ ^= v; 1137 *buf++ ^= v;
1111 data += sizeof(long); 1138 data += sizeof(long);
1112 } while (--count); 1139 } while (--count);
1113 1140 len -= SHA3_IBLK_BYTES;
1141 do_block:
1114 sha3_process_block72(ctx->state); 1142 sha3_process_block72(ctx->state);
1115
1116 bytes -= SHA3_IBLK_BYTES;
1117 } 1143 }
1118 1144
1119 /* Queue remaining data bytes */ 1145 /* Queue remaining data bytes */
1120 while (bytes != 0) { 1146 while (len != 0) {
1121 uint8_t *buffer = (uint8_t*)ctx->state; 1147 uint8_t *buf = (uint8_t*)ctx->state;
1122 buffer[bytes_queued] ^= *data++; 1148 buf[bufpos] ^= *data++;
1123 bytes_queued++; 1149 bufpos++;
1124 bytes--; 1150 len--;
1125 } 1151 }
1126 1152
1127 ctx->bytes_queued = bytes_queued; 1153 ctx->bytes_queued = bufpos;
1154#endif
1128} 1155}
1129 1156
1130void FAST_FUNC sha3_end(sha3_ctx_t *ctx, uint8_t *hashval) 1157void FAST_FUNC sha3_end(sha3_ctx_t *ctx, void *resbuf)
1131{ 1158{
1132 /* Padding */ 1159 /* Padding */
1133 uint8_t *buffer = (uint8_t*)ctx->state; 1160 uint8_t *buf = (uint8_t*)ctx->state;
1134 buffer[ctx->bytes_queued] ^= 1; 1161 buf[ctx->bytes_queued] ^= 1;
1135 buffer[SHA3_IBLK_BYTES - 1] ^= 0x80; 1162 buf[SHA3_IBLK_BYTES - 1] ^= 0x80;
1136 1163
1137 sha3_process_block72(ctx->state); 1164 sha3_process_block72(ctx->state);
1138 1165
1139 /* Output */ 1166 /* Output */
1140 memcpy(hashval, ctx->state, 64); 1167 memcpy(resbuf, ctx->state, 64);
1141} 1168}