Skip to content

Commit 7165749

Browse files
committed
Improve speed of PBKDF2 by caching the digest state of the passphrase
1 parent 3f20efc commit 7165749

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

library/pkcs5.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,12 @@ int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
243243
return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
244244
#endif
245245

246+
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
247+
return( ret );
246248
while( key_length )
247249
{
248250
// U1 ends up in work
249251
//
250-
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
251-
return( ret );
252-
253252
if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
254253
return( ret );
255254

@@ -259,21 +258,24 @@ int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
259258
if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
260259
return( ret );
261260

261+
if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
262+
return( ret );
263+
262264
memcpy( md1, work, md_size );
263265

264266
for( i = 1; i < iteration_count; i++ )
265267
{
266268
// U2 ends up in md1
267269
//
268-
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
269-
return( ret );
270-
271270
if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
272271
return( ret );
273272

274273
if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
275274
return( ret );
276275

276+
if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
277+
return( ret );
278+
277279
// U1 xor U2
278280
//
279281
for( j = 0; j < md_size; j++ )

0 commit comments

Comments
 (0)