Skip to main content

rsa/
key.rs

1use alloc::vec::Vec;
2use core::cmp::Ordering;
3use core::fmt;
4use core::hash::{Hash, Hasher};
5
6use crypto_bigint::{
7    modular::{BoxedMontyForm, BoxedMontyParams},
8    BoxedUint, ConcatenatingMul, Integer, NonZero, Odd, Resize,
9};
10use rand_core::CryptoRng;
11use zeroize::{Zeroize, ZeroizeOnDrop};
12#[cfg(feature = "serde")]
13use {
14    pkcs8::{DecodePrivateKey, EncodePrivateKey},
15    serdect::serde::{de, ser, Deserialize, Serialize},
16    spki::{DecodePublicKey, EncodePublicKey},
17};
18
19use crate::algorithms::generate::generate_multi_prime_key_with_exp;
20use crate::algorithms::rsa::{
21    compute_modulus, compute_private_exponent_carmicheal, compute_private_exponent_euler_totient,
22    recover_primes,
23};
24
25use crate::dummy_rng::DummyRng;
26use crate::errors::{Error, Result};
27use crate::traits::keys::{CrtValue, PrivateKeyParts, PublicKeyParts};
28use crate::traits::{PaddingScheme, SignatureScheme};
29
30/// Represents the public part of an RSA key.
31#[derive(Debug, Clone)]
32pub struct RsaPublicKey {
33    /// Modulus: product of prime numbers `p` and `q`
34    n: NonZero<BoxedUint>,
35    /// Public exponent: power to which a plaintext message is raised in
36    /// order to encrypt it.
37    ///
38    /// Typically `0x10001` (`65537`)
39    e: BoxedUint,
40
41    n_params: BoxedMontyParams,
42}
43
44impl Eq for RsaPublicKey {}
45
46impl PartialEq for RsaPublicKey {
47    #[inline]
48    fn eq(&self, other: &RsaPublicKey) -> bool {
49        self.n == other.n && self.e == other.e
50    }
51}
52
53impl Hash for RsaPublicKey {
54    fn hash<H: Hasher>(&self, state: &mut H) {
55        // Domain separator for RSA private keys
56        state.write(b"RsaPublicKey");
57        // TODO(tarcieri): to match the `PartialEq` impl we should strip leading zeros
58        Hash::hash(&self.n.as_limbs(), state);
59        Hash::hash(&self.e.as_limbs(), state);
60    }
61}
62
63/// Represents a whole RSA key, public and private parts.
64#[derive(Clone)]
65pub struct RsaPrivateKey {
66    /// Public components of the private key.
67    pubkey_components: RsaPublicKey,
68    /// Private exponent
69    pub(crate) d: BoxedUint,
70    /// Prime factors of N, contains >= 2 elements.
71    pub(crate) primes: Vec<BoxedUint>,
72    /// Precomputed values to speed up private operations
73    pub(crate) precomputed: Option<PrecomputedValues>,
74}
75
76impl fmt::Debug for RsaPrivateKey {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        let precomputed = if self.precomputed.is_some() {
79            "Some(...)"
80        } else {
81            "None"
82        };
83        f.debug_struct("RsaPrivateKey")
84            .field("pubkey_components", &self.pubkey_components)
85            .field("d", &"...")
86            .field("primes", &"&[...]")
87            .field("precomputed", &precomputed)
88            .finish()
89    }
90}
91
92impl Eq for RsaPrivateKey {}
93impl PartialEq for RsaPrivateKey {
94    #[inline]
95    fn eq(&self, other: &RsaPrivateKey) -> bool {
96        self.pubkey_components == other.pubkey_components
97            && self.d == other.d
98            && self.primes == other.primes
99    }
100}
101
102impl AsRef<RsaPublicKey> for RsaPrivateKey {
103    fn as_ref(&self) -> &RsaPublicKey {
104        &self.pubkey_components
105    }
106}
107
108impl Hash for RsaPrivateKey {
109    fn hash<H: Hasher>(&self, state: &mut H) {
110        // Domain separator for RSA private keys
111        state.write(b"RsaPrivateKey");
112        Hash::hash(&self.pubkey_components, state);
113    }
114}
115
116impl Drop for RsaPrivateKey {
117    fn drop(&mut self) {
118        self.d.zeroize();
119        self.primes.zeroize();
120        self.precomputed.zeroize();
121    }
122}
123
124impl ZeroizeOnDrop for RsaPrivateKey {}
125
126#[derive(Clone)]
127pub(crate) struct PrecomputedValues {
128    /// D mod (P-1)
129    pub(crate) dp: BoxedUint,
130    /// D mod (Q-1)
131    pub(crate) dq: BoxedUint,
132    /// Q^-1 mod P
133    pub(crate) qinv: BoxedMontyForm,
134
135    /// Montgomery params for `p`
136    pub(crate) p_params: BoxedMontyParams,
137    /// Montgomery params for `q`
138    pub(crate) q_params: BoxedMontyParams,
139}
140
141impl ZeroizeOnDrop for PrecomputedValues {}
142
143impl Zeroize for PrecomputedValues {
144    fn zeroize(&mut self) {
145        self.dp.zeroize();
146        self.dq.zeroize();
147        // TODO: once these have landed in crypto-bigint
148        // self.p_params.zeroize();
149        // self.q_params.zeroize();
150    }
151}
152
153impl Drop for PrecomputedValues {
154    fn drop(&mut self) {
155        self.zeroize();
156    }
157}
158
159impl From<RsaPrivateKey> for RsaPublicKey {
160    fn from(private_key: RsaPrivateKey) -> Self {
161        (&private_key).into()
162    }
163}
164
165impl From<&RsaPrivateKey> for RsaPublicKey {
166    fn from(private_key: &RsaPrivateKey) -> Self {
167        let n = PublicKeyParts::n(private_key);
168        let e = PublicKeyParts::e(private_key);
169        let n_params = PublicKeyParts::n_params(private_key);
170        RsaPublicKey {
171            n: n.clone(),
172            e: e.clone(),
173            n_params: n_params.clone(),
174        }
175    }
176}
177
178impl PublicKeyParts for RsaPublicKey {
179    fn n(&self) -> &NonZero<BoxedUint> {
180        &self.n
181    }
182
183    fn e(&self) -> &BoxedUint {
184        &self.e
185    }
186
187    fn n_params(&self) -> &BoxedMontyParams {
188        &self.n_params
189    }
190}
191
192impl RsaPublicKey {
193    /// Encrypt the given message.
194    pub fn encrypt<R: CryptoRng + ?Sized, P: PaddingScheme>(
195        &self,
196        rng: &mut R,
197        padding: P,
198        msg: &[u8],
199    ) -> Result<Vec<u8>> {
200        padding.encrypt(rng, self, msg)
201    }
202
203    /// Verify a signed message.
204    ///
205    /// `hashed` must be the result of hashing the input using the hashing function
206    /// passed in through `hash`.
207    ///
208    /// If the message is valid `Ok(())` is returned, otherwise an `Err` indicating failure.
209    pub fn verify<S: SignatureScheme>(&self, scheme: S, hashed: &[u8], sig: &[u8]) -> Result<()> {
210        scheme.verify(self, hashed, sig)
211    }
212}
213
214impl RsaPublicKey {
215    /// Minimum value of the public exponent `e`.
216    pub const MIN_PUB_EXPONENT: u64 = 2;
217
218    /// Maximum value of the public exponent `e`.
219    pub const MAX_PUB_EXPONENT: u64 = (1 << 33) - 1;
220
221    /// Maximum size of the modulus `n` in bits.
222    pub const MAX_SIZE: usize = 8192;
223
224    /// Create a new public key from its components.
225    ///
226    /// This function accepts public keys with a modulus size up to 8192-bits,
227    /// i.e. [`RsaPublicKey::MAX_SIZE`].
228    pub fn new(n: BoxedUint, e: BoxedUint) -> Result<Self> {
229        Self::new_with_max_size(n, e, Self::MAX_SIZE)
230    }
231
232    /// Create a new public key from its components.
233    pub fn new_with_max_size(n: BoxedUint, e: BoxedUint, max_size: usize) -> Result<Self> {
234        check_public_with_max_size(&n, &e, Some(max_size))?;
235
236        let n_odd = Odd::new(n.clone())
237            .into_option()
238            .ok_or(Error::InvalidModulus)?;
239        let n_params = BoxedMontyParams::new(n_odd);
240        let n = NonZero::new(n).expect("checked above");
241
242        Ok(Self { n, e, n_params })
243    }
244
245    /// Create a new public key, bypassing checks around the modulus and public
246    /// exponent size.
247    ///
248    /// This method is not recommended, and only intended for unusual use cases.
249    /// Most applications should use [`RsaPublicKey::new`] or
250    /// [`RsaPublicKey::new_with_max_size`] instead.
251    pub fn new_unchecked(n: BoxedUint, e: BoxedUint) -> Self {
252        let n_odd = Odd::new(n.clone()).expect("n must be odd");
253        let n_params = BoxedMontyParams::new(n_odd);
254        let n = NonZero::new(n).expect("odd numbers are non zero");
255
256        Self { n, e, n_params }
257    }
258}
259
260impl PublicKeyParts for RsaPrivateKey {
261    fn n(&self) -> &NonZero<BoxedUint> {
262        &self.pubkey_components.n
263    }
264
265    fn e(&self) -> &BoxedUint {
266        &self.pubkey_components.e
267    }
268
269    fn n_params(&self) -> &BoxedMontyParams {
270        &self.pubkey_components.n_params
271    }
272}
273
274impl RsaPrivateKey {
275    /// Default exponent for RSA keys.
276    const EXP: u64 = 65537;
277
278    /// Minimum size of the modulus `n` in bits. Currently only applies to keygen.
279    const MIN_SIZE: u32 = 1024;
280
281    /// Generate a new RSA key pair with a modulus of the given bit size using the passed in `rng`.
282    ///
283    /// # Errors
284    /// - If `bit_size` is lower than the minimum 1024-bits.
285    pub fn new<R: CryptoRng + ?Sized>(rng: &mut R, bit_size: usize) -> Result<Self> {
286        Self::new_with_exp(rng, bit_size, Self::EXP.into())
287    }
288
289    /// Generate a new RSA key pair of the given bit size.
290    ///
291    /// #⚠️Warning: Hazmat!
292    /// This version does not apply minimum key size checks, and as such may generate keys
293    /// which are insecure!
294    #[cfg(feature = "hazmat")]
295    pub fn new_unchecked<R: CryptoRng + ?Sized>(rng: &mut R, bit_size: usize) -> Result<Self> {
296        Self::new_with_exp_unchecked(rng, bit_size, Self::EXP.into())
297    }
298
299    /// Generate a new RSA key pair of the given bit size and the public exponent
300    /// using the passed in `rng`.
301    ///
302    /// Unless you have specific needs, you should use [`RsaPrivateKey::new`] instead.
303    pub fn new_with_exp<R: CryptoRng + ?Sized>(
304        rng: &mut R,
305        bit_size: usize,
306        exp: BoxedUint,
307    ) -> Result<RsaPrivateKey> {
308        if bit_size < Self::MIN_SIZE as usize {
309            return Err(Error::ModulusTooSmall);
310        }
311
312        let components = generate_multi_prime_key_with_exp(rng, 2, bit_size, exp)?;
313        RsaPrivateKey::from_components(
314            components.n.get(),
315            components.e,
316            components.d,
317            components.primes,
318        )
319    }
320
321    /// Generate a new RSA key pair of the given bit size and the public exponent
322    /// using the passed in `rng`.
323    ///
324    /// Unless you have specific needs, you should use [`RsaPrivateKey::new`] instead.
325    ///
326    /// #⚠️Warning: Hazmat!
327    /// This version does not apply minimum key size checks, and as such may generate keys
328    /// which are insecure!
329    #[cfg(feature = "hazmat")]
330    pub fn new_with_exp_unchecked<R: CryptoRng + ?Sized>(
331        rng: &mut R,
332        bit_size: usize,
333        exp: BoxedUint,
334    ) -> Result<RsaPrivateKey> {
335        let components = generate_multi_prime_key_with_exp(rng, 2, bit_size, exp)?;
336        RsaPrivateKey::from_components(
337            components.n.get(),
338            components.e,
339            components.d,
340            components.primes,
341        )
342    }
343
344    /// Private helper function that constructs an RSA key pair from components
345    /// WITHOUT performing any validation or precomputation.
346    ///
347    /// This is the shared implementation used by `from_components` and
348    /// `from_components_with_large_exponent`.
349    ///
350    /// Callers are responsible for:
351    /// 1. Validating the key (to ensure precomputation won't fail)
352    /// 2. Calling precompute() after validation
353    fn from_components_inner(
354        n: BoxedUint,
355        e: BoxedUint,
356        d: BoxedUint,
357        mut primes: Vec<BoxedUint>,
358    ) -> Result<RsaPrivateKey> {
359        let n = Odd::new(n).into_option().ok_or(Error::InvalidModulus)?;
360
361        // The modulus may come in padded with zeros, shorten it
362        // to ensure optimal performance of arithmetic operations.
363        let n_bits = n.bits_vartime();
364        let n = n.resize_unchecked(n_bits);
365
366        let n_params = BoxedMontyParams::new(n.clone());
367        let n_c = NonZero::new(n.get())
368            .into_option()
369            .ok_or(Error::InvalidModulus)?;
370
371        match primes.len() {
372            0 => {
373                // Recover `p` and `q` from `d`.
374                // See method in Appendix C.2: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Br2.pdf
375                let (p, q) = recover_primes(&n_c, &e, &d)?;
376                primes.push(p);
377                primes.push(q);
378            }
379            1 => return Err(Error::NprimesTooSmall),
380            _ => {
381                // Check that the product of primes matches the modulus.
382                // This also ensures that `bit_precision` of each prime is <= that of the modulus,
383                // and `bit_precision` of their product is >= that of the modulus.
384                if primes
385                    .iter()
386                    .fold(BoxedUint::one(), |acc, p| acc.concatenating_mul(&p))
387                    != n_c.as_ref()
388                {
389                    return Err(Error::InvalidModulus);
390                }
391            }
392        }
393
394        // The primes may come in padded with zeros too, so we need to shorten them as well.
395        let primes = primes
396            .into_iter()
397            .map(|p| {
398                let p_bits = p.bits();
399                p.resize_unchecked(p_bits)
400            })
401            .collect();
402
403        let k = RsaPrivateKey {
404            pubkey_components: RsaPublicKey {
405                n: n_c,
406                e,
407                n_params,
408            },
409            d,
410            primes,
411            precomputed: None,
412        };
413
414        Ok(k)
415    }
416
417    /// Constructs an RSA key pair from individual components, accepting exponents outside
418    /// the normal size bounds.
419    ///
420    /// See [`RsaPrivateKey::from_components`] for an explanation on the parameters.
421    ///
422    /// # ⚠️ Warning: Hazmat!
423    ///
424    /// This method accepts public exponents outside the standard bounds (2 ≤ e ≤ 2^33-1),
425    /// but still performs full cryptographic validation to ensure the key is mathematically
426    /// correct (i.e., verifies that de ≡ 1 mod λ(n)).
427    ///
428    /// **Note:** This method is dangerous as it can be used as a DOS vector if used with
429    /// untrusted input https://www.imperialviolet.org/2012/03/17/rsados.html
430    ///
431    /// This is intended for interoperating with systems that use non-standard exponents
432    /// or loading legacy keys. Use [`RsaPrivateKey::from_components`] for standard key
433    /// construction.
434    #[cfg(feature = "hazmat")]
435    pub fn from_components_with_large_exponent(
436        n: BoxedUint,
437        e: BoxedUint,
438        d: BoxedUint,
439        primes: Vec<BoxedUint>,
440    ) -> Result<RsaPrivateKey> {
441        let mut k = Self::from_components_inner(n, e, d, primes)?;
442
443        // Validate everything except exponent size bounds (to ensure precompute can't fail)
444        validate_skip_exponent_size(&k)?;
445
446        // Precompute when possible, ignore error otherwise.
447        k.precompute().ok();
448
449        Ok(k)
450    }
451
452    /// Constructs an RSA key pair from individual components:
453    ///
454    /// - `n`: RSA modulus
455    /// - `e`: public exponent (i.e. encrypting exponent)
456    /// - `d`: private exponent (i.e. decrypting exponent)
457    /// - `primes`: prime factors of `n`: typically two primes `p` and `q`. More than two primes can
458    ///   be provided for multiprime RSA, however this is generally not recommended. If no `primes`
459    ///   are provided, a prime factor recovery algorithm will be employed to attempt to recover the
460    ///   factors (as described in [NIST SP 800-56B Revision 2] Appendix C.2). This algorithm only
461    ///   works if there are just two prime factors `p` and `q` (as opposed to multiprime), and `e`
462    ///   is between 2^16 and 2^256.
463    ///
464    ///  [NIST SP 800-56B Revision 2]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Br2.pdf
465    pub fn from_components(
466        n: BoxedUint,
467        e: BoxedUint,
468        d: BoxedUint,
469        primes: Vec<BoxedUint>,
470    ) -> Result<RsaPrivateKey> {
471        let mut k = Self::from_components_inner(n, e, d, primes)?;
472
473        // Always validate the key, to ensure precompute can't fail
474        k.validate()?;
475
476        // Precompute when possible, ignore error otherwise.
477        k.precompute().ok();
478
479        Ok(k)
480    }
481
482    /// Constructs an RSA key pair from its two primes p and q.
483    ///
484    /// This will rebuild the private exponent and the modulus.
485    ///
486    /// Private exponent will be rebuilt using the method defined in
487    /// [NIST 800-56B Section 6.2.1](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Br2.pdf#page=47).
488    pub fn from_p_q(
489        p: BoxedUint,
490        q: BoxedUint,
491        public_exponent: BoxedUint,
492    ) -> Result<RsaPrivateKey> {
493        if p == q {
494            return Err(Error::InvalidPrime);
495        }
496
497        let d = compute_private_exponent_carmicheal(&p, &q, &public_exponent)?;
498        let primes = vec![p, q];
499        let n = compute_modulus(&primes);
500
501        Self::from_components(n.get(), public_exponent, d, primes)
502    }
503
504    /// Constructs an RSA key pair from its primes.
505    ///
506    /// This will rebuild the private exponent and the modulus.
507    pub fn from_primes(
508        primes: Vec<BoxedUint>,
509        public_exponent: BoxedUint,
510    ) -> Result<RsaPrivateKey> {
511        if primes.len() < 2 {
512            return Err(Error::NprimesTooSmall);
513        }
514
515        // Makes sure that the primes are pairwise unequal.
516        for (i, prime1) in primes.iter().enumerate() {
517            for prime2 in primes.iter().take(i) {
518                if prime1 == prime2 {
519                    return Err(Error::InvalidPrime);
520                }
521            }
522        }
523
524        let n = compute_modulus(&primes);
525        let d = compute_private_exponent_euler_totient(&primes, &public_exponent)?;
526
527        Self::from_components(n.get(), public_exponent, d, primes)
528    }
529
530    /// Get the public key from the private key.
531    ///
532    /// Specific alternative to [`AsRef::as_ref`].
533    pub fn as_public_key(&self) -> &RsaPublicKey {
534        &self.pubkey_components
535    }
536
537    /// Get the public key from the private key, cloning `n` and `e`.
538    ///
539    /// Generally this is not needed since `RsaPrivateKey` implements the `PublicKey` trait,
540    /// but it can occasionally be useful to discard the private information entirely.
541    pub fn to_public_key(&self) -> RsaPublicKey {
542        self.pubkey_components.clone()
543    }
544
545    /// Performs some calculations to speed up private key operations.
546    pub fn precompute(&mut self) -> Result<()> {
547        if self.precomputed.is_some() {
548            return Ok(());
549        }
550
551        let d = &self.d;
552        let p = self.primes[0].clone();
553        let q = self.primes[1].clone();
554
555        let p_odd = Odd::new(p.clone())
556            .into_option()
557            .ok_or(Error::InvalidPrime)?;
558        let p_params = BoxedMontyParams::new(p_odd);
559        let q_odd = Odd::new(q.clone())
560            .into_option()
561            .ok_or(Error::InvalidPrime)?;
562        let q_params = BoxedMontyParams::new(q_odd);
563
564        let x = NonZero::new(p.wrapping_sub(BoxedUint::one()))
565            .into_option()
566            .ok_or(Error::InvalidPrime)?;
567        let dp = d.rem_vartime(&x);
568
569        let x = NonZero::new(q.wrapping_sub(BoxedUint::one()))
570            .into_option()
571            .ok_or(Error::InvalidPrime)?;
572        let dq = d.rem_vartime(&x);
573
574        // Note that since `p` and `q` may have different `bits_precision`,
575        // so we have to equalize them to calculate the remainder.
576        let q_mod_p = match p.bits_precision().cmp(&q.bits_precision()) {
577            Ordering::Less => (&q
578                % NonZero::new(p.clone())
579                    .expect("`p` is non-zero")
580                    .resize_unchecked(q.bits_precision()))
581            .resize_unchecked(p.bits_precision()),
582            Ordering::Greater => {
583                (&q).resize_unchecked(p.bits_precision())
584                    % &NonZero::new(p.clone()).expect("`p` is non-zero")
585            }
586            Ordering::Equal => &q % NonZero::new(p.clone()).expect("`p` is non-zero"),
587        };
588
589        let q_mod_p = BoxedMontyForm::new(q_mod_p, &p_params);
590        let qinv = q_mod_p.invert().into_option().ok_or(Error::InvalidPrime)?;
591
592        debug_assert_eq!(dp.bits_precision(), p.bits_precision());
593        debug_assert_eq!(dq.bits_precision(), q.bits_precision());
594        debug_assert_eq!(qinv.bits_precision(), p.bits_precision());
595        debug_assert_eq!(p_params.bits_precision(), p.bits_precision());
596        debug_assert_eq!(q_params.bits_precision(), q.bits_precision());
597
598        self.precomputed = Some(PrecomputedValues {
599            dp,
600            dq,
601            qinv,
602            p_params,
603            q_params,
604        });
605
606        Ok(())
607    }
608
609    /// Clears precomputed values by setting to None
610    pub fn clear_precomputed(&mut self) {
611        self.precomputed = None;
612    }
613
614    /// Compute CRT coefficient: `(1/q) mod p`.
615    pub fn crt_coefficient(&self) -> Option<BoxedUint> {
616        let p = &self.primes[0];
617        let q = &self.primes[1];
618        // TODO: maybe store primes as `NonZero`?
619        Option::from(q.invert_mod(&NonZero::new(p.clone()).expect("prime")))
620    }
621
622    /// Performs basic sanity checks on the key.
623    /// Returns `Ok(())` if everything is good, otherwise an appropriate error.
624    pub fn validate(&self) -> Result<()> {
625        check_public(self)?;
626        validate_private_key_parts(self)?;
627        Ok(())
628    }
629
630    /// Decrypt the given message.
631    pub fn decrypt<P: PaddingScheme>(&self, padding: P, ciphertext: &[u8]) -> Result<Vec<u8>> {
632        padding.decrypt(Option::<&mut DummyRng>::None, self, ciphertext)
633    }
634
635    /// Decrypt the given message.
636    ///
637    /// Uses `rng` to blind the decryption process.
638    pub fn decrypt_blinded<R: CryptoRng + ?Sized, P: PaddingScheme>(
639        &self,
640        rng: &mut R,
641        padding: P,
642        ciphertext: &[u8],
643    ) -> Result<Vec<u8>> {
644        padding.decrypt(Some(rng), self, ciphertext)
645    }
646
647    /// Sign the given digest.
648    pub fn sign<S: SignatureScheme>(&self, padding: S, digest_in: &[u8]) -> Result<Vec<u8>> {
649        padding.sign(Option::<&mut DummyRng>::None, self, digest_in)
650    }
651
652    /// Sign the given digest using the provided `rng`, which is used in the
653    /// following ways depending on the [`SignatureScheme`]:
654    ///
655    /// - [`Pkcs1v15Sign`][`crate::Pkcs1v15Sign`] padding: uses the RNG
656    ///   to mask the private key operation with random blinding, which helps
657    ///   mitigate sidechannel attacks.
658    /// - [`Pss`][`crate::Pss`] always requires randomness. Use
659    ///   [`Pss::new`][`crate::Pss::new`] for a standard RSASSA-PSS signature, or
660    ///   [`Pss::new_blinded`][`crate::Pss::new_blinded`] for RSA-BSSA blind
661    ///   signatures.
662    pub fn sign_with_rng<R: CryptoRng + ?Sized, S: SignatureScheme>(
663        &self,
664        rng: &mut R,
665        padding: S,
666        digest_in: &[u8],
667    ) -> Result<Vec<u8>> {
668        padding.sign(Some(rng), self, digest_in)
669    }
670}
671
672impl PrivateKeyParts for RsaPrivateKey {
673    fn d(&self) -> &BoxedUint {
674        &self.d
675    }
676
677    fn primes(&self) -> &[BoxedUint] {
678        &self.primes
679    }
680
681    fn dp(&self) -> Option<&BoxedUint> {
682        self.precomputed.as_ref().map(|p| &p.dp)
683    }
684
685    fn dq(&self) -> Option<&BoxedUint> {
686        self.precomputed.as_ref().map(|p| &p.dq)
687    }
688
689    fn qinv(&self) -> Option<&BoxedMontyForm> {
690        self.precomputed.as_ref().map(|p| &p.qinv)
691    }
692
693    fn crt_values(&self) -> Option<&[CrtValue]> {
694        None
695    }
696
697    fn p_params(&self) -> Option<&BoxedMontyParams> {
698        self.precomputed.as_ref().map(|p| &p.p_params)
699    }
700
701    fn q_params(&self) -> Option<&BoxedMontyParams> {
702        self.precomputed.as_ref().map(|p| &p.q_params)
703    }
704}
705
706/// Check that the public key is well formed and has an exponent within acceptable bounds.
707#[inline]
708pub fn check_public(public_key: &impl PublicKeyParts) -> Result<()> {
709    check_public_with_max_size(public_key.n(), public_key.e(), None)
710}
711
712/// Check that the public key is well formed and has an exponent within acceptable bounds.
713#[inline]
714fn check_public_with_max_size(n: &BoxedUint, e: &BoxedUint, max_size: Option<usize>) -> Result<()> {
715    if let Some(max_size) = max_size {
716        if n.bits_vartime() as usize > max_size {
717            return Err(Error::ModulusTooLarge);
718        }
719    }
720
721    check_public_skip_exponent_size(n, e)?;
722
723    if e < &BoxedUint::from(RsaPublicKey::MIN_PUB_EXPONENT) {
724        return Err(Error::PublicExponentTooSmall);
725    }
726
727    if e > &BoxedUint::from(RsaPublicKey::MAX_PUB_EXPONENT) {
728        return Err(Error::PublicExponentTooLarge);
729    }
730
731    Ok(())
732}
733
734/// Check that the public key is well formed, skipping exponent size bounds checks.
735///
736/// This is used internally by both public validation functions and hazmat APIs.
737#[inline]
738fn check_public_skip_exponent_size(n: &BoxedUint, e: &BoxedUint) -> Result<()> {
739    if e >= n || n.is_even().into() || n.is_zero().into() {
740        return Err(Error::InvalidModulus);
741    }
742
743    if e.is_even().into() {
744        return Err(Error::InvalidExponent);
745    }
746
747    // Skip exponent size bounds checks
748    Ok(())
749}
750
751/// Helper function that validates the private key structure and cryptographic correctness.
752///
753/// This performs the structural and mathematical validation checks that are common to both
754/// `validate()` and `validate_skip_exponent_size()`.
755fn validate_private_key_parts(key: &RsaPrivateKey) -> Result<()> {
756    // Check that Πprimes == n.
757    let mut m = BoxedUint::one_with_precision(key.pubkey_components.n.bits_precision());
758    let one = BoxedUint::one();
759    for prime in &key.primes {
760        // Any primes ≤ 1 will cause divide-by-zero panics later.
761        if prime <= &one {
762            return Err(Error::InvalidPrime);
763        }
764        m = m.wrapping_mul(prime);
765    }
766    if m != *key.pubkey_components.n {
767        return Err(Error::InvalidModulus);
768    }
769
770    // Check that de ≡ 1 mod p-1, for each prime.
771    // This implies that e is coprime to each p-1 as e has a multiplicative
772    // inverse. Therefore e is coprime to lcm(p-1,q-1,r-1,...) =
773    // exponent(ℤ/nℤ). It also implies that a^de ≡ a mod p as a^(p-1) ≡ 1
774    // mod p. Thus a^de ≡ a mod n for all a coprime to n, as required.
775    let de = key.d.concatenating_mul(&key.pubkey_components.e);
776
777    for prime in &key.primes {
778        let x = NonZero::new(prime.wrapping_sub(BoxedUint::one())).unwrap();
779        let congruence = de.rem_vartime(&x);
780        if !bool::from(congruence.is_one()) {
781            return Err(Error::InvalidExponent);
782        }
783    }
784
785    Ok(())
786}
787
788/// Validate the private key structure and cryptographic correctness,
789/// skipping only the exponent size bounds checks.
790///
791/// This performs all the same checks as `RsaPrivateKey::validate()` except
792/// it doesn't verify that the exponent is within the standard bounds.
793#[cfg(feature = "hazmat")]
794fn validate_skip_exponent_size(key: &RsaPrivateKey) -> Result<()> {
795    // Check public key properties (without exponent size checks)
796    check_public_skip_exponent_size(key.pubkey_components.n.as_ref(), &key.pubkey_components.e)?;
797
798    // Perform common private key validation
799    validate_private_key_parts(key)?;
800
801    Ok(())
802}
803
804#[cfg(feature = "serde")]
805impl Serialize for RsaPublicKey {
806    fn serialize<S>(&self, serializer: S) -> core::prelude::v1::Result<S::Ok, S::Error>
807    where
808        S: serdect::serde::Serializer,
809    {
810        let der = self.to_public_key_der().map_err(ser::Error::custom)?;
811        serdect::slice::serialize_hex_lower_or_bin(&der, serializer)
812    }
813}
814
815#[cfg(feature = "serde")]
816impl<'de> Deserialize<'de> for RsaPublicKey {
817    fn deserialize<D>(deserializer: D) -> core::prelude::v1::Result<Self, D::Error>
818    where
819        D: serdect::serde::Deserializer<'de>,
820    {
821        let der_bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
822        Self::from_public_key_der(&der_bytes).map_err(de::Error::custom)
823    }
824}
825
826#[cfg(feature = "serde")]
827impl Serialize for RsaPrivateKey {
828    fn serialize<S>(&self, serializer: S) -> core::prelude::v1::Result<S::Ok, S::Error>
829    where
830        S: ser::Serializer,
831    {
832        let der = self.to_pkcs8_der().map_err(ser::Error::custom)?;
833        serdect::slice::serialize_hex_lower_or_bin(&der.as_bytes(), serializer)
834    }
835}
836
837#[cfg(feature = "serde")]
838impl<'de> Deserialize<'de> for RsaPrivateKey {
839    fn deserialize<D>(deserializer: D) -> core::prelude::v1::Result<Self, D::Error>
840    where
841        D: de::Deserializer<'de>,
842    {
843        let der_bytes = serdect::slice::deserialize_hex_or_bin_vec(deserializer)?;
844        Self::from_pkcs8_der(&der_bytes).map_err(de::Error::custom)
845    }
846}
847
848#[cfg(test)]
849mod tests {
850    use super::*;
851    use crate::algorithms::rsa::{rsa_decrypt_and_check, rsa_encrypt};
852    use crate::traits::{PrivateKeyParts, PublicKeyParts};
853
854    use hex_literal::hex;
855    use rand::rngs::ChaCha8Rng;
856    use rand_core::SeedableRng;
857
858    #[cfg(feature = "encoding")]
859    use pkcs8::DecodePrivateKey;
860
861    #[test]
862    fn test_from_into() {
863        let raw_n = BoxedUint::from(101u64);
864        let n_odd = Odd::new(raw_n.clone()).unwrap();
865        let private_key = RsaPrivateKey {
866            pubkey_components: RsaPublicKey {
867                n: NonZero::new(raw_n.clone()).unwrap(),
868                e: BoxedUint::from(200u64),
869                n_params: BoxedMontyParams::new(n_odd),
870            },
871            d: BoxedUint::from(123u64),
872            primes: vec![],
873            precomputed: None,
874        };
875        let public_key: RsaPublicKey = private_key.into();
876
877        let n_limbs: &[u64] = PublicKeyParts::n(&public_key).as_ref().as_ref();
878        assert_eq!(n_limbs, &[101u64]);
879        assert_eq!(PublicKeyParts::e(&public_key), &BoxedUint::from(200u64));
880        assert_eq!(PublicKeyParts::e_bytes(&public_key), [200].into());
881        assert_eq!(PublicKeyParts::n_bytes(&public_key), [101].into());
882    }
883
884    fn test_key_basics(private_key: &RsaPrivateKey) {
885        private_key.validate().expect("invalid private key");
886
887        assert!(
888            PrivateKeyParts::d(private_key) < PublicKeyParts::n(private_key).as_ref(),
889            "private exponent too large"
890        );
891
892        let pub_key: RsaPublicKey = private_key.clone().into();
893        let m = BoxedUint::from(42u64);
894        let c = rsa_encrypt(&pub_key, &m).expect("encryption successful");
895
896        let m2 = rsa_decrypt_and_check::<ChaCha8Rng>(private_key, None, &c)
897            .expect("unable to decrypt without blinding");
898        assert_eq!(m, m2);
899        let mut rng = ChaCha8Rng::from_seed([42; 32]);
900        let m3 = rsa_decrypt_and_check(private_key, Some(&mut rng), &c)
901            .expect("unable to decrypt with blinding");
902        assert_eq!(m, m3);
903    }
904
905    macro_rules! key_generation {
906        ($name:ident, $multi:expr, $size:expr) => {
907            #[test]
908            fn $name() {
909                let mut rng = ChaCha8Rng::from_seed([42; 32]);
910                let exp = BoxedUint::from(RsaPrivateKey::EXP);
911
912                for _ in 0..10 {
913                    let components =
914                        generate_multi_prime_key_with_exp(&mut rng, $multi, $size, exp.clone())
915                            .unwrap();
916                    let private_key = RsaPrivateKey::from_components(
917                        components.n.get(),
918                        components.e,
919                        components.d,
920                        components.primes,
921                    )
922                    .unwrap();
923                    assert_eq!(PublicKeyParts::n(&private_key).bits(), $size);
924
925                    test_key_basics(&private_key);
926                }
927            }
928        };
929    }
930
931    key_generation!(key_generation_128, 2, 128);
932    key_generation!(key_generation_1024, 2, 1024);
933
934    key_generation!(key_generation_multi_3_256, 3, 256);
935
936    key_generation!(key_generation_multi_4_64, 4, 64);
937
938    key_generation!(key_generation_multi_5_64, 5, 64);
939    key_generation!(key_generation_multi_8_576, 8, 576);
940    key_generation!(key_generation_multi_16_1024, 16, 1024);
941
942    #[test]
943    fn test_negative_decryption_value() {
944        let bits = 128;
945        let private_key = RsaPrivateKey::from_components(
946            BoxedUint::from_le_slice(
947                &[
948                    99, 192, 208, 179, 0, 220, 7, 29, 49, 151, 75, 107, 75, 73, 200, 180,
949                ],
950                bits,
951            )
952            .unwrap(),
953            BoxedUint::from_le_slice(&[1, 0, 1, 0, 0, 0, 0, 0], 64).unwrap(),
954            BoxedUint::from_le_slice(
955                &[
956                    81, 163, 254, 144, 171, 159, 144, 42, 244, 133, 51, 249, 28, 12, 63, 65,
957                ],
958                bits,
959            )
960            .unwrap(),
961            vec![
962                BoxedUint::from_le_slice(&[105, 101, 60, 173, 19, 153, 3, 192], bits / 2).unwrap(),
963                BoxedUint::from_le_slice(&[235, 65, 160, 134, 32, 136, 6, 241], bits / 2).unwrap(),
964            ],
965        )
966        .unwrap();
967
968        for _ in 0..1000 {
969            test_key_basics(&private_key);
970        }
971    }
972
973    #[test]
974    #[cfg(all(feature = "hazmat", feature = "serde"))]
975    fn test_serde() {
976        use rand::rngs::ChaCha8Rng;
977        use rand_core::SeedableRng;
978        use serde_test::{assert_tokens, Configure, Token};
979
980        let mut rng = ChaCha8Rng::from_seed([42; 32]);
981        let priv_key = RsaPrivateKey::new_unchecked(&mut rng, 64).expect("failed to generate key");
982
983        let priv_tokens = [Token::Str(concat!(
984            "3056020100300d06092a864886f70d010101050004423040020100020900a",
985            "b240c3361d02e370203010001020811e54a15259d22f9020500ceff5cf302",
986            "0500d3a7aaad020500ccaddf17020500cb529d3d020500bb526d6f"
987        ))];
988        assert_tokens(&priv_key.clone().readable(), &priv_tokens);
989
990        let priv_tokens = [Token::Str(
991            "3024300d06092a864886f70d01010105000313003010020900ab240c3361d02e370203010001",
992        )];
993        assert_tokens(
994            &RsaPublicKey::from(priv_key.clone()).readable(),
995            &priv_tokens,
996        );
997    }
998
999    #[test]
1000    fn invalid_coeff_private_key_regression() {
1001        use base64ct::{Base64, Encoding};
1002
1003        let n = Base64::decode_vec(
1004            "wC8GyQvTCZOK+iiBR5fGQCmzRCTWX9TQ3aRG5gGFk0wB6EFoLMAyEEqeG3gS8xhA\
1005             m2rSWYx9kKufvNat3iWlbSRVqkcbpVAYlj2vTrpqDpJl+6u+zxFYoUEBevlJJkAh\
1006             l8EuCccOA30fVpcfRvXPTtvRd3yFT9E9EwZljtgSI02w7gZwg7VIxaGeajh5Euz6\
1007             ZVQZ+qNRKgXrRC7gPRqVyI6Dt0Jc+Su5KBGNn0QcPDzOahWha1ieaeMkFisZ9mdp\
1008             sJoZ4tw5eicLaUomKzALHXQVt+/rcZSrCd6/7uUo11B/CYBM4UfSpwXaL88J9AE6\
1009             A5++no9hmJzaF2LLp+Qwx4yY3j9TDutxSAjsraxxJOGZ3XyA9nG++Ybt3cxZ5fP7\
1010             ROjxCfROBmVv5dYn0O9OBIqYeCH6QraNpZMadlLNIhyMv8Y+P3r5l/PaK4VJaEi5\
1011             pPosnEPawp0W0yZDzmjk2z1LthaRx0aZVrAjlH0Rb/6goLUQ9qu1xsDtQVVpN4A8\
1012             9ZUmtTWORnnJr0+595eHHxssd2gpzqf4bPjNITdAEuOCCtpvyi4ls23zwuzryUYj\
1013             cUOEnsXNQ+DrZpLKxdtsD/qNV/j1hfeyBoPllC3cV+6bcGOFcVGbjYqb+Kw1b0+j\
1014             L69RSKQqgmS+qYqr8c48nDRxyq3QXhR8qtzUwBFSLVk=",
1015        )
1016        .unwrap();
1017        let e = Base64::decode_vec("AQAB").unwrap();
1018        let d = Base64::decode_vec(
1019            "qQazSQ+FRN7nVK1bRsROMRB8AmsDwLVEHivlz1V3Td2Dr+oW3YUMgxedhztML1Id\
1020             QJPq/ad6qErJ6yRFNySVIjDaxzBTOEoB1eHa1btOnBJWb8rVvvjaorixvJ6Tn3i4\
1021             EuhsvVy9DoR1k4rGj3qSIiFjUVvLRDAbLyhpGgEfsr0Z577yJmTC5E8JLRMOKX8T\
1022             mxsk3jPVpsgd65Hu1s8S/ZmabwuHCf9SkdMeY/1bd/9i7BqqJeeDLE4B5x1xcC3z\
1023             3scqDUTzqGO+vZPhjgprPDRlBamVwgenhr7KwCn8iaLamFinRVwOAag8BeBqOJj7\
1024             lURiOsKQa9FIX1kdFUS1QMQxgtPycLjkbvCJjriqT7zWKsmJ7l8YLs6Wmm9/+QJR\
1025             wNCEVdMTXKfCP1cJjudaiskEQThfUldtgu8gUDNYbQ/Filb2eKfiX4h1TiMxZqUZ\
1026             HVZyb9nShbQoXJ3vj/MGVF0QM8TxhXM8r2Lv9gDYU5t9nQlUMLhs0jVjai48jHAB\
1027             bFNyH3sEcOmJOIwJrCXw1dzG7AotwyaEVUHOmL04TffmwCFfnyrLjbFgnyOeoyII\
1028             BYjcY7QFRm/9nupXMTH5hZ2qrHfCJIp0KK4tNBdQqmnHapFl5l6Le1s4qBS5bEIz\
1029             jitobLvAFm9abPlDGfxmY6mlrMK4+nytwF9Ct7wc1AE=",
1030        )
1031        .unwrap();
1032        let primes = [
1033            Base64::decode_vec(
1034                "9kQWEAzsbzOcdPa+s5wFfw4XDd7bB1q9foZ31b1+TNjGNxbSBCFlDF1q98vwpV6n\
1035                 M8bWDh/wtbNoETSQDgpEnYOQ26LWEw6YY1+q1Q2GGEFceYUf+Myk8/vTc8TN6Zw0\
1036                 bKZBWy10Qo8h7xk4JpzuI7NcxvjJYTkS9aErFxi3vVH0aiZC0tmfaCqr8a2rJxyV\
1037                 wqreRpOjwAWrotMsf2wGsF4ofx5ScoFy5GB5fJkkdOrW1LyTvZAUCX3cstPr19+T\
1038                 NC5zZOk7WzZatnCkN5H5WzalWtZuu0oVL205KPOa3R8V2yv5e6fm0v5fTmqSuvjm\
1039                 aMJLXCN4QJkmIzojO99ckQ==",
1040            )
1041            .unwrap(),
1042            Base64::decode_vec(
1043                "x8exdMjVA2CiI+Thx7loHtVcevoeE2sZ7btRVAvmBqo+lkHwxb7FHRnWvuj6eJSl\
1044                 D2f0T50EewIhhiW3R9BmktCk7hXjbSCnC1u9Oxc1IAUm/7azRqyfCMx43XhLxpD+\
1045                 xkBCpWkKDLxGczsRwTuaP3lKS3bSdBrNlGmdblubvVBIq4YZ2vXVlnYtza0cS+dg\
1046                 CK7BGTqUsrCUd/ZbIvwcwZkZtpkhj1KQfto9X/0OMurBzAqbkeq1cyRHXHkOfN/q\
1047                 bUIIRqr9Ii7Eswf9Vk8xp2O1Nt8nzcYS9PFD12M5eyaeFEkEYfpNMNGuTzp/31oq\
1048                 VjbpoCxS6vuWAZyADxhISQ==",
1049            )
1050            .unwrap(),
1051            Base64::decode_vec(
1052                "is7d0LY4HoXszlC2NO7gejkq7XqL4p1W6hZJPYTNx+r37t1CC2n3Vvzg6kNdpRix\
1053                 DhIpXVTLjN9O7UO/XuqSumYKJIKoP52eb4Tg+a3hw5Iz2Zsb5lUTNSLgkQSBPAf7\
1054                 1LHxbL82JL4g1nBUog8ae60BwnVArThKY4EwlJguGNw09BAU4lwf6csDl/nX2vfV\
1055                 wiAloYpeZkHL+L8m+bueGZM5KE2jEz+7ztZCI+T+E5i69rZEYDjx0lfLKlEhQlCW\
1056                 3HbCPELqXgNJJkRfi6MP9kXa9lSfnZmoT081RMvqonB/FUa4HOcKyCrw9XZEtnbN\
1057                 CIdbitfDVEX+pSSD7596wQ==",
1058            )
1059            .unwrap(),
1060            Base64::decode_vec(
1061                "GPs0injugfycacaeIP5jMa/WX55VEnKLDHom4k6WlfDF4L4gIGoJdekcPEUfxOI5\
1062                 faKvHyFwRP1wObkPoRBDM0qZxRfBl4zEtpvjHrd5MibSyJkM8+J0BIKk/nSjbRIG\
1063                 eb3hV5O56PvGB3S0dKhCUnuVObiC+ne7izplsD4OTG70l1Yud33UFntyoMxrxGYL\
1064                 USqhBMmZfHquJg4NOWOzKNY/K+EcHDLj1Kjvkcgv9Vf7ocsVxvpFdD9uGPceQ6kw\
1065                 RDdEl6mb+6FDgWuXVyqR9+904oanEIkbJ7vfkthagLbEf57dyG6nJlqh5FBZWxGI\
1066                 R72YGypPuAh7qnnqXXjY2Q==",
1067            )
1068            .unwrap(),
1069            Base64::decode_vec(
1070                "CUWC+hRWOT421kwRllgVjy6FYv6jQUcgDNHeAiYZnf5HjS9iK2ki7v8G5dL/0f+Y\
1071                 f+NhE/4q8w4m8go51hACrVpP1p8GJDjiT09+RsOzITsHwl+ceEKoe56ZW6iDHBLl\
1072                 rNw5/MtcYhKpjNU9KJ2udm5J/c9iislcjgckrZG2IB8ADgXHMEByZ5DgaMl4AKZ1\
1073                 Gx8/q6KftTvmOT5rNTMLi76VN5KWQcDWK/DqXiOiZHM7Nr4dX4me3XeRgABJyNR8\
1074                 Fqxj3N1+HrYLe/zs7LOaK0++F9Ul3tLelhrhsvLxei3oCZkF9A/foD3on3luYA+1\
1075                 cRcxWpSY3h2J4/22+yo4+Q==",
1076            )
1077            .unwrap(),
1078        ];
1079
1080        let e = BoxedUint::from_be_slice(&e, 64).unwrap();
1081
1082        let bits = 4096;
1083        let n = BoxedUint::from_be_slice(&n, bits).unwrap();
1084        let d = BoxedUint::from_be_slice(&d, bits).unwrap();
1085        let primes = primes
1086            .iter()
1087            .map(|p| BoxedUint::from_be_slice(p, bits / 2).unwrap())
1088            .collect();
1089        let res = RsaPrivateKey::from_components(n, e, d, primes);
1090        assert_eq!(res, Err(Error::InvalidModulus));
1091    }
1092
1093    #[test]
1094    fn reject_oversized_private_key() {
1095        // -----BEGIN PUBLIC KEY-----
1096        // MIIEKjANBgkqhkiG9w0BAQEFAAOCBBcAMIIEEgKCBAkAqQn6O7pd9ioQJEOwS2sh
1097        // nD2bM3+PaLovro+OKOE9t7jxrp+b9Xq81oeT6zN5u5yPewa+V08ZsAJQEbF9D5AM
1098        // UZkHZc/sW/XAItC8CojQhHoCQfjOXZpONmGsQxnSJNgwLV5TDVKUApbQIPzIm9yD
1099        // wOvl1yXIypaRINHzthz36ysHmaHlNVZZQ40BHVkOiUd+ws7W9U9vHN0QcaSHC8lH
1100        // UEqb/Iyb0FSmZs+qbm4NXyaI90oloAFftOnt8VFbHfT/TXS0VwMyescxFsuvcuTr
1101        // Xx8EYc9TuJThW22wBAFOK6SpftgtZ6i4WJqk0F8JrTwZ3TyhzKsPRwe8KeNmtmqY
1102        // oaGiPj9lUOc928QzOyTETVd8pV7UpnaOe9Q4WHL0QmnXn61pCu4qpoLuK8jB+IO7
1103        // xifRZHj3PMfsjJurZ4MF57LgpSrI60ekYNh1o6ViXODHQuzGxzTaF3n/7GITDBQX
1104        // DRTlGuQH77hykxFqPclRGI0wxECPKasxpzjhiaTua9eipKedXB+o5XFyosnDt/X4
1105        // Ygqxj/q2/18LPuQgFLqWRzsHd4TdVQyiq9xCmzIoGUjAPz1Q8cjIXRpUnp2rZQjE
1106        // SCLeTjewrGNbjSMDUhdF5M2OBRmn7Q8XHHCUxT9fY/BZeydeE54KvEdEkomxkbXo
1107        // hHKEmbWeEdhp78h04/xW364p1Nu9Y49w7gtO+9nmwKcpNJq32M6Qb0d2dQ3wJ0oI
1108        // I9ml+n/DTna+IIwwbI8UOFEI4KZQzZaqmNv3TzGmpnocHK7eMyEtAUeQZUIGrPmr
1109        // FQEmKZn9rkgr/2Hw8T20q7e0lE65Is69vTP2wXm17B5zKFYska41jJoZ6jIpbMOt
1110        // uVPZV3SoGYM39Z4Ax3JaGZE0L/dQ6lJJhdFUACFIQXwNWqzd7srnvbym4hLqoPuM
1111        // hjkUtTcv6YODEk7LB2FLDcymmH/zCL3w4VSi4+HyZZ13gM7Cz8WmkX4H+jeL0+Ja
1112        // QyG1CzqV/HA78vUpJv/bb/J1+X1i/1HltLeTjteY4rBhVT1cxBoVBGQaCwindAs+
1113        // Fjcp+8cAK+/3pMQghnkrGHzrx9YIYoOGXs4vQIMGngYaTa7aXAaft4fWjg4EeSjd
1114        // rZwqqrPNuUcEuneFP9RPffj8f3vkhqCFgoVBfVM7ontu2d2nRu/hhAkgT13Uc68J
1115        // dM2imBvHAo6DDUt6msWCAMOAEXYuO7aA+n3eettnBqtECoQAoCJdCHCebjIploMB
1116        // XMLXyseGtLK9arI48hDvcxSlf7/1lkBB6LgNQmQJ7925TDipiYQIZ63f4d5Z2JCp
1117        // W0vUkwzrH4iPb2hy+TBQSOw1kvjLyG/lHWjzDQa60xxVW9u59DxQueHsNEMHUORD
1118        // 1oFXvFLe/AllAgMBAAE=
1119        // -----END PUBLIC KEY-----
1120
1121        let n = BoxedUint::from_be_slice(
1122            &hex!(
1123                "a909fa3bba5df62a102443b04b6b219c3d9b337f8f68ba2fae8f8e28e13db7b8
1124                 f1ae9f9bf57abcd68793eb3379bb9c8f7b06be574f19b0025011b17d0f900c51
1125                 990765cfec5bf5c022d0bc0a88d0847a0241f8ce5d9a4e3661ac4319d224d830
1126                 2d5e530d52940296d020fcc89bdc83c0ebe5d725c8ca969120d1f3b61cf7eb2b
1127                 0799a1e5355659438d011d590e89477ec2ced6f54f6f1cdd1071a4870bc94750
1128                 4a9bfc8c9bd054a666cfaa6e6e0d5f2688f74a25a0015fb4e9edf1515b1df4ff
1129                 4d74b45703327ac73116cbaf72e4eb5f1f0461cf53b894e15b6db004014e2ba4
1130                 a97ed82d67a8b8589aa4d05f09ad3c19dd3ca1ccab0f4707bc29e366b66a98a1
1131                 a1a23e3f6550e73ddbc4333b24c44d577ca55ed4a6768e7bd4385872f44269d7
1132                 9fad690aee2aa682ee2bc8c1f883bbc627d16478f73cc7ec8c9bab678305e7b2
1133                 e0a52ac8eb47a460d875a3a5625ce0c742ecc6c734da1779ffec62130c14170d
1134                 14e51ae407efb87293116a3dc951188d30c4408f29ab31a738e189a4ee6bd7a2
1135                 a4a79d5c1fa8e57172a2c9c3b7f5f8620ab18ffab6ff5f0b3ee42014ba96473b
1136                 077784dd550ca2abdc429b32281948c03f3d50f1c8c85d1a549e9dab6508c448
1137                 22de4e37b0ac635b8d2303521745e4cd8e0519a7ed0f171c7094c53f5f63f059
1138                 7b275e139e0abc47449289b191b5e884728499b59e11d869efc874e3fc56dfae
1139                 29d4dbbd638f70ee0b4efbd9e6c0a729349ab7d8ce906f4776750df0274a0823
1140                 d9a5fa7fc34e76be208c306c8f14385108e0a650cd96aa98dbf74f31a6a67a1c
1141                 1caede33212d014790654206acf9ab1501262999fdae482bff61f0f13db4abb7
1142                 b4944eb922cebdbd33f6c179b5ec1e7328562c91ae358c9a19ea32296cc3adb9
1143                 53d95774a8198337f59e00c7725a1991342ff750ea524985d154002148417c0d
1144                 5aacddeecae7bdbca6e212eaa0fb8c863914b5372fe98383124ecb07614b0dcc
1145                 a6987ff308bdf0e154a2e3e1f2659d7780cec2cfc5a6917e07fa378bd3e25a43
1146                 21b50b3a95fc703bf2f52926ffdb6ff275f97d62ff51e5b4b7938ed798e2b061
1147                 553d5cc41a1504641a0b08a7740b3e163729fbc7002beff7a4c42086792b187c
1148                 ebc7d6086283865ece2f4083069e061a4daeda5c069fb787d68e0e047928ddad
1149                 9c2aaab3cdb94704ba77853fd44f7df8fc7f7be486a0858285417d533ba27b6e
1150                 d9dda746efe18409204f5dd473af0974cda2981bc7028e830d4b7a9ac58200c3
1151                 8011762e3bb680fa7dde7adb6706ab440a8400a0225d08709e6e32299683015c
1152                 c2d7cac786b4b2bd6ab238f210ef7314a57fbff5964041e8b80d426409efddb9
1153                 4c38a989840867addfe1de59d890a95b4bd4930ceb1f888f6f6872f9305048ec
1154                 3592f8cbc86fe51d68f30d06bad31c555bdbb9f43c50b9e1ec34430750e443d6
1155                 8157bc52defc0965"
1156            ),
1157            8256,
1158        )
1159        .unwrap();
1160
1161        let e = BoxedUint::from(65_537u64);
1162
1163        assert_eq!(
1164            RsaPublicKey::new(n, e).err().unwrap(),
1165            Error::ModulusTooLarge
1166        );
1167    }
1168
1169    #[test]
1170    #[cfg(feature = "encoding")]
1171    fn build_key_from_primes() {
1172        const RSA_2048_PRIV_DER: &[u8] = include_bytes!("../tests/examples/pkcs8/rsa2048-priv.der");
1173        let ref_key = RsaPrivateKey::from_pkcs8_der(RSA_2048_PRIV_DER).unwrap();
1174        assert_eq!(ref_key.validate(), Ok(()));
1175
1176        let primes = PrivateKeyParts::primes(&ref_key).to_vec();
1177
1178        let exp = PublicKeyParts::e(&ref_key);
1179        let key = RsaPrivateKey::from_primes(primes, exp.clone())
1180            .expect("failed to import key from primes");
1181        assert_eq!(key.validate(), Ok(()));
1182
1183        assert_eq!(PublicKeyParts::n(&key), PublicKeyParts::n(&ref_key));
1184
1185        assert_eq!(PrivateKeyParts::dp(&key), PrivateKeyParts::dp(&ref_key));
1186        assert_eq!(PrivateKeyParts::dq(&key), PrivateKeyParts::dq(&ref_key));
1187
1188        assert_eq!(PrivateKeyParts::d(&key), PrivateKeyParts::d(&ref_key));
1189    }
1190
1191    #[test]
1192    #[cfg(feature = "encoding")]
1193    fn build_key_from_p_q() {
1194        const RSA_2048_SP800_PRIV_DER: &[u8] =
1195            include_bytes!("../tests/examples/pkcs8/rsa2048-sp800-56b-priv.der");
1196        let ref_key = RsaPrivateKey::from_pkcs8_der(RSA_2048_SP800_PRIV_DER).unwrap();
1197        assert_eq!(ref_key.validate(), Ok(()));
1198
1199        let primes = PrivateKeyParts::primes(&ref_key).to_vec();
1200        let exp = PublicKeyParts::e(&ref_key);
1201
1202        let key = RsaPrivateKey::from_p_q(primes[0].clone(), primes[1].clone(), exp.clone())
1203            .expect("failed to import key from primes");
1204        assert_eq!(key.validate(), Ok(()));
1205
1206        assert_eq!(PublicKeyParts::n(&key), PublicKeyParts::n(&ref_key));
1207
1208        assert_eq!(PrivateKeyParts::dp(&key), PrivateKeyParts::dp(&ref_key));
1209        assert_eq!(PrivateKeyParts::dq(&key), PrivateKeyParts::dq(&ref_key));
1210
1211        assert_eq!(PrivateKeyParts::d(&key), PrivateKeyParts::d(&ref_key));
1212    }
1213
1214    #[test]
1215    #[cfg(feature = "hazmat")]
1216    fn test_from_components_with_large_exponent() {
1217        // Test that from_components_with_large_exponent accepts exponents outside normal bounds
1218        // while from_components would reject them
1219
1220        use rand::rngs::ChaCha8Rng;
1221        use rand_core::SeedableRng;
1222
1223        let mut rng = ChaCha8Rng::from_seed([42; 32]);
1224
1225        // Use an exponent larger than the normal maximum (2^33 - 1)
1226        let large_e = BoxedUint::from((1u64 << 34) + 1); // 2^34 + 1 (odd number)
1227
1228        // Generate a key with this large exponent
1229        let components =
1230            generate_multi_prime_key_with_exp(&mut rng, 2, 1024, large_e.clone()).unwrap();
1231
1232        // Extract components
1233        let n = components.n.get().clone();
1234        let d = components.d;
1235        let primes = components.primes;
1236
1237        // from_components should fail with PublicExponentTooLarge
1238        let result =
1239            RsaPrivateKey::from_components(n.clone(), large_e.clone(), d.clone(), primes.clone());
1240        assert!(result.is_err());
1241        assert_eq!(result.unwrap_err(), Error::PublicExponentTooLarge);
1242
1243        // from_components_with_large_exponent should succeed
1244        let key_with_large_exp = RsaPrivateKey::from_components_with_large_exponent(
1245            n.clone(),
1246            large_e.clone(),
1247            d.clone(),
1248            primes.clone(),
1249        );
1250        assert!(key_with_large_exp.is_ok());
1251
1252        let key_with_large_exp = key_with_large_exp.unwrap();
1253        assert_eq!(PublicKeyParts::e(&key_with_large_exp), &large_e);
1254        assert_eq!(PublicKeyParts::n(&key_with_large_exp).as_ref(), &n);
1255        assert_eq!(PrivateKeyParts::d(&key_with_large_exp), &d);
1256
1257        // Verify that the key is still cryptographically valid (de ≡ 1 mod λ(n))
1258        // by checking that validation with skip_exponent_size passes
1259        assert!(validate_skip_exponent_size(&key_with_large_exp).is_ok());
1260    }
1261
1262    #[test]
1263    #[cfg(feature = "hazmat")]
1264    fn test_from_components_with_small_exponent() {
1265        // Test that from_components_with_large_exponent accepts exponents below normal minimum
1266        // (despite the name, it works for any non-standard exponent size)
1267
1268        use rand::rngs::ChaCha8Rng;
1269        use rand_core::SeedableRng;
1270
1271        let mut rng = ChaCha8Rng::from_seed([43; 32]);
1272
1273        // Use an exponent smaller than the normal minimum (2)
1274        let small_e = BoxedUint::from(1u64); // This is odd, which is required
1275
1276        // Generate a key with this small exponent
1277        let components =
1278            generate_multi_prime_key_with_exp(&mut rng, 2, 1024, small_e.clone()).unwrap();
1279
1280        // Extract components
1281        let n = components.n.get().clone();
1282        let d = components.d;
1283        let primes = components.primes;
1284
1285        // from_components should fail
1286        let result =
1287            RsaPrivateKey::from_components(n.clone(), small_e.clone(), d.clone(), primes.clone());
1288        assert!(result.is_err());
1289
1290        // from_components_with_large_exponent should succeed
1291        let key_with_small_exp = RsaPrivateKey::from_components_with_large_exponent(
1292            n.clone(),
1293            small_e.clone(),
1294            d.clone(),
1295            primes,
1296        );
1297        assert!(key_with_small_exp.is_ok());
1298
1299        let key_with_small_exp = key_with_small_exp.unwrap();
1300        assert_eq!(PublicKeyParts::e(&key_with_small_exp), &small_e);
1301
1302        // Verify that the key is cryptographically valid
1303        assert!(validate_skip_exponent_size(&key_with_small_exp).is_ok());
1304    }
1305}