Skip to content

Commit

Permalink
Add getrandom to bring convenience random init functions
Browse files Browse the repository at this point in the history
This is a reroll of dalek-cryptography#103 to correct branch

Co-authored-by: Ciprian Dorin Craciun <[email protected]>
  • Loading branch information
pinkforest and cipriancraciun committed Mar 12, 2023
1 parent 8c9c94a commit d7df5b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ harness = false

[features]
default = ["alloc", "precomputed-tables", "zeroize"]
getrandom = ["rand_core/getrandom"]
zeroize = ["dep:zeroize", "curve25519-dalek/zeroize"]
serde = ["dep:serde", "curve25519-dalek/serde"]
alloc = ["curve25519-dalek/alloc", "serde?/alloc", "zeroize?/alloc"]
Expand Down
16 changes: 16 additions & 0 deletions src/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ impl EphemeralSecret {

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

impl<'a> From<&'a EphemeralSecret> for PublicKey {
Expand Down Expand Up @@ -133,6 +138,11 @@ impl ReusableSecret {

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

#[cfg(feature = "reusable_secrets")]
Expand Down Expand Up @@ -185,6 +195,12 @@ impl StaticSecret {
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}

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

impl From<[u8; 32]> for StaticSecret {
Expand Down

0 comments on commit d7df5b4

Please sign in to comment.