Skip to content

Commit ab0c892

Browse files
Implement caching of expanded keys as a permanent feature
1 parent e6e0e7d commit ab0c892

File tree

3 files changed

+19
-57
lines changed

3 files changed

+19
-57
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,3 @@ defmt = ["dep:defmt", "embedded-io/defmt-03", "heapless/defmt-03"]
5959
std = ["embedded-io/std", "embedded-io-async/std"]
6060
tokio = ["embedded-io-adapters/tokio-1"]
6161
alloc = []
62-
key-cache = []

src/connection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ where
4444
let server_key = key_schedule.get_key()?;
4545
let nonce = key_schedule.get_nonce()?;
4646

47-
let crypto = <CipherSuite::Cipher as KeyInit>::new(&server_key);
47+
let crypto = <CipherSuite::Cipher as KeyInit>::new(server_key);
4848
crypto
4949
.decrypt_in_place(&nonce, header.data(), &mut app_data)
5050
.map_err(|_| TlsError::CryptoError)?;
@@ -106,7 +106,7 @@ where
106106
// trace!("encrypt nonce {:02x?}", nonce);
107107
// trace!("plaintext {} {:02x?}", buf.len(), buf.as_slice(),);
108108
//let crypto = Aes128Gcm::new_varkey(&self.key_schedule.get_client_key()).unwrap();
109-
let crypto = <CipherSuite::Cipher as KeyInit>::new(&client_key);
109+
let crypto = <CipherSuite::Cipher as KeyInit>::new(client_key);
110110
let len = buf.len() + <CipherSuite::Cipher as AeadCore>::TagSize::to_usize();
111111

112112
if len > buf.capacity() {

src/key_schedule.rs

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::handshake::binder::PskBinder;
22
use crate::handshake::finished::Finished;
33
use crate::{TlsError, config::TlsCipherSuite};
4-
#[cfg(feature = "key-cache")]
5-
use core::cell::OnceCell;
64
use digest::OutputSizeUser;
75
use digest::generic_array::ArrayLength;
86
use hmac::{Mac, SimpleHmac};
@@ -137,11 +135,8 @@ where
137135
{
138136
traffic_secret: Secret<CipherSuite>,
139137
counter: u64,
140-
141-
#[cfg(feature = "key-cache")]
142-
key: OnceCell<KeyArray<CipherSuite>>,
143-
#[cfg(feature = "key-cache")]
144-
iv: OnceCell<IvArray<CipherSuite>>,
138+
key: KeyArray<CipherSuite>,
139+
iv: IvArray<CipherSuite>,
145140
}
146141

147142
impl<CipherSuite> KeyScheduleState<CipherSuite>
@@ -152,57 +147,24 @@ where
152147
Self {
153148
traffic_secret: Secret::Uninitialized,
154149
counter: 0,
155-
#[cfg(feature = "key-cache")]
156-
key: OnceCell::new(),
157-
#[cfg(feature = "key-cache")]
158-
iv: OnceCell::new(),
150+
key: KeyArray::<CipherSuite>::default(),
151+
iv: IvArray::<CipherSuite>::default(),
159152
}
160153
}
161154

162155
#[inline]
163-
pub fn get_key(&self) -> Result<KeyArray<CipherSuite>, TlsError> {
164-
#[cfg(feature = "key-cache")]
165-
if let Some(k) = self.key.get() {
166-
Ok(k.clone())
167-
} else {
168-
let k = self.get_key_impl()?;
169-
let _ = self.key.set(k.clone());
170-
Ok(k)
171-
}
172-
173-
#[cfg(not(feature = "key-cache"))]
174-
self.get_key_impl()
156+
pub fn get_key(&self) -> Result<&KeyArray<CipherSuite>, TlsError> {
157+
Ok(&self.key)
175158
}
176159

177160
#[inline]
178-
pub fn get_iv(&self) -> Result<IvArray<CipherSuite>, TlsError> {
179-
#[cfg(feature = "key-cache")]
180-
if let Some(k) = self.iv.get() {
181-
Ok(k.clone())
182-
} else {
183-
let k = self.get_iv_impl()?;
184-
let _ = self.iv.set(k.clone());
185-
Ok(k)
186-
}
187-
188-
#[cfg(not(feature = "key-cache"))]
189-
self.get_iv_impl()
190-
}
191-
192-
fn get_key_impl(&self) -> Result<KeyArray<CipherSuite>, TlsError> {
193-
self.traffic_secret
194-
.make_expanded_hkdf_label(b"key", ContextType::None)
161+
pub fn get_iv(&self) -> Result<&IvArray<CipherSuite>, TlsError> {
162+
Ok(&self.iv)
195163
}
196164

197-
fn get_iv_impl(&self) -> Result<IvArray<CipherSuite>, TlsError> {
198-
self.traffic_secret
199-
.make_expanded_hkdf_label(b"iv", ContextType::None)
200-
}
201-
202-
203165
pub fn get_nonce(&self) -> Result<IvArray<CipherSuite>, TlsError> {
204166
let iv = self.get_iv()?;
205-
Ok(KeySchedule::<CipherSuite>::get_nonce(self.counter, &iv))
167+
Ok(KeySchedule::<CipherSuite>::get_nonce(self.counter, iv))
206168
}
207169

208170
fn calculate_traffic_secret(
@@ -216,11 +178,12 @@ where
216178
Hkdf::<CipherSuite>::from_prk(&secret).map_err(|_| TlsError::InternalError)?;
217179

218180
self.traffic_secret.replace(traffic_secret);
219-
#[cfg(feature = "key-cache")]
220-
{
221-
self.key = OnceCell::new();
222-
self.iv = OnceCell::new();
223-
}
181+
self.key = self
182+
.traffic_secret
183+
.make_expanded_hkdf_label(b"key", ContextType::None)?;
184+
self.iv = self
185+
.traffic_secret
186+
.make_expanded_hkdf_label(b"iv", ContextType::None)?;
224187
self.counter = 0;
225188
Ok(())
226189
}
@@ -449,7 +412,7 @@ where
449412
self.state.increment_counter();
450413
}
451414

452-
pub(crate) fn get_key(&self) -> Result<KeyArray<CipherSuite>, TlsError> {
415+
pub(crate) fn get_key(&self) -> Result<&KeyArray<CipherSuite>, TlsError> {
453416
self.state.get_key()
454417
}
455418

@@ -496,7 +459,7 @@ where
496459
&mut self.transcript_hash
497460
}
498461

499-
pub(crate) fn get_key(&self) -> Result<KeyArray<CipherSuite>, TlsError> {
462+
pub(crate) fn get_key(&self) -> Result<&KeyArray<CipherSuite>, TlsError> {
500463
self.state.get_key()
501464
}
502465

0 commit comments

Comments
 (0)