Skip to content

Commit 2d5be20

Browse files
committed
Convert CipherCtx fns into a safe abstraction. Additional testing.
1 parent 93270a6 commit 2d5be20

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

boring/src/ssl/test/session_resumption.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ fn test_noop_tickey_key_callback(
153153
encrypt: bool,
154154
) -> TicketKeyCallbackResult {
155155
// These should only be used for testing purposes.
156+
const TEST_KEY_NAME: [u8; 16] = [5; 16];
156157
const TEST_CBC_IV: [u8; ffi::EVP_MAX_IV_LENGTH as usize] = [1; ffi::EVP_MAX_IV_LENGTH as usize];
157158
const TEST_AES_128_CBC_KEY: [u8; 16] = [2; 16];
158159
const TEST_HMAC_KEY: [u8; 32] = [3; 32];
@@ -161,24 +162,29 @@ fn test_noop_tickey_key_callback(
161162
let cipher = Cipher::aes_128_cbc();
162163

163164
if encrypt {
165+
NOOP_ENCRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
166+
167+
// Ensure key_name and iv are initialized and set test values.
164168
assert_eq!(key_name, &[0; 16]);
165169
assert_eq!(iv, &[0; 16]);
166-
167-
NOOP_ENCRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
170+
key_name.copy_from_slice(&TEST_KEY_NAME);
171+
iv.copy_from_slice(&TEST_CBC_IV);
168172

169173
// Set the encryption context.
170-
unsafe {
171-
evp_ctx
172-
.init_encrypt(&cipher, &TEST_AES_128_CBC_KEY, &TEST_CBC_IV)
173-
.unwrap()
174-
};
174+
evp_ctx
175+
.init_encrypt(&cipher, &TEST_AES_128_CBC_KEY, &TEST_CBC_IV)
176+
.unwrap();
175177

176178
// Set the hmac context.
177179
unsafe { hmac_ctx.init(&TEST_HMAC_KEY, &digest).unwrap() };
178180

179181
TicketKeyCallbackResult::Success
180182
} else {
181183
NOOP_DECRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
184+
185+
// Check key_name matches.
186+
assert_eq!(key_name, &TEST_KEY_NAME);
187+
182188
TicketKeyCallbackResult::Noop
183189
}
184190
}
@@ -193,6 +199,7 @@ fn test_success_tickey_key_callback(
193199
encrypt: bool,
194200
) -> TicketKeyCallbackResult {
195201
// These should only be used for testing purposes.
202+
const TEST_KEY_NAME: [u8; 16] = [5; 16];
196203
const TEST_CBC_IV: [u8; ffi::EVP_MAX_IV_LENGTH as usize] = [1; ffi::EVP_MAX_IV_LENGTH as usize];
197204
const TEST_AES_128_CBC_KEY: [u8; 16] = [2; 16];
198205
const TEST_HMAC_KEY: [u8; 32] = [3; 32];
@@ -201,28 +208,31 @@ fn test_success_tickey_key_callback(
201208
let cipher = Cipher::aes_128_cbc();
202209

203210
if encrypt {
211+
SUCCESS_ENCRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
212+
213+
// Ensure key_name and iv are initialized and set test values.
204214
assert_eq!(key_name, &[0; 16]);
205215
assert_eq!(iv, &[0; 16]);
206-
207-
SUCCESS_ENCRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
216+
key_name.copy_from_slice(&TEST_KEY_NAME);
217+
iv.copy_from_slice(&TEST_CBC_IV);
208218

209219
// Set the encryption context.
210-
unsafe {
211-
evp_ctx
212-
.init_encrypt(&cipher, &TEST_AES_128_CBC_KEY, &TEST_CBC_IV)
213-
.unwrap()
214-
};
220+
evp_ctx
221+
.init_encrypt(&cipher, &TEST_AES_128_CBC_KEY, &TEST_CBC_IV)
222+
.unwrap();
215223

216224
// Set the hmac context.
217225
unsafe { hmac_ctx.init(&TEST_HMAC_KEY, &digest).unwrap() };
218226
} else {
219227
SUCCESS_DECRYPTION_CALLED_BACK.fetch_add(1, Ordering::SeqCst);
228+
229+
// Check key_name matches.
230+
assert_eq!(key_name, &TEST_KEY_NAME);
231+
220232
// Set the decryption context.
221-
unsafe {
222-
evp_ctx
223-
.init_decrypt(&cipher, &TEST_AES_128_CBC_KEY, &TEST_CBC_IV)
224-
.unwrap()
225-
};
233+
evp_ctx
234+
.init_decrypt(&cipher, &TEST_AES_128_CBC_KEY, iv)
235+
.unwrap();
226236

227237
// Set the hmac context.
228238
unsafe { hmac_ctx.init(&TEST_HMAC_KEY, &digest).unwrap() };

boring/src/symm.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,18 @@ impl CipherCtxRef {
8080
/// Configures CipherCtx for a fresh encryption operation using `cipher`.
8181
///
8282
/// https://commondatastorage.googleapis.com/chromium-boringssl-docs/cipher.h.html#EVP_EncryptInit_ex
83-
///
84-
/// # Safety
85-
///
86-
/// The caller must ensure EVP_CIPHER_CTX has been initalized.
87-
///
88-
/// The caller is responsible for ensuring the length of `key` and `iv` are appropriate for the
89-
/// chosen Cipher.
90-
pub unsafe fn init_encrypt(
83+
pub fn init_encrypt(
9184
&mut self,
9285
cipher: &Cipher,
9386
key: &[u8],
9487
iv: &[u8; ffi::EVP_MAX_IV_LENGTH as usize],
9588
) -> Result<(), ErrorStack> {
9689
ffi::init();
9790

91+
if key.len() != cipher.key_len() {
92+
return Err(ErrorStack::get());
93+
}
94+
9895
unsafe {
9996
cvt(ffi::EVP_EncryptInit_ex(
10097
self.as_ptr(),
@@ -111,21 +108,18 @@ impl CipherCtxRef {
111108
/// Configures CipherCtx for a fresh decryption operation using `cipher`.
112109
///
113110
/// https://commondatastorage.googleapis.com/chromium-boringssl-docs/cipher.h.html#EVP_DecryptInit_ex
114-
///
115-
/// # Safety
116-
///
117-
/// The caller must ensure EVP_CIPHER_CTX has been initalized.
118-
///
119-
/// The caller is responsible for ensuring the length of `key` and `iv` are appropriate for the
120-
/// chosen Cipher.
121-
pub unsafe fn init_decrypt(
111+
pub fn init_decrypt(
122112
&mut self,
123113
cipher: &Cipher,
124114
key: &[u8],
125115
iv: &[u8; ffi::EVP_MAX_IV_LENGTH as usize],
126116
) -> Result<(), ErrorStack> {
127117
ffi::init();
128118

119+
if key.len() != cipher.key_len() {
120+
return Err(ErrorStack::get());
121+
}
122+
129123
unsafe {
130124
cvt(ffi::EVP_DecryptInit_ex(
131125
self.as_ptr(),

0 commit comments

Comments
 (0)