Skip to content

Commit b52f29c

Browse files
committed
Implement actual encryption
1 parent 3fc5c5b commit b52f29c

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/key_exchange.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::str;
22

33
use aws_lc_rs::{
44
agreement::{self, EphemeralPrivateKey, UnparsedPublicKey, X25519},
5-
cipher::{StreamingDecryptingKey, UnboundCipherKey, AES_128},
5+
cipher::{StreamingDecryptingKey, StreamingEncryptingKey, UnboundCipherKey, AES_128},
66
digest, hmac,
77
rand::{self, SystemRandom},
88
signature::KeyPair,
@@ -170,6 +170,26 @@ impl EcdhKeyExchange {
170170
),
171171
);
172172

173+
conn.stream_write.set_encryption_key(
174+
StreamingEncryptingKey::less_safe_ctr(
175+
UnboundCipherKey::new(
176+
&AES_128,
177+
&raw_keys.server_to_client.encryption_key.as_ref()[..16],
178+
)
179+
.unwrap(),
180+
aws_lc_rs::cipher::EncryptionContext::Iv128(
181+
raw_keys.server_to_client.initial_iv.as_ref()[..16]
182+
.try_into()
183+
.unwrap(),
184+
),
185+
)
186+
.unwrap(),
187+
hmac::Key::new(
188+
hmac::HMAC_SHA256,
189+
&raw_keys.server_to_client.integrity_key.as_ref()[..32],
190+
),
191+
);
192+
173193
Ok(())
174194
}
175195
}
@@ -581,7 +601,6 @@ impl<'a, T: From<&'a str>> Decode<'a> for Vec<T> {
581601
/// The raw hashes from which we will derive the crypto keys.
582602
///
583603
/// <https://www.rfc-editor.org/rfc/rfc4253#section-7.2>
584-
#[expect(dead_code)] // FIXME implement encryption/decryption and MAC
585604
struct RawKeySet {
586605
client_to_server: RawKeys,
587606
server_to_client: RawKeys,

src/proto.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ impl<W: AsyncWriteExt + Unpin> EncryptingWriter<W> {
289289
Ok(())
290290
}
291291

292+
pub(crate) fn set_encryption_key(
293+
&mut self,
294+
encryption_key: StreamingEncryptingKey,
295+
integrity_key: hmac::Key,
296+
) {
297+
self.encryption_key = Some((encryption_key, integrity_key));
298+
}
299+
292300
/// Write a packet. Returns written [`Packet`].
293301
pub(crate) async fn write_packet(
294302
&mut self,
@@ -301,11 +309,29 @@ impl<W: AsyncWriteExt + Unpin> EncryptingWriter<W> {
301309
let packet_number = self.packet_number;
302310
self.packet_number = self.packet_number.wrapping_add(1);
303311

312+
let packet = Packet::builder(&mut self.buf).with_payload(payload);
313+
update_exchange_hash(packet.payload()?);
314+
304315
if let Some((encryption_key, integrity_key)) = &mut self.encryption_key {
305-
todo!()
316+
let block_len = encryption_key.algorithm().block_len();
317+
318+
let data = packet.without_mac()?;
319+
320+
self.encrypted_buf.resize(data.len() + block_len, 0);
321+
let update = encryption_key
322+
.update(data, &mut self.encrypted_buf)
323+
.unwrap();
324+
assert_eq!(update.remainder().len(), block_len);
325+
self.encrypted_buf.truncate(data.len());
326+
327+
let mut hmac_ctx = hmac::Context::with_key(integrity_key);
328+
hmac_ctx.update(&packet_number.to_be_bytes());
329+
hmac_ctx.update(data);
330+
let mac = hmac_ctx.sign();
331+
self.encrypted_buf.extend_from_slice(mac.as_ref());
332+
333+
self.stream.write_all(&self.encrypted_buf).await?;
306334
} else {
307-
let packet = Packet::builder(&mut self.buf).with_payload(payload);
308-
update_exchange_hash(packet.payload()?);
309335
self.stream.write_all(packet.without_mac()?).await?;
310336
};
311337

@@ -430,8 +456,6 @@ impl<'a> PacketBuilderWithPayload<'a> {
430456
}
431457
}
432458

433-
buf.extend_from_slice(&[]); // mac
434-
435459
let packet_len = (buf.len() - start - 4) as u32;
436460
if let Some(packet_length_dst) = buf.get_mut(start..start + 4) {
437461
packet_length_dst.copy_from_slice(&packet_len.to_be_bytes());

0 commit comments

Comments
 (0)