|
| 1 | +#include "sha256.h" |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +static const uint32_t K[64] = { |
| 5 | + 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, |
| 6 | + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, |
| 7 | + 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, |
| 8 | + 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, |
| 9 | + 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, |
| 10 | + 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, |
| 11 | + 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, |
| 12 | + 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2, |
| 13 | +}; |
| 14 | + |
| 15 | +#define ROR(x,n) (((x)>>(n))|((x)<<(32-(n)))) |
| 16 | +#define CH(x,y,z) (((x)&(y))^(~(x)&(z))) |
| 17 | +#define MAJ(x,y,z) (((x)&(y))^((x)&(z))^((y)&(z))) |
| 18 | +#define EP0(x) (ROR(x,2)^ROR(x,13)^ROR(x,22)) |
| 19 | +#define EP1(x) (ROR(x,6)^ROR(x,11)^ROR(x,25)) |
| 20 | +#define SIG0(x) (ROR(x,7)^ROR(x,18)^((x)>>3)) |
| 21 | +#define SIG1(x) (ROR(x,17)^ROR(x,19)^((x)>>10)) |
| 22 | + |
| 23 | +static void sha256_transform(sha256_ctx* ctx, const uint8_t block[64]) { |
| 24 | + uint32_t w[64], a, b, c, d, e, f, g, h, t1, t2; |
| 25 | + |
| 26 | + for (int i = 0; i < 16; i++) |
| 27 | + w[i] = ((uint32_t)block[i*4]<<24)|((uint32_t)block[i*4+1]<<16)| |
| 28 | + ((uint32_t)block[i*4+2]<<8)|block[i*4+3]; |
| 29 | + for (int i = 16; i < 64; i++) |
| 30 | + w[i] = SIG1(w[i-2]) + w[i-7] + SIG0(w[i-15]) + w[i-16]; |
| 31 | + |
| 32 | + a=ctx->state[0]; b=ctx->state[1]; c=ctx->state[2]; d=ctx->state[3]; |
| 33 | + e=ctx->state[4]; f=ctx->state[5]; g=ctx->state[6]; h=ctx->state[7]; |
| 34 | + |
| 35 | + for (int i = 0; i < 64; i++) { |
| 36 | + t1 = h + EP1(e) + CH(e,f,g) + K[i] + w[i]; |
| 37 | + t2 = EP0(a) + MAJ(a,b,c); |
| 38 | + h=g; g=f; f=e; e=d+t1; d=c; c=b; b=a; a=t1+t2; |
| 39 | + } |
| 40 | + |
| 41 | + ctx->state[0]+=a; ctx->state[1]+=b; ctx->state[2]+=c; ctx->state[3]+=d; |
| 42 | + ctx->state[4]+=e; ctx->state[5]+=f; ctx->state[6]+=g; ctx->state[7]+=h; |
| 43 | +} |
| 44 | + |
| 45 | +void sha256_init(sha256_ctx* ctx) { |
| 46 | + ctx->state[0]=0x6a09e667; ctx->state[1]=0xbb67ae85; |
| 47 | + ctx->state[2]=0x3c6ef372; ctx->state[3]=0xa54ff53a; |
| 48 | + ctx->state[4]=0x510e527f; ctx->state[5]=0x9b05688c; |
| 49 | + ctx->state[6]=0x1f83d9ab; ctx->state[7]=0x5be0cd19; |
| 50 | + ctx->count = 0; |
| 51 | + memset(ctx->buf, 0, 64); |
| 52 | +} |
| 53 | + |
| 54 | +void sha256_update(sha256_ctx* ctx, const uint8_t* data, size_t len) { |
| 55 | + size_t i = 0; |
| 56 | + size_t idx = (size_t)(ctx->count & 63); |
| 57 | + ctx->count += len; |
| 58 | + |
| 59 | + if (idx) { |
| 60 | + size_t rem = 64 - idx; |
| 61 | + if (len < rem) { memcpy(ctx->buf + idx, data, len); return; } |
| 62 | + memcpy(ctx->buf + idx, data, rem); |
| 63 | + sha256_transform(ctx, ctx->buf); |
| 64 | + i = rem; |
| 65 | + } |
| 66 | + for (; i + 64 <= len; i += 64) |
| 67 | + sha256_transform(ctx, data + i); |
| 68 | + if (i < len) |
| 69 | + memcpy(ctx->buf, data + i, len - i); |
| 70 | +} |
| 71 | + |
| 72 | +void sha256_final(sha256_ctx* ctx, uint8_t hash[32]) { |
| 73 | + size_t idx = (size_t)(ctx->count & 63); |
| 74 | + ctx->buf[idx++] = 0x80; |
| 75 | + if (idx > 56) { |
| 76 | + memset(ctx->buf + idx, 0, 64 - idx); |
| 77 | + sha256_transform(ctx, ctx->buf); |
| 78 | + idx = 0; |
| 79 | + } |
| 80 | + memset(ctx->buf + idx, 0, 56 - idx); |
| 81 | + uint64_t bits = ctx->count * 8; |
| 82 | + for (int i = 0; i < 8; i++) |
| 83 | + ctx->buf[56 + i] = (uint8_t)(bits >> (56 - i * 8)); |
| 84 | + sha256_transform(ctx, ctx->buf); |
| 85 | + for (int i = 0; i < 8; i++) { |
| 86 | + hash[i*4] = (uint8_t)(ctx->state[i]>>24); |
| 87 | + hash[i*4+1] = (uint8_t)(ctx->state[i]>>16); |
| 88 | + hash[i*4+2] = (uint8_t)(ctx->state[i]>>8); |
| 89 | + hash[i*4+3] = (uint8_t)(ctx->state[i]); |
| 90 | + } |
| 91 | +} |
0 commit comments