diff options
author | lloyd <[email protected]> | 2006-08-12 15:49:55 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2006-08-12 15:49:55 +0000 |
commit | ff0cf0be1d26cdc284c0a04657a936d66ffdfe1f (patch) | |
tree | 35c2593ead533278d1aa10b304c248ba83730359 /modules | |
parent | 6cefc0ff776364ce8a01a5cbc11fc3eea55942ba (diff) |
Add preliminary version of an SHA-1 x86 assembly module
Diffstat (limited to 'modules')
-rw-r--r-- | modules/sha_x86/modinfo.txt | 12 | ||||
-rw-r--r-- | modules/sha_x86/sha160.cpp | 129 | ||||
-rw-r--r-- | modules/sha_x86/sha1core.S | 46 |
3 files changed, 187 insertions, 0 deletions
diff --git a/modules/sha_x86/modinfo.txt b/modules/sha_x86/modinfo.txt new file mode 100644 index 000000000..9dccdb814 --- /dev/null +++ b/modules/sha_x86/modinfo.txt @@ -0,0 +1,12 @@ +realname "SHA-1 Assembler Backend for x86 Systems" + +replace_file sha160.cpp +add_file sha1core.S + +<arch> +ia32 +</arch> + +<cc> +gcc +</cc> diff --git a/modules/sha_x86/sha160.cpp b/modules/sha_x86/sha160.cpp new file mode 100644 index 000000000..be8b7f695 --- /dev/null +++ b/modules/sha_x86/sha160.cpp @@ -0,0 +1,129 @@ +/************************************************* +* SHA-160 Source File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#include <botan/sha160.h> +#include <botan/bit_ops.h> + +namespace Botan { + +namespace { + +/************************************************* +* SHA-160 F1 Function * +*************************************************/ +inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) + { + E += (D ^ (B & (C ^ D))) + msg + 0x5A827999 + rotate_left(A, 5); + B = rotate_left(B, 30); + } + +/************************************************* +* SHA-160 F2 Function * +*************************************************/ +inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) + { + E += (B ^ C ^ D) + msg + 0x6ED9EBA1 + rotate_left(A, 5); + B = rotate_left(B, 30); + } + +/************************************************* +* SHA-160 F3 Function * +*************************************************/ +inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) + { + E += ((B & C) | ((B | C) & D)) + msg + 0x8F1BBCDC + rotate_left(A, 5); + B = rotate_left(B, 30); + } + +/************************************************* +* SHA-160 F4 Function * +*************************************************/ +inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) + { + E += (B ^ C ^ D) + msg + 0xCA62C1D6 + rotate_left(A, 5); + B = rotate_left(B, 30); + } + +} + +extern "C" void sha160_core(u32bit[5], const byte[64], u32bit[80]); + +/************************************************* +* SHA-160 Compression Function * +*************************************************/ +void SHA_160::hash(const byte input[]) + { +#if 1 + sha160_core(digest, input, W); +#else + for(u32bit j = 0; j != 16; ++j) + W[j] = make_u32bit(input[4*j], input[4*j+1], input[4*j+2], input[4*j+3]); + + for(u32bit j = 16; j != 80; ++j) + W[j] = rotate_left((W[j-3] ^ W[j-8] ^ W[j-14] ^ W[j-16]), 1); +#endif + + u32bit A = digest[0], B = digest[1], C = digest[2], + D = digest[3], E = digest[4]; + + F1(A,B,C,D,E,W[ 0]); F1(E,A,B,C,D,W[ 1]); F1(D,E,A,B,C,W[ 2]); + F1(C,D,E,A,B,W[ 3]); F1(B,C,D,E,A,W[ 4]); F1(A,B,C,D,E,W[ 5]); + F1(E,A,B,C,D,W[ 6]); F1(D,E,A,B,C,W[ 7]); F1(C,D,E,A,B,W[ 8]); + F1(B,C,D,E,A,W[ 9]); F1(A,B,C,D,E,W[10]); F1(E,A,B,C,D,W[11]); + F1(D,E,A,B,C,W[12]); F1(C,D,E,A,B,W[13]); F1(B,C,D,E,A,W[14]); + F1(A,B,C,D,E,W[15]); F1(E,A,B,C,D,W[16]); F1(D,E,A,B,C,W[17]); + F1(C,D,E,A,B,W[18]); F1(B,C,D,E,A,W[19]); + + F2(A,B,C,D,E,W[20]); F2(E,A,B,C,D,W[21]); F2(D,E,A,B,C,W[22]); + F2(C,D,E,A,B,W[23]); F2(B,C,D,E,A,W[24]); F2(A,B,C,D,E,W[25]); + F2(E,A,B,C,D,W[26]); F2(D,E,A,B,C,W[27]); F2(C,D,E,A,B,W[28]); + F2(B,C,D,E,A,W[29]); F2(A,B,C,D,E,W[30]); F2(E,A,B,C,D,W[31]); + F2(D,E,A,B,C,W[32]); F2(C,D,E,A,B,W[33]); F2(B,C,D,E,A,W[34]); + F2(A,B,C,D,E,W[35]); F2(E,A,B,C,D,W[36]); F2(D,E,A,B,C,W[37]); + F2(C,D,E,A,B,W[38]); F2(B,C,D,E,A,W[39]); + + F3(A,B,C,D,E,W[40]); F3(E,A,B,C,D,W[41]); F3(D,E,A,B,C,W[42]); + F3(C,D,E,A,B,W[43]); F3(B,C,D,E,A,W[44]); F3(A,B,C,D,E,W[45]); + F3(E,A,B,C,D,W[46]); F3(D,E,A,B,C,W[47]); F3(C,D,E,A,B,W[48]); + F3(B,C,D,E,A,W[49]); F3(A,B,C,D,E,W[50]); F3(E,A,B,C,D,W[51]); + F3(D,E,A,B,C,W[52]); F3(C,D,E,A,B,W[53]); F3(B,C,D,E,A,W[54]); + F3(A,B,C,D,E,W[55]); F3(E,A,B,C,D,W[56]); F3(D,E,A,B,C,W[57]); + F3(C,D,E,A,B,W[58]); F3(B,C,D,E,A,W[59]); + + F4(A,B,C,D,E,W[60]); F4(E,A,B,C,D,W[61]); F4(D,E,A,B,C,W[62]); + F4(C,D,E,A,B,W[63]); F4(B,C,D,E,A,W[64]); F4(A,B,C,D,E,W[65]); + F4(E,A,B,C,D,W[66]); F4(D,E,A,B,C,W[67]); F4(C,D,E,A,B,W[68]); + F4(B,C,D,E,A,W[69]); F4(A,B,C,D,E,W[70]); F4(E,A,B,C,D,W[71]); + F4(D,E,A,B,C,W[72]); F4(C,D,E,A,B,W[73]); F4(B,C,D,E,A,W[74]); + F4(A,B,C,D,E,W[75]); F4(E,A,B,C,D,W[76]); F4(D,E,A,B,C,W[77]); + F4(C,D,E,A,B,W[78]); F4(B,C,D,E,A,W[79]); + + digest[0] += A; digest[1] += B; digest[2] += C; + digest[3] += D; digest[4] += E; + } + +/************************************************* +* Copy out the digest * +*************************************************/ +void SHA_160::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; ++j) + output[j] = get_byte(j % 4, digest[j/4]); + } + +/************************************************* +* Clear memory of sensitive data * +*************************************************/ +void SHA_160::clear() throw() + { + MDx_HashFunction::clear(); + digest[0] = 0x67452301; + digest[1] = 0xEFCDAB89; + digest[2] = 0x98BADCFE; + digest[3] = 0x10325476; + digest[4] = 0xC3D2E1F0; + } + +} diff --git a/modules/sha_x86/sha1core.S b/modules/sha_x86/sha1core.S new file mode 100644 index 000000000..a51c24d62 --- /dev/null +++ b/modules/sha_x86/sha1core.S @@ -0,0 +1,46 @@ + .file "sha1core.S" + .text + .p2align 4,,15 + +.global sha160_core + .type sha160_core, @function +sha160_core: + pushl %edi + pushl %esi + + movl 16(%esp), %ecx # byte input[64] + movl 20(%esp), %edi # u32bit W[80] + + movl $0, %esi # loop counter + + .p2align 4,,7 +.LOAD_INPUT_LOOP: + movl 0(%ecx), %eax + addl $4, %ecx + incl %esi + + bswapl %eax + movl %eax, -4(%edi,%esi,4) + cmpl $16, %esi + jne .LOAD_INPUT_LOOP + + + leal 64(%edi), %edx + movl $16, %ecx + + // here esi == 16 +.EXPANSION_LOOP: + movl -32(%edx), %eax + xorl -12(%edx), %eax + incl %ecx + xorl -56(%edx), %eax + xorl -64(%edx), %eax + rorl $31, %eax + movl %eax, (%edx) + addl $4, %edx + cmpl $80, %ecx + jne .EXPANSION_LOOP + + popl %esi + popl %edi + ret |