Fork of François Berder Crypto, fixed AES CBC and small rework
Dependents: AES_example shaun_larada Smartage
Fork of Crypto by
Diff: hash/SHA2_32.cpp
- Revision:
- 10:bc9c23aa3870
- Parent:
- 7:2dbbdfb08123
- Child:
- 13:ac8e23b98dae
diff -r e34e076fb223 -r bc9c23aa3870 hash/SHA2_32.cpp --- a/hash/SHA2_32.cpp Mon Sep 16 08:35:36 2013 +0000 +++ b/hash/SHA2_32.cpp Tue Sep 24 07:19:04 2013 +0000 @@ -1,14 +1,13 @@ #include "SHA2_32.h" #include <string.h> -#include <stdio.h> -#include <stdlib.h> + static const uint8_t MASK = 0x0F; #define W(t) (w[(t)] = SSIG1(w[((t)+14)&MASK]) + w[((t)+9)&MASK] + SSIG0(w[((t)+1)&MASK]) + w[t]) #define ROTL(W,N) (((W) << (N)) | ((W) >> (32-(N)))) -#define ROTR(W,N) (((W) >> (N)) | ((W) << (32-(N)))) +#define ROTR(W,N) (__ror(W,N)) #define CH(X,Y,Z) (((X) & (Y)) ^ ((~(X)) & (Z))) #define MAJ(X,Y,Z) (((X) & (Y)) ^ ((X) & (Z)) ^ ((Y) & (Z))) #define BSIG0(X) (ROTR(X,2) ^ ROTR(X,13) ^ ROTR(X,22)) @@ -164,10 +163,19 @@ void SHA2_32::computeHash(SHA_32_TYPE type, uint8_t *hash, uint8_t *data, uint32_t length) { uint32_t *hash2 = (uint32_t*)hash; - uint32_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3]; - uint32_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7]; + + uint32_t h[8]; + h[0] = H[type*8]; + h[1] = H[type*8+1]; + h[2] = H[type*8+2]; + h[3] = H[type*8+3]; + h[4] = H[type*8+4]; + h[5] = H[type*8+5]; + h[6] = H[type*8+6]; + h[7] = H[type*8+7]; + uint64_t lengthBit = length << 3; - uint16_t padding; + uint32_t padding; if(length % 64 < 56) padding = 56 - (length % 64); else @@ -175,7 +183,7 @@ while(length >= 64) { - computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, data); + computeBlock(h, &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7], data); length -= 64; data += 64; } @@ -188,7 +196,7 @@ else { memset(&buffer[length], 0, 64-length); - computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); + computeBlock(h, &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7], buffer); memset(buffer, 0, 56); } @@ -198,22 +206,22 @@ memcpy(&buffer[60], &lengthBitLow, 4); lengthBitHigh = __rev(lengthBitHigh); memcpy(&buffer[56], &lengthBitHigh, 4); - computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); + computeBlock(h, &h[1], &h[2], &h[3], &h[4], &h[5], &h[6], &h[7], buffer); - hash2[0] = __rev(h0); - hash2[1] = __rev(h1); - hash2[2] = __rev(h2); - hash2[3] = __rev(h3); - hash2[4] = __rev(h4); - hash2[5] = __rev(h5); - hash2[6] = __rev(h6); + hash2[0] = __rev(h[0]); + hash2[1] = __rev(h[1]); + hash2[2] = __rev(h[2]); + hash2[3] = __rev(h[3]); + hash2[4] = __rev(h[4]); + hash2[5] = __rev(h[5]); + hash2[6] = __rev(h[6]); if(type == SHA_256) - hash2[7] = __rev(h7); + hash2[7] = __rev(h[7]); } -void SHA2_32::computeBlock(uint32_t *h02, +__forceinline void SHA2_32::computeBlock(uint32_t *h02, uint32_t *h12, uint32_t *h22, uint32_t *h32, @@ -228,7 +236,6 @@ uint32_t a = *h02, b = *h12, c = *h22, d = *h32, e = *h42, f = *h52, g = *h62, h = *h72; uint32_t T1, T2; - R(a,b,c,d,e,f,g,h,0,0x428a2f98) R(h,a,b,c,d,e,f,g,1,0x71374491) R(g,h,a,b,c,d,e,f,2,0xb5c0fbcf)