From ee09aabfa26ae310a8cf88457c2546b154e00f3a Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 9 Apr 2023 21:59:10 -0600 Subject: [PATCH] pkcs1v15: make `*_with_prefix` methods the default Renames the following: - `SigningKey::new` => `SigningKey::new_unprefixed` - `SigningKey::new_with_prefix` => `SigningKey::new` - `VerifyingKey::new` => `VerifyingKey::new_unprefixed` - `VerifyingKey::new_with_prefix` => `VerifyingKey::new` The `*_with_prefix` methods are preserved with a deprecation warning, which should help people migrate to the new versions. Closes #238 --- src/pkcs1v15.rs | 64 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/pkcs1v15.rs b/src/pkcs1v15.rs index 30d29df2..55610778 100644 --- a/src/pkcs1v15.rs +++ b/src/pkcs1v15.rs @@ -423,8 +423,8 @@ where /// /// ## Note: unprefixed signatures are uncommon /// - /// In most cases you'll want to use [`SigningKey::new_with_prefix`]. - pub fn new(key: RsaPrivateKey) -> Self { + /// In most cases you'll want to use [`SigningKey::new`]. + pub fn new_unprefixed(key: RsaPrivateKey) -> Self { Self { inner: key, prefix: Vec::new(), @@ -432,8 +432,11 @@ where } } - /// Generate a new signing key. - pub fn random(rng: &mut R, bit_size: usize) -> Result { + /// Generate a new signing key with an empty prefix. + pub fn random_unprefixed( + rng: &mut R, + bit_size: usize, + ) -> Result { Ok(Self { inner: RsaPrivateKey::new(rng, bit_size)?, prefix: Vec::new(), @@ -469,7 +472,7 @@ where D: Digest, { fn from(key: RsaPrivateKey) -> Self { - Self::new(key) + Self::new_unprefixed(key) } } @@ -487,7 +490,7 @@ where D: Digest + AssociatedOid, { /// Create a new signing key with a prefix for the digest `D`. - pub fn new_with_prefix(key: RsaPrivateKey) -> Self { + pub fn new(key: RsaPrivateKey) -> Self { Self { inner: key, prefix: generate_prefix::(), @@ -496,16 +499,28 @@ where } /// Generate a new signing key with a prefix for the digest `D`. - pub fn random_with_prefix( - rng: &mut R, - bit_size: usize, - ) -> Result { + pub fn random(rng: &mut R, bit_size: usize) -> Result { Ok(Self { inner: RsaPrivateKey::new(rng, bit_size)?, prefix: generate_prefix::(), phantom: Default::default(), }) } + + /// Create a new signing key with a prefix for the digest `D`. + #[deprecated(since = "0.9.0", note = "use SigningKey::new instead")] + pub fn new_with_prefix(key: RsaPrivateKey) -> Self { + Self::new(key) + } + + /// Generate a new signing key with a prefix for the digest `D`. + #[deprecated(since = "0.9.0", note = "use SigningKey::random instead")] + pub fn random_with_prefix( + rng: &mut R, + bit_size: usize, + ) -> Result { + Self::random(rng, bit_size) + } } impl AsRef for SigningKey @@ -624,8 +639,8 @@ where /// /// ## Note: unprefixed signatures are uncommon /// - /// In most cases you'll want to use [`VerifyingKey::new_with_prefix`]. - pub fn new(key: RsaPublicKey) -> Self { + /// In most cases you'll want to use [`VerifyingKey::new`] instead. + pub fn new_unprefixed(key: RsaPublicKey) -> Self { Self { inner: key, prefix: Vec::new(), @@ -661,7 +676,7 @@ where D: Digest, { fn from(key: RsaPublicKey) -> Self { - Self::new(key) + Self::new_unprefixed(key) } } @@ -679,13 +694,19 @@ where D: Digest + AssociatedOid, { /// Create a new verifying key with a prefix for the digest `D`. - pub fn new_with_prefix(key: RsaPublicKey) -> Self { + pub fn new(key: RsaPublicKey) -> Self { Self { inner: key, prefix: generate_prefix::(), phantom: Default::default(), } } + + /// Create a new verifying key with a prefix for the digest `D`. + #[deprecated(since = "0.9.0", note = "use VerifyingKey::new instead")] + pub fn new_with_prefix(key: RsaPublicKey) -> Self { + Self::new(key) + } } impl AsRef for VerifyingKey @@ -1064,7 +1085,7 @@ mod tests { ), )]; - let signing_key = SigningKey::::new_with_prefix(priv_key); + let signing_key = SigningKey::::new(priv_key); for (text, expected) in &tests { let out = signing_key.sign(text.as_bytes()); @@ -1090,7 +1111,7 @@ mod tests { ), )]; - let signing_key = SigningKey::::new_with_prefix(priv_key); + let signing_key = SigningKey::::new(priv_key); for (text, expected) in &tests { let out = signing_key.sign(text.as_bytes()); @@ -1115,7 +1136,7 @@ mod tests { ), )]; - let signing_key = SigningKey::::new_with_prefix(priv_key); + let signing_key = SigningKey::::new(priv_key); for (text, expected) in &tests { let out = signing_key.sign(text.as_bytes()); @@ -1140,7 +1161,7 @@ mod tests { ), )]; - let signing_key = SigningKey::new_with_prefix(priv_key); + let signing_key = SigningKey::new(priv_key); for (text, expected) in &tests { let mut digest = Sha1::new(); @@ -1218,7 +1239,7 @@ mod tests { ), ]; let pub_key: RsaPublicKey = priv_key.into(); - let verifying_key = VerifyingKey::::new_with_prefix(pub_key); + let verifying_key = VerifyingKey::::new(pub_key); for (text, sig, expected) in &tests { let result = verifying_key.verify( @@ -1257,7 +1278,7 @@ mod tests { ), ]; let pub_key: RsaPublicKey = priv_key.into(); - let verifying_key = VerifyingKey::new_with_prefix(pub_key); + let verifying_key = VerifyingKey::new(pub_key); for (text, sig, expected) in &tests { let mut digest = Sha1::new(); @@ -1272,6 +1293,7 @@ mod tests { } } } + #[test] fn test_unpadded_signature() { let msg = b"Thu Dec 19 18:06:16 EST 2013\n"; @@ -1293,7 +1315,7 @@ mod tests { let expected_sig = Base64::decode_vec("pX4DR8azytjdQ1rtUiC040FjkepuQut5q2ZFX1pTjBrOVKNjgsCDyiJDGZTCNoh9qpXYbhl7iEym30BWWwuiZg==").unwrap(); let priv_key = get_private_key(); - let signing_key = SigningKey::::new(priv_key); + let signing_key = SigningKey::::new_unprefixed(priv_key); let sig = signing_key.sign_prehash(msg).expect("Failure during sign"); assert_eq!(sig.as_ref(), expected_sig);