# ADR 0071: Authenticated encryption in std **Status:** Proposed (2026-10-30, issue #398); needs engineering review (#44). Security-sensitive: it puts a cipher in the standard library, which [ADR 0050](0050-scram-primitives.md) and [ADR 0067](0067-signature-verification-in-std.md) left to packages. ## Context #380 set the rule for Louise's apps: data is encrypted at rest, decrypted per user, and searched in memory; equality lookups go through blind indexes (`packages/blindindex`). 0.6.0 shipped the search half. The encryption half is missing: `std/crypto` hashes, derives with PBKDF2, and verifies Ed25519, and nothing encrypts. toki2 stores note titles and bodies, label names and usernames in plaintext because of it. ADR 0067's reasons to stay in std still hold: a C binding can't take `Bytes` yet, and the runtime already links `ring` 0.17, which has ChaCha20-Poly1305, AES-GCM and HKDF. ## Decision `std/crypto` gets one AEAD and one key derivation, over `ring`, through ADR 0067's five places (the kernels in `louise_rt_prims::crypto`, the externs in `louise_rt/src/crypto.rs` with their `not_built!` fallbacks, `louise_rt.h`, the interpreter's `externs.rs`, and `louise_core`'s table): ```louise pub fn chacha20_poly1305_seal :: key: Bytes nonce: Bytes plaintext: Bytes aad: Bytes -> Bytes pub fn chacha20_poly1305_open :: key: Bytes nonce: Bytes sealed: Bytes aad: Bytes -> Result pub fn hkdf_sha256 :: secret: Bytes salt: Bytes info: Bytes length: Int -> Bytes ``` - **ChaCha20-Poly1305 (RFC 8439), not AES-256-GCM.** Both are in `ring`. ChaCha20 is constant-time in software on every target Louise builds for, including those without AES instructions; AES-GCM is only safe and fast with them. One AEAD, so callers don't choose. - **The IETF construction with 96-bit nonces, not XChaCha20.** `ring` has no XChaCha20, and adding a crate for it would be a second implementation next to the linked one. Random 96-bit nonces are safe for about 2^32 messages per key. The docs say so and point at `hkdf_sha256` to derive a key per owner or per purpose, which keeps every key far below that. - **The caller supplies the nonce.** Every `std/crypto` function is pure; drawing a nonce inside would put `` on `seal`. Callers pass `random.bytes 12` and store the nonce next to the sealed bytes. - **`seal` answers the ciphertext then the 16-byte tag**, RFC 8439's layout, so the result is one value to store. - **`open` answers a `Result`.** A sealed message comes from storage or the network, so a refusal is a value, never a panic. Every refusal (a changed byte, another key, nonce or associated data, a message shorter than its tag) has the same message: telling them apart would help an attacker, and `ring` doesn't. - **Wrong key or nonce lengths panic**, with the same message on both back ends, like ADR 0067's seed: keys and nonces are the program's own, so another length is a programming error. - **HKDF (RFC 5869), extract then expand.** `packages/blindindex` already implements expand privately; apps need the whole of it to derive data keys from one root secret. An empty salt is RFC 5869's zero salt. A length outside `0..=8160` (255 blocks) panics. - **Not `blocking`.** All three are linear in their input, at memory speed, like `sha256`. - **No key types, no encodings, no nonce bookkeeping.** `std/crypto` stays a set of primitives over raw bytes. ## Consequences - No new dependency: `ring` is already linked, and `--gc-sections` keeps the cipher out of programs that don't use it. - No ABI change: three new externs, no new block kind. - It works in the interpreter, so `louise test` covers code that seals. - `tests/run/crypto_seal.lse` checks RFC 8439 ยง2.8.2 and RFC 5869's cases 1 and 3 on both back ends, plus every refusal; `tests/run/crypto_seal_panic.lse` checks the key-length panic. The prims' unit tests repeat the vectors and the length errors. - Reusing a nonce under one key reveals the XOR of the two plaintexts and lets the tag be forged. The API can't prevent it; the docs say so. ## Alternatives considered - **XChaCha20-Poly1305 from the `chacha20poly1305` crate:** easier with random nonces (192 bits), but a second ChaCha20 next to `ring`'s, and more dependency weight for a margin per-purpose keys already give. - **AES-256-GCM:** faster with AES-NI, unsafe in software; offering both would make every caller choose. - **`seal` drawing its own nonce:** the effect would spread to every caller, and tests couldn't pin the output. - **A package:** blocked, as ADR 0067 found, until bindings take `Bytes`.