Skip to content

Commit

Permalink
Block::from can consume its argument (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth authored Aug 19, 2024
1 parent 5346709 commit c2612bf
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion aws-lc-rs/src/aead/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn cipher_new_mask(
cipher_key: &SymmetricCipherKey,
sample: Sample,
) -> Result<[u8; 5], error::Unspecified> {
let block = block::Block::from(&sample);
let block = block::Block::from(sample);

let encrypted_block = match cipher_key {
SymmetricCipherKey::Aes128 { enc_key, .. } | SymmetricCipherKey::Aes256 { enc_key, .. } => {
Expand Down
2 changes: 1 addition & 1 deletion aws-lc-rs/src/cipher/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ pub(crate) fn encrypt_block_aes(aes_key: &AES_KEY, block: Block) -> Block {
AES_ENCRYPT,
));

Block::from(&cipher_text.assume_init())
Block::from(cipher_text.assume_init())
}
}
9 changes: 4 additions & 5 deletions aws-lc-rs/src/cipher/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ impl Block {
}
}

impl From<&'_ [u8; BLOCK_LEN]> for Block {
impl From<[u8; BLOCK_LEN]> for Block {
#[inline]
fn from(bytes: &[u8; BLOCK_LEN]) -> Self {
unsafe { core::mem::transmute_copy(bytes) }
fn from(bytes: [u8; BLOCK_LEN]) -> Self {
unsafe { core::mem::transmute(bytes) }
}
}

Expand All @@ -40,11 +40,10 @@ impl AsRef<[u8; BLOCK_LEN]> for Block {

#[cfg(test)]
mod tests {

#[test]
fn test_block_clone() {
use super::{Block, BLOCK_LEN};
let block_a = Block::from(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
let block_a = Block::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
#[allow(clippy::clone_on_copy)]
let block_b = block_a.clone();

Expand Down
2 changes: 1 addition & 1 deletion aws-lc-rs/src/cipher/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(crate) fn encrypt_block_chacha20(

crate::fips::set_fips_service_status_unapproved();

Ok(Block::from(&cipher_text))
Ok(Block::from(cipher_text))
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions aws-lc-rs/src/cipher/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mod tests {
let input_block: [u8; BLOCK_LEN] = <[u8; BLOCK_LEN]>::try_from(input).unwrap();

let aes128 = SymmetricCipherKey::aes128(key.as_slice()).unwrap();
let result = aes128.encrypt_block(Block::from(&input_block));
let result = aes128.encrypt_block(Block::from(input_block));

assert_eq!(expected_result.as_slice(), result.as_ref());
}
Expand All @@ -167,7 +167,7 @@ mod tests {
let input_block: [u8; BLOCK_LEN] = <[u8; BLOCK_LEN]>::try_from(input).unwrap();

let aes128 = SymmetricCipherKey::aes256(key.as_slice()).unwrap();
let result = aes128.encrypt_block(Block::from(&input_block));
let result = aes128.encrypt_block(Block::from(input_block));

assert_eq!(expected_result.as_slice(), result.as_ref());
}
Expand Down

0 comments on commit c2612bf

Please sign in to comment.