/* * sha256.h — Standalone RFC 6234 SHA-256, header-only (static inline). * No external dependencies beyond and . * * Test vector: * sha256("abc", 3, d) → * ba7816bf 8f01cfea 414140de 5dae2ec7 3b00361b bde327b6 0b82c10f 46850c58 */ #ifndef SHA256_H #define SHA256_H #include #include #include typedef struct { uint32_t state[8]; uint64_t count; /* total bits processed */ uint8_t buf[64]; /* partial block */ } sha256_ctx; /* ── Round constants (first 32 bits of cube roots of first 64 primes) ── */ static const uint32_t SHA256_K[64] = { 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 }; /* ── Bit operations ── */ #define SHA256_ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) #define SHA256_CH(e, f, g) (((e) & (f)) ^ (~(e) & (g))) #define SHA256_MAJ(a, b, c) (((a) & (b)) ^ ((a) & (c)) ^ ((b) & (c))) #define SHA256_EP0(a) (SHA256_ROTR(a, 2) ^ SHA256_ROTR(a, 13) ^ SHA256_ROTR(a, 22)) #define SHA256_EP1(e) (SHA256_ROTR(e, 6) ^ SHA256_ROTR(e, 11) ^ SHA256_ROTR(e, 25)) #define SHA256_SIG0(w)(SHA256_ROTR(w, 7) ^ SHA256_ROTR(w, 18) ^ ((w) >> 3)) #define SHA256_SIG1(w)(SHA256_ROTR(w, 17) ^ SHA256_ROTR(w, 19) ^ ((w) >> 10)) /* ── Process one 64-byte block ── */ static inline void sha256_transform(sha256_ctx *ctx, const uint8_t blk[64]) { uint32_t w[64]; uint32_t a, b, c, d, e, f, g, h, t1, t2; int i; for (i = 0; i < 16; i++) w[i] = ((uint32_t)blk[i*4 ] << 24) | ((uint32_t)blk[i*4+1] << 16) | ((uint32_t)blk[i*4+2] << 8) | (uint32_t)blk[i*4+3]; for (i = 16; i < 64; i++) w[i] = SHA256_SIG1(w[i-2]) + w[i-7] + SHA256_SIG0(w[i-15]) + w[i-16]; a = ctx->state[0]; b = ctx->state[1]; c = ctx->state[2]; d = ctx->state[3]; e = ctx->state[4]; f = ctx->state[5]; g = ctx->state[6]; h = ctx->state[7]; for (i = 0; i < 64; i++) { t1 = h + SHA256_EP1(e) + SHA256_CH(e, f, g) + SHA256_K[i] + w[i]; t2 = SHA256_EP0(a) + SHA256_MAJ(a, b, c); h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; } ctx->state[0] += a; ctx->state[1] += b; ctx->state[2] += c; ctx->state[3] += d; ctx->state[4] += e; ctx->state[5] += f; ctx->state[6] += g; ctx->state[7] += h; } /* ── Public API ── */ static inline void sha256_init(sha256_ctx *ctx) { ctx->state[0] = 0x6a09e667; ctx->state[1] = 0xbb67ae85; ctx->state[2] = 0x3c6ef372; ctx->state[3] = 0xa54ff53a; ctx->state[4] = 0x510e527f; ctx->state[5] = 0x9b05688c; ctx->state[6] = 0x1f83d9ab; ctx->state[7] = 0x5be0cd19; ctx->count = 0; memset(ctx->buf, 0, sizeof(ctx->buf)); } static inline void sha256_update(sha256_ctx *ctx, const uint8_t *data, size_t len) { /* bytes already in partial block */ size_t used = (size_t)((ctx->count / 8) % 64); ctx->count += (uint64_t)len * 8; if (used) { size_t fill = 64 - used; if (len < fill) { memcpy(ctx->buf + used, data, len); return; } memcpy(ctx->buf + used, data, fill); sha256_transform(ctx, ctx->buf); data += fill; len -= fill; } while (len >= 64) { sha256_transform(ctx, data); data += 64; len -= 64; } if (len) memcpy(ctx->buf, data, len); } static inline void sha256_final(sha256_ctx *ctx, uint8_t digest[32]) { /* Padding: append 0x80, zeros, then 64-bit big-endian bit count */ uint64_t bits = ctx->count; size_t used = (size_t)((ctx->count / 8) % 64); uint8_t pad[64]; int i; memset(pad, 0, sizeof(pad)); pad[0] = 0x80; if (used < 56) { /* Enough room in current block: pad to byte 56 */ sha256_update(ctx, pad, 56 - used); } else { /* Need an extra block: fill to end of current, then 56 zero bytes */ sha256_update(ctx, pad, 64 - used + 56); } /* Append 64-bit bit-length (big-endian) */ for (i = 0; i < 8; i++) pad[i] = (uint8_t)(bits >> (56 - 8 * i)); sha256_update(ctx, pad, 8); /* Extract digest from state */ for (i = 0; i < 8; i++) { digest[i*4 + 0] = (uint8_t)(ctx->state[i] >> 24); digest[i*4 + 1] = (uint8_t)(ctx->state[i] >> 16); digest[i*4 + 2] = (uint8_t)(ctx->state[i] >> 8); digest[i*4 + 3] = (uint8_t)(ctx->state[i] ); } } /* One-shot convenience: init + update + final */ static inline void sha256(const uint8_t *data, size_t len, uint8_t digest[32]) { sha256_ctx ctx; sha256_init(&ctx); sha256_update(&ctx, data, len); sha256_final(&ctx, digest); } #endif /* SHA256_H */