1303 files changed, 32187 insertions, 57138 deletions
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index 3f743ac..7e7d8da 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c @@ -137,7 +137,7 @@ static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) #if MD5_SMALL > 0 /* Before we start, one word to the strange constants. They are defined in RFC 1321 as - T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64 + T[i] = (int)(2^32 * fabs(sin(i))), i=1..64 */ static const uint32_t C_array[] = { /* round 1 */ @@ -213,7 +213,7 @@ static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) case 2: temp += FH(B, C, D); break; - case 3: + default: /* case 3 */ temp += FI(B, C, D); } temp += words[(int) (*pp++)] + *pc++; @@ -277,10 +277,6 @@ static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) #else /* MD5_SMALL == 0 or 1 */ - uint32_t A_save = A; - uint32_t B_save = B; - uint32_t C_save = C; - uint32_t D_save = D; # if MD5_SMALL == 1 const uint32_t *pc; const char *pp; @@ -425,10 +421,10 @@ static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) # undef OP # endif /* Add checksum to the starting values */ - ctx->hash[0] = A_save + A; - ctx->hash[1] = B_save + B; - ctx->hash[2] = C_save + C; - ctx->hash[3] = D_save + D; + ctx->hash[0] += A; + ctx->hash[1] += B; + ctx->hash[2] += C; + ctx->hash[3] += D; #endif } #undef FF @@ -926,9 +922,76 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) # define SHA3_SMALL CONFIG_SHA3_SMALL #endif -enum { - SHA3_IBLK_BYTES = 72, /* 576 bits / 8 */ -}; +#define OPTIMIZE_SHA3_FOR_32 0 +/* + * SHA3 can be optimized for 32-bit CPUs with bit-slicing: + * every 64-bit word of state[] can be split into two 32-bit words + * by even/odd bits. In this form, all rotations of sha3 round + * are 32-bit - and there are lots of them. + * However, it requires either splitting/combining state words + * before/after sha3 round (code does this now) + * or shuffling bits before xor'ing them into state and in sha3_end. + * Without shuffling, bit-slicing results in -130 bytes of code + * and marginal speedup (but of course it gives wrong result). + * With shuffling it works, but +260 code bytes, and slower. + * Disabled for now: + */ +#if 0 /* LONG_MAX == 0x7fffffff */ +# undef OPTIMIZE_SHA3_FOR_32 +# define OPTIMIZE_SHA3_FOR_32 1 +#endif + +#if OPTIMIZE_SHA3_FOR_32 +/* This splits every 64-bit word into a pair of 32-bit words, + * even bits go into first word, odd bits go to second one. + * The conversion is done in-place. + */ +static void split_halves(uint64_t *state) +{ + /* Credit: Henry S. Warren, Hacker's Delight, Addison-Wesley, 2002 */ + uint32_t *s32 = (uint32_t*)state; + uint32_t t, x0, x1; + int i; + for (i = 24; i >= 0; --i) { + x0 = s32[0]; + t = (x0 ^ (x0 >> 1)) & 0x22222222; x0 = x0 ^ t ^ (t << 1); + t = (x0 ^ (x0 >> 2)) & 0x0C0C0C0C; x0 = x0 ^ t ^ (t << 2); + t = (x0 ^ (x0 >> 4)) & 0x00F000F0; x0 = x0 ^ t ^ (t << 4); + t = (x0 ^ (x0 >> 8)) & 0x0000FF00; x0 = x0 ^ t ^ (t << 8); + x1 = s32[1]; + t = (x1 ^ (x1 >> 1)) & 0x22222222; x1 = x1 ^ t ^ (t << 1); + t = (x1 ^ (x1 >> 2)) & 0x0C0C0C0C; x1 = x1 ^ t ^ (t << 2); + t = (x1 ^ (x1 >> 4)) & 0x00F000F0; x1 = x1 ^ t ^ (t << 4); + t = (x1 ^ (x1 >> 8)) & 0x0000FF00; x1 = x1 ^ t ^ (t << 8); + *s32++ = (x0 & 0x0000FFFF) | (x1 << 16); + *s32++ = (x0 >> 16) | (x1 & 0xFFFF0000); + } +} +/* The reverse operation */ +static void combine_halves(uint64_t *state) +{ + uint32_t *s32 = (uint32_t*)state; + uint32_t t, x0, x1; + int i; + for (i = 24; i >= 0; --i) { + x0 = s32[0]; + x1 = s32[1]; + t = (x0 & 0x0000FFFF) | (x1 << 16); + x1 = (x0 >> 16) | (x1 & 0xFFFF0000); + x0 = t; + t = (x0 ^ (x0 >> 8)) & 0x0000FF00; x0 = x0 ^ t ^ (t << 8); + t = (x0 ^ (x0 >> 4)) & 0x00F000F0; x0 = x0 ^ t ^ (t << 4); + t = (x0 ^ (x0 >> 2)) & 0x0C0C0C0C; x0 = x0 ^ t ^ (t << 2); + t = (x0 ^ (x0 >> 1)) & 0x22222222; x0 = x0 ^ t ^ (t << 1); + *s32++ = x0; + t = (x1 ^ (x1 >> 8)) & 0x0000FF00; x1 = x1 ^ t ^ (t << 8); + t = (x1 ^ (x1 >> 4)) & 0x00F000F0; x1 = x1 ^ t ^ (t << 4); + t = (x1 ^ (x1 >> 2)) & 0x0C0C0C0C; x1 = x1 ^ t ^ (t << 2); + t = (x1 ^ (x1 >> 1)) & 0x22222222; x1 = x1 ^ t ^ (t << 1); + *s32++ = x1; + } +} +#endif /* * In the crypto literature this function is usually called Keccak-f(). @@ -937,12 +1000,179 @@ static void sha3_process_block72(uint64_t *state) { enum { NROUNDS = 24 }; - /* Elements should be 64-bit, but top half is always zero or 0x80000000. - * We encode 63rd bits in a separate word below. - * Same is true for 31th bits, which lets us use 16-bit table instead of 64-bit. - * The speed penalty is lost in the noise. - */ +#if OPTIMIZE_SHA3_FOR_32 + /* + static const uint32_t IOTA_CONST_0[NROUNDS] = { + 0x00000001UL, + 0x00000000UL, + 0x00000000UL, + 0x00000000UL, + 0x00000001UL, + 0x00000001UL, + 0x00000001UL, + 0x00000001UL, + 0x00000000UL, + 0x00000000UL, + 0x00000001UL, + 0x00000000UL, + 0x00000001UL, + 0x00000001UL, + 0x00000001UL, + 0x00000001UL, + 0x00000000UL, + 0x00000000UL, + 0x00000000UL, + 0x00000000UL, + 0x00000001UL, + 0x00000000UL, + 0x00000001UL, + 0x00000000UL, + }; + ** bits are in lsb: 0101 0000 1111 0100 1111 0001 + */ + uint32_t IOTA_CONST_0bits = (uint32_t)(0x0050f4f1); + static const uint32_t IOTA_CONST_1[NROUNDS] = { + 0x00000000UL, + 0x00000089UL, + 0x8000008bUL, + 0x80008080UL, + 0x0000008bUL, + 0x00008000UL, + 0x80008088UL, + 0x80000082UL, + 0x0000000bUL, + 0x0000000aUL, + 0x00008082UL, + 0x00008003UL, + 0x0000808bUL, + 0x8000000bUL, + 0x8000008aUL, + 0x80000081UL, + 0x80000081UL, + 0x80000008UL, + 0x00000083UL, + 0x80008003UL, + 0x80008088UL, + 0x80000088UL, + 0x00008000UL, + 0x80008082UL, + }; + + uint32_t *const s32 = (uint32_t*)state; + unsigned round; + + split_halves(state); + + for (round = 0; round < NROUNDS; round++) { + unsigned x; + + /* Theta */ + { + uint32_t BC[20]; + for (x = 0; x < 10; ++x) { + BC[x+10] = BC[x] = s32[x]^s32[x+10]^s32[x+20]^s32[x+30]^s32[x+40]; + } + for (x = 0; x < 10; x += 2) { + uint32_t ta, tb; + ta = BC[x+8] ^ rotl32(BC[x+3], 1); + tb = BC[x+9] ^ BC[x+2]; + s32[x+0] ^= ta; + s32[x+1] ^= tb; + s32[x+10] ^= ta; + s32[x+11] ^= tb; + s32[x+20] ^= ta; + s32[x+21] ^= tb; + s32[x+30] ^= ta; + s32[x+31] ^= tb; + s32[x+40] ^= ta; + s32[x+41] ^= tb; + } + } + /* RhoPi */ + { + uint32_t t0a,t0b, t1a,t1b; + t1a = s32[1*2+0]; + t1b = s32[1*2+1]; + +#define RhoPi(PI_LANE, ROT_CONST) \ + t0a = s32[PI_LANE*2+0];\ + t0b = s32[PI_LANE*2+1];\ + if (ROT_CONST & 1) {\ + s32[PI_LANE*2+0] = rotl32(t1b, ROT_CONST/2+1);\ + s32[PI_LANE*2+1] = ROT_CONST == 1 ? t1a : rotl32(t1a, ROT_CONST/2+0);\ + } else {\ + s32[PI_LANE*2+0] = rotl32(t1a, ROT_CONST/2);\ + s32[PI_LANE*2+1] = rotl32(t1b, ROT_CONST/2);\ + }\ + t1a = t0a; t1b = t0b; + + RhoPi(10, 1) + RhoPi( 7, 3) + RhoPi(11, 6) + RhoPi(17,10) + RhoPi(18,15) + RhoPi( 3,21) + RhoPi( 5,28) + RhoPi(16,36) + RhoPi( 8,45) + RhoPi(21,55) + RhoPi(24, 2) + RhoPi( 4,14) + RhoPi(15,27) + RhoPi(23,41) + RhoPi(19,56) + RhoPi(13, 8) + RhoPi(12,25) + RhoPi( 2,43) + RhoPi(20,62) + RhoPi(14,18) + RhoPi(22,39) + RhoPi( 9,61) + RhoPi( 6,20) + RhoPi( 1,44) +#undef RhoPi + } + /* Chi */ + for (x = 0; x <= 40;) { + uint32_t BC0, BC1, BC2, BC3, BC4; + BC0 = s32[x + 0*2]; + BC1 = s32[x + 1*2]; + BC2 = s32[x + 2*2]; + s32[x + 0*2] = BC0 ^ ((~BC1) & BC2); + BC3 = s32[x + 3*2]; + s32[x + 1*2] = BC1 ^ ((~BC2) & BC3); + BC4 = s32[x + 4*2]; + s32[x + 2*2] = BC2 ^ ((~BC3) & BC4); + s32[x + 3*2] = BC3 ^ ((~BC4) & BC0); + s32[x + 4*2] = BC4 ^ ((~BC0) & BC1); + x++; + BC0 = s32[x + 0*2]; + BC1 = s32[x + 1*2]; + BC2 = s32[x + 2*2]; + s32[x + 0*2] = BC0 ^ ((~BC1) & BC2); + BC3 = s32[x + 3*2]; + s32[x + 1*2] = BC1 ^ ((~BC2) & BC3); + BC4 = s32[x + 4*2]; + s32[x + 2*2] = BC2 ^ ((~BC3) & BC4); + s32[x + 3*2] = BC3 ^ ((~BC4) & BC0); + s32[x + 4*2] = BC4 ^ ((~BC0) & BC1); + x += 9; + } + /* Iota */ + s32[0] ^= IOTA_CONST_0bits & 1; + IOTA_CONST_0bits >>= 1; + s32[1] ^= IOTA_CONST_1[round]; + } + + combine_halves(state); +#else + /* Native 64-bit algorithm */ static const uint16_t IOTA_CONST[NROUNDS] = { + /* Elements should be 64-bit, but top half is always zero + * or 0x80000000. We encode 63rd bits in a separate word below. + * Same is true for 31th bits, which lets us use 16-bit table + * instead of 64-bit. The speed penalty is lost in the noise. + */ 0x0001, 0x8082, 0x808a, @@ -983,7 +1213,7 @@ static void sha3_process_block72(uint64_t *state) }; /*static const uint8_t MOD5[10] = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, };*/ - unsigned x, y; + unsigned x; unsigned round; if (BB_BIG_ENDIAN) { @@ -1045,22 +1275,62 @@ static void sha3_process_block72(uint64_t *state) RhoPi_twice(20); RhoPi_twice(22); #undef RhoPi_twice } - /* Chi */ - for (y = 0; y <= 20; y += 5) { +# if LONG_MAX > 0x7fffffff + for (x = 0; x <= 20; x += 5) { uint64_t BC0, BC1, BC2, BC3, BC4; - BC0 = state[y + 0]; - BC1 = state[y + 1]; - BC2 = state[y + 2]; - state[y + 0] = BC0 ^ ((~BC1) & BC2); - BC3 = state[y + 3]; - state[y + 1] = BC1 ^ ((~BC2) & BC3); - BC4 = state[y + 4]; - state[y + 2] = BC2 ^ ((~BC3) & BC4); - state[y + 3] = BC3 ^ ((~BC4) & BC0); - state[y + 4] = BC4 ^ ((~BC0) & BC1); + BC0 = state[x + 0]; + BC1 = state[x + 1]; + BC2 = state[x + 2]; + state[x + 0] = BC0 ^ ((~BC1) & BC2); + BC3 = state[x + 3]; + state[x + 1] = BC1 ^ ((~BC2) & BC3); + BC4 = state[x + 4]; + state[x + 2] = BC2 ^ ((~BC3) & BC4); + state[x + 3] = BC3 ^ ((~BC4) & BC0); + state[x + 4] = BC4 ^ ((~BC0) & BC1); } - +# else + /* Reduced register pressure version + * for register-starved 32-bit arches + * (i386: -95 bytes, and it is _faster_) + */ + for (x = 0; x <= 40;) { + uint32_t BC0, BC1, BC2, BC3, BC4; + uint32_t *const s32 = (uint32_t*)state; +# if SHA3_SMALL + do_half: +# endif + BC0 = s32[x + 0*2]; + BC1 = s32[x + 1*2]; + BC2 = s32[x + 2*2]; + s32[x + 0*2] = BC0 ^ ((~BC1) & BC2); + BC3 = s32[x + 3*2]; + s32[x + 1*2] = BC1 ^ ((~BC2) & BC3); + BC4 = s32[x + 4*2]; + s32[x + 2*2] = BC2 ^ ((~BC3) & BC4); + s32[x + 3*2] = BC3 ^ ((~BC4) & BC0); + s32[x + 4*2] = BC4 ^ ((~BC0) & BC1); + x++; +# if SHA3_SMALL + if (x & 1) + goto do_half; + x += 8; +# else + BC0 = s32[x + 0*2]; + BC1 = s32[x + 1*2]; + BC2 = s32[x + 2*2]; + s32[x + 0*2] = BC0 ^ ((~BC1) & BC2); + BC3 = s32[x + 3*2]; + s32[x + 1*2] = BC1 ^ ((~BC2) & BC3); + BC4 = s32[x + 4*2]; + s32[x + 2*2] = BC2 ^ ((~BC3) & BC4); + s32[x + 3*2] = BC3 ^ ((~BC4) & BC0); + s32[x + 4*2] = BC4 ^ ((~BC0) & BC1); + x += 9; +# endif + } +# endif /* long is 32-bit */ /* Iota */ state[0] ^= IOTA_CONST[round] | (uint32_t)((IOTA_CONST_bit31 << round) & 0x80000000) @@ -1072,11 +1342,14 @@ static void sha3_process_block72(uint64_t *state) state[x] = SWAP_LE64(state[x]); } } +#endif } void FAST_FUNC sha3_begin(sha3_ctx_t *ctx) { memset(ctx, 0, sizeof(*ctx)); + /* SHA3-512, user can override */ + ctx->input_block_bytes = (1600 - 512*2) / 8; /* 72 bytes */ } void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) @@ -1086,7 +1359,7 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) unsigned bufpos = ctx->bytes_queued; while (1) { - unsigned remaining = SHA3_IBLK_BYTES - bufpos; + unsigned remaining = ctx->input_block_bytes - bufpos; if (remaining > len) remaining = len; len -= remaining; @@ -1098,38 +1371,41 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) remaining--; } /* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */ - bufpos -= SHA3_IBLK_BYTES; + bufpos -= ctx->input_block_bytes; if (bufpos != 0) break; /* Buffer is filled up, process it */ sha3_process_block72(ctx->state); /*bufpos = 0; - already is */ } - ctx->bytes_queued = bufpos + SHA3_IBLK_BYTES; + ctx->bytes_queued = bufpos + ctx->input_block_bytes; #else /* +50 bytes code size, but a bit faster because of long-sized XORs */ const uint8_t *data = buffer; unsigned bufpos = ctx->bytes_queued; + unsigned iblk_bytes = ctx->input_block_bytes; /* If already data in queue, continue queuing first */ - while (len != 0 && bufpos != 0) { - uint8_t *buf = (uint8_t*)ctx->state; - buf[bufpos] ^= *data++; - len--; - bufpos++; - if (bufpos == SHA3_IBLK_BYTES) { - bufpos = 0; - goto do_block; + if (bufpos != 0) { + while (len != 0) { + uint8_t *buf = (uint8_t*)ctx->state; + buf[bufpos] ^= *data++; + len--; + bufpos++; + if (bufpos == iblk_bytes) { + bufpos = 0; + goto do_block; + } } } /* Absorb complete blocks */ - while (len >= SHA3_IBLK_BYTES) { + while (len >= iblk_bytes) { /* XOR data onto beginning of state[]. * We try to be efficient - operate one word at a time, not byte. * Careful wrt unaligned access: can't just use "*(long*)data"! */ - unsigned count = SHA3_IBLK_BYTES / sizeof(long); + unsigned count = iblk_bytes / sizeof(long); long *buf = (long*)ctx->state; do { long v; @@ -1137,7 +1413,7 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) *buf++ ^= v; data += sizeof(long); } while (--count); - len -= SHA3_IBLK_BYTES; + len -= iblk_bytes; do_block: sha3_process_block72(ctx->state); } @@ -1158,8 +1434,22 @@ void FAST_FUNC sha3_end(sha3_ctx_t *ctx, void *resbuf) { /* Padding */ uint8_t *buf = (uint8_t*)ctx->state; - buf[ctx->bytes_queued] ^= 1; - buf[SHA3_IBLK_BYTES - 1] ^= 0x80; + /* + * Keccak block padding is: add 1 bit after last bit of input, + * then add zero bits until the end of block, and add the last 1 bit + * (the last bit in the block) - the "10*1" pattern. + * SHA3 standard appends additional two bits, 01, before that padding: + * + * SHA3-224(M) = KECCAK[448](M||01, 224) + * SHA3-256(M) = KECCAK[512](M||01, 256) + * SHA3-384(M) = KECCAK[768](M||01, 384) + * SHA3-512(M) = KECCAK[1024](M||01, 512) + * (M is the input, || is bit concatenation) + * + * The 6 below contains 01 "SHA3" bits and the first 1 "Keccak" bit: + */ + buf[ctx->bytes_queued] ^= 6; /* bit pattern 00000110 */ + buf[ctx->input_block_bytes - 1] ^= 0x80; sha3_process_block72(ctx->state); |