pub mod crc;
pub mod sip;
pub use crc::{Crc16, Crc32, Crc64};
pub use sip::SipTag;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IntegrityConfig {
Crc16(Crc16),
Crc32(Crc32),
Crc64(Crc64),
SipTag(SipTag),
}
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 sip_tag() -> Self {
Self::SipTag(SipTag::DEFAULT)
}
#[must_use]
pub const fn sip_tag_with_key(key: [u8; SipTag::KEY_LEN]) -> Self {
Self::SipTag(SipTag::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::SipTag(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::SipTag(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::SipTag(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;
}