Skip to content

Commit 8c391c3

Browse files
authored
Remove chacha feature (#13)
detect chacha availability using openssl config
1 parent 994ef83 commit 8c391c3

File tree

10 files changed

+48
-37
lines changed

10 files changed

+48
-37
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ rustls = { version = "0.23.0", default-features = false }
1717
rustls-webpki = { version = "0.102.2", default-features = false }
1818

1919
[features]
20-
default = ["tls12", "chacha", "x25519"]
20+
default = ["tls12", "x25519"]
2121
x25519 = []
22-
chacha = []
2322
tls12 = ["rustls/tls12", "foreign-types-shared"]
2423

2524
[dev-dependencies]

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ See the [rustls documentation]((https://docs.rs/rustls/latest/rustls/crypto/stru
1111
## Supported Ciphers
1212

1313
Supported cipher suites are listed below, in descending order of preference.
14+
If OpenSSL is compiled with the `OPENSSL_NO_CHACHA` option, the ChaCha20-Poly1305 ciphers will not be available.
1415

1516
### TLS 1.3
1617

@@ -19,7 +20,7 @@ The following cipher suites are supported for TLS 1.3. These support QUIC.
1920
```
2021
TLS13_AES_256_GCM_SHA384
2122
TLS13_AES_128_GCM_SHA256
22-
TLS13_CHACHA20_POLY1305_SHA256 // Requires the `chacha` feature
23+
TLS13_CHACHA20_POLY1305_SHA256
2324
```
2425

2526
### TLS 1.2
@@ -28,10 +29,10 @@ TLS13_CHACHA20_POLY1305_SHA256 // Requires the `chacha` feature
2829
```
2930
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
3031
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
31-
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 // Requires the `chacha` feature
32+
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
3233
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
3334
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
34-
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 // Requires the `chacha` feature
35+
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
3536
```
3637
## Supported Key Exchanges
3738

build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
use std::env;
44

5+
const OPENSSL_NO_CHACHA: &str = "OPENSSL_NO_CHACHA";
6+
57
fn main() {
8+
println!("cargo:rustc-check-cfg=cfg(chacha)");
69
// Determine whether to work around https://github.com/openssl/openssl/issues/23448
710
// according to the OpenSSL version
811
println!("cargo:rustc-check-cfg=cfg(bugged_add_hkdf_info)");
@@ -13,4 +16,12 @@ fn main() {
1316
println!("cargo:rustc-cfg=bugged_add_hkdf_info");
1417
}
1518
}
19+
20+
// Enable the `chacha` cfg if the `OPENSSL_NO_CHACHA` OpenSSL config is not set.
21+
if std::env::var("DEP_OPENSSL_CONF")
22+
.map(|conf_string| !conf_string.split(",").any(|conf| conf == OPENSSL_NO_CHACHA))
23+
.unwrap_or(true)
24+
{
25+
println!("cargo:rustc-cfg=chacha");
26+
}
1627
}

src/aead.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) struct MessageCrypter {
1818
pub(crate) enum Algorithm {
1919
Aes128Gcm,
2020
Aes256Gcm,
21-
#[cfg(feature = "chacha")]
21+
#[cfg(chacha)]
2222
ChaCha20Poly1305,
2323
}
2424

@@ -30,7 +30,7 @@ impl Algorithm {
3030
match self {
3131
Self::Aes128Gcm => Cipher::aes_128_gcm(),
3232
Self::Aes256Gcm => Cipher::aes_256_gcm(),
33-
#[cfg(feature = "chacha")]
33+
#[cfg(chacha)]
3434
Self::ChaCha20Poly1305 => Cipher::chacha20_poly1305(),
3535
}
3636
}

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@
55
//! ## Supported Ciphers
66
//!
77
//! Supported cipher suites are listed below, ordered by preference. IE: The default configuration prioritizes `TLS13_AES_256_GCM_SHA384` over `TLS13_AES_128_GCM_SHA256`.
8+
//! If OpenSSL is compiled with the `OPENSSL_NO_CHACHA` option, the ChaCha20-Poly1305 ciphers will not be available.
89
//!
910
//! ### TLS 1.3
1011
//!
1112
//! ```ignore
1213
//! TLS13_AES_256_GCM_SHA384
1314
//! TLS13_AES_128_GCM_SHA256
14-
//! TLS13_CHACHA20_POLY1305_SHA256 // Requires the `chacha` feature
15+
//! TLS13_CHACHA20_POLY1305_SHA256
1516
//! ```
1617
//!
1718
//! ### TLS 1.2
1819
//!
1920
//! ```ignore
2021
//! TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
2122
//! TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
22-
//! TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 // Requires the `chacha` feature
23+
//! TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
2324
//! TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
2425
//! TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
25-
//! TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 // Requires the `chacha` feature
26+
//! TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
2627
//! ```
2728
//! ## Supported Key Exchanges
2829
//!
@@ -97,7 +98,6 @@
9798
//!
9899
//! # Features
99100
//! The following non-default features are available:
100-
//! - `chacha`: Enables ChaCha20-Poly1305 cipher suites for TLS 1.2 and TLS 1.3.
101101
//! - `x25519`: Enables X25519 key exchange group.
102102
103103
// Mimic rustls code no_std usage.
@@ -131,11 +131,11 @@ pub mod cipher_suite {
131131
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
132132
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
133133
};
134-
#[cfg(all(feature = "tls12", feature = "chacha"))]
134+
#[cfg(all(feature = "tls12", chacha))]
135135
pub use super::tls12::{
136136
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
137137
};
138-
#[cfg(feature = "chacha")]
138+
#[cfg(chacha)]
139139
pub use super::tls13::TLS13_CHACHA20_POLY1305_SHA256;
140140
pub use super::tls13::{TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384};
141141
}
@@ -265,19 +265,19 @@ pub static DEFAULT_CIPHER_SUITES: &[SupportedCipherSuite] = ALL_CIPHER_SUITES;
265265
pub static ALL_CIPHER_SUITES: &[SupportedCipherSuite] = &[
266266
tls13::TLS13_AES_256_GCM_SHA384,
267267
tls13::TLS13_AES_128_GCM_SHA256,
268-
#[cfg(feature = "chacha")]
268+
#[cfg(chacha)]
269269
tls13::TLS13_CHACHA20_POLY1305_SHA256,
270270
#[cfg(feature = "tls12")]
271271
tls12::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
272272
#[cfg(feature = "tls12")]
273273
tls12::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
274-
#[cfg(all(feature = "tls12", feature = "chacha"))]
274+
#[cfg(all(feature = "tls12", chacha))]
275275
tls12::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
276276
#[cfg(feature = "tls12")]
277277
tls12::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
278278
#[cfg(feature = "tls12")]
279279
tls12::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
280-
#[cfg(all(feature = "tls12", feature = "chacha"))]
280+
#[cfg(all(feature = "tls12", chacha))]
281281
tls12::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
282282
];
283283

src/quic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct PacketKey {
2626
pub(crate) enum HeaderProtectionAlgorithm {
2727
Aes128,
2828
Aes256,
29-
#[cfg(feature = "chacha")]
29+
#[cfg(chacha)]
3030
ChaCha20,
3131
}
3232

@@ -175,7 +175,7 @@ impl HeaderProtectionAlgorithm {
175175
match self {
176176
HeaderProtectionAlgorithm::Aes128 => Cipher::aes_128_ecb(),
177177
HeaderProtectionAlgorithm::Aes256 => Cipher::aes_256_ecb(),
178-
#[cfg(feature = "chacha")]
178+
#[cfg(chacha)]
179179
HeaderProtectionAlgorithm::ChaCha20 => Cipher::chacha20(),
180180
}
181181
}
@@ -191,7 +191,7 @@ impl HeaderProtectionKey {
191191
.map_err(|e| Error::General(format!("OpenSSL error: {e}")))?;
192192
mask.copy_from_slice(&block[..5]);
193193
}
194-
#[cfg(feature = "chacha")]
194+
#[cfg(chacha)]
195195
// https://datatracker.ietf.org/doc/html/rfc9001#section-5.4.4
196196
HeaderProtectionAlgorithm::ChaCha20 => {
197197
let block = encrypt(
@@ -277,7 +277,7 @@ mod test {
277277
assert_eq!(server_packet[..], expected_server_packet[..]);
278278
}
279279

280-
#[cfg(feature = "chacha")]
280+
#[cfg(chacha)]
281281
#[test]
282282
fn test_short_packet_length() {
283283
use rustls::crypto::cipher::AeadKey;

src/tls12.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ const GCM_EXPLICIT_NONCE_LENGTH: usize = 8;
2121
const GCM_IMPLICIT_NONCE_LENGTH: usize = 4;
2222
const GCM_TAG_LENGTH: usize = 16;
2323

24-
#[cfg(feature = "chacha")]
24+
#[cfg(chacha)]
2525
const CHACHA_TAG_LENGTH: usize = 16;
26-
#[cfg(feature = "chacha")]
26+
#[cfg(chacha)]
2727
const CHAHCA_NONCE_LENGTH: usize = 12;
28-
#[cfg(feature = "chacha")]
28+
#[cfg(chacha)]
2929
const CHACHA_KEY_LENGTH: usize = 32;
3030

3131
/// The TLS1.2 ciphersuite `TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256`.
32-
#[cfg(feature = "chacha")]
32+
#[cfg(chacha)]
3333
pub static TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
3434
SupportedCipherSuite::Tls12(&Tls12CipherSuite {
3535
common: CipherSuiteCommon {
@@ -44,7 +44,7 @@ pub static TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
4444
});
4545

4646
/// The TLS1.2 ciphersuite `TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256`
47-
#[cfg(feature = "chacha")]
47+
#[cfg(chacha)]
4848
pub static TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
4949
SupportedCipherSuite::Tls12(&Tls12CipherSuite {
5050
common: CipherSuiteCommon {
@@ -122,16 +122,16 @@ pub static TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: SupportedCipherSuite =
122122
prf_provider: &Prf(SHA384),
123123
});
124124

125-
#[cfg(feature = "chacha")]
125+
#[cfg(chacha)]
126126
pub(crate) struct Tls12ChaCha;
127127

128-
#[cfg(feature = "chacha")]
128+
#[cfg(chacha)]
129129
pub(crate) struct Tls12ChaCha20Poly1305 {
130130
key: [u8; CHACHA_KEY_LENGTH],
131131
iv: Iv,
132132
}
133133

134-
#[cfg(feature = "chacha")]
134+
#[cfg(chacha)]
135135
impl Tls12AeadAlgorithm for Tls12ChaCha {
136136
fn encrypter(&self, key: AeadKey, iv: &[u8], _: &[u8]) -> Box<dyn MessageEncrypter> {
137137
// The caller ensures that the key is the correct length.
@@ -176,7 +176,7 @@ impl Tls12AeadAlgorithm for Tls12ChaCha {
176176
}
177177
}
178178

179-
#[cfg(feature = "chacha")]
179+
#[cfg(chacha)]
180180
impl MessageEncrypter for Tls12ChaCha20Poly1305 {
181181
fn encrypt(
182182
&mut self,
@@ -213,7 +213,7 @@ impl MessageEncrypter for Tls12ChaCha20Poly1305 {
213213
}
214214
}
215215

216-
#[cfg(feature = "chacha")]
216+
#[cfg(chacha)]
217217
impl MessageDecrypter for Tls12ChaCha20Poly1305 {
218218
fn decrypt<'a>(
219219
&mut self,

src/tls13.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use rustls::{
1414
};
1515

1616
/// The TLS1.3 ciphersuite `TLS_CHACHA20_POLY1305_SHA256`
17-
#[cfg(feature = "chacha")]
17+
#[cfg(chacha)]
1818
pub static TLS13_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
1919
SupportedCipherSuite::Tls13(TLS13_CHACHA20_POLY1305_SHA256_INTERNAL);
2020

21-
#[cfg(feature = "chacha")]
21+
#[cfg(chacha)]
2222
pub static TLS13_CHACHA20_POLY1305_SHA256_INTERNAL: &Tls13CipherSuite = &Tls13CipherSuite {
2323
common: CipherSuiteCommon {
2424
suite: CipherSuite::TLS13_CHACHA20_POLY1305_SHA256,
@@ -108,7 +108,7 @@ impl Tls13AeadAlgorithm for aead::Algorithm {
108108
Ok(match self {
109109
aead::Algorithm::Aes128Gcm => ConnectionTrafficSecrets::Aes128Gcm { key, iv },
110110
aead::Algorithm::Aes256Gcm => ConnectionTrafficSecrets::Aes256Gcm { key, iv },
111-
#[cfg(feature = "chacha")]
111+
#[cfg(chacha)]
112112
aead::Algorithm::ChaCha20Poly1305 => {
113113
ConnectionTrafficSecrets::Chacha20Poly1305 { key, iv }
114114
}

tests/0001-Patch-openssl-into-rustls.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ index 2192377f..071d9036 100644
2525
+
2626
[features]
2727
-default = ["aws_lc_rs", "logging", "std", "tls12"]
28-
+default = ["aws_lc_rs", "logging", "std", "tls12", "chacha", "x25519", "read_buf", "fips", "zlib"]
28+
+default = ["aws_lc_rs", "logging", "std", "tls12", "x25519"]
2929
std = ["webpki/std", "pki-types/std", "once_cell/std"]
3030
logging = ["log"]
3131
aws_lc_rs = ["dep:aws-lc-rs", "webpki/aws_lc_rs"]

tests/it.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ fn test_with_custom_config_to_internet(
196196
CipherSuite::TLS13_AES_256_GCM_SHA384
197197
)]
198198
#[cfg_attr(
199-
feature = "chacha",
199+
chacha,
200200
case::tls13_chacha20_poly1305_sha256(
201201
rustls_openssl::cipher_suite::TLS13_CHACHA20_POLY1305_SHA256,
202202
rustls_openssl::kx_group::SECP256R1,
@@ -233,7 +233,7 @@ fn test_with_custom_config_to_internet(
233233
CipherSuite::TLS13_AES_256_GCM_SHA384
234234
)]
235235
#[cfg_attr(
236-
all(feature = "tls12", feature = "chacha"),
236+
all(feature = "tls12", chacha),
237237
case::tls_ecdhe_rsa_with_chacha20_poly1305_sha256(
238238
rustls_openssl::cipher_suite::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
239239
rustls_openssl::kx_group::SECP256R1,
@@ -264,7 +264,7 @@ fn test_tls(
264264

265265
#[rstest]
266266
#[cfg_attr(
267-
feature = "chacha",
267+
chacha,
268268
case(
269269
rustls_openssl::cipher_suite::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
270270
rustls_openssl::kx_group::SECP384R1,

0 commit comments

Comments
 (0)