diff --git a/libsignal-service/src/account_manager.rs b/libsignal-service/src/account_manager.rs index fe3437bbd..47f715d3b 100644 --- a/libsignal-service/src/account_manager.rs +++ b/libsignal-service/src/account_manager.rs @@ -2,6 +2,7 @@ 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 +584,7 @@ pub fn encrypt_device_name( const IV: [u8; 16] = [0; 16]; let mut cipher = Aes256Ctr128BE::new( - cipher_key - .as_slice() - .try_into() - .expect("fixed length key material"), + GenericArray::from_slice(cipher_key.as_slice()), &IV.into(), ); cipher.apply_keystream(&mut ciphertext); @@ -615,10 +613,7 @@ pub fn decrypt_device_name( let mut plaintext = ciphertext.to_vec(); const IV: [u8; 16] = [0; 16]; let mut cipher = Aes256Ctr128BE::new( - cipher_key - .as_slice() - .try_into() - .expect("fixed length key material"), + GenericArray::from_slice(cipher_key.as_slice()), &IV.into(), ); cipher.apply_keystream(&mut plaintext); @@ -678,8 +673,7 @@ mod tests { }; let decrypted_device_name = - super::decrypt_device_name(&ephemeral_private_key, &device_name) - .unwrap(); + super::decrypt_device_name(&ephemeral_private_key, &device_name)?; assert_eq!(decrypted_device_name, "Nokia 3310 Millenial Edition"); diff --git a/libsignal-service/src/attachment_cipher.rs b/libsignal-service/src/attachment_cipher.rs index 83957d1c4..849a50a7f 100644 --- a/libsignal-service/src/attachment_cipher.rs +++ b/libsignal-service/src/attachment_cipher.rs @@ -1,6 +1,5 @@ -use std::convert::TryInto; - 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; @@ -35,10 +34,8 @@ 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( - aes_half.try_into().expect("fixed length key material"), - &iv.into(), - ); + let cipher = + Aes256CbcEnc::new(GenericArray::from_slice(aes_half), &iv.into()); let buffer = plaintext; let ciphertext_slice = cipher @@ -79,8 +76,8 @@ pub fn decrypt_in_place( let (iv, buffer) = buffer.split_at_mut(16); let cipher = Aes256CbcDec::new( - aes_half.try_into().expect("fixed length key material"), - (&*iv).try_into().expect("fixed length iv material"), + GenericArray::from_slice(aes_half), + GenericArray::from_slice(iv), ); let plaintext_slice = cipher diff --git a/libsignal-service/src/envelope.rs b/libsignal-service/src/envelope.rs index 1b014010b..5f5bd0a8c 100644 --- a/libsignal-service/src/envelope.rs +++ b/libsignal-service/src/envelope.rs @@ -1,6 +1,7 @@ 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,8 +72,8 @@ impl Envelope { // 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( - aes_key.try_into().expect("fixed length key material"), - iv.try_into().expect("fixed length iv material"), + GenericArray::from_slice(aes_key), + GenericArray::from_slice(iv), ); let input = &input[CIPHERTEXT_OFFSET..(input.len() - MAC_SIZE)]; let input = cipher