Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Engine reference — generated from the canonical engineering source. Do not edit this page by hand.

Luna Cryptography Backends (SHA-256 / HMAC)

Luna’s digest path — client/core/age_crypto.lua — feeds the ZMQ session-key derivation, per-frame MACs, worker READY registration HMACs, consent hashes, and task/extension fingerprints. Since ** (PC-3)** that path is Zero-fork by default: no openssl process is spawned unless the operator explicitly opts in.

Backend ladder

sha256_hex(data) resolves the first usable layer, memoized for the process lifetime:

#LayerLabelNotes
1Global ffi.C OpenSSL symbolsffi-globalProduction Luna statically links libcrypto.
2ffi.load candidatesffi-loaded:<name>crypto, libcrypto.so.3, libcrypto.so, libcrypto.3.dylib, libcrypto.dylib — the same ordered list as ffi/sha1.lua.
3OpenSSL CLI (opt-in)cliOnly when LUNA_SHA256_CLI=1 (or true), or via the legacy forced-classification test seam. Writes through cel.secure_tmpfile and runs openssl dgst -sha256 -hex; one process per digest.
4Pure-Lua SHA-256pure-luaclient/ffi/sha256.lua (FIPS 180-4 / RFC 6234). Always present, so the ladder normally never reports none.

AgeCrypto.sha256_backend_label returns the selected label for diagnostics. none is reachable only through the test seam or a broken tree.

Smoke test, not symbol presence. Every FFI handle — global first, each loaded candidate after — must produce the correct SHA-256 of the empty string before it is accepted (e3b0c442…b855). The pre- probe trusted pcall(function return ffi.C.EVP_sha256 end), which accepted a handle that declared symbols but could not digest. A failed smoke test falls through to the next candidate.

sha256_backend_available never probes which openssl on the default path. The capability probe is armed only by the env opt-in or the legacy test seam, and its verdict is memoized (one probe per process at most).

HMAC construction

hmac_sha256_hex(key, data) is unchanged by the ladder work: textbook RFC 2104 over sha256_hex (ipad/opad, keys longer than 64 bytes are pre-hashed). It therefore inherits whichever zero-fork backend resolved. The nil-backend contract from holds: if no layer can digest the result is nil, "no_crypto_backend", never a raw Lua error.

CLI opt-in

LUNA_SHA256_CLI=1   # default: unset/off

Use it when OpenSSL throughput on very large buffers matters more than fork avoidance (for example bulk hashing outside the MAC’d planes). It is not recommended for the ZMQ frame path: each digest is a process spawn plus a temporary file. Values other than 1/true leave the CLI layer off.

Tests

client/tests/test_age_crypto_zero_fork.lua — NIST SHA-256 vectors (including the 1,000,000-byte 'a' vector), RFC 4231 HMAC vectors, default zero-io.popen/os.execute assertions, candidate order, smoke-test rejection of broken handles, the real backend seam (_set_sha256_backend_for_test), the mocked CLI leg, and a pure-Lua vs FFI cross-check on FFI-capable hosts. client/tests/test_age_crypto_sha256.lua — NIST vectors, label shape. client/tests/test_age_crypto_hmac_backend.lua — the nil-backend contract (cases a–e) plus the pure-Lua digest-path seam (case f). client/tests/test_ready_hmac_behavioral.lua — the ZMQ READY HMAC against the Sol-side construction.

Benchmark (pure-Lua vs FFI)

Measured 2026-10-17 on an AMD Ryzen 9 7950X (x86_64), LuaJIT 2.1 (JIT on), best of 3 runs. ffi-loaded:crypto was the resolved auto backend.

InputPure-LuaFFI (EVP)CLI (opt-in)
64 B~4.8 µs/op, ~13 MB/s~2.6 µs/op, ~23 MB/snot measured
1 KiB~25 µs/op, ~39 MB/s~2.9 µs/op, ~333 MB/s~76–92 µs/op (fork-dominated)
64 KiB~1.45 ms/op, ~43 MB/s~27 µs/op, ~2.3 GB/snot measured
1 MiB~42 ms/op, ~24 MB/s~0.4 ms/op, ~2.5 GB/snot measured

A MAC’d ZMQ frame hashes agent-sized material (tens to hundreds of bytes), so the pure-Lua floor costs roughly 5–10 µs per HMAC (two digests) versus a few microseconds on FFI — while avoiding the former per-digest CLI fallback’s process spawns and temporary files (two of each per HMAC). LUNA_SHA256_CLI=1 remains available for operators who prefer OpenSSL on large buffers.

Operational risks

Dynamic libcrypto duplication. If the host process already links a libcrypto that does not export the EVP symbols to ffi.C (static or version-skewed), the ladder ffi.loads another copy. Loading a second libcrypto is safe for hashing but costs address space and can surprise allocator/atexit behaviour; the global-first order avoids it on the normal static build. Performance floor. The pure-Lua backend is intentionally the last layer; it is fast enough for MAC-sized inputs but roughly two orders of magnitude slower than EVP on MiB-scale buffers. Operators with bulk hashing should keep libcrypto resolvable or opt into the CLI. Process-lifetime memoization. The selected backend is cached. Tests use _set_sha256_backend_for_test(kind) (ffi | pure-lua | cli | none | auto) or _reset_sha256_backend_for_test to re-resolve; production never mutates the backend at runtime.