Index: include/llvm/Support/SHA1.h =================================================================== --- include/llvm/Support/SHA1.h +++ include/llvm/Support/SHA1.h @@ -61,7 +61,10 @@ // Internal State struct { - uint32_t Buffer[BLOCK_LENGTH / 4]; + union { + uint8_t C[BLOCK_LENGTH]; + uint32_t L[BLOCK_LENGTH / 4]; + } Buffer; uint32_t State[HASH_LENGTH / 4]; uint32_t ByteCount; uint8_t BufferOffset; Index: lib/Support/SHA1.cpp =================================================================== --- lib/Support/SHA1.cpp +++ lib/Support/SHA1.cpp @@ -6,8 +6,10 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// +// // This code is taken from public domain -// (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c) +// (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c and +// https://github.com/jsonn/src/blob/trunk/common/lib/libc/hash/sha1/sha1.c) // and modified by wrapping it in a C++ interface for LLVM, // and removing unnecessary code. // @@ -25,6 +27,41 @@ #define SHA_BIG_ENDIAN #endif +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +#if SHA_BIG_ENDIAN +#define blk0(i) \ + (InternalState.Buffer.L[i] = \ + (rol(InternalState.Buffer.L[i], 24) & 0xFF00FF00) | \ + (rol(InternalState.Buffer.L[i], 8) & 0x00FF00FF)) +#else +#define blk0(i) InternalState.Buffer.L[i] +#endif + +#define blk(i) \ + (InternalState.Buffer.L[i & 15] = \ + rol(InternalState.Buffer.L[(i + 13) & 15] ^ \ + InternalState.Buffer.L[(i + 8) & 15] ^ \ + InternalState.Buffer.L[(i + 2) & 15] ^ \ + InternalState.Buffer.L[i & 15], \ + 1)) + +#define R0(v, w, x, y, z, i) \ + z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ + w = rol(w, 30); +#define R1(v, w, x, y, z, i) \ + z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ + w = rol(w, 30); +#define R2(v, w, x, y, z, i) \ + z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \ + w = rol(w, 30); +#define R3(v, w, x, y, z, i) \ + z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ + w = rol(w, 30); +#define R4(v, w, x, y, z, i) \ + z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ + w = rol(w, 30); + /* code */ #define SHA1_K0 0x5a827999 #define SHA1_K20 0x6ed9eba1 @@ -47,42 +84,95 @@ InternalState.BufferOffset = 0; } -static uint32_t rol32(uint32_t number, uint8_t bits) { - return ((number << bits) | (number >> (32 - bits))); -} - void SHA1::hashBlock() { - uint8_t i; - uint32_t a, b, c, d, e, t; - - a = InternalState.State[0]; - b = InternalState.State[1]; - c = InternalState.State[2]; - d = InternalState.State[3]; - e = InternalState.State[4]; - for (i = 0; i < 80; i++) { - if (i >= 16) { - t = InternalState.Buffer[(i + 13) & 15] ^ - InternalState.Buffer[(i + 8) & 15] ^ - InternalState.Buffer[(i + 2) & 15] ^ InternalState.Buffer[i & 15]; - InternalState.Buffer[i & 15] = rol32(t, 1); - } - if (i < 20) { - t = (d ^ (b & (c ^ d))) + SHA1_K0; - } else if (i < 40) { - t = (b ^ c ^ d) + SHA1_K20; - } else if (i < 60) { - t = ((b & c) | (d & (b | c))) + SHA1_K40; - } else { - t = (b ^ c ^ d) + SHA1_K60; - } - t += rol32(a, 5) + e + InternalState.Buffer[i & 15]; - e = d; - d = c; - c = rol32(b, 30); - b = a; - a = t; - } + uint32_t a = InternalState.State[0]; + uint32_t b = InternalState.State[1]; + uint32_t c = InternalState.State[2]; + uint32_t d = InternalState.State[3]; + uint32_t e = InternalState.State[4]; + + // 4 rounds of 20 operations each. Loop unrolled. + R0(a, b, c, d, e, 0); + R0(e, a, b, c, d, 1); + R0(d, e, a, b, c, 2); + R0(c, d, e, a, b, 3); + R0(b, c, d, e, a, 4); + R0(a, b, c, d, e, 5); + R0(e, a, b, c, d, 6); + R0(d, e, a, b, c, 7); + R0(c, d, e, a, b, 8); + R0(b, c, d, e, a, 9); + R0(a, b, c, d, e, 10); + R0(e, a, b, c, d, 11); + R0(d, e, a, b, c, 12); + R0(c, d, e, a, b, 13); + R0(b, c, d, e, a, 14); + R0(a, b, c, d, e, 15); + R1(e, a, b, c, d, 16); + R1(d, e, a, b, c, 17); + R1(c, d, e, a, b, 18); + R1(b, c, d, e, a, 19); + R2(a, b, c, d, e, 20); + R2(e, a, b, c, d, 21); + R2(d, e, a, b, c, 22); + R2(c, d, e, a, b, 23); + R2(b, c, d, e, a, 24); + R2(a, b, c, d, e, 25); + R2(e, a, b, c, d, 26); + R2(d, e, a, b, c, 27); + R2(c, d, e, a, b, 28); + R2(b, c, d, e, a, 29); + R2(a, b, c, d, e, 30); + R2(e, a, b, c, d, 31); + R2(d, e, a, b, c, 32); + R2(c, d, e, a, b, 33); + R2(b, c, d, e, a, 34); + R2(a, b, c, d, e, 35); + R2(e, a, b, c, d, 36); + R2(d, e, a, b, c, 37); + R2(c, d, e, a, b, 38); + R2(b, c, d, e, a, 39); + R3(a, b, c, d, e, 40); + R3(e, a, b, c, d, 41); + R3(d, e, a, b, c, 42); + R3(c, d, e, a, b, 43); + R3(b, c, d, e, a, 44); + R3(a, b, c, d, e, 45); + R3(e, a, b, c, d, 46); + R3(d, e, a, b, c, 47); + R3(c, d, e, a, b, 48); + R3(b, c, d, e, a, 49); + R3(a, b, c, d, e, 50); + R3(e, a, b, c, d, 51); + R3(d, e, a, b, c, 52); + R3(c, d, e, a, b, 53); + R3(b, c, d, e, a, 54); + R3(a, b, c, d, e, 55); + R3(e, a, b, c, d, 56); + R3(d, e, a, b, c, 57); + R3(c, d, e, a, b, 58); + R3(b, c, d, e, a, 59); + R4(a, b, c, d, e, 60); + R4(e, a, b, c, d, 61); + R4(d, e, a, b, c, 62); + R4(c, d, e, a, b, 63); + R4(b, c, d, e, a, 64); + R4(a, b, c, d, e, 65); + R4(e, a, b, c, d, 66); + R4(d, e, a, b, c, 67); + R4(c, d, e, a, b, 68); + R4(b, c, d, e, a, 69); + R4(a, b, c, d, e, 70); + R4(e, a, b, c, d, 71); + R4(d, e, a, b, c, 72); + R4(c, d, e, a, b, 73); + R4(b, c, d, e, a, 74); + R4(a, b, c, d, e, 75); + R4(e, a, b, c, d, 76); + R4(d, e, a, b, c, 77); + R4(c, d, e, a, b, 78); + R4(b, c, d, e, a, 79); + InternalState.State[0] += a; InternalState.State[1] += b; InternalState.State[2] += c; @@ -91,12 +181,12 @@ } void SHA1::addUncounted(uint8_t data) { - uint8_t *const b = (uint8_t *)InternalState.Buffer; #ifdef SHA_BIG_ENDIAN - b[InternalState.BufferOffset] = data; + InternalState.Buffer.C[InternalState.BufferOffset] = data; #else - b[InternalState.BufferOffset ^ 3] = data; + InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = data; #endif + InternalState.BufferOffset++; if (InternalState.BufferOffset == BLOCK_LENGTH) { hashBlock();