mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-18 14:42:24 +00:00
Refacor code for HKDF-Extract algorithm
Solution provided by @mpg. Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
This commit is contained in:
parent
2fb0dcd403
commit
03d948c47f
@ -4377,8 +4377,7 @@ psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *op
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
|
||||||
/* Read some bytes from an HKDF-based operation. This performs a chunk
|
/* Read some bytes from an HKDF-based operation. */
|
||||||
* of the expand phase of the HKDF algorithm. */
|
|
||||||
static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
|
static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
|
||||||
psa_algorithm_t kdf_alg,
|
psa_algorithm_t kdf_alg,
|
||||||
uint8_t *output,
|
uint8_t *output,
|
||||||
@ -4388,6 +4387,7 @@ static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkd
|
|||||||
uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
|
uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
|
||||||
size_t hmac_output_length;
|
size_t hmac_output_length;
|
||||||
psa_status_t status;
|
psa_status_t status;
|
||||||
|
const uint8_t last_block = PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ? 0 : 0xff;
|
||||||
|
|
||||||
if( hkdf->state < HKDF_STATE_KEYED ||
|
if( hkdf->state < HKDF_STATE_KEYED ||
|
||||||
( ! hkdf->info_set && ! PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) )
|
( ! hkdf->info_set && ! PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) ) )
|
||||||
@ -4406,57 +4406,49 @@ static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkd
|
|||||||
hkdf->offset_in_block += n;
|
hkdf->offset_in_block += n;
|
||||||
if( output_length == 0 )
|
if( output_length == 0 )
|
||||||
break;
|
break;
|
||||||
/* We can't be wanting more output after block 0xff, otherwise
|
/* We can't be wanting more output after the last block, otherwise
|
||||||
* the capacity check in psa_key_derivation_output_bytes() would have
|
* the capacity check in psa_key_derivation_output_bytes() would have
|
||||||
* prevented this call. It could happen only if the operation
|
* prevented this call. It could happen only if the operation
|
||||||
* object was corrupted or if this function is called directly
|
* object was corrupted or if this function is called directly
|
||||||
* inside the library. */
|
* inside the library. */
|
||||||
if( hkdf->block_number == 0xff )
|
if( hkdf->block_number == last_block )
|
||||||
return( PSA_ERROR_BAD_STATE );
|
return( PSA_ERROR_BAD_STATE );
|
||||||
|
|
||||||
|
|
||||||
if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) && hkdf->block_number == 0 )
|
|
||||||
{
|
|
||||||
memcpy( hkdf->output_block, hkdf->prk, hash_length );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We need a new block */
|
/* We need a new block */
|
||||||
++hkdf->block_number;
|
++hkdf->block_number;
|
||||||
hkdf->offset_in_block = 0;
|
hkdf->offset_in_block = 0;
|
||||||
|
|
||||||
if( ! PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
|
status = psa_key_derivation_start_hmac( &hkdf->hmac,
|
||||||
{
|
hash_alg,
|
||||||
status = psa_key_derivation_start_hmac( &hkdf->hmac,
|
hkdf->prk,
|
||||||
hash_alg,
|
hash_length );
|
||||||
hkdf->prk,
|
if( status != PSA_SUCCESS )
|
||||||
hash_length );
|
return( status );
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
return( status );
|
|
||||||
|
|
||||||
if( hkdf->block_number != 1 )
|
if( hkdf->block_number != 1 )
|
||||||
{
|
{
|
||||||
status = psa_mac_update( &hkdf->hmac,
|
|
||||||
hkdf->output_block,
|
|
||||||
hash_length );
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
return( status );
|
|
||||||
}
|
|
||||||
status = psa_mac_update( &hkdf->hmac,
|
status = psa_mac_update( &hkdf->hmac,
|
||||||
hkdf->info,
|
hkdf->output_block,
|
||||||
hkdf->info_length );
|
hash_length );
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
return( status );
|
|
||||||
status = psa_mac_update( &hkdf->hmac,
|
|
||||||
&hkdf->block_number, 1 );
|
|
||||||
if( status != PSA_SUCCESS )
|
|
||||||
return( status );
|
|
||||||
status = psa_mac_sign_finish( &hkdf->hmac,
|
|
||||||
hkdf->output_block,
|
|
||||||
sizeof( hkdf->output_block ),
|
|
||||||
&hmac_output_length );
|
|
||||||
if( status != PSA_SUCCESS )
|
if( status != PSA_SUCCESS )
|
||||||
return( status );
|
return( status );
|
||||||
}
|
}
|
||||||
|
status = psa_mac_update( &hkdf->hmac,
|
||||||
|
hkdf->info,
|
||||||
|
hkdf->info_length );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
return( status );
|
||||||
|
status = psa_mac_update( &hkdf->hmac,
|
||||||
|
&hkdf->block_number, 1 );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
return( status );
|
||||||
|
status = psa_mac_sign_finish( &hkdf->hmac,
|
||||||
|
hkdf->output_block,
|
||||||
|
sizeof( hkdf->output_block ),
|
||||||
|
&hmac_output_length );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
return( status );
|
||||||
}
|
}
|
||||||
|
|
||||||
return( PSA_SUCCESS );
|
return( PSA_SUCCESS );
|
||||||
@ -5235,9 +5227,21 @@ static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
|
|||||||
return( status );
|
return( status );
|
||||||
}
|
}
|
||||||
|
|
||||||
hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
|
|
||||||
hkdf->block_number = 0;
|
|
||||||
hkdf->state = HKDF_STATE_KEYED;
|
hkdf->state = HKDF_STATE_KEYED;
|
||||||
|
hkdf->block_number = 0;
|
||||||
|
if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
|
||||||
|
{
|
||||||
|
/* The only block of output is the PRK. */
|
||||||
|
memcpy( hkdf->output_block, hkdf->prk, PSA_HASH_LENGTH( hash_alg ) );
|
||||||
|
hkdf->offset_in_block = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Block 0 is empty, and the next block will be
|
||||||
|
* generated by psa_key_derivation_hkdf_read(). */
|
||||||
|
hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
|
||||||
|
}
|
||||||
|
|
||||||
return( PSA_SUCCESS );
|
return( PSA_SUCCESS );
|
||||||
case PSA_KEY_DERIVATION_INPUT_INFO:
|
case PSA_KEY_DERIVATION_INPUT_INFO:
|
||||||
if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
|
if( PSA_ALG_IS_HKDF_EXTRACT( kdf_alg ) )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user