Skip to content

refactor: generate AEAD keys and nonces with getrandom instead of rand - #184

Merged
WingZer0o merged 1 commit into
deps/remove-randfrom
deps/179-aead-getrandom
Nov 27, 2026
Merged

WingZer0o merged 1 commit into
deps/remove-randfrom
deps/179-aead-getrandom

Conversation

@WingZer0o

Copy link
Copy Markdown
Member

Summary

src/symmetric/aead_helpers.rs no longer uses rand. Both RNG draws now use getrandom 0.4, which the crate already depends on:

  • random_bytes::<N>() backs generate_key / generate_nonce on AES-GCM, AES-GCM-SIV, ChaCha20-Poly1305 and Ascon-AEAD128, and generate_kek on AES-KW. It now calls getrandom::fill_uninit on a Zeroizing<[MaybeUninit<u8>; N]> and copies out the initialised slice. The Standard: Distribution<[u8; N]> bound is gone.
  • The seal nonce now calls getrandom::fill_uninit on an Array::<MaybeUninit<u8>, A::NonceSize>::uninit(), replacing OsRng.fill_bytes on a defaulted Nonce<A>.

I checked every random_bytes caller (aes.rs, aes_gcm_siv.rs, chacha20poly1305.rs, aes_kw.rs, sponges/ascon_aead.rs). None of them uses rand elsewhere. Outside this chain, rand is still used only in argon2.rs and slh_dsa.rs (#180, #181). Cargo.toml / Cargo.lock are unchanged; removing the dependency is #182.

CodeQL (rust/hard-coded-cryptographic-value)

Neither path creates a zero-filled buffer that later becomes a key or nonce. Both draw into uninitialised MaybeUninit memory, and the code only uses the &mut [u8] that fill_uninit returns. The old Nonce::<A>::default() in seal was a zeroed buffer, and that is now gone too. No unsafe is added.

Zeroization

The stack buffer in random_bytes is wrapped in Zeroizing, so once the function returns, the caller's Vec is the only copy of a key. Nonces are not secret, so the seal nonce buffer is not zeroized.

Panic / error behaviour

  • generate_key / generate_nonce / generate_kek stay infallible and return Vec<u8>, so the public API is unchanged. If the OS RNG fails they panic with getrandom failure: the OS random number generator is unavailable, the same behaviour as rand's OsRng. Each trait declaration and impl now has a # Panics section, as get_ed25519_key_pair() does.
  • seal already returns CasResult, so an RNG failure there now returns CasError::EncryptionFailed instead of panicking. That is the closest existing variant, and no variant was added or renumbered. The CASAeadSeal::seal docs say so.

The OsRng mentions in the trait docs now refer to the operating system's random number generator.

Tests

  • tests/aead_seal.rs: new test generate_key_and_nonce_have_right_length_and_differ covers all six AEADs (the test table gains a key_len column).
  • tests/aes_kw.rs: new test generate_kek_has_right_length_and_differs.
  • No existing vectors or expected values were edited.

Results, run locally on Windows:

  • cargo build --release --locked: OK.
  • cargo test --release --locked: all 236 pass, including the Wycheproof tests that need the network.
  • cargo doc --no-deps: no warnings.
  • Clippy was not run because it isn't installed for the pinned toolchain on this machine.

Part of #179

🤖 Generated with Claude Code

random_bytes (behind every AEAD's generate_key / generate_nonce and the
AES-KW generate_kek) and the CASAeadSeal::seal nonce now come from
getrandom::fill_uninit on an uninitialised MaybeUninit buffer instead of
rand 0.8's OsRng. Drawing into uninitialised memory keeps CodeQL's
rust/hard-coded-cryptographic-value rule quiet, and the stack buffer in
random_bytes is wrapped in Zeroizing so the returned Vec is the only copy.

Public API is unchanged: the generators stay infallible and panic if the
OS RNG fails, as rand's OsRng did; this is now documented with # Panics
sections. seal already returns CasResult, so an RNG failure there returns
CasError::EncryptionFailed instead of panicking.

Adds tests that consecutive generate_key / generate_nonce (and
generate_kek) calls have the right lengths and differ.

Part of #179.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WingZer0o
WingZer0o merged commit 6efa0d3 into deps/remove-rand Nov 27, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant