Skip to main content

crypto_dispatch/
mac.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use crate::algorithms::hmac::{HmacSha256Algo, HmacSha512Algo};
6use crate::traits::{MacAlgorithmAdapter, MacParams};
7use crate::AlgorithmError;
8use crypto_core::MacAlgorithm;
9
10/// Computes a MAC tag using the selected MAC algorithm.
11pub fn mac_authenticate(
12    alg: MacAlgorithm,
13    params: &MacParams<'_>,
14    message: &[u8],
15) -> Result<Vec<u8>, AlgorithmError> {
16    match alg {
17        MacAlgorithm::HmacSha256 => HmacSha256Algo::authenticate(params, message),
18        MacAlgorithm::HmacSha512 => HmacSha512Algo::authenticate(params, message),
19    }
20}
21
22/// Verifies a MAC tag using the selected MAC algorithm.
23pub fn mac_verify(
24    alg: MacAlgorithm,
25    params: &MacParams<'_>,
26    message: &[u8],
27    tag: &[u8],
28) -> Result<(), AlgorithmError> {
29    match alg {
30        MacAlgorithm::HmacSha256 => HmacSha256Algo::verify(params, message, tag),
31        MacAlgorithm::HmacSha512 => HmacSha512Algo::verify(params, message, tag),
32    }
33}