Skip to content

Improve speed of PBKDF2 by caching the digest state of the passphrase #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions library/pkcs5.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,12 @@ int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
#endif

if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
return( ret );
while( key_length )
{
// U1 ends up in work
//
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
return( ret );

if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
return( ret );

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

if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
return( ret );

memcpy( md1, work, md_size );

for( i = 1; i < iteration_count; i++ )
{
// U2 ends up in md1
//
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
return( ret );

if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
return( ret );

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

if( ( ret = mbedtls_md_hmac_reset( ctx ) ) != 0 )
return( ret );

// U1 xor U2
//
for( j = 0; j < md_size; j++ )
Expand Down