awslc: add SHA-2, SHA-3 and HMAC-SHA-2

HMAC verification uses aws-lc-rs's constant-time hmac::verify; BLAKE2b
and Poly1305 are not in aws-lc-rs and report FeatureNotCompiled.

Refs: R3
This commit is contained in:
JohnE 2026-11-16 20:18:03 -05:00
parent 0631c1c637
commit 72c24def82
3 changed files with 159 additions and 0 deletions

View File

@ -0,0 +1,60 @@
//! Hash functions from `aws_lc_rs::digest`: SHA-2 and SHA-3.
//!
//! BLAKE2b-512 is not in AWS-LC and returns
//! [`CryptoError::FeatureNotCompiled`].
use aws_lc_rs::digest::{self, SHA256, SHA384, SHA3_256, SHA3_512, SHA512};
use ccc_crypto_core::{algorithms::HashAlgorithm, error::CryptoError};
/// Compute the digest of `data`.
pub fn hash(algo: HashAlgorithm, data: &[u8]) -> Result<Vec<u8>, CryptoError> {
let alg = match algo {
HashAlgorithm::Sha256 => &SHA256,
HashAlgorithm::Sha384 => &SHA384,
HashAlgorithm::Sha512 => &SHA512,
HashAlgorithm::Sha3_256 => &SHA3_256,
HashAlgorithm::Sha3_512 => &SHA3_512,
HashAlgorithm::Blake2b512 => {
return Err(CryptoError::FeatureNotCompiled(format!(
"{} is not offered by the awslc provider",
algo.name()
)))
}
};
Ok(digest::digest(alg, data).as_ref().to_vec())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn digest_lengths_match_registry() {
for algo in [
HashAlgorithm::Sha256,
HashAlgorithm::Sha384,
HashAlgorithm::Sha512,
HashAlgorithm::Sha3_256,
HashAlgorithm::Sha3_512,
] {
assert_eq!(hash(algo, b"abc").unwrap().len(), algo.digest_len());
}
}
#[test]
fn sha256_abc() {
assert_eq!(
hex::encode(hash(HashAlgorithm::Sha256, b"abc").unwrap()),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
}
#[test]
fn blake2b_is_not_compiled() {
assert!(matches!(
hash(HashAlgorithm::Blake2b512, b"abc"),
Err(CryptoError::FeatureNotCompiled(_))
));
}
}

View File

@ -7,6 +7,8 @@
#![forbid(unsafe_code)]
pub mod aead;
pub mod hash;
pub mod mac;
/// The provider's registry name.
pub const PROVIDER_NAME: &str = "awslc";

View File

@ -0,0 +1,97 @@
//! HMAC-SHA-2 from `aws_lc_rs::hmac`.
//!
//! Keyed BLAKE2b and one-time Poly1305 are not exposed by aws-lc-rs and
//! return [`CryptoError::FeatureNotCompiled`].
use aws_lc_rs::hmac::{self, HMAC_SHA256, HMAC_SHA384, HMAC_SHA512};
use ccc_crypto_core::{algorithms::MacAlgorithm, error::CryptoError};
fn algorithm(algo: MacAlgorithm) -> Result<hmac::Algorithm, CryptoError> {
match algo {
MacAlgorithm::HmacSha256 => Ok(HMAC_SHA256),
MacAlgorithm::HmacSha384 => Ok(HMAC_SHA384),
MacAlgorithm::HmacSha512 => Ok(HMAC_SHA512),
MacAlgorithm::Blake2bMac | MacAlgorithm::Poly1305 => Err(CryptoError::FeatureNotCompiled(
format!("{} is not offered by the awslc provider", algo.name()),
)),
}
}
/// Compute a MAC tag over `data`. Any key length is accepted (RFC 2104).
pub fn compute_mac(algo: MacAlgorithm, key: &[u8], data: &[u8]) -> Result<Vec<u8>, CryptoError> {
let key = hmac::Key::new(algorithm(algo)?, key);
Ok(hmac::sign(&key, data).as_ref().to_vec())
}
/// Verify a MAC tag in constant time (`hmac::verify`). A mismatch,
/// including a wrong-length tag, is `Ok(false)`.
pub fn verify_mac(
algo: MacAlgorithm,
key: &[u8],
data: &[u8],
mac: &[u8],
) -> Result<bool, CryptoError> {
let key = hmac::Key::new(algorithm(algo)?, key);
Ok(hmac::verify(&key, data, mac).is_ok())
}
#[cfg(test)]
mod tests {
use super::*;
const OFFERED: [MacAlgorithm; 3] = [
MacAlgorithm::HmacSha256,
MacAlgorithm::HmacSha384,
MacAlgorithm::HmacSha512,
];
#[test]
fn tag_lengths_and_verify() {
let key = [0x42u8; 32];
for algo in OFFERED {
let tag = compute_mac(algo, &key, b"data").unwrap();
assert_eq!(tag.len(), algo.tag_len(), "{}", algo.name());
assert!(verify_mac(algo, &key, b"data", &tag).unwrap());
let mut bad = tag.clone();
bad[0] ^= 1;
assert!(!verify_mac(algo, &key, b"data", &bad).unwrap());
assert!(!verify_mac(algo, &key, b"data", &tag[1..]).unwrap());
assert!(!verify_mac(algo, &key, b"Data", &tag).unwrap());
}
}
#[test]
fn hmac_sha256_rfc4231_tc2() {
let tag = compute_mac(
MacAlgorithm::HmacSha256,
b"Jefe",
b"what do ya want for nothing?",
)
.unwrap();
assert_eq!(
hex::encode(tag),
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
);
}
#[test]
fn empty_and_long_keys_work() {
assert!(compute_mac(MacAlgorithm::HmacSha512, b"", b"x").is_ok());
assert!(compute_mac(MacAlgorithm::HmacSha256, &[1u8; 200], b"x").is_ok());
}
#[test]
fn unoffered_macs_are_not_compiled() {
for algo in [MacAlgorithm::Blake2bMac, MacAlgorithm::Poly1305] {
assert!(matches!(
compute_mac(algo, &[1u8; 32], b"x"),
Err(CryptoError::FeatureNotCompiled(_))
));
assert!(matches!(
verify_mac(algo, &[1u8; 32], b"x", &[0u8; 16]),
Err(CryptoError::FeatureNotCompiled(_))
));
}
}
}