extern crate alloc;
use alloc::vec::Vec;
use cx448::{MontgomeryPoint, Scalar, WideScalarBytes, x448::x448};
use rand_core::{Rng, CryptoRng};
use zeroize::{Zeroize, Zeroizing};
use crate::crypto::dh::{DH, DHKeypair};
use crate::error::NoiseError;
pub struct X448Keys {
pub public: MontgomeryPoint,
private: Scalar,
}
impl Drop for X448Keys {
fn drop(&mut self) {
self.private.zeroize();
}
}
impl DHKeypair for X448Keys {
type PrivKey = Scalar;
type PubKey = MontgomeryPoint;
fn public(&self) -> Self::PubKey {
self.public
}
fn private(&self) -> &Self::PrivKey {
&self.private
}
fn pubkey_bytes(&self) -> Vec<u8> {
self.public.as_bytes().to_vec()
}
fn derive_keypair(sk: Self::PrivKey) -> Self {
let pk = x448(sk.to_bytes(), MontgomeryPoint::GENERATOR.0).expect("Couldn't derive the pk");
Self {
private: sk,
public: MontgomeryPoint(pk),
}
}
}
pub struct X448dh;
impl DH for X448dh {
const DHLEN: usize = 56;
const NAME: &'static str = "448";
type Keypair = X448Keys;
type PrivKey = Scalar;
type PubKey = MontgomeryPoint;
type SharedSecret = Zeroizing<[u8; Self::DHLEN]>;
fn privkey_from_bytes(bytes: &[u8]) -> Result<Self::PrivKey, NoiseError> {
let sk_bytes: [u8; Self::DHLEN] = bytes
.try_into()
.map_err(|_| NoiseError::ConversionError("private key has wrong length for X448"))?;
Ok(Scalar::from_bytes(&sk_bytes))
}
fn pubkey_from_bytes(bytes: &[u8]) -> Result<Self::PubKey, NoiseError> {
let arr: [u8; Self::DHLEN] = bytes
.try_into()
.map_err(|_| NoiseError::ConversionError("public key has wrong length for X448"))?;
Ok(MontgomeryPoint(arr))
}
fn pubkey_bytes(pk: &Self::PubKey) -> Vec<u8> {
pk.as_bytes().to_vec()
}
fn generate_keypair<R: Rng + CryptoRng>(rng: &mut R) -> Self::Keypair {
let mut wide_bytes = WideScalarBytes::default();
rng.fill_bytes(&mut wide_bytes);
let private = Scalar::from_bytes_mod_order_wide(&wide_bytes);
wide_bytes.zeroize();
let public = &private * &MontgomeryPoint::GENERATOR;
X448Keys { public, private }
}
fn dh(sk: &Self::PrivKey, pk: &Self::PubKey) -> Self::SharedSecret {
Zeroizing::new(
x448(sk.to_bytes(), pk.0)
.expect("Couldn't derive the pk")
)
}
}