mod decrypting_key;
mod encrypting_key;
pub use self::{decrypting_key::DecryptingKey, encrypting_key::EncryptingKey};
use core::fmt;
use digest::{Digest, DynDigest, FixedOutputReset};
use rand_core::CryptoRngCore;
use zeroize::Zeroizing;
use crate::algorithms::oaep::*;
use crate::algorithms::pad::{uint_to_be_pad, uint_to_zeroizing_be_pad};
use crate::algorithms::rsa::{rsa_decrypt_and_check, rsa_encrypt};
use crate::errors::{Error, Result};
use crate::key::{self, RsaPrivateKey, RsaPublicKey};
use crate::traits::{PaddingScheme, PublicKeyParts};
use crate::traits::UnsignedModularInt;
use heapless::String;
use core::marker::PhantomData;
pub struct Oaep {
pub digest: PhantomData<u8>,
pub mgf_digest: PhantomData<u8>,
pub label: Option<Label>,
}
impl Oaep {
pub fn new<T: 'static + Digest + DynDigest + Send + Sync>() -> Self {
Self {
digest: Default::default(), mgf_digest: Default::default(), label: None,
}
}
pub fn new_with_label<T: 'static + Digest + DynDigest + Send + Sync, S: AsRef<str>>(
label: S,
) -> Self {
Self {
digest: Default::default(), mgf_digest: Default::default(), label: None, }
}
pub fn new_with_mgf_hash<
T: 'static + Digest + DynDigest + Send + Sync,
U: 'static + Digest + DynDigest + Send + Sync,
>() -> Self {
Self {
digest: Default::default(), mgf_digest: Default::default(), label: None,
}
}
pub fn new_with_mgf_hash_and_label<
T: 'static + Digest + DynDigest + Send + Sync,
U: 'static + Digest + DynDigest + Send + Sync,
S: AsRef<str>,
>(
label: S,
) -> Self {
Self {
digest: Default::default(), mgf_digest: Default::default(), label: None, }
}
}
impl<T> PaddingScheme<T> for Oaep
where
T: UnsignedModularInt,
{
fn encrypt<'a, Rng: CryptoRngCore>(
self,
rng: &mut Rng,
pub_key: &RsaPublicKey<T>,
msg: &[u8],
storage: &'a mut [u8],
) -> Result<&'a [u8]> {
todo!()
}
}
impl fmt::Debug for Oaep {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OAEP")
.field("digest", &"...")
.field("mgf_digest", &"...")
.field("label", &self.label)
.finish()
}
}
#[inline]
fn encrypt<'a, T, R: CryptoRngCore + ?Sized, D>(
rng: &mut R,
pub_key: &RsaPublicKey<T>,
msg: &[u8],
digest: &mut D,
mgf_digest: &mut D,
label: Option<Label>,
storage: &'a mut [u8],
) -> Result<&'a [u8]>
where
T: UnsignedModularInt,
D: Digest + FixedOutputReset,
{
key::check_public(pub_key)?;
let em = oaep_encrypt(rng, msg, digest, mgf_digest, label, pub_key.size(), storage)?;
todo!()
}
fn encrypt_digest<'a, T, R: CryptoRngCore + ?Sized, D: Digest, MGD: Digest + FixedOutputReset>(
rng: &mut R,
pub_key: &RsaPublicKey<T>,
msg: &[u8],
label: Option<Label>,
storage: &'a mut [u8],
) -> Result<&'a [u8]>
where
T: UnsignedModularInt,
{
key::check_public(pub_key)?;
let em = oaep_encrypt_digest::<_, D, MGD>(rng, msg, label, pub_key.size(), storage)?;
todo!()
}
#[inline]
fn decrypt<'a, T, R: CryptoRngCore + ?Sized, D>(
rng: Option<&mut R>,
priv_key: &RsaPrivateKey<T>,
ciphertext: &[u8],
digest: &mut D,
mgf_digest: &mut D,
label: Option<Label>,
storage: &'a mut [u8],
) -> Result<&'a [u8]>
where
T: UnsignedModularInt,
D: Digest + FixedOutputReset,
{
todo!()
}
#[inline]
fn decrypt_digest<'a, T, R: CryptoRngCore + ?Sized, D, MGD: Digest + FixedOutputReset>(
rng: Option<&mut R>,
priv_key: &RsaPrivateKey<T>,
ciphertext: &[u8],
label: Option<Label>,
) -> Result<&'a [u8]>
where
T: UnsignedModularInt,
D: Digest + FixedOutputReset,
{
todo!()
}
#[cfg(test)]
mod tests {
use crate::key::{RsaPrivateKey, RsaPublicKey};
use crate::oaep::{EncryptingKey, Oaep};
use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor};
use crate::traits::{PublicKeyParts, UnsignedModularInt};
use digest::{Digest, DynDigest, FixedOutputReset};
use num_traits::FromPrimitive;
use rand_chacha::{
rand_core::{RngCore, SeedableRng},
ChaCha8Rng,
};
use sha1::Sha1;
use sha2::{Sha224, Sha256, Sha384, Sha512};
use sha3::{Sha3_256, Sha3_384, Sha3_512};
fn get_private_key<T: UnsignedModularInt>() -> RsaPrivateKey<T> {
todo!()
}
#[test]
#[ignore]
fn test_encrypt_decrypt_oaep() {
todo!()
}
fn get_label(rng: &mut ChaCha8Rng) -> Option<String> {
todo!()
}
fn do_test_encrypt_decrypt_oaep<
T: UnsignedModularInt,
D: 'static + Digest + DynDigest + Send + Sync,
>(
prk: &RsaPrivateKey<T>,
) {
todo!()
}
fn do_test_oaep_with_different_hashes<
T: UnsignedModularInt,
D: 'static + Digest + DynDigest + Send + Sync,
U: 'static + Digest + DynDigest + Send + Sync,
>(
prk: &RsaPrivateKey<T>,
) {
todo!()
}
#[test]
#[ignore]
fn test_decrypt_oaep_invalid_hash() {
todo!()
}
#[test]
#[ignore]
fn test_encrypt_decrypt_oaep_traits() {
todo!()
}
fn do_test_encrypt_decrypt_oaep_traits<T: UnsignedModularInt, D: Digest + FixedOutputReset>(
prk: &RsaPrivateKey<T>,
) {
todo!()
}
fn do_test_oaep_with_different_hashes_traits<
T: UnsignedModularInt,
D: Digest,
MGD: Digest + FixedOutputReset,
>(
prk: &RsaPrivateKey<T>,
) {
todo!()
}
#[test]
#[ignore]
fn test_decrypt_oaep_invalid_hash_traits() {
todo!()
}
}