wolf_crypto/aead/
tag.rs

1use core::fmt;
2
3/// Represents the authentication tag for AEADs
4#[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    /// The size of the authentication tag in bytes.
19    pub const CAPACITY: usize = 16;
20    #[cfg(feature = "allow-non-fips")]
21    pub(crate) const SIZE: u32 = 16;
22
23    /// Creates a new `Tag` instance from a 16-byte array.
24    ///
25    /// # Arguments
26    ///
27    /// * `inner` - A 16-byte array containing the authentication tag.
28    ///
29    /// # Returns
30    ///
31    /// A new `Tag` instance.
32    pub const fn new(inner: [u8; Self::CAPACITY]) -> Self {
33        Self { inner }
34    }
35
36    /// Creates a new `Tag` instance filled with zeros.
37    ///
38    /// This is typically used to create a tag buffer that will be filled
39    /// by an encryption operation.
40    ///
41    /// # Returns
42    ///
43    /// A new `Tag` instance with all bytes set to zero.
44    ///
45    /// # Example
46    ///
47    /// ```
48    /// use wolf_crypto::aead::Tag;
49    ///
50    /// let tag = Tag::new_zeroed();
51    /// assert_eq!(tag.as_slice(), &[0u8; 16]);
52    /// ```
53    pub const fn new_zeroed() -> Self {
54        Self::new([0u8; Self::CAPACITY])
55    }
56
57    /// Consumes the `Tag` and returns the underlying 16-byte array.
58    #[inline]
59    pub const fn take(self) -> [u8; Self::CAPACITY] {
60        self.inner
61    }
62
63    /// Returns a reference to the tag as a byte slice.
64    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    /// Constant Time Equivalence
81    #[inline]
82    fn eq(&self, other: &Self) -> bool {
83        // wolfcrypt (or just with my current config) does not expose the `misc` module
84        // utilities. This function is only ensuring both pointers are valid, and then uses the
85        // `misc` module's ConstantCompare on the two tags. So this will work for all tags, and
86        // allows us not to depend on a crate like subtle.
87        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    /// Constant Time Equivalence
94    #[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    /// Constant Time Equivalence (variable timing for length of `other`)
103    #[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    /// Constant Time Equivalence
113    #[inline]
114    fn eq(&self, other: &Tag) -> bool {
115        other.eq(self)
116    }
117}
118
119impl PartialEq<Tag> for [u8] {
120    /// Constant Time Equivalence (variable timing for length of `self`)
121    #[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    /// `PartialEq` for references of types which already can be compared to `Tag` in constant
129    /// time.
130    #[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    /// `PartialEq` for references of types which already can be compared to `Tag` in constant
138    /// time.
139    #[inline]
140    fn eq(&self, other: &&mut T) -> bool {
141        self.eq(*other)
142    }
143}