Skip to content

Commit 93535ca

Browse files
sinui0themighty1
andauthored
feat(mpc-tls): improve error message for incorrect transcript config (#754)
* feat(mpc-tls): improve error message for incorrect transcript config * rustfmt --------- Co-authored-by: dan <[email protected]>
1 parent a34dd57 commit 93535ca

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

crates/mpc-tls/src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl ConfigBuilder {
5050
let mut max_recv_online = self
5151
.max_recv_online
5252
.ok_or(ConfigBuilderError::UninitializedField("max_recv_online"))?;
53-
let max_recv = self
53+
let mut max_recv = self
5454
.max_recv
5555
.ok_or(ConfigBuilderError::UninitializedField("max_recv"))?;
5656

@@ -61,6 +61,7 @@ impl ConfigBuilder {
6161
}
6262

6363
max_recv_online += MIN_RECV;
64+
max_recv += MIN_RECV;
6465

6566
let max_sent_records = self
6667
.max_sent_records

crates/mpc-tls/src/follower.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl MpcTlsFollower {
135135
self.config.max_recv_records,
136136
self.config.max_sent,
137137
self.config.max_recv_online,
138+
self.config.max_recv,
138139
)?;
139140

140141
(keys, cf_vd, sf_vd)

crates/mpc-tls/src/leader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ impl MpcTlsLeader {
169169
self.config.max_recv_records,
170170
self.config.max_sent,
171171
self.config.max_recv_online,
172+
self.config.max_recv,
172173
)?;
173174

174175
self.state = State::Setup {

crates/mpc-tls/src/record_layer.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ pub(crate) struct RecordLayer {
8080
state: State,
8181
/// Whether the record layer has started processing application data.
8282
started: bool,
83+
/// Number of bytes sent.
84+
sent: usize,
85+
/// Number of bytes received and decrypted online.
86+
recv_online: usize,
87+
/// Number of bytes received.
88+
recv: usize,
89+
/// Maximum number of bytes sent.
90+
max_sent: usize,
91+
/// Maximum number of bytes received to be decrypted online.
92+
max_recv_online: usize,
93+
/// Maximum number of bytes received.
94+
max_recv: usize,
8395

8496
encrypt_buffer: Vec<EncryptOp>,
8597
decrypt_buffer: Vec<DecryptOp>,
@@ -99,6 +111,12 @@ impl RecordLayer {
99111
aes_ctr: AesCtr::new(role),
100112
state: State::Init,
101113
started: false,
114+
sent: 0,
115+
recv_online: 0,
116+
recv: 0,
117+
max_sent: 0,
118+
max_recv_online: 0,
119+
max_recv: 0,
102120
encrypt_buffer: Vec::new(),
103121
decrypt_buffer: Vec::new(),
104122
encrypted_buffer: VecDeque::new(),
@@ -114,13 +132,16 @@ impl RecordLayer {
114132
/// * `sent_records` - Number of sent records to allocate.
115133
/// * `recv_records` - Number of received records to allocate.
116134
/// * `sent_len` - Total length of sent records to allocate.
135+
/// * `recv_len_online` - Total length of received records to be decrypted
136+
/// online.
117137
/// * `recv_len` - Total length of received records to allocate.
118138
pub(crate) fn alloc(
119139
&mut self,
120140
vm: &mut dyn VmTrait<Binary>,
121141
sent_records: usize,
122142
recv_records: usize,
123143
sent_len: usize,
144+
recv_len_online: usize,
124145
recv_len: usize,
125146
) -> Result<(), MpcTlsError> {
126147
let State::Init = self.state.take() else {
@@ -142,12 +163,12 @@ impl RecordLayer {
142163
.map_err(MpcTlsError::record_layer)?;
143164

144165
decrypt
145-
.alloc(vm, recv_records, recv_len)
166+
.alloc(vm, recv_records, recv_len_online)
146167
.map_err(MpcTlsError::record_layer)?;
147168

148169
let recv_otp = match self.role {
149170
Role::Leader => {
150-
let mut recv_otp = vec![0u8; recv_len];
171+
let mut recv_otp = vec![0u8; recv_len_online];
151172
rand::rng().fill_bytes(&mut recv_otp);
152173

153174
Some(recv_otp)
@@ -157,6 +178,10 @@ impl RecordLayer {
157178

158179
self.aes_ctr.alloc(vm)?;
159180

181+
self.max_sent += sent_len;
182+
self.max_recv_online += recv_len_online;
183+
self.max_recv += recv_len;
184+
160185
self.state = State::Online {
161186
recv_otp,
162187
sent_records: Vec::new(),
@@ -267,9 +292,15 @@ impl RecordLayer {
267292
) -> Result<(), MpcTlsError> {
268293
if self.encrypt_buffer.len() >= MAX_BUFFER_SIZE {
269294
return Err(MpcTlsError::peer("encrypt buffer is full"));
295+
} else if self.sent + len > self.max_sent {
296+
return Err(MpcTlsError::record_layer(format!(
297+
"attempted to send more data than was configured, increase `max_sent` in the config: current={}, additional={}, max={}",
298+
self.sent, len, self.max_sent
299+
)));
270300
}
271301

272302
let (seq, explicit_nonce, aad) = self.next_write(typ, version, len);
303+
self.sent += len;
273304
self.encrypt_buffer.push(EncryptOp::new(
274305
seq,
275306
typ,
@@ -295,9 +326,15 @@ impl RecordLayer {
295326
) -> Result<(), MpcTlsError> {
296327
if self.decrypt_buffer.len() >= MAX_BUFFER_SIZE {
297328
return Err(MpcTlsError::peer("decrypt buffer is full"));
329+
} else if self.recv + ciphertext.len() > self.max_recv {
330+
return Err(MpcTlsError::record_layer(format!(
331+
"attempted to receive more data than was configured, increase `max_recv` in the config: current={}, additional={}, max={}",
332+
self.recv, ciphertext.len(), self.max_recv
333+
)));
298334
}
299335

300336
let (seq, aad) = self.next_read(typ, version, ciphertext.len());
337+
self.recv += ciphertext.len();
301338
self.decrypt_buffer.push(DecryptOp::new(
302339
seq,
303340
typ,
@@ -386,6 +423,18 @@ impl RecordLayer {
386423
return Ok(());
387424
}
388425

426+
if is_decrypting {
427+
let decrypt_len: usize = decrypt_ops.iter().map(|op| op.ciphertext.len()).sum();
428+
if self.recv_online + decrypt_len > self.max_recv_online {
429+
return Err(MpcTlsError::record_layer(format!(
430+
"attempted to decrypt more data in the online phase than was configured, increase `max_recv_online` in the config: current={}, additional={}, max={}",
431+
self.recv_online, decrypt_len, self.max_recv_online
432+
)));
433+
} else {
434+
self.recv_online += decrypt_len;
435+
}
436+
}
437+
389438
debug!(
390439
"processing {} encrypt ops and {} decrypt ops",
391440
encrypt_ops.len(),

0 commit comments

Comments
 (0)