TAY
Fork of Crypto_light by
Diff: SHA2_32.cpp
- Revision:
- 4:0da19393bd57
- Parent:
- 3:85c6ee25cf3e
- Child:
- 5:06cd9c8afa0b
--- a/SHA2_32.cpp Wed Sep 11 17:22:40 2013 +0000 +++ b/SHA2_32.cpp Thu Sep 12 10:18:57 2013 +0000 @@ -3,25 +3,9 @@ #include <stdio.h> #include <stdlib.h> -static const uint32_t K[] = -{ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, - 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, - 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, - 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, - 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, - 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 -}; + +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)))) @@ -31,7 +15,11 @@ #define BSIG1(X) (ROTR(X,6) ^ ROTR(X,11) ^ ROTR(X,25)) #define SSIG0(X) (ROTR((X),7) ^ ROTR((X),18) ^ ((X) >> 3)) #define SSIG1(X) (ROTR((X),17) ^ ROTR((X),19) ^ ((X) >> 10)) -#define R(A,B,C,D,E,F,G,H,T) T1 = H + BSIG1(E) + CH(E,F,G) + K[T] + w[T]; \ +#define R(A,B,C,D,E,F,G,H,T,K) T1 = H + BSIG1(E) + CH(E,F,G) + K + (w[T] = __rev(buffer2[T])); \ + T2 = BSIG0(A) + MAJ(A,B,C); \ + D += T1; \ + H = T1 + T2; +#define R2(A,B,C,D,E,F,G,H,T,K) T1 = H + BSIG1(E) + CH(E,F,G) + K + W(T&MASK); \ T2 = BSIG0(A) + MAJ(A,B,C); \ D += T1; \ H = T1 + T2; @@ -119,7 +107,7 @@ { memset(&buffer[bufferLength], 0, 64-bufferLength); computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); - memset(buffer, 0, bufferLength); + memset(buffer, 0, 48); } uint64_t lengthBit = totalBufferLength << 3; @@ -191,102 +179,83 @@ uint32_t *h72, uint8_t *buffer) { - uint32_t w[64]; + uint32_t w[16]; uint32_t *buffer2 = (uint32_t*)buffer; - w[0] = __rev(buffer2[0]); - w[1] = __rev(buffer2[1]); - w[2] = __rev(buffer2[2]); - w[3] = __rev(buffer2[3]); - w[4] = __rev(buffer2[4]); - w[5] = __rev(buffer2[5]); - w[6] = __rev(buffer2[6]); - w[7] = __rev(buffer2[7]); - w[8] = __rev(buffer2[8]); - w[9] = __rev(buffer2[9]); - w[10] = __rev(buffer2[10]); - w[11] = __rev(buffer2[11]); - w[12] = __rev(buffer2[12]); - w[13] = __rev(buffer2[13]); - w[14] = __rev(buffer2[14]); - w[15] = __rev(buffer2[15]); - - for(int t = 16; t < 64; ++t) - w[t] = SSIG1(w[t-2]) + w[t-7] + SSIG0(w[t-15]) + w[t-16]; - 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) - R(h,a,b,c,d,e,f,g,1) - R(g,h,a,b,c,d,e,f,2) - R(f,g,h,a,b,c,d,e,3) - R(e,f,g,h,a,b,c,d,4) - R(d,e,f,g,h,a,b,c,5) - R(c,d,e,f,g,h,a,b,6) - R(b,c,d,e,f,g,h,a,7) + + + 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) + R(f,g,h,a,b,c,d,e,3,0xe9b5dba5) + R(e,f,g,h,a,b,c,d,4,0x3956c25b) + R(d,e,f,g,h,a,b,c,5,0x59f111f1) + R(c,d,e,f,g,h,a,b,6,0x923f82a4) + R(b,c,d,e,f,g,h,a,7,0xab1c5ed5) - R(a,b,c,d,e,f,g,h,8) - R(h,a,b,c,d,e,f,g,9) - R(g,h,a,b,c,d,e,f,10) - R(f,g,h,a,b,c,d,e,11) - R(e,f,g,h,a,b,c,d,12) - R(d,e,f,g,h,a,b,c,13) - R(c,d,e,f,g,h,a,b,14) - R(b,c,d,e,f,g,h,a,15) - - R(a,b,c,d,e,f,g,h,16) - R(h,a,b,c,d,e,f,g,17) - R(g,h,a,b,c,d,e,f,18) - R(f,g,h,a,b,c,d,e,19) - R(e,f,g,h,a,b,c,d,20) - R(d,e,f,g,h,a,b,c,21) - R(c,d,e,f,g,h,a,b,22) - R(b,c,d,e,f,g,h,a,23) + R(a,b,c,d,e,f,g,h,8,0xd807aa98) + R(h,a,b,c,d,e,f,g,9,0x12835b01) + R(g,h,a,b,c,d,e,f,10,0x243185be) + R(f,g,h,a,b,c,d,e,11,0x550c7dc3) + R(e,f,g,h,a,b,c,d,12,0x72be5d74) + R(d,e,f,g,h,a,b,c,13,0x80deb1fe) + R(c,d,e,f,g,h,a,b,14,0x9bdc06a7) + R(b,c,d,e,f,g,h,a,15,0xc19bf174) + + R2(a,b,c,d,e,f,g,h,16,0xe49b69c1) + R2(h,a,b,c,d,e,f,g,17,0xefbe4786) + R2(g,h,a,b,c,d,e,f,18,0x0fc19dc6) + R2(f,g,h,a,b,c,d,e,19,0x240ca1cc) + R2(e,f,g,h,a,b,c,d,20,0x2de92c6f) + R2(d,e,f,g,h,a,b,c,21,0x4a7484aa) + R2(c,d,e,f,g,h,a,b,22,0x5cb0a9dc) + R2(b,c,d,e,f,g,h,a,23,0x76f988da) - R(a,b,c,d,e,f,g,h,24) - R(h,a,b,c,d,e,f,g,25) - R(g,h,a,b,c,d,e,f,26) - R(f,g,h,a,b,c,d,e,27) - R(e,f,g,h,a,b,c,d,28) - R(d,e,f,g,h,a,b,c,29) - R(c,d,e,f,g,h,a,b,30) - R(b,c,d,e,f,g,h,a,31) - - R(a,b,c,d,e,f,g,h,32) - R(h,a,b,c,d,e,f,g,33) - R(g,h,a,b,c,d,e,f,34) - R(f,g,h,a,b,c,d,e,35) - R(e,f,g,h,a,b,c,d,36) - R(d,e,f,g,h,a,b,c,37) - R(c,d,e,f,g,h,a,b,38) - R(b,c,d,e,f,g,h,a,39) + R2(a,b,c,d,e,f,g,h,24,0x983e5152) + R2(h,a,b,c,d,e,f,g,25,0xa831c66d) + R2(g,h,a,b,c,d,e,f,26,0xb00327c8) + R2(f,g,h,a,b,c,d,e,27,0xbf597fc7) + R2(e,f,g,h,a,b,c,d,28,0xc6e00bf3) + R2(d,e,f,g,h,a,b,c,29,0xd5a79147) + R2(c,d,e,f,g,h,a,b,30,0x06ca6351) + R2(b,c,d,e,f,g,h,a,31,0x14292967) + + R2(a,b,c,d,e,f,g,h,32,0x27b70a85) + R2(h,a,b,c,d,e,f,g,33,0x2e1b2138) + R2(g,h,a,b,c,d,e,f,34,0x4d2c6dfc) + R2(f,g,h,a,b,c,d,e,35,0x53380d13) + R2(e,f,g,h,a,b,c,d,36,0x650a7354) + R2(d,e,f,g,h,a,b,c,37,0x766a0abb) + R2(c,d,e,f,g,h,a,b,38,0x81c2c92e) + R2(b,c,d,e,f,g,h,a,39,0x92722c85) - R(a,b,c,d,e,f,g,h,40) - R(h,a,b,c,d,e,f,g,41) - R(g,h,a,b,c,d,e,f,42) - R(f,g,h,a,b,c,d,e,43) - R(e,f,g,h,a,b,c,d,44) - R(d,e,f,g,h,a,b,c,45) - R(c,d,e,f,g,h,a,b,46) - R(b,c,d,e,f,g,h,a,47) - - R(a,b,c,d,e,f,g,h,48) - R(h,a,b,c,d,e,f,g,49) - R(g,h,a,b,c,d,e,f,50) - R(f,g,h,a,b,c,d,e,51) - R(e,f,g,h,a,b,c,d,52) - R(d,e,f,g,h,a,b,c,53) - R(c,d,e,f,g,h,a,b,54) - R(b,c,d,e,f,g,h,a,55) + R2(a,b,c,d,e,f,g,h,40,0xa2bfe8a1) + R2(h,a,b,c,d,e,f,g,41,0xa81a664b) + R2(g,h,a,b,c,d,e,f,42,0xc24b8b70) + R2(f,g,h,a,b,c,d,e,43,0xc76c51a3) + R2(e,f,g,h,a,b,c,d,44,0xd192e819) + R2(d,e,f,g,h,a,b,c,45,0xd6990624) + R2(c,d,e,f,g,h,a,b,46,0xf40e3585) + R2(b,c,d,e,f,g,h,a,47,0x106aa070) - R(a,b,c,d,e,f,g,h,56) - R(h,a,b,c,d,e,f,g,57) - R(g,h,a,b,c,d,e,f,58) - R(f,g,h,a,b,c,d,e,59) - R(e,f,g,h,a,b,c,d,60) - R(d,e,f,g,h,a,b,c,61) - R(c,d,e,f,g,h,a,b,62) - R(b,c,d,e,f,g,h,a,63) + R2(a,b,c,d,e,f,g,h,48,0x19a4c116) + R2(h,a,b,c,d,e,f,g,49,0x1e376c08) + R2(g,h,a,b,c,d,e,f,50,0x2748774c) + R2(f,g,h,a,b,c,d,e,51,0x34b0bcb5) + R2(e,f,g,h,a,b,c,d,52,0x391c0cb3) + R2(d,e,f,g,h,a,b,c,53,0x4ed8aa4a) + R2(c,d,e,f,g,h,a,b,54,0x5b9cca4f) + R2(b,c,d,e,f,g,h,a,55,0x682e6ff3) + + R2(a,b,c,d,e,f,g,h,56,0x748f82ee) + R2(h,a,b,c,d,e,f,g,57,0x78a5636f) + R2(g,h,a,b,c,d,e,f,58,0x84c87814) + R2(f,g,h,a,b,c,d,e,59,0x8cc70208) + R2(e,f,g,h,a,b,c,d,60,0x90befffa) + R2(d,e,f,g,h,a,b,c,61,0xa4506ceb) + R2(c,d,e,f,g,h,a,b,62,0xbef9a3f7) + R2(b,c,d,e,f,g,h,a,63,0xc67178f2) *h02 += a; @@ -326,7 +295,7 @@ { memset(&buffer[length], 0, 64-length); computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); - memset(buffer, 0, length); + memset(buffer, 0, 48); } uint32_t lengthBitLow = lengthBit;