50 lines
1.3 KiB
Rust
Raw Normal View History

2026-01-15 08:47:02 +08:00
use rand_core::OsRng;
use x25519_dalek::{PublicKey, StaticSecret};
use zeroize::{Zeroize, ZeroizeOnDrop};
2026-01-15 08:47:02 +08:00
use crate::types::SharedSecret;
#[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
}
/// Export the secret key as raw bytes for serialization/storage.
pub fn secret_bytes(&self) -> &[u8; 32] {
self.secret.as_bytes()
}
/// Import the secret key from raw 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
}
impl From<StaticSecret> for InstallationKeyPair {
fn from(value: StaticSecret) -> Self {
let public = PublicKey::from(&value);
Self {
secret: value,
public,
}
}
}