diff --git a/Cargo.lock b/Cargo.lock index 19a1b56..525b616 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,58 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aead" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99" +dependencies = [ + "crypto-common 0.2.2", + "inout", +] + +[[package]] +name = "aes" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1fc76eaeac4c9164506c466d4ffdd8ec9d0c5bf57ee97177c4d8eceb3a0e138" +dependencies = [ + "cipher", + "cpubits", + "cpufeatures 0.3.1", + "zeroize", +] + +[[package]] +name = "aes-gcm" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f2b8006a0c83f52b62ba44a97b58bf76fe2f70a329e588f67f89691d93d498f" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ctutils", + "ghash", + "zeroize", +] + +[[package]] +name = "aes-gcm-siv" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c32325128cb24de2a2d14d92da7569dae7073f9e47334521d494747fa94d902" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ctutils", + "polyval", + "zeroize", +] + [[package]] name = "aho-corasick" version = "1.1.4" @@ -75,11 +127,33 @@ checksum = "3c3610892ee6e0cbce8ae2700349fcf8f98adb0dbfbee85aec3c9179d29cc072" dependencies = [ "base64ct", "blake2", - "cpufeatures", + "cpufeatures 0.2.17", "password-hash", "zeroize", ] +[[package]] +name = "ascon-aead" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f2bd90369c4fec07a32d8d04b61e0b9e789e9f45b2b2fe9f7aeef5152307bed" +dependencies = [ + "aead", + "ascon-core", + "ctutils", + "inout", + "zeroize", +] + +[[package]] +name = "ascon-core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c90c8493a0b3bf8678a82f096821a2c527dd6344bbcef6265bfcc09745f44e24" +dependencies = [ + "zeroize", +] + [[package]] name = "base64ct" version = "1.8.3" @@ -130,6 +204,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "cc" version = "1.2.56" @@ -174,6 +257,20 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ccc-crypto-rustcrypto" +version = "0.1.0" +dependencies = [ + "aes-gcm", + "aes-gcm-siv", + "ascon-aead", + "ccc-crypto-core", + "chacha20poly1305", + "hex", + "log", + "zeroize", +] + [[package]] name = "ccc-crypto-wolfssl" version = "0.2.0" @@ -203,6 +300,42 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.3.1", + "zeroize", +] + +[[package]] +name = "chacha20poly1305" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b89e1c441e926b9c82a8d023f6e1b7ae0adcfaa7d621814e4d60789bac751cb" +dependencies = [ + "aead", + "chacha20", + "cipher", + "poly1305", + "zeroize", +] + +[[package]] +name = "cipher" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" +dependencies = [ + "block-buffer 0.12.1", + "crypto-common 0.2.2", + "inout", +] + [[package]] name = "clang-sys" version = "1.8.1" @@ -223,12 +356,24 @@ dependencies = [ "cc", ] +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + [[package]] name = "colorchoice" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +[[package]] +name = "cpubits" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -238,6 +383,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + [[package]] name = "crypto-common" version = "0.1.7" @@ -248,14 +402,42 @@ dependencies = [ "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "hybrid-array", + "rand_core 0.10.1", +] + +[[package]] +name = "ctr" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baaca1c4b237092596f64d571e9db6ce4109c4ef9742e27590f1709594461f21" +dependencies = [ + "cipher", +] + +[[package]] +name = "ctutils" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" +dependencies = [ + "cmov", +] + [[package]] name = "digest" version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "crypto-common", + "block-buffer 0.10.4", + "crypto-common 0.1.7", "subtle", ] @@ -304,6 +486,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "ghash" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eecf2d5dc9b66b732b97707a0210906b1d30523eb773193ab777c0c84b3e8d5" +dependencies = [ + "polyval", + "zeroize", +] + [[package]] name = "glob" version = "0.3.3" @@ -316,6 +508,24 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hybrid-array" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3944cf8cf766b40e2a1a333ee5e9b563f854d5fa49d6a8ca2764e97c6eddb214" +dependencies = [ + "typenum", +] + +[[package]] +name = "inout" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9" +dependencies = [ + "hybrid-array", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -424,10 +634,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" dependencies = [ "base64ct", - "rand_core", + "rand_core 0.6.4", "subtle", ] +[[package]] +name = "poly1305" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2d0073b297041425c7c3df6eb4792d598a15323fe63346852b092eca02904c" +dependencies = [ + "cpufeatures 0.3.1", + "universal-hash", +] + +[[package]] +name = "polyval" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0fa31d631f2b2cb2a544d0aa321ce847a94764d701ca2becc411138b93d49cd" +dependencies = [ + "cpubits", + "cpufeatures 0.3.1", + "universal-hash", + "zeroize", +] + [[package]] name = "portable-atomic" version = "1.13.1" @@ -477,6 +709,12 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + [[package]] name = "regex" version = "1.12.3" @@ -610,6 +848,16 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "universal-hash" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4987bdc12753382e0bec4a65c50738ffaabc998b9cdd1f952fb5f39b0048a96" +dependencies = [ + "crypto-common 0.2.2", + "ctutils", +] + [[package]] name = "utf8parse" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index f67c506..6bd5daf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "crates/ccc-crypto-core", "crates/ccc-cascade", "crates/ccc-crypto-wolfssl", + "crates/ccc-crypto-rustcrypto", "tests/conformance", ] @@ -28,4 +29,5 @@ env_logger = "0.11" # ccc-crypto-core is referenced by every crate in the workspace. ccc-crypto-core = { path = "crates/ccc-crypto-core" } ccc-cascade = { path = "crates/ccc-cascade" } +ccc-crypto-rustcrypto = { path = "crates/ccc-crypto-rustcrypto" } diff --git a/crates/ccc-crypto-rustcrypto/Cargo.toml b/crates/ccc-crypto-rustcrypto/Cargo.toml new file mode 100644 index 0000000..f8a1c4e --- /dev/null +++ b/crates/ccc-crypto-rustcrypto/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "ccc-crypto-rustcrypto" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +description = "Pure-Rust (RustCrypto) provider for the CCC crypto system." + +[dependencies] +ccc-crypto-core.workspace = true +zeroize.workspace = true +log.workspace = true + +# AEAD +aes-gcm = { version = "0.11", default-features = false, features = ["aes", "alloc", "zeroize"] } +aes-gcm-siv = { version = "0.12", default-features = false, features = ["aes", "alloc", "zeroize"] } +chacha20poly1305 = { version = "0.11", default-features = false, features = ["alloc", "zeroize"] } +# Ascon-AEAD128 as standardised in NIST SP 800-232 (not the pre-standard Ascon-128a). +# Default features are needed: they pull in the optional zeroize dependency +# that the crate's own "zeroize" feature forgets to enable. +ascon-aead = { version = "0.6", features = ["zeroize"] } + +[dev-dependencies] +hex = "0.4" diff --git a/crates/ccc-crypto-rustcrypto/src/aead.rs b/crates/ccc-crypto-rustcrypto/src/aead.rs new file mode 100644 index 0000000..56ebf9c --- /dev/null +++ b/crates/ccc-crypto-rustcrypto/src/aead.rs @@ -0,0 +1,239 @@ +//! AEAD implementations from the RustCrypto `aead` 0.6 family. +//! +//! Wire format matches the wolfSSL provider: `ciphertext || 16-byte tag`. +//! +//! | Algorithm | Crate | +//! |--------------------|-----------------------| +//! | AES-256-GCM | `aes-gcm` | +//! | AES-256-GCM-SIV | `aes-gcm-siv` | +//! | ChaCha20-Poly1305 | `chacha20poly1305` | +//! | XChaCha20-Poly1305 | `chacha20poly1305` | +//! | Ascon-AEAD128 | `ascon-aead` (SP 800-232) | +//! +//! The pre-standard Ascon-128a (ID 15) is not offered: `ascon-aead` 0.6 +//! implements only the final NIST SP 800-232 Ascon-AEAD128, which is a +//! different algorithm, so ID 15 returns [`CryptoError::FeatureNotCompiled`]. + +use aes_gcm::aead::{Aead, KeyInit, Nonce, Payload}; +use aes_gcm::Aes256Gcm; +use aes_gcm_siv::Aes256GcmSiv; +use ascon_aead::AsconAead128; +use chacha20poly1305::{ChaCha20Poly1305, XChaCha20Poly1305}; + +use ccc_crypto_core::{algorithms::AeadAlgorithm, error::CryptoError}; + +const TAG_LEN: usize = 16; + +/// Encrypt `plaintext`; returns `ciphertext || tag`. +pub fn encrypt( + algo: AeadAlgorithm, + key: &[u8], + nonce: &[u8], + plaintext: &[u8], + aad: &[u8], +) -> Result, CryptoError> { + validate_key_nonce(algo, key, nonce)?; + match algo { + AeadAlgorithm::AesGcm256 => seal::(algo, key, nonce, plaintext, aad), + AeadAlgorithm::AesGcmSiv256 => seal::(algo, key, nonce, plaintext, aad), + AeadAlgorithm::ChaCha20Poly1305 => { + seal::(algo, key, nonce, plaintext, aad) + } + AeadAlgorithm::XChaCha20Poly1305 => { + seal::(algo, key, nonce, plaintext, aad) + } + AeadAlgorithm::AsconAead128 => seal::(algo, key, nonce, plaintext, aad), + AeadAlgorithm::Ascon128a => Err(CryptoError::FeatureNotCompiled(algo.name().into())), + } +} + +/// Authenticate and decrypt `ciphertext || tag`. +/// +/// Returns [`CryptoError::AuthenticationFailed`] if the tag does not verify. +pub fn decrypt( + algo: AeadAlgorithm, + key: &[u8], + nonce: &[u8], + ciphertext_and_tag: &[u8], + aad: &[u8], +) -> Result, CryptoError> { + validate_key_nonce(algo, key, nonce)?; + if ciphertext_and_tag.len() < TAG_LEN { + return Err(CryptoError::InvalidInput(format!( + "{}: ciphertext too short (no room for tag)", + algo.name() + ))); + } + let ct = ciphertext_and_tag; + match algo { + AeadAlgorithm::AesGcm256 => open::(algo, key, nonce, ct, aad), + AeadAlgorithm::AesGcmSiv256 => open::(algo, key, nonce, ct, aad), + AeadAlgorithm::ChaCha20Poly1305 => open::(algo, key, nonce, ct, aad), + AeadAlgorithm::XChaCha20Poly1305 => open::(algo, key, nonce, ct, aad), + AeadAlgorithm::AsconAead128 => open::(algo, key, nonce, ct, aad), + AeadAlgorithm::Ascon128a => Err(CryptoError::FeatureNotCompiled(algo.name().into())), + } +} + +fn validate_key_nonce(algo: AeadAlgorithm, key: &[u8], nonce: &[u8]) -> Result<(), CryptoError> { + if key.len() != algo.key_len() { + return Err(CryptoError::InvalidKey(format!( + "{}: expected {}-byte key, got {}", + algo.name(), + algo.key_len(), + key.len() + ))); + } + if nonce.len() != algo.nonce_len() { + return Err(CryptoError::InvalidNonce(format!( + "{}: expected {}-byte nonce, got {}", + algo.name(), + algo.nonce_len(), + nonce.len() + ))); + } + Ok(()) +} + +fn cipher(algo: AeadAlgorithm, key: &[u8]) -> Result { + C::new_from_slice(key).map_err(|_| CryptoError::InvalidKey(algo.name().into())) +} + +fn nonce_of(algo: AeadAlgorithm, nonce: &[u8]) -> Result, CryptoError> { + Nonce::::try_from(nonce).map_err(|_| CryptoError::InvalidNonce(algo.name().into())) +} + +fn seal( + algo: AeadAlgorithm, + key: &[u8], + nonce: &[u8], + plaintext: &[u8], + aad: &[u8], +) -> Result, CryptoError> { + let c = cipher::(algo, key)?; + let n = nonce_of::(algo, nonce)?; + c.encrypt( + &n, + Payload { + msg: plaintext, + aad, + }, + ) + .map_err(|_| CryptoError::InvalidInput(format!("{}: input too long", algo.name()))) +} + +fn open( + algo: AeadAlgorithm, + key: &[u8], + nonce: &[u8], + ciphertext_and_tag: &[u8], + aad: &[u8], +) -> Result, CryptoError> { + let c = cipher::(algo, key)?; + let n = nonce_of::(algo, nonce)?; + c.decrypt( + &n, + Payload { + msg: ciphertext_and_tag, + aad, + }, + ) + .map_err(|_| CryptoError::AuthenticationFailed) +} + +#[cfg(test)] +mod tests { + use super::*; + + const OFFERED: [AeadAlgorithm; 5] = [ + AeadAlgorithm::AesGcm256, + AeadAlgorithm::AesGcmSiv256, + AeadAlgorithm::ChaCha20Poly1305, + AeadAlgorithm::XChaCha20Poly1305, + AeadAlgorithm::AsconAead128, + ]; + + fn inputs(algo: AeadAlgorithm) -> (Vec, Vec) { + let key = (0..algo.key_len() as u8).collect(); + let nonce = (0..algo.nonce_len() as u8).map(|b| b ^ 0x5a).collect(); + (key, nonce) + } + + #[test] + fn roundtrip_all_offered() { + for algo in OFFERED { + let (key, nonce) = inputs(algo); + for (pt, aad) in [ + (&b"hello cascade"[..], &b"hdr"[..]), + (b"", b""), + (b"x", b""), + (b"", b"aad only"), + ] { + let ct = encrypt(algo, &key, &nonce, pt, aad).unwrap(); + assert_eq!(ct.len(), pt.len() + TAG_LEN, "{}", algo.name()); + assert_eq!(decrypt(algo, &key, &nonce, &ct, aad).unwrap(), pt); + } + } + } + + #[test] + fn tampering_is_rejected() { + for algo in OFFERED { + let (key, nonce) = inputs(algo); + let ct = encrypt(algo, &key, &nonce, b"payload", b"aad").unwrap(); + for i in [0, ct.len() - 1] { + let mut bad = ct.clone(); + bad[i] ^= 0x01; + assert!(matches!( + decrypt(algo, &key, &nonce, &bad, b"aad"), + Err(CryptoError::AuthenticationFailed) + )); + } + assert!(matches!( + decrypt(algo, &key, &nonce, &ct, b"aaD"), + Err(CryptoError::AuthenticationFailed) + )); + } + } + + #[test] + fn bad_lengths_are_rejected() { + for algo in OFFERED { + let (key, nonce) = inputs(algo); + assert!(matches!( + encrypt(algo, &key[1..], &nonce, b"", b""), + Err(CryptoError::InvalidKey(_)) + )); + assert!(matches!( + encrypt(algo, &key, &nonce[1..], b"", b""), + Err(CryptoError::InvalidNonce(_)) + )); + assert!(matches!( + decrypt(algo, &key, &nonce, &[0u8; 15], b""), + Err(CryptoError::InvalidInput(_)) + )); + } + } + + #[test] + fn ascon128a_is_not_compiled() { + let key = [0u8; 16]; + let nonce = [0u8; 16]; + assert!(matches!( + encrypt(AeadAlgorithm::Ascon128a, &key, &nonce, b"", b""), + Err(CryptoError::FeatureNotCompiled(_)) + )); + } + + #[test] + fn algorithms_are_not_interchangeable() { + let key = [7u8; 32]; + let nonce = [9u8; 12]; + let gcm = encrypt(AeadAlgorithm::AesGcm256, &key, &nonce, b"same", b"").unwrap(); + let siv = encrypt(AeadAlgorithm::AesGcmSiv256, &key, &nonce, b"same", b"").unwrap(); + let cha = encrypt(AeadAlgorithm::ChaCha20Poly1305, &key, &nonce, b"same", b"").unwrap(); + assert_ne!(gcm, siv); + assert_ne!(gcm, cha); + assert!(decrypt(AeadAlgorithm::AesGcmSiv256, &key, &nonce, &gcm, b"").is_err()); + } +} diff --git a/crates/ccc-crypto-rustcrypto/src/lib.rs b/crates/ccc-crypto-rustcrypto/src/lib.rs new file mode 100644 index 0000000..bfd93ef --- /dev/null +++ b/crates/ccc-crypto-rustcrypto/src/lib.rs @@ -0,0 +1,10 @@ +//! # ccc-crypto-rustcrypto +//! +//! Pure-Rust implementation of the CCC provider traits, built on the +//! RustCrypto crates. It is the independent second implementation next to +//! `ccc-crypto-wolfssl` (design: "Provider diversity") and the provider a +//! web build would use. It is not a FIPS module: `fips_mode()` is `false`. + +#![forbid(unsafe_code)] + +pub mod aead;