Conversation
|
I think in the past One thing that's nice about defining them in |
FromRng and Generate traitsFromCryptoRng and Generate traits
|
As long as we rely on It may be worth to ask about random-value generation trait in |
Note there are two traits of note: one which uses the ambient RNG ( I personally care a lot more about the former one, which I don't think is covered by any existing traits anywhere. It's been a huge source of pain to have users deal with
Which we can't use, because it's in I find myself implementing these two patterns all over the place, but there are subtle inconsistencies. Some places are fallible and some are not. Having traits to wrap up the fallibility can reduce a lot of duplication, and generally having traits for this ensures consistency. To me having a trait for key generation which works everywhere, not just for uniformly random bytestrings, seems like a no brainer and I am somewhat perplexed why you don't want to move forward with this. The alternatives you suggested don't apply. I guess we can keep doing this all as same-shaped-inherent-methods, and try to manually adopt some consistency if you are truly unwilling to entertain traits to wrap this up. Perhaps if you don't like |
Because I think the correct place for it is I am fine with having the Also, is there a reason to keep the |
|
This PR factors and feature-gates the traits which depend on I guess we could gate the whole thing as |
|
I think we can write something like this: pub trait Generate: Sized {
// find a better name?
fn from_closure<E>(fill: impl FnMut(&mut [u8]) -> Result<(), E>) -> Result<Self, E>;
#[cfg(feature = "getrandom")]
fn generate() -> Self {
Self::try_generate().expect("RNG failure")
}
#[cfg(feature = "getrandom")]
fn fn try_generate() -> Result<Self, RngError> {
Self::from_closure(|buf| getrandom::fill(buf).map_err(RngError))
}
#[cfg(feature = "rand_core")]
fn from_rng<R: CryptoRng + ?Sized>(rng: &mut R) -> Self {
let Ok(ret) = Self::try_from_rng(rng);
ret
}
#[cfg(feature = "rand_core")]
fn try_from_rng<R: TryCryptoRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
Self::from_closure(|buf| rng.fill_bytes(buf))
}
}It also helps to reduce the implementation boilerplate. |
|
I'm not sure that sort of abstraction is going to make sense in a lot of cases. Some keygen is going to require a lot of structured randomness, e.g. RSA and DSA. |
|
Note that the closure can be called as many times as you want and with different buffer sizes. It's no different from calling Meanwhile with two separate traits you would have to either duplicate implementation or introduce a similar closure on the implementation side. |
|
I think in some cases like Otherwise this pattern needs to be implemented all the way down the stack. I think it's too onerous. |
|
In that case, accounting for rust-random/getrandom#751 we should implement all methods in terms of |
|
Oh, |
|
In the next breaking release (i.e. v0.4), most likely immediately after |
|
@newpavlov any ETA on a prerelease with that feature? |
|
Not sure. I will ping in the PR and we probably can cut a pre-release after merge. |
|
If |
FromCryptoRng and Generate traitsGenerate trait
|
@newpavlov okay, I've pushed up a new version like what you proposed, and updated the title/description accordingly. To fill the gap for now I have privately vendored |
3ce714f to
c749411
Compare
I guess I can do stacked PRs for other changes like #1897 and updating other crates to use this API. It's a bit less convenient and also means crate releases are blocked until a The only thing that will change when migrating to |
|
The |
f3631bb to
8dabf55
Compare
Replaces all of the previous RNG functionality with a `Generate` trait
which has the same basic shape as the existing methods, but allows types
to impl only `try_from_rng` and receive provided versions of all of the
other methods:
- `generate`: infallible generation using system RNG with panic on error
- `try_generate`: fallible version of above with RNG error `Result`s
- `from_rng`: infallible generation with RNG parameter that panics on
error
The `generate` and `try_generate` methods are available when the
`getrandom` feature is enabled.
Impls are provided for `hybrid_array::Array<u8, U>`.
With this approach we can generate things like symmetric keys and IVs
like:
type Aes256CbcEnc = cbc::Encryptor<aes::Aes256>;
let key = Key::<Aes256CbcEnc>::generate();
let iv = Iv::<Aes256CbcEnc>::generate();
The trait is also intended to be impl'd by consuming crates for other
key generation use cases, e.g. `RsaPrivateKey`, `dsa::SigningKey`,
`ecdsa::SigningKey`, or KEM decapsulation keys.
Replaces them with the `Generate` trait from #2096. Moves documentation about generating random nonces to the `Nonce` type.
Replaces them with the `Generate` trait introduced in #2096. Moves documentation about generating random nonces to the `Nonce` type.
It's now been a month since #2096 was merged and there still has not been a corresponding `getrandom` release. This commit temporarily internally vendors `SysRng` (which has a very small implementation) until there is a `getrandom` crate release which is usable. Once there is, this commit can be reverted.
It's now been a month since #2096 was merged and there still has not been a corresponding `getrandom` release. This commit temporarily internally vendors `SysRng` (which has a very small implementation) until there is a `getrandom` crate release which is usable. This will unblock work on rolling out the `Generate` trait. Once there is, this commit can be reverted.
This is primarily the documentation changes for the newly introduced `Generate` trait which subsumed the previous RNG APIs. See also: - RustCrypto/traits#2096 - RustCrypto/traits#2098
This is primarily the documentation changes for the newly introduced `Generate` trait which subsumed the previous RNG APIs. See also: - RustCrypto/traits#2096 - RustCrypto/traits#2098
This is primarily the documentation changes for the newly introduced `Generate` trait which subsumed the previous RNG APIs. See also: - RustCrypto/traits#2096 - RustCrypto/traits#2098
Adds a dependency on `crypto-common` (all curve implementations pretty much have a transitive one already) and replaces all of the RNG functionality with the new `Generate` trait (#2096), for the following types: - `NonZeroScalar` - `ScalarValue` - `SecretKey` - `ecdh::EphemeralSecret` Additionally `Generate` trait bounds have been added to the associated types for `CurveArithmetic`: `AffinePoint`, `ProjectivePoint`, `Scalar`
Adds a dependency on `crypto-common` (all curve implementations pretty much have a transitive one already) and replaces all of the RNG functionality with the new `Generate` trait (#2096), for the following types: - `NonZeroScalar` - `ScalarValue` - `SecretKey` - `ecdh::EphemeralSecret` Additionally `Generate` trait bounds have been added to the associated types for `CurveArithmetic`: `AffinePoint`, `ProjectivePoint`, `Scalar`
Adds a dependency on `crypto-common` (all curve implementations pretty much have a transitive one already) and replaces all of the RNG functionality with the new `Generate` trait (#2096), for the following types: - `NonZeroScalar` - `ScalarValue` - `SecretKey` - `ecdh::EphemeralSecret` Additionally `Generate` trait bounds have been added to the associated types for `CurveArithmetic`: `AffinePoint`, `ProjectivePoint`, `Scalar`
Adds a dependency on `crypto-common` (all curve implementations pretty much have a transitive one already) and replaces all of the RNG functionality with the new `Generate` trait (#2096), for the following types: - `NonZeroScalar` - `ScalarValue` - `SecretKey` - `ecdh::EphemeralSecret` Additionally `Generate` trait bounds have been added to the associated types for `CurveArithmetic`: `AffinePoint`, `ProjectivePoint`, `Scalar`
Adds a dependency on `crypto-common` (all curve implementations pretty much have a transitive one already) and replaces all of the RNG functionality with the new `Generate` trait (#2096), for the following types: - `NonZeroScalar` - `ScalarValue` - `SecretKey` - `ecdh::EphemeralSecret` Additionally `Generate` trait bounds have been added to the associated types for `CurveArithmetic`: `AffinePoint`, `ProjectivePoint`, `Scalar`
Updates that go along with RustCrypto/traits#2173, which switched the `elliptic-curve` to use the `Generate` trait introduced in RustCrypto/traits#2096
Updates that go along with RustCrypto/traits#2173, which switched the `elliptic-curve` to use the `Generate` trait introduced in RustCrypto/traits#2096
### Added - Sealed `BlockSizes` trait implemented for types from `U1` to `U255` ([#1172]) - `SerializableState` trait under `hazmat` module ([#1369]) - `OutputSize` type alias ([#1533]) - `IvState` trait ([#1636]) - `core::error::Error` impls for error types ([#1660]) - `Generate` trait as a common RNG API ([#2096], [#2145]) - `TryKeyInit` trait ([#2097]) - Re-export `getrandom` ([#2152]) - `KeyExport` trait ([#2213]) ### Changed - Replaced `generic-array` with `hybrid-array` ([#1319], [#1976]) - `BlockUser::BlockSize` is now bounded by the `BlockSizes` trait - Edition changed to 2024 and MSRV bumped to 1.85 ([#1759]) - `generate_*` methods on `KeyInit` and `KeyIvInit` traits have been deprecated in favor of the new `Generate` trait ([#2162]) - Bump `rand_core` to v0.10 ([#2250]) - Bump `getrandom` to v0.4 ([#2258]) ### Removed - `std` feature ([#1680]) [#1172]: #1172 [#1319]: #1319 [#1369]: #1369 [#1533]: #1533 [#1636]: #1636 [#1660]: #1660 [#1680]: #1680 [#1759]: #1759 [#1976]: #1976 [#2096]: #2096 [#2097]: #2097 [#2145]: #2145 [#2152]: #2152 [#2162]: #2162 [#2213]: #2213 [#2250]: #2250 [#2258]: #2258
Replaces all of the previous RNG functionality with a
Generatetrait which has the same basic shape as the existing methods, but allows types to impl onlytry_from_rngand receive provided versions of all of the other methods:generate: infallible generation using system RNG with panic on errortry_generate: fallible version of above with RNG errorResultsfrom_rng: infallible generation with RNG parameter that panics on errorThe
generateandtry_generatemethods are available when thegetrandomfeature is enabled.Impls are provided for
hybrid_array::Array<u8, U>.With this approach we can generate things like symmetric keys and IVs like:
The trait is also intended to be impl'd by consuming crates for other key generation use cases, e.g.
RsaPrivateKey,dsa::SigningKey,ecdsa::SigningKey, or KEM decapsulation keys.