From 2aae040615c504175f8835a42e5340417c2670ef Mon Sep 17 00:00:00 2001 From: Przemek Stekiel Date: Fri, 29 Jul 2022 11:20:07 +0200 Subject: [PATCH] make ret_from_status() global function and move it to has_info.[ch] Signed-off-by: Przemek Stekiel --- library/hash_info.c | 18 ++++++++++++++++++ library/hash_info.h | 8 ++++++++ library/rsa.c | 25 +++---------------------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/library/hash_info.c b/library/hash_info.c index c3a81cfdb9..19afae8cab 100644 --- a/library/hash_info.c +++ b/library/hash_info.c @@ -22,6 +22,7 @@ #include "hash_info.h" #include "legacy_or_psa.h" +#include "error.h" typedef struct { @@ -107,3 +108,20 @@ mbedtls_md_type_t mbedtls_hash_info_md_from_psa( psa_algorithm_t psa_alg ) return entry->md_type; } + +int mbedtls_md_error_from_psa( psa_status_t status ) +{ + switch( status ) + { + case PSA_SUCCESS: + return( 0 ); + case PSA_ERROR_NOT_SUPPORTED: + return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); + case PSA_ERROR_INVALID_ARGUMENT: + return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); + case PSA_ERROR_INSUFFICIENT_MEMORY: + return( MBEDTLS_ERR_MD_ALLOC_FAILED ); + default: + return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); + } +} diff --git a/library/hash_info.h b/library/hash_info.h index 67983f7ff8..54f5f7c2fd 100644 --- a/library/hash_info.h +++ b/library/hash_info.h @@ -74,4 +74,12 @@ psa_algorithm_t mbedtls_hash_info_psa_from_md( mbedtls_md_type_t md_type ); */ mbedtls_md_type_t mbedtls_hash_info_md_from_psa( psa_algorithm_t psa_alg ); +/** Convert PSA status to MD error code. + * + * \param status PSA status. + * + * \return The corresponding MD error code, + */ +int mbedtls_md_error_from_psa( psa_status_t status ); + #endif /* MBEDTLS_HASH_INFO_H */ diff --git a/library/rsa.c b/library/rsa.c index e461f6ed94..3ff6fdcd80 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -1098,25 +1098,6 @@ cleanup: } #if defined(MBEDTLS_PKCS1_V21) -#if !defined(MBEDTLS_MD_C) -static int ret_from_status( psa_status_t status ) -{ - switch( status ) - { - case PSA_SUCCESS: - return( 0 ); - case PSA_ERROR_NOT_SUPPORTED: - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); - case PSA_ERROR_INVALID_ARGUMENT: - return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); - case PSA_ERROR_INSUFFICIENT_MEMORY: - return( MBEDTLS_ERR_MD_ALLOC_FAILED ); - default: - return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); - } -} -#endif /* !MBEDTLS_MD_C */ - /** * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer. * @@ -1208,7 +1189,7 @@ exit: #else psa_hash_abort( &op ); - return( ret_from_status( status ) ); + return( mbedtls_md_error_from_psa( status ) ); #endif } @@ -1276,7 +1257,7 @@ exit: exit: psa_hash_abort( &op ); - return( ret_from_status( status ) ); + return( mbedtls_md_error_from_psa( status ) ); #endif /* !MBEDTLS_MD_C */ } @@ -1308,7 +1289,7 @@ static int compute_hash( mbedtls_md_type_t md_alg, status = psa_hash_compute( alg, input, ilen, output, out_size, &out_len ); - return( ret_from_status( status ) ); + return( mbedtls_md_error_from_psa( status ) ); #endif /* !MBEDTLS_MD_C */ } #endif /* MBEDTLS_PKCS1_V21 */