use super::decrypt_digest;
use crate::{
dummy_rng::DummyRng,
traits::{Decryptor, RandomizedDecryptor},
Result, RsaPrivateKey,
};
use core::marker::PhantomData;
use digest::{Digest, FixedOutputReset};
use rand_core::CryptoRngCore;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use zeroize::ZeroizeOnDrop;
use crate::algorithms::oaep::Label;
use crate::traits::UnsignedModularInt;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DecryptingKey<T, D, MGD = D>
where
T: UnsignedModularInt,
D: Digest,
MGD: Digest + FixedOutputReset,
{
inner: RsaPrivateKey<T>,
label: Option<Label>,
phantom: PhantomData<D>,
mg_phantom: PhantomData<MGD>,
}
impl<T, D, MGD> DecryptingKey<T, D, MGD>
where
T: UnsignedModularInt,
D: Digest,
MGD: Digest + FixedOutputReset,
{
pub fn new(key: RsaPrivateKey<T>) -> Self {
Self {
inner: key,
label: None,
phantom: Default::default(),
mg_phantom: Default::default(),
}
}
pub fn new_with_label<S: AsRef<str>>(key: RsaPrivateKey<T>, label: S) -> Self {
Self {
inner: key,
label: Some(label.as_ref().into()),
phantom: Default::default(),
mg_phantom: Default::default(),
}
}
}
impl<T, D, MGD> Decryptor for DecryptingKey<T, D, MGD>
where
T: UnsignedModularInt,
D: Digest,
MGD: Digest + FixedOutputReset,
{
}
impl<T, D, MGD> RandomizedDecryptor for DecryptingKey<T, D, MGD>
where
D: Digest,
MGD: Digest + FixedOutputReset,
T: UnsignedModularInt,
{
}
impl<T, D, MGD> ZeroizeOnDrop for DecryptingKey<T, D, MGD>
where
D: Digest,
MGD: Digest + FixedOutputReset,
T: UnsignedModularInt,
{
}
impl<T, D, MGD> PartialEq for DecryptingKey<T, D, MGD>
where
D: Digest,
MGD: Digest + FixedOutputReset,
T: UnsignedModularInt,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner && self.label == other.label
}
}
#[cfg(test)]
mod tests {
#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use super::*;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};
use sha2::Sha256;
let mut rng = ChaCha8Rng::from_seed([42; 32]);
}
}