Guard hash test functions as a block

Replicate the way the internal hash functions are guarded

Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
This commit is contained in:
Steven Cooreman 2021-03-15 11:00:12 +01:00
parent 830aff2a98
commit a85e2f835e
3 changed files with 21 additions and 61 deletions

View File

@ -43,6 +43,18 @@
#define MBEDTLS_PSA_BUILTIN_HASH #define MBEDTLS_PSA_BUILTIN_HASH
#endif #endif
#if defined(MBEDTLS_PSA_ACCEL_ALG_MD2) || \
defined(MBEDTLS_PSA_ACCEL_ALG_MD4) || \
defined(MBEDTLS_PSA_ACCEL_ALG_MD5) || \
defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512)
#define MBEDTLS_PSA_ACCEL_HASH
#endif
typedef struct typedef struct
{ {
psa_algorithm_t alg; psa_algorithm_t alg;

View File

@ -1085,7 +1085,7 @@ psa_status_t psa_driver_wrapper_hash_compute(
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
/* Try accelerators first */ /* Try accelerators first */
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
status = mbedtls_transparent_test_driver_hash_compute( alg, input, input_length, status = mbedtls_transparent_test_driver_hash_compute( alg, input, input_length,
hash, hash_size, hash_length ); hash, hash_size, hash_length );
if( status != PSA_ERROR_NOT_SUPPORTED ) if( status != PSA_ERROR_NOT_SUPPORTED )
@ -1117,7 +1117,7 @@ psa_status_t psa_driver_wrapper_hash_setup(
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
/* Try setup on accelerators first */ /* Try setup on accelerators first */
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
status = mbedtls_transparent_test_driver_hash_setup( &operation->ctx.test_ctx, alg ); status = mbedtls_transparent_test_driver_hash_setup( &operation->ctx.test_ctx, alg );
if( status == PSA_SUCCESS ) if( status == PSA_SUCCESS )
operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
@ -1148,7 +1148,7 @@ psa_status_t psa_driver_wrapper_hash_clone(
{ {
switch( source_operation->id ) switch( source_operation->id )
{ {
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
return( mbedtls_transparent_test_driver_hash_clone( &source_operation->ctx.test_ctx, return( mbedtls_transparent_test_driver_hash_clone( &source_operation->ctx.test_ctx,
@ -1174,7 +1174,7 @@ psa_status_t psa_driver_wrapper_hash_update(
{ {
switch( operation->id ) switch( operation->id )
{ {
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
return( mbedtls_transparent_test_driver_hash_update( &operation->ctx.test_ctx, return( mbedtls_transparent_test_driver_hash_update( &operation->ctx.test_ctx,
input, input_length ) ); input, input_length ) );
@ -1200,7 +1200,7 @@ psa_status_t psa_driver_wrapper_hash_finish(
{ {
switch( operation->id ) switch( operation->id )
{ {
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
return( mbedtls_transparent_test_driver_hash_finish( &operation->ctx.test_ctx, return( mbedtls_transparent_test_driver_hash_finish( &operation->ctx.test_ctx,
hash, hash_size, hash_length ) ); hash, hash_size, hash_length ) );
@ -1225,7 +1225,7 @@ psa_status_t psa_driver_wrapper_hash_abort(
{ {
switch( operation->id ) switch( operation->id )
{ {
#if defined(PSA_CRYPTO_DRIVER_TEST) #if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
return( mbedtls_transparent_test_driver_hash_abort( &operation->ctx.test_ctx ) ); return( mbedtls_transparent_test_driver_hash_abort( &operation->ctx.test_ctx ) );
#endif #endif

View File

@ -72,15 +72,7 @@
* transparent test driver, then the mbedtls_transparent_test_driver_hash_* * transparent test driver, then the mbedtls_transparent_test_driver_hash_*
* entry points need to be implemented. */ * entry points need to be implemented. */
#if defined(PSA_CRYPTO_DRIVER_TEST) && \ #if defined(PSA_CRYPTO_DRIVER_TEST) && \
( defined(MBEDTLS_PSA_ACCEL_ALG_MD2) || \ defined(MBEDTLS_PSA_ACCEL_HASH)
defined(MBEDTLS_PSA_ACCEL_ALG_MD4) || \
defined(MBEDTLS_PSA_ACCEL_ALG_MD5) || \
defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384) || \
defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512) )
#define INCLUDE_HASH_TEST_DRIVER #define INCLUDE_HASH_TEST_DRIVER
#endif #endif
@ -558,9 +550,8 @@ psa_status_t mbedtls_psa_hash_abort(
/* /*
* BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
*/ */
#if defined(PSA_CRYPTO_DRIVER_TEST)
#if defined(INCLUDE_HASH_TEST_DRIVER) #if defined(INCLUDE_HASH_TEST_DRIVER)
psa_status_t is_hash_accelerated( psa_algorithm_t alg ) psa_status_t is_hash_accelerated( psa_algorithm_t alg )
{ {
switch( alg ) switch( alg )
@ -605,7 +596,6 @@ psa_status_t is_hash_accelerated( psa_algorithm_t alg )
return( PSA_ERROR_NOT_SUPPORTED ); return( PSA_ERROR_NOT_SUPPORTED );
} }
} }
#endif /* INCLUDE_HASH_TEST_DRIVER */
psa_status_t mbedtls_transparent_test_driver_hash_compute( psa_status_t mbedtls_transparent_test_driver_hash_compute(
psa_algorithm_t alg, psa_algorithm_t alg,
@ -615,54 +605,32 @@ psa_status_t mbedtls_transparent_test_driver_hash_compute(
size_t hash_size, size_t hash_size,
size_t *hash_length) size_t *hash_length)
{ {
#if defined(INCLUDE_HASH_TEST_DRIVER)
if( is_hash_accelerated( alg ) == PSA_SUCCESS ) if( is_hash_accelerated( alg ) == PSA_SUCCESS )
return( hash_compute( alg, input, input_length, return( hash_compute( alg, input, input_length,
hash, hash_size, hash_length ) ); hash, hash_size, hash_length ) );
else else
return( PSA_ERROR_NOT_SUPPORTED ); return( PSA_ERROR_NOT_SUPPORTED );
#else
(void) alg;
(void) input;
(void) input_length;
(void) hash;
(void) hash_size;
(void) hash_length;
return( PSA_ERROR_NOT_SUPPORTED );
#endif
} }
psa_status_t mbedtls_transparent_test_driver_hash_setup( psa_status_t mbedtls_transparent_test_driver_hash_setup(
mbedtls_transparent_test_driver_hash_operation_t *operation, mbedtls_transparent_test_driver_hash_operation_t *operation,
psa_algorithm_t alg ) psa_algorithm_t alg )
{ {
#if defined(INCLUDE_HASH_TEST_DRIVER)
if( is_hash_accelerated( alg ) == PSA_SUCCESS ) if( is_hash_accelerated( alg ) == PSA_SUCCESS )
return( hash_setup( &operation->operation, alg ) ); return( hash_setup( &operation->operation, alg ) );
else else
return( PSA_ERROR_NOT_SUPPORTED ); return( PSA_ERROR_NOT_SUPPORTED );
#else
(void) alg;
(void) operation;
return( PSA_ERROR_NOT_SUPPORTED );
#endif
} }
psa_status_t mbedtls_transparent_test_driver_hash_clone( psa_status_t mbedtls_transparent_test_driver_hash_clone(
const mbedtls_transparent_test_driver_hash_operation_t *source_operation, const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
mbedtls_transparent_test_driver_hash_operation_t *target_operation ) mbedtls_transparent_test_driver_hash_operation_t *target_operation )
{ {
#if defined(INCLUDE_HASH_TEST_DRIVER)
if( is_hash_accelerated( source_operation->operation.alg ) == PSA_SUCCESS ) if( is_hash_accelerated( source_operation->operation.alg ) == PSA_SUCCESS )
return( hash_clone( &source_operation->operation, return( hash_clone( &source_operation->operation,
&target_operation->operation ) ); &target_operation->operation ) );
else else
return( PSA_ERROR_BAD_STATE ); return( PSA_ERROR_BAD_STATE );
#else
(void) source_operation;
(void) target_operation;
return( PSA_ERROR_NOT_SUPPORTED );
#endif
} }
psa_status_t mbedtls_transparent_test_driver_hash_update( psa_status_t mbedtls_transparent_test_driver_hash_update(
@ -670,18 +638,11 @@ psa_status_t mbedtls_transparent_test_driver_hash_update(
const uint8_t *input, const uint8_t *input,
size_t input_length ) size_t input_length )
{ {
#if defined(INCLUDE_HASH_TEST_DRIVER)
if( is_hash_accelerated( operation->operation.alg ) == PSA_SUCCESS ) if( is_hash_accelerated( operation->operation.alg ) == PSA_SUCCESS )
return( hash_update( &operation->operation, return( hash_update( &operation->operation,
input, input_length ) ); input, input_length ) );
else else
return( PSA_ERROR_BAD_STATE ); return( PSA_ERROR_BAD_STATE );
#else
(void) operation;
(void) input;
(void) input_length;
return( PSA_ERROR_NOT_SUPPORTED );
#endif
} }
psa_status_t mbedtls_transparent_test_driver_hash_finish( psa_status_t mbedtls_transparent_test_driver_hash_finish(
@ -690,32 +651,19 @@ psa_status_t mbedtls_transparent_test_driver_hash_finish(
size_t hash_size, size_t hash_size,
size_t *hash_length ) size_t *hash_length )
{ {
#if defined(INCLUDE_HASH_TEST_DRIVER)
if( is_hash_accelerated( operation->operation.alg ) == PSA_SUCCESS ) if( is_hash_accelerated( operation->operation.alg ) == PSA_SUCCESS )
return( hash_finish( &operation->operation, return( hash_finish( &operation->operation,
hash, hash_size, hash_length ) ); hash, hash_size, hash_length ) );
else else
return( PSA_ERROR_BAD_STATE ); return( PSA_ERROR_BAD_STATE );
#else
(void) operation;
(void) hash;
(void) hash_size;
(void) hash_length;
return( PSA_ERROR_NOT_SUPPORTED );
#endif
} }
psa_status_t mbedtls_transparent_test_driver_hash_abort( psa_status_t mbedtls_transparent_test_driver_hash_abort(
mbedtls_transparent_test_driver_hash_operation_t *operation ) mbedtls_transparent_test_driver_hash_operation_t *operation )
{ {
#if defined(INCLUDE_HASH_TEST_DRIVER)
return( hash_abort( &operation->operation ) ); return( hash_abort( &operation->operation ) );
#else
(void) operation;
return( PSA_ERROR_NOT_SUPPORTED );
#endif
} }
#endif /* PSA_CRYPTO_DRIVER_TEST */ #endif /* INCLUDE_HASH_TEST_DRIVER */
#endif /* MBEDTLS_PSA_CRYPTO_C */ #endif /* MBEDTLS_PSA_CRYPTO_C */