Skip to content

Commit

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

Fix #214
  • Loading branch information
gdesmott committed Oct 20, 2022
1 parent 942571f commit c764f8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/key.rs
Expand Up @@ -930,7 +930,7 @@ 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>(prk: &RsaPrivateKey) {
let mut rng = ChaCha8Rng::from_seed([42; 32]);

let k = prk.size();
Expand Down Expand Up @@ -974,8 +974,8 @@ mod tests {
}

fn do_test_oaep_with_different_hashes<
D: 'static + Digest + DynDigest,
U: 'static + Digest + DynDigest,
D: 'static + Digest + DynDigest + Send,
U: 'static + Digest + DynDigest + Send,
>(
prk: &RsaPrivateKey,
) {
Expand Down
24 changes: 13 additions & 11 deletions src/padding.rs
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>,
mgf_digest: Box<dyn DynDigest + Send>,
label: Option<String>,
},
/// Sign and Verify using PSS padding.
PSS {
digest: Box<dyn DynDigest>,
digest: Box<dyn DynDigest + Send>,
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,
U: 'static + Digest + DynDigest + Send,
>() -> 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>() -> 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,
U: 'static + Digest + DynDigest + Send,
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, 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>() -> 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>(len: usize) -> Self {
PaddingScheme::PSS {
digest: Box::new(T::new()),
salt_len: Some(len),
Expand Down

0 comments on commit c764f8a

Please sign in to comment.