use dig_session::UnlockedMasterSeed;
use x25519_dalek::{PublicKey, StaticSecret};
use crate::id::ProfileIx;
pub fn profile_sealing_secret(seed: &UnlockedMasterSeed, ix: ProfileIx) -> StaticSecret {
let ikm = seed.profile_derive_symmetric_key(ix.0, dig_constants::PROFILE_SEALING_X25519_LABEL);
StaticSecret::from(*ikm)
}
pub fn profile_sealing_public_key(seed: &UnlockedMasterSeed, ix: ProfileIx) -> [u8; 32] {
PublicKey::from(&profile_sealing_secret(seed, ix)).to_bytes()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::keys::dek::profile_dek;
use dig_keystore::{BackendKey, MemoryBackend};
use dig_session::{Password, Session, ENTROPY_LEN};
use std::sync::Arc;
const SEED: [u8; ENTROPY_LEN] = [0x42; ENTROPY_LEN];
const GOLDEN_SEALING_PK0: [u8; 32] =
hex32("93f1556d839a6bf56930b8a3f895ac95c34b289b3cbf55e47a78de06858bfb00");
const fn hex32(s: &str) -> [u8; 32] {
let bytes = s.as_bytes();
assert!(bytes.len() == 64, "hex length mismatch");
let mut out = [0u8; 32];
let mut i = 0;
while i < 32 {
out[i] = nibble(bytes[i * 2]) << 4 | nibble(bytes[i * 2 + 1]);
i += 1;
}
out
}
const fn nibble(c: u8) -> u8 {
match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 10,
_ => panic!("bad hex nibble"),
}
}
fn unlocked_seed() -> UnlockedMasterSeed {
Session::enroll_master_seed(
Arc::new(MemoryBackend::new()),
BackendKey::new("k".to_string()),
Password::new("pw"),
&SEED,
)
.unwrap()
}
#[test]
fn sealing_public_key_matches_the_pinned_golden_vector() {
let seed = unlocked_seed();
assert_eq!(
profile_sealing_public_key(&seed, ProfileIx::ROOT),
GOLDEN_SEALING_PK0,
"sealing key drifted from the frozen §5.1 contract — every sealed DIGCHAT1 message \
to this profile would become unopenable"
);
}
#[test]
fn derivation_is_deterministic() {
let seed = unlocked_seed();
assert_eq!(
profile_sealing_public_key(&seed, ProfileIx::ROOT),
profile_sealing_public_key(&seed, ProfileIx::ROOT),
"same seed + index must derive the same sealing key"
);
}
#[test]
fn distinct_indices_derive_distinct_sealing_keys() {
let seed = unlocked_seed();
let root = profile_sealing_public_key(&seed, ProfileIx::ROOT);
let one = profile_sealing_public_key(&seed, ProfileIx(1));
let four = profile_sealing_public_key(&seed, ProfileIx(4));
assert_ne!(root, one);
assert_ne!(root, four);
assert_ne!(one, four);
}
#[test]
fn public_key_corresponds_to_the_secret_key() {
let seed = unlocked_seed();
let secret = profile_sealing_secret(&seed, ProfileIx(3));
assert_eq!(
PublicKey::from(&secret).to_bytes(),
profile_sealing_public_key(&seed, ProfileIx(3)),
);
}
#[test]
fn sealing_ikm_is_domain_separated_from_the_dek() {
let seed = unlocked_seed();
let sealing_ikm =
*seed.profile_derive_symmetric_key(0, dig_constants::PROFILE_SEALING_X25519_LABEL);
let dek = profile_dek(&seed, ProfileIx::ROOT);
assert_ne!(
sealing_ikm, dek,
"sealing ikm must be domain-separated from the DEK (distinct HKDF info labels)"
);
}
}