nx_signed_cookie_candidate_t187.nx
buildroot/runtime/nx_signed_cookie_candidate_t187.nx
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 · 2 importers
imports: nx_syscalls.nxnx_hmac_workspace_adapter_t187.nxnx_base64.nxnx_ct.nx
imported by: nx_cap_token_candidate_t186.nxnx_crypto_workspace_gate_t187.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 41 | const SC_ERR_SHORT: i64 = -1 |
| 42 | const SC_ERR_FORMAT: i64 = -2 |
| 43 | const SC_ERR_MAC: i64 = -3 |
| 168 | const SC_SIGNATURE_BYTES:i64=(HMAC_HASH*8+5)/6 |
| 169 | const SC_BASE64_BYTES:i64=((HMAC_HASH+2)/3)*4 |
functions
| 47 | func sc_sign_bytes(key: *u8, key_len: i64, |
| 73 | func signed_cookie_sign(value: *u8, value_len: i64, |
| 92 | func signed_cookie_verify(cookie: *u8, n: i64, |
| 126 | func main() -> i64 |
| 170 | func sc_sign_prefix_bytes()->i64 |
| 173 | func sc_sign_workspace_bytes()->i64{return sc_sign_prefix_bytes()+hmac_sha256_workspace_bytes()} |
| 174 | func sc_workspace_valid(key:*u8,kn:i64,value:*u8,vn:i64,out:*u8,on:i64,workspace:*u8,capacity:i64)->i64 |
| 182 | func sc_sign_bytes_workspace(key:*u8,key_len:i64,value:*u8,value_len:i64,out:*u8,out_cap:i64,workspace:*u8,capacity:i64)->i64 |
| 195 | func signed_cookie_sign_workspace(value:*u8,value_len:i64,key:*u8,key_len:i64,out:*u8,cap:i64,workspace:*u8,capacity:i64)->i64 |
| 207 | func sc_verify_workspace_bytes()->i64 |
| 210 | func signed_cookie_verify_workspace(cookie:*u8,n:i64,key:*u8,key_len:i64,value_len_out:*i64,workspace:*u8,capacity:i64)->i64 |