Skip to content

Commit

Permalink
padding: ensure PaddingScheme is Send and Sync (#215)
Browse files Browse the repository at this point in the history
It's more convenient for users.

Fix #214
  • Loading branch information
gdesmott authored Oct 24, 2022
1 parent 942571f commit a857c8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,9 @@ mod tests {
}
}

fn do_test_encrypt_decrypt_oaep<D: 'static + Digest + DynDigest>(prk: &RsaPrivateKey) {
fn do_test_encrypt_decrypt_oaep<D: 'static + Digest + DynDigest + Send + Sync>(
prk: &RsaPrivateKey,
) {
let mut rng = ChaCha8Rng::from_seed([42; 32]);

let k = prk.size();
Expand Down Expand Up @@ -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,
) {
Expand Down
24 changes: 13 additions & 11 deletions src/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn DynDigest>,
mgf_digest: Box<dyn DynDigest>,
digest: Box<dyn DynDigest + Send + Sync>,
mgf_digest: Box<dyn DynDigest + Send + Sync>,
label: Option<String>,
},
/// Sign and Verify using PSS padding.
PSS {
digest: Box<dyn DynDigest>,
digest: Box<dyn DynDigest + Send + Sync>,
salt_len: Option<usize>,
},
}
Expand Down Expand Up @@ -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()),
Expand All @@ -125,7 +125,7 @@ impl PaddingScheme {
/// let padding = PaddingScheme::new_oaep::<Sha256>();
/// let encrypted_data = key.encrypt(&mut rng, padding, b"secret").unwrap();
/// ```
pub fn new_oaep<T: 'static + Digest + DynDigest>() -> Self {
pub fn new_oaep<T: 'static + Digest + DynDigest + Send + Sync>() -> Self {
PaddingScheme::OAEP {
digest: Box::new(T::new()),
mgf_digest: Box::new(T::new()),
Expand All @@ -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<str>,
>(
label: S,
Expand All @@ -149,22 +149,24 @@ 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<T: 'static + Digest + DynDigest, S: AsRef<str>>(label: S) -> Self {
pub fn new_oaep_with_label<T: 'static + Digest + DynDigest + Send + Sync, S: AsRef<str>>(
label: S,
) -> Self {
PaddingScheme::OAEP {
digest: Box::new(T::new()),
mgf_digest: Box::new(T::new()),
label: Some(label.as_ref().to_string()),
}
}

pub fn new_pss<T: 'static + Digest + DynDigest>() -> Self {
pub fn new_pss<T: 'static + Digest + DynDigest + Send + Sync>() -> Self {
PaddingScheme::PSS {
digest: Box::new(T::new()),
salt_len: None,
}
}

pub fn new_pss_with_salt<T: 'static + Digest + DynDigest>(len: usize) -> Self {
pub fn new_pss_with_salt<T: 'static + Digest + DynDigest + Send + Sync>(len: usize) -> Self {
PaddingScheme::PSS {
digest: Box::new(T::new()),
salt_len: Some(len),
Expand Down

0 comments on commit a857c8f

Please sign in to comment.