@@ -18,7 +18,7 @@ use der::{
1818 asn1:: { AnyRef , ObjectIdentifier , OctetStringRef } ,
1919} ;
2020
21- #[ cfg( feature = "rand_core" ) ]
21+ #[ cfg( all ( feature = "pbes2" , feature = " rand_core") ) ]
2222use rand_core:: TryCryptoRng ;
2323
2424#[ cfg( all( feature = "alloc" , feature = "pbes2" ) ) ]
@@ -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 ) ]
7284pub struct Parameters {
@@ -79,15 +91,27 @@ pub struct Parameters {
7991
8092impl Parameters {
8193 /// Default length of an initialization vector.
82- #[ cfg( feature = "rand_core" ) ]
94+ #[ cfg( all ( feature = "pbes2" , feature = " rand_core") ) ]
8395 const DEFAULT_IV_LEN : usize = AES_BLOCK_SIZE ;
8496
8597 /// Default length of a salt for password hashing.
86- #[ cfg( feature = "rand_core" ) ]
98+ #[ cfg( all ( feature = "pbes2" , 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+ #[ must_use]
108+ #[ track_caller]
109+ pub fn generate ( ) -> Self {
110+ Self :: generate_recommended ( & mut getrandom:: SysRng ) . expect ( "random generation failure" )
111+ }
112+
113+ /// Generate PBES2 parameters using the recommended algorithm settings and a randomly generated
114+ /// salt and IV.
91115 ///
92116 /// This is currently an alias for [`Parameters::generate_scrypt`]. See that method
93117 /// for more information.
@@ -109,7 +133,7 @@ impl Parameters {
109133 ///
110134 /// # Errors
111135 /// Returns [`Error::Rng`] in the event the random number generator `R` fails.
112- #[ cfg( feature = "rand_core" ) ]
136+ #[ cfg( all ( feature = "pbes2" , feature = " rand_core") ) ]
113137 pub fn generate_pbkdf2 < R : TryCryptoRng > ( rng : & mut R ) -> Result < Self > {
114138 let mut iv = [ 0u8 ; Self :: DEFAULT_IV_LEN ] ;
115139 rng. try_fill_bytes ( & mut iv) . map_err ( |_| Error :: Rng ) ?;
0 commit comments