Commit 15b181e2

15b181e2f756Merged

Ori Pekelman <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified

Description

feat(crypto): add sp_crypto.{c,h} -- SHA-256/HMAC/PBKDF2/B64URL/CSPRNG

A small, audit-sized crypto helper for spinel programs that need the standard cookie/JWT/password-hashing primitives without dragging in OpenSSL or libsodium. Pure C, sibling in scope to sp_bigint.c.

Surface (six functions, see lib/sp_crypto.h):

  • sp_crypto_hmac_sha256_hex(key, msg) -> 64-char hex
  • sp_crypto_hmac_sha256_b64url(key, msg) -> 43-char b64url
  • sp_crypto_b64url_encode(src) -> b64url
  • sp_crypto_b64url_decode(src) -> bytes
  • sp_crypto_pbkdf2_sha256_b64url(pw, s, n) -> 43-char b64url
  • sp_crypto_random_b64url(nbytes) -> b64url, CSPRNG-backed

Implementations:

  • SHA-256: compact public-domain FIPS-180-4 reference
  • HMAC-SHA256: RFC 2104 / 4231
  • PBKDF2-SHA256: RFC 8018, dkLen fixed at 32 (one block)
  • Base64URL: RFC 4648 §5, no padding (tolerates padded input on decode)
  • CSPRNG: arc4random_buf on BSD/macOS, /dev/urandom on Linux/POSIX

Build:

  • lib/sp_crypto.o gets archived into lib/libspinel_rt.a alongside sp_bigint.o, so any spinel program that links the runtime can ffi_func against these symbols without extra build wiring.
  • No spinel-codegen integration in this PR -- consumers ffi_func directly. A follow-up could surface these as Digest::SHA256.hexdigest / OpenSSL::HMAC.hexdigest("sha256", ...) -shaped builtins, but that's an opt-in design decision.

Testing:

  • test/sp_crypto_basic.rb exercises the surface with deterministic vectors (RFC 4231 case 2 for HMAC, b64url round-trip on a 5-byte input, PBKDF2 with iters=1, random b64url length + inequality).
  • 483/483 spinel tests pass (482 prior + 1 new).

Motivation

tep (Sinatra-shaped framework targeting spinel) maintains its own copy of these primitives in lib/tep/tep_crypto.c (split out from sphttp.c in OriPekelman/tep#2). Once this lands tep's copy becomes a thin shim and the duplicated implementations collapse to one canonical version in spinel.

Sketched with rubys's rack-surface conversation in mind: any spinel-targeted framework working in that space needs the same shape (cookies need HMAC-SHA256, JWT verification needs the same, PBKDF2 covers password hashing, secure random covers session IDs). Better to ship it once in the runtime than have each framework reinvent it.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
15b181e2f756
Parent
3f45b24309c5

MakefilemodifiedView file

@@ -204,9 +204,13 @@
204204 @mkdir -p build
205205 $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_bigint.c -o build/sp_bigint.o
206206
207+build/sp_crypto.o: lib/sp_crypto.c lib/sp_crypto.h
208+ @mkdir -p build
209+ $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_crypto.c -o build/sp_crypto.o
210+
207211 SP_RT_LIB = lib/libspinel_rt.a
208212
209-$(SP_RT_LIB): $(RE_OBJ) build/sp_bigint.o
213+$(SP_RT_LIB): $(RE_OBJ) build/sp_bigint.o build/sp_crypto.o
210214 ar rcs $@ $^
211215
212216 regexp: $(SP_RT_LIB)