1use alloc::boxed::Box;
4use crypto_bigint::{
5 modular::{BoxedMontyForm, BoxedMontyParams},
6 BoxedUint, NonZero,
7};
8use zeroize::Zeroize;
9
10pub trait PublicKeyParts {
12 fn n(&self) -> &NonZero<BoxedUint>;
14
15 fn e(&self) -> &BoxedUint;
17
18 fn size(&self) -> usize {
21 (self.n().bits() as usize).div_ceil(8)
22 }
23
24 fn n_params(&self) -> &BoxedMontyParams;
26
27 fn n_bits_precision(&self) -> u32 {
29 self.n().bits_precision()
30 }
31
32 fn n_bytes(&self) -> Box<[u8]> {
34 self.n().to_be_bytes_trimmed_vartime()
35 }
36
37 fn e_bytes(&self) -> Box<[u8]> {
39 self.e().to_be_bytes_trimmed_vartime()
40 }
41}
42
43pub trait PrivateKeyParts: PublicKeyParts {
45 fn d(&self) -> &BoxedUint;
47
48 fn primes(&self) -> &[BoxedUint];
50
51 fn dp(&self) -> Option<&BoxedUint>;
53
54 fn dq(&self) -> Option<&BoxedUint>;
56
57 fn qinv(&self) -> Option<&BoxedMontyForm>;
59
60 fn crt_values(&self) -> Option<&[CrtValue]>;
62
63 fn p_params(&self) -> Option<&BoxedMontyParams>;
65
66 fn q_params(&self) -> Option<&BoxedMontyParams>;
68}
69
70#[derive(Debug, Clone)]
72pub struct CrtValue {
73 pub(crate) exp: BoxedUint,
75 pub(crate) coeff: BoxedUint,
77 pub(crate) r: BoxedUint,
79}
80
81impl Zeroize for CrtValue {
82 fn zeroize(&mut self) {
83 self.exp.zeroize();
84 self.coeff.zeroize();
85 self.r.zeroize();
86 }
87}
88
89impl Drop for CrtValue {
90 fn drop(&mut self) {
91 self.zeroize();
92 }
93}