Skip to content

Commit a3f5dce

Browse files
larseggertCopilot
authored andcommitted
perf(crypto): eliminate bounds checks in header protection mask computation (#3049)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b1cfa03 commit a3f5dce

2 files changed

Lines changed: 4 additions & 20 deletions

File tree

src/hp.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,12 @@ impl Key {
138138
///
139139
/// # Errors
140140
///
141-
/// An error is returned if the NSS functions fail; a sample of the
142-
/// wrong size is the obvious cause.
141+
/// An error is returned if the NSS functions fail.
143142
///
144143
/// # Panics
145144
///
146145
/// When the mechanism for our key is not supported.
147-
/// Or when the sample provided is not at least `self.sample_size()` bytes.
148-
pub fn mask(&self, sample: &[u8]) -> Res<[u8; Self::SAMPLE_SIZE]> {
146+
pub fn mask(&self, sample: &[u8; Self::SAMPLE_SIZE]) -> Res<[u8; Self::SAMPLE_SIZE]> {
149147
let mut output = [0; Self::SAMPLE_SIZE];
150148

151149
match self {
@@ -157,7 +155,7 @@ impl Key {
157155
output.as_mut_ptr(),
158156
&mut output_len,
159157
c_int::try_from(output.len())?,
160-
sample[..Self::SAMPLE_SIZE].as_ptr().cast(),
158+
sample.as_ptr().cast(),
161159
c_int::try_from(Self::SAMPLE_SIZE)?,
162160
)
163161
})?;
@@ -169,7 +167,7 @@ impl Key {
169167
let params: CK_CHACHA20_PARAMS = CK_CHACHA20_PARAMS {
170168
pBlockCounter: sample.as_ptr().cast_mut(),
171169
blockCounterBits: 32,
172-
pNonce: sample[4..Self::SAMPLE_SIZE].as_ptr().cast_mut(),
170+
pNonce: sample[4..].as_ptr().cast_mut(),
173171
ulNonceBits: 96,
174172
};
175173
let mut output_len: c_uint = 0;

tests/hp.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,3 @@ fn chacha20_ctr() {
6767

6868
hp_test(TLS_CHACHA20_POLY1305_SHA256, EXPECTED);
6969
}
70-
71-
#[test]
72-
#[should_panic(expected = "out of range")]
73-
fn aes_short() {
74-
let hp = make_hp(TLS_AES_128_GCM_SHA256);
75-
drop(hp.mask(&[0; 15]));
76-
}
77-
78-
#[test]
79-
#[should_panic(expected = "out of range")]
80-
fn chacha20_short() {
81-
let hp = make_hp(TLS_CHACHA20_POLY1305_SHA256);
82-
drop(hp.mask(&[0; 15]));
83-
}

0 commit comments

Comments
 (0)