-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
16 changed files
with
1,436 additions
and
1,256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use super::decrypt_digest; | ||
use crate::{ | ||
dummy_rng::DummyRng, | ||
traits::{Decryptor, RandomizedDecryptor}, | ||
Result, RsaPrivateKey, | ||
}; | ||
use alloc::{ | ||
string::{String, ToString}, | ||
vec::Vec, | ||
}; | ||
use core::marker::PhantomData; | ||
use digest::{Digest, FixedOutputReset}; | ||
use rand_core::CryptoRngCore; | ||
use zeroize::ZeroizeOnDrop; | ||
|
||
/// Decryption key for PKCS#1 v1.5 decryption as described in [RFC8017 § 7.1]. | ||
/// | ||
/// [RFC8017 § 7.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1 | ||
#[derive(Debug, Clone)] | ||
pub struct DecryptingKey<D, MGD = D> | ||
where | ||
D: Digest, | ||
MGD: Digest + FixedOutputReset, | ||
{ | ||
inner: RsaPrivateKey, | ||
label: Option<String>, | ||
phantom: PhantomData<D>, | ||
mg_phantom: PhantomData<MGD>, | ||
} | ||
|
||
impl<D, MGD> DecryptingKey<D, MGD> | ||
where | ||
D: Digest, | ||
MGD: Digest + FixedOutputReset, | ||
{ | ||
/// Create a new verifying key from an RSA public key. | ||
pub fn new(key: RsaPrivateKey) -> Self { | ||
Self { | ||
inner: key, | ||
label: None, | ||
phantom: Default::default(), | ||
mg_phantom: Default::default(), | ||
} | ||
} | ||
|
||
/// Create a new verifying key from an RSA public key using provided label | ||
pub fn new_with_label<S: AsRef<str>>(key: RsaPrivateKey, label: S) -> Self { | ||
Self { | ||
inner: key, | ||
label: Some(label.as_ref().to_string()), | ||
phantom: Default::default(), | ||
mg_phantom: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<D, MGD> Decryptor for DecryptingKey<D, MGD> | ||
where | ||
D: Digest, | ||
MGD: Digest + FixedOutputReset, | ||
{ | ||
fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> { | ||
decrypt_digest::<DummyRng, D, MGD>( | ||
None, | ||
&self.inner, | ||
ciphertext, | ||
self.label.as_ref().cloned(), | ||
) | ||
} | ||
} | ||
|
||
impl<D, MGD> RandomizedDecryptor for DecryptingKey<D, MGD> | ||
where | ||
D: Digest, | ||
MGD: Digest + FixedOutputReset, | ||
{ | ||
fn decrypt_with_rng<R: CryptoRngCore + ?Sized>( | ||
&self, | ||
rng: &mut R, | ||
ciphertext: &[u8], | ||
) -> Result<Vec<u8>> { | ||
decrypt_digest::<_, D, MGD>( | ||
Some(rng), | ||
&self.inner, | ||
ciphertext, | ||
self.label.as_ref().cloned(), | ||
) | ||
} | ||
} | ||
|
||
impl<D, MGD> ZeroizeOnDrop for DecryptingKey<D, MGD> | ||
where | ||
D: Digest, | ||
MGD: Digest + FixedOutputReset, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use super::encrypt_digest; | ||
use crate::{traits::RandomizedEncryptor, Result, RsaPublicKey}; | ||
use alloc::{ | ||
string::{String, ToString}, | ||
vec::Vec, | ||
}; | ||
use core::marker::PhantomData; | ||
use digest::{Digest, FixedOutputReset}; | ||
use rand_core::CryptoRngCore; | ||
|
||
/// Encryption key for PKCS#1 v1.5 encryption as described in [RFC8017 § 7.1]. | ||
/// | ||
/// [RFC8017 § 7.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1 | ||
#[derive(Debug, Clone)] | ||
pub struct EncryptingKey<D, MGD = D> | ||
where | ||
D: Digest, | ||
MGD: Digest + FixedOutputReset, | ||
{ | ||
inner: RsaPublicKey, | ||
label: Option<String>, | ||
phantom: PhantomData<D>, | ||
mg_phantom: PhantomData<MGD>, | ||
} | ||
|
||
impl<D, MGD> EncryptingKey<D, MGD> | ||
where | ||
D: Digest, | ||
MGD: Digest + FixedOutputReset, | ||
{ | ||
/// Create a new verifying key from an RSA public key. | ||
pub fn new(key: RsaPublicKey) -> Self { | ||
Self { | ||
inner: key, | ||
label: None, | ||
phantom: Default::default(), | ||
mg_phantom: Default::default(), | ||
} | ||
} | ||
|
||
/// Create a new verifying key from an RSA public key using provided label | ||
pub fn new_with_label<S: AsRef<str>>(key: RsaPublicKey, label: S) -> Self { | ||
Self { | ||
inner: key, | ||
label: Some(label.as_ref().to_string()), | ||
phantom: Default::default(), | ||
mg_phantom: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<D, MGD> RandomizedEncryptor for EncryptingKey<D, MGD> | ||
where | ||
D: Digest, | ||
MGD: Digest + FixedOutputReset, | ||
{ | ||
fn encrypt_with_rng<R: CryptoRngCore + ?Sized>( | ||
&self, | ||
rng: &mut R, | ||
msg: &[u8], | ||
) -> Result<Vec<u8>> { | ||
encrypt_digest::<_, D, MGD>(rng, &self.inner, msg, self.label.as_ref().cloned()) | ||
} | ||
} |
Oops, something went wrong.