113 lines
3.9 KiB
Rust
113 lines
3.9 KiB
Rust
//! MACs: HMAC-SHA-2 (`hmac`), keyed BLAKE2b-512 (`blake2`) and one-time
|
||
//! Poly1305 (`poly1305`).
|
||
//!
|
||
//! Keyed BLAKE2b follows the wolfSSL provider: key of 1–64 bytes, 64-byte
|
||
//! tag, no salt or personalisation (RFC 7693 keyed mode).
|
||
|
||
use blake2::Blake2bMac512;
|
||
use hmac::{Hmac, KeyInit, Mac};
|
||
use poly1305::Poly1305;
|
||
use sha2::{Sha256, Sha384, Sha512};
|
||
use subtle::ConstantTimeEq;
|
||
|
||
use ccc_crypto_core::{algorithms::MacAlgorithm, error::CryptoError};
|
||
|
||
/// Compute a MAC tag over `data`.
|
||
pub fn compute_mac(algo: MacAlgorithm, key: &[u8], data: &[u8]) -> Result<Vec<u8>, CryptoError> {
|
||
match algo {
|
||
MacAlgorithm::HmacSha256 => hmac::<Hmac<Sha256>>(key, data),
|
||
MacAlgorithm::HmacSha384 => hmac::<Hmac<Sha384>>(key, data),
|
||
MacAlgorithm::HmacSha512 => hmac::<Hmac<Sha512>>(key, data),
|
||
MacAlgorithm::Blake2bMac => blake2b_mac(key, data),
|
||
MacAlgorithm::Poly1305 => poly1305(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)?;
|
||
Ok(expected.len() == mac.len() && bool::from(expected.ct_eq(mac)))
|
||
}
|
||
|
||
/// HMAC with any `hmac` instance; any key length is accepted (RFC 2104).
|
||
pub(crate) fn hmac<M: Mac + KeyInit>(key: &[u8], data: &[u8]) -> Result<Vec<u8>, CryptoError> {
|
||
let mut m = <M as KeyInit>::new_from_slice(key)
|
||
.map_err(|_| CryptoError::InvalidKey("HMAC key".into()))?;
|
||
m.update(data);
|
||
Ok(m.finalize().into_bytes().to_vec())
|
||
}
|
||
|
||
fn blake2b_mac(key: &[u8], data: &[u8]) -> Result<Vec<u8>, CryptoError> {
|
||
if key.is_empty() || key.len() > 64 {
|
||
return Err(CryptoError::InvalidKey(
|
||
"BLAKE2b-MAC: key must be 1–64 bytes".into(),
|
||
));
|
||
}
|
||
let mut m = Blake2bMac512::new_with_salt_and_personal(Some(key), &[], &[])
|
||
.map_err(|_| CryptoError::InvalidKey("BLAKE2b-MAC key".into()))?;
|
||
m.update(data);
|
||
Ok(m.finalize().into_bytes().to_vec())
|
||
}
|
||
|
||
fn poly1305(key: &[u8], data: &[u8]) -> Result<Vec<u8>, CryptoError> {
|
||
let p = Poly1305::new_from_slice(key)
|
||
.map_err(|_| CryptoError::InvalidKey("Poly1305: key must be 32 bytes".into()))?;
|
||
Ok(p.compute_unpadded(data).to_vec())
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
const ALL: [MacAlgorithm; 5] = [
|
||
MacAlgorithm::HmacSha256,
|
||
MacAlgorithm::HmacSha384,
|
||
MacAlgorithm::HmacSha512,
|
||
MacAlgorithm::Blake2bMac,
|
||
MacAlgorithm::Poly1305,
|
||
];
|
||
|
||
#[test]
|
||
fn tag_lengths_and_verify() {
|
||
let key = [0x42u8; 32];
|
||
for algo in ALL {
|
||
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 key_length_rules() {
|
||
assert!(compute_mac(MacAlgorithm::Blake2bMac, b"", b"x").is_err());
|
||
assert!(compute_mac(MacAlgorithm::Blake2bMac, &[1u8; 65], b"x").is_err());
|
||
assert!(compute_mac(MacAlgorithm::Blake2bMac, &[1u8; 64], b"x").is_ok());
|
||
assert!(compute_mac(MacAlgorithm::Poly1305, &[1u8; 16], b"x").is_err());
|
||
assert!(compute_mac(MacAlgorithm::HmacSha512, b"", b"x").is_ok());
|
||
}
|
||
}
|