Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkcs5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ der = { version = "0.8", features = ["oid"] }
spki = "0.8"

# optional dependencies
aead = { version = "0.6", default-features = false, optional = true }
cbc = { version = "0.2", optional = true }
aes = { version = "0.9", optional = true, default-features = false }
aes-gcm = { version = "0.11", optional = true, default-features = false, features = ["aes"] }
Expand All @@ -40,7 +41,7 @@ alloc = []
3des = ["dep:des", "pbes2"]
des-insecure = ["dep:des", "pbes2"]
getrandom = ["dep:getrandom", "rand_core"]
pbes2 = ["dep:aes", "dep:cbc", "dep:pbkdf2", "dep:scrypt", "dep:sha2", "dep:aes-gcm"]
pbes2 = ["dep:aes", "dep:cbc", "dep:pbkdf2", "dep:scrypt", "dep:sha2", "dep:aes-gcm", "dep:aead"]
rand_core = ["dep:rand_core"]
sha1-insecure = ["dep:sha1", "pbes2"]

Expand Down
74 changes: 33 additions & 41 deletions pkcs5/src/pbes2/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::{EncryptionScheme, Kdf, Parameters, Pbkdf2Params, Pbkdf2Prf, ScryptParams};
use crate::{Error, Result};
use aes_gcm::{KeyInit as GcmKeyInit, Nonce, Tag, aead::AeadInOut};
use aead::{AeadInOut, Nonce, array::typenum::Unsigned};
use cbc::cipher::{
BlockCipherDecrypt, BlockCipherEncrypt, BlockModeDecrypt, BlockModeEncrypt, KeyInit, KeyIvInit,
block_padding::Pkcs7,
Expand All @@ -20,6 +20,9 @@ use pbkdf2::{
};
use scrypt::scrypt;

type Aes128Gcm = aes_gcm::AesGcm<aes::Aes128Enc, U12, U16>;
type Aes256Gcm = aes_gcm::AesGcm<aes::Aes256Enc, U12, U16>;

/// Maximum size of a derived encryption key
const MAX_KEY_LEN: usize = 32;

Expand Down Expand Up @@ -48,61 +51,50 @@ fn cbc_decrypt<'a, C: BlockCipherDecrypt + KeyInit>(
.map_err(|_| Error::DecryptFailed)
}

fn gcm_encrypt<C, NonceSize, TagSize>(
fn aead_encrypt<'a, A>(
es: EncryptionScheme,
key: EncryptionKey,
nonce: Nonce<NonceSize>,
buffer: &mut [u8],
key: &EncryptionKey,
nonce: &Nonce<A>,
buffer: &'a mut [u8],
pos: usize,
) -> Result<&[u8]>
) -> Result<&'a [u8]>
where
C: BlockSizeUser<BlockSize = U16> + GcmKeyInit + BlockCipherEncrypt,
aes_gcm::AesGcm<C, NonceSize, TagSize>: GcmKeyInit,
TagSize: aes_gcm::TagSize,
NonceSize: aes::cipher::array::ArraySize,
A: AeadInOut + KeyInit,
{
if buffer.len() < TagSize::USIZE + pos {
if buffer.len() < A::TagSize::USIZE + pos {
return Err(Error::EncryptFailed);
}
let gcm =
<aes_gcm::AesGcm<C, NonceSize, TagSize> as GcmKeyInit>::new_from_slice(key.as_slice())
.map_err(|_| es.to_alg_params_invalid())?;
let tag = gcm
.encrypt_inout_detached(&nonce, &[], (&mut buffer[..pos]).into())

let aead = A::new_from_slice(key.as_slice()).map_err(|_| es.to_alg_params_invalid())?;

let tag = aead
.encrypt_inout_detached(nonce, &[], (&mut buffer[..pos]).into())
.map_err(|_| Error::EncryptFailed)?;

buffer[pos..].copy_from_slice(tag.as_ref());
Ok(&buffer[0..pos + TagSize::USIZE])
Ok(&buffer[0..pos + A::TagSize::USIZE])
}

fn gcm_decrypt<C, NonceSize, TagSize>(
fn aead_decrypt<'a, A>(
es: EncryptionScheme,
key: EncryptionKey,
nonce: Nonce<NonceSize>,
buffer: &mut [u8],
) -> Result<&[u8]>
key: &EncryptionKey,
nonce: &Nonce<A>,
buffer: &'a mut [u8],
) -> Result<&'a [u8]>
where
C: BlockSizeUser<BlockSize = U16> + GcmKeyInit + BlockCipherEncrypt,
aes_gcm::AesGcm<C, NonceSize, TagSize>: GcmKeyInit,
TagSize: aes_gcm::TagSize,
NonceSize: aes::cipher::array::ArraySize,
A: AeadInOut + KeyInit,
{
let msg_len = buffer
.len()
.checked_sub(TagSize::USIZE)
.checked_sub(A::TagSize::USIZE)
.ok_or(Error::DecryptFailed)?;

let gcm =
<aes_gcm::AesGcm<C, NonceSize, TagSize> as GcmKeyInit>::new_from_slice(key.as_slice())
.map_err(|_| es.to_alg_params_invalid())?;
let aead = A::new_from_slice(key.as_slice()).map_err(|_| es.to_alg_params_invalid())?;

let tag = Tag::try_from(&buffer[msg_len..]).map_err(|_| Error::DecryptFailed)?;
let tag = aead::Tag::<A>::try_from(&buffer[msg_len..]).map_err(|_| Error::DecryptFailed)?;

if gcm
.decrypt_inout_detached(&nonce, &[], (&mut buffer[..msg_len]).into(), &tag)
.is_err()
{
return Err(Error::DecryptFailed);
}
aead.decrypt_inout_detached(nonce, &[], (&mut buffer[..msg_len]).into(), &tag)
.map_err(|_| Error::DecryptFailed)?;

Ok(&buffer[..msg_len])
}
Expand All @@ -125,10 +117,10 @@ pub fn encrypt_in_place<'b>(
EncryptionScheme::Aes192Cbc { iv } => cbc_encrypt::<aes::Aes192Enc>(es, key, &iv, buf, pos),
EncryptionScheme::Aes256Cbc { iv } => cbc_encrypt::<aes::Aes256Enc>(es, key, &iv, buf, pos),
EncryptionScheme::Aes128Gcm { nonce } => {
gcm_encrypt::<aes::Aes128Enc, U12, U16>(es, key, Nonce::from(nonce), buf, pos)
aead_encrypt::<Aes128Gcm>(es, &key, &Nonce::<Aes128Gcm>::from(nonce), buf, pos)
}
EncryptionScheme::Aes256Gcm { nonce } => {
gcm_encrypt::<aes::Aes256Enc, U12, U16>(es, key, Nonce::from(nonce), buf, pos)
aead_encrypt::<Aes256Gcm>(es, &key, &Nonce::<Aes256Gcm>::from(nonce), buf, pos)
}
#[cfg(feature = "3des")]
EncryptionScheme::DesEde3Cbc { iv } => cbc_encrypt::<des::TdesEde3>(es, key, &iv, buf, pos),
Expand All @@ -153,10 +145,10 @@ pub fn decrypt_in_place<'a>(
EncryptionScheme::Aes192Cbc { iv } => cbc_decrypt::<aes::Aes192Dec>(es, key, &iv, buf),
EncryptionScheme::Aes256Cbc { iv } => cbc_decrypt::<aes::Aes256Dec>(es, key, &iv, buf),
EncryptionScheme::Aes128Gcm { nonce } => {
gcm_decrypt::<aes::Aes128Enc, U12, U16>(es, key, Nonce::from(nonce), buf)
aead_decrypt::<Aes128Gcm>(es, &key, &Nonce::<Aes128Gcm>::from(nonce), buf)
}
EncryptionScheme::Aes256Gcm { nonce } => {
gcm_decrypt::<aes::Aes256Enc, U12, U16>(es, key, Nonce::from(nonce), buf)
aead_decrypt::<Aes256Gcm>(es, &key, &Nonce::<Aes256Gcm>::from(nonce), buf)
}
#[cfg(feature = "3des")]
EncryptionScheme::DesEde3Cbc { iv } => cbc_decrypt::<des::TdesEde3>(es, key, &iv, buf),
Expand Down
Loading