Skip to main content

rustls_verified_garbage/
lib.rs

1//! A rustls [`CryptoProvider`] whose cryptography is verified-garbage's:
2//! formally verified assembly, generated from Lean.
3//!
4//! It offers what rustls' aws-lc-rs provider does: TLS 1.2 and 1.3 with
5//! AES-GCM and ChaCha20-Poly1305; X25519, P-256, P-384, ML-KEM and the
6//! X25519MLKEM768 and SECP256R1MLKEM768 hybrids; signatures with RSA
7//! (PKCS #1 v1.5 and PSS), ECDSA (P-256, P-384, P-521), Ed25519 and ML-DSA;
8//! session tickets; and QUIC's packet and header protection. Keys are read
9//! with rust-asn1. HPKE (for ECH) is not provided.
10//!
11//! Use it like:
12//!
13//! ```
14//! # use std::sync::Arc;
15//! # use rustls::{ClientConfig, RootCertStore};
16//! # let roots = RootCertStore::empty();
17//! let config = ClientConfig::builder(Arc::new(rustls_verified_garbage::DEFAULT_PROVIDER))
18//!     .with_root_certificates(roots)
19//!     .with_no_client_auth();
20//! ```
21//!
22//! To offer other suites or groups, replace [`DEFAULT_PROVIDER`]'s with those
23//! of [`cipher_suite`] or [`kx_group`].
24
25#![no_std]
26#![warn(missing_docs)]
27
28extern crate alloc;
29#[cfg(any(feature = "std", test))]
30extern crate std;
31
32use alloc::borrow::Cow;
33use alloc::boxed::Box;
34use alloc::sync::Arc;
35#[cfg(feature = "std")]
36use core::time::Duration;
37
38use pki_types::PrivateKeyDer;
39use rustls::crypto::{
40    CryptoProvider, GetRandomFailed, KeyProvider, SecureRandom, SigningKey, TicketProducer,
41    TicketerFactory,
42};
43use rustls::error::Error;
44#[cfg(feature = "std")]
45use rustls::ticketer::TicketRotator;
46
47mod aead;
48mod der;
49mod hash;
50mod hkdf;
51mod hmac;
52mod kx;
53mod quic;
54mod sign;
55#[cfg(feature = "std")]
56mod ticketer;
57mod tls12;
58mod tls13;
59mod verify;
60
61pub use verify::ALL_VERIFICATION_ALGS;
62
63/// The default `CryptoProvider` backed by verified-garbage.
64pub const DEFAULT_PROVIDER: CryptoProvider = CryptoProvider {
65    tls12_cipher_suites: Cow::Borrowed(tls12::ALL_TLS12_CIPHER_SUITES),
66    tls13_cipher_suites: Cow::Borrowed(tls13::ALL_TLS13_CIPHER_SUITES),
67    kx_groups: Cow::Borrowed(kx::DEFAULT_KX_GROUPS),
68    signature_verification_algorithms: verify::SUPPORTED_SIG_ALGS,
69    secure_random: &VerifiedGarbage,
70    key_provider: &VerifiedGarbage,
71    ticketer_factory: &VerifiedGarbage,
72};
73
74/// The default `CryptoProvider` backed by verified-garbage that only
75/// supports TLS1.3.
76pub const DEFAULT_TLS13_PROVIDER: CryptoProvider = CryptoProvider {
77    tls12_cipher_suites: Cow::Borrowed(&[]),
78    ..DEFAULT_PROVIDER
79};
80
81/// The default `CryptoProvider` backed by verified-garbage that only
82/// supports TLS1.2.
83///
84/// Use of TLS1.3 is **strongly** recommended.
85pub const DEFAULT_TLS12_PROVIDER: CryptoProvider = CryptoProvider {
86    tls13_cipher_suites: Cow::Borrowed(&[]),
87    ..DEFAULT_PROVIDER
88};
89
90#[derive(Debug)]
91struct VerifiedGarbage;
92
93impl SecureRandom for VerifiedGarbage {
94    fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> {
95        // verified-garbage draws its own randomness (keys, nonces, salts)
96        // from the operating system, as this does.
97        getrandom::fill(buf).map_err(|_| GetRandomFailed)
98    }
99}
100
101impl KeyProvider for VerifiedGarbage {
102    fn load_private_key(
103        &self,
104        key_der: PrivateKeyDer<'static>,
105    ) -> Result<Box<dyn SigningKey>, Error> {
106        let key_der = zeroize::Zeroizing::new(key_der);
107        sign::load_private_key(&key_der)
108    }
109}
110
111impl TicketerFactory for VerifiedGarbage {
112    /// Make the recommended `Ticketer`.
113    ///
114    /// This produces tickets:
115    ///
116    /// - where each lasts for at least 6 hours,
117    /// - with randomly generated keys, and
118    /// - where keys are rotated every 6 hours.
119    ///
120    /// The encryption mechanism used is AES-256-GCM.
121    fn ticketer(&self) -> Result<Arc<dyn TicketProducer>, Error> {
122        #[cfg(feature = "std")]
123        {
124            let rotator = TicketRotator::new(SIX_HOURS, ticketer::AeadTicketer::new)?;
125            Ok(Arc::new(rotator))
126        }
127        #[cfg(not(feature = "std"))]
128        {
129            Err(Error::General(
130                "VerifiedGarbage::ticketer() relies on std-only RwLock via TicketRotator".into(),
131            ))
132        }
133    }
134}
135
136#[cfg(feature = "std")]
137const SIX_HOURS: Duration = Duration::from_secs(6 * 60 * 60);
138
139/// The cipher suites this provider supports.
140///
141/// [`DEFAULT_PROVIDER`] offers all of them.
142pub mod cipher_suite {
143    pub use super::tls12::{
144        ALL_TLS12_CIPHER_SUITES, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
145        TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
146        TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
147        TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
148    };
149    pub use super::tls13::{
150        ALL_TLS13_CIPHER_SUITES, TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384,
151        TLS13_CHACHA20_POLY1305_SHA256,
152    };
153}
154
155/// The key exchange groups this provider supports.
156///
157/// The default providers offer [`X25519MLKEM768`](kx_group::X25519MLKEM768),
158/// [`X25519`](kx_group::X25519), [`SECP256R1`](kx_group::SECP256R1) and
159/// [`SECP384R1`](kx_group::SECP384R1), in that order of preference.
160pub mod kx_group {
161    pub use super::kx::{
162        ALL_KX_GROUPS, MLKEM768, MLKEM1024, SECP256R1, SECP256R1MLKEM768, SECP384R1, X25519,
163        X25519MLKEM768,
164    };
165}