SSH icon indicating copy to clipboard operation
SSH copied to clipboard

rand_core v0.9 support

Open xdevs23 opened this issue 11 months ago • 3 comments

I'm trying to use rand_chacha to generate an SSH key but it does not accept it as ssh_key::rand_core::CryptoRngCore even though it implements rand_core::CryptoRng. To me it seems like these are seen as different traits because of parameter mismatch due to ssh_key having its own rand_core re-export.

Code:

    let chacha = ChaCha20Rng::from_os_rng();
    PrivateKey::random(&mut chacha, ssh_key::Algorithm::Ed25519);

Error:

error[E0277]: the trait bound `ChaCha20Rng: CryptoRngCore` is not satisfied
   --> src/main.rs:41:24
    |
41  |     PrivateKey::random(&mut chacha, ssh_key::Algorithm::Ed25519);
    |     ------------------ ^^^^^^^^^^^ the trait `ssh_key::rand_core::CryptoRng` is not implemented for `ChaCha20Rng`, which is required by `ChaCha20Rng: CryptoRngCore`
    |     |
    |     required by a bound introduced by this call
    |
    = help: the following other types implement trait `ssh_key::rand_core::CryptoRng`:
              &'a mut R
              ssh_key::rand_core::block::BlockRng<R>
    = note: required for `ChaCha20Rng` to implement `CryptoRngCore`
note: required by a bound in `PrivateKey::random`
   --> /home/simao/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ssh-key-0.6.7/src/private.rs:488:34
    |
488 |     pub fn random(rng: &mut impl CryptoRngCore, algorithm: Algorithm) -> Result<Self> {
    |                                  ^^^^^^^^^^^^^ required by this bound in `PrivateKey::random`

The rand_core this is using appears to be rand_core 0.6.4.

Cargo.toml dependencies:

tokio = { version = "1.43.0", features = ["full"] }
clap = { version = "4.5.28", features = ["derive", "unicode"] }
hcloud = "0.21.0"
structured-logger = "1.0.3"
strum = "0.27"
strum_macros = "0.27"
log = "0.4.25"
thiserror = "2.0.11"
anyhow = "1.0.95"
ssh-key = { version = "0.6.7", features = ["ed25519"] }
rand_chacha = { version = "0.9.0", features = ["os_rng", "std"] }

Adding rand_core = { version = "0.9.1", features = ["os_rng", "std"] } does not help.

Is there a way to convert between ssh_key::rand_core and rand_core variants?

xdevs23 avatar Feb 21 '25 22:02 xdevs23

We are in the process of upgrading all of the crates in the @RustCrypto org to rand_core v0.9. You can track upstream progress here: https://github.com/RustCrypto/traits/issues/1642

tarcieri avatar Feb 21 '25 22:02 tarcieri

Oh that's great, thanks for letting me know. Is this going to be an ongoing issue where upgrades to rand_core could cause these issues? Or is it only when an API change happens? I'm guessing when rand_core reaches v1 it would only break on v2. Would you recommend using v0.6.4 in the meantime?

xdevs23 avatar Feb 22 '25 08:02 xdevs23

Yes, this problem comes up every time there’s a breaking release to rand_core

For now you can use v0.6

tarcieri avatar Feb 23 '25 11:02 tarcieri

The latest release candidates use rand_core v0.9

tarcieri avatar Jul 10 '25 22:07 tarcieri