From 263e14f3e2131248a0fe13abca32841776918ac3 Mon Sep 17 00:00:00 2001 From: boxdot Date: Fri, 15 Dec 2023 10:42:30 +0100 Subject: [PATCH] replace GenericArray::from_slice by just into() --- libsignal-service/src/account_manager.rs | 13 ++++--------- libsignal-service/src/attachment_cipher.rs | 9 ++------- libsignal-service/src/envelope.rs | 7 ++----- libsignal-service/src/provisioning/cipher.rs | 11 ++--------- 4 files changed, 10 insertions(+), 30 deletions(-) diff --git a/libsignal-service/src/account_manager.rs b/libsignal-service/src/account_manager.rs index 47f715d3b..eb6970f97 100644 --- a/libsignal-service/src/account_manager.rs +++ b/libsignal-service/src/account_manager.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use std::convert::{TryFrom, TryInto}; use std::time::SystemTime; -use aes::cipher::generic_array::GenericArray; use aes::cipher::{KeyIvInit, StreamCipher as _}; use hmac::{Hmac, Mac}; use libsignal_protocol::{ @@ -583,10 +582,8 @@ pub fn encrypt_device_name( let mut ciphertext = plaintext; const IV: [u8; 16] = [0; 16]; - let mut cipher = Aes256Ctr128BE::new( - GenericArray::from_slice(cipher_key.as_slice()), - &IV.into(), - ); + let mut cipher = + Aes256Ctr128BE::new(cipher_key.as_slice().into(), &IV.into()); cipher.apply_keystream(&mut ciphertext); Ok(DeviceName { @@ -612,10 +609,8 @@ pub fn decrypt_device_name( let mut plaintext = ciphertext.to_vec(); const IV: [u8; 16] = [0; 16]; - let mut cipher = Aes256Ctr128BE::new( - GenericArray::from_slice(cipher_key.as_slice()), - &IV.into(), - ); + let mut cipher = + Aes256Ctr128BE::new(cipher_key.as_slice().into(), &IV.into()); cipher.apply_keystream(&mut plaintext); let key1 = calculate_hmac256(&master_secret, b"auth")?; diff --git a/libsignal-service/src/attachment_cipher.rs b/libsignal-service/src/attachment_cipher.rs index 849a50a7f..06eeb2c6e 100644 --- a/libsignal-service/src/attachment_cipher.rs +++ b/libsignal-service/src/attachment_cipher.rs @@ -1,5 +1,4 @@ use aes::cipher::block_padding::Pkcs7; -use aes::cipher::generic_array::GenericArray; use aes::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit}; use hmac::{Hmac, Mac}; use sha2::Sha256; @@ -34,8 +33,7 @@ pub fn encrypt_in_place(iv: [u8; 16], key: [u8; 64], plaintext: &mut Vec) { // Pad with zeroes for padding plaintext.extend(&[0u8; 16]); - let cipher = - Aes256CbcEnc::new(GenericArray::from_slice(aes_half), &iv.into()); + let cipher = Aes256CbcEnc::new(aes_half.into(), &iv.into()); let buffer = plaintext; let ciphertext_slice = cipher @@ -75,10 +73,7 @@ pub fn decrypt_in_place( let (iv, buffer) = buffer.split_at_mut(16); - let cipher = Aes256CbcDec::new( - GenericArray::from_slice(aes_half), - GenericArray::from_slice(iv), - ); + let cipher = Aes256CbcDec::new(aes_half.into(), (&*iv).into()); let plaintext_slice = cipher .decrypt_padded_mut::(buffer) diff --git a/libsignal-service/src/envelope.rs b/libsignal-service/src/envelope.rs index 5f5bd0a8c..c90a36330 100644 --- a/libsignal-service/src/envelope.rs +++ b/libsignal-service/src/envelope.rs @@ -1,7 +1,6 @@ use std::convert::{TryFrom, TryInto}; use aes::cipher::block_padding::Pkcs7; -use aes::cipher::generic_array::GenericArray; use aes::cipher::{BlockDecryptMut, KeyIvInit}; use prost::Message; use uuid::Uuid; @@ -71,10 +70,8 @@ impl Envelope { // libsignal-service-java uses Pkcs5, // but that should not matter. // https://crypto.stackexchange.com/questions/9043/what-is-the-difference-between-pkcs5-padding-and-pkcs7-padding - let cipher = cbc::Decryptor::::new( - GenericArray::from_slice(aes_key), - GenericArray::from_slice(iv), - ); + let cipher = + cbc::Decryptor::::new(aes_key.into(), iv.into()); let input = &input[CIPHERTEXT_OFFSET..(input.len() - MAC_SIZE)]; let input = cipher .decrypt_padded_vec_mut::(input) diff --git a/libsignal-service/src/provisioning/cipher.rs b/libsignal-service/src/provisioning/cipher.rs index 47a52516a..0434408d5 100644 --- a/libsignal-service/src/provisioning/cipher.rs +++ b/libsignal-service/src/provisioning/cipher.rs @@ -1,4 +1,3 @@ -use std::convert::TryInto; use std::fmt::{self, Debug}; use aes::cipher::block_padding::Pkcs7; @@ -103,10 +102,7 @@ impl ProvisioningCipher { let mac_key = &shared_secrets[32..]; let iv: [u8; IV_LENGTH] = rng.gen(); - let cipher = cbc::Encryptor::::new( - aes_key.try_into().expect("fixed length key material"), - &iv.into(), - ); + let cipher = cbc::Encryptor::::new(aes_key.into(), &iv.into()); let ciphertext = cipher.encrypt_padded_vec_mut::(&msg); let mut mac = Hmac::::new_from_slice(mac_key) .expect("HMAC can take any size key"); @@ -180,10 +176,7 @@ impl ProvisioningCipher { // libsignal-service-java uses Pkcs5, // but that should not matter. // https://crypto.stackexchange.com/questions/9043/what-is-the-difference-between-pkcs5-padding-and-pkcs7-padding - let cipher = cbc::Decryptor::::new( - parts1.try_into().expect("fixed length key material"), - iv.try_into().expect("fixed length iv material"), - ); + let cipher = cbc::Decryptor::::new(parts1.into(), iv.into()); let input = cipher .decrypt_padded_vec_mut::(cipher_text) .map_err(|e| ProvisioningError::InvalidData {