-
Notifications
You must be signed in to change notification settings - Fork 230
crypto-common: Generate trait
#2096
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
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good (with the caveat that it should be replaced someday with rand_core or core::random). Although I would prefer to wait for the getrandom pre-release before merging.
Also, please add a changelog entry.
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 |
95eb39c to
c625773
Compare
Replaces them with the `Generate` trait (#2096) Moves documentation about generating random nonces to the `Nonce` type.
|
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.
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.