pub mod aead;
pub mod crc;
pub use aead::Aead;
pub use crc::{Crc8, Crc16, Crc32, Crc64};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IntegrityConfig {
Crc16(Crc16),
Crc32(Crc32),
Crc64(Crc64),
Aead(Aead),
}
impl IntegrityConfig {
pub const DEFAULT: Self = Self::Crc16(Crc16);
#[must_use]
pub const fn crc16() -> Self {
Self::Crc16(Crc16)
}
#[must_use]
pub const fn crc32() -> Self {
Self::Crc32(Crc32)
}
#[must_use]
pub const fn crc64() -> Self {
Self::Crc64(Crc64)
}
#[must_use]
pub const fn aead() -> Self {
Self::Aead(Aead::DEFAULT)
}
#[must_use]
pub const fn aead_with_key(key: [u8; Aead::KEY_LEN]) -> Self {
Self::Aead(Aead::new(key))
}
}
impl Default for IntegrityConfig {
fn default() -> Self {
Self::DEFAULT
}
}
impl Integrity for IntegrityConfig {
fn tag_len(&self) -> usize {
match self {
Self::Crc16(integrity) => integrity.tag_len(),
Self::Crc32(integrity) => integrity.tag_len(),
Self::Crc64(integrity) => integrity.tag_len(),
Self::Aead(integrity) => integrity.tag_len(),
}
}
fn seal(&self, bytes: &[u8], out: &mut [u8]) {
match self {
Self::Crc16(integrity) => integrity.seal(bytes, out),
Self::Crc32(integrity) => integrity.seal(bytes, out),
Self::Crc64(integrity) => integrity.seal(bytes, out),
Self::Aead(integrity) => integrity.seal(bytes, out),
}
}
fn verify(&self, bytes: &[u8], tag: &[u8]) -> bool {
match self {
Self::Crc16(integrity) => integrity.verify(bytes, tag),
Self::Crc32(integrity) => integrity.verify(bytes, tag),
Self::Crc64(integrity) => integrity.verify(bytes, tag),
Self::Aead(integrity) => integrity.verify(bytes, tag),
}
}
}
pub trait Integrity {
fn tag_len(&self) -> usize;
fn seal(&self, bytes: &[u8], out: &mut [u8]);
fn verify(&self, bytes: &[u8], tag: &[u8]) -> bool;
}