Skip to content
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

Add getrandom feature that enables builtin new_random constructors (see issue #85) #103

Closed
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ u64_backend = ["curve25519-dalek/u64_backend"]
u32_backend = ["curve25519-dalek/u32_backend"]
fiat_u64_backend = ["curve25519-dalek/fiat_u64_backend"]
fiat_u32_backend = ["curve25519-dalek/fiat_u32_backend"]
getrandom = ["rand_core/getrandom"]
18 changes: 18 additions & 0 deletions src/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ impl EphemeralSecret {

EphemeralSecret(clamp_scalar(bytes))
}

/// Generate an x25519 [`EphemeralSecret`] key using [`rand_core::OsRng`].
#[cfg(feature = "getrandom")]
pub fn new_random() -> Self {
Self::new(&mut rand_core::OsRng)
}
}

impl<'a> From<&'a EphemeralSecret> for PublicKey {
Expand Down Expand Up @@ -135,6 +141,12 @@ impl ReusableSecret {

ReusableSecret(clamp_scalar(bytes))
}

/// Generate a non-serializeable x25519 [`ReuseableSecret`] key using [`rand_core::OsRng`].
#[cfg(feature = "getrandom")]
pub fn new_random() -> Self {
Self::new(&mut rand_core::OsRng)
}
}

#[cfg(feature = "reusable_secrets")]
Expand Down Expand Up @@ -186,6 +198,12 @@ impl StaticSecret {
StaticSecret(clamp_scalar(bytes))
}

/// Generate an x25519 key key using [`rand_core::OsRng`].
#[cfg(feature = "getrandom")]
pub fn new_random() -> Self {
Self::new(&mut rand_core::OsRng)
}

/// Extract this key's bytes for serialization.
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
Expand Down