@@ -171,17 +171,21 @@ int EVP_PBE_scrypt(const char *pass, size_t passlen,
171171 if (r == 0 || p == 0 || N < 2 || (N & (N - 1 )))
172172 return 0 ;
173173 /* Check p * r < SCRYPT_PR_MAX avoiding overflow */
174- if (p > SCRYPT_PR_MAX / r )
174+ if (p > SCRYPT_PR_MAX / r ) {
175+ EVPerr (EVP_F_EVP_PBE_SCRYPT , EVP_R_MEMORY_LIMIT_EXCEEDED );
175176 return 0 ;
177+ }
176178
177179 /*
178180 * Need to check N: if 2^(128 * r / 8) overflows limit this is
179181 * automatically satisfied since N <= UINT64_MAX.
180182 */
181183
182184 if (16 * r <= LOG2_UINT64_MAX ) {
183- if (N >= (((uint64_t )1 ) << (16 * r )))
185+ if (N >= (((uint64_t )1 ) << (16 * r ))) {
186+ EVPerr (EVP_F_EVP_PBE_SCRYPT , EVP_R_MEMORY_LIMIT_EXCEEDED );
184187 return 0 ;
188+ }
185189 }
186190
187191 /* Memory checks: check total allocated buffer size fits in uint64_t */
@@ -199,13 +203,17 @@ int EVP_PBE_scrypt(const char *pass, size_t passlen,
199203 * This is combined size V, X and T (section 4)
200204 */
201205 i = UINT64_MAX / (32 * sizeof (uint32_t ));
202- if (N + 2 > i / r )
206+ if (N + 2 > i / r ) {
207+ EVPerr (EVP_F_EVP_PBE_SCRYPT , EVP_R_MEMORY_LIMIT_EXCEEDED );
203208 return 0 ;
209+ }
204210 Vlen = 32 * r * (N + 2 ) * sizeof (uint32_t );
205211
206212 /* check total allocated size fits in uint64_t */
207- if (Blen > UINT64_MAX - Vlen )
213+ if (Blen > UINT64_MAX - Vlen ) {
214+ EVPerr (EVP_F_EVP_PBE_SCRYPT , EVP_R_MEMORY_LIMIT_EXCEEDED );
208215 return 0 ;
216+ }
209217 /* check total allocated size fits in size_t */
210218 if (Blen > SIZE_MAX - Vlen )
211219 return 0 ;
@@ -225,8 +233,10 @@ int EVP_PBE_scrypt(const char *pass, size_t passlen,
225233 return 1 ;
226234
227235 B = OPENSSL_malloc (allocsize );
228- if (B == NULL )
236+ if (B == NULL ) {
237+ EVPerr (EVP_F_EVP_PBE_SCRYPT , ERR_R_MALLOC_FAILURE );
229238 return 0 ;
239+ }
230240 X = (uint32_t * )(B + Blen );
231241 T = X + 32 * r ;
232242 V = T + 32 * r ;
@@ -242,6 +252,9 @@ int EVP_PBE_scrypt(const char *pass, size_t passlen,
242252 goto err ;
243253 rv = 1 ;
244254 err :
255+ if (rv == 0 )
256+ EVPerr (EVP_F_EVP_PBE_SCRYPT , EVP_R_PBKDF2_ERROR );
257+
245258 OPENSSL_clear_free (B , allocsize );
246259 return rv ;
247260}
0 commit comments