Skip to content

Add additive *_with_rng entropy-injection variants to the API - #409

Open
MatMaul wants to merge 12 commits into
matrix-org:mainfrom
tchapgouv:feature/with-rng-entropy-injection
Open

Add additive *_with_rng entropy-injection variants to the API#409
MatMaul wants to merge 12 commits into
matrix-org:mainfrom
tchapgouv:feature/with-rng-entropy-injection

Conversation

@MatMaul

@MatMaul MatMaul commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

PR addressing comments on #379.

Thanks @shakhvit for the initial work.

It also adds a disallow-default-rng feature to disable public methods that use the default RNG. This will force the caller to provide a custom RNG via with_rng methods instead.

shakhvit and others added 5 commits July 13, 2026 14:27
Every Olm key-generation path currently draws randomness from the
thread-local `rand::rng()`. This adds parallel `*_with_rng` variants that
accept a caller-supplied `impl CryptoRng`, threading it down to the leaf
`Curve25519SecretKey`/`Ed25519Keypair` constructors:

  * Account::new_with_rng
  * Account::generate_one_time_keys_with_rng
  * Account::generate_fallback_key_with_rng
  * Account::create_outbound_session_with_rng
  * Session::encrypt_with_rng (consumes rng only on a DH-ratchet advance)
  * Curve25519SecretKey::new_with_rng / Curve25519Keypair::new_with_rng
  * Ed25519Keypair::new_with_rng / Ed25519SecretKey::new_with_rng

The change is purely additive: every existing method keeps its exact body
and behaviour (still backed by `rand::rng()`), so there is zero behaviour
change for current users. The new variants enable deterministic testing,
reproducible builds, and custom/hardware entropy sources.

Each new public method documents the footgun: a low-entropy, predictable,
or reused generator produces predictable or repeated keys and breaks the
security of the protocol, so callers must pass a cryptographically secure
generator with fresh entropy per genuinely-new operation.

Adds tests/with_rng.rs covering determinism, distinct-seed divergence,
interop with the default OsRng-backed path, and the lazy DH-ratchet advance
entropy seam.
Reformat the new `*_with_rng` doc comments to the repo's nightly rustfmt config
(comment_width=80, wrap_comments) and collapse single-line signatures. Formatting
only — no API or behaviour change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011eZSbrttghGooZCgz1QKLj
cargo-mutants (incremental, --in-diff) reported 2 surviving mutants in the new
`*_with_rng` code that no test pinned down; add targeted tests so both are caught:

- Ed25519SecretKey::new_with_rng -> Default::default() (src/types/ed25519.rs:253):
  account-level tests exercise Ed25519Keypair but never the bare secret key. Add
  ed25519_secret_key_with_rng_is_seed_driven: same seed => identical key AND
  distinct seeds => distinct keys — the divergence kills a constant/Default key.

- key_id += 1 -> *= 1 in FallbackKeys::generate_fallback_key_with_rng
  (src/olm/account/fallback_keys.rs:99): no test observed the id counter advancing.
  Add fallback_key_with_rng_assigns_a_fresh_key_id_each_time: regenerating the
  fallback key must publish under a distinct KeyId, killing a non-incrementing counter.

cargo-mutants --in-diff now reports 0 missed. fmt/clippy(all+no-default)/tests green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011eZSbrttghGooZCgz1QKLj
…ightly)

The 'Run clippy' job failed with an r, not a lint: the clippy-driver
(rustc 1.99.0-nightly da80ed070, 2026-07-14) panicked in
resolver_for_lowering_raw during macro expansion (exit 101). Reproduced locally
on nightly-2026-07-14 and confirmed CLEAN on nightly-2026-07-15 — a transient
compiler bug fixed the next day. The job pins 'toolchain: nightly' (floating), so
re-running picks up the fixed toolchain. No source change: fmt/clippy/tests/mutants
all pass locally on a current nightly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011eZSbrttghGooZCgz1QKLj
@MatMaul MatMaul changed the title Feature/with rng entropy injection Add additive *_with_rng entropy-injection variants to the API Sep 1, 2026
@MatMaul
MatMaul force-pushed the feature/with-rng-entropy-injection branch 6 times, most recently from 1fcee6f to f284754 Compare September 2, 2026 07:53
@MatMaul

MatMaul commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

I tried to use that in the matrix-rust-sdk, and we would need to change a bunch of calls there to always use the with_rng versions.

@poljar in your review here you mention:
The *_with_rng() functions should be called by the current methods with the RNG set to UnwrapErr(SysRng)

That is not the current behavior, since randomness is draw from a thread local CSPNRG initialized with SysRng, and not SysRng directly.
If we want to change that to use SysRng directly by default I am all for that, since that is what I am asked to do for compliance.

@MatMaul
MatMaul force-pushed the feature/with-rng-entropy-injection branch from f284754 to 4d67e8d Compare September 2, 2026 13:53
@MatMaul

MatMaul commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Here are the matching needed changes in matrix-rust-sdk.
tchapgouv/matrix-rust-sdk@3ab3803

I haven't changed the tests yet, let's see where we want to go regarding what my open questions in previous comment.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.80519% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.95%. Comparing base (30d27bb) to head (4d67e8d).

Files with missing lines Patch % Lines
src/olm/session/double_ratchet.rs 76.92% 1 Missing and 2 partials ⚠️
src/olm/session/mod.rs 92.30% 0 Missing and 2 partials ⚠️
src/types/ed25519.rs 75.00% 2 Missing ⚠️
src/olm/session/ratchet.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #409      +/-   ##
==========================================
+ Coverage   91.72%   91.95%   +0.22%     
==========================================
  Files          40       40              
  Lines        5533     5616      +83     
  Branches     5533     5616      +83     
==========================================
+ Hits         5075     5164      +89     
+ Misses        287      281       -6     
  Partials      171      171              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@codspeed-hq

codspeed-hq Bot commented Sep 3, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 6 untouched benchmarks


Comparing tchapgouv:feature/with-rng-entropy-injection (4d67e8d) with main (30d27bb)

Open in CodSpeed

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.

2 participants