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(mls: &MlsContext, label: &str, epoch: u64) -> Result<[u8; 32], SFrameError> {
61 let context = epoch.to_be_bytes();
62 let raw = mls
63 .export_raw(label, &context, 32)
64 .map_err(|e| SFrameError::MlsExport(e.to_string()))?;
65 let mut out = [0u8; 32];
66 out.copy_from_slice(&raw);
67 Ok(out)
68}
69
70pub(crate) fn derive_participant(
76 base_key: &[u8; 32],
77 leaf_index: u32,
78 suite: CipherSuite,
79) -> ParticipantKeys {
80 let hk =
82 Hkdf::<Sha256>::from_prk(base_key).expect("base_key is exactly SHA-256 HashLen (32 bytes)");
83
84 let leaf_be = leaf_index.to_be_bytes();
85
86 let mut key_info = b"gbp sframe key ".to_vec();
87 key_info.extend_from_slice(&leaf_be);
88 let mut key = vec![0u8; suite.key_len()];
89 hk.expand(&key_info, &mut key)
90 .expect("key length is well within 255 * HashLen");
91
92 let mut salt_info = b"gbp sframe salt ".to_vec();
93 salt_info.extend_from_slice(&leaf_be);
94 let mut salt = [0u8; 12];
95 hk.expand(&salt_info, &mut salt)
96 .expect("salt length (12) is well within 255 * HashLen");
97
98 ParticipantKeys { key, salt }
99}
100
101pub(crate) fn make_nonce(salt: &[u8; 12], ctr: u64) -> [u8; 12] {
104 let mut nonce = *salt;
105 let ctr_le = ctr.to_le_bytes(); for i in 0..8 {
107 nonce[i] ^= ctr_le[i];
108 }
109 nonce
110}