Skip to content

Commit 720d48f

Browse files
Structural refactor: code part
1 parent 992778d commit 720d48f

51 files changed

Lines changed: 4432 additions & 1415 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 890 additions & 248 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 235 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,250 @@ readme = "README.md"
1111
repository = "https://github.com/RustCrypto/rustls-rustcrypto"
1212
categories = ["cryptography", "no-std"]
1313
keywords = ["rustls", "tls"]
14-
edition = "2021"
15-
rust-version = "1.75"
16-
resolver = "1" # Hack to enable the `custom` feature of `getrandom`
14+
edition = "2024"
15+
rust-version = "1.88.0"
16+
resolver = "2"
1717

1818
# Ensure all dependencies + feats are mapped to crate features for correct usage
1919
# default features often have std breaking no_std and potentially other unwanted
2020
[dependencies]
21-
aead = { version = "0.5.2", default-features = false }
22-
aes-gcm = { version = "0.10.3", default-features = false, features = ["aes", "alloc"] }
23-
chacha20poly1305 = { version = "0.10.1", default-features = false }
24-
crypto-common = { version = "0.1.6", default-features = false }
25-
der = { version = "0.7.9", default-features = false }
26-
digest = { version = "0.10.7", default-features = false }
27-
ecdsa = { version = "0.16.8", default-features = false, features = ["alloc"] }
28-
ed25519-dalek = { version = "2", default-features = false, features = ["pkcs8"] }
29-
hmac = { version = "0.12.1", default-features = false }
30-
p256 = { version = "0.13.2", default-features = false, features = ["pem", "ecdsa", "ecdh"] }
31-
p384 = { version = "0.13.0", default-features = false, features = ["pem", "ecdsa", "ecdh"] }
32-
paste = { version = "1.0.15", default-features = false }
33-
pkcs8 = { version = "0.10.2", default-features = false, features = ["pem", "pkcs5"] }
34-
pki-types = { package = "rustls-pki-types", version = "1.0.1", default-features = false }
35-
rand_core = { version = "0.6.4", default-features = false, features = ["getrandom"] }
36-
rsa = { version = "0.9.2", default-features = false, features = ["sha2"] }
37-
rustls = { version = "0.23.12", default-features = false }
38-
sec1 = { version = "0.7.3", default-features = false, features = ["pkcs8", "pem"] }
39-
sha2 = { version = "0.10.7", default-features = false }
40-
signature = { version = "2.1.0", default-features = false }
41-
webpki = { package = "rustls-webpki", version = "0.102.0", default-features = false }
42-
x25519-dalek = { version = "2", default-features = false }
21+
# Cryptographic dependencies
22+
aead = { version = "0.6.0-rc.5", default-features = false, optional = true }
23+
aes = { version = "0.9.0-rc.2", default-features = false, optional = true }
24+
aes-gcm = { version = "0.11.0-rc.2", default-features = false, optional = true }
25+
ccm = { version = "0.6.0-rc.2", default-features = false, optional = true }
26+
chacha20 = { version = "0.10.0-rc.6", default-features = false, optional = true }
27+
chacha20poly1305 = { version = "0.11.0-rc.2", default-features = false, optional = true }
28+
cipher = "0.5.0-rc.3"
29+
crypto-common = { version = "0.2.0-rc.9", default-features = false }
30+
der = { version = "0.8.0-rc.10", default-features = false, optional = true }
31+
digest = { version = "0.11.0-rc.5", default-features = false }
32+
ecdsa = { version = "0.17.0-rc.11", default-features = false, optional = true }
33+
ed25519-dalek = { version = "3.0.0-pre.4", default-features = false, optional = true }
34+
ed448-goldilocks = { version = "0.14.0-pre.5", default-features = false, optional = true }
35+
elliptic-curve = { version = "0.14.0-rc.20", default-features = false, optional = true }
36+
hmac = { version = "0.13.0-rc.3", default-features = false }
37+
p256 = { version = "0.14.0-rc.3", default-features = false, optional = true }
38+
p384 = { version = "0.14.0-rc.3", default-features = false, optional = true }
39+
p521 = { version = "0.14.0-rc.3", default-features = false, optional = true }
40+
pkcs1 = { version = "0.8.0-rc.4", default-features = false, optional = true }
41+
pkcs8 = { version = "0.11.0-rc.8", default-features = false, optional = true }
42+
rsa = { version = "0.10.0-rc.11", default-features = false, optional = true }
43+
sec1 = { version = "0.8.0-rc.11", default-features = false, optional = true }
44+
sha2 = { version = "0.11.0-rc.3", default-features = false }
45+
signature = { version = "3.0.0-rc.6", default-features = false, optional = true }
46+
typenum = { version = "1.19.0", features = ["no_std", "const-generics"] }
47+
x25519-dalek = { version = "3.0.0-pre.4", default-features = false, optional = true }
48+
x448 = { version = "=0.14.0-pre.3", default-features = false, optional = true }
49+
50+
# External groups
51+
pki-types = { package = "rustls-pki-types", version = "1.12.0", default-features = false }
52+
rand_core = { version = "0.9.3", default-features = false, features = [
53+
"os_rng",
54+
], optional = true }
55+
rustls = { version = "0.23.32", default-features = false }
56+
webpki = { package = "rustls-webpki", version = "0.103.6", default-features = false, optional = true }
57+
enum_dispatch = "0.3.13"
58+
tinyvec = { version = "1.10.0", default-features = false, optional = true }
59+
thiserror = { version = "2.0.17", default-features = false }
60+
getrandom = "0.3.4"
4361

4462
[dev-dependencies]
45-
getrandom = { version = "0.2", features = ["custom"] } # workaround to build on no_std targets
63+
bytes = { version = "1.10.1", default-features = false }
64+
itertools = { version = "0.14.0", default-features = false }
65+
rsa_098 = { package = "rsa", version = "0.9.8", features = ["sha2"] }
66+
signature_220 = { package = "signature", version = "2.2.0" }
67+
rustls = { version = "0.23.32", default-features = false, features = ["std"] }
68+
x509-cert = { version = "0.2.5", default-features = false, features = [
69+
"builder",
70+
] }
71+
rand_core_064 = { package = "rand_core", version = "0.6.4" }
72+
p256_0132 = { package = "p256", version = "0.13.2" }
4673

4774
[features]
48-
default = ["std", "tls12", "zeroize"]
75+
default = ["std", "tls12", "zeroize", "full", "fast", "quic", "ticketer"]
76+
full = [
77+
"aead-full",
78+
"sign-full",
79+
"verify-full",
80+
"kx-full",
81+
"hash-full",
82+
"format",
83+
]
84+
format = ["pem", "pkcs1", "pkcs8", "sec1"]
4985
logging = ["rustls/logging"]
5086
tls12 = ["rustls/tls12"]
5187

52-
# Only enable feature in upstream if there is an overall effect e.g. aead/alloc in-place
53-
# zeroize is another typical that can be turned off
88+
# RustCrypto is preparing to migrate to core::error::Error
89+
# and in before most of the use case for std is just std::error::Error
90+
std = ["alloc", "rustls/std", "ed448-goldilocks?/std", "tinyvec?/std", "thiserror/std"]
91+
alloc = [
92+
"ecdsa?/alloc",
93+
"ed448-goldilocks?/alloc",
94+
"elliptic-curve?/alloc",
95+
"pkcs8?/alloc",
96+
"sec1?/alloc",
97+
"signature?/alloc",
98+
]
99+
zeroize = [
100+
"aes-gcm?/zeroize",
101+
"aes?/zeroize",
102+
"der?/zeroize",
103+
"ed25519-dalek?/zeroize",
104+
"pkcs1?/zeroize",
105+
"sec1?/zeroize",
106+
"x25519-dalek?/zeroize",
107+
]
108+
subtle = ["digest/subtle", "pkcs8?/subtle", "sec1?/subtle"]
109+
fast = [
110+
"ed25519-dalek?/fast",
111+
# "rsa?/u64_digit",
112+
"x25519-dalek?/precomputed-tables",
113+
]
114+
115+
nist = []
116+
p256 = ["dep:p256", "nist", "p256/pkcs8"]
117+
p384 = ["dep:p384", "nist", "p384/pkcs8"]
118+
p521 = ["dep:p521", "nist"]
119+
ed25519 = ["dep:ed25519-dalek"]
120+
ed448 = ["dep:ed448-goldilocks"]
121+
122+
ecdsa = ["dep:ecdsa", "verify", "signature", "rand", "der", "elliptic-curve"]
123+
ecdsa-p256 = ["ecdsa", "p256", "p256/ecdsa"]
124+
ecdsa-p384 = ["ecdsa", "p384", "p384/ecdsa"]
125+
ecdsa-p521 = ["ecdsa", "p521", "p521/ecdsa"]
126+
ecdsa-full = ["ecdsa-p256", "ecdsa-p384", "ecdsa-p521"]
127+
128+
eddsa = ["verify", "signature", "elliptic-curve"]
129+
eddsa-ed25519 = ["eddsa", "ed25519"]
130+
eddsa-ed448 = ["eddsa", "ed448"]
131+
eddsa-full = ["eddsa-ed25519", "eddsa-ed448"]
132+
133+
kx = ["rand", "elliptic-curve"]
134+
kx-x448 = ["kx", "x448"]
135+
kx-x25519 = ["kx", "dep:x25519-dalek"]
136+
kx-nist = ["sec1"]
137+
kx-p256 = ["kx", "p256", "kx-nist", "p256/ecdh"]
138+
kx-p384 = ["kx", "p384", "kx-nist", "p384/ecdh"]
139+
kx-p521 = ["kx", "p521", "kx-nist", "p521/ecdh"]
140+
kx-full = ["kx-x448", "kx-x25519", "kx-p256", "kx-p384", "kx-p521"]
141+
142+
rsa = ["dep:rsa", "rsa/sha2", "pkcs1"]
143+
rsa-pkcs1 = ["rsa", "pkcs1"]
144+
rsa-pss = ["rsa"]
145+
146+
aead = ["dep:aead"]
147+
aead-aes-gcm = ["aead", "aes-gcm"]
148+
aead-aes-ccm = ["aead", "aes-ccm"]
149+
aead-chacha20poly1305 = ["aead", "chacha20poly1305"]
150+
aead-full = ["aead-aes-gcm", "aead-aes-ccm", "aead-chacha20poly1305"]
151+
152+
sign = ["signature", "der"]
153+
sign-ecdsa-nist = ["sign"]
154+
sign-ecdsa-p256 = ["sign-ecdsa-nist", "ecdsa-p256"]
155+
sign-ecdsa-p384 = ["sign-ecdsa-nist", "ecdsa-p384"]
156+
sign-ecdsa-p521 = ["sign-ecdsa-nist", "ecdsa-p521"]
157+
sign-eddsa = ["sign"]
158+
sign-eddsa-ed25519 = ["sign-eddsa", "eddsa-ed25519"]
159+
sign-eddsa-ed448 = ["sign-eddsa", "eddsa-ed448", "ed448-goldilocks?/signing"]
160+
sign-rsa = ["sign", "rsa"]
161+
sign-rsa-pkcs1 = ["sign-rsa", "rsa-pkcs1"]
162+
sign-rsa-pss = ["sign-rsa", "rsa-pss"]
163+
sign-full = [
164+
"sign-ecdsa-p256",
165+
"sign-ecdsa-p384",
166+
"sign-ecdsa-p521",
167+
"sign-eddsa-ed25519",
168+
"sign-eddsa-ed448",
169+
"sign-rsa-pkcs1",
170+
"sign-rsa-pss",
171+
]
172+
173+
verify = ["dep:webpki"]
174+
verify-ecdsa-nist = ["verify"]
175+
verify-ecdsa-p256 = ["verify-ecdsa-nist", "ecdsa-p256"]
176+
verify-ecdsa-p256-sha256 = ["verify-ecdsa-p256", "hash-sha256"]
177+
verify-ecdsa-p256-sha384 = ["verify-ecdsa-p256", "hash-sha384"]
178+
verify-ecdsa-p256-sha512 = ["verify-ecdsa-p256", "hash-sha512"]
179+
verify-ecdsa-p384 = ["verify-ecdsa-nist", "ecdsa-p384"]
180+
verify-ecdsa-p384-sha256 = ["verify-ecdsa-p384", "hash-sha256"]
181+
verify-ecdsa-p384-sha384 = ["verify-ecdsa-p384", "hash-sha384"]
182+
verify-ecdsa-p384-sha512 = ["verify-ecdsa-p384", "hash-sha512"]
183+
verify-ecdsa-p521 = ["verify-ecdsa-nist", "ecdsa-p521"]
184+
verify-ecdsa-p521-sha256 = ["verify-ecdsa-p521", "hash-sha256"]
185+
verify-ecdsa-p521-sha384 = ["verify-ecdsa-p521", "hash-sha384"]
186+
verify-ecdsa-p521-sha512 = ["verify-ecdsa-p521", "hash-sha512"]
187+
verify-eddsa = ["verify"]
188+
verify-eddsa-ed25519 = ["verify-eddsa", "eddsa-ed25519"]
189+
verify-eddsa-ed448 = ["verify-eddsa", "eddsa-ed448"]
190+
verify-rsa = ["verify"]
191+
verify-rsa-pkcs1 = ["verify-rsa", "rsa-pkcs1"]
192+
verify-rsa-pkcs1-sha256 = ["verify-rsa-pkcs1", "hash-sha256"]
193+
verify-rsa-pkcs1-sha384 = ["verify-rsa-pkcs1", "hash-sha384"]
194+
verify-rsa-pkcs1-sha512 = ["verify-rsa-pkcs1", "hash-sha512"]
195+
verify-rsa-pss = ["verify-rsa", "rsa-pss"]
196+
verify-rsa-pss-sha256 = ["verify-rsa-pss", "hash-sha256"]
197+
verify-rsa-pss-sha384 = ["verify-rsa-pss", "hash-sha384"]
198+
verify-rsa-pss-sha512 = ["verify-rsa-pss", "hash-sha512"]
199+
verify-full = [
200+
"verify-ecdsa-p256-sha256",
201+
"verify-ecdsa-p256-sha384",
202+
"verify-ecdsa-p384-sha256",
203+
"verify-ecdsa-p384-sha384",
204+
"verify-ecdsa-p256-sha512",
205+
"verify-ecdsa-p384-sha512",
206+
"verify-ecdsa-p521-sha256",
207+
"verify-ecdsa-p521-sha384",
208+
"verify-ecdsa-p521-sha512",
209+
"verify-eddsa-ed25519",
210+
"verify-eddsa-ed448",
211+
"verify-rsa-pkcs1-sha256",
212+
"verify-rsa-pkcs1-sha384",
213+
"verify-rsa-pkcs1-sha512",
214+
"verify-rsa-pss-sha256",
215+
"verify-rsa-pss-sha384",
216+
"verify-rsa-pss-sha512",
217+
]
218+
219+
hash = []
220+
hash-sha224 = ["hash"]
221+
hash-sha256 = ["hash"]
222+
hash-sha384 = ["hash"]
223+
hash-sha512 = ["hash"]
224+
hash-full = ["hash-sha224", "hash-sha256", "hash-sha384", "hash-sha512"]
225+
226+
quic = ["aead", "chacha20?/cipher", "tinyvec"]
227+
ticketer = ["aead", "chacha20poly1305", "rand"]
228+
229+
# Formats
230+
der = ["dep:der", "sec1?/der"]
231+
sec1 = ["dep:sec1", "elliptic-curve?/sec1"]
232+
pem = ["elliptic-curve?/pem", "ecdsa?/pem", "ed25519-dalek?/pem"]
233+
pkcs1 = ["dep:pkcs1", "rsa?/encoding"]
234+
pkcs8 = [
235+
"dep:pkcs8",
236+
"ecdsa?/pkcs8",
237+
"ed25519-dalek?/pkcs8",
238+
"ed448-goldilocks?/pkcs8",
239+
"elliptic-curve?/pkcs8",
240+
"p256?/pkcs8",
241+
"p384?/pkcs8",
242+
"p521?/pkcs8",
243+
]
54244

55-
# TODO: go through all of these that what gets exposed re: std error type
56-
std = ["alloc", "webpki/std", "pki-types/std", "rustls/std", "ed25519-dalek/std"]
57-
# TODO: go through all of these to ensure to_vec etc. impls are exposed
58-
alloc = ["webpki/alloc", "pki-types/alloc", "aead/alloc", "ed25519-dalek/alloc"]
59-
zeroize = ["ed25519-dalek/zeroize", "x25519-dalek/zeroize"]
245+
aes = ["dep:aes"]
246+
aes-ccm = ["aes", "ccm"]
247+
aes-gcm = ["dep:aes-gcm", "aes", "gcm"]
248+
ccm = ["dep:ccm"]
249+
chacha20 = ["dep:chacha20"]
250+
chacha20poly1305 = ["dep:chacha20poly1305", "chacha20"]
251+
elliptic-curve = [
252+
"dep:elliptic-curve",
253+
"elliptic-curve/ecdh",
254+
"elliptic-curve/sec1",
255+
]
256+
gcm = []
257+
rand = ["dep:rand_core", "signature?/rand_core", "x25519-dalek?/getrandom"]
258+
signature = ["dep:signature"]
259+
x448 = ["dep:x448"]
260+
tinyvec = ["dep:tinyvec"]

src/aead.rs

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,89 @@
11
use aead::Buffer;
22
use rustls::crypto::cipher::{BorrowedPayload, PrefixedPayload};
33

4-
pub mod chacha20;
4+
#[cfg(all(feature = "quic", feature = "alloc"))]
5+
use alloc::vec::Vec;
6+
7+
#[cfg(feature = "gcm")]
58
pub mod gcm;
69

7-
pub(crate) struct EncryptBufferAdapter<'a>(&'a mut PrefixedPayload);
10+
#[cfg(feature = "ccm")]
11+
pub mod ccm;
12+
13+
#[macro_use]
14+
pub(crate) mod common;
15+
16+
#[cfg(feature = "tinyvec")]
17+
use tinyvec::SliceVec;
18+
19+
pub(crate) enum EncryptBufferAdapter<'a> {
20+
PrefixedPayload(&'a mut PrefixedPayload),
21+
#[cfg(feature = "quic")]
22+
Vec(Vec<u8>),
23+
}
824

925
impl AsRef<[u8]> for EncryptBufferAdapter<'_> {
1026
fn as_ref(&self) -> &[u8] {
11-
self.0.as_ref()
27+
match self {
28+
EncryptBufferAdapter::PrefixedPayload(payload) => payload.as_ref(),
29+
#[cfg(feature = "quic")]
30+
EncryptBufferAdapter::Vec(payload) => payload.as_ref(),
31+
}
1232
}
1333
}
1434

1535
impl AsMut<[u8]> for EncryptBufferAdapter<'_> {
1636
fn as_mut(&mut self) -> &mut [u8] {
17-
self.0.as_mut()
37+
match self {
38+
EncryptBufferAdapter::PrefixedPayload(payload) => payload.as_mut(),
39+
#[cfg(feature = "quic")]
40+
EncryptBufferAdapter::Vec(payload) => payload.as_mut(),
41+
}
1842
}
1943
}
2044

2145
impl Buffer for EncryptBufferAdapter<'_> {
2246
fn extend_from_slice(&mut self, other: &[u8]) -> aead::Result<()> {
23-
self.0.extend_from_slice(other);
47+
match self {
48+
EncryptBufferAdapter::PrefixedPayload(payload) => payload.extend_from_slice(other),
49+
#[cfg(feature = "quic")]
50+
EncryptBufferAdapter::Vec(payload) => payload.extend_from_slice(other),
51+
}
2452
Ok(())
2553
}
2654

2755
fn truncate(&mut self, len: usize) {
28-
self.0.truncate(len)
56+
match self {
57+
EncryptBufferAdapter::PrefixedPayload(payload) => payload.truncate(len),
58+
#[cfg(feature = "quic")]
59+
EncryptBufferAdapter::Vec(payload) => payload.truncate(len),
60+
}
2961
}
3062
}
3163

32-
pub(crate) struct DecryptBufferAdapter<'a, 'p>(&'a mut BorrowedPayload<'p>);
64+
pub(crate) enum DecryptBufferAdapter<'a, 'p> {
65+
BorrowedPayload(&'a mut BorrowedPayload<'p>),
66+
#[cfg(feature = "tinyvec")]
67+
Slice(SliceVec<'a, u8>),
68+
}
3369

3470
impl AsRef<[u8]> for DecryptBufferAdapter<'_, '_> {
3571
fn as_ref(&self) -> &[u8] {
36-
self.0
72+
match self {
73+
DecryptBufferAdapter::BorrowedPayload(payload) => payload,
74+
#[cfg(feature = "tinyvec")]
75+
DecryptBufferAdapter::Slice(slice) => slice,
76+
}
3777
}
3878
}
3979

4080
impl AsMut<[u8]> for DecryptBufferAdapter<'_, '_> {
4181
fn as_mut(&mut self) -> &mut [u8] {
42-
self.0
82+
match self {
83+
DecryptBufferAdapter::BorrowedPayload(payload) => payload,
84+
#[cfg(feature = "tinyvec")]
85+
DecryptBufferAdapter::Slice(slice) => slice,
86+
}
4387
}
4488
}
4589

@@ -49,6 +93,13 @@ impl Buffer for DecryptBufferAdapter<'_, '_> {
4993
}
5094

5195
fn truncate(&mut self, len: usize) {
52-
self.0.truncate(len)
96+
match self {
97+
DecryptBufferAdapter::BorrowedPayload(payload) => payload.truncate(len),
98+
#[cfg(feature = "tinyvec")]
99+
DecryptBufferAdapter::Slice(payload) => payload.truncate(len),
100+
}
53101
}
54102
}
103+
104+
#[cfg(feature = "aes")]
105+
pub mod aes;

0 commit comments

Comments
 (0)