diff --git a/library/psa_crypto_driver_wrappers.c b/library/psa_crypto_driver_wrappers.c index 43aa180cb..81a7d4dc2 100644 --- a/library/psa_crypto_driver_wrappers.c +++ b/library/psa_crypto_driver_wrappers.c @@ -1083,6 +1083,12 @@ psa_status_t psa_driver_wrapper_hash_compute( psa_status_t status = PSA_ERROR_NOT_SUPPORTED; /* Try accelerators first */ +#if defined(PSA_CRYPTO_DRIVER_TEST) + status = test_transparent_hash_compute( alg, input, input_length, + hash, hash_size, hash_length ); + if( status != PSA_ERROR_NOT_SUPPORTED ) + return( status ); +#endif /* If software fallback is compiled in, try fallback */ #if defined(MBEDTLS_PSA_BUILTIN_HASH) @@ -1112,6 +1118,14 @@ psa_status_t psa_driver_wrapper_hash_setup( psa_status_t status = PSA_ERROR_NOT_SUPPORTED; /* Try setup on accelerators first */ +#if defined(PSA_CRYPTO_DRIVER_TEST) + status = test_transparent_hash_setup( &operation->ctx.test_ctx, alg ); + if( status == PSA_SUCCESS ) + operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + + if( status != PSA_ERROR_NOT_SUPPORTED ) + return( status ); +#endif /* If software fallback is compiled in, try fallback */ #if defined(MBEDTLS_PSA_BUILTIN_HASH) @@ -1135,6 +1149,12 @@ psa_status_t psa_driver_wrapper_hash_clone( { switch( source_operation->id ) { +#if defined(PSA_CRYPTO_DRIVER_TEST) + case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; + return( test_transparent_hash_clone( &source_operation->ctx.test_ctx, + &target_operation->ctx.test_ctx ) ); +#endif #if defined(MBEDTLS_PSA_BUILTIN_HASH) case PSA_CRYPTO_MBED_TLS_DRIVER_ID: target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; @@ -1155,6 +1175,11 @@ psa_status_t psa_driver_wrapper_hash_update( { switch( operation->id ) { +#if defined(PSA_CRYPTO_DRIVER_TEST) + case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + return( test_transparent_hash_update( &operation->ctx.test_ctx, + input, input_length ) ); +#endif #if defined(MBEDTLS_PSA_BUILTIN_HASH) case PSA_CRYPTO_MBED_TLS_DRIVER_ID: return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, @@ -1176,6 +1201,11 @@ psa_status_t psa_driver_wrapper_hash_finish( { switch( operation->id ) { +#if defined(PSA_CRYPTO_DRIVER_TEST) + case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + return( test_transparent_hash_finish( &operation->ctx.test_ctx, + hash, hash_size, hash_length ) ); +#endif #if defined(MBEDTLS_PSA_BUILTIN_HASH) case PSA_CRYPTO_MBED_TLS_DRIVER_ID: return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, @@ -1196,6 +1226,10 @@ psa_status_t psa_driver_wrapper_hash_abort( { switch( operation->id ) { +#if defined(PSA_CRYPTO_DRIVER_TEST) + case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: + return( test_transparent_hash_abort( &operation->ctx.test_ctx ) ); +#endif #if defined(MBEDTLS_PSA_BUILTIN_HASH) case PSA_CRYPTO_MBED_TLS_DRIVER_ID: return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); diff --git a/library/psa_crypto_driver_wrappers_contexts.h b/library/psa_crypto_driver_wrappers_contexts.h index 9bb79664a..db4153c4d 100644 --- a/library/psa_crypto_driver_wrappers_contexts.h +++ b/library/psa_crypto_driver_wrappers_contexts.h @@ -28,6 +28,9 @@ /* Include all structure definitions for the drivers that have been included * during the auto-generation of this file (autogeneration not yet in place) */ +#if defined(PSA_CRYPTO_DRIVER_TEST) +#include "test/drivers/test_driver.h" +#endif /* Include the structure definitions for the mbed TLS software drivers */ #include "psa_crypto_hash.h" @@ -42,6 +45,9 @@ union psa_driver_hash_context_u { unsigned dummy; /* Make sure this structure is always non-empty */ mbedtls_psa_hash_operation_t mbedtls_ctx; +#if defined(PSA_CRYPTO_DRIVER_TEST) + test_transparent_hash_operation_t test_ctx; +#endif }; #endif /* PSA_CRYPTO_DRIVER_WRAPPERS_CONTEXTS_H */ diff --git a/library/psa_crypto_hash.c b/library/psa_crypto_hash.c index 8ac21d0cb..bd3b57e6e 100644 --- a/library/psa_crypto_hash.c +++ b/library/psa_crypto_hash.c @@ -434,4 +434,179 @@ psa_status_t mbedtls_psa_hash_abort( return( PSA_SUCCESS ); } + /* + * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. + */ +#if defined(PSA_CRYPTO_DRIVER_TEST) + +#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 INCLUDE_HASH_TEST_DRIVER +#endif + +#if defined(INCLUDE_HASH_TEST_DRIVER) +psa_status_t is_hash_accelerated( psa_algorithm_t alg ) +{ + switch( alg ) + { +#if defined(MBEDTLS_PSA_ACCEL_ALG_MD2) + case PSA_ALG_MD2: + return( PSA_SUCCESS ); +#endif +#if defined(MBEDTLS_PSA_ACCEL_ALG_MD4) + case PSA_ALG_MD4: + return( PSA_SUCCESS ); +#endif +#if defined(MBEDTLS_PSA_ACCEL_ALG_MD5) + case PSA_ALG_MD5: + return( PSA_SUCCESS ); +#endif +#if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160) + case PSA_ALG_RIPEMD160: + return( PSA_SUCCESS ); +#endif +#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1) + case PSA_ALG_SHA_1: + return( PSA_SUCCESS ); +#endif +#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224) + case PSA_ALG_SHA_224: + return( PSA_SUCCESS ); +#endif +#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256) + case PSA_ALG_SHA_256: + return( PSA_SUCCESS ); +#endif +#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384) + case PSA_ALG_SHA_384: + return( PSA_SUCCESS ); +#endif +#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512) + case PSA_ALG_SHA_512: + return( PSA_SUCCESS ); +#endif + default: + return( PSA_ERROR_NOT_SUPPORTED ); + } +} +#endif /* INCLUDE_HASH_TEST_DRIVER */ + +psa_status_t test_transparent_hash_compute( + psa_algorithm_t alg, + const uint8_t *input, + size_t input_length, + uint8_t *hash, + size_t hash_size, + size_t *hash_length) +{ +#if defined(INCLUDE_HASH_TEST_DRIVER) + if( is_hash_accelerated( alg ) == PSA_SUCCESS ) + return( mbedtls_psa_hash_compute( alg, input, input_length, + hash, hash_size, hash_length ) ); + else + 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 test_transparent_hash_setup( + test_transparent_hash_operation_t *operation, + psa_algorithm_t alg ) +{ +#if defined(INCLUDE_HASH_TEST_DRIVER) + if( is_hash_accelerated( alg ) == PSA_SUCCESS ) + return( mbedtls_psa_hash_setup( &operation->operation, alg ) ); + else + return( PSA_ERROR_NOT_SUPPORTED ); +#else + (void) alg; + (void) operation; + return( PSA_ERROR_NOT_SUPPORTED ); +#endif +} + +psa_status_t test_transparent_hash_clone( + const test_transparent_hash_operation_t *source_operation, + test_transparent_hash_operation_t *target_operation ) +{ +#if defined(INCLUDE_HASH_TEST_DRIVER) + if( is_hash_accelerated( source_operation->operation.alg ) == PSA_SUCCESS ) + return( mbedtls_psa_hash_clone( &source_operation->operation, + &target_operation->operation ) ); + else + return( PSA_ERROR_BAD_STATE ); +#else + (void) source_operation; + (void) target_operation; + return( PSA_ERROR_NOT_SUPPORTED ); +#endif +} + +psa_status_t test_transparent_hash_update( + test_transparent_hash_operation_t *operation, + const uint8_t *input, + size_t input_length ) +{ +#if defined(INCLUDE_HASH_TEST_DRIVER) + if( is_hash_accelerated( operation->operation.alg ) == PSA_SUCCESS ) + return( mbedtls_psa_hash_update( &operation->operation, + input, input_length ) ); + else + return( PSA_ERROR_BAD_STATE ); +#else + (void) operation; + (void) input; + (void) input_length; + return( PSA_ERROR_NOT_SUPPORTED ); +#endif +} + +psa_status_t test_transparent_hash_finish( + test_transparent_hash_operation_t *operation, + uint8_t *hash, + size_t hash_size, + size_t *hash_length ) +{ +#if defined(INCLUDE_HASH_TEST_DRIVER) + if( is_hash_accelerated( operation->operation.alg ) == PSA_SUCCESS ) + return( mbedtls_psa_hash_finish( &operation->operation, + hash, hash_size, hash_length ) ); + else + 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 test_transparent_hash_abort( + test_transparent_hash_operation_t *operation ) +{ +#if defined(INCLUDE_HASH_TEST_DRIVER) + return( mbedtls_psa_hash_abort( &operation->operation ) ); +#else + (void) operation; + return( PSA_ERROR_NOT_SUPPORTED ); +#endif +} + +#endif /* PSA_CRYPTO_DRIVER_TEST */ + #endif /* MBEDTLS_PSA_CRYPTO_C */ diff --git a/tests/include/test/drivers/hash.h b/tests/include/test/drivers/hash.h new file mode 100644 index 000000000..45c770c81 --- /dev/null +++ b/tests/include/test/drivers/hash.h @@ -0,0 +1,69 @@ +/* + * Test driver for hash functions + */ +/* Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PSA_CRYPTO_TEST_DRIVERS_HASH_H +#define PSA_CRYPTO_TEST_DRIVERS_HASH_H + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#if defined(PSA_CRYPTO_DRIVER_TEST) +/* Include path is relative to the tests/include folder, which is the base + * include path for including this (hash.h) test driver header. */ +#include "../../library/psa_crypto_hash.h" + +typedef struct { + mbedtls_psa_hash_operation_t operation; +} test_transparent_hash_operation_t; + +psa_status_t test_transparent_hash_compute( + psa_algorithm_t alg, + const uint8_t *input, + size_t input_length, + uint8_t *hash, + size_t hash_size, + size_t *hash_length); + +psa_status_t test_transparent_hash_setup( + test_transparent_hash_operation_t *operation, + psa_algorithm_t alg ); + +psa_status_t test_transparent_hash_clone( + const test_transparent_hash_operation_t *source_operation, + test_transparent_hash_operation_t *target_operation ); + +psa_status_t test_transparent_hash_update( + test_transparent_hash_operation_t *operation, + const uint8_t *input, + size_t input_length ); + +psa_status_t test_transparent_hash_finish( + test_transparent_hash_operation_t *operation, + uint8_t *hash, + size_t hash_size, + size_t *hash_length ); + +psa_status_t test_transparent_hash_abort( + test_transparent_hash_operation_t *operation ); + +#endif /* PSA_CRYPTO_DRIVER_TEST */ +#endif /* PSA_CRYPTO_TEST_DRIVERS_HASH_H */ diff --git a/tests/include/test/drivers/test_driver.h b/tests/include/test/drivers/test_driver.h index f26b795dd..8783924b8 100644 --- a/tests/include/test/drivers/test_driver.h +++ b/tests/include/test/drivers/test_driver.h @@ -26,5 +26,6 @@ #include "test/drivers/key_management.h" #include "test/drivers/cipher.h" #include "test/drivers/size.h" +#include "test/drivers/hash.h" #endif /* PSA_CRYPTO_TEST_DRIVER_H */ diff --git a/visualc/VS2010/mbedTLS.vcxproj b/visualc/VS2010/mbedTLS.vcxproj index c4ec8b674..7322cc76d 100644 --- a/visualc/VS2010/mbedTLS.vcxproj +++ b/visualc/VS2010/mbedTLS.vcxproj @@ -242,6 +242,7 @@ +