bc_components/symmetric/
authentication_tag.rs

1use std::rc::Rc;
2
3use anyhow::{Error, Result, bail};
4use dcbor::prelude::*;
5
6/// The authentication tag produced by the encryption process to verify message
7/// integrity.
8///
9/// An `AuthenticationTag` is a 16-byte value generated during ChaCha20-Poly1305
10/// authenticated encryption. It serves as a message authentication code (MAC)
11/// that verifies both the authenticity and integrity of the encrypted message.
12///
13/// During decryption, the tag is verified to ensure:
14/// - The message has not been tampered with (integrity)
15/// - The message was encrypted by someone who possesses the encryption key
16///   (authenticity)
17///
18/// This implementation follows the Poly1305 MAC algorithm as specified in
19/// [RFC-8439](https://datatracker.ietf.org/doc/html/rfc8439).
20#[derive(Clone, Eq, PartialEq)]
21pub struct AuthenticationTag([u8; Self::AUTHENTICATION_TAG_SIZE]);
22
23impl AuthenticationTag {
24    pub const AUTHENTICATION_TAG_SIZE: usize = 16;
25
26    /// Restore an `AuthenticationTag` from a fixed-size array of bytes.
27    pub const fn from_data(data: [u8; Self::AUTHENTICATION_TAG_SIZE]) -> Self {
28        Self(data)
29    }
30
31    /// Restore an `AuthenticationTag` from a reference to an array of bytes.
32    pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
33        let data = data.as_ref();
34        if data.len() != Self::AUTHENTICATION_TAG_SIZE {
35            bail!("Invalid authentication tag size");
36        }
37        let mut arr = [0u8; Self::AUTHENTICATION_TAG_SIZE];
38        arr.copy_from_slice(data.as_ref());
39        Ok(Self::from_data(arr))
40    }
41
42    /// Get a reference to the fixed-size array of bytes.
43    pub fn data(&self) -> &[u8; Self::AUTHENTICATION_TAG_SIZE] { self.into() }
44
45    /// Get the reference as a byte slice.
46    pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
47}
48
49impl AsRef<[u8]> for AuthenticationTag {
50    fn as_ref(&self) -> &[u8] { &self.0 }
51}
52
53/// Implements `AsRef<AuthenticationTag>` to allow self-reference.
54impl AsRef<AuthenticationTag> for AuthenticationTag {
55    fn as_ref(&self) -> &AuthenticationTag { self }
56}
57
58/// Implements Debug formatting to display the tag in hexadecimal format.
59impl std::fmt::Debug for AuthenticationTag {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        f.debug_tuple("AuthenticationTag")
62            .field(&hex::encode(self.data()))
63            .finish()
64    }
65}
66
67/// Implements conversion from a reference-counted AuthenticationTag to an owned
68/// AuthenticationTag.
69impl From<Rc<AuthenticationTag>> for AuthenticationTag {
70    fn from(value: Rc<AuthenticationTag>) -> Self { (*value).clone() }
71}
72
73/// Implements conversion from an AuthenticationTag reference to a byte array
74/// reference.
75impl<'a> From<&'a AuthenticationTag>
76    for &'a [u8; AuthenticationTag::AUTHENTICATION_TAG_SIZE]
77{
78    fn from(value: &'a AuthenticationTag) -> Self { &value.0 }
79}
80
81/// Implements conversion from a byte slice to an AuthenticationTag.
82impl From<&[u8]> for AuthenticationTag {
83    fn from(data: &[u8]) -> Self { Self::from_data_ref(data).unwrap() }
84}
85
86/// Implements conversion from a fixed-size byte array to an AuthenticationTag.
87impl From<[u8; Self::AUTHENTICATION_TAG_SIZE]> for AuthenticationTag {
88    fn from(data: [u8; Self::AUTHENTICATION_TAG_SIZE]) -> Self {
89        Self::from_data(data)
90    }
91}
92
93/// Implements conversion from a byte vector to an AuthenticationTag.
94impl From<Vec<u8>> for AuthenticationTag {
95    fn from(data: Vec<u8>) -> Self { Self::from_data_ref(data).unwrap() }
96}
97
98/// Implements conversion from AuthenticationTag to CBOR for serialization.
99impl From<AuthenticationTag> for CBOR {
100    fn from(value: AuthenticationTag) -> Self {
101        CBOR::to_byte_string(value.data())
102    }
103}
104
105/// Implements conversion from CBOR to AuthenticationTag for deserialization.
106impl TryFrom<CBOR> for AuthenticationTag {
107    type Error = Error;
108
109    fn try_from(cbor: CBOR) -> Result<Self, Self::Error> {
110        let data = CBOR::try_into_byte_string(cbor)?;
111        Self::from_data_ref(data)
112    }
113}