rustls/crypto/aws_lc_rs/
mod.rs

1use alloc::vec::Vec;
2
3// aws-lc-rs has a -- roughly -- ring-compatible API, so we just reuse all that
4// glue here.  The shared files should always use `super::ring_like` to access a
5// ring-compatible crate, and `super::ring_shim` to bridge the gaps where they are
6// small.
7pub(crate) use aws_lc_rs as ring_like;
8use pki_types::PrivateKeyDer;
9use webpki::aws_lc_rs as webpki_algs;
10
11use crate::crypto::{CryptoProvider, KeyProvider, SecureRandom, SupportedKxGroup};
12use crate::enums::SignatureScheme;
13use crate::rand::GetRandomFailed;
14use crate::sign::SigningKey;
15use crate::suites::SupportedCipherSuite;
16use crate::sync::Arc;
17use crate::webpki::WebPkiSupportedAlgorithms;
18use crate::{Error, OtherError};
19
20/// Hybrid public key encryption (HPKE).
21pub mod hpke;
22/// Post-quantum secure algorithms.
23pub(crate) mod pq;
24/// Using software keys for authentication.
25pub mod sign;
26
27#[path = "../ring/hash.rs"]
28pub(crate) mod hash;
29#[path = "../ring/hmac.rs"]
30pub(crate) mod hmac;
31#[path = "../ring/kx.rs"]
32pub(crate) mod kx;
33#[path = "../ring/quic.rs"]
34pub(crate) mod quic;
35#[cfg(any(feature = "std", feature = "hashbrown"))]
36pub(crate) mod ticketer;
37#[cfg(feature = "tls12")]
38pub(crate) mod tls12;
39pub(crate) mod tls13;
40
41/// A `CryptoProvider` backed by aws-lc-rs.
42pub fn default_provider() -> CryptoProvider {
43    CryptoProvider {
44        cipher_suites: DEFAULT_CIPHER_SUITES.to_vec(),
45        kx_groups: default_kx_groups(),
46        signature_verification_algorithms: SUPPORTED_SIG_ALGS,
47        secure_random: &AwsLcRs,
48        key_provider: &AwsLcRs,
49    }
50}
51
52fn default_kx_groups() -> Vec<&'static dyn SupportedKxGroup> {
53    #[cfg(feature = "fips")]
54    {
55        DEFAULT_KX_GROUPS
56            .iter()
57            .filter(|cs| cs.fips())
58            .copied()
59            .collect()
60    }
61    #[cfg(not(feature = "fips"))]
62    {
63        DEFAULT_KX_GROUPS.to_vec()
64    }
65}
66
67#[derive(Debug)]
68struct AwsLcRs;
69
70impl SecureRandom for AwsLcRs {
71    fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> {
72        use ring_like::rand::SecureRandom;
73
74        ring_like::rand::SystemRandom::new()
75            .fill(buf)
76            .map_err(|_| GetRandomFailed)
77    }
78
79    fn fips(&self) -> bool {
80        fips()
81    }
82}
83
84impl KeyProvider for AwsLcRs {
85    fn load_private_key(
86        &self,
87        key_der: PrivateKeyDer<'static>,
88    ) -> Result<Arc<dyn SigningKey>, Error> {
89        sign::any_supported_type(&key_der)
90    }
91
92    fn fips(&self) -> bool {
93        fips()
94    }
95}
96
97/// The cipher suite configuration that an application should use by default.
98///
99/// This will be [`ALL_CIPHER_SUITES`] sans any supported cipher suites that
100/// shouldn't be enabled by most applications.
101pub static DEFAULT_CIPHER_SUITES: &[SupportedCipherSuite] = &[
102    // TLS1.3 suites
103    tls13::TLS13_AES_256_GCM_SHA384,
104    tls13::TLS13_AES_128_GCM_SHA256,
105    #[cfg(not(feature = "fips"))]
106    tls13::TLS13_CHACHA20_POLY1305_SHA256,
107    // TLS1.2 suites
108    #[cfg(feature = "tls12")]
109    tls12::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
110    #[cfg(feature = "tls12")]
111    tls12::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
112    #[cfg(all(feature = "tls12", not(feature = "fips")))]
113    tls12::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
114    #[cfg(feature = "tls12")]
115    tls12::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
116    #[cfg(feature = "tls12")]
117    tls12::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
118    #[cfg(all(feature = "tls12", not(feature = "fips")))]
119    tls12::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
120];
121
122/// A list of all the cipher suites supported by the rustls aws-lc-rs provider.
123pub static ALL_CIPHER_SUITES: &[SupportedCipherSuite] = &[
124    // TLS1.3 suites
125    tls13::TLS13_AES_256_GCM_SHA384,
126    tls13::TLS13_AES_128_GCM_SHA256,
127    tls13::TLS13_CHACHA20_POLY1305_SHA256,
128    // TLS1.2 suites
129    #[cfg(feature = "tls12")]
130    tls12::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
131    #[cfg(feature = "tls12")]
132    tls12::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
133    #[cfg(feature = "tls12")]
134    tls12::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
135    #[cfg(feature = "tls12")]
136    tls12::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
137    #[cfg(feature = "tls12")]
138    tls12::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
139    #[cfg(feature = "tls12")]
140    tls12::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
141];
142
143/// All defined cipher suites supported by aws-lc-rs appear in this module.
144pub mod cipher_suite {
145    #[cfg(feature = "tls12")]
146    pub use super::tls12::{
147        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
148        TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
149        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
150    };
151    pub use super::tls13::{
152        TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
153    };
154}
155
156/// A `WebPkiSupportedAlgorithms` value that reflects webpki's capabilities when
157/// compiled against aws-lc-rs.
158static SUPPORTED_SIG_ALGS: WebPkiSupportedAlgorithms = WebPkiSupportedAlgorithms {
159    all: &[
160        webpki_algs::ECDSA_P256_SHA256,
161        webpki_algs::ECDSA_P256_SHA384,
162        webpki_algs::ECDSA_P384_SHA256,
163        webpki_algs::ECDSA_P384_SHA384,
164        webpki_algs::ECDSA_P521_SHA256,
165        webpki_algs::ECDSA_P521_SHA384,
166        webpki_algs::ECDSA_P521_SHA512,
167        webpki_algs::ED25519,
168        webpki_algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
169        webpki_algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
170        webpki_algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
171        webpki_algs::RSA_PKCS1_2048_8192_SHA256,
172        webpki_algs::RSA_PKCS1_2048_8192_SHA384,
173        webpki_algs::RSA_PKCS1_2048_8192_SHA512,
174        webpki_algs::RSA_PKCS1_3072_8192_SHA384,
175    ],
176    mapping: &[
177        // Note: for TLS1.2 the curve is not fixed by SignatureScheme. For TLS1.3 it is.
178        (
179            SignatureScheme::ECDSA_NISTP384_SHA384,
180            &[
181                webpki_algs::ECDSA_P384_SHA384,
182                webpki_algs::ECDSA_P256_SHA384,
183                webpki_algs::ECDSA_P521_SHA384,
184            ],
185        ),
186        (
187            SignatureScheme::ECDSA_NISTP256_SHA256,
188            &[
189                webpki_algs::ECDSA_P256_SHA256,
190                webpki_algs::ECDSA_P384_SHA256,
191                webpki_algs::ECDSA_P521_SHA256,
192            ],
193        ),
194        (
195            SignatureScheme::ECDSA_NISTP521_SHA512,
196            &[webpki_algs::ECDSA_P521_SHA512],
197        ),
198        (SignatureScheme::ED25519, &[webpki_algs::ED25519]),
199        (
200            SignatureScheme::RSA_PSS_SHA512,
201            &[webpki_algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY],
202        ),
203        (
204            SignatureScheme::RSA_PSS_SHA384,
205            &[webpki_algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY],
206        ),
207        (
208            SignatureScheme::RSA_PSS_SHA256,
209            &[webpki_algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY],
210        ),
211        (
212            SignatureScheme::RSA_PKCS1_SHA512,
213            &[webpki_algs::RSA_PKCS1_2048_8192_SHA512],
214        ),
215        (
216            SignatureScheme::RSA_PKCS1_SHA384,
217            &[webpki_algs::RSA_PKCS1_2048_8192_SHA384],
218        ),
219        (
220            SignatureScheme::RSA_PKCS1_SHA256,
221            &[webpki_algs::RSA_PKCS1_2048_8192_SHA256],
222        ),
223    ],
224};
225
226/// All defined key exchange groups supported by aws-lc-rs appear in this module.
227///
228/// [`ALL_KX_GROUPS`] is provided as an array of all of these values.
229/// [`DEFAULT_KX_GROUPS`] is provided as an array of this provider's defaults.
230pub mod kx_group {
231    pub use super::kx::{SECP256R1, SECP384R1, X25519};
232    pub use super::pq::{MLKEM768, X25519MLKEM768};
233}
234
235/// A list of the default key exchange groups supported by this provider.
236///
237/// This does not contain MLKEM768; by default MLKEM768 is only offered
238/// in hybrid with X25519.
239pub static DEFAULT_KX_GROUPS: &[&dyn SupportedKxGroup] = &[
240    #[cfg(feature = "prefer-post-quantum")]
241    kx_group::X25519MLKEM768,
242    kx_group::X25519,
243    kx_group::SECP256R1,
244    kx_group::SECP384R1,
245    #[cfg(not(feature = "prefer-post-quantum"))]
246    kx_group::X25519MLKEM768,
247];
248
249/// A list of all the key exchange groups supported by this provider.
250pub static ALL_KX_GROUPS: &[&dyn SupportedKxGroup] = &[
251    #[cfg(feature = "prefer-post-quantum")]
252    kx_group::X25519MLKEM768,
253    kx_group::X25519,
254    kx_group::SECP256R1,
255    kx_group::SECP384R1,
256    #[cfg(not(feature = "prefer-post-quantum"))]
257    kx_group::X25519MLKEM768,
258    kx_group::MLKEM768,
259];
260
261#[cfg(any(feature = "std", feature = "hashbrown"))]
262pub use ticketer::Ticketer;
263
264/// Compatibility shims between ring 0.16.x and 0.17.x API
265mod ring_shim {
266    use super::ring_like;
267    use crate::crypto::SharedSecret;
268
269    pub(super) fn agree_ephemeral(
270        priv_key: ring_like::agreement::EphemeralPrivateKey,
271        peer_key: &ring_like::agreement::UnparsedPublicKey<&[u8]>,
272    ) -> Result<SharedSecret, ()> {
273        ring_like::agreement::agree_ephemeral(priv_key, peer_key, (), |secret| {
274            Ok(SharedSecret::from(secret))
275        })
276    }
277}
278
279/// Are we in FIPS mode?
280pub(super) fn fips() -> bool {
281    aws_lc_rs::try_fips_mode().is_ok()
282}
283
284pub(super) fn unspecified_err(_e: aws_lc_rs::error::Unspecified) -> Error {
285    #[cfg(feature = "std")]
286    {
287        Error::Other(OtherError(Arc::new(_e)))
288    }
289    #[cfg(not(feature = "std"))]
290    {
291        Error::Other(OtherError())
292    }
293}
294
295#[cfg(test)]
296mod tests {
297    #[cfg(feature = "fips")]
298    #[test]
299    fn default_suites_are_fips() {
300        assert!(
301            super::DEFAULT_CIPHER_SUITES
302                .iter()
303                .all(|scs| scs.fips())
304        );
305    }
306
307    #[cfg(not(feature = "fips"))]
308    #[test]
309    fn default_suites() {
310        assert_eq!(super::DEFAULT_CIPHER_SUITES, super::ALL_CIPHER_SUITES);
311    }
312}