bc_components/symmetric/
authentication_tag.rs1use std::rc::Rc;
2
3use anyhow::{ bail, Error, Result };
4use dcbor::prelude::*;
5
6#[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 pub const fn from_data(data: [u8; Self::AUTHENTICATION_TAG_SIZE]) -> Self {
26 Self(data)
27 }
28
29 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 pub fn data(&self) -> &[u8; Self::AUTHENTICATION_TAG_SIZE] {
42 self.into()
43 }
44}
45
46impl AsRef<AuthenticationTag> for AuthenticationTag {
48 fn as_ref(&self) -> &AuthenticationTag {
49 self
50 }
51}
52
53impl 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
60impl From<Rc<AuthenticationTag>> for AuthenticationTag {
62 fn from(value: Rc<AuthenticationTag>) -> Self {
63 (*value).clone()
64 }
65}
66
67impl<'a> From<&'a AuthenticationTag> for &'a [u8; AuthenticationTag::AUTHENTICATION_TAG_SIZE] {
69 fn from(value: &'a AuthenticationTag) -> Self {
70 &value.0
71 }
72}
73
74impl From<&[u8]> for AuthenticationTag {
76 fn from(data: &[u8]) -> Self {
77 Self::from_data_ref(data).unwrap()
78 }
79}
80
81impl 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
88impl From<Vec<u8>> for AuthenticationTag {
90 fn from(data: Vec<u8>) -> Self {
91 Self::from_data_ref(data).unwrap()
92 }
93}
94
95impl From<AuthenticationTag> for CBOR {
97 fn from(value: AuthenticationTag) -> Self {
98 CBOR::to_byte_string(value.data())
99 }
100}
101
102impl 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}