Skip to main content

rsa/traits/
keys.rs

1//! Traits related to the key components
2
3#[cfg(feature = "alloc")]
4use alloc::boxed::Box;
5#[cfg(feature = "private-key")]
6use crypto_bigint::{
7    modular::{BoxedMontyForm, BoxedMontyParams},
8    BoxedUint,
9};
10use zeroize::Zeroize;
11
12use crate::traits::{modular::ModulusParams, NonZero, UnsignedModularInt};
13
14/// Components of an RSA public key.
15pub trait PublicKeyParts<T: UnsignedModularInt> {
16    /// Montgomery parameter type matching this modulus type.
17    type MontyParams: ModulusParams<Modulus = T>;
18
19    /// Returns the modulus of the key.
20    fn n(&self) -> &NonZero<T>;
21
22    /// Returns the public exponent of the key.
23    fn e(&self) -> &T;
24
25    /// Returns the modulus size in bytes. Raw signatures and ciphertexts for
26    /// or by this public key will have the same size.
27    fn size(&self) -> usize {
28        (self.n().bits() as usize).div_ceil(8)
29    }
30
31    /// Returns the parameters for montgomery operations.
32    fn n_params(&self) -> &Self::MontyParams;
33
34    /// Returns precision (in bits) of `n`.
35    fn n_bits_precision(&self) -> u32 {
36        self.n().bits_precision()
37    }
38
39    /// Returns the big endian serialization of the modulus of the key
40    #[cfg(feature = "alloc")]
41    fn n_bytes(&self) -> Box<[u8]> {
42        self.n().to_be_bytes_trimmed_vartime()
43    }
44
45    /// Returns the big endian serialization of the public exponent of the key
46    #[cfg(feature = "alloc")]
47    fn e_bytes(&self) -> Box<[u8]> {
48        self.e().to_be_bytes_trimmed_vartime()
49    }
50}
51
52/// Components of an RSA private key.
53#[cfg(feature = "private-key")]
54pub trait PrivateKeyParts: PublicKeyParts<BoxedUint> {
55    /// Returns the private exponent of the key.
56    fn d(&self) -> &BoxedUint;
57
58    /// Returns the prime factors.
59    fn primes(&self) -> &[BoxedUint];
60
61    /// Returns the precomputed dp value, D mod (P-1)
62    fn dp(&self) -> Option<&BoxedUint>;
63
64    /// Returns the precomputed dq value, D mod (Q-1)
65    fn dq(&self) -> Option<&BoxedUint>;
66
67    /// Returns the precomputed qinv value, Q^-1 mod P
68    fn qinv(&self) -> Option<&BoxedMontyForm>;
69
70    /// Returns an iterator over the CRT Values
71    fn crt_values(&self) -> Option<&[CrtValue]>;
72
73    /// Returns the params for `p` if precomputed.
74    fn p_params(&self) -> Option<&BoxedMontyParams>;
75
76    /// Returns the params for `q` if precomputed.
77    fn q_params(&self) -> Option<&BoxedMontyParams>;
78}
79
80/// Contains the precomputed Chinese remainder theorem values.
81#[cfg(feature = "private-key")]
82#[derive(Debug, Clone)]
83pub struct CrtValue {
84    /// D mod (prime - 1)
85    pub(crate) exp: BoxedUint,
86    /// R·Coeff ≡ 1 mod Prime.
87    pub(crate) coeff: BoxedUint,
88    /// product of primes prior to this (inc p and q)
89    pub(crate) r: BoxedUint,
90}
91
92#[cfg(feature = "private-key")]
93impl Zeroize for CrtValue {
94    fn zeroize(&mut self) {
95        self.exp.zeroize();
96        self.coeff.zeroize();
97        self.r.zeroize();
98    }
99}
100
101#[cfg(feature = "private-key")]
102impl Drop for CrtValue {
103    fn drop(&mut self) {
104        self.zeroize();
105    }
106}