Skip to content

chore(deps): remove the rand dependency in favour of getrandom - #187

Merged
WingZer0o merged 10 commits into
mainfrom
deps/remove-rand
Nov 27, 2026
Merged

WingZer0o merged 10 commits into
mainfrom
deps/remove-rand

Conversation

@WingZer0o

Copy link
Copy Markdown
Member

Summary

Removes the rand crate from cas-lib. Every OS-randomness call now goes through getrandom 0.4, which was already a dependency and is now the crate's only OS RNG. Done as one child PR per sub-issue of #172, each reviewed, CI-tested on Linux/Windows/macOS and merged into this branch:

PR Sub-issue Change
#183 #180 CASArgon::generate_salt uses getrandom::fill_uninit
#184 #179 AEAD generate_key / generate_nonce / generate_kek and the seal nonce use getrandom::fill_uninit; keys go through a buffer zeroized on drop
#185 #181 SLH-DSA keygen draws the three FIPS 205 seeds from getrandom and calls slh_keygen_internal (what SigningKey::new does internally, without the rand_core 0.6 RNG); adds NIST ACVP SLH-DSA-SHAKE-128f keygen KATs
#186 #182 Drops rand from Cargo.toml / Cargo.lock (removals only), moves tests to getrandom, fixes OsRng wording in docs and CLAUDE.md

Behaviour

  • No public API changes; docs/EXAMPLES.md still compiles against cas-lib alone.
  • Key, nonce, salt lengths and encodings are unchanged (existing KATs and Wycheproof pass unedited; new tests check lengths and freshness, and that the new SLH-DSA keygen matches SigningKey::new byte-for-byte).
  • seal: an OS RNG failure now returns CasError::EncryptionFailed instead of panicking (no new CasError variants).
  • generate_key / generate_nonce / generate_kek / generate_salt / SLH-DSA keygen stay infallible and still panic if the OS RNG fails, as before; now documented under # Panics.
  • Buffers are filled from uninitialised memory (fill_uninit), so CodeQL's rust/hard-coded-cryptographic-value never sees a zero-filled key/nonce buffer.

Dependency tree

Testing

Final branch locally on Windows: cargo build --release --locked (no warnings), cargo test --release --locked 243 passed / 0 failed, cargo audit --deny unsound --deny yanked clean.

Closes #179
Closes #180
Closes #181
Closes #182

Part of #172 (stays open until #171 removes rand_core 0.6).

🤖 Generated with Claude Code

WingZer0o and others added 10 commits November 27, 2026 11:27
CASArgon::generate_salt() drew its salt with rand 0.8's OsRng, the last
rand use in the password hashers. Fill a [MaybeUninit<u8>; KDF_SALT_LENGTH]
with getrandom::fill_uninit instead, which avoids the zeroed-buffer pattern
CodeQL flags as rust/hard-coded-cryptographic-value. The signature stays
infallible and still panics if the OS RNG fails; document that under
# Panics. Add a test that two salts are KDF_SALT_LENGTH long and differ.

Part of #180.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
…getrandom instead of rand

refactor: generate the Argon2 KDF salt with getrandom instead of rand
…nternal

generate_signing_and_verification_key() drew its key from rand 0.8's OsRng
through SigningKey::new, the last place cas-lib hands slh-dsa 0.0.3 a
rand_core 0.6 RNG. SigningKey::new only fills SK.seed, SK.prf and PK.seed
(n bytes each, in that order) and builds the key from them, exactly what
the public slh_keygen_internal does with caller-supplied seeds. Draw the
three seeds with getrandom::fill_uninit into a Zeroizing buffer instead and
call slh_keygen_internal.

n is read from the parameter set's public SkLen (4n), with compile-time
checks that SkLen = 4n and VkLen = 2n, so the seeds can't have the length
slh_keygen_internal panics on. The function stays infallible and panics if
the OS RNG fails, now documented under # Panics. Key and signature
encodings are unchanged.

Tests: a unit test feeds SigningKey::new a replay RNG and checks the new
path builds the same key from the same bytes, plus NIST ACVP
SLH-DSA-keyGen-FIPS205 SLH-DSA-SHAKE-128f known-answer tests (tcId 31-40)
for slh_keygen_internal and a fresh-key sign/verify round trip.

Part of #181

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… getrandom instead of rand

refactor: generate AEAD keys and nonces with getrandom instead of rand
Nothing in src/ uses rand any more: RSA keys come from getrandom's SysRng,
and Ed25519 keys, SLH-DSA seeds, AEAD keys and nonces, and Argon2/PBKDF2
salts from getrandom::fill / fill_uninit. Drop rand 0.8 from Cargo.toml;
Cargo.lock loses rand, rand_chacha, ppv-lite86, getrandom 0.2, wasi 0.11
and zerocopy 0.8 (removals only, no remaining crate changes version).

The HKDF and password-hasher tests draw their random inputs from getrandom
directly, with alphanumeric test passwords mapped from random bytes. Docs
and comments that said OsRng now name the OS random number generator.

Part of #182.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…om seeds via slh_keygen_internal

refactor: generate SLH-DSA keys from getrandom seeds via slh_keygen_internal
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WingZer0o
WingZer0o marked this pull request as ready for review November 27, 2026 16:45
@WingZer0o
WingZer0o merged commit d3f8126 into main Nov 27, 2026
8 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

1 participant