From 8f07f8411fab4cc155af6906448e89c50e1d7d9d Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Thu, 20 Oct 2022 17:42:48 +0200 Subject: [PATCH] padding: ensure PaddingScheme is Send and Sync It's more convenient for users. Fix #214 --- src/key.rs | 8 +++++--- src/padding.rs | 24 +++++++++++++----------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/key.rs b/src/key.rs index b43434d..12e60a7 100644 --- a/src/key.rs +++ b/src/key.rs @@ -930,7 +930,9 @@ mod tests { } } - fn do_test_encrypt_decrypt_oaep(prk: &RsaPrivateKey) { + fn do_test_encrypt_decrypt_oaep( + prk: &RsaPrivateKey, + ) { let mut rng = ChaCha8Rng::from_seed([42; 32]); let k = prk.size(); @@ -974,8 +976,8 @@ mod tests { } fn do_test_oaep_with_different_hashes< - D: 'static + Digest + DynDigest, - U: 'static + Digest + DynDigest, + D: 'static + Digest + DynDigest + Send + Sync, + U: 'static + Digest + DynDigest + Send + Sync, >( prk: &RsaPrivateKey, ) { diff --git a/src/padding.rs b/src/padding.rs index 49d3a41..71c3e2c 100644 --- a/src/padding.rs +++ b/src/padding.rs @@ -27,13 +27,13 @@ pub enum PaddingScheme { /// A prominent example is the [`AndroidKeyStore`](https://developer.android.com/guide/topics/security/cryptography#oaep-mgf1-digest). /// It uses SHA-1 for `mgf_digest` and a user-chosen SHA flavour for `digest`. OAEP { - digest: Box, - mgf_digest: Box, + digest: Box, + mgf_digest: Box, label: Option, }, /// Sign and Verify using PSS padding. PSS { - digest: Box, + digest: Box, salt_len: Option, }, } @@ -98,8 +98,8 @@ impl PaddingScheme { /// let encrypted_data = key.encrypt(&mut rng, padding, b"secret").unwrap(); /// ``` pub fn new_oaep_with_mgf_hash< - T: 'static + Digest + DynDigest, - U: 'static + Digest + DynDigest, + T: 'static + Digest + DynDigest + Send + Sync, + U: 'static + Digest + DynDigest + Send + Sync, >() -> Self { PaddingScheme::OAEP { digest: Box::new(T::new()), @@ -125,7 +125,7 @@ impl PaddingScheme { /// let padding = PaddingScheme::new_oaep::(); /// let encrypted_data = key.encrypt(&mut rng, padding, b"secret").unwrap(); /// ``` - pub fn new_oaep() -> Self { + pub fn new_oaep() -> Self { PaddingScheme::OAEP { digest: Box::new(T::new()), mgf_digest: Box::new(T::new()), @@ -135,8 +135,8 @@ impl PaddingScheme { /// Create a new OAEP `PaddingScheme` with an associated `label`, using `T` as the hash function for the label, and `U` as the hash function for MGF1. pub fn new_oaep_with_mgf_hash_with_label< - T: 'static + Digest + DynDigest, - U: 'static + Digest + DynDigest, + T: 'static + Digest + DynDigest + Send + Sync, + U: 'static + Digest + DynDigest + Send + Sync, S: AsRef, >( label: S, @@ -149,7 +149,9 @@ impl PaddingScheme { } /// Create a new OAEP `PaddingScheme` with an associated `label`, using `T` as the hash function for both the label and for MGF1. - pub fn new_oaep_with_label>(label: S) -> Self { + pub fn new_oaep_with_label>( + label: S, + ) -> Self { PaddingScheme::OAEP { digest: Box::new(T::new()), mgf_digest: Box::new(T::new()), @@ -157,14 +159,14 @@ impl PaddingScheme { } } - pub fn new_pss() -> Self { + pub fn new_pss() -> Self { PaddingScheme::PSS { digest: Box::new(T::new()), salt_len: None, } } - pub fn new_pss_with_salt(len: usize) -> Self { + pub fn new_pss_with_salt(len: usize) -> Self { PaddingScheme::PSS { digest: Box::new(T::new()), salt_len: Some(len),