From 16984a62e6fe3a169d4cb8166146d17ffa95c17d Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Sat, 17 Dec 2022 02:54:14 +0200 Subject: [PATCH 1/3] feat: swich completely to CryptoRngCore Use the new CryptoRngCore where possible instead of separate CryptoRng + RngCore. Signed-off-by: Dmitry Baryshkov --- src/internals.rs | 8 ++++---- src/oaep.rs | 8 ++++---- src/pss.rs | 1 - src/raw.rs | 6 +++--- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/internals.rs b/src/internals.rs index fc19516..1eb8de9 100644 --- a/src/internals.rs +++ b/src/internals.rs @@ -3,7 +3,7 @@ use alloc::vec; use alloc::vec::Vec; use num_bigint::{BigInt, BigUint, IntoBigInt, IntoBigUint, ModInverse, RandBigInt, ToBigInt}; use num_traits::{One, Signed, Zero}; -use rand_core::{CryptoRng, RngCore}; +use rand_core::CryptoRngCore; use zeroize::Zeroize; use crate::errors::{Error, Result}; @@ -18,7 +18,7 @@ pub fn encrypt(key: &K, m: &BigUint) -> BigUint { /// Performs raw RSA decryption with no padding, resulting in a plaintext `BigUint`. /// Peforms RSA blinding if an `Rng` is passed. #[inline] -pub fn decrypt( +pub fn decrypt( mut rng: Option<&mut R>, priv_key: &RsaPrivateKey, c: &BigUint, @@ -108,7 +108,7 @@ pub fn decrypt( /// Peforms RSA blinding if an `Rng` is passed. /// This will also check for errors in the CRT computation. #[inline] -pub fn decrypt_and_check( +pub fn decrypt_and_check( rng: Option<&mut R>, priv_key: &RsaPrivateKey, c: &BigUint, @@ -127,7 +127,7 @@ pub fn decrypt_and_check( } /// Returns the blinded c, along with the unblinding factor. -pub fn blind( +pub fn blind( rng: &mut R, key: &K, c: &BigUint, diff --git a/src/oaep.rs b/src/oaep.rs index adc166d..82d55c1 100644 --- a/src/oaep.rs +++ b/src/oaep.rs @@ -1,7 +1,7 @@ use alloc::string::String; use alloc::vec; use alloc::vec::Vec; -use rand_core::{CryptoRng, RngCore}; +use rand_core::CryptoRngCore; use digest::DynDigest; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; @@ -23,7 +23,7 @@ const MAX_LABEL_LEN: u64 = 2_305_843_009_213_693_951; /// /// [PKCS#1 OAEP]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1 #[inline] -pub fn encrypt( +pub fn encrypt( rng: &mut R, pub_key: &K, msg: &[u8], @@ -80,7 +80,7 @@ pub fn encrypt( /// /// [PKCS#1 OAEP]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1 #[inline] -pub fn decrypt( +pub fn decrypt( rng: Option<&mut R>, priv_key: &SK, ciphertext: &[u8], @@ -104,7 +104,7 @@ pub fn decrypt( /// `rng` is given. It returns one or zero in valid that indicates whether the /// plaintext was correctly structured. #[inline] -fn decrypt_inner( +fn decrypt_inner( rng: Option<&mut R>, priv_key: &SK, ciphertext: &[u8], diff --git a/src/pss.rs b/src/pss.rs index 0e321d6..528b3f0 100644 --- a/src/pss.rs +++ b/src/pss.rs @@ -136,7 +136,6 @@ where /// Note that hashed must be the result of hashing the input message using the /// given hash function. The opts argument may be nil, in which case sensible /// defaults are used. -// TODO: bind T with the CryptoRng trait pub(crate) fn sign( rng: &mut T, blind: bool, diff --git a/src/raw.rs b/src/raw.rs index 4e13c97..f8e37f9 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -1,6 +1,6 @@ use alloc::vec::Vec; use num_bigint::BigUint; -use rand_core::{CryptoRng, RngCore}; +use rand_core::CryptoRngCore; use zeroize::Zeroize; use crate::errors::{Error, Result}; @@ -14,7 +14,7 @@ pub trait EncryptionPrimitive { pub trait DecryptionPrimitive { /// Do NOT use directly! Only for implementors. - fn raw_decryption_primitive( + fn raw_decryption_primitive( &self, rng: Option<&mut R>, ciphertext: &[u8], @@ -43,7 +43,7 @@ impl EncryptionPrimitive for RsaPublicKey { } impl DecryptionPrimitive for RsaPrivateKey { - fn raw_decryption_primitive( + fn raw_decryption_primitive( &self, rng: Option<&mut R>, ciphertext: &[u8], From 4d7a152091ec356da4c3dc0f3e535977982147bc Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Sat, 17 Dec 2022 02:59:50 +0200 Subject: [PATCH 2/3] feat: switch to signature v2.0.0-pre.3 Switch to newer signature crate pre-release. Signed-off-by: Dmitry Baryshkov --- Cargo.toml | 2 +- src/internals.rs | 4 ++-- src/pkcs1v15.rs | 10 +++++----- src/pss.rs | 36 ++++++++++++++++++++++-------------- src/raw.rs | 4 ++-- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f96aab2..7b3c3d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ subtle = { version = "2.1.1", default-features = false } digest = { version = "0.10.5", default-features = false, features = ["alloc", "oid"] } pkcs1 = { version = "0.4", default-features = false, features = ["pkcs8", "alloc"] } pkcs8 = { version = "0.9", default-features = false, features = ["alloc"] } -signature = { version = "2.0.0-pre.2", default-features = false , features = ["digest-preview", "rand-preview"] } +signature = { version = "2.0.0-pre.3", default-features = false , features = ["digest-preview", "rand-preview"] } zeroize = { version = "1", features = ["alloc"] } [dependencies.serde_crate] diff --git a/src/internals.rs b/src/internals.rs index 1eb8de9..0a1e5f7 100644 --- a/src/internals.rs +++ b/src/internals.rs @@ -18,7 +18,7 @@ pub fn encrypt(key: &K, m: &BigUint) -> BigUint { /// Performs raw RSA decryption with no padding, resulting in a plaintext `BigUint`. /// Peforms RSA blinding if an `Rng` is passed. #[inline] -pub fn decrypt( +pub fn decrypt( mut rng: Option<&mut R>, priv_key: &RsaPrivateKey, c: &BigUint, @@ -108,7 +108,7 @@ pub fn decrypt( /// Peforms RSA blinding if an `Rng` is passed. /// This will also check for errors in the CRT computation. #[inline] -pub fn decrypt_and_check( +pub fn decrypt_and_check( rng: Option<&mut R>, priv_key: &RsaPrivateKey, c: &BigUint, diff --git a/src/pkcs1v15.rs b/src/pkcs1v15.rs index 7588367..e8fe3d7 100644 --- a/src/pkcs1v15.rs +++ b/src/pkcs1v15.rs @@ -161,7 +161,7 @@ pub(crate) fn decrypt( /// messages to signatures and identify the signed messages. As ever, /// signatures provide authenticity, not confidentiality. #[inline] -pub(crate) fn sign( +pub(crate) fn sign( rng: Option<&mut R>, priv_key: &SK, prefix: &[u8], @@ -420,9 +420,9 @@ impl RandomizedSigner for SigningKey where D: Digest, { - fn try_sign_with_rng( + fn try_sign_with_rng( &self, - rng: &mut impl CryptoRngCore, + rng: &mut R, msg: &[u8], ) -> signature::Result { sign(Some(rng), &self.inner, &self.prefix, &D::digest(msg)) @@ -446,9 +446,9 @@ impl RandomizedDigestSigner for SigningKey where D: Digest, { - fn try_sign_digest_with_rng( + fn try_sign_digest_with_rng( &self, - rng: &mut impl CryptoRngCore, + rng: &mut R, digest: D, ) -> signature::Result { sign(Some(rng), &self.inner, &self.prefix, &digest.finalize()) diff --git a/src/pss.rs b/src/pss.rs index 528b3f0..104c5e0 100644 --- a/src/pss.rs +++ b/src/pss.rs @@ -149,7 +149,11 @@ pub(crate) fn sign( sign_pss_with_salt(blind.then(|| rng), priv_key, hashed, &salt, digest) } -pub(crate) fn sign_digest( +pub(crate) fn sign_digest< + T: CryptoRngCore + ?Sized, + SK: PrivateKey, + D: Digest + FixedOutputReset, +>( rng: &mut T, blind: bool, priv_key: &SK, @@ -193,7 +197,11 @@ fn sign_pss_with_salt( priv_key.raw_decryption_primitive(blind_rng, &em, priv_key.size()) } -fn sign_pss_with_salt_digest( +fn sign_pss_with_salt_digest< + T: CryptoRngCore + ?Sized, + SK: PrivateKey, + D: Digest + FixedOutputReset, +>( blind_rng: Option<&mut T>, priv_key: &SK, hashed: &[u8], @@ -625,9 +633,9 @@ impl RandomizedSigner for SigningKey where D: Digest + FixedOutputReset, { - fn try_sign_with_rng( + fn try_sign_with_rng( &self, - rng: &mut impl CryptoRngCore, + rng: &mut R, msg: &[u8], ) -> signature::Result { sign_digest::<_, _, D>(rng, false, &self.inner, &D::digest(msg), self.salt_len) @@ -640,9 +648,9 @@ impl RandomizedDigestSigner for SigningKey where D: Digest + FixedOutputReset, { - fn try_sign_digest_with_rng( + fn try_sign_digest_with_rng( &self, - rng: &mut impl CryptoRngCore, + rng: &mut R, digest: D, ) -> signature::Result { sign_digest::<_, _, D>(rng, false, &self.inner, &digest.finalize(), self.salt_len) @@ -655,9 +663,9 @@ impl RandomizedPrehashSigner for SigningKey where D: Digest + FixedOutputReset, { - fn sign_prehash_with_rng( + fn sign_prehash_with_rng( &self, - rng: &mut impl CryptoRngCore, + rng: &mut R, prehash: &[u8], ) -> signature::Result { sign_digest::<_, _, D>(rng, false, &self.inner, prehash, self.salt_len) @@ -756,9 +764,9 @@ impl RandomizedSigner for BlindedSigningKey where D: Digest + FixedOutputReset, { - fn try_sign_with_rng( + fn try_sign_with_rng( &self, - rng: &mut impl CryptoRngCore, + rng: &mut R, msg: &[u8], ) -> signature::Result { sign_digest::<_, _, D>(rng, true, &self.inner, &D::digest(msg), self.salt_len) @@ -771,9 +779,9 @@ impl RandomizedDigestSigner for BlindedSigningKey where D: Digest + FixedOutputReset, { - fn try_sign_digest_with_rng( + fn try_sign_digest_with_rng( &self, - rng: &mut impl CryptoRngCore, + rng: &mut R, digest: D, ) -> signature::Result { sign_digest::<_, _, D>(rng, true, &self.inner, &digest.finalize(), self.salt_len) @@ -786,9 +794,9 @@ impl RandomizedPrehashSigner for BlindedSigningKey where D: Digest + FixedOutputReset, { - fn sign_prehash_with_rng( + fn sign_prehash_with_rng( &self, - rng: &mut impl CryptoRngCore, + rng: &mut R, prehash: &[u8], ) -> signature::Result { sign_digest::<_, _, D>(rng, true, &self.inner, prehash, self.salt_len) diff --git a/src/raw.rs b/src/raw.rs index f8e37f9..f77fd89 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -14,7 +14,7 @@ pub trait EncryptionPrimitive { pub trait DecryptionPrimitive { /// Do NOT use directly! Only for implementors. - fn raw_decryption_primitive( + fn raw_decryption_primitive( &self, rng: Option<&mut R>, ciphertext: &[u8], @@ -43,7 +43,7 @@ impl EncryptionPrimitive for RsaPublicKey { } impl DecryptionPrimitive for RsaPrivateKey { - fn raw_decryption_primitive( + fn raw_decryption_primitive( &self, rng: Option<&mut R>, ciphertext: &[u8], From 1484bbe59601022fa2424edb47536450c22f88ca Mon Sep 17 00:00:00 2001 From: Dmitry Baryshkov Date: Sat, 17 Dec 2022 03:11:41 +0200 Subject: [PATCH 3/3] v0.8.0-pre.0 Mark it for the pre-release to ease testing of the crate. Signed-off-by: Dmitry Baryshkov --- CHANGELOG.md | 10 ++++++++++ Cargo.toml | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dde716..81475ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.8.0-pre.0 (2022-12-??) +### Added +- Fix benches (#225) + +### Changed +- Switched to signature trait v2.0.0-pre development versions (#217) + +[#225]: https://github.com/RustCrypto/RSA/pull/225 +[#217]: https://github.com/RustCrypto/RSA/pull/217 + ## 0.7.2 (2022-11-14) ### Added - Public accessor methods for `PrecomputedValues` ([#221]) diff --git a/Cargo.toml b/Cargo.toml index 7b3c3d2..140df9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rsa" -version = "0.8.0-pre" +version = "0.8.0-pre.0" authors = ["RustCrypto Developers", "dignifiedquire "] edition = "2021" description = "Pure Rust RSA implementation"