Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,6 @@ impl Field for Scalar {
const ZERO: Self = Self(ScalarPrimitive::ZERO);
const ONE: Self = Self(ScalarPrimitive::ONE);

fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
let mut bytes = FieldBytes::default();

loop {
rng.fill_bytes(&mut bytes);
if let Some(scalar) = Self::from_repr(bytes).into() {
return scalar;
}
}
}

fn try_from_rng<R: TryRngCore + ?Sized>(rng: &mut R) -> core::result::Result<Self, R::Error> {
let mut bytes = FieldBytes::default();

Expand Down
9 changes: 8 additions & 1 deletion elliptic-curve/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use core::{borrow::Borrow, fmt};
use digest::{Digest, crypto_common::BlockSizeUser};
use group::Curve as _;
use hkdf::{Hkdf, hmac::SimpleHmac};
use rand_core::CryptoRng;
use rand_core::{CryptoRng, TryCryptoRng};
use zeroize::{Zeroize, ZeroizeOnDrop};

/// Low-level Elliptic Curve Diffie-Hellman (ECDH) function.
Expand Down Expand Up @@ -114,6 +114,13 @@ where
}
}

/// Generate a cryptographically random [`EphemeralSecret`].
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
Ok(Self {
scalar: NonZeroScalar::try_from_rng(rng)?,
})
}

/// Get the public key associated with this ephemeral secret.
///
/// The `compress` flag enables point compression.
Expand Down
18 changes: 17 additions & 1 deletion elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use subtle::{Choice, ConstantTimeEq};
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};

#[cfg(feature = "arithmetic")]
use crate::{CurveArithmetic, NonZeroScalar, PublicKey, rand_core::CryptoRng};
use crate::{
CurveArithmetic, NonZeroScalar, PublicKey,
rand_core::{CryptoRng, TryCryptoRng},
};

#[cfg(feature = "jwk")]
use crate::jwk::{JwkEcKey, JwkParameters};
Expand Down Expand Up @@ -100,6 +103,19 @@ where
}
}

/// Generate a random [`SecretKey`].
#[cfg(feature = "arithmetic")]
pub fn try_from_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> core::result::Result<Self, R::Error>
where
C: CurveArithmetic,
{
Ok(Self {
inner: NonZeroScalar::<C>::try_from_rng(rng)?.into(),
})
}

/// Create a new secret key from a scalar value.
pub fn new(scalar: ScalarPrimitive<C>) -> Self {
Self { inner: scalar }
Expand Down