Skip to content

Commit 795eea7

Browse files
shanerapheldjc
authored andcommitted
Advance the HPKE sequence only after seal succeeds
The AEAD nonce sequence was incremented before authentication, so a rejected ciphertext consumed sequence zero and the retry failed.
1 parent d67cb71 commit 795eea7

1 file changed

Lines changed: 42 additions & 17 deletions

File tree

‎rustls/src/crypto/aws_lc_rs/hpke.rs‎

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use alloc::boxed::Box;
22
use alloc::vec::Vec;
33
use core::fmt::{self, Debug, Formatter};
44

5-
use aws_lc_rs::aead::{
6-
self, Aad, BoundKey, NONCE_LEN, Nonce, NonceSequence, OpeningKey, SealingKey, UnboundKey,
7-
};
5+
use aws_lc_rs::aead::{self, Aad, LessSafeKey, NONCE_LEN, Nonce, UnboundKey};
86
use aws_lc_rs::agreement;
97
use aws_lc_rs::cipher::{AES_128_KEY_LEN, AES_256_KEY_LEN};
108
use aws_lc_rs::digest::{SHA256_OUTPUT_LEN, SHA384_OUTPUT_LEN, SHA512_OUTPUT_LEN};
@@ -441,11 +439,16 @@ impl<const KEY_SIZE: usize, const KDF_SIZE: usize> HpkeSealer for Sealer<KEY_SIZ
441439

442440
let key = UnboundKey::new(self.key_schedule.aead, &self.key_schedule.key.0)
443441
.map_err(unspecified_err)?;
444-
let mut sealing_key = SealingKey::new(key, &mut self.key_schedule);
442+
let sealing_key = LessSafeKey::new(key);
443+
444+
let nonce = self.key_schedule.compute_nonce();
445+
self.key_schedule
446+
.increment_seq_num()
447+
.map_err(unspecified_err)?;
445448

446449
let mut in_out_buffer = Vec::from(plaintext);
447450
sealing_key
448-
.seal_in_place_append_tag(Aad::from(aad), &mut in_out_buffer)
451+
.seal_in_place_append_tag(nonce, Aad::from(aad), &mut in_out_buffer)
449452
.map_err(unspecified_err)?;
450453

451454
Ok(in_out_buffer)
@@ -494,11 +497,18 @@ impl<const KEY_SIZE: usize, const KDF_SIZE: usize> HpkeOpener for Opener<KEY_SIZ
494497

495498
let key = UnboundKey::new(self.key_schedule.aead, &self.key_schedule.key.0)
496499
.map_err(unspecified_err)?;
497-
let mut opening_key = OpeningKey::new(key, &mut self.key_schedule);
500+
let opening_key = LessSafeKey::new(key);
498501

499502
let mut in_out_buffer = Vec::from(ciphertext);
500503
let plaintext = opening_key
501-
.open_in_place(Aad::from(aad), &mut in_out_buffer)
504+
.open_in_place(
505+
self.key_schedule.compute_nonce(),
506+
Aad::from(aad),
507+
&mut in_out_buffer,
508+
)
509+
.map_err(unspecified_err)?;
510+
self.key_schedule
511+
.increment_seq_num()
502512
.map_err(unspecified_err)?;
503513

504514
Ok(plaintext.to_vec())
@@ -739,7 +749,7 @@ impl<const KEY_SIZE: usize> KeySchedule<KEY_SIZE> {
739749
/// See [RFC 9180 §5.2 "Encryption and Decryption"][0].
740750
///
741751
/// [0]: https://www.rfc-editor.org/rfc/rfc9180.html#section-5.2
742-
fn compute_nonce(&self) -> [u8; NONCE_LEN] {
752+
fn compute_nonce(&self) -> Nonce {
743753
// def Context<ROLE>.ComputeNonce(seq):
744754
// seq_bytes = I2OSP(seq, Nn)
745755
// return xor(self.base_nonce, seq_bytes)
@@ -755,7 +765,7 @@ impl<const KEY_SIZE: usize> KeySchedule<KEY_SIZE> {
755765
*n ^= b;
756766
}
757767

758-
nonce
768+
Nonce::assume_unique_for_key(nonce)
759769
}
760770

761771
/// See [RFC 9180 §5.2 "Encryption and Decryption"][0].
@@ -782,14 +792,6 @@ impl<const KEY_SIZE: usize> KeySchedule<KEY_SIZE> {
782792
}
783793
}
784794

785-
impl<const KEY_SIZE: usize> NonceSequence for &mut KeySchedule<KEY_SIZE> {
786-
fn advance(&mut self) -> Result<Nonce, aws_lc_rs::error::Unspecified> {
787-
let nonce = self.compute_nonce();
788-
self.increment_seq_num()?;
789-
Nonce::try_assume_unique_for_key(&nonce)
790-
}
791-
}
792-
793795
/// See [RFC 9180 §4 "Cryptographic Dependencies"][0].
794796
///
795797
/// [0]: https://www.rfc-editor.org/rfc/rfc9180.html#section-4
@@ -1031,6 +1033,29 @@ mod tests {
10311033
assert_eq!(suite.fips(), *expected);
10321034
}
10331035
}
1036+
1037+
#[test]
1038+
fn failed_open_does_not_consume_sequence() {
1039+
for suite in ALL_SUPPORTED_SUITES {
1040+
let (pk, sk) = suite.generate_key_pair().unwrap();
1041+
let (enc, mut sealer) = suite
1042+
.setup_sealer(b"example", &pk)
1043+
.unwrap();
1044+
let ct0 = sealer
1045+
.seal(b"aad", b"message 0")
1046+
.unwrap();
1047+
let ct1 = sealer
1048+
.seal(b"aad", b"message 1")
1049+
.unwrap();
1050+
1051+
let mut opener = suite
1052+
.setup_opener(&enc, b"example", &sk)
1053+
.unwrap();
1054+
assert!(opener.open(b"wrong aad", &ct0).is_err());
1055+
assert_eq!(opener.open(b"aad", &ct0).unwrap(), b"message 0");
1056+
assert_eq!(opener.open(b"aad", &ct1).unwrap(), b"message 1");
1057+
}
1058+
}
10341059
}
10351060

10361061
#[cfg(test)]

0 commit comments

Comments
 (0)