boring: add HMAC-SHA-2 with constant-time verify
HMAC-SHA-256/384/512 from BoringSSL; tags are compared with CRYPTO_memcmp. Refs: R3
This commit is contained in:
parent
0ed4d9bb19
commit
01fb73fb4c
|
|
@ -17,6 +17,7 @@
|
|||
#![forbid(unsafe_code)]
|
||||
|
||||
pub mod hash;
|
||||
pub mod mac;
|
||||
|
||||
/// The provider's registry name.
|
||||
pub const PROVIDER_NAME: &str = "boring";
|
||||
|
|
|
|||
|
|
@ -0,0 +1,119 @@
|
|||
//! MACs: HMAC-SHA-256/384/512 from BoringSSL's `HMAC_CTX`.
|
||||
//!
|
||||
//! Verification compares in constant time with `CRYPTO_memcmp`
|
||||
//! ([`boring::memcmp::eq`]). BoringSSL's Poly1305 and BLAKE2b are not in the
|
||||
//! safe `boring` API, so those IDs return [`CryptoError::FeatureNotCompiled`].
|
||||
|
||||
use boring::hash::MessageDigest;
|
||||
use boring::hmac::Hmac;
|
||||
|
||||
use ccc_crypto_core::{algorithms::MacAlgorithm, error::CryptoError};
|
||||
|
||||
/// 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 md = digest(algo)?;
|
||||
hmac(&md, key, data)
|
||||
}
|
||||
|
||||
/// Verify a MAC tag in constant time. A mismatch is `Ok(false)`.
|
||||
pub fn verify_mac(
|
||||
algo: MacAlgorithm,
|
||||
key: &[u8],
|
||||
data: &[u8],
|
||||
mac: &[u8],
|
||||
) -> Result<bool, CryptoError> {
|
||||
let expected = compute_mac(algo, key, data)?;
|
||||
// `memcmp::eq` panics on unequal lengths; the length is public.
|
||||
Ok(expected.len() == mac.len() && boring::memcmp::eq(&expected, mac))
|
||||
}
|
||||
|
||||
fn digest(algo: MacAlgorithm) -> Result<MessageDigest, CryptoError> {
|
||||
match algo {
|
||||
MacAlgorithm::HmacSha256 => Ok(MessageDigest::sha256()),
|
||||
MacAlgorithm::HmacSha384 => Ok(MessageDigest::sha384()),
|
||||
MacAlgorithm::HmacSha512 => Ok(MessageDigest::sha512()),
|
||||
MacAlgorithm::Blake2bMac | MacAlgorithm::Poly1305 => Err(CryptoError::FeatureNotCompiled(
|
||||
format!("{} is not in the boring API", algo.name()),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// HMAC with `md`. BoringSSL wipes the key schedule when the context is
|
||||
/// freed (`HMAC_CTX_cleanup`).
|
||||
fn hmac(md: &MessageDigest, key: &[u8], data: &[u8]) -> Result<Vec<u8>, CryptoError> {
|
||||
let internal = |e: boring::error::ErrorStack| CryptoError::InternalError(format!("HMAC: {e}"));
|
||||
let mut m = Hmac::init(key, md).map_err(internal)?;
|
||||
m.update(data).map_err(internal)?;
|
||||
m.finalize().map_err(internal)
|
||||
}
|
||||
|
||||
#[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", &[]).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() {
|
||||
// RFC 4231 TC6: a 131-byte key is hashed first.
|
||||
let tag = compute_mac(
|
||||
MacAlgorithm::HmacSha256,
|
||||
&[0xaa; 131],
|
||||
b"Test Using Larger Than Block-Size Key - Hash Key First",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
hex::encode(tag),
|
||||
"60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"
|
||||
);
|
||||
assert!(compute_mac(MacAlgorithm::HmacSha512, b"", b"x").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_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(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue