rsa/oaep/
decrypting_key.rs1use super::decrypt_digest;
2use crate::{
3 dummy_rng::DummyRng,
4 traits::{Decryptor, RandomizedDecryptor},
5 Result, RsaPrivateKey,
6};
7use alloc::{
8 string::{String, ToString},
9 vec::Vec,
10};
11use core::marker::PhantomData;
12use digest::{Digest, FixedOutputReset};
13use rand_core::CryptoRngCore;
14use zeroize::ZeroizeOnDrop;
15
16#[derive(Debug, Clone)]
20pub struct DecryptingKey<D, MGD = D>
21where
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
31impl<D, MGD> DecryptingKey<D, MGD>
32where
33 D: Digest,
34 MGD: Digest + FixedOutputReset,
35{
36 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 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
57impl<D, MGD> Decryptor for DecryptingKey<D, MGD>
58where
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
72impl<D, MGD> RandomizedDecryptor for DecryptingKey<D, MGD>
73where
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
91impl<D, MGD> ZeroizeOnDrop for DecryptingKey<D, MGD>
92where
93 D: Digest,
94 MGD: Digest + FixedOutputReset,
95{
96}