pub struct AuthenticatedData<const W: usize = { options::DEFAULT_GRID_WIDTH }, const H: usize = { options::DEFAULT_GRID_HEIGHT }> {
pub mac: [u8; 32],
pub encrypted_data: EncryptedData<W, H>,
}Expand description
Authenticated encryption data containing both MAC tag and encrypted data.
This structure represents the output of the authentication process,
combining a 32-byte HMAC-SHA256 tag with the original EncryptedData.
§Layout
When serialized to bytes, the format is:
[32-byte HMAC-SHA256 tag][serialized encrypted grids]§Fields
mac: The 32-byte HMAC-SHA256 authentication tag.encrypted_data: The originalEncryptedDatacontaining encrypted grids, nonce, and key.
§Notes
- The MAC is computed over the serialized grids (nonce + ciphertext).
- The nonce is included in the authenticated data to ensure freshness.
- This structure preserves the original grid structure for easy decryption.
Fields§
§mac: [u8; 32]§encrypted_data: EncryptedData<W, H>Implementations§
Source§impl<const W: usize, const H: usize> AuthenticatedData<W, H>
impl<const W: usize, const H: usize> AuthenticatedData<W, H>
Sourcepub fn authenticate(
encrypted_data: EncryptedData<W, H>,
mac_key: &[u8; 32],
) -> Self
pub fn authenticate( encrypted_data: EncryptedData<W, H>, mac_key: &[u8; 32], ) -> Self
Computes HMAC-SHA256 authentication tag for encrypted data.
This function serializes the encrypted grids into big-endian bytes,
computes an HMAC-SHA256 tag using the provided MAC key, and returns
an AuthenticatedData structure containing both the tag and the original
EncryptedData.
§Algorithm
The authentication process follows these steps:
-
Serialization: Convert encrypted grids to bytes: $ C = \text{nonce} \mathbin| G_0 \mathbin| G_1 \mathbin| \cdots \mathbin| G_n $ where each grid cell is encoded as big-endian
i64. -
MAC Computation: Calculate HMAC-SHA256: $ \text{tag} = \text{HMAC-SHA256}(K_{\text{mac}}, C) $
-
Package Construction: Return structure with tag and original data: $ \text{output} = (\text{tag}, \text{EncryptedData}) $
§Parameters
encrypted_data: TheEncryptedDatastructure containing:output: Vector of encryptedGridsnonce: The CTR mode initialization vectorkey: The encryption key (not used in MAC computation)
mac_key: A 32-byte key for HMAC-SHA256 computation.
§Returns
An AuthenticatedData structure containing:
mac: The 32-byte HMAC-SHA256 tagencrypted_data: The originalEncryptedData
§Security Notes
- The MAC key must be derived independently from the encryption key.
- The nonce is included in the authenticated data to prevent replay attacks.
- Use HKDF or similar KDF to derive separate encryption and MAC keys.
Sourcepub fn verify(&self, mac_key: &[u8; 32]) -> bool
pub fn verify(&self, mac_key: &[u8; 32]) -> bool
Verifies the integrity and authenticity of the encrypted data.
This function re-computes the HMAC-SHA256 tag over the stored encrypted grids
and compares it with the attached authentication tag (self.mac).
§Algorithm
The verification process mirrors the authentication steps to ensure consistency:
-
Re-serialization: The internal nonce and ciphertext grids are serialized into bytes using the exact same order as during authentication (Big-Endian).
-
MAC Re-computation: Calculate the expected tag using the provided key: $$ T’ = \text{HMAC-SHA256}(K_{\text{mac}}, C) $$
-
Constant-Time Comparison: Compare the calculated tag with the stored
self.mac: $$ \text{valid} \iff T’ \stackrel{?}{=} T $$
where $T$ is the stored tag and $T’$ is the recomputed tag.
§Parameters
mac_key: The 32-byte key used for HMAC-SHA256 computation. Must be the same key used to generate the authentication tag.
§Returns
true: If the computed tag matchesself.mac(data is authentic).false: If the tags do not match (integrity check failed).
§Security Notes
- This function uses constant-time comparison (
verify_slice) to prevent timing side-channel attacks. - If this function returns
false, theencrypted_dataMUST be discarded and NOT treated as valid ciphertext.
Trait Implementations§
Source§impl<const W: usize, const H: usize> From<AuthenticatedData<W, H>> for Vec<u8>
impl<const W: usize, const H: usize> From<AuthenticatedData<W, H>> for Vec<u8>
Source§fn from(data: AuthenticatedData<W, H>) -> Self
fn from(data: AuthenticatedData<W, H>) -> Self
Auto Trait Implementations§
impl<const W: usize, const H: usize> Freeze for AuthenticatedData<W, H>
impl<const W: usize, const H: usize> RefUnwindSafe for AuthenticatedData<W, H>
impl<const W: usize, const H: usize> Send for AuthenticatedData<W, H>
impl<const W: usize, const H: usize> Sync for AuthenticatedData<W, H>
impl<const W: usize, const H: usize> Unpin for AuthenticatedData<W, H>
impl<const W: usize, const H: usize> UnwindSafe for AuthenticatedData<W, H>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more