@@ -19,7 +19,7 @@ use der::{
1919} ;
2020
2121#[ cfg( feature = "rand_core" ) ]
22- use rand_core:: CryptoRng ;
22+ use rand_core:: TryCryptoRng ;
2323
2424#[ cfg( all( feature = "alloc" , feature = "pbes2" ) ) ]
2525use alloc:: vec:: Vec ;
@@ -89,11 +89,14 @@ impl Parameters {
8989 /// Generate PBES2 parameters using the recommended algorithm settings and
9090 /// a randomly generated salt and IV.
9191 ///
92- /// This is currently an alias for [`Parameters::scrypt `]. See that method
92+ /// This is currently an alias for [`Parameters::generate_scrypt `]. See that method
9393 /// for more information.
94+ ///
95+ /// # Errors
96+ /// Returns [`Error::Rng`] in the event the random number generator `R` fails.
9497 #[ cfg( all( feature = "pbes2" , feature = "rand_core" ) ) ]
95- pub fn recommended < R : CryptoRng > ( rng : & mut R ) -> Self {
96- Self :: scrypt ( rng)
98+ pub fn generate_recommended < R : TryCryptoRng > ( rng : & mut R ) -> Result < Self > {
99+ Self :: generate_scrypt ( rng)
97100 }
98101
99102 /// Generate PBES2 parameters using PBKDF2 as the password hashing
@@ -103,29 +106,31 @@ impl Parameters {
103106 ///
104107 /// This will use AES-256-CBC as the encryption algorithm and SHA-256 as
105108 /// the hash function for PBKDF2.
109+ ///
110+ /// # Errors
111+ /// Returns [`Error::Rng`] in the event the random number generator `R` fails.
106112 #[ cfg( feature = "rand_core" ) ]
107- #[ allow( clippy:: missing_panics_doc, reason = "params should be valid" ) ]
108- pub fn pbkdf2 < R : CryptoRng > ( rng : & mut R ) -> Self {
113+ pub fn generate_pbkdf2 < R : TryCryptoRng > ( rng : & mut R ) -> Result < Self > {
109114 let mut iv = [ 0u8 ; Self :: DEFAULT_IV_LEN ] ;
110- rng. fill_bytes ( & mut iv) ;
115+ rng. try_fill_bytes ( & mut iv) . map_err ( |_| Error :: Rng ) ? ;
111116
112117 let mut salt = [ 0u8 ; Self :: DEFAULT_SALT_LEN ] ;
113- rng. fill_bytes ( & mut salt) ;
118+ rng. try_fill_bytes ( & mut salt) . map_err ( |_| Error :: Rng ) ? ;
114119
115- Self :: pbkdf2_sha256_aes256cbc ( 600_000 , & salt, iv) . expect ( "invalid PBKDF2 parameters" )
120+ Self :: generate_pbkdf2_sha256_aes256cbc ( Pbkdf2Params :: DEFAULT_SHA256_ITERATIONS , & salt, iv)
116121 }
117122
118123 /// Initialize PBES2 parameters using PBKDF2-SHA256 as the password-based
119124 /// key derivation function and AES-128-CBC as the symmetric cipher.
120125 ///
121126 /// # Errors
122- /// Propagates errors from [`Pbkdf2Params::hmac_with_sha256 `].
123- pub fn pbkdf2_sha256_aes128cbc (
127+ /// Propagates errors from [`Pbkdf2Params::hmac_sha256 `].
128+ pub fn generate_pbkdf2_sha256_aes128cbc (
124129 pbkdf2_iterations : u32 ,
125130 pbkdf2_salt : & [ u8 ] ,
126131 aes_iv : [ u8 ; AES_BLOCK_SIZE ] ,
127132 ) -> Result < Self > {
128- let kdf = Pbkdf2Params :: hmac_with_sha256 ( pbkdf2_iterations, pbkdf2_salt) ?. into ( ) ;
133+ let kdf = Pbkdf2Params :: hmac_sha256 ( pbkdf2_iterations, pbkdf2_salt) ?. into ( ) ;
129134 let encryption = EncryptionScheme :: Aes128Cbc { iv : aes_iv } ;
130135 Ok ( Self { kdf, encryption } )
131136 }
@@ -134,13 +139,13 @@ impl Parameters {
134139 /// key derivation function and AES-256-CBC as the symmetric cipher.
135140 ///
136141 /// # Errors
137- /// Propagates errors from [`Pbkdf2Params::hmac_with_sha256 `].
138- pub fn pbkdf2_sha256_aes256cbc (
142+ /// Propagates errors from [`Pbkdf2Params::hmac_sha256 `].
143+ pub fn generate_pbkdf2_sha256_aes256cbc (
139144 pbkdf2_iterations : u32 ,
140145 pbkdf2_salt : & [ u8 ] ,
141146 aes_iv : [ u8 ; AES_BLOCK_SIZE ] ,
142147 ) -> Result < Self > {
143- let kdf = Pbkdf2Params :: hmac_with_sha256 ( pbkdf2_iterations, pbkdf2_salt) ?. into ( ) ;
148+ let kdf = Pbkdf2Params :: hmac_sha256 ( pbkdf2_iterations, pbkdf2_salt) ?. into ( ) ;
144149 let encryption = EncryptionScheme :: Aes256Cbc { iv : aes_iv } ;
145150 Ok ( Self { kdf, encryption } )
146151 }
@@ -161,20 +166,26 @@ impl Parameters {
161166 /// - salt length: 16
162167 ///
163168 /// [RustCrypto/formats#1205]: https://github.com/RustCrypto/formats/issues/1205
169+ ///
170+ /// # Errors
171+ /// Returns [`Error::Rng`] in the event the random number generator `R` fails.
164172 #[ cfg( all( feature = "pbes2" , feature = "rand_core" ) ) ]
165173 #[ cfg( feature = "rand_core" ) ]
166- #[ allow( clippy:: missing_panics_doc, reason = "params should be valid" ) ]
167- pub fn scrypt < R : CryptoRng > ( rng : & mut R ) -> Self {
174+ pub fn generate_scrypt < R : TryCryptoRng > ( rng : & mut R ) -> Result < Self > {
168175 let mut iv = [ 0u8 ; Self :: DEFAULT_IV_LEN ] ;
169- rng. fill_bytes ( & mut iv) ;
176+ rng. try_fill_bytes ( & mut iv) . map_err ( |_| Error :: Rng ) ? ;
170177
171178 let mut salt = [ 0u8 ; Self :: DEFAULT_SALT_LEN ] ;
172- rng. fill_bytes ( & mut salt) ;
179+ rng. try_fill_bytes ( & mut salt) . map_err ( |_| Error :: Rng ) ?;
180+
181+ let params = scrypt:: Params :: new (
182+ ScryptParams :: DEFAULT_LOG_N ,
183+ ScryptParams :: DEFAULT_R ,
184+ ScryptParams :: DEFAULT_P ,
185+ )
186+ . map_err ( |_| Error :: AlgorithmParametersInvalid { oid : SCRYPT_OID } ) ?;
173187
174- scrypt:: Params :: new ( 14 , 8 , 1 )
175- . ok ( )
176- . and_then ( |params| Self :: scrypt_aes256cbc ( params, & salt, iv) . ok ( ) )
177- . expect ( "invalid scrypt parameters" )
188+ Self :: generate_scrypt_aes256cbc ( params, & salt, iv)
178189 }
179190
180191 /// Initialize PBES2 parameters using scrypt as the password-based
@@ -187,7 +198,7 @@ impl Parameters {
187198 /// Propagates errors from [`ScryptParams::from_params_and_salt`].
188199 // TODO(tarcieri): encapsulate `scrypt::Params`?
189200 #[ cfg( feature = "pbes2" ) ]
190- pub fn scrypt_aes128cbc (
201+ pub fn generate_scrypt_aes128cbc (
191202 params : scrypt:: Params ,
192203 salt : & [ u8 ] ,
193204 aes_iv : [ u8 ; AES_BLOCK_SIZE ] ,
@@ -210,7 +221,7 @@ impl Parameters {
210221 /// Propagates errors from [`ScryptParams::from_params_and_salt`].
211222 // TODO(tarcieri): encapsulate `scrypt::Params`?
212223 #[ cfg( feature = "pbes2" ) ]
213- pub fn scrypt_aes256cbc (
224+ pub fn generate_scrypt_aes256cbc (
214225 params : scrypt:: Params ,
215226 salt : & [ u8 ] ,
216227 aes_iv : [ u8 ; AES_BLOCK_SIZE ] ,
0 commit comments