|
| 1 | +/* SHA-256 per FIPS 180-4. Straightforward reference implementation; validated |
| 2 | + * against the NIST test vectors in tests/test_cli.c. */ |
| 3 | + |
| 4 | +#include "foundation/sha256.h" |
| 5 | + |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +static const uint32_t K[64] = { |
| 9 | + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 10 | + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 11 | + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 12 | + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 13 | + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 14 | + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 15 | + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 16 | + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; |
| 17 | + |
| 18 | +#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) |
| 19 | +#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) |
| 20 | +#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
| 21 | +#define EP0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) |
| 22 | +#define EP1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) |
| 23 | +#define SIG0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3)) |
| 24 | +#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10)) |
| 25 | + |
| 26 | +static void sha256_transform(cbm_sha256_ctx *c, const uint8_t *data) { |
| 27 | + uint32_t m[64]; |
| 28 | + for (int i = 0, j = 0; i < 16; i++, j += 4) { |
| 29 | + m[i] = ((uint32_t)data[j] << 24) | ((uint32_t)data[j + 1] << 16) | |
| 30 | + ((uint32_t)data[j + 2] << 8) | (uint32_t)data[j + 3]; |
| 31 | + } |
| 32 | + for (int i = 16; i < 64; i++) { |
| 33 | + m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; |
| 34 | + } |
| 35 | + |
| 36 | + uint32_t a = c->state[0], b = c->state[1], cc = c->state[2], d = c->state[3]; |
| 37 | + uint32_t e = c->state[4], f = c->state[5], g = c->state[6], h = c->state[7]; |
| 38 | + |
| 39 | + for (int i = 0; i < 64; i++) { |
| 40 | + uint32_t t1 = h + EP1(e) + CH(e, f, g) + K[i] + m[i]; |
| 41 | + uint32_t t2 = EP0(a) + MAJ(a, b, cc); |
| 42 | + h = g; |
| 43 | + g = f; |
| 44 | + f = e; |
| 45 | + e = d + t1; |
| 46 | + d = cc; |
| 47 | + cc = b; |
| 48 | + b = a; |
| 49 | + a = t1 + t2; |
| 50 | + } |
| 51 | + |
| 52 | + c->state[0] += a; |
| 53 | + c->state[1] += b; |
| 54 | + c->state[2] += cc; |
| 55 | + c->state[3] += d; |
| 56 | + c->state[4] += e; |
| 57 | + c->state[5] += f; |
| 58 | + c->state[6] += g; |
| 59 | + c->state[7] += h; |
| 60 | +} |
| 61 | + |
| 62 | +void cbm_sha256_init(cbm_sha256_ctx *c) { |
| 63 | + c->bitlen = 0; |
| 64 | + c->buflen = 0; |
| 65 | + c->state[0] = 0x6a09e667; |
| 66 | + c->state[1] = 0xbb67ae85; |
| 67 | + c->state[2] = 0x3c6ef372; |
| 68 | + c->state[3] = 0xa54ff53a; |
| 69 | + c->state[4] = 0x510e527f; |
| 70 | + c->state[5] = 0x9b05688c; |
| 71 | + c->state[6] = 0x1f83d9ab; |
| 72 | + c->state[7] = 0x5be0cd19; |
| 73 | +} |
| 74 | + |
| 75 | +void cbm_sha256_update(cbm_sha256_ctx *c, const void *data, size_t len) { |
| 76 | + const uint8_t *p = (const uint8_t *)data; |
| 77 | + for (size_t i = 0; i < len; i++) { |
| 78 | + c->buf[c->buflen++] = p[i]; |
| 79 | + if (c->buflen == 64) { |
| 80 | + sha256_transform(c, c->buf); |
| 81 | + c->bitlen += 512; |
| 82 | + c->buflen = 0; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void cbm_sha256_final(cbm_sha256_ctx *c, uint8_t out[CBM_SHA256_DIGEST_LEN]) { |
| 88 | + c->bitlen += (uint64_t)c->buflen * 8; |
| 89 | + |
| 90 | + size_t i = c->buflen; |
| 91 | + c->buf[i++] = 0x80; /* append the '1' bit + zero padding */ |
| 92 | + if (i > 56) { |
| 93 | + while (i < 64) { |
| 94 | + c->buf[i++] = 0; |
| 95 | + } |
| 96 | + sha256_transform(c, c->buf); |
| 97 | + i = 0; |
| 98 | + } |
| 99 | + while (i < 56) { |
| 100 | + c->buf[i++] = 0; |
| 101 | + } |
| 102 | + /* append the 64-bit big-endian message length */ |
| 103 | + for (int j = 0; j < 8; j++) { |
| 104 | + c->buf[56 + j] = (uint8_t)(c->bitlen >> (56 - 8 * j)); |
| 105 | + } |
| 106 | + sha256_transform(c, c->buf); |
| 107 | + |
| 108 | + for (int j = 0; j < 8; j++) { |
| 109 | + out[j * 4] = (uint8_t)(c->state[j] >> 24); |
| 110 | + out[j * 4 + 1] = (uint8_t)(c->state[j] >> 16); |
| 111 | + out[j * 4 + 2] = (uint8_t)(c->state[j] >> 8); |
| 112 | + out[j * 4 + 3] = (uint8_t)(c->state[j]); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void cbm_sha256_hex(const void *data, size_t len, char out[CBM_SHA256_HEX_LEN + 1]) { |
| 117 | + uint8_t digest[CBM_SHA256_DIGEST_LEN]; |
| 118 | + cbm_sha256_ctx c; |
| 119 | + cbm_sha256_init(&c); |
| 120 | + cbm_sha256_update(&c, data, len); |
| 121 | + cbm_sha256_final(&c, digest); |
| 122 | + |
| 123 | + static const char hex[] = "0123456789abcdef"; |
| 124 | + for (int i = 0; i < CBM_SHA256_DIGEST_LEN; i++) { |
| 125 | + out[i * 2] = hex[digest[i] >> 4]; |
| 126 | + out[i * 2 + 1] = hex[digest[i] & 0x0f]; |
| 127 | + } |
| 128 | + out[CBM_SHA256_HEX_LEN] = '\0'; |
| 129 | +} |
0 commit comments