rsa/traits/keys.rs
1//! Traits related to the key components
2
3use num_bigint::{BigInt, BigUint};
4use zeroize::Zeroize;
5
6/// Components of an RSA public key.
7pub trait PublicKeyParts {
8 /// Returns the modulus of the key.
9 fn n(&self) -> &BigUint;
10
11 /// Returns the public exponent of the key.
12 fn e(&self) -> &BigUint;
13
14 /// Returns the modulus size in bytes. Raw signatures and ciphertexts for
15 /// or by this public key will have the same size.
16 fn size(&self) -> usize {
17 (self.n().bits() + 7) / 8
18 }
19}
20
21/// Components of an RSA private key.
22pub trait PrivateKeyParts: PublicKeyParts {
23 /// Returns the private exponent of the key.
24 fn d(&self) -> &BigUint;
25
26 /// Returns the prime factors.
27 fn primes(&self) -> &[BigUint];
28
29 /// Returns the precomputed dp value, D mod (P-1)
30 fn dp(&self) -> Option<&BigUint>;
31
32 /// Returns the precomputed dq value, D mod (Q-1)
33 fn dq(&self) -> Option<&BigUint>;
34
35 /// Returns the precomputed qinv value, Q^-1 mod P
36 fn qinv(&self) -> Option<&BigInt>;
37
38 /// Returns an iterator over the CRT Values
39 fn crt_values(&self) -> Option<&[CrtValue]>;
40}
41
42/// Contains the precomputed Chinese remainder theorem values.
43#[derive(Debug, Clone)]
44pub struct CrtValue {
45 /// D mod (prime - 1)
46 pub(crate) exp: BigInt,
47 /// R·Coeff ≡ 1 mod Prime.
48 pub(crate) coeff: BigInt,
49 /// product of primes prior to this (inc p and q)
50 pub(crate) r: BigInt,
51}
52
53impl Zeroize for CrtValue {
54 fn zeroize(&mut self) {
55 self.exp.zeroize();
56 self.coeff.zeroize();
57 self.r.zeroize();
58 }
59}
60
61impl Drop for CrtValue {
62 fn drop(&mut self) {
63 self.zeroize();
64 }
65}