bc_components/symmetric/
authentication_tag.rs1use std::rc::Rc;
2
3use dcbor::prelude::*;
4
5use crate::{Error, Result};
6
7#[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 pub const fn from_data(data: [u8; Self::AUTHENTICATION_TAG_SIZE]) -> Self {
29 Self(data)
30 }
31
32 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 pub fn data(&self) -> &[u8; Self::AUTHENTICATION_TAG_SIZE] { self.into() }
49
50 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
58impl AsRef<AuthenticationTag> for AuthenticationTag {
60 fn as_ref(&self) -> &AuthenticationTag { self }
61}
62
63impl 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
72impl From<Rc<AuthenticationTag>> for AuthenticationTag {
75 fn from(value: Rc<AuthenticationTag>) -> Self { (*value).clone() }
76}
77
78impl<'a> From<&'a AuthenticationTag>
81 for &'a [u8; AuthenticationTag::AUTHENTICATION_TAG_SIZE]
82{
83 fn from(value: &'a AuthenticationTag) -> Self { &value.0 }
84}
85
86impl From<&[u8]> for AuthenticationTag {
88 fn from(data: &[u8]) -> Self { Self::from_data_ref(data).unwrap() }
89}
90
91impl 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
98impl From<Vec<u8>> for AuthenticationTag {
100 fn from(data: Vec<u8>) -> Self { Self::from_data_ref(data).unwrap() }
101}
102
103impl From<AuthenticationTag> for CBOR {
105 fn from(value: AuthenticationTag) -> Self {
106 CBOR::to_byte_string(value.data())
107 }
108}
109
110impl 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}