2026-01-15 08:47:02 +08:00
|
|
|
use rand_core::OsRng;
|
|
|
|
|
use x25519_dalek::{PublicKey, StaticSecret};
|
2026-01-21 17:24:20 +08:00
|
|
|
use zeroize::{Zeroize, ZeroizeOnDrop};
|
2026-01-15 08:47:02 +08:00
|
|
|
|
|
|
|
|
use crate::types::SharedSecret;
|
|
|
|
|
|
2026-01-21 17:24:20 +08:00
|
|
|
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
|
2026-01-15 08:47:02 +08:00
|
|
|
pub struct InstallationKeyPair {
|
|
|
|
|
secret: StaticSecret,
|
|
|
|
|
public: PublicKey,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InstallationKeyPair {
|
|
|
|
|
pub fn generate() -> Self {
|
|
|
|
|
let secret = StaticSecret::random_from_rng(OsRng);
|
|
|
|
|
let public = PublicKey::from(&secret);
|
|
|
|
|
Self { secret, public }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn dh(&self, their_public: &PublicKey) -> SharedSecret {
|
|
|
|
|
self.secret.diffie_hellman(their_public).to_bytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn public(&self) -> &PublicKey {
|
|
|
|
|
&self.public
|
|
|
|
|
}
|
2026-01-28 14:54:16 +08:00
|
|
|
|
|
|
|
|
/// Export the secret key as raw bytes for storage.
|
|
|
|
|
pub fn secret_bytes(&self) -> [u8; 32] {
|
|
|
|
|
self.secret.to_bytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reconstruct from secret key bytes.
|
|
|
|
|
pub fn from_secret_bytes(bytes: [u8; 32]) -> Self {
|
|
|
|
|
let secret = StaticSecret::from(bytes);
|
|
|
|
|
let public = PublicKey::from(&secret);
|
|
|
|
|
Self { secret, public }
|
|
|
|
|
}
|
2026-01-15 08:47:02 +08:00
|
|
|
}
|