Skip to content

Commit fce8bd5

Browse files
committed
pkcs5: getrandom feature
Adds simplified infallible random generation APIs: - `EncryptionScheme::generate` - `pbes2::Parameters::generate`
1 parent 4556972 commit fce8bd5

4 files changed

Lines changed: 49 additions & 5 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkcs5/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ aes = { version = "0.9", optional = true, default-features = false }
2424
cbc = { version = "0.2", optional = true }
2525
des = { version = "0.9", optional = true, default-features = false }
2626
pbkdf2 = { version = "0.13", optional = true, default-features = false, features = ["hmac"] }
27+
getrandom = { version = "0.4", optional = true, features = ["sys_rng"] }
2728
rand_core = { version = "0.10", optional = true, default-features = false }
2829
scrypt = { version = "0.12", optional = true, default-features = false }
2930
sha1 = { version = "0.11", optional = true, default-features = false }
@@ -37,7 +38,9 @@ alloc = []
3738

3839
3des = ["dep:des", "pbes2"]
3940
des-insecure = ["dep:des", "pbes2"]
41+
getrandom = ["dep:getrandom", "rand_core"]
4042
pbes2 = ["dep:aes", "dep:cbc", "dep:pbkdf2", "dep:scrypt", "dep:sha2"]
43+
rand_core = ["dep:rand_core"]
4144
sha1-insecure = ["dep:sha1", "pbes2"]
4245

4346
[lints]

pkcs5/src/lib.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ pub use scrypt;
4040
#[cfg(all(feature = "alloc", feature = "pbes2"))]
4141
use alloc::vec::Vec;
4242

43-
/// Supported PKCS#5 password-based encryption schemes.
43+
/// Configuration for supported PKCS#5 password-based encryption schemes.
44+
///
45+
/// <div class="warning">
46+
/// <strong>Security Warning</strong>
47+
///
48+
/// This type should not be used to encrypt multiple plaintexts under the same IV/salt values.
49+
///
50+
/// Instead, new values should be randomly generated for every usage.
51+
/// </div>
4452
#[derive(Clone, Debug, Eq, PartialEq)]
4553
#[non_exhaustive]
4654
#[allow(clippy::large_enum_variant)]
@@ -57,13 +65,23 @@ pub enum EncryptionScheme {
5765
}
5866

5967
impl EncryptionScheme {
68+
/// Generate PBES2 parameters using recommended algorithm settings and parameters (salt/IV)
69+
/// generated using the system's secure random number generator.
70+
///
71+
/// # Panics
72+
/// In the event the system's secure random generator experiences an internal failure.
73+
#[cfg(all(feature = "pbes2", feature = "getrandom"))]
74+
pub fn generate() -> Self {
75+
Self::Pbes2(pbes2::Parameters::generate())
76+
}
77+
6078
/// Attempt to decrypt the given ciphertext, allocating and returning a byte vector containing
6179
/// the plaintext.
6280
///
6381
/// # Errors
6482
/// Returns an error if the algorithm specified in this scheme's parameters is unsupported
65-
/// (e.g. PBES1 is completely unsupported), or if the ciphertext is malformed (e.g. not a
66-
/// multiple of a block mode's padding).
83+
/// (e.g. PBES1 is completely unsupported), or if the ciphertext is malformed (e.g. ciphertext
84+
/// length is not a multiple of a block mode's padding).
6785
#[cfg(all(feature = "alloc", feature = "pbes2"))]
6886
pub fn decrypt(&self, password: impl AsRef<[u8]>, ciphertext: &[u8]) -> Result<Vec<u8>> {
6987
match self {

pkcs5/src/pbes2.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ const DES_BLOCK_SIZE: usize = 8;
6767
/// encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} }
6868
/// ```
6969
///
70+
/// These define a set of algorithms for password-based key derivation, as well as a salt value
71+
/// (typically randomly generated) to provide to the KDF algorithm, along with an encryption
72+
/// algorithm and its associated IV/nonce (typically randomly generated).
73+
///
74+
/// <div class="warning">
75+
/// <strong>Security Warning</strong>
76+
///
77+
/// This type should not be used to encrypt multiple plaintexts under the same IV/salt values.
78+
///
79+
/// Instead, new values should be randomly generated for every usage.
80+
/// </div>
81+
///
7082
/// [RFC 8018 Appendix A.4]: https://tools.ietf.org/html/rfc8018#appendix-A.4
7183
#[derive(Clone, Debug, Eq, PartialEq)]
7284
pub struct Parameters {
@@ -86,8 +98,18 @@ impl Parameters {
8698
#[cfg(feature = "rand_core")]
8799
const DEFAULT_SALT_LEN: usize = 16;
88100

89-
/// Generate PBES2 parameters using the recommended algorithm settings and
90-
/// a randomly generated salt and IV.
101+
/// Generate PBES2 parameters using recommended algorithm settings and parameters (salt/IV)
102+
/// generated using the system's secure random number generator.
103+
///
104+
/// # Panics
105+
/// In the event the system's secure random generator experiences an internal failure.
106+
#[cfg(all(feature = "pbes2", feature = "getrandom"))]
107+
pub fn generate() -> Self {
108+
Self::generate_recommended(&mut getrandom::SysRng).expect("random generation failure")
109+
}
110+
111+
/// Generate PBES2 parameters using the recommended algorithm settings and a randomly generated
112+
/// salt and IV.
91113
///
92114
/// This is currently an alias for [`Parameters::generate_scrypt`]. See that method
93115
/// for more information.

0 commit comments

Comments
 (0)