Skip to content

Commit 504f3c3

Browse files
authored
Merge pull request #28 from yaws-rs/main
Upgrade 0.23, re-organise external examples and tests as external
2 parents 7794220 + e041ae2 commit 504f3c3

File tree

21 files changed

+245
-167
lines changed

21 files changed

+245
-167
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pkcs8 = { version = "0.10.2", features = ["pem", "pkcs5"] }
2525
pki-types = { package = "rustls-pki-types", version = "1.0.1", default-features = false }
2626
rand_core = "0.6.4"
2727
rsa = { version = "0.9.2", features = ["sha2"] }
28-
rustls = { version = "0.22.1", default-features = false }
28+
rustls = { version = "0.23.0", default-features = false }
2929
sec1 = { version = "0.7.3", features = ["pkcs8", "pem"] }
3030
sha2 = "0.10.7"
3131
signature = "2.1.0"
@@ -35,10 +35,10 @@ webpki = { package = "rustls-webpki", version = "0.102.0", default-features = fa
3535
x25519-dalek = "2"
3636

3737
[features]
38-
default = ["std"]
38+
default = ["std", "tls12"]
3939
logging = ["rustls/logging"]
4040
tls12 = ["rustls/tls12"]
41-
std = ["webpki/std", "pki-types/std"]
41+
std = ["webpki/std", "pki-types/std", "rustls/std"]
4242
alloc = ["webpki/alloc", "pki-types/alloc"]
4343

4444
[dev-dependencies]
File renamed without changes.
File renamed without changes.

src/aead.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
1+
use aead::Buffer;
2+
use rustls::crypto::cipher::{BorrowedPayload, PrefixedPayload};
3+
14
pub mod chacha20;
25
pub mod gcm;
6+
7+
pub(crate) struct EncryptBufferAdapter<'a>(&'a mut PrefixedPayload);
8+
9+
impl AsRef<[u8]> for EncryptBufferAdapter<'_> {
10+
fn as_ref(&self) -> &[u8] {
11+
self.0.as_ref()
12+
}
13+
}
14+
15+
impl AsMut<[u8]> for EncryptBufferAdapter<'_> {
16+
fn as_mut(&mut self) -> &mut [u8] {
17+
self.0.as_mut()
18+
}
19+
}
20+
21+
impl Buffer for EncryptBufferAdapter<'_> {
22+
fn extend_from_slice(&mut self, other: &[u8]) -> aead::Result<()> {
23+
self.0.extend_from_slice(other);
24+
Ok(())
25+
}
26+
27+
fn truncate(&mut self, len: usize) {
28+
self.0.truncate(len)
29+
}
30+
}
31+
32+
pub(crate) struct DecryptBufferAdapter<'a, 'p>(&'a mut BorrowedPayload<'p>);
33+
34+
impl AsRef<[u8]> for DecryptBufferAdapter<'_, '_> {
35+
fn as_ref(&self) -> &[u8] {
36+
self.0
37+
}
38+
}
39+
40+
impl AsMut<[u8]> for DecryptBufferAdapter<'_, '_> {
41+
fn as_mut(&mut self) -> &mut [u8] {
42+
self.0
43+
}
44+
}
45+
46+
impl Buffer for DecryptBufferAdapter<'_, '_> {
47+
fn extend_from_slice(&mut self, _: &[u8]) -> aead::Result<()> {
48+
unreachable!("not used by `AeadInPlace::decrypt_in_place`")
49+
}
50+
51+
fn truncate(&mut self, len: usize) {
52+
self.0.truncate(len)
53+
}
54+
}

src/aead/chacha20.rs

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1+
#[cfg(not(feature = "std"))]
2+
use alloc::boxed::Box;
3+
4+
use super::{DecryptBufferAdapter, EncryptBufferAdapter};
5+
16
use chacha20poly1305::{AeadInPlace, KeyInit, KeySizeUser};
2-
#[cfg(feature = "tls12")]
3-
use rustls::crypto::cipher::NONCE_LEN;
4-
use rustls::{
5-
crypto::cipher::{self, AeadKey, Iv, UnsupportedOperationError},
6-
ConnectionTrafficSecrets, ContentType, ProtocolVersion,
7+
use rustls::crypto::cipher::{
8+
self, AeadKey, InboundOpaqueMessage, InboundPlainMessage, Iv, MessageDecrypter,
9+
MessageEncrypter, OutboundOpaqueMessage, OutboundPlainMessage, PrefixedPayload,
10+
Tls13AeadAlgorithm, UnsupportedOperationError,
711
};
12+
use rustls::{ConnectionTrafficSecrets, ContentType, ProtocolVersion};
13+
14+
#[cfg(feature = "tls12")]
15+
use rustls::crypto::cipher::{KeyBlockShape, Tls12AeadAlgorithm, NONCE_LEN};
816

917
pub struct Chacha20Poly1305;
1018

11-
impl cipher::Tls13AeadAlgorithm for Chacha20Poly1305 {
12-
fn encrypter(&self, key: cipher::AeadKey, iv: cipher::Iv) -> Box<dyn cipher::MessageEncrypter> {
19+
impl Tls13AeadAlgorithm for Chacha20Poly1305 {
20+
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
1321
Box::new(Tls13Cipher(
1422
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
1523
iv,
1624
))
1725
}
1826

19-
fn decrypter(&self, key: cipher::AeadKey, iv: cipher::Iv) -> Box<dyn cipher::MessageDecrypter> {
27+
fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
2028
Box::new(Tls13Cipher(
2129
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
2230
iv,
@@ -37,28 +45,23 @@ impl cipher::Tls13AeadAlgorithm for Chacha20Poly1305 {
3745
}
3846

3947
#[cfg(feature = "tls12")]
40-
impl cipher::Tls12AeadAlgorithm for Chacha20Poly1305 {
41-
fn encrypter(
42-
&self,
43-
key: cipher::AeadKey,
44-
iv: &[u8],
45-
_: &[u8],
46-
) -> Box<dyn cipher::MessageEncrypter> {
48+
impl Tls12AeadAlgorithm for Chacha20Poly1305 {
49+
fn encrypter(&self, key: AeadKey, iv: &[u8], _: &[u8]) -> Box<dyn MessageEncrypter> {
4750
Box::new(Tls12Cipher(
4851
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
49-
cipher::Iv::copy(iv),
52+
Iv::copy(iv),
5053
))
5154
}
5255

53-
fn decrypter(&self, key: cipher::AeadKey, iv: &[u8]) -> Box<dyn cipher::MessageDecrypter> {
56+
fn decrypter(&self, key: AeadKey, iv: &[u8]) -> Box<dyn MessageDecrypter> {
5457
Box::new(Tls12Cipher(
5558
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
56-
cipher::Iv::copy(iv),
59+
Iv::copy(iv),
5760
))
5861
}
5962

60-
fn key_block_shape(&self) -> cipher::KeyBlockShape {
61-
cipher::KeyBlockShape {
63+
fn key_block_shape(&self) -> KeyBlockShape {
64+
KeyBlockShape {
6265
enc_key_len: 32,
6366
fixed_iv_len: 12,
6467
explicit_nonce_len: 0,
@@ -81,29 +84,28 @@ impl cipher::Tls12AeadAlgorithm for Chacha20Poly1305 {
8184
}
8285
}
8386

84-
struct Tls13Cipher(chacha20poly1305::ChaCha20Poly1305, cipher::Iv);
87+
struct Tls13Cipher(chacha20poly1305::ChaCha20Poly1305, Iv);
8588

86-
impl cipher::MessageEncrypter for Tls13Cipher {
89+
impl MessageEncrypter for Tls13Cipher {
8790
fn encrypt(
8891
&mut self,
89-
m: cipher::BorrowedPlainMessage,
92+
m: OutboundPlainMessage,
9093
seq: u64,
91-
) -> Result<cipher::OpaqueMessage, rustls::Error> {
94+
) -> Result<OutboundOpaqueMessage, rustls::Error> {
9295
let total_len = self.encrypted_payload_len(m.payload.len());
96+
let mut payload = PrefixedPayload::with_capacity(total_len);
9397

94-
// construct a TLSInnerPlaintext
95-
let mut payload = Vec::with_capacity(total_len);
96-
payload.extend_from_slice(m.payload);
97-
payload.push(m.typ.get_u8());
98+
payload.extend_from_chunks(&m.payload);
99+
payload.extend_from_slice(&m.typ.to_array());
98100

99-
let nonce = chacha20poly1305::Nonce::from(cipher::Nonce::new(&self.1, seq).0);
101+
let nonce: chacha20poly1305::Nonce = cipher::Nonce::new(&self.1, seq).0.into();
100102
let aad = cipher::make_tls13_aad(total_len);
101103

102104
self.0
103-
.encrypt_in_place(&nonce, &aad, &mut payload)
105+
.encrypt_in_place(&nonce, &aad, &mut EncryptBufferAdapter(&mut payload))
104106
.map_err(|_| rustls::Error::EncryptError)
105107
.map(|()| {
106-
cipher::OpaqueMessage::new(
108+
OutboundOpaqueMessage::new(
107109
ContentType::ApplicationData,
108110
ProtocolVersion::TLSv1_2,
109111
payload,
@@ -116,46 +118,46 @@ impl cipher::MessageEncrypter for Tls13Cipher {
116118
}
117119
}
118120

119-
impl cipher::MessageDecrypter for Tls13Cipher {
120-
fn decrypt(
121+
impl MessageDecrypter for Tls13Cipher {
122+
fn decrypt<'a>(
121123
&mut self,
122-
mut m: cipher::OpaqueMessage,
124+
mut m: InboundOpaqueMessage<'a>,
123125
seq: u64,
124-
) -> Result<cipher::PlainMessage, rustls::Error> {
125-
let payload = m.payload_mut();
126-
let nonce = chacha20poly1305::Nonce::from(cipher::Nonce::new(&self.1, seq).0);
126+
) -> Result<InboundPlainMessage<'a>, rustls::Error> {
127+
let payload = &mut m.payload;
128+
let nonce: chacha20poly1305::Nonce = cipher::Nonce::new(&self.1, seq).0.into();
127129
let aad = cipher::make_tls13_aad(payload.len());
128130

129131
self.0
130-
.decrypt_in_place(&nonce, &aad, payload)
132+
.decrypt_in_place(&nonce, &aad, &mut DecryptBufferAdapter(payload))
131133
.map_err(|_| rustls::Error::DecryptError)?;
132134

133135
m.into_tls13_unpadded_message()
134136
}
135137
}
136138

137139
#[cfg(feature = "tls12")]
138-
struct Tls12Cipher(chacha20poly1305::ChaCha20Poly1305, cipher::Iv);
140+
struct Tls12Cipher(chacha20poly1305::ChaCha20Poly1305, Iv);
139141

140142
#[cfg(feature = "tls12")]
141-
impl cipher::MessageEncrypter for Tls12Cipher {
143+
impl MessageEncrypter for Tls12Cipher {
142144
fn encrypt(
143145
&mut self,
144-
m: cipher::BorrowedPlainMessage,
146+
m: OutboundPlainMessage,
145147
seq: u64,
146-
) -> Result<cipher::OpaqueMessage, rustls::Error> {
148+
) -> Result<OutboundOpaqueMessage, rustls::Error> {
147149
let total_len = self.encrypted_payload_len(m.payload.len());
150+
let mut payload = PrefixedPayload::with_capacity(total_len);
148151

149-
let mut payload = Vec::with_capacity(total_len);
150-
payload.extend_from_slice(m.payload);
152+
payload.extend_from_chunks(&m.payload);
151153

152-
let nonce = chacha20poly1305::Nonce::from(cipher::Nonce::new(&self.1, seq).0);
153-
let aad = cipher::make_tls12_aad(seq, m.typ, m.version, payload.len());
154+
let nonce: chacha20poly1305::Nonce = cipher::Nonce::new(&self.1, seq).0.into();
155+
let aad = cipher::make_tls12_aad(seq, m.typ, m.version, m.payload.len());
154156

155157
self.0
156-
.encrypt_in_place(&nonce, &aad, &mut payload)
158+
.encrypt_in_place(&nonce, &aad, &mut EncryptBufferAdapter(&mut payload))
157159
.map_err(|_| rustls::Error::EncryptError)
158-
.map(|_| cipher::OpaqueMessage::new(m.typ, m.version, payload))
160+
.map(|_| OutboundOpaqueMessage::new(m.typ, m.version, payload))
159161
}
160162

161163
fn encrypted_payload_len(&self, payload_len: usize) -> usize {
@@ -164,24 +166,24 @@ impl cipher::MessageEncrypter for Tls12Cipher {
164166
}
165167

166168
#[cfg(feature = "tls12")]
167-
impl cipher::MessageDecrypter for Tls12Cipher {
168-
fn decrypt(
169+
impl MessageDecrypter for Tls12Cipher {
170+
fn decrypt<'a>(
169171
&mut self,
170-
mut m: cipher::OpaqueMessage,
172+
mut m: InboundOpaqueMessage<'a>,
171173
seq: u64,
172-
) -> Result<cipher::PlainMessage, rustls::Error> {
173-
let payload = m.payload();
174-
let nonce = chacha20poly1305::Nonce::from(cipher::Nonce::new(&self.1, seq).0);
174+
) -> Result<InboundPlainMessage<'a>, rustls::Error> {
175+
let payload = &m.payload;
176+
let nonce: chacha20poly1305::Nonce = cipher::Nonce::new(&self.1, seq).0.into();
175177
let aad = cipher::make_tls12_aad(
176178
seq,
177179
m.typ,
178180
m.version,
179181
payload.len() - CHACHAPOLY1305_OVERHEAD,
180182
);
181183

182-
let payload = m.payload_mut();
184+
let payload = &mut m.payload;
183185
self.0
184-
.decrypt_in_place(&nonce, &aad, payload)
186+
.decrypt_in_place(&nonce, &aad, &mut DecryptBufferAdapter(payload))
185187
.map_err(|_| rustls::Error::DecryptError)?;
186188

187189
Ok(m.into_plain_message())

0 commit comments

Comments
 (0)