Skip to content

Commit

Permalink
fix serde tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 6, 2024
1 parent cf3548b commit 3ef5f3f
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 88 deletions.
17 changes: 9 additions & 8 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl TryFrom<pkcs8::PrivateKeyInfo<'_>> for RsaPrivateKey {
let raw_e_slice = pkcs1_key.public_exponent.as_bytes();
e_slice[8 - raw_e_slice.len()..].copy_from_slice(raw_e_slice);
let e = u64::from_be_bytes(e_slice);

let d = BoxedUint::from_be_slice(pkcs1_key.private_exponent.as_bytes(), bits)
.map_err(|_| key_malformed)?;

Expand Down Expand Up @@ -92,16 +93,16 @@ impl TryFrom<pkcs8::SubjectPublicKeyInfoRef<'_>> for RsaPublicKey {

let key_malformed = pkcs8::spki::Error::KeyMalformed;
let bits =
u32::try_from(pkcs1_key.modulus.as_bytes().len()).map_err(|_| key_malformed)? / 8;
u32::try_from(pkcs1_key.modulus.as_bytes().len()).map_err(|_| key_malformed)? * 8;
let n = BoxedUint::from_be_slice(pkcs1_key.modulus.as_bytes(), bits)
.map_err(|_| key_malformed)?;
let e = u64::from_be_bytes(
pkcs1_key
.public_exponent
.as_bytes()
.try_into()
.map_err(|_| key_malformed)?,
);

// exponent potentially needs padding
let mut e_slice = [0u8; 8];
let raw_e_slice = pkcs1_key.public_exponent.as_bytes();
e_slice[8 - raw_e_slice.len()..].copy_from_slice(raw_e_slice);
let e = u64::from_be_bytes(e_slice);

RsaPublicKey::new(n, e).map_err(|_| pkcs8::spki::Error::KeyMalformed)
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,14 +767,12 @@ mod tests {
let priv_key = RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");

let priv_tokens = [Token::Str(
"3054020100300d06092a864886f70d01010105000440303e020100020900cc6c\
6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d4\
6b68cb02046d9a09f102047b4e3a4f020500f45065cc",
"3054020100300d06092a864886f70d01010105000440303e020100020900aaadacc31e2e5119020301000102087e1710295cb2ba81020500b21fdf97020500f54c6acf02040b862461020463ed8f8d0205008bb00f5f",
)];
assert_tokens(&priv_key.clone().readable(), &priv_tokens);

let priv_tokens = [Token::Str(
"3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001",
"3024300d06092a864886f70d01010105000313003010020900aaadacc31e2e51190203010001",
)];
assert_tokens(
&RsaPublicKey::from(priv_key.clone()).readable(),
Expand Down
2 changes: 1 addition & 1 deletion src/oaep/decrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ mod tests {
let tokens = [
Token::Struct { name: "DecryptingKey", len: 4 },
Token::Str("inner"),
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc"),
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900aaadacc31e2e5119020301000102087e1710295cb2ba81020500b21fdf97020500f54c6acf02040b862461020463ed8f8d0205008bb00f5f"),
Token::Str("label"),
Token::None,
Token::Str("phantom"),
Expand Down
2 changes: 1 addition & 1 deletion src/oaep/encrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod tests {
},
Token::Str("inner"),
Token::Str(
"3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001",
"3024300d06092a864886f70d01010105000313003010020900aaadacc31e2e51190203010001",
),
Token::Str("label"),
Token::None,
Expand Down
11 changes: 2 additions & 9 deletions src/pkcs1v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ impl SignatureScheme for Pkcs1v15Sign {
self.prefix.as_ref(),
hashed,
&BoxedUint::from_be_slice(sig, sig.len() as u32 * 8)?,
sig.len(),
)
}
}
Expand Down Expand Up @@ -209,15 +208,9 @@ fn sign<R: CryptoRngCore + ?Sized>(

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

Expand Down
2 changes: 1 addition & 1 deletion src/pkcs1v15/decrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod tests {
let tokens = [
Token::Struct { name: "DecryptingKey", len: 1 },
Token::Str("inner"),
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc"),
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900aaadacc31e2e5119020301000102087e1710295cb2ba81020500b21fdf97020500f54c6acf02040b862461020463ed8f8d0205008bb00f5f"),
Token::StructEnd,
];
assert_tokens(&decrypting_key.readable(), &tokens);
Expand Down
2 changes: 1 addition & 1 deletion src/pkcs1v15/encrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod tests {
},
Token::Str("inner"),
Token::Str(
"3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001",
"3024300d06092a864886f70d01010105000313003010020900aaadacc31e2e51190203010001",
),
Token::StructEnd,
];
Expand Down
22 changes: 4 additions & 18 deletions src/pkcs1v15/signature.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! `RSASSA-PKCS1-v1_5` signatures.
use crate::algorithms::pad::uint_to_be_pad;
use ::signature::SignatureEncoding;
use alloc::{boxed::Box, string::ToString};
use alloc::boxed::Box;
use core::fmt::{Debug, Display, Formatter, LowerHex, UpperHex};
use crypto_bigint::BoxedUint;

Expand All @@ -16,10 +15,9 @@ use spki::{
/// `RSASSA-PKCS1-v1_5` signatures as described in [RFC8017 § 8.2].
///
/// [RFC8017 § 8.2]: https://datatracker.ietf.org/doc/html/rfc8017#section-8.2
#[derive(Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signature {
pub(super) inner: BoxedUint,
pub(super) len: usize,
}

impl SignatureEncoding for Signature {
Expand All @@ -40,24 +38,13 @@ impl TryFrom<&[u8]> for Signature {
Ok(Self {
// TODO: how to convert error?
inner: BoxedUint::from_be_slice(bytes, len as u32 * 8).unwrap(),
len,
})
}
}

impl From<Signature> for Box<[u8]> {
fn from(signature: Signature) -> Box<[u8]> {
uint_to_be_pad(signature.inner, signature.len)
.expect("RSASSA-PKCS1-v1_5 length invariants should've been enforced")
.into_boxed_slice()
}
}

impl Debug for Signature {
fn fmt(&self, fmt: &mut Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
fmt.debug_tuple("Signature")
.field(&self.to_string())
.finish()
signature.inner.to_be_bytes()
}
}

Expand Down Expand Up @@ -117,10 +104,9 @@ mod tests {
use serde_test::{assert_tokens, Configure, Token};
let signature = Signature {
inner: BoxedUint::from(42u32),
len: 1,
};

let tokens = [Token::Str("2a")];
let tokens = [Token::Str("000000000000002a")];
assert_tokens(&signature.readable(), &tokens);
}
}
2 changes: 1 addition & 1 deletion src/pkcs1v15/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ mod tests {
let signing_key = SigningKey::<Sha256>::new(priv_key);

let tokens = [
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc")
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900aaadacc31e2e5119020301000102087e1710295cb2ba81020500b21fdf97020500f54c6acf02040b862461020463ed8f8d0205008bb00f5f")
];

assert_tokens(&signing_key.readable(), &tokens);
Expand Down
13 changes: 2 additions & 11 deletions src/pkcs1v15/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ where
&self.prefix,
&digest.finalize(),
&signature.inner,
signature.len,
)
.map_err(|e| e.into())
}
Expand All @@ -96,14 +95,7 @@ where
D: Digest,
{
fn verify_prehash(&self, prehash: &[u8], signature: &Signature) -> signature::Result<()> {
verify(
&self.inner,
&self.prefix,
prehash,
&signature.inner,
signature.len,
)
.map_err(|e| e.into())
verify(&self.inner, &self.prefix, prehash, &signature.inner).map_err(|e| e.into())
}
}

Expand All @@ -117,7 +109,6 @@ where
&self.prefix.clone(),
&D::digest(msg),
&signature.inner,
signature.len,
)
.map_err(|e| e.into())
}
Expand Down Expand Up @@ -265,7 +256,7 @@ mod tests {
let verifying_key = VerifyingKey::<Sha256>::new(pub_key);

let tokens = [Token::Str(
"3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001",
"3024300d06092a864886f70d01010105000313003010020900aaadacc31e2e51190203010001",
)];

assert_tokens(&verifying_key.readable(), &tokens);
Expand Down
3 changes: 1 addition & 2 deletions src/pss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,13 @@ pub(crate) fn verify_digest<D>(
pub_key: &RsaPublicKey,
hashed: &[u8],
sig: &BoxedUint,
sig_len: usize,
salt_len: usize,
) -> Result<()>
where
D: Digest + FixedOutputReset,
{
let n = crate::traits::keys::PublicKeyParts::n(pub_key);
if sig >= n.as_ref() || sig_len != pub_key.size() {
if sig >= n.as_ref() || sig.bits_precision() != pub_key.n_bits_precision() {
return Err(Error::Verification);
}

Expand Down
2 changes: 1 addition & 1 deletion src/pss/blinded_signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ mod tests {
);

let tokens = [
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc")
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900aaadacc31e2e5119020301000102087e1710295cb2ba81020500b21fdf97020500f54c6acf02040b862461020463ed8f8d0205008bb00f5f")
];
assert_tokens(&signing_key.readable(), &tokens);
}
Expand Down
22 changes: 4 additions & 18 deletions src/pss/signature.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! `RSASSA-PSS` signatures.
use crate::algorithms::pad::uint_to_be_pad;
use ::signature::SignatureEncoding;
use alloc::{boxed::Box, string::ToString};
use alloc::boxed::Box;
use core::fmt::{Debug, Display, Formatter, LowerHex, UpperHex};
use crypto_bigint::BoxedUint;

Expand All @@ -16,10 +15,9 @@ use spki::{
/// `RSASSA-PSS` signatures as described in [RFC8017 § 8.1].
///
/// [RFC8017 § 8.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-8.1
#[derive(Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signature {
pub(super) inner: BoxedUint,
pub(super) len: usize,
}

impl SignatureEncoding for Signature {
Expand All @@ -38,7 +36,6 @@ impl TryFrom<&[u8]> for Signature {
fn try_from(bytes: &[u8]) -> signature::Result<Self> {
let len = bytes.len();
Ok(Self {
len,
// TODO: how to convert the error?
inner: BoxedUint::from_be_slice(bytes, len as u32 * 8).unwrap(),
})
Expand All @@ -47,17 +44,7 @@ impl TryFrom<&[u8]> for Signature {

impl From<Signature> for Box<[u8]> {
fn from(signature: Signature) -> Box<[u8]> {
uint_to_be_pad(signature.inner, signature.len)
.expect("RSASSA-PKCS1-v1_5 length invariants should've been enforced")
.into_boxed_slice()
}
}

impl Debug for Signature {
fn fmt(&self, fmt: &mut Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
fmt.debug_tuple("Signature")
.field(&self.to_string())
.finish()
signature.inner.to_be_bytes()
}
}

Expand Down Expand Up @@ -111,10 +98,9 @@ mod tests {
use serde_test::{assert_tokens, Configure, Token};
let signature = Signature {
inner: BoxedUint::from(42u32),
len: 1,
};

let tokens = [Token::Str("2a")];
let tokens = [Token::Str("000000000000002a")];
assert_tokens(&signature.readable(), &tokens);
}
}
2 changes: 1 addition & 1 deletion src/pss/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ mod tests {
let signing_key = SigningKey::<Sha256>::new(priv_key);

let tokens = [
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc")
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900aaadacc31e2e5119020301000102087e1710295cb2ba81020500b21fdf97020500f54c6acf02040b862461020463ed8f8d0205008bb00f5f")
];

assert_tokens(&signing_key.readable(), &tokens);
Expand Down
14 changes: 3 additions & 11 deletions src/pss/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ where
&self.inner,
&digest.finalize(),
&signature.inner,
signature.len,
self.salt_len,
)
.map_err(|e| e.into())
Expand All @@ -73,14 +72,8 @@ where
D: Digest + FixedOutputReset,
{
fn verify_prehash(&self, prehash: &[u8], signature: &Signature) -> signature::Result<()> {
verify_digest::<D>(
&self.inner,
prehash,
&signature.inner,
signature.len,
self.salt_len,
)
.map_err(|e| e.into())
verify_digest::<D>(&self.inner, prehash, &signature.inner, self.salt_len)
.map_err(|e| e.into())
}
}

Expand All @@ -93,7 +86,6 @@ where
&self.inner,
&D::digest(msg),
&signature.inner,
signature.len,
self.salt_len,
)
.map_err(|e| e.into())
Expand Down Expand Up @@ -236,7 +228,7 @@ mod tests {
let verifying_key = VerifyingKey::<Sha256>::new(pub_key);

let tokens = [Token::Str(
"3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001",
"3024300d06092a864886f70d01010105000313003010020900aaadacc31e2e51190203010001",
)];

assert_tokens(&verifying_key.readable(), &tokens);
Expand Down

0 comments on commit 3ef5f3f

Please sign in to comment.