use core::fmt;
#[must_use = "You must use the tag, or GCM is doing nothing for you"]
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Tag {
inner: [u8; 16],
}
impl fmt::Debug for Tag {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!("Tag({:?})", &self.inner))
}
}
impl Tag {
pub const CAPACITY: usize = 16;
#[cfg(feature = "allow-non-fips")]
pub(crate) const SIZE: u32 = 16;
pub const fn new(inner: [u8; Self::CAPACITY]) -> Self {
Self { inner }
}
pub const fn new_zeroed() -> Self {
Self::new([0u8; Self::CAPACITY])
}
#[inline]
pub const fn take(self) -> [u8; Self::CAPACITY] {
self.inner
}
pub const fn as_slice(&self) -> &[u8] {
self.inner.as_slice()
}
#[inline]
pub(crate) fn as_mut_ptr(&mut self) -> *mut u8 {
self.inner.as_mut_ptr()
}
#[inline]
pub(crate) const fn as_ptr(&self) -> *const u8 {
self.inner.as_ptr()
}
}
impl PartialEq for Tag {
#[inline]
fn eq(&self, other: &Self) -> bool {
use wolf_crypto_sys::wc_ChaCha20Poly1305_CheckTag;
unsafe { wc_ChaCha20Poly1305_CheckTag(self.as_ptr(), other.as_ptr()) == 0 }
}
}
impl PartialEq<[u8; 16]> for Tag {
#[inline]
fn eq(&self, other: &[u8; 16]) -> bool {
use wolf_crypto_sys::wc_ChaCha20Poly1305_CheckTag;
unsafe { wc_ChaCha20Poly1305_CheckTag(self.as_ptr(), other.as_ptr()) == 0 }
}
}
impl PartialEq<[u8]> for Tag {
#[inline]
fn eq(&self, other: &[u8]) -> bool {
use wolf_crypto_sys::wc_ChaCha20Poly1305_CheckTag;
if other.len() != Self::CAPACITY { return false; }
unsafe { wc_ChaCha20Poly1305_CheckTag(self.as_ptr(), other.as_ptr()) == 0 }
}
}
impl PartialEq<Tag> for [u8; 16] {
#[inline]
fn eq(&self, other: &Tag) -> bool {
other.eq(self)
}
}
impl PartialEq<Tag> for [u8] {
#[inline]
fn eq(&self, other: &Tag) -> bool {
other.eq(self)
}
}
impl<T> PartialEq<&T> for Tag where Self: PartialEq<T>, T: ?Sized {
#[inline]
fn eq(&self, other: &&T) -> bool {
self.eq(*other)
}
}
impl<T> PartialEq<&mut T> for Tag where Self: PartialEq<T>, T: ?Sized {
#[inline]
fn eq(&self, other: &&mut T) -> bool {
self.eq(*other)
}
}