1use gbp_mls::MlsContext;
2use hkdf::Hkdf;
3use sha2::Sha256;
4
5use crate::error::SFrameError;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum CipherSuite {
13 Aes128Gcm,
15 Aes256Gcm,
17}
18
19impl CipherSuite {
20 pub(crate) fn key_len(self) -> usize {
22 match self {
23 Self::Aes128Gcm => 16,
24 Self::Aes256Gcm => 32,
25 }
26 }
27
28 pub fn from_u8(v: u8) -> Option<Self> {
30 match v {
31 0 => Some(Self::Aes128Gcm),
32 1 => Some(Self::Aes256Gcm),
33 _ => None,
34 }
35 }
36
37 pub fn as_u8(self) -> u8 {
39 match self {
40 Self::Aes128Gcm => 0,
41 Self::Aes256Gcm => 1,
42 }
43 }
44}
45
46pub(crate) struct ParticipantKeys {
48 pub key: Vec<u8>,
50 pub salt: [u8; 12],
52}
53
54pub fn derive_base_key(
61 mls: &MlsContext,
62 label: &str,
63 epoch: u64,
64) -> Result<[u8; 32], SFrameError> {
65 let context = epoch.to_be_bytes();
66 let raw = mls
67 .export_raw(label, &context, 32)
68 .map_err(|e| SFrameError::MlsExport(e.to_string()))?;
69 let mut out = [0u8; 32];
70 out.copy_from_slice(&raw);
71 Ok(out)
72}
73
74pub(crate) fn derive_participant(
80 base_key: &[u8; 32],
81 leaf_index: u32,
82 suite: CipherSuite,
83) -> ParticipantKeys {
84 let hk = Hkdf::<Sha256>::from_prk(base_key)
86 .expect("base_key is exactly SHA-256 HashLen (32 bytes)");
87
88 let leaf_be = leaf_index.to_be_bytes();
89
90 let mut key_info = b"gbp sframe key ".to_vec();
91 key_info.extend_from_slice(&leaf_be);
92 let mut key = vec![0u8; suite.key_len()];
93 hk.expand(&key_info, &mut key)
94 .expect("key length is well within 255 * HashLen");
95
96 let mut salt_info = b"gbp sframe salt ".to_vec();
97 salt_info.extend_from_slice(&leaf_be);
98 let mut salt = [0u8; 12];
99 hk.expand(&salt_info, &mut salt)
100 .expect("salt length (12) is well within 255 * HashLen");
101
102 ParticipantKeys { key, salt }
103}
104
105pub(crate) fn make_nonce(salt: &[u8; 12], ctr: u64) -> [u8; 12] {
108 let mut nonce = *salt;
109 let ctr_le = ctr.to_le_bytes(); for i in 0..8 {
111 nonce[i] ^= ctr_le[i];
112 }
113 nonce
114}