1#[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
14pub trait PublicKeyParts<T: UnsignedModularInt> {
16 type MontyParams: ModulusParams<Modulus = T>;
18
19 fn n(&self) -> &NonZero<T>;
21
22 fn e(&self) -> &T;
24
25 fn size(&self) -> usize {
28 (self.n().bits() as usize).div_ceil(8)
29 }
30
31 fn n_params(&self) -> &Self::MontyParams;
33
34 fn n_bits_precision(&self) -> u32 {
36 self.n().bits_precision()
37 }
38
39 #[cfg(feature = "alloc")]
41 fn n_bytes(&self) -> Box<[u8]> {
42 self.n().to_be_bytes_trimmed_vartime()
43 }
44
45 #[cfg(feature = "alloc")]
47 fn e_bytes(&self) -> Box<[u8]> {
48 self.e().to_be_bytes_trimmed_vartime()
49 }
50}
51
52#[cfg(feature = "private-key")]
54pub trait PrivateKeyParts: PublicKeyParts<BoxedUint> {
55 fn d(&self) -> &BoxedUint;
57
58 fn primes(&self) -> &[BoxedUint];
60
61 fn dp(&self) -> Option<&BoxedUint>;
63
64 fn dq(&self) -> Option<&BoxedUint>;
66
67 fn qinv(&self) -> Option<&BoxedMontyForm>;
69
70 fn crt_values(&self) -> Option<&[CrtValue]>;
72
73 fn p_params(&self) -> Option<&BoxedMontyParams>;
75
76 fn q_params(&self) -> Option<&BoxedMontyParams>;
78}
79
80#[cfg(feature = "private-key")]
82#[derive(Debug, Clone)]
83pub struct CrtValue {
84 pub(crate) exp: BoxedUint,
86 pub(crate) coeff: BoxedUint,
88 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}