Skip to content

Commit c5b103f

Browse files
bors[bot]kedars
andauthored
Merge #177
177: Create in-place variants for encrypt_auth/decrypt_auth r=jethrogb a=kedars This is invaluable for embedded systems Co-authored-by: Kedar Sovani <[email protected]>
2 parents 4fb6294 + 98e59cf commit c5b103f

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

mbedtls/src/cipher/mod.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,19 @@ impl Cipher<Encryption, Authenticated, AdditionalData> {
286286
self.change_state(),
287287
))
288288
}
289+
290+
pub fn encrypt_auth_inplace(
291+
mut self,
292+
ad: &[u8],
293+
data: &mut [u8],
294+
tag: &mut [u8],
295+
) -> Result<(usize, Cipher<Encryption, Authenticated, Finished>)> {
296+
Ok((
297+
self.raw_cipher
298+
.encrypt_auth_inplace(ad, data, tag)?,
299+
self.change_state(),
300+
))
301+
}
289302
}
290303

291304
impl Cipher<Decryption, Authenticated, AdditionalData> {
@@ -302,6 +315,19 @@ impl Cipher<Decryption, Authenticated, AdditionalData> {
302315
self.change_state(),
303316
))
304317
}
318+
319+
pub fn decrypt_auth_inplace(
320+
mut self,
321+
ad: &[u8],
322+
data: &mut [u8],
323+
tag: &[u8],
324+
) -> Result<(usize, Cipher<Decryption, Authenticated, Finished>)> {
325+
Ok((
326+
self.raw_cipher
327+
.decrypt_auth_inplace(ad, data, tag)?,
328+
self.change_state(),
329+
))
330+
}
305331
}
306332

307333
impl<O: Operation, T: Type> Cipher<O, T, CipherData> {
@@ -401,6 +427,44 @@ fn ccm() {
401427
assert_eq!(p, p_out);
402428
}
403429

430+
#[test]
431+
fn ccm_inplace() {
432+
// Example vector C.1
433+
let k = [
434+
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
435+
0x4f,
436+
];
437+
let iv = [0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16];
438+
let ad = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
439+
let mut c = [0x20, 0x21, 0x22, 0x23, 0x0, 0x0, 0x0, 0x0];
440+
let validate_cipher = [0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d];
441+
let validate_plain = [0x20, 0x21, 0x22, 0x23];
442+
443+
let cipher = Cipher::<_, Authenticated, _>::new(
444+
raw::CipherId::Aes,
445+
raw::CipherMode::CCM,
446+
(k.len() * 8) as _,
447+
)
448+
.unwrap();
449+
let cipher = cipher.set_key_iv(&k, &iv).unwrap();
450+
let (data, tag) = c.split_at_mut(4);
451+
cipher
452+
.encrypt_auth_inplace(&ad, data, tag)
453+
.unwrap();
454+
assert_eq!(c, validate_cipher);
455+
456+
let cipher = Cipher::<_, Authenticated, _>::new(
457+
raw::CipherId::Aes,
458+
raw::CipherMode::CCM,
459+
(k.len() * 8) as _,
460+
)
461+
.unwrap();
462+
let cipher = cipher.set_key_iv(&k, &iv).unwrap();
463+
let (data, tag) = c.split_at_mut(4);
464+
cipher.decrypt_auth_inplace(&ad, data, tag).unwrap();
465+
assert_eq!(validate_plain, data);
466+
}
467+
404468
#[test]
405469
fn aes_kw() {
406470
let k = [0x75, 0x75, 0xda, 0x3a, 0x93, 0x60, 0x7c, 0xc2, 0xbf, 0xd8, 0xce, 0xc7, 0xaa, 0xdf, 0xd9, 0xa6];

mbedtls/src/cipher/raw/mod.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,66 @@ impl Cipher {
387387
Ok(plain_len)
388388
}
389389

390+
pub fn encrypt_auth_inplace(
391+
&mut self,
392+
ad: &[u8],
393+
data: &mut [u8],
394+
tag: &mut [u8],
395+
) -> Result<usize> {
396+
397+
let iv = self.inner.iv;
398+
let iv_len = self.inner.iv_size;
399+
let mut olen = data.len();
400+
unsafe {
401+
cipher_auth_encrypt(
402+
&mut self.inner,
403+
iv.as_ptr(),
404+
iv_len,
405+
ad.as_ptr(),
406+
ad.len(),
407+
data.as_ptr(),
408+
data.len(),
409+
data.as_mut_ptr(),
410+
&mut olen,
411+
tag.as_mut_ptr(),
412+
tag.len(),
413+
)
414+
.into_result()?
415+
};
416+
417+
Ok(olen)
418+
}
419+
420+
pub fn decrypt_auth_inplace(
421+
&mut self,
422+
ad: &[u8],
423+
data: &mut [u8],
424+
tag: &[u8],
425+
) -> Result<usize> {
426+
427+
let iv = self.inner.iv;
428+
let iv_len = self.inner.iv_size;
429+
let mut plain_len = data.len();
430+
unsafe {
431+
cipher_auth_decrypt(
432+
&mut self.inner,
433+
iv.as_ptr(),
434+
iv_len,
435+
ad.as_ptr(),
436+
ad.len(),
437+
data.as_ptr(),
438+
data.len(),
439+
data.as_mut_ptr(),
440+
&mut plain_len,
441+
tag.as_ptr(),
442+
tag.len(),
443+
)
444+
.into_result()?
445+
};
446+
447+
Ok(plain_len)
448+
}
449+
390450
fn do_crypto(&mut self, indata: &[u8], outdata: &mut [u8]) -> Result<usize> {
391451
self.reset()?;
392452

0 commit comments

Comments
 (0)