diff --git a/Cargo.lock b/Cargo.lock index ddfc86f2..6b64be4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -262,6 +262,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + [[package]] name = "keccak" version = "0.2.0-pre.0" @@ -527,6 +533,7 @@ dependencies = [ "rand_core", "rand_xorshift", "serde", + "serde_json", "serde_test", "serdect", "sha1", @@ -563,6 +570,12 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "ryu" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" + [[package]] name = "salsa20" version = "0.10.2" @@ -603,6 +616,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_test" version = "1.0.176" diff --git a/Cargo.toml b/Cargo.toml index fe3ba569..453113c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/oaep/decrypting_key.rs b/src/oaep/decrypting_key.rs index 340d0d16..aa9a018b 100644 --- a/src/oaep/decrypting_key.rs +++ b/src/oaep/decrypting_key.rs @@ -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]. @@ -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::::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::>(&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); + } +} diff --git a/src/oaep/encrypting_key.rs b/src/oaep/encrypting_key.rs index 39053098..26aa9f0b 100644 --- a/src/oaep/encrypting_key.rs +++ b/src/oaep/encrypting_key.rs @@ -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::::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::>(&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); + } +} diff --git a/src/pkcs1v15/decrypting_key.rs b/src/pkcs1v15/decrypting_key.rs index 32982f6c..2d569b76 100644 --- a/src/pkcs1v15/decrypting_key.rs +++ b/src/pkcs1v15/decrypting_key.rs @@ -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, @@ -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); + } +} \ No newline at end of file diff --git a/src/pkcs1v15/encrypting_key.rs b/src/pkcs1v15/encrypting_key.rs index ccf5eec2..7e59fdbf 100644 --- a/src/pkcs1v15/encrypting_key.rs +++ b/src/pkcs1v15/encrypting_key.rs @@ -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, @@ -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); + } +} \ No newline at end of file diff --git a/src/pkcs1v15/signature.rs b/src/pkcs1v15/signature.rs index 8151566b..0727137f 100644 --- a/src/pkcs1v15/signature.rs +++ b/src/pkcs1v15/signature.rs @@ -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); + } } \ No newline at end of file diff --git a/src/pkcs1v15/signing_key.rs b/src/pkcs1v15/signing_key.rs index 241357fd..dbc62842 100644 --- a/src/pkcs1v15/signing_key.rs +++ b/src/pkcs1v15/signing_key.rs @@ -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::::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::>(&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); + } +} \ No newline at end of file diff --git a/src/pkcs1v15/verifying_key.rs b/src/pkcs1v15/verifying_key.rs index 35113d8c..b9ef0620 100644 --- a/src/pkcs1v15/verifying_key.rs +++ b/src/pkcs1v15/verifying_key.rs @@ -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}; @@ -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::::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::>(&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); + } +} diff --git a/src/pss/blinded_signing_key.rs b/src/pss/blinded_signing_key.rs index d641c2b6..22ce6756 100644 --- a/src/pss/blinded_signing_key.rs +++ b/src/pss/blinded_signing_key.rs @@ -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::::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::>(&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); + } +} \ No newline at end of file diff --git a/src/pss/signature.rs b/src/pss/signature.rs index e6a185b8..190c8643 100644 --- a/src/pss/signature.rs +++ b/src/pss/signature.rs @@ -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); + } } \ No newline at end of file diff --git a/src/pss/signing_key.rs b/src/pss/signing_key.rs index 105a0c63..a7b982b4 100644 --- a/src/pss/signing_key.rs +++ b/src/pss/signing_key.rs @@ -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::::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::>(&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); + } +} \ No newline at end of file diff --git a/src/pss/verifying_key.rs b/src/pss/verifying_key.rs index 6aaee591..c8c67b31 100644 --- a/src/pss/verifying_key.rs +++ b/src/pss/verifying_key.rs @@ -193,3 +193,28 @@ 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 pub_key = priv_key.to_public_key(); + let verifying_key = VerifyingKey::::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::>(&ser_verifying_key) + .expect("unable to deserialize verifying key"); + + assert_eq!(verifying_key.inner, deser_verifying_key.inner); + assert_eq!(verifying_key.salt_len, deser_verifying_key.salt_len); + } +}