1use core::fmt;
2
3#[must_use = "You must use the tag, or GCM is doing nothing for you"]
5#[derive(Copy, Clone)]
6#[repr(transparent)]
7pub struct Tag {
8 inner: [u8; 16],
9}
10
11impl fmt::Debug for Tag {
12 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13 f.write_fmt(format_args!("Tag({:?})", &self.inner))
14 }
15}
16
17impl Tag {
18 pub const CAPACITY: usize = 16;
20 #[cfg(feature = "allow-non-fips")]
21 pub(crate) const SIZE: u32 = 16;
22
23 pub const fn new(inner: [u8; Self::CAPACITY]) -> Self {
33 Self { inner }
34 }
35
36 pub const fn new_zeroed() -> Self {
54 Self::new([0u8; Self::CAPACITY])
55 }
56
57 #[inline]
59 pub const fn take(self) -> [u8; Self::CAPACITY] {
60 self.inner
61 }
62
63 pub const fn as_slice(&self) -> &[u8] {
65 self.inner.as_slice()
66 }
67
68 #[inline]
69 pub(crate) fn as_mut_ptr(&mut self) -> *mut u8 {
70 self.inner.as_mut_ptr()
71 }
72
73 #[inline]
74 pub(crate) const fn as_ptr(&self) -> *const u8 {
75 self.inner.as_ptr()
76 }
77}
78
79impl PartialEq for Tag {
80 #[inline]
82 fn eq(&self, other: &Self) -> bool {
83 use wolf_crypto_sys::wc_ChaCha20Poly1305_CheckTag;
88 unsafe { wc_ChaCha20Poly1305_CheckTag(self.as_ptr(), other.as_ptr()) == 0 }
89 }
90}
91
92impl PartialEq<[u8; 16]> for Tag {
93 #[inline]
95 fn eq(&self, other: &[u8; 16]) -> bool {
96 use wolf_crypto_sys::wc_ChaCha20Poly1305_CheckTag;
97 unsafe { wc_ChaCha20Poly1305_CheckTag(self.as_ptr(), other.as_ptr()) == 0 }
98 }
99}
100
101impl PartialEq<[u8]> for Tag {
102 #[inline]
104 fn eq(&self, other: &[u8]) -> bool {
105 use wolf_crypto_sys::wc_ChaCha20Poly1305_CheckTag;
106 if other.len() != Self::CAPACITY { return false; }
107 unsafe { wc_ChaCha20Poly1305_CheckTag(self.as_ptr(), other.as_ptr()) == 0 }
108 }
109}
110
111impl PartialEq<Tag> for [u8; 16] {
112 #[inline]
114 fn eq(&self, other: &Tag) -> bool {
115 other.eq(self)
116 }
117}
118
119impl PartialEq<Tag> for [u8] {
120 #[inline]
122 fn eq(&self, other: &Tag) -> bool {
123 other.eq(self)
124 }
125}
126
127impl<T> PartialEq<&T> for Tag where Self: PartialEq<T>, T: ?Sized {
128 #[inline]
131 fn eq(&self, other: &&T) -> bool {
132 self.eq(*other)
133 }
134}
135
136impl<T> PartialEq<&mut T> for Tag where Self: PartialEq<T>, T: ?Sized {
137 #[inline]
140 fn eq(&self, other: &&mut T) -> bool {
141 self.eq(*other)
142 }
143}