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
26 changes: 24 additions & 2 deletions src/pkcs1v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl SignatureScheme for Pkcs1v15Sign {
self.prefix.as_ref(),
hashed,
&BigUint::from_bytes_be(sig),
sig.len(),
)
}
}
Expand All @@ -143,6 +144,7 @@ impl SignatureScheme for Pkcs1v15Sign {
#[derive(Clone, PartialEq, Eq)]
pub struct Signature {
inner: BigUint,
len: usize,
}

impl SignatureEncoding for Signature {
Expand All @@ -155,6 +157,7 @@ impl TryFrom<&[u8]> for Signature {
fn try_from(bytes: &[u8]) -> signature::Result<Self> {
Ok(Self {
inner: BigUint::from_bytes_be(bytes),
len: bytes.len(),
})
}
}
Expand Down Expand Up @@ -260,7 +263,17 @@ fn sign<R: CryptoRngCore + ?Sized>(

/// Verifies an RSA PKCS#1 v1.5 signature.
#[inline]
fn verify(pub_key: &RsaPublicKey, prefix: &[u8], hashed: &[u8], sig: &BigUint) -> Result<()> {
fn verify(
pub_key: &RsaPublicKey,
prefix: &[u8],
hashed: &[u8],
sig: &BigUint,
sig_len: usize,
) -> Result<()> {
if sig >= pub_key.n() || sig_len != pub_key.size() {
return Err(Error::Verification);
}

let em = uint_to_be_pad(rsa_encrypt(pub_key, sig)?, pub_key.size())?;

pkcs1v15_sign_unpad(prefix, hashed, &em, pub_key.size())
Expand Down Expand Up @@ -606,6 +619,7 @@ where
&self.prefix.clone(),
&D::digest(msg),
&signature.inner,
signature.len,
)
.map_err(|e| e.into())
}
Expand All @@ -621,6 +635,7 @@ where
&self.prefix,
&digest.finalize(),
&signature.inner,
signature.len,
)
.map_err(|e| e.into())
}
Expand All @@ -631,7 +646,14 @@ where
D: Digest,
{
fn verify_prehash(&self, prehash: &[u8], signature: &Signature) -> signature::Result<()> {
verify(&self.inner, &self.prefix, prehash, &signature.inner).map_err(|e| e.into())
verify(
&self.inner,
&self.prefix,
prehash,
&signature.inner,
signature.len,
)
.map_err(|e| e.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub(crate) fn verify_digest<D>(
where
D: Digest + FixedOutputReset,
{
if sig_len != pub_key.size() {
if sig >= pub_key.n() || sig_len != pub_key.size() {
return Err(Error::Verification);
}

Expand Down