51 lines
1.3 KiB
Rust
Raw Normal View History

2026-02-12 16:02:49 -08:00
use crypto::X25519PublicKey;
2026-01-15 08:47:02 +08:00
use rand_core::OsRng;
2026-02-12 16:02:49 -08:00
use x25519_dalek::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,
2026-02-12 16:02:49 -08:00
public: X25519PublicKey,
2026-01-15 08:47:02 +08:00
}
impl InstallationKeyPair {
pub fn generate() -> Self {
let secret = StaticSecret::random_from_rng(OsRng);
2026-02-12 16:02:49 -08:00
let public = X25519PublicKey::from(&secret);
2026-01-15 08:47:02 +08:00
Self { secret, public }
}
2026-02-12 16:02:49 -08:00
pub fn dh(&self, their_public: &X25519PublicKey) -> SharedSecret {
2026-01-15 08:47:02 +08:00
self.secret.diffie_hellman(their_public).to_bytes()
}
2026-02-12 16:02:49 -08:00
pub fn public(&self) -> &X25519PublicKey {
2026-01-15 08:47:02 +08:00
&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);
2026-02-12 16:02:49 -08:00
let public = X25519PublicKey::from(&secret);
Self { secret, public }
}
2026-01-15 08:47:02 +08:00
}
impl From<StaticSecret> for InstallationKeyPair {
fn from(value: StaticSecret) -> Self {
2026-02-12 16:02:49 -08:00
let public = X25519PublicKey::from(&value);
Self {
secret: value,
public,
}
}
}