Skip to main content

rsa/
oaep.rs

1//! Encryption and Decryption using [OAEP padding](https://datatracker.ietf.org/doc/html/rfc8017#section-7.1).
2//!
3//! # Usage
4//!
5//! See [code example in the toplevel rustdoc](../index.html#oaep-encryption).
6
7#[cfg(feature = "private-key")]
8mod decrypting_key;
9mod encrypting_key;
10#[cfg(not(feature = "alloc"))]
11mod label;
12
13#[cfg(feature = "private-key")]
14pub use self::decrypting_key::DecryptingKey;
15#[cfg(feature = "alloc")]
16pub use self::encrypting_key::EncryptingKey;
17pub use self::encrypting_key::GenericEncryptingKey;
18#[cfg(not(feature = "alloc"))]
19pub use self::label::{Label, MAX_LABEL_LEN};
20
21#[cfg(feature = "alloc")]
22use alloc::boxed::Box;
23#[cfg(feature = "alloc")]
24use alloc::{vec, vec::Vec};
25use core::fmt;
26#[cfg(feature = "alloc")]
27use crypto_bigint::BoxedUint;
28
29use digest::{Digest, FixedOutputReset};
30use rand_core::TryCryptoRng;
31
32use crate::algorithms::oaep::*;
33#[cfg(feature = "alloc")]
34use crate::algorithms::pad::{uint_to_be_pad, uint_to_be_pad_into, uint_to_zeroizing_be_pad};
35#[cfg(feature = "private-key")]
36use crate::algorithms::rsa::rsa_decrypt_and_check;
37#[cfg(feature = "alloc")]
38use crate::algorithms::rsa::rsa_encrypt;
39use crate::errors::{Error, Result};
40#[cfg(feature = "private-key")]
41use crate::key::RsaPrivateKey;
42#[cfg(feature = "alloc")]
43use crate::key::{self, RsaPublicKey};
44use crate::traits::{PaddingScheme, PublicKeyParts, UnsignedModularInt};
45
46/// Encryption and Decryption using [OAEP padding](https://datatracker.ietf.org/doc/html/rfc8017#section-7.1).
47///
48/// - `digest` is used to hash the label. The maximum possible plaintext length is `m = k - 2 * h_len - 2`,
49///   where `k` is the size of the RSA modulus.
50/// - `mgf_digest` specifies the hash function that is used in the [MGF1](https://datatracker.ietf.org/doc/html/rfc8017#appendix-B.2).
51/// - `label` is optional data that can be associated with the message.
52///
53/// The two hash functions can, but don't need to be the same.
54///
55/// A prominent example is the [`AndroidKeyStore`](https://developer.android.com/guide/topics/security/cryptography#oaep-mgf1-digest).
56/// It uses SHA-1 for `mgf_digest` and a user-chosen SHA flavour for `digest`.
57#[cfg(feature = "alloc")]
58pub struct Oaep<D, MGD = D> {
59    /// Digest type to use.
60    pub digest: D,
61
62    /// Digest to use for Mask Generation Function (MGF).
63    pub mgf_digest: MGD,
64
65    /// Optional label.
66    pub label: Option<Box<[u8]>>,
67}
68
69#[cfg(feature = "alloc")]
70impl<D> Default for Oaep<D>
71where
72    D: Digest + FixedOutputReset,
73{
74    fn default() -> Self {
75        Self::new()
76    }
77}
78
79#[cfg(feature = "alloc")]
80impl<D> Oaep<D>
81where
82    D: Digest + FixedOutputReset,
83{
84    /// Create a new OAEP `PaddingScheme`, using `T` as the hash function for both the default (empty) label and for MGF1.
85    ///
86    /// # Example
87    /// ```
88    /// use sha1::Sha1;
89    /// use sha2::Sha256;
90    /// use rsa::{RsaPublicKey, Oaep};
91    /// use base64ct::{Base64, Encoding};
92    /// use crypto_bigint::BoxedUint;
93    ///
94    /// let n_bytes = Base64::decode_vec("seAOhmYFAjH6NOaB54dboqw86uPXV/oK9ayJGV4mVClbvsDBJmF3bVkOaVMp9ogcFJTFFSy5g2HsTZIfHyuQVUJADb+BeRnkYrYhRvNJOKj2pcDbkxYe9XGMx8pIvxkDFnIpusb3gUsuzMUAU5qIstjwQKzuD51c6uJi0HAtQkr6Wmlt34SX7xkD/MfRuTu9uqmHmkiiJaCDHB2reYTPguetSWfuvp1qBJDNgSsp7BjwYANWldyrmZ8cLXEXYMUG5vtsWMxUzl8ertEr6kbnGM0OJghNuEtittW/dfTPvk683R1jj0hNaMzvHK8xYldUlLuwmWCYIIvpHBaA/w+FwQ==").unwrap();
95    /// let e_bytes = Base64::decode_vec("AQAB").unwrap();
96    /// let n = BoxedUint::from_be_slice(&n_bytes, 2048).unwrap();
97    /// let e = BoxedUint::from_be_slice(&e_bytes, 32).unwrap();
98    ///
99    /// let mut rng = rand::rng();
100    /// let key = RsaPublicKey::new(n, e).unwrap();
101    /// let padding = Oaep::<Sha256>::new();
102    /// let encrypted_data = key.encrypt(&mut rng, padding, b"secret").unwrap();
103    /// ```
104    pub fn new() -> Self {
105        Self {
106            digest: D::new(),
107            mgf_digest: D::new(),
108            label: None,
109        }
110    }
111
112    /// Create a new OAEP `PaddingScheme` with an associated `label`, using `T` as the hash function for both the label and for MGF1.
113    pub fn new_with_label<S: Into<Box<[u8]>>>(label: S) -> Self {
114        Self {
115            digest: D::new(),
116            mgf_digest: D::new(),
117            label: Some(label.into()),
118        }
119    }
120}
121
122#[cfg(feature = "alloc")]
123impl<D, MGD> Oaep<D, MGD>
124where
125    D: Digest + FixedOutputReset,
126    MGD: Digest + FixedOutputReset,
127{
128    /// Create a new OAEP `PaddingScheme`, using `T` as the hash function for the default (empty) label, and `U` as the hash function for MGF1.
129    /// If a label is needed use `PaddingScheme::new_oaep_with_label` or `PaddingScheme::new_oaep_with_mgf_hash_with_label`.
130    ///
131    /// # Example
132    /// ```
133    /// use sha1::Sha1;
134    /// use sha2::Sha256;
135    /// use rsa::{RsaPublicKey, Oaep};
136    /// use base64ct::{Base64, Encoding};
137    /// use crypto_bigint::BoxedUint;
138    ///
139    /// let n_bytes = Base64::decode_vec("seAOhmYFAjH6NOaB54dboqw86uPXV/oK9ayJGV4mVClbvsDBJmF3bVkOaVMp9ogcFJTFFSy5g2HsTZIfHyuQVUJADb+BeRnkYrYhRvNJOKj2pcDbkxYe9XGMx8pIvxkDFnIpusb3gUsuzMUAU5qIstjwQKzuD51c6uJi0HAtQkr6Wmlt34SX7xkD/MfRuTu9uqmHmkiiJaCDHB2reYTPguetSWfuvp1qBJDNgSsp7BjwYANWldyrmZ8cLXEXYMUG5vtsWMxUzl8ertEr6kbnGM0OJghNuEtittW/dfTPvk683R1jj0hNaMzvHK8xYldUlLuwmWCYIIvpHBaA/w+FwQ==").unwrap();
140    /// let e_bytes = Base64::decode_vec("AQAB").unwrap();
141    /// let n = BoxedUint::from_be_slice(&n_bytes, 2048).unwrap();
142    /// let e = BoxedUint::from_be_slice(&e_bytes, 32).unwrap();
143    ///
144    /// let mut rng = rand::rng();
145    /// let key = RsaPublicKey::new(n, e).unwrap();
146    /// let padding = Oaep::<Sha256, Sha1>::new_with_mgf_hash();
147    /// let encrypted_data = key.encrypt(&mut rng, padding, b"secret").unwrap();
148    /// ```
149    pub fn new_with_mgf_hash() -> Self {
150        Self {
151            digest: D::new(),
152            mgf_digest: MGD::new(),
153            label: None,
154        }
155    }
156
157    /// Create a new OAEP `PaddingScheme` with an associated `label`, using `T` as the hash function for the label, and `U` as the hash function for MGF1.
158    pub fn new_with_mgf_hash_and_label<S: Into<Box<[u8]>>>(label: S) -> Self {
159        Self {
160            digest: D::new(),
161            mgf_digest: MGD::new(),
162            label: Some(label.into()),
163        }
164    }
165}
166
167#[cfg(feature = "alloc")]
168impl<D, MGD> PaddingScheme for Oaep<D, MGD>
169where
170    D: Digest + FixedOutputReset,
171    MGD: Digest + FixedOutputReset,
172{
173    #[cfg(feature = "private-key")]
174    fn decrypt<Rng: TryCryptoRng + ?Sized>(
175        mut self,
176        rng: Option<&mut Rng>,
177        priv_key: &RsaPrivateKey,
178        ciphertext: &[u8],
179    ) -> Result<Vec<u8>> {
180        decrypt(
181            rng,
182            priv_key,
183            ciphertext,
184            &mut self.digest,
185            &mut self.mgf_digest,
186            self.label,
187        )
188    }
189
190    fn encrypt<Rng, K, T>(mut self, rng: &mut Rng, pub_key: &K, msg: &[u8]) -> Result<Vec<u8>>
191    where
192        Rng: TryCryptoRng + ?Sized,
193        T: UnsignedModularInt,
194        K: PublicKeyParts<T>,
195    {
196        let em = oaep_encrypt(
197            rng,
198            msg,
199            &mut self.digest,
200            &mut self.mgf_digest,
201            self.label,
202            pub_key.size(),
203        )?;
204        let int = T::try_from_be_bytes_vartime(&em)?;
205        let mut storage = vec![0u8; pub_key.size()];
206        let ciphertext =
207            uint_to_be_pad_into(rsa_encrypt(pub_key, &int)?, pub_key.size(), &mut storage)?;
208        Ok(ciphertext.to_vec())
209    }
210}
211
212#[cfg(feature = "alloc")]
213impl<D, MGD> fmt::Debug for Oaep<D, MGD> {
214    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215        f.debug_struct("OAEP")
216            .field("digest", &"...")
217            .field("mgf_digest", &"...")
218            .field("label", &self.label)
219            .finish()
220    }
221}
222
223/// Encrypts the given message with RSA and the padding scheme from
224/// [PKCS#1 OAEP].
225///
226/// The message must be no longer than the length of the public modulus minus
227/// `2 + (2 * hash.size())`.
228///
229/// [PKCS#1 OAEP]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1
230#[cfg(feature = "alloc")]
231#[inline]
232#[allow(dead_code)]
233fn encrypt<R, D, MGD>(
234    rng: &mut R,
235    pub_key: &RsaPublicKey,
236    msg: &[u8],
237    digest: &mut D,
238    mgf_digest: &mut MGD,
239    label: Option<Box<[u8]>>,
240) -> Result<Vec<u8>>
241where
242    R: TryCryptoRng + ?Sized,
243    D: Digest + FixedOutputReset,
244    MGD: Digest + FixedOutputReset,
245{
246    key::check_public(pub_key)?;
247
248    let em = oaep_encrypt(rng, msg, digest, mgf_digest, label, pub_key.size())?;
249
250    let int = BoxedUint::from_be_slice(&em, pub_key.n_bits_precision())?;
251    uint_to_be_pad(rsa_encrypt(pub_key, &int)?, pub_key.size())
252}
253
254/// Encrypts the given message with RSA and the padding scheme from
255/// [PKCS#1 OAEP].
256///
257/// The message must be no longer than the length of the public modulus minus
258/// `2 + (2 * hash.size())`.
259///
260/// [PKCS#1 OAEP]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1
261#[cfg(feature = "alloc")]
262#[allow(dead_code)]
263fn encrypt_digest<R, D, MGD>(
264    rng: &mut R,
265    pub_key: &RsaPublicKey,
266    msg: &[u8],
267    label: Option<Box<[u8]>>,
268) -> Result<Vec<u8>>
269where
270    R: TryCryptoRng + ?Sized,
271    D: Digest,
272    MGD: Digest + FixedOutputReset,
273{
274    key::check_public(pub_key)?;
275
276    let em = oaep_encrypt_digest::<_, D, MGD>(rng, msg, label, pub_key.size())?;
277
278    let int = BoxedUint::from_be_slice(&em, pub_key.n_bits_precision())?;
279    uint_to_be_pad(rsa_encrypt(pub_key, &int)?, pub_key.size())
280}
281
282/// Does not call `key::check_public` — that validator is `alloc`-only.
283pub fn encrypt_digest_into<'a, R, D, MGD, K, T>(
284    rng: &mut R,
285    pub_key: &K,
286    msg: &[u8],
287    label: Option<&[u8]>,
288    storage: &'a mut [u8],
289) -> crate::Result<&'a [u8]>
290where
291    R: rand_core::TryCryptoRng + ?Sized,
292    D: digest::Digest,
293    MGD: digest::Digest + digest::FixedOutputReset,
294    K: crate::traits::PublicKeyParts<T>,
295    T: crate::traits::UnsignedModularInt,
296{
297    let padded_len = pub_key.size();
298    let em = crate::algorithms::oaep::oaep_encrypt_digest_into::<_, D, MGD>(
299        rng, msg, label, padded_len, storage,
300    )?;
301    let int = T::try_from_be_bytes_vartime(em)?;
302    crate::algorithms::pad::uint_to_be_pad_into(
303        crate::algorithms::rsa::rsa_encrypt(pub_key, &int)?,
304        padded_len,
305        storage,
306    )
307}
308
309/// Decrypts a plaintext using RSA and the padding scheme from [PKCS#1 OAEP].
310///
311/// If an `rng` is passed, it uses RSA blinding to avoid timing side-channel attacks.
312///
313/// Note that whether this function returns an error or not discloses secret
314/// information. If an attacker can cause this function to run repeatedly and
315/// learn whether each instance returned an error then they can decrypt and
316/// forge signatures as if they had the private key.
317///
318/// See `decrypt_session_key` for a way of solving this problem.
319///
320/// [PKCS#1 OAEP]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1
321#[cfg(feature = "private-key")]
322#[inline]
323fn decrypt<R, D, MGD>(
324    rng: Option<&mut R>,
325    priv_key: &RsaPrivateKey,
326    ciphertext: &[u8],
327    digest: &mut D,
328    mgf_digest: &mut MGD,
329    label: Option<Box<[u8]>>,
330) -> Result<Vec<u8>>
331where
332    R: TryCryptoRng + ?Sized,
333    D: Digest + FixedOutputReset,
334    MGD: Digest + FixedOutputReset,
335{
336    if ciphertext.len() != priv_key.size() {
337        return Err(Error::Decryption);
338    }
339
340    let ciphertext = BoxedUint::from_be_slice(ciphertext, priv_key.n_bits_precision())?;
341
342    let em = rsa_decrypt_and_check(priv_key, rng, &ciphertext)?;
343    let mut em = uint_to_zeroizing_be_pad(em, priv_key.size())?;
344
345    oaep_decrypt(&mut em, digest, mgf_digest, label, priv_key.size())
346}
347
348/// Decrypts a plaintext using RSA and the padding scheme from [PKCS#1 OAEP].
349///
350/// If an `rng` is passed, it uses RSA blinding to avoid timing side-channel attacks.
351///
352/// Note that whether this function returns an error or not discloses secret
353/// information. If an attacker can cause this function to run repeatedly and
354/// learn whether each instance returned an error then they can decrypt and
355/// forge signatures as if they had the private key.
356///
357/// See `decrypt_session_key` for a way of solving this problem.
358///
359/// [PKCS#1 OAEP]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1
360#[cfg(feature = "private-key")]
361#[inline]
362fn decrypt_digest<R, D, MGD>(
363    rng: Option<&mut R>,
364    priv_key: &RsaPrivateKey,
365    ciphertext: &[u8],
366    label: Option<Box<[u8]>>,
367) -> Result<Vec<u8>>
368where
369    R: TryCryptoRng + ?Sized,
370    D: Digest,
371    MGD: Digest + FixedOutputReset,
372{
373    key::check_public(priv_key)?;
374
375    if ciphertext.len() != priv_key.size() {
376        return Err(Error::Decryption);
377    }
378
379    let ciphertext = BoxedUint::from_be_slice(ciphertext, priv_key.n_bits_precision())?;
380    let em = rsa_decrypt_and_check(priv_key, rng, &ciphertext)?;
381    let mut em = uint_to_zeroizing_be_pad(em, priv_key.size())?;
382
383    oaep_decrypt_digest::<D, MGD>(&mut em, label, priv_key.size())
384}
385
386#[cfg(test)]
387#[cfg(all(feature = "alloc", feature = "private-key"))]
388mod tests {
389    use crate::key::{RsaPrivateKey, RsaPublicKey};
390    use crate::oaep::{DecryptingKey, EncryptingKey, Oaep};
391    use crate::traits::PublicKeyParts;
392    use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor};
393
394    use crypto_bigint::BoxedUint;
395    use digest::{Digest, FixedOutputReset};
396    use rand::rngs::ChaCha8Rng;
397    use rand_core::{Rng, SeedableRng};
398    use sha1::Sha1;
399    use sha2::{Sha224, Sha256, Sha384, Sha512};
400    use sha3::{Sha3_256, Sha3_384, Sha3_512};
401
402    fn get_private_key() -> RsaPrivateKey {
403        // -----BEGIN RSA PRIVATE KEY-----
404        // MIIEpAIBAAKCAQEA05e4TZikwmE47RtpWoEG6tkdVTvwYEG2LT/cUKBB4iK49FKW
405        // icG4LF5xVU9d1p+i9LYVjPDb61eBGg/DJ+HyjnT+dNO8Fmweq9wbi1e5NMqL5bAL
406        // TymXW8yZrK9BW1m7KKZ4K7QaLDwpdrPBjbre9i8AxrsiZkAJUJbAzGDSL+fvmH11
407        // xqgbENlr8pICivEQ3HzBu8Q9Iq2rN5oM1dgHjMeA/1zWIJ3qNMkiz3hPdxfkKNdb
408        // WuyP8w5fAUFRB2bi4KuNRzyE6HELK5gifD2wlTN600UvGeK5v7zN2BSKv2d2+lUn
409        // debnWVbkUimuWpxGlJurHmIvDkj1ZSSoTtNIOwIDAQABAoIBAQDE5wxokWLJTGYI
410        // KBkbUrTYOSEV30hqmtvoMeRY1zlYMg3Bt1VFbpNwHpcC12+wuS+Q4B0f4kgVMoH+
411        // eaqXY6kvrmnY1+zRRN4p+hNb0U+Vc+NJ5FAx47dpgvWDADgmxVLomjl8Gga9IWNI
412        // hjDZLowrtkPXq+9wDaldaFyUFImkb1S1MW9itdLDp/G70TTLNzU6RGg/3J2V02RY
413        // 3iL2xEBX/nSgpDbEMI9z9NpC81xHrBanE41IOvyR5B3DoRJzguDA9RGbAiG0/GOd
414        // a5w4F3pt6bUm69iMONeYLAf5ig79h31Qiq4nW5RpFcAuLhEG0XXXTsZ3f16A0SwF
415        // PZx74eNBAoGBAPgnu/OkGHfHzFmuv0LtSynDLe/LjtloY9WwkKBaiTDdYkohydz5
416        // g4Vo/foN9luEYqXyrJE9bFb5dVMr2OePsHvUBcqZpIS89Z8Bm73cs5M/K85wYwC0
417        // 97EQEgxd+QGBWQZ8NdowYaVshjWlK1QnOzEnG0MR8Hld9gIeY1XhpC5hAoGBANpI
418        // F84Aid028q3mo/9BDHPsNL8bT2vaOEMb/t4RzvH39u+nDl+AY6Ox9uFylv+xX+76
419        // CRKgMluNH9ZaVZ5xe1uWHsNFBy4OxSA9A0QdKa9NZAVKBFB0EM8dp457YRnZCexm
420        // 5q1iW/mVsnmks8W+fYlc18W5xMSX/ecwkW/NtOQbAoGAHabpz4AhKFbodSLrWbzv
421        // CUt4NroVFKdjnoodjfujfwJFF2SYMV5jN9LG3lVCxca43ulzc1tqka33Nfv8TBcg
422        // WHuKQZ5ASVgm5VwU1wgDMSoQOve07MWy/yZTccTc1zA0ihDXgn3bfR/NnaVh2wlh
423        // CkuI92eyW1494hztc7qlmqECgYEA1zenyOQ9ChDIW/ABGIahaZamNxsNRrDFMl3j
424        // AD+cxHSRU59qC32CQH8ShRy/huHzTaPX2DZ9EEln76fnrS4Ey7uLH0rrFl1XvT6K
425        // /timJgLvMEvXTx/xBtUdRN2fUqXtI9odbSyCtOYFL+zVl44HJq2UzY4pVRDrNcxs
426        // SUkQJqsCgYBSaNfPBzR5rrstLtTdZrjImRW1LRQeDEky9WsMDtCTYUGJTsTSfVO8
427        // hkU82MpbRVBFIYx+GWIJwcZRcC7OCQoV48vMJllxMAAjqG/p00rVJ+nvA7et/nNu
428        // BoB0er/UmDm4Ly/97EO9A0PKMOE5YbMq9s3t3RlWcsdrU7dvw+p2+A==
429        // -----END RSA PRIVATE KEY-----
430
431        RsaPrivateKey::from_components(
432            BoxedUint::from_be_hex("d397b84d98a4c26138ed1b695a8106ead91d553bf06041b62d3fdc50a041e222b8f4529689c1b82c5e71554f5dd69fa2f4b6158cf0dbeb57811a0fc327e1f28e74fe74d3bc166c1eabdc1b8b57b934ca8be5b00b4f29975bcc99acaf415b59bb28a6782bb41a2c3c2976b3c18dbadef62f00c6bb226640095096c0cc60d22fe7ef987d75c6a81b10d96bf292028af110dc7cc1bbc43d22adab379a0cd5d8078cc780ff5cd6209dea34c922cf784f7717e428d75b5aec8ff30e5f0141510766e2e0ab8d473c84e8710b2b98227c3db095337ad3452f19e2b9bfbccdd8148abf6776fa552775e6e75956e45229ae5a9c46949bab1e622f0e48f56524a84ed3483b", 2048).unwrap(),
433            BoxedUint::from(65_537u64),
434            BoxedUint::from_be_hex("c4e70c689162c94c660828191b52b4d8392115df486a9adbe831e458d73958320dc1b755456e93701e9702d76fb0b92f90e01d1fe248153281fe79aa9763a92fae69d8d7ecd144de29fa135bd14f9573e349e45031e3b76982f583003826c552e89a397c1a06bd2163488630d92e8c2bb643d7abef700da95d685c941489a46f54b5316f62b5d2c3a7f1bbd134cb37353a44683fdc9d95d36458de22f6c44057fe74a0a436c4308f73f4da42f35c47ac16a7138d483afc91e41dc3a1127382e0c0f5119b0221b4fc639d6b9c38177a6de9b526ebd88c38d7982c07f98a0efd877d508aae275b946915c02e2e1106d175d74ec6777f5e80d12c053d9c7be1e341", 2048).unwrap(),
435            vec![
436                BoxedUint::from_be_hex("f827bbf3a41877c7cc59aebf42ed4b29c32defcb8ed96863d5b090a05a8930dd624a21c9dcf9838568fdfa0df65b8462a5f2ac913d6c56f975532bd8e78fb07bd405ca99a484bcf59f019bbddcb3933f2bce706300b4f7b110120c5df9018159067c35da3061a56c8635a52b54273b31271b4311f0795df6021e6355e1a42e61", 1024).unwrap(),
437                BoxedUint::from_be_hex("da4817ce0089dd36f2ade6a3ff410c73ec34bf1b4f6bda38431bfede11cef1f7f6efa70e5f8063a3b1f6e17296ffb15feefa0912a0325b8d1fd65a559e717b5b961ec345072e0ec5203d03441d29af4d64054a04507410cf1da78e7b6119d909ec66e6ad625bf995b279a4b3c5be7d895cd7c5b9c4c497fde730916fcdb4e41b", 1024).unwrap()
438            ],
439        ).unwrap()
440    }
441
442    #[test]
443    fn test_encrypt_decrypt_oaep() {
444        let priv_key = get_private_key();
445        do_test_encrypt_decrypt_oaep::<Sha1>(&priv_key);
446        do_test_encrypt_decrypt_oaep::<Sha224>(&priv_key);
447        do_test_encrypt_decrypt_oaep::<Sha256>(&priv_key);
448        do_test_encrypt_decrypt_oaep::<Sha384>(&priv_key);
449        do_test_encrypt_decrypt_oaep::<Sha512>(&priv_key);
450        do_test_encrypt_decrypt_oaep::<Sha3_256>(&priv_key);
451        do_test_encrypt_decrypt_oaep::<Sha3_384>(&priv_key);
452        do_test_encrypt_decrypt_oaep::<Sha3_512>(&priv_key);
453
454        do_test_oaep_with_different_hashes::<Sha1, Sha1>(&priv_key);
455        do_test_oaep_with_different_hashes::<Sha224, Sha1>(&priv_key);
456        do_test_oaep_with_different_hashes::<Sha256, Sha1>(&priv_key);
457        do_test_oaep_with_different_hashes::<Sha384, Sha1>(&priv_key);
458        do_test_oaep_with_different_hashes::<Sha512, Sha1>(&priv_key);
459        do_test_oaep_with_different_hashes::<Sha3_256, Sha1>(&priv_key);
460        do_test_oaep_with_different_hashes::<Sha3_384, Sha1>(&priv_key);
461        do_test_oaep_with_different_hashes::<Sha3_512, Sha1>(&priv_key);
462    }
463
464    fn get_label(rng: &mut ChaCha8Rng) -> Option<Box<[u8]>> {
465        let mut buf = [0u8; 32];
466        rng.fill_bytes(&mut buf);
467
468        if rng.next_u32() % 2 == 0 {
469            Some(buf.into())
470        } else {
471            None
472        }
473    }
474
475    fn do_test_encrypt_decrypt_oaep<D: Digest + FixedOutputReset>(prk: &RsaPrivateKey) {
476        let mut rng = ChaCha8Rng::from_seed([42; 32]);
477
478        let k = prk.size();
479
480        for i in 1..8 {
481            let mut input = vec![0u8; i * 8];
482            rng.fill_bytes(&mut input);
483
484            if input.len() > k - 11 {
485                input = input[0..k - 11].to_vec();
486            }
487            let label = get_label(&mut rng);
488
489            let pub_key: RsaPublicKey = prk.into();
490
491            let ciphertext = if let Some(ref label) = label {
492                let padding = Oaep::<D>::new_with_label(label.clone());
493                pub_key.encrypt(&mut rng, padding, &input).unwrap()
494            } else {
495                let padding = Oaep::<D>::new();
496                pub_key.encrypt(&mut rng, padding, &input).unwrap()
497            };
498
499            assert_ne!(input, ciphertext);
500            let blind: bool = rng.next_u32() < (1 << 31);
501
502            let padding = if let Some(label) = label {
503                Oaep::<D>::new_with_label::<Box<[u8]>>(label)
504            } else {
505                Oaep::<D>::new()
506            };
507
508            let plaintext = if blind {
509                prk.decrypt(padding, &ciphertext).unwrap()
510            } else {
511                prk.decrypt_blinded(&mut rng, padding, &ciphertext).unwrap()
512            };
513
514            assert_eq!(input, plaintext);
515        }
516    }
517
518    fn do_test_oaep_with_different_hashes<
519        D: Digest + FixedOutputReset,
520        U: Digest + FixedOutputReset,
521    >(
522        prk: &RsaPrivateKey,
523    ) {
524        let mut rng = ChaCha8Rng::from_seed([42; 32]);
525
526        let k = prk.size();
527
528        for i in 1..8 {
529            let mut input = vec![0u8; i * 8];
530            rng.fill_bytes(&mut input);
531
532            if input.len() > k - 11 {
533                input = input[0..k - 11].to_vec();
534            }
535            let label = get_label(&mut rng);
536
537            let pub_key: RsaPublicKey = prk.into();
538
539            let ciphertext = if let Some(ref label) = label {
540                let padding = Oaep::<D, U>::new_with_mgf_hash_and_label::<_>(label.clone());
541                pub_key.encrypt(&mut rng, padding, &input).unwrap()
542            } else {
543                let padding = Oaep::<D, U>::new_with_mgf_hash();
544                pub_key.encrypt(&mut rng, padding, &input).unwrap()
545            };
546
547            assert_ne!(input, ciphertext);
548            let blind: bool = rng.next_u32() < (1 << 31);
549
550            let padding = if let Some(label) = label {
551                Oaep::<D, U>::new_with_mgf_hash_and_label::<_>(label)
552            } else {
553                Oaep::<D, U>::new_with_mgf_hash()
554            };
555
556            let plaintext = if blind {
557                prk.decrypt(padding, &ciphertext).unwrap()
558            } else {
559                prk.decrypt_blinded(&mut rng, padding, &ciphertext).unwrap()
560            };
561
562            assert_eq!(input, plaintext);
563        }
564    }
565
566    #[test]
567    fn test_decrypt_oaep_invalid_hash() {
568        let mut rng = ChaCha8Rng::from_seed([42; 32]);
569        let priv_key = get_private_key();
570        let pub_key: RsaPublicKey = (&priv_key).into();
571        let ciphertext = pub_key
572            .encrypt(&mut rng, Oaep::<Sha1>::new(), "a_plain_text".as_bytes())
573            .unwrap();
574        assert!(
575            priv_key
576                .decrypt_blinded(
577                    &mut rng,
578                    Oaep::<Sha1>::new_with_label::<_>("label".as_bytes()),
579                    &ciphertext,
580                )
581                .is_err(),
582            "decrypt should have failed on hash verification"
583        );
584    }
585
586    #[test]
587    fn test_encrypt_decrypt_oaep_traits() {
588        let priv_key = get_private_key();
589        do_test_encrypt_decrypt_oaep_traits::<Sha1>(&priv_key);
590        do_test_encrypt_decrypt_oaep_traits::<Sha224>(&priv_key);
591        do_test_encrypt_decrypt_oaep_traits::<Sha256>(&priv_key);
592        do_test_encrypt_decrypt_oaep_traits::<Sha384>(&priv_key);
593        do_test_encrypt_decrypt_oaep_traits::<Sha512>(&priv_key);
594        do_test_encrypt_decrypt_oaep_traits::<Sha3_256>(&priv_key);
595        do_test_encrypt_decrypt_oaep_traits::<Sha3_384>(&priv_key);
596        do_test_encrypt_decrypt_oaep_traits::<Sha3_512>(&priv_key);
597
598        do_test_oaep_with_different_hashes_traits::<Sha1, Sha1>(&priv_key);
599        do_test_oaep_with_different_hashes_traits::<Sha224, Sha1>(&priv_key);
600        do_test_oaep_with_different_hashes_traits::<Sha256, Sha1>(&priv_key);
601        do_test_oaep_with_different_hashes_traits::<Sha384, Sha1>(&priv_key);
602        do_test_oaep_with_different_hashes_traits::<Sha512, Sha1>(&priv_key);
603        do_test_oaep_with_different_hashes_traits::<Sha3_256, Sha1>(&priv_key);
604        do_test_oaep_with_different_hashes_traits::<Sha3_384, Sha1>(&priv_key);
605        do_test_oaep_with_different_hashes_traits::<Sha3_512, Sha1>(&priv_key);
606    }
607
608    fn do_test_encrypt_decrypt_oaep_traits<D: Digest + FixedOutputReset>(prk: &RsaPrivateKey) {
609        do_test_oaep_with_different_hashes_traits::<D, D>(prk);
610    }
611
612    fn do_test_oaep_with_different_hashes_traits<D: Digest, MGD: Digest + FixedOutputReset>(
613        prk: &RsaPrivateKey,
614    ) {
615        let mut rng = ChaCha8Rng::from_seed([42; 32]);
616
617        let k = prk.size();
618
619        for i in 1..8 {
620            let mut input = vec![0u8; i * 8];
621            rng.fill_bytes(&mut input);
622
623            if input.len() > k - 11 {
624                input = input[0..k - 11].to_vec();
625            }
626            let label = get_label(&mut rng);
627
628            let pub_key: RsaPublicKey = prk.into();
629
630            let ciphertext = if let Some(ref label) = label {
631                let encrypting_key =
632                    EncryptingKey::<D, MGD>::new_with_label(pub_key, label.clone());
633                encrypting_key.encrypt_with_rng(&mut rng, &input).unwrap()
634            } else {
635                let encrypting_key = EncryptingKey::<D, MGD>::new(pub_key);
636                encrypting_key.encrypt_with_rng(&mut rng, &input).unwrap()
637            };
638
639            assert_ne!(input, ciphertext);
640            let blind: bool = rng.next_u32() < (1 << 31);
641
642            let decrypting_key = if let Some(ref label) = label {
643                DecryptingKey::<D, MGD>::new_with_label(prk.clone(), label.clone())
644            } else {
645                DecryptingKey::<D, MGD>::new(prk.clone())
646            };
647
648            let plaintext = if blind {
649                decrypting_key.decrypt(&ciphertext).unwrap()
650            } else {
651                decrypting_key
652                    .decrypt_with_rng(&mut rng, &ciphertext)
653                    .unwrap()
654            };
655
656            assert_eq!(input, plaintext);
657        }
658    }
659
660    #[test]
661    fn test_decrypt_oaep_invalid_hash_traits() {
662        let mut rng = ChaCha8Rng::from_seed([42; 32]);
663        let priv_key = get_private_key();
664        let pub_key: RsaPublicKey = (&priv_key).into();
665        let encrypting_key = EncryptingKey::<Sha1>::new(pub_key);
666        let decrypting_key = DecryptingKey::<Sha1>::new_with_label(priv_key, "label".as_bytes());
667        let ciphertext = encrypting_key
668            .encrypt_with_rng(&mut rng, "a_plain_text".as_bytes())
669            .unwrap();
670        assert!(
671            decrypting_key
672                .decrypt_with_rng(&mut rng, &ciphertext)
673                .is_err(),
674            "decrypt should have failed on hash verification"
675        );
676    }
677}