From 0631c1c63724b4faea809611f6fa4f664fb6cc02 Mon Sep 17 00:00:00 2001 From: JohnE Date: Mon, 16 Nov 2026 20:15:48 -0500 Subject: [PATCH] awslc: add the aws-lc-rs provider crate with AEADs aws-lc-rs (Apache-2.0/ISC) replaces wolfSSL as the vetted provider and FIPS anchor; start with AES-256-GCM, AES-256-GCM-SIV and ChaCha20-Poly1305. Refs: R3 --- Cargo.lock | 99 ++++++++++++- Cargo.toml | 1 + crates/ccc-crypto-awslc/Cargo.toml | 27 ++++ crates/ccc-crypto-awslc/src/aead.rs | 217 ++++++++++++++++++++++++++++ crates/ccc-crypto-awslc/src/lib.rs | 12 ++ 5 files changed, 353 insertions(+), 3 deletions(-) create mode 100644 crates/ccc-crypto-awslc/Cargo.toml create mode 100644 crates/ccc-crypto-awslc/src/aead.rs create mode 100644 crates/ccc-crypto-awslc/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index d091f12..230b830 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,45 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "aws-lc-fips-sys" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b744eed18cd75d814effdf2aba2880f912abe0fe1c327b361341d31aba9a7005" +dependencies = [ + "bindgen 0.73.2", + "cc", + "cmake", + "dunce", + "fs_extra", + "pkg-config", + "regex", +] + +[[package]] +name = "aws-lc-rs" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "048006790404103c488e6c5616b3e7e702a71292aadda08e8f7698a9e0d2eb53" +dependencies = [ + "aws-lc-fips-sys", + "aws-lc-sys", + "zeroize", +] + +[[package]] +name = "aws-lc-sys" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bfbe32ac36b3c428696ce315464181bc4029d689fd58e3f2a4e58c252242f52" +dependencies = [ + "cc", + "cmake", + "dunce", + "fs_extra", + "pkg-config", +] + [[package]] name = "base64ct" version = "1.8.3" @@ -195,7 +234,7 @@ dependencies = [ "quote", "regex", "rustc-hash", - "shlex", + "shlex 1.3.0", "syn 2.0.117", ] @@ -215,10 +254,29 @@ dependencies = [ "quote", "regex", "rustc-hash", - "shlex", + "shlex 1.3.0", "syn 2.0.117", ] +[[package]] +name = "bindgen" +version = "0.73.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "787ef8ef523575546b106a58213d6e6b06198a05c2f757258c68a74273670cfa" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex 2.0.1", + "syn 3.0.10", +] + [[package]] name = "bitflags" version = "2.11.0" @@ -303,7 +361,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2" dependencies = [ "find-msvc-tools", - "shlex", + "jobserver", + "libc", + "shlex 1.3.0", ] [[package]] @@ -336,6 +396,17 @@ dependencies = [ "serde_json", ] +[[package]] +name = "ccc-crypto-awslc" +version = "0.2.0" +dependencies = [ + "aws-lc-rs", + "ccc-crypto-core", + "hex", + "log", + "zeroize", +] + [[package]] name = "ccc-crypto-boring" version = "0.2.0" @@ -652,6 +723,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + [[package]] name = "either" version = "1.15.0" @@ -887,6 +964,16 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom", + "libc", +] + [[package]] name = "keccak" version = "0.1.6" @@ -1324,6 +1411,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + [[package]] name = "spki" version = "0.8.1" diff --git a/Cargo.toml b/Cargo.toml index 38fbc57..9b09521 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "crates/ccc-crypto-rustcrypto", "crates/ccc-crypto-oqs", "crates/ccc-crypto-boring", + "crates/ccc-crypto-awslc", "tests/conformance", ] diff --git a/crates/ccc-crypto-awslc/Cargo.toml b/crates/ccc-crypto-awslc/Cargo.toml new file mode 100644 index 0000000..b3e592a --- /dev/null +++ b/crates/ccc-crypto-awslc/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "ccc-crypto-awslc" +version.workspace = true +edition.workspace = true +authors.workspace = true +license.workspace = true +description = "AWS-LC (aws-lc-rs) provider for the CCC crypto system; FIPS 140-3 with the `fips` feature." + +[dependencies] +ccc-crypto-core.workspace = true +zeroize.workspace = true +log.workspace = true +# Apache-2.0 OR ISC. The backend (`aws-lc-sys` or `aws-lc-fips-sys`) is +# chosen by this crate's features below. +aws-lc-rs = { version = "1.19", default-features = false, features = ["alloc"] } + +[features] +default = ["non-fips"] +# The open-source AWS-LC build (`aws-lc-sys`). +non-fips = ["aws-lc-rs/aws-lc-sys"] +# AWS-LC-FIPS (`aws-lc-fips-sys`), the FIPS 140-3 validated module. aws-lc-rs +# uses it whenever this feature is on; for a build without the unused +# open-source backend, use `--no-default-features --features fips`. +fips = ["aws-lc-rs/fips"] + +[dev-dependencies] +hex = "0.4" diff --git a/crates/ccc-crypto-awslc/src/aead.rs b/crates/ccc-crypto-awslc/src/aead.rs new file mode 100644 index 0000000..686b729 --- /dev/null +++ b/crates/ccc-crypto-awslc/src/aead.rs @@ -0,0 +1,217 @@ +//! AEADs from `aws_lc_rs::aead`. +//! +//! Wire format matches the other providers: `ciphertext || 16-byte tag`. +//! +//! | Algorithm | aws-lc-rs | FIPS 140-3 approved | +//! |-------------------|---------------------|---------------------| +//! | AES-256-GCM | `AES_256_GCM` | yes | +//! | AES-256-GCM-SIV | `AES_256_GCM_SIV` | no | +//! | ChaCha20-Poly1305 | `CHACHA20_POLY1305` | no | +//! +//! XChaCha20-Poly1305 and the Ascon AEADs are not in AWS-LC and return +//! [`CryptoError::FeatureNotCompiled`]. +//! +//! The caller supplies the nonce, so AES-256-GCM here is the "external IV" +//! mode; a FIPS deployment relies on the cascade's nonce construction for +//! uniqueness, as with the wolfSSL provider. + +use aws_lc_rs::aead::{ + self, Aad, LessSafeKey, Nonce, UnboundKey, AES_256_GCM, AES_256_GCM_SIV, CHACHA20_POLY1305, +}; +use zeroize::Zeroizing; + +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> { + let (key, nonce) = prepare(algo, key, nonce)?; + let mut in_out = Vec::with_capacity(plaintext.len() + TAG_LEN); + in_out.extend_from_slice(plaintext); + key.seal_in_place_append_tag(nonce, Aad::from(aad), &mut in_out) + .map_err(|_| CryptoError::InvalidInput(format!("{}: input too long", algo.name())))?; + Ok(in_out) +} + +/// 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> { + let (key, nonce) = prepare(algo, key, nonce)?; + if ciphertext_and_tag.len() < TAG_LEN { + return Err(CryptoError::InvalidInput(format!( + "{}: ciphertext too short (no room for tag)", + algo.name() + ))); + } + // Wiped on every path, so a rejected message leaves no unauthenticated + // plaintext behind. + let mut in_out = Zeroizing::new(ciphertext_and_tag.to_vec()); + let pt_len = key + .open_in_place(nonce, Aad::from(aad), &mut in_out) + .map_err(|_| CryptoError::AuthenticationFailed)? + .len(); + Ok(in_out[..pt_len].to_vec()) +} + +fn algorithm(algo: AeadAlgorithm) -> Result<&'static aead::Algorithm, CryptoError> { + match algo { + AeadAlgorithm::AesGcm256 => Ok(&AES_256_GCM), + AeadAlgorithm::AesGcmSiv256 => Ok(&AES_256_GCM_SIV), + AeadAlgorithm::ChaCha20Poly1305 => Ok(&CHACHA20_POLY1305), + AeadAlgorithm::XChaCha20Poly1305 + | AeadAlgorithm::Ascon128a + | AeadAlgorithm::AsconAead128 => Err(CryptoError::FeatureNotCompiled(format!( + "{} is not offered by the awslc provider", + algo.name() + ))), + } +} + +/// Check lengths and build the key (aws-lc keeps its own copy and cleanses +/// it on drop) and nonce. +fn prepare( + algo: AeadAlgorithm, + key: &[u8], + nonce: &[u8], +) -> Result<(LessSafeKey, Nonce), CryptoError> { + let alg = algorithm(algo)?; + 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() + ))); + } + let key = UnboundKey::new(alg, key).map_err(|_| CryptoError::InvalidKey(algo.name().into()))?; + let nonce = Nonce::try_assume_unique_for_key(nonce) + .map_err(|_| CryptoError::InvalidNonce(algo.name().into()))?; + Ok((LessSafeKey::new(key), nonce)) +} + +#[cfg(test)] +mod tests { + use super::*; + + const OFFERED: [AeadAlgorithm; 3] = [ + AeadAlgorithm::AesGcm256, + AeadAlgorithm::AesGcmSiv256, + AeadAlgorithm::ChaCha20Poly1305, + ]; + + 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 unoffered_aeads_are_not_compiled() { + for algo in [ + AeadAlgorithm::XChaCha20Poly1305, + AeadAlgorithm::Ascon128a, + AeadAlgorithm::AsconAead128, + ] { + let key = vec![0u8; algo.key_len()]; + let nonce = vec![0u8; algo.nonce_len()]; + assert!(matches!( + encrypt(algo, &key, &nonce, b"", b""), + Err(CryptoError::FeatureNotCompiled(_)) + )); + assert!(matches!( + decrypt(algo, &key, &nonce, &[0u8; 16], 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-awslc/src/lib.rs b/crates/ccc-crypto-awslc/src/lib.rs new file mode 100644 index 0000000..f9b58e5 --- /dev/null +++ b/crates/ccc-crypto-awslc/src/lib.rs @@ -0,0 +1,12 @@ +//! # ccc-crypto-awslc +//! +//! AWS-LC implementation of the CCC provider traits, built on `aws-lc-rs`. +//! It is the default vetted provider: with the `fips` feature it links +//! AWS-LC-FIPS, a FIPS 140-3 validated module. + +#![forbid(unsafe_code)] + +pub mod aead; + +/// The provider's registry name. +pub const PROVIDER_NAME: &str = "awslc";