/* * sha256.c - SHA-256 (FIPS 180-4) and HMAC-SHA256 (RFC 2104), pure C99. * * See sha256.h for why this exists instead of a vendored crypto library: the * only thing this plugin needs from a crypto stack is one hash and one MAC, * and both are short enough to audit in one sitting. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #define _POSIX_C_SOURCE 200809L #include "sha256.h" #include static const uint32_t K[64] = { 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u, 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u, 0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u, 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u }; static uint32_t rotr(uint32_t x, unsigned n) { return (x >> n) | (x << (32 - n)); } static void compress(sx_sha256 *c, const unsigned char blk[64]) { uint32_t w[64]; uint32_t a, b, d, e, f, g, h, cc; unsigned 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++) { uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3); uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10); w[i] = w[i - 16] + s0 + w[i - 7] + s1; } a = c->h[0]; b = c->h[1]; cc = c->h[2]; d = c->h[3]; e = c->h[4]; f = c->h[5]; g = c->h[6]; h = c->h[7]; for (i = 0; i < 64; i++) { uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25); uint32_t ch = (e & f) ^ ((~e) & g); uint32_t t1 = h + S1 + ch + K[i] + w[i]; uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22); uint32_t maj = (a & b) ^ (a & cc) ^ (b & cc); uint32_t t2 = S0 + maj; h = g; g = f; f = e; e = d + t1; d = cc; cc = b; b = a; a = t1 + t2; } c->h[0] += a; c->h[1] += b; c->h[2] += cc; c->h[3] += d; c->h[4] += e; c->h[5] += f; c->h[6] += g; c->h[7] += h; } void sx_sha256_init(sx_sha256 *c) { c->h[0] = 0x6a09e667u; c->h[1] = 0xbb67ae85u; c->h[2] = 0x3c6ef372u; c->h[3] = 0xa54ff53au; c->h[4] = 0x510e527fu; c->h[5] = 0x9b05688cu; c->h[6] = 0x1f83d9abu; c->h[7] = 0x5be0cd19u; c->bits = 0; c->used = 0; } void sx_sha256_update(sx_sha256 *c, const void *data, size_t len) { const unsigned char *p = (const unsigned char *)data; c->bits += (uint64_t)len * 8u; while (len) { size_t take; if (c->used == 0 && len >= 64) { compress(c, p); p += 64; len -= 64; continue; } take = 64 - c->used; if (take > len) take = len; memcpy(c->buf + c->used, p, take); c->used += take; p += take; len -= take; if (c->used == 64) { compress(c, c->buf); c->used = 0; } } } void sx_sha256_final(sx_sha256 *c, unsigned char out[SX_SHA256_DIGEST_LEN]) { unsigned char pad[72]; size_t padlen; uint64_t bits = c->bits; unsigned i; padlen = (c->used < 56) ? (56 - c->used) : (120 - c->used); memset(pad, 0, sizeof pad); pad[0] = 0x80; /* The length field is appended by update(), which must not add to bits. */ { unsigned char lenbuf[8]; uint64_t save = c->bits; for (i = 0; i < 8; i++) lenbuf[7 - i] = (unsigned char)((bits >> (i * 8)) & 0xFFu); sx_sha256_update(c, pad, padlen); sx_sha256_update(c, lenbuf, 8); c->bits = save; } for (i = 0; i < 8; i++) { out[i * 4] = (unsigned char)(c->h[i] >> 24); out[i * 4 + 1] = (unsigned char)(c->h[i] >> 16); out[i * 4 + 2] = (unsigned char)(c->h[i] >> 8); out[i * 4 + 3] = (unsigned char)(c->h[i]); } } static void to_hex(const unsigned char *in, size_t n, char *hex) { static const char digits[] = "0123456789abcdef"; size_t i; for (i = 0; i < n; i++) { hex[i * 2] = digits[in[i] >> 4]; hex[i * 2 + 1] = digits[in[i] & 0x0F]; } hex[n * 2] = '\0'; } void sx_sha256_hex(const void *data, size_t len, char hex[SX_SHA256_HEX_LEN + 1]) { sx_sha256 c; unsigned char d[SX_SHA256_DIGEST_LEN]; sx_sha256_init(&c); sx_sha256_update(&c, data, len); sx_sha256_final(&c, d); to_hex(d, sizeof d, hex); } void sx_hmac_sha256(const void *key, size_t klen, const void *msg, size_t len, unsigned char out[SX_SHA256_DIGEST_LEN]) { unsigned char k[64]; unsigned char ipad[64], opad[64]; unsigned char inner[SX_SHA256_DIGEST_LEN]; sx_sha256 c; size_t i; memset(k, 0, sizeof k); if (klen > sizeof k) { sx_sha256_init(&c); sx_sha256_update(&c, key, klen); sx_sha256_final(&c, k); /* hashed key is 32 bytes, rest stays 0 */ } else if (klen) { memcpy(k, key, klen); } for (i = 0; i < sizeof k; i++) { ipad[i] = (unsigned char)(k[i] ^ 0x36u); opad[i] = (unsigned char)(k[i] ^ 0x5cu); } sx_sha256_init(&c); sx_sha256_update(&c, ipad, sizeof ipad); sx_sha256_update(&c, msg, len); sx_sha256_final(&c, inner); sx_sha256_init(&c); sx_sha256_update(&c, opad, sizeof opad); sx_sha256_update(&c, inner, sizeof inner); sx_sha256_final(&c, out); } void sx_hmac_sha256_hex(const void *key, size_t klen, const void *msg, size_t len, char hex[SX_SHA256_HEX_LEN + 1]) { unsigned char d[SX_SHA256_DIGEST_LEN]; sx_hmac_sha256(key, klen, msg, len, d); to_hex(d, sizeof d, hex); } int sx_ct_eq(const unsigned char *a, const unsigned char *b, size_t n) { unsigned char diff = 0; size_t i; for (i = 0; i < n; i++) diff |= (unsigned char)(a[i] ^ b[i]); return diff == 0; }