Skip to content

Adapt to chacha20 #1642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

hpenne
Copy link

@hpenne hpenne commented Jun 15, 2025

  • Added a CHANGELOG.md entry

Summary

Replaced the dependency on rand_chacha with one on chacha20. Added some tests to std.rs to ensure that the output of StdRng did not change.

Fixes #934.

Motivation

Reduces total code size and the total amount of unsafe code.

Details

Changes to config.toml and some replacement of rand_chacha:: with chacha20::.

Added three new unit tests to std.rs. These were based on tests of IETF test vectors from rand_chacha, but the actual expected values had to be replaced, as the IETF test vectors are for ChaCha20 while rand uses ChaCha12. The expected values were generated by using rand_chacha (before chacha20 was used) to verify that the algorithm change did not affect the output.

Comment on lines +73 to +76
# Force chacha20 to use the local rand_code to avoid compiling two different "rand_core"s.
# This is necessary even if the specified versions are the same.
[patch.crates-io]
rand_core = { path = "rand_core" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to make it impossible to make releases.

Ugh. I think there are only three options here:

  1. Do this, but remove the patch when publishing. Undesirable because then the source does not correspond to the published versions.
  2. Move chacha20 source into this repo (likely undesirable; CC @tarcieri)
  3. Remove rand_core from the workspace. This will require using a patch similar to this for development whenever rand_core has breaking changes, but at least allows clean compiling otherwise.

This is why rand_chacha is co-located in this repo with external dependencies.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think crate releases ignore these directives and try to build directly against crates.io?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be a 4th option: Depend on chacha20 but without the "rng" feature, then implement the required traits for chacha inside the rand crate instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it? I wish their documentation would clarify.

cargo publish --dry-run looks okay, so this might work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But even assuming this does work, how are we supposed to handle breaking changes to rand_core in the future? If we ever make any changes incompatible with chacha20 our local builds will simply not work until upstream chacha20 gets fixed.

I don't think that's viable. (And the only reason it's not definitely unviable is that I don't expect much in the way of breaking changes to rand_core in the future.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hpenne I think the patch.crates-io approach should be fine. We use it frequently in @RustCrypto repos where we have external interdependencies

@tarcieri @newpavlov do you have a policy on breaking changes to affected crates (e.g. digest, signature)? Do you just simultaneously commit a fix to other affected repos and pin the version? Or do you never really get breaking changes which affect the build in practice?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, often we'll have a PR to the other repo ready to go.

That said we'll hopefully be wrapping up breaking changes soon and be stable for awhile.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But even assuming this does work, how are we supposed to handle breaking changes to rand_core in the future? If we ever make any changes incompatible with chacha20 our local builds will simply not work until upstream chacha20 gets fixed.

@dhardy Isn't that a deadlock situation? With the "patch"-solution, if you make changes to rand_core so that local builds fail (due to chacha being forced to use the newer version that it is not compatible with), then you cannot publish a new version of rand_core, which means that chacha20 cannot be updated to support the new rand_core version either, because there is no such version published. Can that deadlock be broken in any acceptable way? If I implement the newtype/adapter workaround above, then we can use the patch solution 99% of the time to ensure that rand_core is only built once, but if you get into this potential deadlock situation then you can remove the patch temporarily to resolve it (everything will work, but you get rand_core built twice) and put it back again when both rand_core and chacha20 has been updated. You will get to have your cake and eat it too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hpenne publishing a new version of rand_core only requires that the rand_core build succeeds, not the whole workspace, so breaking changes don't cause a deadlock, no.

I guess the patch solution is tolerable. If you do try implementing the newtype/adapter workaround, please push as a new PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hpenne publishing a new version of rand_core only requires that the rand_core build succeeds, not the whole workspace, so breaking changes don't cause a deadlock, no.

Thanks for clearing that up for me. I suppose that the problem of rand itself potentially not building in that situation due to breaking changes in rand_core can be resolved by modifying rand to fetch rand_core from crates.io for a while (using the previous release of rand_core), until chacha20 is updated to use the new rand_core and everything is in sync again so that it all builds with the local current rand_core from the rand git repo. Or perhaps there are even better ways, these kinds of sort-of-circular dependencies are a little new to me.

I guess the patch solution is tolerable. If you do try implementing the newtype/adapter workaround, please push as a new PR.

Noted. It probably won't be needed as long as the situation can be resolved by publishing any new breaking version rand_core separately.

@dhardy
Copy link
Member

dhardy commented Jun 17, 2025

I opened #1643. This PR is useful as a draft but won't be merged in its current form (likely we'll want the MSRV/edition bump first as its own PR).

I'm not certain on the timeline yet; the main blocker is the chacha20 release; we also need to decide whether we are ready to merge breaking changes to rand yet.

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.

Replacing rand_chacha with chacha20
3 participants