diff --git a/Cargo.lock b/Cargo.lock index 6b64be4f..ddfc86f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -262,12 +262,6 @@ 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" @@ -533,7 +527,6 @@ dependencies = [ "rand_core", "rand_xorshift", "serde", - "serde_json", "serde_test", "serdect", "sha1", @@ -570,12 +563,6 @@ 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" @@ -616,17 +603,6 @@ 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 453113c3..fe3ba569 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,6 @@ 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 05cb532c..83ab2824 100644 --- a/src/oaep/decrypting_key.rs +++ b/src/oaep/decrypting_key.rs @@ -98,6 +98,16 @@ where { } +impl PartialEq for DecryptingKey +where + D: Digest, + MGD: Digest + FixedOutputReset, +{ + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner && self.label == other.label + } +} + #[cfg(test)] mod tests { #[test] @@ -105,21 +115,26 @@ mod tests { fn test_serde() { use super::*; use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng}; + use serde_test::{assert_tokens, Configure, Token}; use sha2::Sha256; let mut rng = ChaCha8Rng::from_seed([42; 32]); - let decrypting_key = DecryptingKey::::new_with_label( + let decrypting_key = DecryptingKey::::new( 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); + let tokens = [ + Token::Struct { name: "DecryptingKey", len: 4 }, + Token::Str("inner"), + Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc"), + Token::Str("label"), + Token::None, + Token::Str("phantom"), + Token::UnitStruct { name: "PhantomData", }, + Token::Str("mg_phantom"), + Token::UnitStruct { name: "PhantomData", }, + Token::StructEnd, + ]; + assert_tokens(&decrypting_key.readable(), &tokens); } } diff --git a/src/oaep/encrypting_key.rs b/src/oaep/encrypting_key.rs index f4235ed4..1565e467 100644 --- a/src/oaep/encrypting_key.rs +++ b/src/oaep/encrypting_key.rs @@ -4,11 +4,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}; /// Encryption key for PKCS#1 v1.5 encryption as described in [RFC8017 ยง 7.1]. /// @@ -66,29 +66,47 @@ where } } +impl PartialEq for EncryptingKey +where + D: Digest, + MGD: Digest + FixedOutputReset, +{ + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner && self.label == other.label + } +} + #[cfg(test)] mod tests { + #[test] #[cfg(feature = "serde")] fn test_serde() { use super::*; use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng}; - use sha2::Sha256; - - use crate::RsaPrivateKey; + use serde_test::{assert_tokens, Configure, Token}; 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"); + let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key"); + let encrypting_key = EncryptingKey::::new(priv_key.to_public_key()); - assert_eq!(encrypting_key.label, deser_encrypting_key.label); - assert_eq!(encrypting_key.inner, deser_encrypting_key.inner); + let tokens = [ + Token::Struct { + name: "EncryptingKey", + len: 4, + }, + Token::Str("inner"), + Token::Str( + "3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001", + ), + Token::Str("label"), + Token::None, + Token::Str("phantom"), + Token::UnitStruct { name: "PhantomData", }, + Token::Str("mg_phantom"), + Token::UnitStruct { name: "PhantomData", }, + Token::StructEnd, + ]; + assert_tokens(&encrypting_key.readable(), &tokens); } } diff --git a/src/pkcs1v15/decrypting_key.rs b/src/pkcs1v15/decrypting_key.rs index 1322280a..78aee178 100644 --- a/src/pkcs1v15/decrypting_key.rs +++ b/src/pkcs1v15/decrypting_key.rs @@ -72,6 +72,6 @@ mod tests { Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc"), Token::StructEnd, ]; - assert_tokens(&decrypting_key.clone().readable(), &tokens); + assert_tokens(&decrypting_key.readable(), &tokens); } } diff --git a/src/pkcs1v15/signing_key.rs b/src/pkcs1v15/signing_key.rs index b263df61..d8d4bc33 100644 --- a/src/pkcs1v15/signing_key.rs +++ b/src/pkcs1v15/signing_key.rs @@ -261,6 +261,12 @@ where impl ZeroizeOnDrop for SigningKey where D: Digest {} +impl PartialEq for SigningKey where D: Digest { + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner && self.prefix == other.prefix + } +} + #[cfg(feature = "serde")] impl Serialize for SigningKey where @@ -297,16 +303,17 @@ mod tests { fn test_serde() { use super::*; use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng}; + use serde_test::{assert_tokens, Configure, Token}; 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); + let tokens = [ + Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc") + ]; + + assert_tokens(&signing_key.readable(), &tokens); } } \ No newline at end of file diff --git a/src/pkcs1v15/verifying_key.rs b/src/pkcs1v15/verifying_key.rs index 6ca119bf..ba88a11d 100644 --- a/src/pkcs1v15/verifying_key.rs +++ b/src/pkcs1v15/verifying_key.rs @@ -211,6 +211,15 @@ where } } +impl PartialEq for VerifyingKey +where + D: Digest, +{ + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner && self.prefix == other.prefix + } +} + #[cfg(feature = "serde")] impl Serialize for VerifyingKey where @@ -247,6 +256,7 @@ mod tests { fn test_serde() { use super::*; use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng}; + use serde_test::{assert_tokens, Configure, Token}; use sha2::Sha256; let mut rng = ChaCha8Rng::from_seed([42; 32]); @@ -254,12 +264,10 @@ mod tests { 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"); + let tokens = [ + Token::Str("3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001") + ]; - assert_eq!(verifying_key.inner, deser_verifying_key.inner); - assert_eq!(verifying_key.prefix, deser_verifying_key.prefix); + assert_tokens(&verifying_key.readable(), &tokens); } } diff --git a/src/pss/blinded_signing_key.rs b/src/pss/blinded_signing_key.rs index a4c34347..b17d84b1 100644 --- a/src/pss/blinded_signing_key.rs +++ b/src/pss/blinded_signing_key.rs @@ -204,6 +204,15 @@ where impl ZeroizeOnDrop for BlindedSigningKey where D: Digest {} +impl PartialEq for BlindedSigningKey +where + D: Digest, +{ + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner && self.salt_len == other.salt_len + } +} + #[cfg(feature = "serde")] impl Serialize for BlindedSigningKey where @@ -242,15 +251,17 @@ mod tests { fn test_serde() { use super::*; use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng}; + use serde_test::{assert_tokens, Configure, Token}; 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 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); + let tokens = [ + Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc") + ]; + assert_tokens(&signing_key.readable(), &tokens); } -} \ No newline at end of file +} diff --git a/src/pss/signing_key.rs b/src/pss/signing_key.rs index 14689869..7a5e1eca 100644 --- a/src/pss/signing_key.rs +++ b/src/pss/signing_key.rs @@ -227,6 +227,15 @@ where impl ZeroizeOnDrop for SigningKey where D: Digest {} +impl PartialEq for SigningKey +where + D: Digest, +{ + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner && self.salt_len == other.salt_len + } +} + #[cfg(feature = "serde")] impl Serialize for SigningKey where @@ -265,16 +274,17 @@ mod tests { fn test_serde() { use super::*; use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng}; + use serde_test::{assert_tokens, Configure, Token}; 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); + let tokens = [ + Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc") + ]; + + assert_tokens(&signing_key.readable(), &tokens); } -} \ No newline at end of file +} diff --git a/src/pss/verifying_key.rs b/src/pss/verifying_key.rs index b8646592..9e93df5e 100644 --- a/src/pss/verifying_key.rs +++ b/src/pss/verifying_key.rs @@ -163,6 +163,15 @@ where } } +impl PartialEq for VerifyingKey +where + D: Digest, +{ + fn eq(&self, other: &Self) -> bool { + self.inner == other.inner && self.salt_len == other.salt_len + } +} + #[cfg(feature = "serde")] impl Serialize for VerifyingKey where @@ -201,6 +210,7 @@ mod tests { fn test_serde() { use super::*; use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng}; + use serde_test::{assert_tokens, Configure, Token}; use sha2::Sha256; let mut rng = ChaCha8Rng::from_seed([42; 32]); @@ -208,12 +218,10 @@ mod tests { 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"); + let tokens = [ + Token::Str("3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001") + ]; - assert_eq!(verifying_key.inner, deser_verifying_key.inner); - assert_eq!(verifying_key.salt_len, deser_verifying_key.salt_len); + assert_tokens(&verifying_key.readable(), &tokens); } }