rustcrypto: add hash and MAC primitives
SHA-2, SHA-3 and BLAKE2b digests plus HMAC, keyed BLAKE2b and Poly1305, matching the wolfSSL provider's key and tag conventions. Refs: R3-4
This commit is contained in:
parent
bc589960c8
commit
bb5473eeca
|
|
@ -126,7 +126,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072"
|
checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64ct",
|
"base64ct",
|
||||||
"blake2",
|
"blake2 0.10.6",
|
||||||
"cpufeatures 0.2.17",
|
"cpufeatures 0.2.17",
|
||||||
"password-hash",
|
"password-hash",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
|
|
@ -192,7 +192,16 @@ version = "0.10.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe"
|
checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"digest",
|
"digest 0.10.7",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "blake2"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b5d4d889834ee8ecfc0f8426ad30faf7cdcb10f741a8e6d7224d95325479f6f"
|
||||||
|
dependencies = [
|
||||||
|
"digest 0.11.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -264,10 +273,16 @@ dependencies = [
|
||||||
"aes-gcm",
|
"aes-gcm",
|
||||||
"aes-gcm-siv",
|
"aes-gcm-siv",
|
||||||
"ascon-aead",
|
"ascon-aead",
|
||||||
|
"blake2 0.11.0",
|
||||||
"ccc-crypto-core",
|
"ccc-crypto-core",
|
||||||
"chacha20poly1305",
|
"chacha20poly1305",
|
||||||
"hex",
|
"hex",
|
||||||
|
"hmac",
|
||||||
"log",
|
"log",
|
||||||
|
"poly1305",
|
||||||
|
"sha2",
|
||||||
|
"sha3",
|
||||||
|
"subtle",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -368,6 +383,12 @@ version = "1.0.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "const-oid"
|
||||||
|
version = "0.10.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cpubits"
|
name = "cpubits"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
|
|
@ -441,6 +462,18 @@ dependencies = [
|
||||||
"subtle",
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "digest"
|
||||||
|
version = "0.11.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
|
||||||
|
dependencies = [
|
||||||
|
"block-buffer 0.12.1",
|
||||||
|
"const-oid",
|
||||||
|
"crypto-common 0.2.2",
|
||||||
|
"ctutils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "either"
|
name = "either"
|
||||||
version = "1.15.0"
|
version = "1.15.0"
|
||||||
|
|
@ -508,6 +541,15 @@ version = "0.4.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hmac"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f"
|
||||||
|
dependencies = [
|
||||||
|
"digest 0.11.3",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hybrid-array"
|
name = "hybrid-array"
|
||||||
version = "0.4.10"
|
version = "0.4.10"
|
||||||
|
|
@ -571,6 +613,16 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "keccak"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d8f198d1db720e4940b5a493201d199d9f24f568f8f746bd13706243a2f71598"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures 0.3.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.182"
|
version = "0.2.182"
|
||||||
|
|
@ -793,12 +845,40 @@ dependencies = [
|
||||||
"zmij",
|
"zmij",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha2"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures 0.3.1",
|
||||||
|
"digest 0.11.3",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha3"
|
||||||
|
version = "0.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bc9bad02c26382724b2d2692c6f179285e4b54eeecd7968f52a50059c3c11759"
|
||||||
|
dependencies = [
|
||||||
|
"digest 0.11.3",
|
||||||
|
"keccak",
|
||||||
|
"sponge-cursor",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shlex"
|
name = "shlex"
|
||||||
version = "1.3.0"
|
version = "1.3.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sponge-cursor"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "subtle"
|
name = "subtle"
|
||||||
version = "2.6.1"
|
version = "2.6.1"
|
||||||
|
|
|
||||||
|
|
@ -20,5 +20,13 @@ chacha20poly1305 = { version = "0.11", default-features = false, features = ["al
|
||||||
# that the crate's own "zeroize" feature forgets to enable.
|
# that the crate's own "zeroize" feature forgets to enable.
|
||||||
ascon-aead = { version = "0.6", features = ["zeroize"] }
|
ascon-aead = { version = "0.6", features = ["zeroize"] }
|
||||||
|
|
||||||
|
# Hash and MAC
|
||||||
|
sha2 = "0.11"
|
||||||
|
sha3 = "0.12"
|
||||||
|
blake2 = "0.11"
|
||||||
|
hmac = "0.13"
|
||||||
|
poly1305 = "0.9"
|
||||||
|
subtle = "2.6"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
//! Hash functions: SHA-2 (`sha2`), SHA-3 (`sha3`) and BLAKE2b-512 (`blake2`).
|
||||||
|
|
||||||
|
use blake2::Blake2b512;
|
||||||
|
use sha2::{Digest, Sha256, Sha384, Sha512};
|
||||||
|
use sha3::{Sha3_256, Sha3_512};
|
||||||
|
|
||||||
|
use ccc_crypto_core::{algorithms::HashAlgorithm, error::CryptoError};
|
||||||
|
|
||||||
|
/// Compute the digest of `data`.
|
||||||
|
pub fn hash(algo: HashAlgorithm, data: &[u8]) -> Result<Vec<u8>, CryptoError> {
|
||||||
|
Ok(match algo {
|
||||||
|
HashAlgorithm::Sha256 => Sha256::digest(data).to_vec(),
|
||||||
|
HashAlgorithm::Sha384 => Sha384::digest(data).to_vec(),
|
||||||
|
HashAlgorithm::Sha512 => Sha512::digest(data).to_vec(),
|
||||||
|
HashAlgorithm::Blake2b512 => Blake2b512::digest(data).to_vec(),
|
||||||
|
HashAlgorithm::Sha3_256 => Sha3_256::digest(data).to_vec(),
|
||||||
|
HashAlgorithm::Sha3_512 => Sha3_512::digest(data).to_vec(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn digest_lengths_match_registry() {
|
||||||
|
for algo in [
|
||||||
|
HashAlgorithm::Sha256,
|
||||||
|
HashAlgorithm::Sha384,
|
||||||
|
HashAlgorithm::Sha512,
|
||||||
|
HashAlgorithm::Blake2b512,
|
||||||
|
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"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,3 +8,5 @@
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
pub mod aead;
|
pub mod aead;
|
||||||
|
pub mod hash;
|
||||||
|
pub mod mac;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,112 @@
|
||||||
|
//! 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue