Skip to content

Commit

Permalink
Replace try_into by GenericArray::from_slice
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot committed Dec 15, 2023
1 parent e790b4f commit bf6fd81
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
14 changes: 4 additions & 10 deletions libsignal-service/src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -583,10 +584,7 @@ pub fn encrypt_device_name<R: rand::Rng + rand::CryptoRng>(

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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");

Expand Down
13 changes: 5 additions & 8 deletions libsignal-service/src/attachment_cipher.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,10 +34,8 @@ 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(
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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions libsignal-service/src/envelope.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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::<aes::Aes256>::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
Expand Down

0 comments on commit bf6fd81

Please sign in to comment.