| 1 | /* |
| 2 | * sha256.c - SHA-256 (FIPS 180-4) and HMAC-SHA256 (RFC 2104), pure C99. |
| 3 | * |
| 4 | * See sha256.h for why this exists instead of a vendored crypto library: the |
| 5 | * only thing this plugin needs from a crypto stack is one hash and one MAC, |
| 6 | * and both are short enough to audit in one sitting. |
| 7 | * |
| 8 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 9 | */ |
| 10 | #define _POSIX_C_SOURCE 200809L |
| 11 | |
| 12 | #include "sha256.h" |
| 13 | |
| 14 | #include <string.h> |
| 15 | |
| 16 | static const uint32_t K[64] = { |
| 17 | 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, |
| 18 | 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u, |
| 19 | 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, |
| 20 | 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u, |
| 21 | 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, |
| 22 | 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, |
| 23 | 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u, |
| 24 | 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u, |
| 25 | 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u, |
| 26 | 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u, |
| 27 | 0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, |
| 28 | 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u, |
| 29 | 0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, |
| 30 | 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u, |
| 31 | 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, |
| 32 | 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u |
| 33 | }; |
| 34 | |
| 35 | static uint32_t rotr(uint32_t x, unsigned n) |
| 36 | { |
| 37 | return (x >> n) | (x << (32 - n)); |
| 38 | } |
| 39 | |
| 40 | static void compress(sx_sha256 *c, const unsigned char blk[64]) |
| 41 | { |
| 42 | uint32_t w[64]; |
| 43 | uint32_t a, b, d, e, f, g, h, cc; |
| 44 | unsigned i; |
| 45 | |
| 46 | for (i = 0; i < 16; i++) { |
| 47 | w[i] = ((uint32_t)blk[i * 4] << 24) | |
| 48 | ((uint32_t)blk[i * 4 + 1] << 16) | |
| 49 | ((uint32_t)blk[i * 4 + 2] << 8) | |
| 50 | ((uint32_t)blk[i * 4 + 3]); |
| 51 | } |
| 52 | for (i = 16; i < 64; i++) { |
| 53 | uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3); |
| 54 | uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10); |
| 55 | w[i] = w[i - 16] + s0 + w[i - 7] + s1; |
| 56 | } |
| 57 | |
| 58 | a = c->h[0]; b = c->h[1]; cc = c->h[2]; d = c->h[3]; |
| 59 | e = c->h[4]; f = c->h[5]; g = c->h[6]; h = c->h[7]; |
| 60 | |
| 61 | for (i = 0; i < 64; i++) { |
| 62 | uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25); |
| 63 | uint32_t ch = (e & f) ^ ((~e) & g); |
| 64 | uint32_t t1 = h + S1 + ch + K[i] + w[i]; |
| 65 | uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22); |
| 66 | uint32_t maj = (a & b) ^ (a & cc) ^ (b & cc); |
| 67 | uint32_t t2 = S0 + maj; |
| 68 | h = g; g = f; f = e; |
| 69 | e = d + t1; |
| 70 | d = cc; cc = b; b = a; |
| 71 | a = t1 + t2; |
| 72 | } |
| 73 | |
| 74 | c->h[0] += a; c->h[1] += b; c->h[2] += cc; c->h[3] += d; |
| 75 | c->h[4] += e; c->h[5] += f; c->h[6] += g; c->h[7] += h; |
| 76 | } |
| 77 | |
| 78 | void sx_sha256_init(sx_sha256 *c) |
| 79 | { |
| 80 | c->h[0] = 0x6a09e667u; c->h[1] = 0xbb67ae85u; |
| 81 | c->h[2] = 0x3c6ef372u; c->h[3] = 0xa54ff53au; |
| 82 | c->h[4] = 0x510e527fu; c->h[5] = 0x9b05688cu; |
| 83 | c->h[6] = 0x1f83d9abu; c->h[7] = 0x5be0cd19u; |
| 84 | c->bits = 0; |
| 85 | c->used = 0; |
| 86 | } |
| 87 | |
| 88 | void sx_sha256_update(sx_sha256 *c, const void *data, size_t len) |
| 89 | { |
| 90 | const unsigned char *p = (const unsigned char *)data; |
| 91 | |
| 92 | c->bits += (uint64_t)len * 8u; |
| 93 | while (len) { |
| 94 | size_t take; |
| 95 | if (c->used == 0 && len >= 64) { |
| 96 | compress(c, p); |
| 97 | p += 64; |
| 98 | len -= 64; |
| 99 | continue; |
| 100 | } |
| 101 | take = 64 - c->used; |
| 102 | if (take > len) take = len; |
| 103 | memcpy(c->buf + c->used, p, take); |
| 104 | c->used += take; |
| 105 | p += take; |
| 106 | len -= take; |
| 107 | if (c->used == 64) { |
| 108 | compress(c, c->buf); |
| 109 | c->used = 0; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void sx_sha256_final(sx_sha256 *c, unsigned char out[SX_SHA256_DIGEST_LEN]) |
| 115 | { |
| 116 | unsigned char pad[72]; |
| 117 | size_t padlen; |
| 118 | uint64_t bits = c->bits; |
| 119 | unsigned i; |
| 120 | |
| 121 | padlen = (c->used < 56) ? (56 - c->used) : (120 - c->used); |
| 122 | memset(pad, 0, sizeof pad); |
| 123 | pad[0] = 0x80; |
| 124 | /* The length field is appended by update(), which must not add to bits. */ |
| 125 | { |
| 126 | unsigned char lenbuf[8]; |
| 127 | uint64_t save = c->bits; |
| 128 | for (i = 0; i < 8; i++) |
| 129 | lenbuf[7 - i] = (unsigned char)((bits >> (i * 8)) & 0xFFu); |
| 130 | sx_sha256_update(c, pad, padlen); |
| 131 | sx_sha256_update(c, lenbuf, 8); |
| 132 | c->bits = save; |
| 133 | } |
| 134 | for (i = 0; i < 8; i++) { |
| 135 | out[i * 4] = (unsigned char)(c->h[i] >> 24); |
| 136 | out[i * 4 + 1] = (unsigned char)(c->h[i] >> 16); |
| 137 | out[i * 4 + 2] = (unsigned char)(c->h[i] >> 8); |
| 138 | out[i * 4 + 3] = (unsigned char)(c->h[i]); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void to_hex(const unsigned char *in, size_t n, char *hex) |
| 143 | { |
| 144 | static const char digits[] = "0123456789abcdef"; |
| 145 | size_t i; |
| 146 | for (i = 0; i < n; i++) { |
| 147 | hex[i * 2] = digits[in[i] >> 4]; |
| 148 | hex[i * 2 + 1] = digits[in[i] & 0x0F]; |
| 149 | } |
| 150 | hex[n * 2] = '\0'; |
| 151 | } |
| 152 | |
| 153 | void sx_sha256_hex(const void *data, size_t len, char hex[SX_SHA256_HEX_LEN + 1]) |
| 154 | { |
| 155 | sx_sha256 c; |
| 156 | unsigned char d[SX_SHA256_DIGEST_LEN]; |
| 157 | sx_sha256_init(&c); |
| 158 | sx_sha256_update(&c, data, len); |
| 159 | sx_sha256_final(&c, d); |
| 160 | to_hex(d, sizeof d, hex); |
| 161 | } |
| 162 | |
| 163 | void sx_hmac_sha256(const void *key, size_t klen, |
| 164 | const void *msg, size_t len, |
| 165 | unsigned char out[SX_SHA256_DIGEST_LEN]) |
| 166 | { |
| 167 | unsigned char k[64]; |
| 168 | unsigned char ipad[64], opad[64]; |
| 169 | unsigned char inner[SX_SHA256_DIGEST_LEN]; |
| 170 | sx_sha256 c; |
| 171 | size_t i; |
| 172 | |
| 173 | memset(k, 0, sizeof k); |
| 174 | if (klen > sizeof k) { |
| 175 | sx_sha256_init(&c); |
| 176 | sx_sha256_update(&c, key, klen); |
| 177 | sx_sha256_final(&c, k); /* hashed key is 32 bytes, rest stays 0 */ |
| 178 | } else if (klen) { |
| 179 | memcpy(k, key, klen); |
| 180 | } |
| 181 | for (i = 0; i < sizeof k; i++) { |
| 182 | ipad[i] = (unsigned char)(k[i] ^ 0x36u); |
| 183 | opad[i] = (unsigned char)(k[i] ^ 0x5cu); |
| 184 | } |
| 185 | |
| 186 | sx_sha256_init(&c); |
| 187 | sx_sha256_update(&c, ipad, sizeof ipad); |
| 188 | sx_sha256_update(&c, msg, len); |
| 189 | sx_sha256_final(&c, inner); |
| 190 | |
| 191 | sx_sha256_init(&c); |
| 192 | sx_sha256_update(&c, opad, sizeof opad); |
| 193 | sx_sha256_update(&c, inner, sizeof inner); |
| 194 | sx_sha256_final(&c, out); |
| 195 | } |
| 196 | |
| 197 | void sx_hmac_sha256_hex(const void *key, size_t klen, |
| 198 | const void *msg, size_t len, |
| 199 | char hex[SX_SHA256_HEX_LEN + 1]) |
| 200 | { |
| 201 | unsigned char d[SX_SHA256_DIGEST_LEN]; |
| 202 | sx_hmac_sha256(key, klen, msg, len, d); |
| 203 | to_hex(d, sizeof d, hex); |
| 204 | } |
| 205 | |
| 206 | int sx_ct_eq(const unsigned char *a, const unsigned char *b, size_t n) |
| 207 | { |
| 208 | unsigned char diff = 0; |
| 209 | size_t i; |
| 210 | for (i = 0; i < n; i++) diff |= (unsigned char)(a[i] ^ b[i]); |
| 211 | return diff == 0; |
| 212 | } |