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
2 changes: 2 additions & 0 deletions ssh-key/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ impl PrivateKey {
pub fn random(mut rng: impl CryptoRng + RngCore, algorithm: Algorithm) -> Result<Self> {
let checkint = rng.next_u32();
let key_data = match algorithm {
#[cfg(feature = "dsa")]
Algorithm::Dsa => KeypairData::from(DsaKeypair::random(rng)?),
#[cfg(any(feature = "p256", feature = "p384"))]
Algorithm::Ecdsa { curve } => KeypairData::from(EcdsaKeypair::random(rng, curve)?),
#[cfg(feature = "ed25519")]
Expand Down
18 changes: 18 additions & 0 deletions ssh-key/src/private/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::Error;
#[cfg(feature = "subtle")]
use subtle::{Choice, ConstantTimeEq};

#[cfg(all(feature = "dsa", feature = "rand_core"))]
use rand_core::{CryptoRng, RngCore};

/// Digital Signature Algorithm (DSA) private key.
///
/// Uniformly random integer `x`, such that `0 < x < q`, i.e. `x` is in the
Expand Down Expand Up @@ -147,6 +150,21 @@ pub struct DsaKeypair {
pub private: DsaPrivateKey,
}

impl DsaKeypair {
/// Key size.
#[cfg(all(feature = "dsa", feature = "rand_core"))]
#[allow(deprecated)]
pub(crate) const KEY_SIZE: dsa::KeySize = dsa::KeySize::DSA_1024_160;

/// Generate a random DSA private key.
#[cfg(all(feature = "dsa", feature = "rand_core"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "dsa", feature = "rand_core"))))]
pub fn random(mut rng: impl CryptoRng + RngCore) -> Result<Self> {
let components = dsa::Components::generate(&mut rng, Self::KEY_SIZE);
dsa::SigningKey::generate(&mut rng, components).try_into()
}
}

impl Decode for DsaKeypair {
fn decode(reader: &mut impl Reader) -> Result<Self> {
let public = DsaPublicKey::decode(reader)?;
Expand Down