Skip to content

Commit f5918ad

Browse files
authored
Refactor padding modes into submodules (#312)
The padding mode modules have gotten quite large. This commit refactors types into respective submodules, with the toplevel module defining the same-named padding schemes.
1 parent d9968bc commit f5918ad

16 files changed

Lines changed: 1436 additions & 1256 deletions

src/key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl PartialEq for RsaPrivateKey {
5353
}
5454

5555
impl Hash for RsaPrivateKey {
56-
fn hash<H: Hasher>(&self, state: &mut H) -> () {
56+
fn hash<H: Hasher>(&self, state: &mut H) {
5757
// Domain separator for RSA private keys
5858
state.write(b"RsaPrivateKey");
5959
Hash::hash(&self.pubkey_components, state);

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ pub use pkcs8;
239239
pub use sha2;
240240

241241
pub use crate::{
242+
errors::{Error, Result},
242243
key::{RsaPrivateKey, RsaPublicKey},
243244
oaep::Oaep,
244245
pkcs1v15::{Pkcs1v15Encrypt, Pkcs1v15Sign},

src/oaep.rs

Lines changed: 8 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44
//!
55
//! See [code example in the toplevel rustdoc](../index.html#oaep-encryption).
66
7+
mod decrypting_key;
8+
mod encrypting_key;
9+
10+
pub use self::{decrypting_key::DecryptingKey, encrypting_key::EncryptingKey};
11+
712
use alloc::boxed::Box;
813
use alloc::string::{String, ToString};
914
use alloc::vec::Vec;
1015
use core::fmt;
11-
use core::marker::PhantomData;
1216

1317
use digest::{Digest, DynDigest, FixedOutputReset};
1418
use num_bigint::BigUint;
1519
use rand_core::CryptoRngCore;
16-
use zeroize::{ZeroizeOnDrop, Zeroizing};
20+
use zeroize::Zeroizing;
1721

1822
use crate::algorithms::oaep::*;
1923
use crate::algorithms::pad::{uint_to_be_pad, uint_to_zeroizing_be_pad};
2024
use crate::algorithms::rsa::{rsa_decrypt_and_check, rsa_encrypt};
21-
use crate::dummy_rng::DummyRng;
2225
use crate::errors::{Error, Result};
2326
use crate::key::{self, RsaPrivateKey, RsaPublicKey};
24-
use crate::traits::PaddingScheme;
25-
use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor};
26-
use crate::traits::PublicKeyParts;
27+
use crate::traits::{PaddingScheme, PublicKeyParts};
2728

2829
/// Encryption and Decryption using [OAEP padding](https://datatracker.ietf.org/doc/html/rfc8017#section-7.1).
2930
///
@@ -282,149 +283,12 @@ fn decrypt_digest<R: CryptoRngCore + ?Sized, D: Digest, MGD: Digest + FixedOutpu
282283
oaep_decrypt_digest::<D, MGD>(&mut em, label, priv_key.size())
283284
}
284285

285-
/// Encryption key for PKCS#1 v1.5 encryption as described in [RFC8017 § 7.1].
286-
///
287-
/// [RFC8017 § 7.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1
288-
#[derive(Debug, Clone)]
289-
pub struct EncryptingKey<D, MGD = D>
290-
where
291-
D: Digest,
292-
MGD: Digest + FixedOutputReset,
293-
{
294-
inner: RsaPublicKey,
295-
label: Option<String>,
296-
phantom: PhantomData<D>,
297-
mg_phantom: PhantomData<MGD>,
298-
}
299-
300-
impl<D, MGD> EncryptingKey<D, MGD>
301-
where
302-
D: Digest,
303-
MGD: Digest + FixedOutputReset,
304-
{
305-
/// Create a new verifying key from an RSA public key.
306-
pub fn new(key: RsaPublicKey) -> Self {
307-
Self {
308-
inner: key,
309-
label: None,
310-
phantom: Default::default(),
311-
mg_phantom: Default::default(),
312-
}
313-
}
314-
315-
/// Create a new verifying key from an RSA public key using provided label
316-
pub fn new_with_label<S: AsRef<str>>(key: RsaPublicKey, label: S) -> Self {
317-
Self {
318-
inner: key,
319-
label: Some(label.as_ref().to_string()),
320-
phantom: Default::default(),
321-
mg_phantom: Default::default(),
322-
}
323-
}
324-
}
325-
326-
impl<D, MGD> RandomizedEncryptor for EncryptingKey<D, MGD>
327-
where
328-
D: Digest,
329-
MGD: Digest + FixedOutputReset,
330-
{
331-
fn encrypt_with_rng<R: CryptoRngCore + ?Sized>(
332-
&self,
333-
rng: &mut R,
334-
msg: &[u8],
335-
) -> Result<Vec<u8>> {
336-
encrypt_digest::<_, D, MGD>(rng, &self.inner, msg, self.label.as_ref().cloned())
337-
}
338-
}
339-
340-
/// Decryption key for PKCS#1 v1.5 decryption as described in [RFC8017 § 7.1].
341-
///
342-
/// [RFC8017 § 7.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1
343-
#[derive(Debug, Clone)]
344-
pub struct DecryptingKey<D, MGD = D>
345-
where
346-
D: Digest,
347-
MGD: Digest + FixedOutputReset,
348-
{
349-
inner: RsaPrivateKey,
350-
label: Option<String>,
351-
phantom: PhantomData<D>,
352-
mg_phantom: PhantomData<MGD>,
353-
}
354-
355-
impl<D, MGD> DecryptingKey<D, MGD>
356-
where
357-
D: Digest,
358-
MGD: Digest + FixedOutputReset,
359-
{
360-
/// Create a new verifying key from an RSA public key.
361-
pub fn new(key: RsaPrivateKey) -> Self {
362-
Self {
363-
inner: key,
364-
label: None,
365-
phantom: Default::default(),
366-
mg_phantom: Default::default(),
367-
}
368-
}
369-
370-
/// Create a new verifying key from an RSA public key using provided label
371-
pub fn new_with_label<S: AsRef<str>>(key: RsaPrivateKey, label: S) -> Self {
372-
Self {
373-
inner: key,
374-
label: Some(label.as_ref().to_string()),
375-
phantom: Default::default(),
376-
mg_phantom: Default::default(),
377-
}
378-
}
379-
}
380-
381-
impl<D, MGD> Decryptor for DecryptingKey<D, MGD>
382-
where
383-
D: Digest,
384-
MGD: Digest + FixedOutputReset,
385-
{
386-
fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
387-
decrypt_digest::<DummyRng, D, MGD>(
388-
None,
389-
&self.inner,
390-
ciphertext,
391-
self.label.as_ref().cloned(),
392-
)
393-
}
394-
}
395-
396-
impl<D, MGD> RandomizedDecryptor for DecryptingKey<D, MGD>
397-
where
398-
D: Digest,
399-
MGD: Digest + FixedOutputReset,
400-
{
401-
fn decrypt_with_rng<R: CryptoRngCore + ?Sized>(
402-
&self,
403-
rng: &mut R,
404-
ciphertext: &[u8],
405-
) -> Result<Vec<u8>> {
406-
decrypt_digest::<_, D, MGD>(
407-
Some(rng),
408-
&self.inner,
409-
ciphertext,
410-
self.label.as_ref().cloned(),
411-
)
412-
}
413-
}
414-
415-
impl<D, MGD> ZeroizeOnDrop for DecryptingKey<D, MGD>
416-
where
417-
D: Digest,
418-
MGD: Digest + FixedOutputReset,
419-
{
420-
}
421-
422286
#[cfg(test)]
423287
mod tests {
424288
use crate::key::{RsaPrivateKey, RsaPublicKey};
425289
use crate::oaep::{DecryptingKey, EncryptingKey, Oaep};
426-
use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor};
427290
use crate::traits::PublicKeyParts;
291+
use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor};
428292

429293
use alloc::string::String;
430294
use digest::{Digest, DynDigest, FixedOutputReset};

src/oaep/decrypting_key.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use super::decrypt_digest;
2+
use crate::{
3+
dummy_rng::DummyRng,
4+
traits::{Decryptor, RandomizedDecryptor},
5+
Result, RsaPrivateKey,
6+
};
7+
use alloc::{
8+
string::{String, ToString},
9+
vec::Vec,
10+
};
11+
use core::marker::PhantomData;
12+
use digest::{Digest, FixedOutputReset};
13+
use rand_core::CryptoRngCore;
14+
use zeroize::ZeroizeOnDrop;
15+
16+
/// Decryption key for PKCS#1 v1.5 decryption as described in [RFC8017 § 7.1].
17+
///
18+
/// [RFC8017 § 7.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1
19+
#[derive(Debug, Clone)]
20+
pub struct DecryptingKey<D, MGD = D>
21+
where
22+
D: Digest,
23+
MGD: Digest + FixedOutputReset,
24+
{
25+
inner: RsaPrivateKey,
26+
label: Option<String>,
27+
phantom: PhantomData<D>,
28+
mg_phantom: PhantomData<MGD>,
29+
}
30+
31+
impl<D, MGD> DecryptingKey<D, MGD>
32+
where
33+
D: Digest,
34+
MGD: Digest + FixedOutputReset,
35+
{
36+
/// Create a new verifying key from an RSA public key.
37+
pub fn new(key: RsaPrivateKey) -> Self {
38+
Self {
39+
inner: key,
40+
label: None,
41+
phantom: Default::default(),
42+
mg_phantom: Default::default(),
43+
}
44+
}
45+
46+
/// Create a new verifying key from an RSA public key using provided label
47+
pub fn new_with_label<S: AsRef<str>>(key: RsaPrivateKey, label: S) -> Self {
48+
Self {
49+
inner: key,
50+
label: Some(label.as_ref().to_string()),
51+
phantom: Default::default(),
52+
mg_phantom: Default::default(),
53+
}
54+
}
55+
}
56+
57+
impl<D, MGD> Decryptor for DecryptingKey<D, MGD>
58+
where
59+
D: Digest,
60+
MGD: Digest + FixedOutputReset,
61+
{
62+
fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
63+
decrypt_digest::<DummyRng, D, MGD>(
64+
None,
65+
&self.inner,
66+
ciphertext,
67+
self.label.as_ref().cloned(),
68+
)
69+
}
70+
}
71+
72+
impl<D, MGD> RandomizedDecryptor for DecryptingKey<D, MGD>
73+
where
74+
D: Digest,
75+
MGD: Digest + FixedOutputReset,
76+
{
77+
fn decrypt_with_rng<R: CryptoRngCore + ?Sized>(
78+
&self,
79+
rng: &mut R,
80+
ciphertext: &[u8],
81+
) -> Result<Vec<u8>> {
82+
decrypt_digest::<_, D, MGD>(
83+
Some(rng),
84+
&self.inner,
85+
ciphertext,
86+
self.label.as_ref().cloned(),
87+
)
88+
}
89+
}
90+
91+
impl<D, MGD> ZeroizeOnDrop for DecryptingKey<D, MGD>
92+
where
93+
D: Digest,
94+
MGD: Digest + FixedOutputReset,
95+
{
96+
}

src/oaep/encrypting_key.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use super::encrypt_digest;
2+
use crate::{traits::RandomizedEncryptor, Result, RsaPublicKey};
3+
use alloc::{
4+
string::{String, ToString},
5+
vec::Vec,
6+
};
7+
use core::marker::PhantomData;
8+
use digest::{Digest, FixedOutputReset};
9+
use rand_core::CryptoRngCore;
10+
11+
/// Encryption key for PKCS#1 v1.5 encryption as described in [RFC8017 § 7.1].
12+
///
13+
/// [RFC8017 § 7.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1
14+
#[derive(Debug, Clone)]
15+
pub struct EncryptingKey<D, MGD = D>
16+
where
17+
D: Digest,
18+
MGD: Digest + FixedOutputReset,
19+
{
20+
inner: RsaPublicKey,
21+
label: Option<String>,
22+
phantom: PhantomData<D>,
23+
mg_phantom: PhantomData<MGD>,
24+
}
25+
26+
impl<D, MGD> EncryptingKey<D, MGD>
27+
where
28+
D: Digest,
29+
MGD: Digest + FixedOutputReset,
30+
{
31+
/// Create a new verifying key from an RSA public key.
32+
pub fn new(key: RsaPublicKey) -> Self {
33+
Self {
34+
inner: key,
35+
label: None,
36+
phantom: Default::default(),
37+
mg_phantom: Default::default(),
38+
}
39+
}
40+
41+
/// Create a new verifying key from an RSA public key using provided label
42+
pub fn new_with_label<S: AsRef<str>>(key: RsaPublicKey, label: S) -> Self {
43+
Self {
44+
inner: key,
45+
label: Some(label.as_ref().to_string()),
46+
phantom: Default::default(),
47+
mg_phantom: Default::default(),
48+
}
49+
}
50+
}
51+
52+
impl<D, MGD> RandomizedEncryptor for EncryptingKey<D, MGD>
53+
where
54+
D: Digest,
55+
MGD: Digest + FixedOutputReset,
56+
{
57+
fn encrypt_with_rng<R: CryptoRngCore + ?Sized>(
58+
&self,
59+
rng: &mut R,
60+
msg: &[u8],
61+
) -> Result<Vec<u8>> {
62+
encrypt_digest::<_, D, MGD>(rng, &self.inner, msg, self.label.as_ref().cloned())
63+
}
64+
}

0 commit comments

Comments
 (0)