Skip to content

Commit d9968bc

Browse files
authored
Refactor traits (#315)
There were several modules that defined traits, including one called `traits`. This consolidates all of them under `traits`, retaining the previous module structure as internal submodules: - `keytraits` => `traits::keys` - `padding` => `traits::padding` - `traits` => `traits::encryption` Additionally this removes the traits that were re-exported at the toplevel, instead re-exporting them all under `traits`.
1 parent a8bddc2 commit d9968bc

13 files changed

Lines changed: 66 additions & 59 deletions

File tree

src/algorithms/rsa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rand_core::CryptoRngCore;
88
use zeroize::Zeroize;
99

1010
use crate::errors::{Error, Result};
11-
use crate::keytraits::{PrivateKeyParts, PublicKeyParts};
11+
use crate::traits::{PrivateKeyParts, PublicKeyParts};
1212

1313
/// Raw RSA encryption of m with the public key. No padding is performed.
1414
#[inline]

src/encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! `pkcs1` crate's traits for types which impl the `pkcs8` crate's traits.
55
66
use crate::{
7-
keytraits::{PrivateKeyParts, PublicKeyParts},
7+
traits::{PrivateKeyParts, PublicKeyParts},
88
BigUint, RsaPrivateKey, RsaPublicKey,
99
};
1010
use core::convert::{TryFrom, TryInto};

src/key.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
1616
use crate::algorithms::generate::generate_multi_prime_key_with_exp;
1717
use crate::dummy_rng::DummyRng;
1818
use crate::errors::{Error, Result};
19-
use crate::keytraits::{CrtValue, PrivateKeyParts, PublicKeyParts};
20-
21-
use crate::padding::{PaddingScheme, SignatureScheme};
19+
use crate::traits::{PaddingScheme, PrivateKeyParts, PublicKeyParts, SignatureScheme};
20+
use crate::CrtValue;
2221

2322
/// Represents the public part of an RSA key.
2423
#[derive(Debug, Clone, Hash, PartialEq, Eq)]

src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ pub use signature;
224224

225225
mod algorithms;
226226
pub mod errors;
227-
mod keytraits;
228227
pub mod oaep;
229228
pub mod pkcs1v15;
230229
pub mod pss;
@@ -233,7 +232,6 @@ pub mod traits;
233232
mod dummy_rng;
234233
mod encoding;
235234
mod key;
236-
mod padding;
237235

238236
pub use pkcs1;
239237
pub use pkcs8;
@@ -242,9 +240,8 @@ pub use sha2;
242240

243241
pub use crate::{
244242
key::{RsaPrivateKey, RsaPublicKey},
245-
keytraits::{CrtValue, PrivateKeyParts, PublicKeyParts},
246243
oaep::Oaep,
247-
padding::{PaddingScheme, SignatureScheme},
248244
pkcs1v15::{Pkcs1v15Encrypt, Pkcs1v15Sign},
249245
pss::Pss,
246+
traits::keys::CrtValue,
250247
};

src/oaep.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use crate::algorithms::rsa::{rsa_decrypt_and_check, rsa_encrypt};
2121
use crate::dummy_rng::DummyRng;
2222
use crate::errors::{Error, Result};
2323
use crate::key::{self, RsaPrivateKey, RsaPublicKey};
24-
use crate::keytraits::PublicKeyParts;
25-
use crate::padding::PaddingScheme;
24+
use crate::traits::PaddingScheme;
2625
use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor};
26+
use crate::traits::PublicKeyParts;
2727

2828
/// Encryption and Decryption using [OAEP padding](https://datatracker.ietf.org/doc/html/rfc8017#section-7.1).
2929
///
@@ -422,9 +422,9 @@ where
422422
#[cfg(test)]
423423
mod tests {
424424
use crate::key::{RsaPrivateKey, RsaPublicKey};
425-
use crate::keytraits::PublicKeyParts;
426425
use crate::oaep::{DecryptingKey, EncryptingKey, Oaep};
427426
use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor};
427+
use crate::traits::PublicKeyParts;
428428

429429
use alloc::string::String;
430430
use digest::{Digest, DynDigest, FixedOutputReset};

src/pkcs1v15.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ use crate::algorithms::rsa::{rsa_decrypt_and_check, rsa_encrypt};
3232
use crate::dummy_rng::DummyRng;
3333
use crate::errors::{Error, Result};
3434
use crate::key;
35-
use crate::keytraits::PublicKeyParts;
36-
use crate::padding::{PaddingScheme, SignatureScheme};
35+
use crate::traits::{PaddingScheme, SignatureScheme};
36+
use crate::traits::PublicKeyParts;
3737
use crate::traits::{Decryptor, EncryptingKeypair, RandomizedDecryptor, RandomizedEncryptor};
3838
use crate::{RsaPrivateKey, RsaPublicKey};
3939

@@ -792,7 +792,7 @@ mod tests {
792792
use sha3::Sha3_256;
793793
use signature::{RandomizedSigner, Signer, Verifier};
794794

795-
use crate::{PublicKeyParts, RsaPrivateKey, RsaPublicKey};
795+
use crate::{traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey};
796796

797797
fn get_private_key() -> RsaPrivateKey {
798798
// In order to generate new test vectors you'll need the PEM form of this key:

src/pss.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ use crate::algorithms::pad::{uint_to_be_pad, uint_to_zeroizing_be_pad};
3636
use crate::algorithms::pss::*;
3737
use crate::algorithms::rsa::{rsa_decrypt_and_check, rsa_encrypt};
3838
use crate::errors::{Error, Result};
39-
use crate::keytraits::PublicKeyParts;
40-
use crate::padding::SignatureScheme;
39+
use crate::traits::SignatureScheme;
40+
use crate::traits::PublicKeyParts;
4141
use crate::{RsaPrivateKey, RsaPublicKey};
4242

4343
#[cfg(feature = "getrandom")]

src/traits.rs

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,9 @@
1-
//! Generic traits for message encryption and decryption
1+
//! RSA-related trait definitions.
22
3-
use alloc::vec::Vec;
4-
use rand_core::CryptoRngCore;
3+
mod encryption;
4+
pub(crate) mod keys;
5+
mod padding;
56

6-
use crate::errors::Result;
7-
8-
/// Encrypt the message using provided random source
9-
pub trait RandomizedEncryptor {
10-
/// Encrypt the given message.
11-
fn encrypt_with_rng<R: CryptoRngCore + ?Sized>(
12-
&self,
13-
rng: &mut R,
14-
msg: &[u8],
15-
) -> Result<Vec<u8>>;
16-
}
17-
18-
/// Decrypt the given message
19-
pub trait Decryptor {
20-
/// Decrypt the given message.
21-
fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>>;
22-
}
23-
24-
/// Decrypt the given message using provided random source
25-
pub trait RandomizedDecryptor {
26-
/// Decrypt the given message.
27-
fn decrypt_with_rng<R: CryptoRngCore + ?Sized>(
28-
&self,
29-
rng: &mut R,
30-
ciphertext: &[u8],
31-
) -> Result<Vec<u8>>;
32-
}
33-
34-
/// Encryption keypair with an associated encryption key.
35-
pub trait EncryptingKeypair {
36-
/// Encrypting key type for this keypair.
37-
type EncryptingKey: Clone;
38-
39-
/// Get the encrypting key which can encrypt messages to be decrypted by
40-
/// the decryption key portion of this keypair.
41-
fn encrypting_key(&self) -> Self::EncryptingKey;
42-
}
7+
pub use encryption::{Decryptor, EncryptingKeypair, RandomizedDecryptor, RandomizedEncryptor};
8+
pub use keys::{PrivateKeyParts, PublicKeyParts};
9+
pub use padding::{PaddingScheme, SignatureScheme};

src/traits/encryption.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! Encryption-related traits.
2+
3+
use alloc::vec::Vec;
4+
use rand_core::CryptoRngCore;
5+
6+
use crate::errors::Result;
7+
8+
/// Encrypt the message using provided random source
9+
pub trait RandomizedEncryptor {
10+
/// Encrypt the given message.
11+
fn encrypt_with_rng<R: CryptoRngCore + ?Sized>(
12+
&self,
13+
rng: &mut R,
14+
msg: &[u8],
15+
) -> Result<Vec<u8>>;
16+
}
17+
18+
/// Decrypt the given message
19+
pub trait Decryptor {
20+
/// Decrypt the given message.
21+
fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>>;
22+
}
23+
24+
/// Decrypt the given message using provided random source
25+
pub trait RandomizedDecryptor {
26+
/// Decrypt the given message.
27+
fn decrypt_with_rng<R: CryptoRngCore + ?Sized>(
28+
&self,
29+
rng: &mut R,
30+
ciphertext: &[u8],
31+
) -> Result<Vec<u8>>;
32+
}
33+
34+
/// Encryption keypair with an associated encryption key.
35+
pub trait EncryptingKeypair {
36+
/// Encrypting key type for this keypair.
37+
type EncryptingKey: Clone;
38+
39+
/// Get the encrypting key which can encrypt messages to be decrypted by
40+
/// the decryption key portion of this keypair.
41+
fn encrypting_key(&self) -> Self::EncryptingKey;
42+
}
File renamed without changes.

0 commit comments

Comments
 (0)