#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "private-key")]
use crypto_bigint::{
modular::{BoxedMontyForm, BoxedMontyParams},
BoxedUint,
};
use zeroize::Zeroize;
use crate::traits::{modular::ModulusParams, NonZero, UnsignedModularInt};
pub trait PublicKeyParts<T: UnsignedModularInt> {
type MontyParams: ModulusParams<Modulus = T>;
fn n(&self) -> &NonZero<T>;
fn e(&self) -> &T;
fn size(&self) -> usize {
(self.n().bits() as usize).div_ceil(8)
}
fn n_params(&self) -> &Self::MontyParams;
fn n_bits_precision(&self) -> u32 {
self.n().bits_precision()
}
#[cfg(feature = "alloc")]
fn n_bytes(&self) -> Box<[u8]> {
self.n().to_be_bytes_trimmed_vartime()
}
#[cfg(feature = "alloc")]
fn e_bytes(&self) -> Box<[u8]> {
self.e().to_be_bytes_trimmed_vartime()
}
}
#[cfg(feature = "private-key")]
pub trait PrivateKeyParts: PublicKeyParts<BoxedUint> {
fn d(&self) -> &BoxedUint;
fn primes(&self) -> &[BoxedUint];
fn dp(&self) -> Option<&BoxedUint>;
fn dq(&self) -> Option<&BoxedUint>;
fn qinv(&self) -> Option<&BoxedMontyForm>;
fn crt_values(&self) -> Option<&[CrtValue]>;
fn p_params(&self) -> Option<&BoxedMontyParams>;
fn q_params(&self) -> Option<&BoxedMontyParams>;
}
#[cfg(feature = "private-key")]
#[derive(Debug, Clone)]
pub struct CrtValue {
pub(crate) exp: BoxedUint,
pub(crate) coeff: BoxedUint,
pub(crate) r: BoxedUint,
}
#[cfg(feature = "private-key")]
impl Zeroize for CrtValue {
fn zeroize(&mut self) {
self.exp.zeroize();
self.coeff.zeroize();
self.r.zeroize();
}
}
#[cfg(feature = "private-key")]
impl Drop for CrtValue {
fn drop(&mut self) {
self.zeroize();
}
}