Skip to content

Commit

Permalink
Merge pull request #128 from ANSSI-FR/ghash-no-cloned
Browse files Browse the repository at this point in the history
aesgcm: use slice::from_ref to avoid unecessary copy
  • Loading branch information
commial committed Aug 19, 2022
2 parents 7eedffa + eb32d9c commit 05a3a58
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions mla/src/crypto/aesgcm.rs
@@ -1,3 +1,5 @@
use core::slice;

use crate::Error;

use aes::Aes256;
Expand Down Expand Up @@ -90,9 +92,8 @@ impl AesGcm256 {
self.current_block.extend_from_slice(in_block);
// `current_block` length is now BLOCK_SIZE -> update GHash and
// clear it
self.ghash.update(&[GenericArray::clone_from_slice(
self.current_block.as_slice(),
)]);
self.ghash
.update(slice::from_ref(self.current_block.as_slice().into()));
self.current_block.clear();

// Deals with the rest of the data, now aligned on BLOCK_SIZE
Expand All @@ -105,7 +106,8 @@ impl AesGcm256 {
// Interleaved ghash update
for chunk in &mut chunks {
self.cipher.apply_keystream(chunk);
self.ghash.update(&[GenericArray::clone_from_slice(chunk)]);
self.ghash
.update(slice::from_ref(GenericArray::from_slice(chunk)));
}

// Encrypt and save extra encrypted bytes for further GHash computation
Expand Down Expand Up @@ -149,7 +151,8 @@ impl AesGcm256 {

// Interleaved ghash update
for chunk in &mut chunks {
self.ghash.update(&[GenericArray::clone_from_slice(chunk)]);
self.ghash
.update(slice::from_ref(GenericArray::from_slice(chunk)));
self.cipher.apply_keystream(chunk);
}

Expand Down

0 comments on commit 05a3a58

Please sign in to comment.