code wiki / (root) / aead.nx

aead.nx

buildroot/runtime/aead.nx

6239 B169 linesdepth 5pulls 7 transitivereach 0 importersview sourcekind tooltopic aead
docsdependenciesstructsconstsfunctions

about

aead.nx -- ChaCha20-Poly1305 AEAD (RFC 8439 section 2.8). Authenticated Encryption with Associated Data. Combines ChaCha20 (confidentiality) + Poly1305 (integrity) into a single primitive with a clean API: aead_seal(key, nonce, aad, plaintext) -> (ciphertext, tag) aead_open(key, nonce, aad, ciphertext, tag) -> (plaintext | FAIL) The AEAD construction derives a fresh Poly1305 key per message from the first 32 bytes of ChaCha20(key, nonce, counter=0), enforcing the one-time-key requirement from poly1305.nx P4. Subsequent counter values (starting at 1) encrypt the payload. MAC input format (RFC 8439 ยง2.8.1): aad || pad16(aad) || ct || pad16(ct) || len(aad) || len(ct) where pad16 zero-pads to the next 16-byte boundary and the length fields are u64-little-endian byte counts. Invariants: A1 Poly1305 otk is derived freshly per (key, nonce) pair; reusing a nonce with the same key collapses AEAD security. Callers are responsible for nonce uniqueness. A2 Tag verification uses ct_memcmp (constant-time) so failure doesn't leak the position of the first mismatched byte. A3 aead_open does NOT write plaintext when tag fails -- the output buffer is left untouched so callers cannot process partially-decrypted data. A4 Every byte of ciphertext participates in the MAC, enforced by the padding layout; no "length-extension" style attack. References: RFC 8439 section 2.8 (AEAD construction) RFC 8439 Appendix A.5 (full AEAD test vector)

dependencies 4 imports · 0 importers

syscalls.nx chacha20.nx poly1305.nx ct.nx aead.nx

imports: syscalls.nxchacha20.nxpoly1305.nxct.nx

imported by: nobody (leaf or entry point)

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

main aead_seal chacha20_block load_u32_le qr u32_mask rotl32 nx_bits_rotl32 u32_mask ↻ store_u32_le chacha20_encrypt chacha20_block ↻ pad16_len build_mac_input pad16_len ↻ store_u64_le poly1305_mac poly1305_clamp p_load_u32_le p_store_u32_le aead_open chacha20_block ↻ pad16_len ↻ build_mac_input ↻ poly1305_mac ↻ ct_memcmp ct_eq chacha20_encrypt ↻ ct_memcmp ↻

structs

none

consts

90const AEAD_MAX_BYTES: i64 = 1048576 // 1 MiB

functions

42func pad16_len(n: i64) -> i64 {
49func store_u64_le(buf: *u8, off: i64, v: i64) -> i64 {
called by 1: build_mac_input
61func build_mac_input(mac_buf: *u8,
94func aead_seal(key: *u8, nonce: *u8,
116func aead_open(key: *u8, nonce: *u8,
145func main() -> i64 {