1use hkdf::Hkdf;
2use sha3::Sha3_384;
3use zeroize::Zeroize;
4use secrecy::{Secret, ExposeSecret};
5use rand::RngCore;
6
7pub struct ContentKeys {
8 pub aes_k1: Secret<[u8; 32]>,
9 #[allow(dead_code)]
10 pub chacha_k2: Secret<[u8; 32]>,
11}
12
13impl Drop for ContentKeys {
14 fn drop(&mut self) {
15 }
17}
18
19pub struct ContentEncryptionKey(Secret<[u8; 32]>);
20
21impl ContentEncryptionKey {
22 pub fn generate() -> anyhow::Result<Self> {
23 let mut cek = [0u8; 32];
24 rand::rngs::OsRng.fill_bytes(&mut cek);
25 Ok(ContentEncryptionKey(Secret::new(cek)))
26 }
27
28 pub fn expose_secret(&self) -> &[u8; 32] {
29 self.0.expose_secret()
30 }
31}
32
33impl Drop for ContentEncryptionKey {
34 fn drop(&mut self) {
35 }
37}
38
39pub fn hkdf_sha384_expand(ikm: &[u8], info: &[u8], len: usize) -> Vec<u8> {
40 let salt = b"qsfs/hkdf/v2";
42 let hk = Hkdf::<Sha3_384>::new(Some(salt), ikm);
43 let mut output = vec![0u8; len];
44 hk.expand(info, &mut output).unwrap();
45 output
46}
47
48pub fn hkdf_expand_keys(cek: &[u8; 32], confirm: Option<&[u8]>) -> ContentKeys {
49 let mut info_k1 = b"qsfs/stream/k1".to_vec();
50 let mut info_k2 = b"qsfs/stream/k2".to_vec();
51
52 if let Some(conf) = confirm {
53 info_k1.extend_from_slice(conf);
54 info_k2.extend_from_slice(conf);
55 }
56
57 let k1_bytes = hkdf_sha384_expand(cek, &info_k1, 32);
58 let k2_bytes = hkdf_sha384_expand(cek, &info_k2, 32);
59
60 let mut k1 = [0u8; 32];
61 let mut k2 = [0u8; 32];
62 k1.copy_from_slice(&k1_bytes);
63 k2.copy_from_slice(&k2_bytes);
64
65 ContentKeys {
66 aes_k1: Secret::new(k1),
67 chacha_k2: Secret::new(k2)
68 }
69}
70
71pub fn derive_file_nonce_seed(cek: &[u8; 32]) -> [u8; 8] {
72 let output = hkdf_sha384_expand(cek, b"qsfs/nonce-prefix", 8);
73 let mut seed = [0u8; 8];
74 seed.copy_from_slice(&output);
75 seed
76}
77
78pub fn derive_kek(mlkem_ss: &[u8], x25519_ss: &[u8], kdf_salt_opt: Option<&[u8]>) -> [u8; 32] {
80 let mut ikm = Vec::with_capacity(mlkem_ss.len() + x25519_ss.len());
82 ikm.extend_from_slice(mlkem_ss);
83 ikm.extend_from_slice(x25519_ss);
84
85 let salt = kdf_salt_opt.unwrap_or(b"qsfs/kdf/v2");
87 let hk = Hkdf::<Sha3_384>::new(Some(salt), &ikm);
88 let mut out = [0u8; 32];
89 hk.expand(b"qsfs/kek/v2", &mut out).expect("HKDF expand");
90 out
91}
92
93pub fn wrap_dek(kek: &[u8; 32], nonce: &[u8; 12], dek: &[u8; 32]) -> anyhow::Result<Vec<u8>> {
95 use aes_gcm::{Aes256Gcm, aead::{Aead, KeyInit}, Nonce};
96 let cipher = Aes256Gcm::new_from_slice(kek).unwrap();
97 let n = Nonce::from_slice(nonce);
98 let ct = cipher.encrypt(n, dek.as_slice())
99 .map_err(|_| anyhow::anyhow!("DEK wrap failed"))?;
100 Ok(ct)
101}
102
103pub fn unwrap_dek(kek: &[u8; 32], nonce: &[u8; 12], wrapped: &[u8]) -> anyhow::Result<[u8; 32]> {
105 use aes_gcm::{Aes256Gcm, aead::{Aead, KeyInit}, Nonce};
106 if wrapped.len() != 48 { return Err(anyhow::anyhow!("Invalid wrapped_dek length")); }
107 let cipher = Aes256Gcm::new_from_slice(kek).unwrap();
108 let n = Nonce::from_slice(nonce);
109 let pt = cipher.decrypt(n, wrapped)
110 .map_err(|_| anyhow::anyhow!("DEK unwrap failed"))?;
111 let mut dek = [0u8; 32];
112 dek.copy_from_slice(&pt);
113 Ok(dek)
114}
115
116#[allow(dead_code)]
118pub fn unwrap_cek_legacy(wrapped: &[u8], shared_secret: &[u8]) -> anyhow::Result<[u8; 32]> {
119 if wrapped.len() != 32 { return Err(anyhow::anyhow!("Invalid wrapped CEK length")); }
120 let keystream = hkdf_sha384_expand(shared_secret, b"qsfs/cek-wrap", 32);
121 let mut cek = [0u8; 32];
122 for (i, (w, k)) in wrapped.iter().zip(keystream.iter()).enumerate() {
123 cek[i] = w ^ k;
124 }
125 Ok(cek)
126}
127
128#[allow(dead_code)]
129pub fn zeroize_bytes(mut v: Vec<u8>) {
130 v.zeroize();
131}