code wiki / (root) / nx_hmac.nx

nx_hmac.nx

buildroot/runtime/nx_hmac.nx

4818 B124 linesdepth 5pulls 5 transitivereach 752 importersview sourcekind tooltopic hmac
docsdependenciesstructsconstsfunctions

about

hmac.nx -- HMAC-SHA-256 (RFC 2104, FIPS 198-1). license_tier: INDEPENDENT_REDERIVE genealogy_id: international-research-sources/nist/fips_198_1 Keyed-hash MAC built on sha256.nx. Used throughout TLS 1.3 key schedule (RFC 8446 ยง7.1) as the HKDF primitive, in older TLS as Finished-message signer, and wherever we need a symmetric authenticator without a fresh nonce (unlike Poly1305). Construction (RFC 2104): HMAC(K, M) = H( (K' xor opad) || H( (K' xor ipad) || M ) ) where H = SHA-256, block size B = 64 bytes, and K' = - H(K) if len(K) > B -- pre-hash oversized keys - K || zeros if len(K) < B -- zero-pad short keys - K if len(K) == B ipad = 0x36 repeated; opad = 0x5c repeated. Why not Poly1305 everywhere: - Poly1305 is one-time-key (P4 in poly1305.nx); reusing a key across messages breaks it. HMAC is many-to-one: a long-lived HMAC key authenticates many messages safely. TLS transcripts span the whole handshake so HMAC is the right tool there. Invariants: HM1 Key processing depends only on key LENGTH, not key VALUE: short keys zero-padded, long keys pre-hashed through SHA- 256 (which is constant-time by construction). HM2 XOR loops read every byte of the block regardless of key content; no early exit. HM3 Output is exactly 32 bytes (SHA-256 digest width). References: RFC 2104 (HMAC), FIPS 198-1, NIST SP 800-107. Test vectors from RFC 4231 (HMAC-SHA-256 specifically). nx_safety_envelope: intended_use: "HMAC-SHA-256 -- TLS 1.3 MAC + HKDF foundation + general keyed-hash authentication" sil_target: SIL3 (authentication primitive; MAC

dependencies 2 imports · 38 importers

nx_syscalls.nx nx_sha256.nx nx_hmac.nx csrf_token.nx hkdf.nx jwt.nx nx_csrf_token.nx nx_drbg_hmac.nx nx_drbg_hmac_test.nx nx_edge_serve_static_test.nx nx_hkdf.nx nx_jwt.nx nx_mtls_pkcs12.nx

diagram shows first 10 each side; +0 more imports, +28 more importers in the complete lists below.

imports: nx_syscalls.nxnx_sha256.nx

imported by: csrf_token.nxhkdf.nxjwt.nxnx_csrf_token.nxnx_drbg_hmac.nxnx_drbg_hmac_test.nxnx_edge_serve_static_test.nxnx_hkdf.nxnx_jwt.nxnx_mtls_pkcs12.nxnx_opaque_3dh.nxnx_opaque_core.nxnx_opaque_envelope.nxnx_opaque_pake.nxnx_otp.nxnx_pbkdf2.nxnx_quic_handshake_sm.nxnx_reg_sign.nxnx_save_file_format.nxnx_signed_cookie.nxnx_tls12_client.nxnx_tls12_mutant.nxnx_tls12_prf.nxnx_tls12_req.nxnx_tls13_finished.nxnx_tls13_finished_test.nxnx_tls13_mtls_finished.nxnx_tls13_server_full_handshake_test.nxnx_tls13_server_session_app_data_test.nxnx_tls13_server_session_emit_sf.nxnx_tls13_server_session_recv_cf.nxnx_tls13_server_session_recv_cf_test.nxnx_tls13_server_session_run_mtls.nxnx_tor_ntor.nxnx_tor_ntor_gate.nxotp.nxpbkdf2.nxsigned_cookie.nx

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

main 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_compress_ni blk_word blk_byte sha256_final blk_set_byte ↻ sha256_compress ↻ sha256_init ↻ sha256_update ↻ sha256_final ↻

structs

none

consts

62const HMAC_BLOCK: i64 = 64 // SHA-256 block size
63const HMAC_HASH: i64 = 32 // SHA-256 output size
64const IPAD: i64 = 0x36
65const OPAD: i64 = 0x5C

functions

68func hmac_sha256(key: *u8, key_len: i64, msg: *u8, msg_len: i64,
116func main() -> i64 {