bc_components/symmetric/
authentication_tag.rs1use std::rc::Rc;
2
3use anyhow::{Error, Result, bail};
4use dcbor::prelude::*;
5
6#[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 pub const fn from_data(data: [u8; Self::AUTHENTICATION_TAG_SIZE]) -> Self {
28 Self(data)
29 }
30
31 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 pub fn data(&self) -> &[u8; Self::AUTHENTICATION_TAG_SIZE] { self.into() }
44
45 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
53impl AsRef<AuthenticationTag> for AuthenticationTag {
55 fn as_ref(&self) -> &AuthenticationTag { self }
56}
57
58impl 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
67impl From<Rc<AuthenticationTag>> for AuthenticationTag {
70 fn from(value: Rc<AuthenticationTag>) -> Self { (*value).clone() }
71}
72
73impl<'a> From<&'a AuthenticationTag>
76 for &'a [u8; AuthenticationTag::AUTHENTICATION_TAG_SIZE]
77{
78 fn from(value: &'a AuthenticationTag) -> Self { &value.0 }
79}
80
81impl From<&[u8]> for AuthenticationTag {
83 fn from(data: &[u8]) -> Self { Self::from_data_ref(data).unwrap() }
84}
85
86impl 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
93impl From<Vec<u8>> for AuthenticationTag {
95 fn from(data: Vec<u8>) -> Self { Self::from_data_ref(data).unwrap() }
96}
97
98impl From<AuthenticationTag> for CBOR {
100 fn from(value: AuthenticationTag) -> Self {
101 CBOR::to_byte_string(value.data())
102 }
103}
104
105impl 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}