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
2 changes: 1 addition & 1 deletion pkcs8/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ spki = { version = "=0.8.0-pre.0" }

# optional dependencies
rand_core = { version = "0.6", optional = true, default-features = false }
pkcs5 = { version = "=0.8.0-pre.0", optional = true }
pkcs5 = { version = "=0.8.0-pre.0", optional = true, features = ["rand_core"] }
subtle = { version = "2", optional = true, default-features = false }

[dev-dependencies]
Expand Down
15 changes: 3 additions & 12 deletions pkcs8/src/encrypted_private_key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use pkcs5::EncryptionScheme;
use der::SecretDocument;

#[cfg(feature = "encryption")]
use {
pkcs5::pbes2,
rand_core::{CryptoRng, RngCore},
};
use {pkcs5::pbes2, rand_core::CryptoRngCore};

#[cfg(feature = "pem")]
use der::pem::PemLabel;
Expand Down Expand Up @@ -64,17 +61,11 @@ impl<'a> EncryptedPrivateKeyInfo<'a> {
/// derived from the provided password.
#[cfg(feature = "encryption")]
pub(crate) fn encrypt(
mut rng: impl CryptoRng + RngCore,
rng: &mut impl CryptoRngCore,
password: impl AsRef<[u8]>,
doc: &[u8],
) -> Result<SecretDocument> {
let mut salt = [0u8; 16];
rng.fill_bytes(&mut salt);

let mut iv = [0u8; 16];
rng.fill_bytes(&mut iv);

let pbes2_params = pbes2::Parameters::scrypt_aes256cbc(Default::default(), &salt, iv)?;
let pbes2_params = pbes2::Parameters::recommended(rng);
EncryptedPrivateKeyInfo::encrypt_with(pbes2_params, password, doc)
}

Expand Down
7 changes: 2 additions & 5 deletions pkcs8/src/private_key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use der::SecretDocument;

#[cfg(feature = "encryption")]
use {
crate::EncryptedPrivateKeyInfo,
der::zeroize::Zeroizing,
pkcs5::pbes2,
rand_core::{CryptoRng, RngCore},
crate::EncryptedPrivateKeyInfo, der::zeroize::Zeroizing, pkcs5::pbes2, rand_core::CryptoRngCore,
};

#[cfg(feature = "pem")]
Expand Down Expand Up @@ -137,7 +134,7 @@ impl<'a> PrivateKeyInfo<'a> {
#[cfg(feature = "encryption")]
pub fn encrypt(
&self,
rng: impl CryptoRng + RngCore,
rng: &mut impl CryptoRngCore,
password: impl AsRef<[u8]>,
) -> Result<SecretDocument> {
let der = Zeroizing::new(self.to_der()?);
Expand Down
9 changes: 3 additions & 6 deletions pkcs8/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use crate::{Error, PrivateKeyInfo, Result};
use der::SecretDocument;

#[cfg(feature = "encryption")]
use {
crate::EncryptedPrivateKeyInfo,
rand_core::{CryptoRng, RngCore},
};
use {crate::EncryptedPrivateKeyInfo, rand_core::CryptoRngCore};

#[cfg(feature = "pem")]
use {
Expand Down Expand Up @@ -106,7 +103,7 @@ pub trait EncodePrivateKey {
#[cfg(feature = "encryption")]
fn to_pkcs8_encrypted_der(
&self,
rng: impl CryptoRng + RngCore,
rng: &mut impl CryptoRngCore,
password: impl AsRef<[u8]>,
) -> Result<SecretDocument> {
EncryptedPrivateKeyInfo::encrypt(rng, password, self.to_pkcs8_der()?.as_bytes())
Expand All @@ -124,7 +121,7 @@ pub trait EncodePrivateKey {
#[cfg(all(feature = "encryption", feature = "pem"))]
fn to_pkcs8_encrypted_pem(
&self,
rng: impl CryptoRng + RngCore,
rng: &mut impl CryptoRngCore,
password: impl AsRef<[u8]>,
line_ending: LineEnding,
) -> Result<Zeroizing<String>> {
Expand Down