Skip to content

Commit

Permalink
replace GenericArray::from_slice by just into()
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot committed Dec 15, 2023
1 parent bf6fd81 commit 263e14f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 30 deletions.
13 changes: 4 additions & 9 deletions libsignal-service/src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -583,10 +582,8 @@ pub fn encrypt_device_name<R: rand::Rng + rand::CryptoRng>(
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 {
Expand All @@ -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")?;
Expand Down
9 changes: 2 additions & 7 deletions libsignal-service/src/attachment_cipher.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,8 +33,7 @@ pub fn encrypt_in_place(iv: [u8; 16], key: [u8; 64], plaintext: &mut Vec<u8>) {
// 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
Expand Down Expand Up @@ -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::<Pkcs7>(buffer)
Expand Down
7 changes: 2 additions & 5 deletions libsignal-service/src/envelope.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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::<aes::Aes256>::new(
GenericArray::from_slice(aes_key),
GenericArray::from_slice(iv),
);
let cipher =
cbc::Decryptor::<aes::Aes256>::new(aes_key.into(), iv.into());
let input = &input[CIPHERTEXT_OFFSET..(input.len() - MAC_SIZE)];
let input = cipher
.decrypt_padded_vec_mut::<Pkcs7>(input)
Expand Down
11 changes: 2 additions & 9 deletions libsignal-service/src/provisioning/cipher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::convert::TryInto;
use std::fmt::{self, Debug};

use aes::cipher::block_padding::Pkcs7;
Expand Down Expand Up @@ -103,10 +102,7 @@ impl ProvisioningCipher {
let mac_key = &shared_secrets[32..];
let iv: [u8; IV_LENGTH] = rng.gen();

let cipher = cbc::Encryptor::<Aes256>::new(
aes_key.try_into().expect("fixed length key material"),
&iv.into(),
);
let cipher = cbc::Encryptor::<Aes256>::new(aes_key.into(), &iv.into());
let ciphertext = cipher.encrypt_padded_vec_mut::<Pkcs7>(&msg);
let mut mac = Hmac::<Sha256>::new_from_slice(mac_key)
.expect("HMAC can take any size key");
Expand Down Expand Up @@ -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::<Aes256>::new(
parts1.try_into().expect("fixed length key material"),
iv.try_into().expect("fixed length iv material"),
);
let cipher = cbc::Decryptor::<Aes256>::new(parts1.into(), iv.into());
let input = cipher
.decrypt_padded_vec_mut::<Pkcs7>(cipher_text)
.map_err(|e| ProvisioningError::InvalidData {
Expand Down

0 comments on commit 263e14f

Please sign in to comment.