1#![deny(missing_docs)]
24#![forbid(unsafe_code)]
25
26mod primitives;
27
28#[cfg(feature = "provider")]
29mod aead;
30#[cfg(feature = "provider")]
31mod hash;
32#[cfg(feature = "provider")]
33mod hkdf;
34#[cfg(feature = "provider")]
35mod hmac;
36#[cfg(feature = "provider")]
37mod kx;
38#[cfg(feature = "provider")]
39mod random;
40#[cfg(feature = "provider")]
41mod sign;
42#[cfg(feature = "provider")]
43mod verify;
44
45pub use primitives::{
46 Sha1, Sha256, base64_decode, base64_encode, constant_time_eq_32, hmac_sha256, sha1, sha256,
47};
48
49#[cfg(feature = "provider")]
50use std::sync::Arc;
51
52#[cfg(feature = "provider")]
53use rustls::crypto::{CryptoProvider, KeyProvider};
54#[cfg(feature = "provider")]
55use rustls::pki_types::PrivateKeyDer;
56#[cfg(feature = "provider")]
57use rustls::sign::SigningKey;
58#[cfg(feature = "provider")]
59use rustls::{CipherSuite, CipherSuiteCommon, Error, SupportedCipherSuite, Tls13CipherSuite};
60
61#[cfg(feature = "provider")]
62#[must_use]
71pub fn provider() -> CryptoProvider {
72 CryptoProvider {
73 cipher_suites: vec![
74 SupportedCipherSuite::Tls13(&TLS13_CHACHA20_POLY1305_SHA256_SUITE),
75 SupportedCipherSuite::Tls13(&TLS13_AES_128_GCM_SHA256_SUITE),
76 SupportedCipherSuite::Tls13(&TLS13_AES_256_GCM_SHA384_SUITE),
77 ],
78 kx_groups: vec![&kx::X25519, &kx::P256],
79 signature_verification_algorithms: verify::ALGORITHMS,
80 secure_random: &random::Getrandom,
81 key_provider: &RustCryptoKeyProvider,
82 }
83}
84
85#[cfg(feature = "provider")]
86static TLS13_CHACHA20_POLY1305_SHA256_SUITE: Tls13CipherSuite = Tls13CipherSuite {
87 common: CipherSuiteCommon {
88 suite: CipherSuite::TLS13_CHACHA20_POLY1305_SHA256,
89 hash_provider: &hash::SHA256,
90 confidentiality_limit: u64::MAX,
91 },
92 hkdf_provider: &hkdf::HKDF_SHA256,
93 aead_alg: &aead::ChaCha20Poly1305,
94 quic: None,
95};
96
97#[cfg(feature = "provider")]
98static TLS13_AES_128_GCM_SHA256_SUITE: Tls13CipherSuite = Tls13CipherSuite {
99 common: CipherSuiteCommon {
100 suite: CipherSuite::TLS13_AES_128_GCM_SHA256,
101 hash_provider: &hash::SHA256,
102 confidentiality_limit: 1 << 23,
103 },
104 hkdf_provider: &hkdf::HKDF_SHA256,
105 aead_alg: &aead::Aes128Gcm,
106 quic: None,
107};
108
109#[cfg(feature = "provider")]
110static TLS13_AES_256_GCM_SHA384_SUITE: Tls13CipherSuite = Tls13CipherSuite {
111 common: CipherSuiteCommon {
112 suite: CipherSuite::TLS13_AES_256_GCM_SHA384,
113 hash_provider: &hash::SHA384,
114 confidentiality_limit: 1 << 23,
115 },
116 hkdf_provider: &hkdf::HKDF_SHA384,
117 aead_alg: &aead::Aes256Gcm,
118 quic: None,
119};
120
121#[cfg(feature = "provider")]
122#[derive(Debug)]
128struct RustCryptoKeyProvider;
129
130#[cfg(feature = "provider")]
131impl KeyProvider for RustCryptoKeyProvider {
132 fn load_private_key(
133 &self,
134 key_der: PrivateKeyDer<'static>,
135 ) -> Result<Arc<dyn SigningKey>, Error> {
136 sign::any_supported_type(&key_der)
137 }
138}