enquo_core/key_provider/static.rs
1//! Derives keys from locally provided key material.
2//!
3
4use cretrit::kbkdf::{KBKDFInit, CMACAES256, KBKDF};
5
6use super::KeyProvider;
7use crate::Error;
8
9/// A straightforward means of generating keys from a locally provided key
10///
11/// Takes a 256 bit key as input, and uses a KBKDF to derive keys for any purpose to which you may
12/// wish to use them.
13///
14#[derive(Clone, Debug)]
15#[non_exhaustive]
16pub struct Static {
17 /// The KDF we're using
18 pub kdf: CMACAES256,
19}
20
21impl Static {
22 /// Create a new Static key provider
23 ///
24 /// # Errors
25 ///
26 /// Can return an error if the key-derivation function fails to initialise. Why this would
27 /// happen, though, is a bit of a mystery.
28 ///
29 pub fn new(key: &[u8; 32]) -> Result<Static, Error> {
30 Ok(Static {
31 kdf: *CMACAES256::new(key).map_err(|e| Error::KeyError(e.to_string()))?,
32 })
33 }
34}
35
36impl KeyProvider for Static {
37 fn derive_key(&self, subkey: &mut [u8], id: &[u8]) -> Result<(), Error> {
38 self.kdf
39 .derive_key(subkey, id)
40 .map_err(|e| Error::KeyError(e.to_string()))
41 }
42}