code wiki / (root) / nx_signed_cookie.nx

nx_signed_cookie.nx

buildroot/runtime/nx_signed_cookie.nx

5527 B165 linesdepth 6pulls 8 transitivereach 25 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

signed_cookie.nx -- HMAC-signed tamper-evident cookies. Browser cookies are stored client-side and editable by the user. For session state you typically want the user to HOLD the state but NOT modify it. Signed cookies solve this: cookie = value || \".\" || base64url(HMAC-SHA-256(key, value)) On read: split at the last dot, verify HMAC with secret key, reject if tampered. Same idea as Rails / Django signed cookies + Express cookie-parser's signed mode + the session cookie in tens of thousands of production Python apps. Composes hmac.nx (HMAC-SHA-256) + base64.nx (for URL-safe signature bytes) + ct.nx (constant-time compare). Invariants: SC1 Only the PAYLOAD is signed -- key isn't revealed by cookie value. Tampering changes MAC -> rejected. SC2 Verification uses ct_memcmp to eliminate timing leaks. SC3 Base64url without padding keeps cookie URL-safe + single-valued (compatible with cookie attribute grammar). SC4 No expiry here -- cookie's own Max-Age / Expires attribute handles that. Replay-attack defence requires caller to include a timestamp in the payload and enforce bounds on read.

dependencies 4 imports · 1 importers

nx_syscalls.nx nx_hmac.nx nx_base64.nx nx_ct.nx nx_signed_cookie.nx nx_cap_token.nx

imports: nx_syscalls.nxnx_hmac.nxnx_base64.nxnx_ct.nx

imported by: nx_cap_token.nx

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main sys_mmap signed_cookie_sign sc_sign_bytes sys_mmap ↻ hmac_sha256 sys_mmap ↻ sha256_digest sys_mmap ↻ sha256_init sys_mmap ↻ sha256_k sha256_update sha256_compress_ni_blocks blk_set_byte sha256_compress sha256_final blk_set_byte ↻ sha256_compress ↻ sha256_init ↻ sha256_update ↻ sha256_final ↻ b64_encode b64_enc_char signed_cookie_verify sys_mmap ↻ sc_sign_bytes ↻ ct_memcmp ct_eq

structs

none

consts

41const SC_ERR_SHORT: i64 = -1
42const SC_ERR_FORMAT: i64 = -2
43const SC_ERR_MAC: i64 = -3

functions

47func sc_sign_bytes(key: *u8, key_len: i64,
126func main() -> i64