bc_components/symmetric/
authentication_tag.rs

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