TAY
Fork of Crypto_light by
SHA2_32.cpp@3:85c6ee25cf3e, 2013-09-11 (annotated)
- Committer:
- feb11
- Date:
- Wed Sep 11 17:22:40 2013 +0000
- Revision:
- 3:85c6ee25cf3e
- Parent:
- 2:473bac39ae7c
- Child:
- 4:0da19393bd57
improved speed of MD2, MD5, SHA-1 and SHA-2 (32bits)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feb11 | 0:7a1237bd2d13 | 1 | #include "SHA2_32.h" |
feb11 | 0:7a1237bd2d13 | 2 | #include <string.h> |
feb11 | 0:7a1237bd2d13 | 3 | #include <stdio.h> |
feb11 | 0:7a1237bd2d13 | 4 | #include <stdlib.h> |
feb11 | 0:7a1237bd2d13 | 5 | |
feb11 | 0:7a1237bd2d13 | 6 | static const uint32_t K[] = |
feb11 | 0:7a1237bd2d13 | 7 | { |
feb11 | 0:7a1237bd2d13 | 8 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
feb11 | 0:7a1237bd2d13 | 9 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
feb11 | 0:7a1237bd2d13 | 10 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
feb11 | 0:7a1237bd2d13 | 11 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
feb11 | 0:7a1237bd2d13 | 12 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
feb11 | 0:7a1237bd2d13 | 13 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
feb11 | 0:7a1237bd2d13 | 14 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
feb11 | 0:7a1237bd2d13 | 15 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
feb11 | 0:7a1237bd2d13 | 16 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
feb11 | 0:7a1237bd2d13 | 17 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
feb11 | 0:7a1237bd2d13 | 18 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
feb11 | 0:7a1237bd2d13 | 19 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
feb11 | 0:7a1237bd2d13 | 20 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
feb11 | 0:7a1237bd2d13 | 21 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
feb11 | 0:7a1237bd2d13 | 22 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
feb11 | 0:7a1237bd2d13 | 23 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
feb11 | 0:7a1237bd2d13 | 24 | }; |
feb11 | 0:7a1237bd2d13 | 25 | |
feb11 | 3:85c6ee25cf3e | 26 | #define ROTL(W,N) (((W) << (N)) | ((W) >> (32-(N)))) |
feb11 | 3:85c6ee25cf3e | 27 | #define ROTR(W,N) (((W) >> (N)) | ((W) << (32-(N)))) |
feb11 | 3:85c6ee25cf3e | 28 | #define CH(X,Y,Z) (((X) & (Y)) ^ ((~(X)) & (Z))) |
feb11 | 3:85c6ee25cf3e | 29 | #define MAJ(X,Y,Z) (((X) & (Y)) ^ ((X) & (Z)) ^ ((Y) & (Z))) |
feb11 | 3:85c6ee25cf3e | 30 | #define BSIG0(X) (ROTR(X,2) ^ ROTR(X,13) ^ ROTR(X,22)) |
feb11 | 3:85c6ee25cf3e | 31 | #define BSIG1(X) (ROTR(X,6) ^ ROTR(X,11) ^ ROTR(X,25)) |
feb11 | 3:85c6ee25cf3e | 32 | #define SSIG0(X) (ROTR((X),7) ^ ROTR((X),18) ^ ((X) >> 3)) |
feb11 | 3:85c6ee25cf3e | 33 | #define SSIG1(X) (ROTR((X),17) ^ ROTR((X),19) ^ ((X) >> 10)) |
feb11 | 3:85c6ee25cf3e | 34 | #define R(A,B,C,D,E,F,G,H,T) T1 = H + BSIG1(E) + CH(E,F,G) + K[T] + w[T]; \ |
feb11 | 3:85c6ee25cf3e | 35 | T2 = BSIG0(A) + MAJ(A,B,C); \ |
feb11 | 3:85c6ee25cf3e | 36 | D += T1; \ |
feb11 | 3:85c6ee25cf3e | 37 | H = T1 + T2; |
feb11 | 3:85c6ee25cf3e | 38 | |
feb11 | 0:7a1237bd2d13 | 39 | static const uint32_t H[] = |
feb11 | 0:7a1237bd2d13 | 40 | { |
feb11 | 0:7a1237bd2d13 | 41 | // SHA-224 |
feb11 | 0:7a1237bd2d13 | 42 | 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, |
feb11 | 0:7a1237bd2d13 | 43 | 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4, |
feb11 | 0:7a1237bd2d13 | 44 | |
feb11 | 0:7a1237bd2d13 | 45 | // SHA-256 |
feb11 | 0:7a1237bd2d13 | 46 | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
feb11 | 0:7a1237bd2d13 | 47 | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 |
feb11 | 0:7a1237bd2d13 | 48 | }; |
feb11 | 0:7a1237bd2d13 | 49 | |
feb11 | 0:7a1237bd2d13 | 50 | SHA2_32::SHA2_32(SHA_32_TYPE t): |
feb11 | 0:7a1237bd2d13 | 51 | type(t), |
feb11 | 0:7a1237bd2d13 | 52 | totalBufferLength(0), |
feb11 | 0:7a1237bd2d13 | 53 | bufferLength(0) |
feb11 | 0:7a1237bd2d13 | 54 | { |
feb11 | 0:7a1237bd2d13 | 55 | switch(type) |
feb11 | 0:7a1237bd2d13 | 56 | { |
feb11 | 0:7a1237bd2d13 | 57 | case SHA_224: |
feb11 | 0:7a1237bd2d13 | 58 | h0 = H[0]; |
feb11 | 0:7a1237bd2d13 | 59 | h1 = H[1]; |
feb11 | 0:7a1237bd2d13 | 60 | h2 = H[2]; |
feb11 | 0:7a1237bd2d13 | 61 | h3 = H[3]; |
feb11 | 0:7a1237bd2d13 | 62 | h4 = H[4]; |
feb11 | 0:7a1237bd2d13 | 63 | h5 = H[5]; |
feb11 | 0:7a1237bd2d13 | 64 | h6 = H[6]; |
feb11 | 0:7a1237bd2d13 | 65 | h7 = H[7]; |
feb11 | 0:7a1237bd2d13 | 66 | break; |
feb11 | 0:7a1237bd2d13 | 67 | |
feb11 | 0:7a1237bd2d13 | 68 | case SHA_256: |
feb11 | 0:7a1237bd2d13 | 69 | h0 = H[8]; |
feb11 | 0:7a1237bd2d13 | 70 | h1 = H[9]; |
feb11 | 0:7a1237bd2d13 | 71 | h2 = H[10]; |
feb11 | 0:7a1237bd2d13 | 72 | h3 = H[11]; |
feb11 | 0:7a1237bd2d13 | 73 | h4 = H[12]; |
feb11 | 0:7a1237bd2d13 | 74 | h5 = H[13]; |
feb11 | 0:7a1237bd2d13 | 75 | h6 = H[14]; |
feb11 | 0:7a1237bd2d13 | 76 | h7 = H[15]; |
feb11 | 0:7a1237bd2d13 | 77 | break; |
feb11 | 0:7a1237bd2d13 | 78 | } |
feb11 | 0:7a1237bd2d13 | 79 | } |
feb11 | 0:7a1237bd2d13 | 80 | |
feb11 | 0:7a1237bd2d13 | 81 | void SHA2_32::add(uint8_t *in, uint32_t length) |
feb11 | 0:7a1237bd2d13 | 82 | { |
feb11 | 0:7a1237bd2d13 | 83 | if(length < 64-bufferLength) |
feb11 | 0:7a1237bd2d13 | 84 | { |
feb11 | 0:7a1237bd2d13 | 85 | memcpy(&buffer[bufferLength], in, length); |
feb11 | 0:7a1237bd2d13 | 86 | bufferLength += length; |
feb11 | 0:7a1237bd2d13 | 87 | totalBufferLength += length; |
feb11 | 0:7a1237bd2d13 | 88 | return; |
feb11 | 0:7a1237bd2d13 | 89 | } |
feb11 | 0:7a1237bd2d13 | 90 | int offset = 64-bufferLength; |
feb11 | 0:7a1237bd2d13 | 91 | memcpy(&buffer[bufferLength], in, offset); |
feb11 | 0:7a1237bd2d13 | 92 | computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer); |
feb11 | 0:7a1237bd2d13 | 93 | while(length-offset > 64) |
feb11 | 0:7a1237bd2d13 | 94 | { |
feb11 | 0:7a1237bd2d13 | 95 | memcpy(buffer, &in[offset], 64); |
feb11 | 0:7a1237bd2d13 | 96 | computeBlock(&h0,&h1,&h2,&h3,&h4,&h5,&h6,&h7,buffer); |
feb11 | 0:7a1237bd2d13 | 97 | offset += 64; |
feb11 | 0:7a1237bd2d13 | 98 | } |
feb11 | 0:7a1237bd2d13 | 99 | if(offset > length) |
feb11 | 0:7a1237bd2d13 | 100 | offset -= 64; |
feb11 | 0:7a1237bd2d13 | 101 | bufferLength = length - offset; |
feb11 | 0:7a1237bd2d13 | 102 | memcpy(buffer, &in[offset], bufferLength); |
feb11 | 0:7a1237bd2d13 | 103 | totalBufferLength += length; |
feb11 | 0:7a1237bd2d13 | 104 | } |
feb11 | 0:7a1237bd2d13 | 105 | |
feb11 | 0:7a1237bd2d13 | 106 | void SHA2_32::computeDigest(uint8_t *digest) |
feb11 | 0:7a1237bd2d13 | 107 | { |
feb11 | 0:7a1237bd2d13 | 108 | uint16_t padding; |
feb11 | 0:7a1237bd2d13 | 109 | if(totalBufferLength % 64 < 56) |
feb11 | 0:7a1237bd2d13 | 110 | padding = 56 - (totalBufferLength % 64); |
feb11 | 0:7a1237bd2d13 | 111 | else |
feb11 | 0:7a1237bd2d13 | 112 | padding = 56 + (64 - (totalBufferLength % 64)); |
feb11 | 3:85c6ee25cf3e | 113 | |
feb11 | 3:85c6ee25cf3e | 114 | buffer[bufferLength++] = 0x80; |
feb11 | 3:85c6ee25cf3e | 115 | padding--; |
feb11 | 3:85c6ee25cf3e | 116 | if(padding+bufferLength == 56) |
feb11 | 3:85c6ee25cf3e | 117 | memset(&buffer[bufferLength], 0, padding); |
feb11 | 3:85c6ee25cf3e | 118 | else |
feb11 | 3:85c6ee25cf3e | 119 | { |
feb11 | 3:85c6ee25cf3e | 120 | memset(&buffer[bufferLength], 0, 64-bufferLength); |
feb11 | 3:85c6ee25cf3e | 121 | computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); |
feb11 | 3:85c6ee25cf3e | 122 | memset(buffer, 0, bufferLength); |
feb11 | 3:85c6ee25cf3e | 123 | } |
feb11 | 3:85c6ee25cf3e | 124 | |
feb11 | 3:85c6ee25cf3e | 125 | uint64_t lengthBit = totalBufferLength << 3; |
feb11 | 0:7a1237bd2d13 | 126 | uint32_t lengthBitLow = lengthBit; |
feb11 | 0:7a1237bd2d13 | 127 | uint32_t lengthBitHigh = lengthBit >> 32; |
feb11 | 3:85c6ee25cf3e | 128 | lengthBitLow = __rev(lengthBitLow); |
feb11 | 3:85c6ee25cf3e | 129 | lengthBitHigh = __rev(lengthBitHigh); |
feb11 | 3:85c6ee25cf3e | 130 | memcpy(&buffer[60], &lengthBitLow, 4); |
feb11 | 3:85c6ee25cf3e | 131 | memcpy(&buffer[56], &lengthBitHigh, 4); |
feb11 | 3:85c6ee25cf3e | 132 | computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); |
feb11 | 0:7a1237bd2d13 | 133 | |
feb11 | 3:85c6ee25cf3e | 134 | h0 = __rev(h0); |
feb11 | 3:85c6ee25cf3e | 135 | h1 = __rev(h1); |
feb11 | 3:85c6ee25cf3e | 136 | h2 = __rev(h2); |
feb11 | 3:85c6ee25cf3e | 137 | h3 = __rev(h3); |
feb11 | 3:85c6ee25cf3e | 138 | h4 = __rev(h4); |
feb11 | 3:85c6ee25cf3e | 139 | h5 = __rev(h5); |
feb11 | 3:85c6ee25cf3e | 140 | h6 = __rev(h6); |
feb11 | 3:85c6ee25cf3e | 141 | memcpy(digest, &h0, 4); |
feb11 | 3:85c6ee25cf3e | 142 | memcpy(&digest[4], &h1, 4); |
feb11 | 3:85c6ee25cf3e | 143 | memcpy(&digest[8], &h2, 4); |
feb11 | 3:85c6ee25cf3e | 144 | memcpy(&digest[12], &h3, 4); |
feb11 | 3:85c6ee25cf3e | 145 | memcpy(&digest[16], &h4, 4); |
feb11 | 3:85c6ee25cf3e | 146 | memcpy(&digest[20], &h5, 4); |
feb11 | 3:85c6ee25cf3e | 147 | memcpy(&digest[24], &h6, 4); |
feb11 | 3:85c6ee25cf3e | 148 | |
feb11 | 0:7a1237bd2d13 | 149 | if(type == SHA_256) |
feb11 | 0:7a1237bd2d13 | 150 | { |
feb11 | 3:85c6ee25cf3e | 151 | h7 = __rev(h7); |
feb11 | 3:85c6ee25cf3e | 152 | memcpy(&digest[28], &h7, 4); |
feb11 | 0:7a1237bd2d13 | 153 | } |
feb11 | 0:7a1237bd2d13 | 154 | |
feb11 | 0:7a1237bd2d13 | 155 | // reset state |
feb11 | 0:7a1237bd2d13 | 156 | switch(type) |
feb11 | 0:7a1237bd2d13 | 157 | { |
feb11 | 0:7a1237bd2d13 | 158 | case SHA_224: |
feb11 | 0:7a1237bd2d13 | 159 | h0 = H[0]; |
feb11 | 0:7a1237bd2d13 | 160 | h1 = H[1]; |
feb11 | 0:7a1237bd2d13 | 161 | h2 = H[2]; |
feb11 | 0:7a1237bd2d13 | 162 | h3 = H[3]; |
feb11 | 0:7a1237bd2d13 | 163 | h4 = H[4]; |
feb11 | 0:7a1237bd2d13 | 164 | h5 = H[5]; |
feb11 | 0:7a1237bd2d13 | 165 | h6 = H[6]; |
feb11 | 0:7a1237bd2d13 | 166 | h7 = H[7]; |
feb11 | 0:7a1237bd2d13 | 167 | break; |
feb11 | 0:7a1237bd2d13 | 168 | |
feb11 | 0:7a1237bd2d13 | 169 | case SHA_256: |
feb11 | 0:7a1237bd2d13 | 170 | h0 = H[8]; |
feb11 | 0:7a1237bd2d13 | 171 | h1 = H[9]; |
feb11 | 0:7a1237bd2d13 | 172 | h2 = H[10]; |
feb11 | 0:7a1237bd2d13 | 173 | h3 = H[11]; |
feb11 | 0:7a1237bd2d13 | 174 | h4 = H[12]; |
feb11 | 0:7a1237bd2d13 | 175 | h5 = H[13]; |
feb11 | 0:7a1237bd2d13 | 176 | h6 = H[14]; |
feb11 | 0:7a1237bd2d13 | 177 | h7 = H[15]; |
feb11 | 0:7a1237bd2d13 | 178 | break; |
feb11 | 0:7a1237bd2d13 | 179 | } |
feb11 | 0:7a1237bd2d13 | 180 | totalBufferLength = 0; |
feb11 | 0:7a1237bd2d13 | 181 | bufferLength = 0; |
feb11 | 0:7a1237bd2d13 | 182 | } |
feb11 | 0:7a1237bd2d13 | 183 | |
feb11 | 0:7a1237bd2d13 | 184 | void SHA2_32::computeBlock(uint32_t *h02, |
feb11 | 0:7a1237bd2d13 | 185 | uint32_t *h12, |
feb11 | 0:7a1237bd2d13 | 186 | uint32_t *h22, |
feb11 | 0:7a1237bd2d13 | 187 | uint32_t *h32, |
feb11 | 0:7a1237bd2d13 | 188 | uint32_t *h42, |
feb11 | 0:7a1237bd2d13 | 189 | uint32_t *h52, |
feb11 | 0:7a1237bd2d13 | 190 | uint32_t *h62, |
feb11 | 0:7a1237bd2d13 | 191 | uint32_t *h72, |
feb11 | 0:7a1237bd2d13 | 192 | uint8_t *buffer) |
feb11 | 0:7a1237bd2d13 | 193 | { |
feb11 | 0:7a1237bd2d13 | 194 | uint32_t w[64]; |
feb11 | 3:85c6ee25cf3e | 195 | uint32_t *buffer2 = (uint32_t*)buffer; |
feb11 | 3:85c6ee25cf3e | 196 | w[0] = __rev(buffer2[0]); |
feb11 | 3:85c6ee25cf3e | 197 | w[1] = __rev(buffer2[1]); |
feb11 | 3:85c6ee25cf3e | 198 | w[2] = __rev(buffer2[2]); |
feb11 | 3:85c6ee25cf3e | 199 | w[3] = __rev(buffer2[3]); |
feb11 | 3:85c6ee25cf3e | 200 | w[4] = __rev(buffer2[4]); |
feb11 | 3:85c6ee25cf3e | 201 | w[5] = __rev(buffer2[5]); |
feb11 | 3:85c6ee25cf3e | 202 | w[6] = __rev(buffer2[6]); |
feb11 | 3:85c6ee25cf3e | 203 | w[7] = __rev(buffer2[7]); |
feb11 | 3:85c6ee25cf3e | 204 | w[8] = __rev(buffer2[8]); |
feb11 | 3:85c6ee25cf3e | 205 | w[9] = __rev(buffer2[9]); |
feb11 | 3:85c6ee25cf3e | 206 | w[10] = __rev(buffer2[10]); |
feb11 | 3:85c6ee25cf3e | 207 | w[11] = __rev(buffer2[11]); |
feb11 | 3:85c6ee25cf3e | 208 | w[12] = __rev(buffer2[12]); |
feb11 | 3:85c6ee25cf3e | 209 | w[13] = __rev(buffer2[13]); |
feb11 | 3:85c6ee25cf3e | 210 | w[14] = __rev(buffer2[14]); |
feb11 | 3:85c6ee25cf3e | 211 | w[15] = __rev(buffer2[15]); |
feb11 | 3:85c6ee25cf3e | 212 | |
feb11 | 0:7a1237bd2d13 | 213 | for(int t = 16; t < 64; ++t) |
feb11 | 0:7a1237bd2d13 | 214 | w[t] = SSIG1(w[t-2]) + w[t-7] + SSIG0(w[t-15]) + w[t-16]; |
feb11 | 0:7a1237bd2d13 | 215 | |
feb11 | 3:85c6ee25cf3e | 216 | uint32_t a = *h02, b = *h12, c = *h22, d = *h32, e = *h42, f = *h52, g = *h62, h = *h72; |
feb11 | 3:85c6ee25cf3e | 217 | uint32_t T1, T2; |
feb11 | 3:85c6ee25cf3e | 218 | |
feb11 | 3:85c6ee25cf3e | 219 | R(a,b,c,d,e,f,g,h,0) |
feb11 | 3:85c6ee25cf3e | 220 | R(h,a,b,c,d,e,f,g,1) |
feb11 | 3:85c6ee25cf3e | 221 | R(g,h,a,b,c,d,e,f,2) |
feb11 | 3:85c6ee25cf3e | 222 | R(f,g,h,a,b,c,d,e,3) |
feb11 | 3:85c6ee25cf3e | 223 | R(e,f,g,h,a,b,c,d,4) |
feb11 | 3:85c6ee25cf3e | 224 | R(d,e,f,g,h,a,b,c,5) |
feb11 | 3:85c6ee25cf3e | 225 | R(c,d,e,f,g,h,a,b,6) |
feb11 | 3:85c6ee25cf3e | 226 | R(b,c,d,e,f,g,h,a,7) |
feb11 | 3:85c6ee25cf3e | 227 | |
feb11 | 3:85c6ee25cf3e | 228 | R(a,b,c,d,e,f,g,h,8) |
feb11 | 3:85c6ee25cf3e | 229 | R(h,a,b,c,d,e,f,g,9) |
feb11 | 3:85c6ee25cf3e | 230 | R(g,h,a,b,c,d,e,f,10) |
feb11 | 3:85c6ee25cf3e | 231 | R(f,g,h,a,b,c,d,e,11) |
feb11 | 3:85c6ee25cf3e | 232 | R(e,f,g,h,a,b,c,d,12) |
feb11 | 3:85c6ee25cf3e | 233 | R(d,e,f,g,h,a,b,c,13) |
feb11 | 3:85c6ee25cf3e | 234 | R(c,d,e,f,g,h,a,b,14) |
feb11 | 3:85c6ee25cf3e | 235 | R(b,c,d,e,f,g,h,a,15) |
feb11 | 3:85c6ee25cf3e | 236 | |
feb11 | 3:85c6ee25cf3e | 237 | R(a,b,c,d,e,f,g,h,16) |
feb11 | 3:85c6ee25cf3e | 238 | R(h,a,b,c,d,e,f,g,17) |
feb11 | 3:85c6ee25cf3e | 239 | R(g,h,a,b,c,d,e,f,18) |
feb11 | 3:85c6ee25cf3e | 240 | R(f,g,h,a,b,c,d,e,19) |
feb11 | 3:85c6ee25cf3e | 241 | R(e,f,g,h,a,b,c,d,20) |
feb11 | 3:85c6ee25cf3e | 242 | R(d,e,f,g,h,a,b,c,21) |
feb11 | 3:85c6ee25cf3e | 243 | R(c,d,e,f,g,h,a,b,22) |
feb11 | 3:85c6ee25cf3e | 244 | R(b,c,d,e,f,g,h,a,23) |
feb11 | 3:85c6ee25cf3e | 245 | |
feb11 | 3:85c6ee25cf3e | 246 | R(a,b,c,d,e,f,g,h,24) |
feb11 | 3:85c6ee25cf3e | 247 | R(h,a,b,c,d,e,f,g,25) |
feb11 | 3:85c6ee25cf3e | 248 | R(g,h,a,b,c,d,e,f,26) |
feb11 | 3:85c6ee25cf3e | 249 | R(f,g,h,a,b,c,d,e,27) |
feb11 | 3:85c6ee25cf3e | 250 | R(e,f,g,h,a,b,c,d,28) |
feb11 | 3:85c6ee25cf3e | 251 | R(d,e,f,g,h,a,b,c,29) |
feb11 | 3:85c6ee25cf3e | 252 | R(c,d,e,f,g,h,a,b,30) |
feb11 | 3:85c6ee25cf3e | 253 | R(b,c,d,e,f,g,h,a,31) |
feb11 | 3:85c6ee25cf3e | 254 | |
feb11 | 3:85c6ee25cf3e | 255 | R(a,b,c,d,e,f,g,h,32) |
feb11 | 3:85c6ee25cf3e | 256 | R(h,a,b,c,d,e,f,g,33) |
feb11 | 3:85c6ee25cf3e | 257 | R(g,h,a,b,c,d,e,f,34) |
feb11 | 3:85c6ee25cf3e | 258 | R(f,g,h,a,b,c,d,e,35) |
feb11 | 3:85c6ee25cf3e | 259 | R(e,f,g,h,a,b,c,d,36) |
feb11 | 3:85c6ee25cf3e | 260 | R(d,e,f,g,h,a,b,c,37) |
feb11 | 3:85c6ee25cf3e | 261 | R(c,d,e,f,g,h,a,b,38) |
feb11 | 3:85c6ee25cf3e | 262 | R(b,c,d,e,f,g,h,a,39) |
feb11 | 3:85c6ee25cf3e | 263 | |
feb11 | 3:85c6ee25cf3e | 264 | R(a,b,c,d,e,f,g,h,40) |
feb11 | 3:85c6ee25cf3e | 265 | R(h,a,b,c,d,e,f,g,41) |
feb11 | 3:85c6ee25cf3e | 266 | R(g,h,a,b,c,d,e,f,42) |
feb11 | 3:85c6ee25cf3e | 267 | R(f,g,h,a,b,c,d,e,43) |
feb11 | 3:85c6ee25cf3e | 268 | R(e,f,g,h,a,b,c,d,44) |
feb11 | 3:85c6ee25cf3e | 269 | R(d,e,f,g,h,a,b,c,45) |
feb11 | 3:85c6ee25cf3e | 270 | R(c,d,e,f,g,h,a,b,46) |
feb11 | 3:85c6ee25cf3e | 271 | R(b,c,d,e,f,g,h,a,47) |
feb11 | 3:85c6ee25cf3e | 272 | |
feb11 | 3:85c6ee25cf3e | 273 | R(a,b,c,d,e,f,g,h,48) |
feb11 | 3:85c6ee25cf3e | 274 | R(h,a,b,c,d,e,f,g,49) |
feb11 | 3:85c6ee25cf3e | 275 | R(g,h,a,b,c,d,e,f,50) |
feb11 | 3:85c6ee25cf3e | 276 | R(f,g,h,a,b,c,d,e,51) |
feb11 | 3:85c6ee25cf3e | 277 | R(e,f,g,h,a,b,c,d,52) |
feb11 | 3:85c6ee25cf3e | 278 | R(d,e,f,g,h,a,b,c,53) |
feb11 | 3:85c6ee25cf3e | 279 | R(c,d,e,f,g,h,a,b,54) |
feb11 | 3:85c6ee25cf3e | 280 | R(b,c,d,e,f,g,h,a,55) |
feb11 | 3:85c6ee25cf3e | 281 | |
feb11 | 3:85c6ee25cf3e | 282 | R(a,b,c,d,e,f,g,h,56) |
feb11 | 3:85c6ee25cf3e | 283 | R(h,a,b,c,d,e,f,g,57) |
feb11 | 3:85c6ee25cf3e | 284 | R(g,h,a,b,c,d,e,f,58) |
feb11 | 3:85c6ee25cf3e | 285 | R(f,g,h,a,b,c,d,e,59) |
feb11 | 3:85c6ee25cf3e | 286 | R(e,f,g,h,a,b,c,d,60) |
feb11 | 3:85c6ee25cf3e | 287 | R(d,e,f,g,h,a,b,c,61) |
feb11 | 3:85c6ee25cf3e | 288 | R(c,d,e,f,g,h,a,b,62) |
feb11 | 3:85c6ee25cf3e | 289 | R(b,c,d,e,f,g,h,a,63) |
feb11 | 3:85c6ee25cf3e | 290 | |
feb11 | 0:7a1237bd2d13 | 291 | |
feb11 | 0:7a1237bd2d13 | 292 | *h02 += a; |
feb11 | 0:7a1237bd2d13 | 293 | *h12 += b; |
feb11 | 0:7a1237bd2d13 | 294 | *h22 += c; |
feb11 | 0:7a1237bd2d13 | 295 | *h32 += d; |
feb11 | 0:7a1237bd2d13 | 296 | *h42 += e; |
feb11 | 0:7a1237bd2d13 | 297 | *h52 += f; |
feb11 | 0:7a1237bd2d13 | 298 | *h62 += g; |
feb11 | 0:7a1237bd2d13 | 299 | *h72 += h; |
feb11 | 0:7a1237bd2d13 | 300 | } |
feb11 | 0:7a1237bd2d13 | 301 | |
feb11 | 0:7a1237bd2d13 | 302 | void SHA2_32::computeDigest(SHA_32_TYPE type, uint8_t *digest, uint8_t *in, uint32_t length) |
feb11 | 0:7a1237bd2d13 | 303 | { |
feb11 | 0:7a1237bd2d13 | 304 | uint32_t h0 = H[type*8], h1 = H[type*8+1], h2 = H[type*8+2], h3 = H[type*8+3]; |
feb11 | 0:7a1237bd2d13 | 305 | uint32_t h4 = H[type*8+4], h5 = H[type*8+5], h6 = H[type*8+6], h7 = H[type*8+7]; |
feb11 | 3:85c6ee25cf3e | 306 | uint64_t lengthBit = length << 3; |
feb11 | 0:7a1237bd2d13 | 307 | uint16_t padding; |
feb11 | 0:7a1237bd2d13 | 308 | if(length % 64 < 56) |
feb11 | 0:7a1237bd2d13 | 309 | padding = 56 - (length % 64); |
feb11 | 0:7a1237bd2d13 | 310 | else |
feb11 | 0:7a1237bd2d13 | 311 | padding = 56 + (64 - (length % 64)); |
feb11 | 3:85c6ee25cf3e | 312 | |
feb11 | 3:85c6ee25cf3e | 313 | while(length >= 64) |
feb11 | 0:7a1237bd2d13 | 314 | { |
feb11 | 3:85c6ee25cf3e | 315 | computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, in); |
feb11 | 3:85c6ee25cf3e | 316 | length -= 64; |
feb11 | 3:85c6ee25cf3e | 317 | in += 64; |
feb11 | 0:7a1237bd2d13 | 318 | } |
feb11 | 3:85c6ee25cf3e | 319 | uint8_t buffer[64]; |
feb11 | 3:85c6ee25cf3e | 320 | memcpy(buffer, in,length); |
feb11 | 3:85c6ee25cf3e | 321 | buffer[length++] = 0x80; |
feb11 | 3:85c6ee25cf3e | 322 | padding--; |
feb11 | 3:85c6ee25cf3e | 323 | if(padding+length == 56) |
feb11 | 3:85c6ee25cf3e | 324 | memset(&buffer[length], 0, padding); |
feb11 | 3:85c6ee25cf3e | 325 | else |
feb11 | 3:85c6ee25cf3e | 326 | { |
feb11 | 3:85c6ee25cf3e | 327 | memset(&buffer[length], 0, 64-length); |
feb11 | 3:85c6ee25cf3e | 328 | computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); |
feb11 | 3:85c6ee25cf3e | 329 | memset(buffer, 0, length); |
feb11 | 3:85c6ee25cf3e | 330 | } |
feb11 | 3:85c6ee25cf3e | 331 | |
feb11 | 0:7a1237bd2d13 | 332 | uint32_t lengthBitLow = lengthBit; |
feb11 | 0:7a1237bd2d13 | 333 | uint32_t lengthBitHigh = lengthBit >> 32; |
feb11 | 2:473bac39ae7c | 334 | lengthBitLow = __rev(lengthBitLow); |
feb11 | 2:473bac39ae7c | 335 | memcpy(&buffer[60], &lengthBitLow, 4); |
feb11 | 2:473bac39ae7c | 336 | lengthBitHigh = __rev(lengthBitHigh); |
feb11 | 2:473bac39ae7c | 337 | memcpy(&buffer[56], &lengthBitHigh, 4); |
feb11 | 0:7a1237bd2d13 | 338 | computeBlock(&h0, &h1, &h2, &h3, &h4, &h5, &h6, &h7, buffer); |
feb11 | 0:7a1237bd2d13 | 339 | |
feb11 | 2:473bac39ae7c | 340 | h0 = __rev(h0); |
feb11 | 2:473bac39ae7c | 341 | h1 = __rev(h1); |
feb11 | 2:473bac39ae7c | 342 | h2 = __rev(h2); |
feb11 | 2:473bac39ae7c | 343 | h3 = __rev(h3); |
feb11 | 2:473bac39ae7c | 344 | h4 = __rev(h4); |
feb11 | 2:473bac39ae7c | 345 | h5 = __rev(h5); |
feb11 | 2:473bac39ae7c | 346 | h6 = __rev(h6); |
feb11 | 2:473bac39ae7c | 347 | |
feb11 | 2:473bac39ae7c | 348 | memcpy(digest, &h0, 4); |
feb11 | 2:473bac39ae7c | 349 | memcpy(&digest[4], &h1, 4); |
feb11 | 2:473bac39ae7c | 350 | memcpy(&digest[8], &h2, 4); |
feb11 | 2:473bac39ae7c | 351 | memcpy(&digest[12], &h3, 4); |
feb11 | 2:473bac39ae7c | 352 | memcpy(&digest[16], &h4, 4); |
feb11 | 2:473bac39ae7c | 353 | memcpy(&digest[20], &h5, 4); |
feb11 | 2:473bac39ae7c | 354 | memcpy(&digest[24], &h6, 4); |
feb11 | 2:473bac39ae7c | 355 | |
feb11 | 0:7a1237bd2d13 | 356 | |
feb11 | 0:7a1237bd2d13 | 357 | if(type == SHA_256) |
feb11 | 0:7a1237bd2d13 | 358 | { |
feb11 | 2:473bac39ae7c | 359 | h7 = __rev(h7); |
feb11 | 2:473bac39ae7c | 360 | memcpy(&digest[28], &h7, 4); |
feb11 | 0:7a1237bd2d13 | 361 | } |
feb11 | 0:7a1237bd2d13 | 362 | } |