/* * sha256.h - SHA-256 (FIPS 180-4) and HMAC-SHA256 (RFC 2104) in pure C99. * * The plugin signs outbound webhook bodies with HMAC-SHA256, so it needs a * digest that is correct rather than convenient. This implementation is the * textbook one: no tables generated at run time, no allocation, no global * state, and the two FIPS/RFC test vectors in tests/run.sh are the contract. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_WH_SHA256_H #define SX_WH_SHA256_H #include #include #define SX_SHA256_DIGEST_LEN 32 #define SX_SHA256_HEX_LEN 64 typedef struct { uint32_t h[8]; uint64_t bits; /* total message length in bits */ unsigned char buf[64]; size_t used; /* bytes currently in buf */ } sx_sha256; void sx_sha256_init(sx_sha256 *c); void sx_sha256_update(sx_sha256 *c, const void *data, size_t len); void sx_sha256_final(sx_sha256 *c, unsigned char out[SX_SHA256_DIGEST_LEN]); /* Convenience: lowercase hex + NUL into `hex` (65 bytes). */ void sx_sha256_hex(const void *data, size_t len, char hex[SX_SHA256_HEX_LEN + 1]); /* HMAC-SHA256 over `len` bytes of `msg` with `klen` bytes of key. * Writes the raw 32-byte tag into out. */ void sx_hmac_sha256(const void *key, size_t klen, const void *msg, size_t len, unsigned char out[SX_SHA256_DIGEST_LEN]); /* HMAC-SHA256 as lowercase hex + NUL (65 bytes). */ void sx_hmac_sha256_hex(const void *key, size_t klen, const void *msg, size_t len, char hex[SX_SHA256_HEX_LEN + 1]); /* Constant-time compare of two equal-length buffers. 1 when equal. * Used for signature checks so a mismatch costs the same as a match. */ int sx_ct_eq(const unsigned char *a, const unsigned char *b, size_t n); #endif /* SX_WH_SHA256_H */