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; @@ -75,6 +78,19 @@ void hashBlock(); void addUncounted(uint8_t data); void pad(); + + uint32_t blk0(int I); + uint32_t blk(int I); + void r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I); + void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I); + void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I); + void r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I); + void r4(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I); }; } // end llvm namespace 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 +// http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/hash/sha1/sha1.c?rev=1.6) // and modified by wrapping it in a C++ interface for LLVM, // and removing unnecessary code. // @@ -25,6 +27,60 @@ #define SHA_BIG_ENDIAN #endif +static uint32_t rol(uint32_t number, int bits) { + return (number << bits) | (number >> (32 - bits)); +}; + +#if SHA_BIG_ENDIAN +uint32_t SHA1::blk0(int I) { + InternalState.Buffer.L[I] = + (rol(InternalState.Buffer.L[I], 24) & 0xFF00FF00) | + (rol(InternalState.Buffer.L[I], 8) & 0x00FF00FF); + return InternalState.Buffer.L[I]; +} +#else +uint32_t SHA1::blk0(int I) { return InternalState.Buffer.L[I]; } +#endif + +uint32_t SHA1::blk(int 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); + return InternalState.Buffer.L[I & 15]; +} + +void SHA1::r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I) { + E += ((B & (C ^ D)) ^ D) + blk0(I) + 0x5A827999 + rol(A, 5); + B = rol(B, 30); +} + +void SHA1::r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I) { + E += ((B & (C ^ D)) ^ D) + blk(I) + 0x5A827999 + rol(A, 5); + B = rol(B, 30); +} + +void SHA1::r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I) { + E += (B ^ C ^ D) + blk(I) + 0x6ED9EBA1 + rol(A, 5); + B = rol(B, 30); +} + +void SHA1::r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I) { + E += (((B | C) & D) | (B & C)) + blk(I) + 0x8F1BBCDC + rol(A, 5); + B = rol(B, 30); +} + +void SHA1::r4(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, + int I) { + E += (B ^ C ^ D) + blk(I) + 0xCA62C1D6 + rol(A, 5); + B = rol(B, 30); +} + /* code */ #define SHA1_K0 0x5a827999 #define SHA1_K20 0x6ed9eba1 @@ -47,56 +103,112 @@ 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; - } - InternalState.State[0] += a; - InternalState.State[1] += b; - InternalState.State[2] += c; - InternalState.State[3] += d; - InternalState.State[4] += e; + 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; + InternalState.State[3] += D; + InternalState.State[4] += E; } 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();