ludic/selfhost/tests/crypto.ludic
Orkuncakilkaya 9ae69e64b4
All checks were successful
docs / build-and-deploy (push) Successful in 3s
feat(stdlib): Crypto.* — SHA-256 + HMAC-SHA256, constant-time verify (#19)
The security-sensitive counterpart to the fast, non-cryptographic Hash.*
library: standard, test-vector-backed hashing for signed saves and message
integrity, kept in its own namespace so nobody reaches for the wrong tool.

  Crypto.sha256(s)                SHA-256 -> 64-char lowercase hex
  Crypto.hmac_sha256(key, msg)    HMAC-SHA256 -> 64-char hex
  Crypto.verify_hmac(key, msg, mac)  recompute + constant-time compare -> bool
  Crypto.hex(s)                   lowercase hex of a string's bytes
  Crypto.ct_equal(a, b)           constant-time string equality

The primitives are implemented from scratch in plain integer LLVM IR
(FIPS 180-4 / RFC 2104): no libc crypto, no data-dependent branches in the
compression rounds, so a given input hashes to the same 32 bytes on every
platform and run. Digests are returned as hex strings, not raw bytes, because
a `str` is null-terminated and a raw digest can contain a NUL. MAC checks use
a non-short-circuiting compare so timing does not leak how much of a forged tag
was correct.

Emitted on demand via g_uses_cryptort, mirroring the emit_hash prelude gate.
Scoped to the deterministic, known-answer-testable core; OS-backed
random_bytes (the one piece that can't be validated by test vectors) is left
for a follow-up.

Tested against published SHA-256 vectors (empty/"abc"/fox + 55/56/64-byte
multi-block padding) and HMAC-SHA256 vectors; wired into the self-host suite as
`crypto`. Docs: a new Crypto section with honest "what this protects / does
not" guidance, one page per method, all fences checked and in the inventory.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-30 13:37:02 +03:00

36 lines
2.2 KiB
Text

program T {
entry {
# ---- Crypto.sha256 — FIPS 180-4 known-answer vectors ----
print(Crypto.sha256("")) # e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
print(Crypto.sha256("abc")) # ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
print(Crypto.sha256("The quick brown fox jumps over the lazy dog"))
# d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
# multi-block padding edge cases: 55 bytes (fits one block), 56 (spills into a
# second), 64 (an exact block that forces a whole padding block).
print(Crypto.sha256(Text.repeat("a", 55))) # 9f4390f8d30c2dd92ec9f095b65e2b9ae9b0a925a5258e241c9f1e910f734318
print(Crypto.sha256(Text.repeat("a", 56))) # b35439a4ac6f0948b6d6f9e3c6af0f5f590ce20f1bde7090ef7970686ec6738a
print(Crypto.sha256(Text.repeat("a", 64))) # ffe054fe7ae0cb6dc65c3af9b61d5209f439851db43d0ba5997337df154668eb
# ---- Crypto.hmac_sha256 — RFC-style known answers ----
print(Crypto.hmac_sha256("key", "The quick brown fox jumps over the lazy dog"))
# f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
print(Crypto.hmac_sha256("Jefe", "what do ya want for nothing?"))
# 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843
# ---- Crypto.verify_hmac — constant-time MAC check ----
let key = "s3cret"
let payload = "score=9001;level=12"
let mac = Crypto.hmac_sha256(key, payload)
if Crypto.verify_hmac(key, payload, mac) { print(1) } else { print(0) } # 1: untampered
if Crypto.verify_hmac(key, "score=9999;level=12", mac) { print(1) } else { print(0) } # 0: tampered payload
let forged = "0000000000000000000000000000000000000000000000000000000000000000"
if Crypto.verify_hmac(key, payload, forged) { print(1) } else { print(0) } # 0: forged mac
# ---- Crypto.ct_equal + Crypto.hex ----
if Crypto.ct_equal("abc", "abc") { print(1) } else { print(0) } # 1
if Crypto.ct_equal("abc", "abd") { print(1) } else { print(0) } # 0
if Crypto.ct_equal("abc", "abcd") { print(1) } else { print(0) } # 0 (unequal length)
print(Crypto.hex("abc")) # 616263
}
}