use crate::{
crypto::{self, SYMM_KEY_LEN_BYTES},
error::{Error, ErrorCode},
tlv::{FromTLV, TLVWriter, TagType, ToTLV},
};
type KeySetKey = [u8; SYMM_KEY_LEN_BYTES];
#[derive(Debug, Default, FromTLV, ToTLV)]
pub struct KeySet {
pub epoch_key: KeySetKey,
pub op_key: KeySetKey,
}
impl KeySet {
pub fn new(epoch_key: &[u8], compressed_id: &[u8]) -> Result<Self, Error> {
let mut ks = KeySet::default();
KeySet::op_key_from_ipk(epoch_key, compressed_id, &mut ks.op_key)?;
ks.epoch_key.copy_from_slice(epoch_key);
Ok(ks)
}
fn op_key_from_ipk(ipk: &[u8], compressed_id: &[u8], opkey: &mut [u8]) -> Result<(), Error> {
const GRP_KEY_INFO: [u8; 13] = [
0x47, 0x72, 0x6f, 0x75, 0x70, 0x4b, 0x65, 0x79, 0x20, 0x76, 0x31, 0x2e, 0x30,
];
crypto::hkdf_sha256(compressed_id, ipk, &GRP_KEY_INFO, opkey)
.map_err(|_| ErrorCode::NoSpace.into())
}
pub fn op_key(&self) -> &[u8] {
&self.op_key
}
pub fn epoch_key(&self) -> &[u8] {
&self.epoch_key
}
}