diff --git a/Cargo.lock b/Cargo.lock index 525b616..0d80cea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -126,7 +126,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072" dependencies = [ "base64ct", - "blake2", + "blake2 0.10.6", "cpufeatures 0.2.17", "password-hash", "zeroize", @@ -192,7 +192,16 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" 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]] @@ -264,10 +273,16 @@ dependencies = [ "aes-gcm", "aes-gcm-siv", "ascon-aead", + "blake2 0.11.0", "ccc-crypto-core", "chacha20poly1305", "hex", + "hmac", "log", + "poly1305", + "sha2", + "sha3", + "subtle", "zeroize", ] @@ -368,6 +383,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +[[package]] +name = "const-oid" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" + [[package]] name = "cpubits" version = "0.1.1" @@ -441,6 +462,18 @@ dependencies = [ "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]] name = "either" version = "1.15.0" @@ -508,6 +541,15 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "hybrid-array" version = "0.4.10" @@ -571,6 +613,16 @@ dependencies = [ "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]] name = "libc" version = "0.2.182" @@ -793,12 +845,40 @@ dependencies = [ "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]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "sponge-cursor" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620" + [[package]] name = "subtle" version = "2.6.1" diff --git a/crates/ccc-crypto-rustcrypto/Cargo.toml b/crates/ccc-crypto-rustcrypto/Cargo.toml index f8a1c4e..bae9ff3 100644 --- a/crates/ccc-crypto-rustcrypto/Cargo.toml +++ b/crates/ccc-crypto-rustcrypto/Cargo.toml @@ -20,5 +20,13 @@ chacha20poly1305 = { version = "0.11", default-features = false, features = ["al # that the crate's own "zeroize" feature forgets to enable. 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] hex = "0.4" diff --git a/crates/ccc-crypto-rustcrypto/src/hash.rs b/crates/ccc-crypto-rustcrypto/src/hash.rs new file mode 100644 index 0000000..553e326 --- /dev/null +++ b/crates/ccc-crypto-rustcrypto/src/hash.rs @@ -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, 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" + ); + } +} diff --git a/crates/ccc-crypto-rustcrypto/src/lib.rs b/crates/ccc-crypto-rustcrypto/src/lib.rs index bfd93ef..b45446b 100644 --- a/crates/ccc-crypto-rustcrypto/src/lib.rs +++ b/crates/ccc-crypto-rustcrypto/src/lib.rs @@ -8,3 +8,5 @@ #![forbid(unsafe_code)] pub mod aead; +pub mod hash; +pub mod mac; diff --git a/crates/ccc-crypto-rustcrypto/src/mac.rs b/crates/ccc-crypto-rustcrypto/src/mac.rs new file mode 100644 index 0000000..c0c314a --- /dev/null +++ b/crates/ccc-crypto-rustcrypto/src/mac.rs @@ -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, CryptoError> { + match algo { + MacAlgorithm::HmacSha256 => hmac::>(key, data), + MacAlgorithm::HmacSha384 => hmac::>(key, data), + MacAlgorithm::HmacSha512 => hmac::>(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 { + 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(key: &[u8], data: &[u8]) -> Result, CryptoError> { + let mut m = ::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, 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, 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()); + } +}