# Verified Garbage Verified Garbage logo Verified Garbage is an experimental cryptography library, implemented entirely by LLMs. All of the cryptography primitives are formally verified using Lean. Our goal is to implement all the cryptographic algorithms that are used by the Python pyca/cryptography library. Its aims are, in order: 1. Security 2. Correctness 3. Performance It targets x86 (i686 with SSE2), x86-64, ARMv7, ARM64, PPC64LE (little-endian 64-bit PowerPC, `powerpc64le-unknown-linux-gnu`'s baseline, POWER8) and RISC-V (RV64GC). The tables of [algorithms](#algorithms) say what each has. The crate refuses to build for configurations its ISA models do not describe: big-endian ARM, ARM64, PowerPC and RISC-V, RISC-V without the M or C extension, x32, x86 or x86-64 without SSE2 (e.g. `i586-*`, `x86_64-unknown-none`, the UEFI targets), ARM64 without NEON (`aarch64-unknown-none-softfloat`), and Apple's 32-bit ARM targets, which do not use AAPCS. Rust has no `cfg` for some other assumptions, so they are yours to keep: * On 32-bit x86, don't build with nightly's `-Zregparm`, which moves `extern "C"` arguments from the stack to registers. * ARMv7 code does word loads and stores at unaligned addresses. Hosted targets allow them; bare-metal code (e.g. `armv7a-none-eabi*`, built `+strict-align`) must turn off alignment checking and run with the MMU on, with its buffers in Normal memory: otherwise an unaligned access faults or, on some cores, is UNPREDICTABLE. * Only ARMv7 and later are supported on 32-bit ARM; older targets (`arm-*`, `armv5te-*`, …) are rejected only because the code does not assemble for them. ## Usage ```sh cargo add verified-garbage ``` ```rust use verified_garbage::aead::chacha20poly1305::ChaCha20Poly1305; use verified_garbage::hashes::sha256::Sha256; fn main() -> Result<(), Box> { let digest: [u8; 32] = Sha256::digest(b"hello world"); let key = [0; 32]; // a secret, random key let nonce = [0; 12]; // never used twice with the same key let aead = ChaCha20Poly1305::new(&key); let mut data = *b"a message"; let tag = aead.encrypt_in_place(&nonce, &digest, &mut data)?; aead.decrypt_in_place(&nonce, &digest, &mut data, &tag)?; assert_eq!(&data, b"a message"); Ok(()) } ``` It needs Rust 1.95 or later. Its Cargo features are: * `std` (on by default): detects the CPU's features at run time on every architecture. Without it, AArch64 uses only those the code is compiled for. * `alloc` (on with `std`): the algorithms that allocate (Argon2, scrypt, RSA, DSA and FFDH), without `std`. * `cpu-features-env`: the environment variable `VG_CPU_FEATURES` restricts the CPU features used, for testing. [`rustls-verified-garbage`](crates/rustls-verified-garbage/) is a rustls provider built on it. ## Algorithms ### Hashes
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
BLAKE2b ✅ ✅ AVX-512F, AVX-512VL, AVX2 ✅ ✅ ✅ ✅ ✅
BLAKE2s ✅ ✅ AVX ✅ ✅ ✅ ✅ ✅
MD5 ✅ ✅ ✅ ✅ ✅ ✅ ✅
SHA-1 ✅ ✅ SHA extensions, AVX, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
SHA-224 ✅ ✅ SHA extensions, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
SHA-256 ✅ ✅ SHA extensions, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, SHAKE256 ✅ ✅ ✅ SHA extensions ✅ ✅ ✅ ✅
SHA-384 ✅ ✅ SHA512, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
SHA-512/224 ✅ ✅ SHA512, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
SHA-512/256 ✅ ✅ SHA512, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
SHA-512 ✅ ✅ SHA512, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
SM3 ✅ ✅ ✅ SM4 ✅ ✅ ✅ ✅
### MACs
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
AES-CMAC (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
3DES-CMAC (two- and three-key TDEA) ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
HMAC-MD5 ✅ ✅ ✅ ✅ ✅ ✅ ✅
HMAC-SHA-1 ✅ ✅ SHA extensions, AVX, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
HMAC-SHA-224 ✅ ✅ SHA extensions, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
HMAC-SHA-256 ✅ ✅ SHA extensions, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
HMAC-SHA-384 ✅ ✅ SHA512, AVX-512F, AVX-512VL, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
HMAC-SHA-512/224 ✅ ✅ SHA512, AVX-512F, AVX-512VL, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
HMAC-SHA-512/256 ✅ ✅ SHA512, AVX-512F, AVX-512VL, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
HMAC-SHA-512 ✅ ✅ SHA512, AVX-512F, AVX-512VL, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
Poly1305 ✅ ✅ AVX-512F, AVX-512 IFMA, AVX2 ✅ NEON ✅ umaal ✅ ✅ ✅
### Ciphers
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
3DES-CBC ✅ ✅ AVX-512F, AVX2 ✅ ✅ ✅ SSSE3 ✅ ✅
3DES-CFB64 ✅ ✅ AVX-512F, AVX2 ✅ ✅ ✅ SSSE3 ✅ ✅
3DES-CFB8 ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
3DES-ECB ✅ ✅ AVX-512F, AVX2 ✅ ✅ ✅ ✅ ✅
3DES-OFB ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
AES-CBC (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, AVX-512F, AVX2 ✅ AES, PMULL, SHA extensions, NEON ✅ ✅ AES-NI ✅ ✅
AES-CFB128 (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
AES-CFB8 (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
AES-CTR (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
AES-ECB (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
AES-OFB (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
XTS-AES (256-, 384- and 512-bit keys) ✅ ✅ AES-NI, VAES, PCLMULQDQ, VPCLMULQDQ, AVX-512F, AVX-512BW, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
Blowfish-CBC ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
Blowfish-CFB64 ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
Blowfish-ECB ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
Blowfish-OFB ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
Camellia-CBC (128-, 192- and 256-bit keys) ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
Camellia-CFB128 (128-, 192- and 256-bit keys) ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
Camellia-CTR (128-, 192- and 256-bit keys) ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
Camellia-ECB (128-, 192- and 256-bit keys) ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
Camellia-OFB (128-, 192- and 256-bit keys) ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
CAST5-CBC ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
CAST5-CFB64 ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
CAST5-ECB (CAST-128, 40- to 128-bit keys) ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
CAST5-OFB ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
ChaCha20 ✅ ✅ AVX-512F, AVX-512VL, AVX2 ✅ SVE2, NEON ✅ ✅ SSSE3 ✅ ✅
IDEA-CBC ✅ ✅ ✅ ✅ ✅ ✅ ✅
IDEA-CFB64 ✅ ✅ ✅ ✅ ✅ ✅ ✅
IDEA-ECB ✅ ✅ ✅ ✅ ✅ ✅ ✅
IDEA-OFB ✅ ✅ ✅ ✅ ✅ ✅ ✅
RC2-CBC ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
RC4 ✅ ✅ ✅ ✅ ✅ SSSE3 ✅ ✅
SEED-CBC ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
SEED-CFB128 ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
SEED-ECB ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
SEED-OFB ✅ ✅ ✅ ✅ ✅ AES-NI ✅ ✅
SM4-CBC ✅ ✅ ✅ SM4 ✅ ✅ AES-NI ✅ ✅
SM4-CFB128 ✅ ✅ ✅ SM4 ✅ ✅ AES-NI ✅ ✅
SM4-CTR ✅ ✅ ✅ SM4 ✅ ✅ AES-NI ✅ ✅
SM4-ECB ✅ ✅ ✅ SM4 ✅ ✅ AES-NI ✅ ✅
SM4-OFB ✅ ✅ ✅ SM4 ✅ ✅ AES-NI ✅ ✅
### AEADs
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
AES-CCM (128-, 192- and 256-bit keys) ✅ ✅ AES-NI ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
AES-GCM-SIV (128- and 256-bit keys) ✅ ✅ AES-NI, VAES, PCLMULQDQ, VPCLMULQDQ, AVX2 ✅ AES, PMULL, SHA extensions, NEON ✅ ✅ AES-NI, PCLMULQDQ ✅ ✅
AES-GCM (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, PCLMULQDQ, VPCLMULQDQ, AVX-512F, AVX-512BW, AVX2 ✅ AES, PMULL, SHA extensions, NEON ✅ ✅ AES-NI, PCLMULQDQ ✅ ✅
AES-OCB3 (128-, 192- and 256-bit keys) ✅ ✅ AES-NI, VAES, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
AES-SIV (256-, 384- and 512-bit keys) ✅ ✅ AES-NI, VAES, AVX2 ✅ AES, PMULL, NEON ✅ ✅ AES-NI ✅ ✅
ChaCha20-Poly1305 ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2 ✅ SVE2, NEON ✅ umaal ✅ SSSE3 ✅ ✅
SM4-GCM ✅ ✅ PCLMULQDQ, VPCLMULQDQ, AVX2 ✅ AES, PMULL, SM4 ✅ ✅ AES-NI, PCLMULQDQ ✅ ✅
### KDFs
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
Argon2d ✅ ✅ AVX-512F, AVX-512VL, AVX2 ✅ ✅ ✅ ✅ ✅
Argon2i ✅ ✅ AVX-512F, AVX-512VL, AVX2 ✅ ✅ ✅ ✅ ✅
Argon2id ✅ ✅ AVX-512F, AVX-512VL, AVX2 ✅ ✅ ✅ ✅ ✅
HKDF ✅ ✅ SHA extensions, SHA512, AVX-512F, AVX-512VL, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
PBKDF2-HMAC-MD5 ✅ ✅ ✅ ✅ ✅ ✅ ✅
PBKDF2-HMAC-SHA-1 ✅ ✅ SHA extensions, AVX, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
PBKDF2-HMAC-SHA-224 ✅ ✅ SHA extensions, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
PBKDF2-HMAC-SHA-256 ✅ ✅ SHA extensions, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
PBKDF2-HMAC-SHA-384 ✅ ✅ SHA512, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
PBKDF2-HMAC-SHA-512/224 ✅ ✅ SHA512, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
PBKDF2-HMAC-SHA-512/256 ✅ ✅ SHA512, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
PBKDF2-HMAC-SHA-512 ✅ ✅ SHA512, AVX2, BMI1, BMI2 ✅ SHA extensions ✅ ✅ ✅ ✅
scrypt ✅ ✅ SHA extensions, AVX2, BMI1, BMI2, SSE2 ✅ SHA extensions ✅ ✅ SHA extensions ✅ ✅
### KEMs
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
ML-KEM-1024 ✅ ✅ AVX-512F, AVX-512VL, AVX-512BW, AVX2 ✅ SHA extensions ✅ ✅ ✅ ✅
ML-KEM-768 ✅ ✅ AVX-512F, AVX-512VL, AVX-512BW, AVX2 ✅ SHA extensions ✅ ✅ ✅ ✅
### Key agreement
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
SEC 1 EC point compression ✅ ✅ BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
ECDH brainpoolP224r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDH brainpoolP256r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDH brainpoolP320r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDH brainpoolP384r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDH brainpoolP512r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDH P-192 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDH P-224 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDH P-256 ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
ECDH P-384 ✅ ✅ AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
ECDH P-521 ✅ ✅ AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
ECDH secp256k1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
Finite-field Diffie–Hellman (FFDH) ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
X25519 ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
X448 ✅ ✅ BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
### Signatures
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
ECDSA brainpoolP224r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDSA brainpoolP256r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDSA brainpoolP320r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDSA brainpoolP384r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDSA brainpoolP512r1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDSA P-192 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDSA P-224 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
ECDSA P-256 ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
ECDSA P-384 ✅ ✅ AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
ECDSA P-521 ✅ ✅ AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
ECDSA secp256k1 ✅ ✅ ✅ ✅ umaal ✅ ✅ ✅
Ed25519 ✅ ✅ SHA512, AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI1, BMI2, ADX ✅ SHA extensions ✅ umaal ✅ ✅ ✅
Ed448 ✅ ✅ BMI2, ADX ✅ SHA extensions ✅ umaal ✅ ✅ ✅
ML-DSA-44 ✅ ✅ AVX-512F, AVX-512VL, AVX2 ✅ SHA extensions ✅ ✅ ✅ ✅
ML-DSA-65 ✅ ✅ AVX-512F, AVX-512VL, AVX2 ✅ SHA extensions ✅ ✅ ✅ ✅
ML-DSA-87 ✅ ✅ AVX-512F, AVX-512VL, AVX2 ✅ SHA extensions ✅ ✅ ✅ ✅
### DSA
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
DSA key generation ✅ ✅ SHA extensions, AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI1, BMI2, ADX ✅ SHA extensions ✅ umaal ✅ SHA extensions ✅ ✅
DSA parameter generation ✅ ✅ SHA extensions, AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI1, BMI2, ADX ✅ SHA extensions ✅ umaal ✅ SHA extensions ✅ ✅
DSA ✅ ✅ SHA extensions, AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI1, BMI2, ADX ✅ SHA extensions ✅ umaal ✅ SHA extensions ✅ ✅
### RSA
Algorithm Spec landed x86-64 ARM64 ARMv7 x86 PPC64LE RISC-V
RSA key generation ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
RSAES-OAEP ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
RSASSA-PSS ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
RSA ✅ ✅ BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
RSAES-PKCS1-v1_5 (implicit rejection) ✅ ✅ SHA extensions, AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI1, BMI2, ADX ✅ SHA extensions ✅ umaal ✅ SHA extensions ✅ ✅
RSASSA-PKCS1-v1_5 ✅ ✅ AVX-512F, AVX-512 IFMA, AVX-512VL, AVX2, BMI2, ADX ✅ ✅ umaal ✅ ✅ ✅
The tables are generated from the code by `ci/algorithms_table.py`. * **Spec landed**: the algorithm's specification, transcribed from its standard, is in `lean/VerifiedGarbage/Spec/`. * **x86-64**, **ARM64**, **ARMv7**, **x86**, **PPC64LE**, **RISC-V**: ✅ when verified assembly and a public Rust API exist on that architecture, followed by the instruction set extensions of its other implementations, if it has any (e.g. SHA extensions, AVX2). They are detected at run time, and CPUs without them run the baseline implementation. NEON, which every ARM64 CPU has, names another implementation that needs nothing beyond the baseline. ## How it works * Each primitive is written in assembly, as a program over a Lean model of the target ISA, and proven in Lean to be correct against a specification, memory safe, and constant time (scrypt's ROMix is the exception its standard makes: it reads memory at indices derived from the password, and its contract declares that it leaks them and nothing else secret; see also [what the constant-time guarantee assumes](#what-the-constant-time-guarantee-assumes)). See [`lean/README.md`](lean/README.md) for the layout, the pipeline, and exactly what has to be trusted. * Constant time means that the sequence of instructions and memory addresses does not depend on secrets; that each instruction's own timing does not depend on its data is an assumption about the CPU, recorded in each ISA model (`lean/VerifiedGarbage/TCB//Isa.lean`). On x86 and x86-64 it rests on Intel's data operand independent timing guidance, which covers only Intel Core and Atom processors (not AMD's, VIA's or the Pentium 4's), holds on Intel processors from Ice Lake (Atom: Gracemont) on only if the operating system has set the DOITM bit, which user code cannot, and does not list the `VSHA512*` instructions that SHA-512 uses on CPUs with the SHA512 extension. * The proven assembly is emitted into [`crates/`](crates/) (a crate per architecture, `verified-garbage-asm-`) as Rust naked functions (`naked_asm!`); there is no build script and no separate assembler step. Naked functions' machine code is the same at every optimization level, and optimized, rustc's ThinLTO has LLVM parse their assembly twice more, so a crate depending on this one builds faster with them unoptimized, and in as many codegen units as it states (rustc merges the units of a crate of naked functions into one, assembled on one thread, unless their number is stated), as this repository's own builds are: ```toml [profile.release.package.verified-garbage-asm-x86_64] opt-level = 0 codegen-units = 16 ``` * The public APIs are Rust that composes these verified primitives (see [what is not verified](#what-is-not-verified)). * The public APIs are tested against the [Wycheproof](https://github.com/C2SP/wycheproof) test vectors (`tests/wycheproof/`). ### What the constant-time guarantee assumes * **AArch64 needs PSTATE.DIT set.** Arm specifies that the modelled instructions take a time independent of their data only while PSTATE.DIT (Data Independent Timing, `FEAT_DIT`) is 1; with DIT = 0, "the architecture makes no statement about the timing properties of any instructions" (Arm ARM, `DIT`). The proofs assume DIT = 1 (`lean/VerifiedGarbage/TCB/AArch64/Isa.lean`). Every operation that may process secrets sets it for the current thread while it runs, on CPUs with `FEAT_DIT` (which Linux reports as `HWCAP_DIT` and macOS as `hw.optional.arm.FEAT_DIT`), and restores it after (`src/dit.rs`, with the verified `vg_dit_enter` and `vg_dit_clear`; on Apple's targets with a speculation barrier after turning it on, `sb` or else `dsb sy; isb`, as Apple's documentation asks, though Arm requires none). Operations on public data alone (hashing without a key, verifying a signature) leave it as it is. A program making many short calls can set it once around them with `verified_garbage::dit::scope`. On CPUs without `FEAT_DIT`, Arm makes no timing statement at all. * **PPC64LE's multiplies are constant time only on the cores surveyed.** The Power ISA makes no architectural promise about the timing of any instruction. The model has no division and only the 64-bit multiplies `mulld` and `mulhdu`, which T. Pornin's [survey](https://www.bearssl.org/ctmul.html) found to be constant time on POWER8 and POWER9; it does not cover POWER10 or later (`lean/VerifiedGarbage/TCB/PPC64LE/Isa.lean`). * **Some leaks are hashes of secrets.** A constant-time theorem says that the code's secret-dependent behaviour (branches and memory addresses) is a function of what its contract says it may leak. For most functions that is nothing secret; a few leak values computed from secrets by SHAKE: ML-DSA key generation leaks `ρ` (from `H(ξ ‖ k ‖ ℓ)`) and which half-bytes of the `ρ′`-derived stream `ExpandS` rejects; ML-DSA signing leaks each iteration's commitment hash `c̃`, whether it was rejected, and the hint of the signature; ML-KEM key generation leaks `ρ` (from `G(d ‖ k)`) (`Spec/MlDsa/Contract.lean`, `Spec/MlKem/Contract.lean`). `ρ` is part of the public key and the last `c̃` and the hint of the signature, and the others are pseudorandom outputs of SHAKE that are never revealed, so these leaks are not believed to reveal anything useful about the secret. But since such a leak is (nearly) a one-to-one function of the secret input, "equal leaks imply equal timing" relates very few pairs of inputs: these theorems guarantee much less than "independent of the secret", and what the leak reveals is an argument about SHAKE, outside the proofs. * **Speculative execution is not modelled.** Each ISA model runs one instruction at a time, in program order, with each load seeing the latest store, and the constant-time theorems are about that sequential execution. A CPU may also run instructions transiently: past a mispredicted branch, call or return, or with a load that bypasses an earlier store to the same address. The transient instructions are then discarded, but the cache state and other effects they leave behind can still reveal data. These are the Spectre-class attacks (Spectre v1, v1.1, v2, v4 and return-stack-buffer variants, among others), and they are outside the current proofs: nothing here shows that the code is free of them, and the code contains no mitigations aimed at them. ### What is not verified The public APIs are Rust around the verified functions, and that Rust is tested, not proven. Most of it only lays out buffers, but in some algorithms it does part of the cryptography: * **AES-GCM**: the length limits of SP 800-38D §5.2.1.1 (a nonempty nonce, at most `2^36 − 32` bytes of text and `2^61 − 1` of additional data) and the order of the streaming calls are checked in Rust (`src/aead/aes_gcm.rs`); the mode itself, including `J0`, the padding, the length block and the comparison of the tag, is verified end to end, as are ChaCha20-Poly1305 and ML-KEM. * **HMAC** with a key longer than a block hashes it first, in Rust (with the verified hash). ### Unsupported platform features * **Stack unwinding.** The verified functions carry no unwind information on any platform: no DWARF CFI on ELF, no ARM EHABI tables, no Apple compact unwind, and no `.pdata`/`.xdata` on Windows. The proofs establish the calling convention on entry and return, not the state of the stack between them, and many functions save callee-saved registers and the return address in caller-provided scratch buffers, where Windows' unwind codes cannot describe them. Nothing inside them panics or calls back into Rust, so a Rust unwind never needs to pass through them. But a stack walk that starts inside one (a debugger, a sampling profiler, a crash reporter, Windows' `RtlVirtualUnwind`) may be truncated or recover the wrong caller, stack pointer or callee-saved registers. * **Branch target enforcement.** The functions do not begin with landing pads (`bti c` on AArch64, `endbr64` on x86-64), and the Rust code calls some of them through function pointers. Do not build this library with `-Zbranch-protection=bti` or `-Zcf-protection=branch`, or link it with `-z force-bti` or `-z force-ibt`: on a CPU that enforces BTI or IBT, those calls fault. (`-Zbranch-protection=pac-ret` does not break them, but the return addresses they save are not signed.) ## Development ```sh git clone https://github.com/C2SP/wycheproof WYCHEPROOF_ROOT=$PWD/wycheproof cargo test # without it, the Wycheproof tests are skipped cd lean lake exe cache get # prebuilt Mathlib lake build --log-level=warning # check all proofs (without each module's recorded profile) lake env lean --run Emit.lean # regenerate the crates of crates/ after changing lean/VerifiedGarbage/Artifacts/ ``` To benchmark against OpenSSL (through rust-openssl; needs its headers) and aws-lc-rs (built from source; needs a C compiler), and to compare a branch with a checkout of `main` (as the Benchmarks workflow does when run by hand, below): ```sh (cd bench && cargo bench) python3 ci/bench_compare.py path/to/main-checkout . # --openssl to add OpenSSL's and aws-lc-rs's times ``` For a targeted run, `VG_CPU_FEATURES` (see `crates/verified-garbage-cpu/src/detect.rs`) restricts the CPU features the library uses, `VG_BENCH_MODULES` the library modules whose benchmarks run, `VG_BENCHES` the benchmarks (the names of the files of `bench/benches/primitives/`) that run, and Criterion's filter the benchmark ids (`//`): ```sh (cd bench && VG_CPU_FEATURES=avx,avx2 VG_BENCH_MODULES=chacha20 cargo bench -- '^chacha20/verified-garbage/') VG_CPU_FEATURES=none python3 ci/bench_compare.py --modules chacha20 path/to/main-checkout . ``` The Benchmarks workflow runs only by hand, comparing a branch with any commit on runners of each architecture. Its runs take the architectures, the `VG_CPU_FEATURES` configurations (`-` for none) and the modules to run, so that an implementation can be measured on a runner of its architecture without running every benchmark. `git diff --name-only BASE HEAD | python3 ci/bench_arches.py --base BASE` prints what a change needs, and `python3 ci/bench_arches.py --choices` lists the values each takes: ```sh gh workflow run bench.yml --ref my-branch -f base_commit= \ -f arches=aarch64 -f cpu_features='- neon' -f modules=chacha20 ``` ## Credits This project is inspired by: - [Graviola](https://github.com/ctz/graviola/) - [s2n-bignum](https://github.com/awslabs/s2n-bignum) - [Bobby Powers](https://bpowers.net/) - [HACS Workshop](https://www.hacs-workshop.org)