Skip to content

Commit

Permalink
feat: allow generating random SigningKey's
Browse files Browse the repository at this point in the history
Allow generating random SigningKey structs directly.

Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
  • Loading branch information
lumag committed Nov 7, 2022
1 parent 5f7ef14 commit a972877
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/pkcs1v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ where
phantom: Default::default(),
}
}

/// Generate a new signing key.
pub fn random<R: CryptoRngCore>(rng: &mut R, bit_size: usize) -> Result<Self> {
Ok(Self {
inner: RsaPrivateKey::new(rng, bit_size)?,
prefix: Vec::new(),
phantom: Default::default(),
})
}
}

impl<D> From<RsaPrivateKey> for SigningKey<D>
Expand All @@ -352,14 +361,23 @@ impl<D> SigningKey<D>
where
D: Digest + AssociatedOid,
{
/// Create a new verifying key with a prefix for the digest `D`.
/// Create a new signing key with a prefix for the digest `D`.
pub fn new_with_prefix(key: RsaPrivateKey) -> Self {
Self {
inner: key,
prefix: generate_prefix::<D>(),
phantom: Default::default(),
}
}

/// Generate a new signing key with a prefix for the digest `D`.
pub fn random_with_prefix<R: CryptoRngCore>(rng: &mut R, bit_size: usize) -> Result<Self> {
Ok(Self {
inner: RsaPrivateKey::new(rng, bit_size)?,
prefix: generate_prefix::<D>(),
phantom: Default::default(),
})
}
}

impl<D> AsRef<RsaPrivateKey> for SigningKey<D>
Expand Down
22 changes: 22 additions & 0 deletions src/pss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,28 @@ where
phantom: Default::default(),
}
}

/// Generate a new random RSASSA-PSS signing key.
pub fn random<R: CryptoRngCore>(rng: &mut R, bit_size: usize) -> Result<Self> {
Ok(Self {
inner: RsaPrivateKey::new(rng, bit_size)?,
salt_len: None,
phantom: Default::default(),
})
}

/// Generate a new random RSASSA-PSS signing key with a salt of the given length.
pub fn random_with_salt_len<R: CryptoRngCore>(
rng: &mut R,
bit_size: usize,
salt_len: usize,
) -> Result<Self> {
Ok(Self {
inner: RsaPrivateKey::new(rng, bit_size)?,
salt_len: Some(salt_len),
phantom: Default::default(),
})
}
}

impl<D> From<RsaPrivateKey> for SigningKey<D>
Expand Down

0 comments on commit a972877

Please sign in to comment.