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(feature = "std")]
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_2048_8192_SHA256_ABSENT_PARAMS,
175        webpki_algs::RSA_PKCS1_2048_8192_SHA384_ABSENT_PARAMS,
176        webpki_algs::RSA_PKCS1_2048_8192_SHA512_ABSENT_PARAMS,
177    ],
178    mapping: &[
179        // Note: for TLS1.2 the curve is not fixed by SignatureScheme. For TLS1.3 it is.
180        (
181            SignatureScheme::ECDSA_NISTP384_SHA384,
182            &[
183                webpki_algs::ECDSA_P384_SHA384,
184                webpki_algs::ECDSA_P256_SHA384,
185                webpki_algs::ECDSA_P521_SHA384,
186            ],
187        ),
188        (
189            SignatureScheme::ECDSA_NISTP256_SHA256,
190            &[
191                webpki_algs::ECDSA_P256_SHA256,
192                webpki_algs::ECDSA_P384_SHA256,
193                webpki_algs::ECDSA_P521_SHA256,
194            ],
195        ),
196        (
197            SignatureScheme::ECDSA_NISTP521_SHA512,
198            &[webpki_algs::ECDSA_P521_SHA512],
199        ),
200        (SignatureScheme::ED25519, &[webpki_algs::ED25519]),
201        (
202            SignatureScheme::RSA_PSS_SHA512,
203            &[webpki_algs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY],
204        ),
205        (
206            SignatureScheme::RSA_PSS_SHA384,
207            &[webpki_algs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY],
208        ),
209        (
210            SignatureScheme::RSA_PSS_SHA256,
211            &[webpki_algs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY],
212        ),
213        (
214            SignatureScheme::RSA_PKCS1_SHA512,
215            &[webpki_algs::RSA_PKCS1_2048_8192_SHA512],
216        ),
217        (
218            SignatureScheme::RSA_PKCS1_SHA384,
219            &[webpki_algs::RSA_PKCS1_2048_8192_SHA384],
220        ),
221        (
222            SignatureScheme::RSA_PKCS1_SHA256,
223            &[webpki_algs::RSA_PKCS1_2048_8192_SHA256],
224        ),
225    ],
226};
227
228/// All defined key exchange groups supported by aws-lc-rs appear in this module.
229///
230/// [`ALL_KX_GROUPS`] is provided as an array of all of these values.
231/// [`DEFAULT_KX_GROUPS`] is provided as an array of this provider's defaults.
232pub mod kx_group {
233    pub use super::kx::{SECP256R1, SECP384R1, X25519};
234    pub use super::pq::{MLKEM768, SECP256R1MLKEM768, X25519MLKEM768};
235}
236
237/// A list of the default key exchange groups supported by this provider.
238///
239/// This does not contain MLKEM768; by default MLKEM768 is only offered
240/// in hybrid with X25519.
241pub static DEFAULT_KX_GROUPS: &[&dyn SupportedKxGroup] = &[
242    #[cfg(feature = "prefer-post-quantum")]
243    kx_group::X25519MLKEM768,
244    kx_group::X25519,
245    kx_group::SECP256R1,
246    kx_group::SECP384R1,
247    #[cfg(not(feature = "prefer-post-quantum"))]
248    kx_group::X25519MLKEM768,
249];
250
251/// A list of all the key exchange groups supported by this provider.
252pub static ALL_KX_GROUPS: &[&dyn SupportedKxGroup] = &[
253    #[cfg(feature = "prefer-post-quantum")]
254    kx_group::X25519MLKEM768,
255    #[cfg(feature = "prefer-post-quantum")]
256    kx_group::SECP256R1MLKEM768,
257    kx_group::X25519,
258    kx_group::SECP256R1,
259    kx_group::SECP384R1,
260    #[cfg(not(feature = "prefer-post-quantum"))]
261    kx_group::X25519MLKEM768,
262    #[cfg(not(feature = "prefer-post-quantum"))]
263    kx_group::SECP256R1MLKEM768,
264    kx_group::MLKEM768,
265];
266
267#[cfg(feature = "std")]
268pub use ticketer::Ticketer;
269
270/// Compatibility shims between ring 0.16.x and 0.17.x API
271mod ring_shim {
272    use super::ring_like;
273    use crate::crypto::SharedSecret;
274
275    pub(super) fn agree_ephemeral(
276        priv_key: ring_like::agreement::EphemeralPrivateKey,
277        peer_key: &ring_like::agreement::UnparsedPublicKey<&[u8]>,
278    ) -> Result<SharedSecret, ()> {
279        ring_like::agreement::agree_ephemeral(priv_key, peer_key, (), |secret| {
280            Ok(SharedSecret::from(secret))
281        })
282    }
283}
284
285/// Are we in FIPS mode?
286pub(super) fn fips() -> bool {
287    aws_lc_rs::try_fips_mode().is_ok()
288}
289
290pub(super) fn unspecified_err(_e: aws_lc_rs::error::Unspecified) -> Error {
291    #[cfg(feature = "std")]
292    {
293        Error::Other(OtherError(Arc::new(_e)))
294    }
295    #[cfg(not(feature = "std"))]
296    {
297        Error::Other(OtherError())
298    }
299}
300
301#[cfg(test)]
302mod tests {
303    #[cfg(feature = "fips")]
304    #[test]
305    fn default_suites_are_fips() {
306        assert!(
307            super::DEFAULT_CIPHER_SUITES
308                .iter()
309                .all(|scs| scs.fips())
310        );
311    }
312
313    #[cfg(not(feature = "fips"))]
314    #[test]
315    fn default_suites() {
316        assert_eq!(super::DEFAULT_CIPHER_SUITES, super::ALL_CIPHER_SUITES);
317    }
318}