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: 1 addition & 1 deletion Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,7 +23,7 @@ subtle = { version = "2.1.1", default-features = false }
digest = { version = "0.10.5", default-features = false, features = ["alloc", "oid"] }
pkcs1 = { version = "0.4", default-features = false, features = ["pkcs8", "alloc"] }
pkcs8 = { version = "0.9", default-features = false, features = ["alloc"] }
signature = { version = "1.6.2", default-features = false , features = ["digest-preview", "rand-preview"] }
signature = { version = "1.6.4", default-features = false , features = ["digest-preview", "rand-preview"] }
zeroize = { version = "1", features = ["alloc"] }

# Temporary workaround until https://github.com/dignifiedquire/num-bigint/pull/42 lands
Expand Down
119 changes: 119 additions & 0 deletions src/pss.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,8 @@ use core::marker::PhantomData;
use core::ops::Deref;
use digest::{Digest, DynDigest, FixedOutputReset};
use rand_core::{CryptoRng, RngCore};
#[cfg(feature = "hazmat")]
use signature::hazmat::{PrehashVerifier, RandomizedPrehashSigner};
use signature::{
DigestVerifier, RandomizedDigestSigner, RandomizedSigner, Signature as SignSignature, Verifier,
};
Expand DownExpand Up@@ -607,6 +609,22 @@ where
}
}

#[cfg(feature = "hazmat")]
impl<D> RandomizedPrehashSigner<Signature> for SigningKey<D>
where
D: Digest + FixedOutputReset,
{
fn sign_prehash_with_rng(
&self,
mut rng: impl CryptoRng + RngCore,
prehash: &[u8],
) -> signature::Result<Signature> {
sign_digest::<_, _, D>(&mut rng, false, &self.inner, prehash, self.salt_len)
.map(|v| v.into())
.map_err(|e| e.into())
}
}

impl<D> AsRef<RsaPrivateKey> for SigningKey<D>
where
D: Digest,
Expand DownExpand Up@@ -705,6 +723,22 @@ where
}
}

#[cfg(feature = "hazmat")]
impl<D> RandomizedPrehashSigner<Signature> for BlindedSigningKey<D>
where
D: Digest + FixedOutputReset,
{
fn sign_prehash_with_rng(
&self,
mut rng: impl CryptoRng + RngCore,
prehash: &[u8],
) -> signature::Result<Signature> {
sign_digest::<_, _, D>(&mut rng, true, &self.inner, prehash, self.salt_len)
.map(|v| v.into())
.map_err(|e| e.into())
}
}

impl<D> AsRef<RsaPrivateKey> for BlindedSigningKey<D>
where
D: Digest,
Expand DownExpand Up@@ -821,6 +855,16 @@ where
}
}

#[cfg(feature = "hazmat")]
impl<D> PrehashVerifier<Signature> for VerifyingKey<D>
where
D: Digest + FixedOutputReset,
{
fn verify_prehash(&self, prehash: &[u8], signature: &Signature) -> signature::Result<()> {
verify_digest::<_, D>(&self.inner, prehash, signature.as_ref()).map_err(|e| e.into())
}
}

impl<D> AsRef<RsaPublicKey> for VerifyingKey<D>
where
D: Digest,
Expand All@@ -840,6 +884,8 @@ mod test {
use num_traits::{FromPrimitive, Num};
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use sha1::{Digest, Sha1};
#[cfg(feature = "hazmat")]
use signature::hazmat::{PrehashVerifier, RandomizedPrehashSigner};
use signature::{
DigestVerifier, RandomizedDigestSigner, RandomizedSigner, Signature, Verifier,
};
Expand DownExpand Up@@ -1093,4 +1139,77 @@ mod test {
.expect("failed to verify");
}
}

#[cfg(feature = "hazmat")]
#[test]
fn test_verify_pss_hazmat() {
let priv_key = get_private_key();

let tests = [
(
Sha1::digest("test\n"),
hex!(
"6f86f26b14372b2279f79fb6807c49889835c204f71e38249b4c5601462da8ae"
"30f26ffdd9c13f1c75eee172bebe7b7c89f2f1526c722833b9737d6c172a962f"
),
true,
),
(
Sha1::digest("test\n"),
hex!(
"6f86f26b14372b2279f79fb6807c49889835c204f71e38249b4c5601462da8ae"
"30f26ffdd9c13f1c75eee172bebe7b7c89f2f1526c722833b9737d6c172a962e"
),
false,
),
];
let pub_key: RsaPublicKey = priv_key.into();
let verifying_key = VerifyingKey::<Sha1>::new(pub_key);

for (text, sig, expected) in &tests {
let result = verifying_key.verify_prehash(text.as_ref(), &Signature::from_bytes(sig).unwrap());
match expected {
true => result.expect("failed to verify"),
false => {
result.expect_err("expected verifying error");
}
}
}
}

#[cfg(feature = "hazmat")]
#[test]
fn test_sign_and_verify_pss_hazmat() {
let priv_key = get_private_key();

let tests = [Sha1::digest("test\n")];
let mut rng = ChaCha8Rng::from_seed([42; 32]);
let signing_key = SigningKey::<Sha1>::new(priv_key);
let verifying_key = VerifyingKey::from(&signing_key);

for test in &tests {
let sig = signing_key.sign_prehash_with_rng(&mut rng, &test).expect("failed to sign");
verifying_key
.verify_prehash(&test, &sig)
.expect("failed to verify");
}
}

#[cfg(feature = "hazmat")]
#[test]
fn test_sign_and_verify_pss_blinded_hazmat() {
let priv_key = get_private_key();

let tests = [Sha1::digest("test\n")];
let mut rng = ChaCha8Rng::from_seed([42; 32]);
let signing_key = BlindedSigningKey::<Sha1>::new(priv_key);
let verifying_key = VerifyingKey::from(&signing_key);

for test in &tests {
let sig = signing_key.sign_prehash_with_rng(&mut rng, &test).expect("failed to sign");
verifying_key
.verify_prehash(&test, &sig)
.expect("failed to verify");
}
}
}