File
Blob: firmware/crates/esp32-radio/src/platform/crypto/key.rs
| 1 | //! Keys remain Rust-owned; native operations borrow them only during the call. |
| 2 | use zeroize::Zeroizing; |
| 3 | |
| 4 | pub(super) struct Key { |
| 5 | bytes: Zeroizing<[u8; 32]>, |
| 6 | length: usize, |
| 7 | } |
| 8 | impl 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 | } |
| 24 | impl 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 | } |