Skip to content
File

Blob: firmware/crates/esp32-radio/src/platform/crypto/key.rs

rust29 lines
1//! Keys remain Rust-owned; native operations borrow them only during the call.
2use zeroize::Zeroizing;
3 
4pub(super) struct Key {
5 bytes: Zeroizing<[u8; 32]>,
6 length: usize,
7}
8impl Key {
9 pub(super) fn new(key: &[u8]) -> Option<Self> {
10 if key.len() != 16 && key.len() != 32 {
11 return None;
12 }
13 let mut bytes = Zeroizing::new([0; 32]);
14 bytes[..key.len()].copy_from_slice(key);
15 Some(Self {
16 bytes,
17 length: key.len(),
18 })
19 }
20 pub(super) fn bytes(&self) -> &[u8] {
21 &self.bytes[..self.length]
22 }
23}
24impl std::fmt::Debug for Key {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 f.debug_struct("AesKey").finish_non_exhaustive()
27 }
28}