Skip to content

Commit

Permalink
added serde tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LWEdslev committed Mar 22, 2024
1 parent 2e57bb3 commit a7dc12a
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 5 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ base64ct = { version = "1", features = ["alloc"] }
hex-literal = "0.4.1"
proptest = "1"
serde_test = "1.0.89"
serde_json = "1.0.114"
rand_xorshift = "0.3"
rand_chacha = "0.3"
rand = "0.8"
Expand Down
30 changes: 28 additions & 2 deletions src/oaep/decrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use alloc::{
string::{String, ToString},
vec::Vec,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use core::marker::PhantomData;
use digest::{Digest, FixedOutputReset};
use rand_core::CryptoRngCore;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use zeroize::ZeroizeOnDrop;

/// Decryption key for PKCS#1 v1.5 decryption as described in [RFC8017 § 7.1].
Expand Down Expand Up @@ -97,3 +97,29 @@ where
MGD: Digest + FixedOutputReset,
{
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let decrypting_key = DecryptingKey::<Sha256>::new_with_label(
RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key"),
"label",
);

let ser_decrypting_key =
serde_json::to_string(&decrypting_key).expect("unable to serialize decrypting key");
let deser_decrypting_key = serde_json::from_str::<DecryptingKey<Sha256>>(&ser_decrypting_key)
.expect("unable to serialize decrypting key");

assert_eq!(decrypting_key.label, deser_decrypting_key.label);
assert_eq!(decrypting_key.inner, deser_decrypting_key.inner);
}
}
28 changes: 28 additions & 0 deletions src/oaep/encrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,31 @@ where
encrypt_digest::<_, D, MGD>(rng, &self.inner, msg, self.label.as_ref().cloned())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use sha2::Sha256;

use crate::RsaPrivateKey;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let encrypting_key = EncryptingKey::<Sha256>::new_with_label(
RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key").to_public_key(),
"label",
);

let ser_encrypting_key =
serde_json::to_string(&encrypting_key).expect("unable to serialize encrypting key");
let deser_encrypting_key = serde_json::from_str::<EncryptingKey<Sha256>>(&ser_encrypting_key)
.expect("unable to serialize encrypting key");

assert_eq!(encrypting_key.label, deser_encrypting_key.label);
assert_eq!(encrypting_key.inner, deser_encrypting_key.inner);
}
}
25 changes: 24 additions & 1 deletion src/pkcs1v15/decrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use zeroize::ZeroizeOnDrop;
/// Decryption key for PKCS#1 v1.5 decryption as described in [RFC8017 § 7.2].
///
/// [RFC8017 § 7.2]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.2
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DecryptingKey {
inner: RsaPrivateKey,
Expand Down Expand Up @@ -52,3 +52,26 @@ impl EncryptingKeypair for DecryptingKey {
}

impl ZeroizeOnDrop for DecryptingKey {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let decrypting_key = DecryptingKey::new(RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key"));

let tokens = [
Token::Struct { name: "DecryptingKey", len: 1 },
Token::Str("inner"),
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc"),
Token::StructEnd,
];
assert_tokens(&decrypting_key.clone().readable(), &tokens);
}
}
26 changes: 25 additions & 1 deletion src/pkcs1v15/encrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand_core::CryptoRngCore;
/// Encryption key for PKCS#1 v1.5 encryption as described in [RFC8017 § 7.2].
///
/// [RFC8017 § 7.2]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.2
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct EncryptingKey {
pub(super) inner: RsaPublicKey,
Expand All @@ -30,3 +30,27 @@ impl RandomizedEncryptor for EncryptingKey {
encrypt(rng, &self.inner, msg)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let encrypting_key = EncryptingKey::new(priv_key.to_public_key());

let tokens = [
Token::Struct { name: "EncryptingKey", len: 1 },
Token::Str("inner"),
Token::Str("3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001"),
Token::StructEnd,
];
assert_tokens(&encrypting_key.clone().readable(), &tokens);
}
}
20 changes: 20 additions & 0 deletions src/pkcs1v15/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,24 @@ impl<'de> Deserialize<'de> for Signature {
len: bytes.len(),
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use serde_test::{assert_tokens, Configure, Token};
let signature = Signature {
inner: BigUint::new(Vec::from([42])),
len: 1,
};

let tokens = [
Token::Str("2a"),
];
assert_tokens(&signature.readable(), &tokens);
}
}
22 changes: 22 additions & 0 deletions src/pkcs1v15/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,25 @@ where
Self::try_from(pki).map_err(de::Error::custom)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let signing_key = SigningKey::<Sha256>::new(priv_key);

let ser_signing_key = serde_json::to_string(&signing_key).expect("unable to serialize signing key");
let deser_signing_key = serde_json::from_str::<SigningKey<Sha256>>(&ser_signing_key).expect("unable to deserialize signing key");

assert_eq!(signing_key.inner, deser_signing_key.inner);
assert_eq!(signing_key.prefix, deser_signing_key.prefix);
}
}
27 changes: 26 additions & 1 deletion src/pkcs1v15/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use pkcs8::{
use {
pkcs8::SubjectPublicKeyInfo,
serdect::serde::{de, ser, Deserialize, Serialize},
spki::der::Decode,
spki::der::Decode,
};

use signature::{hazmat::PrehashVerifier, DigestVerifier, Verifier};
Expand Down Expand Up @@ -239,3 +239,28 @@ where
Self::try_from(spki).map_err(de::Error::custom)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let pub_key = priv_key.to_public_key();
let verifying_key = VerifyingKey::<Sha256>::new(pub_key);

let ser_verifying_key =
serde_json::to_string(&verifying_key).expect("unable to serialize verifying key");
let deser_verifying_key = serde_json::from_str::<VerifyingKey<Sha256>>(&ser_verifying_key)
.expect("unable to deserialize verifying key");

assert_eq!(verifying_key.inner, deser_verifying_key.inner);
assert_eq!(verifying_key.prefix, deser_verifying_key.prefix);
}
}
21 changes: 21 additions & 0 deletions src/pss/blinded_signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,24 @@ where
.map(Self::new)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let signing_key = BlindedSigningKey::<Sha256>::new(RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key"));

let ser_signing_key = serde_json::to_string(&signing_key).expect("unable to serialize signing key");
let deser_signing_key = serde_json::from_str::<BlindedSigningKey<Sha256>>(&ser_signing_key).expect("unable to deserialize signing key");

assert_eq!(signing_key.inner, deser_signing_key.inner);
assert_eq!(signing_key.salt_len, deser_signing_key.salt_len);
}
}
20 changes: 20 additions & 0 deletions src/pss/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,24 @@ impl<'de> Deserialize<'de> for Signature {
len: bytes.len(),
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use serde_test::{assert_tokens, Configure, Token};
let signature = Signature {
inner: BigUint::new(Vec::from([42])),
len: 1,
};

let tokens = [
Token::Str("2a"),
];
assert_tokens(&signature.readable(), &tokens);
}
}
22 changes: 22 additions & 0 deletions src/pss/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,25 @@ where
.map(Self::new)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let signing_key = SigningKey::<Sha256>::new(priv_key);

let ser_signing_key = serde_json::to_string(&signing_key).expect("unable to serialize signing key");
let deser_signing_key = serde_json::from_str::<SigningKey<Sha256>>(&ser_signing_key).expect("unable to deserialize signing key");

assert_eq!(signing_key.inner, deser_signing_key.inner);
assert_eq!(signing_key.salt_len, deser_signing_key.salt_len);
}
}
Loading

0 comments on commit a7dc12a

Please sign in to comment.