1use anyhow::Result;
2
3use byteorder::{BigEndian, WriteBytesExt};
4
5pub struct Fabric {
6 pub id: u64,
7 pub ipk_epoch_key: Vec<u8>,
8 pub ca_id: u64,
9 ca_public_key: Vec<u8>,
10}
11
12impl Fabric {
13 pub fn new(fabric_id: u64, ca_id: u64, ca_public_key: &[u8]) -> Self {
14 Self {
15 id: fabric_id,
16 ipk_epoch_key: vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf],
17 ca_id,
18 ca_public_key: ca_public_key.to_owned(),
19 }
20 }
21
22 pub fn compressed(&self) -> Result<Vec<u8>> {
24 let mut buf_id = Vec::new();
25 buf_id.write_u64::<BigEndian>(self.id)?;
26 crate::util::cryptoutil::hkdf_sha256(
27 &buf_id,
28 &self.ca_public_key.as_slice()[1..],
29 "CompressedFabric".as_bytes(),
30 8,
31 )
32 }
33
34 pub fn signed_ipk(&self) -> Result<Vec<u8>> {
36 crate::util::cryptoutil::hkdf_sha256(
37 &self.compressed()?,
38 &self.ipk_epoch_key,
39 "GroupKey v1.0".as_bytes(),
40 16,
41 )
42 }
43}