Save memory by not storing the HMAC key

This commit is contained in:
Manuel Pégourié-Gonnard 2014-02-01 11:38:05 +01:00
parent e6cdbbd40b
commit b05db2a6aa
2 changed files with 21 additions and 11 deletions

View file

@ -56,10 +56,10 @@ extern "C" {
*/ */
typedef struct typedef struct
{ {
/* Working state */ /* Working state: the key K is not stored explicitely,
md_context_t md_ctx; /*!< HMAC context */ * but is implied by the HMAC context */
md_context_t md_ctx; /*!< HMAC context (inc. K) */
unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */ unsigned char V[POLARSSL_MD_MAX_SIZE]; /*!< V in the spec */
unsigned char K[POLARSSL_MD_MAX_SIZE]; /*!< Key in the spec */
int reseed_counter; /*!< reseed counter */ int reseed_counter; /*!< reseed counter */
/* Administrative state */ /* Administrative state */

View file

@ -48,19 +48,20 @@ void hmac_drbg_update( hmac_drbg_context *ctx,
size_t md_len = ctx->md_ctx.md_info->size; size_t md_len = ctx->md_ctx.md_info->size;
unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1; unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1;
unsigned char sep[1]; unsigned char sep[1];
unsigned char K[POLARSSL_MD_MAX_SIZE];
for( sep[0] = 0; sep[0] < rounds; sep[0]++ ) for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
{ {
/* Step 1 or 4 */ /* Step 1 or 4 */
md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); md_hmac_reset( &ctx->md_ctx );
md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
md_hmac_update( &ctx->md_ctx, sep, 1 ); md_hmac_update( &ctx->md_ctx, sep, 1 );
if( rounds == 2 ) if( rounds == 2 )
md_hmac_update( &ctx->md_ctx, additional, add_len ); md_hmac_update( &ctx->md_ctx, additional, add_len );
md_hmac_finish( &ctx->md_ctx, ctx->K ); md_hmac_finish( &ctx->md_ctx, K );
/* Step 2 or 5 */ /* Step 2 or 5 */
md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); md_hmac_starts( &ctx->md_ctx, K, md_len );
md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
md_hmac_finish( &ctx->md_ctx, ctx->V ); md_hmac_finish( &ctx->md_ctx, ctx->V );
} }
@ -80,8 +81,13 @@ int hmac_drbg_init_buf( hmac_drbg_context *ctx,
if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 ) if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
return( ret ); return( ret );
/*
* Set initial working state.
* Use the V memory location, which is currently all 0, to initialize the
* MD context with an all-zero key. Then set V to its initial value.
*/
md_hmac_starts( &ctx->md_ctx, ctx->V, md_info->size );
memset( ctx->V, 0x01, md_info->size ); memset( ctx->V, 0x01, md_info->size );
/* ctx->K is already 0 */
hmac_drbg_update( ctx, data, data_len ); hmac_drbg_update( ctx, data, data_len );
@ -147,9 +153,13 @@ int hmac_drbg_init( hmac_drbg_context *ctx,
if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 ) if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
return( ret ); return( ret );
/* Set initial working state */ /*
* Set initial working state.
* Use the V memory location, which is currently all 0, to initialize the
* MD context with an all-zero key. Then set V to its initial value.
*/
md_hmac_starts( &ctx->md_ctx, ctx->V, md_info->size );
memset( ctx->V, 0x01, md_info->size ); memset( ctx->V, 0x01, md_info->size );
/* ctx->K is already 0 */
ctx->f_entropy = f_entropy; ctx->f_entropy = f_entropy;
ctx->p_entropy = p_entropy; ctx->p_entropy = p_entropy;