bc_components/symmetric/
authentication_tag.rs

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