rsa/traits/keys.rs
1//! Traits related to the key components
2
3use crate::private_key::SecretPrime;
4use alloc::boxed::Box;
5use crypto_bigint::{
6 BoxedUint, NonZero, Odd,
7 modular::{BoxedMontyForm, BoxedMontyParams},
8};
9use zeroize::Zeroize;
10
11/// Components of an RSA public key.
12pub trait PublicKeyParts {
13 /// Returns the modulus of the key.
14 fn n(&self) -> &NonZero<BoxedUint>;
15
16 /// Returns the public exponent of the key.
17 fn e(&self) -> &BoxedUint;
18
19 /// Returns `k`, the modulus length in octets from RFC8017: `ceil(modBits / 8)`.
20 ///
21 /// Ciphertexts and signatures have this length, including any leading zero octets.
22 /// This may be smaller than the modulus's allocated storage width.
23 fn size(&self) -> usize {
24 (self.n().bit_width() as usize).div_ceil(8)
25 }
26
27 /// Returns the parameters for montgomery operations.
28 fn n_params(&self) -> &BoxedMontyParams;
29
30 /// Returns the limb-aligned storage precision of `n` in bits.
31 ///
32 /// This can exceed the modulus bit length (`modBits` in RFC8017). Use this precision for
33 /// integer arithmetic and decoding representatives, and [`Self::size`] for wire encodings.
34 fn n_bits_precision(&self) -> u32 {
35 self.n().bits_precision()
36 }
37
38 /// Returns the big endian serialization of the modulus of the key
39 fn n_bytes(&self) -> Box<[u8]> {
40 self.n().to_be_bytes_trimmed_vartime()
41 }
42
43 /// Returns the big endian serialization of the public exponent of the key
44 fn e_bytes(&self) -> Box<[u8]> {
45 self.e().to_be_bytes_trimmed_vartime()
46 }
47}
48
49/// Components of an RSA private key.
50///
51/// Implementations used for private operations must choose all storage precisions from public
52/// parameters, not from the significant bits of secret components. In particular, `d`, `dp`, and
53/// `dq` are processed for their full storage precision. Prime and Montgomery-parameter precisions
54/// must follow the same public policy. `RsaPrivateKey` enforces this during construction.
55pub trait PrivateKeyParts: PublicKeyParts {
56 /// Returns the private exponent of the key.
57 fn d(&self) -> &BoxedUint;
58
59 /// Returns the prime factors as [`Odd`].
60 ///
61 /// Blinded non-CRT private operations require all factors to compute φ(n).
62 fn primes(&self) -> &[Odd<BoxedUint>];
63
64 /// Returns prime `p` if precomputed.
65 fn p(&self) -> Option<&SecretPrime>;
66
67 /// Returns prime `q` if precomputed.
68 fn q(&self) -> Option<&SecretPrime>;
69
70 /// Returns the precomputed dp value, D mod (P-1)
71 fn dp(&self) -> Option<&BoxedUint>;
72
73 /// Returns the precomputed dq value, D mod (Q-1)
74 fn dq(&self) -> Option<&BoxedUint>;
75
76 /// Returns the precomputed qinv value, Q^-1 mod P
77 fn qinv(&self) -> Option<&BoxedMontyForm>;
78
79 /// Returns an iterator over the CRT Values
80 fn crt_values(&self) -> Option<&[CrtValue]>;
81}
82
83/// Contains the precomputed Chinese remainder theorem values.
84#[derive(Debug, Clone)]
85pub struct CrtValue {
86 /// D mod (prime - 1)
87 pub(crate) exp: BoxedUint,
88 /// R·Coeff ≡ 1 mod Prime.
89 pub(crate) coeff: BoxedUint,
90 /// product of primes prior to this (inc p and q)
91 pub(crate) r: BoxedUint,
92}
93
94impl Zeroize for CrtValue {
95 fn zeroize(&mut self) {
96 self.exp.zeroize();
97 self.coeff.zeroize();
98 self.r.zeroize();
99 }
100}
101
102impl Drop for CrtValue {
103 fn drop(&mut self) {
104 self.zeroize();
105 }
106}