| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
#include "os_port.h" |
| 33 |
#include "crypto.h" |
| 34 |
|
| 35 |
#define GET_UINT32(n,b,i) \ |
| 36 |
{ \ |
| 37 |
(n) = ((uint32_t) (b)[(i) ] << 24) \ |
| 38 |
| ((uint32_t) (b)[(i) + 1] << 16) \ |
| 39 |
| ((uint32_t) (b)[(i) + 2] << 8) \ |
| 40 |
| ((uint32_t) (b)[(i) + 3] ); \ |
| 41 |
} |
| 42 |
|
| 43 |
#define PUT_UINT32(n,b,i) \ |
| 44 |
{ \ |
| 45 |
(b)[(i) ] = (uint8_t) ((n) >> 24); \ |
| 46 |
(b)[(i) + 1] = (uint8_t) ((n) >> 16); \ |
| 47 |
(b)[(i) + 2] = (uint8_t) ((n) >> 8); \ |
| 48 |
(b)[(i) + 3] = (uint8_t) ((n) ); \ |
| 49 |
} |
| 50 |
|
| 51 |
static const uint8_t sha256_padding[64] = |
| 52 |
{ |
| 53 |
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 54 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 55 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 56 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
void SHA256_Init(SHA256_CTX *ctx) |
| 63 |
{ |
| 64 |
ctx->total[0] = 0; |
| 65 |
ctx->total[1] = 0; |
| 66 |
|
| 67 |
ctx->state[0] = 0x6A09E667; |
| 68 |
ctx->state[1] = 0xBB67AE85; |
| 69 |
ctx->state[2] = 0x3C6EF372; |
| 70 |
ctx->state[3] = 0xA54FF53A; |
| 71 |
ctx->state[4] = 0x510E527F; |
| 72 |
ctx->state[5] = 0x9B05688C; |
| 73 |
ctx->state[6] = 0x1F83D9AB; |
| 74 |
ctx->state[7] = 0x5BE0CD19; |
| 75 |
} |
| 76 |
|
| 77 |
static void SHA256_Process(const uint8_t digest[64], SHA256_CTX *ctx) |
| 78 |
{ |
| 79 |
uint32_t temp1, temp2, W[64]; |
| 80 |
uint32_t A, B, C, D, E, F, G, H; |
| 81 |
|
| 82 |
GET_UINT32(W[0], digest, 0); |
| 83 |
GET_UINT32(W[1], digest, 4); |
| 84 |
GET_UINT32(W[2], digest, 8); |
| 85 |
GET_UINT32(W[3], digest, 12); |
| 86 |
GET_UINT32(W[4], digest, 16); |
| 87 |
GET_UINT32(W[5], digest, 20); |
| 88 |
GET_UINT32(W[6], digest, 24); |
| 89 |
GET_UINT32(W[7], digest, 28); |
| 90 |
GET_UINT32(W[8], digest, 32); |
| 91 |
GET_UINT32(W[9], digest, 36); |
| 92 |
GET_UINT32(W[10], digest, 40); |
| 93 |
GET_UINT32(W[11], digest, 44); |
| 94 |
GET_UINT32(W[12], digest, 48); |
| 95 |
GET_UINT32(W[13], digest, 52); |
| 96 |
GET_UINT32(W[14], digest, 56); |
| 97 |
GET_UINT32(W[15], digest, 60); |
| 98 |
|
| 99 |
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n) |
| 100 |
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n))) |
| 101 |
|
| 102 |
#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3)) |
| 103 |
#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10)) |
| 104 |
|
| 105 |
#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22)) |
| 106 |
#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25)) |
| 107 |
|
| 108 |
#define F0(x,y,z) ((x & y) | (z & (x | y))) |
| 109 |
#define F1(x,y,z) (z ^ (x & (y ^ z))) |
| 110 |
|
| 111 |
#define R(t) \ |
| 112 |
( \ |
| 113 |
W[t] = S1(W[t - 2]) + W[t - 7] + \ |
| 114 |
S0(W[t - 15]) + W[t - 16] \ |
| 115 |
) |
| 116 |
|
| 117 |
#define P(a,b,c,d,e,f,g,h,x,K) \ |
| 118 |
{ \ |
| 119 |
temp1 = h + S3(e) + F1(e,f,g) + K + x; \ |
| 120 |
temp2 = S2(a) + F0(a,b,c); \ |
| 121 |
d += temp1; h = temp1 + temp2; \ |
| 122 |
} |
| 123 |
|
| 124 |
A = ctx->state[0]; |
| 125 |
B = ctx->state[1]; |
| 126 |
C = ctx->state[2]; |
| 127 |
D = ctx->state[3]; |
| 128 |
E = ctx->state[4]; |
| 129 |
F = ctx->state[5]; |
| 130 |
G = ctx->state[6]; |
| 131 |
H = ctx->state[7]; |
| 132 |
|
| 133 |
P(A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98); |
| 134 |
P(H, A, B, C, D, E, F, G, W[ 1], 0x71374491); |
| 135 |
P(G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF); |
| 136 |
P(F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5); |
| 137 |
P(E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B); |
| 138 |
P(D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1); |
| 139 |
P(C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4); |
| 140 |
P(B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5); |
| 141 |
P(A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98); |
| 142 |
P(H, A, B, C, D, E, F, G, W[ 9], 0x12835B01); |
| 143 |
P(G, H, A, B, C, D, E, F, W[10], 0x243185BE); |
| 144 |
P(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3); |
| 145 |
P(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74); |
| 146 |
P(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE); |
| 147 |
P(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7); |
| 148 |
P(B, C, D, E, F, G, H, A, W[15], 0xC19BF174); |
| 149 |
P(A, B, C, D, E, F, G, H, R(16), 0xE49B69C1); |
| 150 |
P(H, A, B, C, D, E, F, G, R(17), 0xEFBE4786); |
| 151 |
P(G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6); |
| 152 |
P(F, G, H, A, B, C, D, E, R(19), 0x240CA1CC); |
| 153 |
P(E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F); |
| 154 |
P(D, E, F, G, H, A, B, C, R(21), 0x4A7484AA); |
| 155 |
P(C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC); |
| 156 |
P(B, C, D, E, F, G, H, A, R(23), 0x76F988DA); |
| 157 |
P(A, B, C, D, E, F, G, H, R(24), 0x983E5152); |
| 158 |
P(H, A, B, C, D, E, F, G, R(25), 0xA831C66D); |
| 159 |
P(G, H, A, B, C, D, E, F, R(26), 0xB00327C8); |
| 160 |
P(F, G, H, A, B, C, D, E, R(27), 0xBF597FC7); |
| 161 |
P(E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3); |
| 162 |
P(D, E, F, G, H, A, B, C, R(29), 0xD5A79147); |
| 163 |
P(C, D, E, F, G, H, A, B, R(30), 0x06CA6351); |
| 164 |
P(B, C, D, E, F, G, H, A, R(31), 0x14292967); |
| 165 |
P(A, B, C, D, E, F, G, H, R(32), 0x27B70A85); |
| 166 |
P(H, A, B, C, D, E, F, G, R(33), 0x2E1B2138); |
| 167 |
P(G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC); |
| 168 |
P(F, G, H, A, B, C, D, E, R(35), 0x53380D13); |
| 169 |
P(E, F, G, H, A, B, C, D, R(36), 0x650A7354); |
| 170 |
P(D, E, F, G, H, A, B, C, R(37), 0x766A0ABB); |
| 171 |
P(C, D, E, F, G, H, A, B, R(38), 0x81C2C92E); |
| 172 |
P(B, C, D, E, F, G, H, A, R(39), 0x92722C85); |
| 173 |
P(A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1); |
| 174 |
P(H, A, B, C, D, E, F, G, R(41), 0xA81A664B); |
| 175 |
P(G, H, A, B, C, D, E, F, R(42), 0xC24B8B70); |
| 176 |
P(F, G, H, A, B, C, D, E, R(43), 0xC76C51A3); |
| 177 |
P(E, F, G, H, A, B, C, D, R(44), 0xD192E819); |
| 178 |
P(D, E, F, G, H, A, B, C, R(45), 0xD6990624); |
| 179 |
P(C, D, E, F, G, H, A, B, R(46), 0xF40E3585); |
| 180 |
P(B, C, D, E, F, G, H, A, R(47), 0x106AA070); |
| 181 |
P(A, B, C, D, E, F, G, H, R(48), 0x19A4C116); |
| 182 |
P(H, A, B, C, D, E, F, G, R(49), 0x1E376C08); |
| 183 |
P(G, H, A, B, C, D, E, F, R(50), 0x2748774C); |
| 184 |
P(F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5); |
| 185 |
P(E, F, G, H, A, B, C, D, R(52), 0x391C0CB3); |
| 186 |
P(D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A); |
| 187 |
P(C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F); |
| 188 |
P(B, C, D, E, F, G, H, A, R(55), 0x682E6FF3); |
| 189 |
P(A, B, C, D, E, F, G, H, R(56), 0x748F82EE); |
| 190 |
P(H, A, B, C, D, E, F, G, R(57), 0x78A5636F); |
| 191 |
P(G, H, A, B, C, D, E, F, R(58), 0x84C87814); |
| 192 |
P(F, G, H, A, B, C, D, E, R(59), 0x8CC70208); |
| 193 |
P(E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA); |
| 194 |
P(D, E, F, G, H, A, B, C, R(61), 0xA4506CEB); |
| 195 |
P(C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7); |
| 196 |
P(B, C, D, E, F, G, H, A, R(63), 0xC67178F2); |
| 197 |
|
| 198 |
ctx->state[0] += A; |
| 199 |
ctx->state[1] += B; |
| 200 |
ctx->state[2] += C; |
| 201 |
ctx->state[3] += D; |
| 202 |
ctx->state[4] += E; |
| 203 |
ctx->state[5] += F; |
| 204 |
ctx->state[6] += G; |
| 205 |
ctx->state[7] += H; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
void SHA256_Update(SHA256_CTX *ctx, const uint8_t * msg, int len) |
| 212 |
{ |
| 213 |
uint32_t left = ctx->total[0] & 0x3F; |
| 214 |
uint32_t fill = 64 - left; |
| 215 |
|
| 216 |
ctx->total[0] += len; |
| 217 |
ctx->total[0] &= 0xFFFFFFFF; |
| 218 |
|
| 219 |
if (ctx->total[0] < len) |
| 220 |
ctx->total[1]++; |
| 221 |
|
| 222 |
if (left && len >= fill) |
| 223 |
{ |
| 224 |
memcpy((void *) (ctx->buffer + left), (void *)msg, fill); |
| 225 |
SHA256_Process(ctx->buffer, ctx); |
| 226 |
len -= fill; |
| 227 |
msg += fill; |
| 228 |
left = 0; |
| 229 |
} |
| 230 |
|
| 231 |
while (len >= 64) |
| 232 |
{ |
| 233 |
SHA256_Process(msg, ctx); |
| 234 |
len -= 64; |
| 235 |
msg += 64; |
| 236 |
} |
| 237 |
|
| 238 |
if (len) |
| 239 |
{ |
| 240 |
memcpy((void *) (ctx->buffer + left), (void *) msg, len); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
void SHA256_Final(uint8_t *digest, SHA256_CTX *ctx) |
| 248 |
{ |
| 249 |
uint32_t last, padn; |
| 250 |
uint32_t high, low; |
| 251 |
uint8_t msglen[8]; |
| 252 |
|
| 253 |
high = (ctx->total[0] >> 29) |
| 254 |
| (ctx->total[1] << 3); |
| 255 |
low = (ctx->total[0] << 3); |
| 256 |
|
| 257 |
PUT_UINT32(high, msglen, 0); |
| 258 |
PUT_UINT32(low, msglen, 4); |
| 259 |
|
| 260 |
last = ctx->total[0] & 0x3F; |
| 261 |
padn = (last < 56) ? (56 - last) : (120 - last); |
| 262 |
|
| 263 |
SHA256_Update(ctx, sha256_padding, padn); |
| 264 |
SHA256_Update(ctx, msglen, 8); |
| 265 |
|
| 266 |
PUT_UINT32(ctx->state[0], digest, 0); |
| 267 |
PUT_UINT32(ctx->state[1], digest, 4); |
| 268 |
PUT_UINT32(ctx->state[2], digest, 8); |
| 269 |
PUT_UINT32(ctx->state[3], digest, 12); |
| 270 |
PUT_UINT32(ctx->state[4], digest, 16); |
| 271 |
PUT_UINT32(ctx->state[5], digest, 20); |
| 272 |
PUT_UINT32(ctx->state[6], digest, 24); |
| 273 |
PUT_UINT32(ctx->state[7], digest, 28); |
| 274 |
} |