use super::parser::EUI64;
use super::securityhelpers::generic_array::{typenum::U16, GenericArray};
macro_rules! lorawan_key {
(
$(#[$outer:meta])*
pub struct $type:ident(AES128);
) => {
$(#[$outer])*
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct $type(pub(crate) AES128);
impl From<[u8;16]> for $type {
fn from(key: [u8; 16]) -> Self {
$type(AES128(key))
}
}
impl $type {
pub fn inner(&self) -> &AES128 {
&self.0
}
}
impl AsRef<[u8]> for $type {
fn as_ref(&self) -> &[u8] {
&self.0 .0
}
}
};
}
lorawan_key!(
pub struct AppKey(AES128);
);
lorawan_key!(
pub struct NewSKey(AES128);
);
lorawan_key!(
pub struct AppSKey(AES128);
);
macro_rules! lorawan_eui {
(
$(#[$outer:meta])*
pub struct $type:ident(EUI64<[u8; 8]>);
) => {
$(#[$outer])*
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct $type(EUI64<[u8; 8]>);
impl From<[u8;8]> for $type {
fn from(key: [u8; 8]) -> Self {
$type(EUI64::from(key))
}
}
impl From<$type> for EUI64<[u8; 8]> {
fn from(key: $type) -> Self {
key.0
}
}
impl AsRef<[u8]> for $type {
fn as_ref(&self) -> &[u8] {
&self.0.as_ref()
}
}
};
}
lorawan_eui!(
pub struct DevEui(EUI64<[u8; 8]>);
);
lorawan_eui!(
pub struct AppEui(EUI64<[u8; 8]>);
);
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
pub struct AES128(pub [u8; 16]);
impl From<[u8; 16]> for AES128 {
fn from(v: [u8; 16]) -> Self {
AES128(v)
}
}
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
pub struct MIC(pub [u8; 4]);
impl From<[u8; 4]> for MIC {
fn from(v: [u8; 4]) -> Self {
MIC(v)
}
}
pub trait Encrypter {
fn encrypt_block(&self, block: &mut GenericArray<u8, U16>);
}
pub trait Decrypter {
fn decrypt_block(&self, block: &mut GenericArray<u8, U16>);
}
pub trait Mac {
fn input(&mut self, data: &[u8]);
fn reset(&mut self);
fn result(self) -> GenericArray<u8, U16>;
}
pub trait CryptoFactory {
type E: Encrypter;
type D: Decrypter;
type M: Mac;
fn new_enc(&self, key: &AES128) -> Self::E;
fn new_dec(&self, key: &AES128) -> Self::D;
fn new_mac(&self, key: &AES128) -> Self::M;
}