From cd501f406e09b315e5a6f5b6542fec2e2552546e Mon Sep 17 00:00:00 2001 From: Neil Armstrong Date: Mon, 21 Feb 2022 10:41:39 +0100 Subject: [PATCH] Add specialized PSA to mbedtls PK/ECDSA error mapping function Signed-off-by: Neil Armstrong --- include/mbedtls/pk.h | 4 ++++ library/ecdsa.c | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/mbedtls/pk.h b/include/mbedtls/pk.h index b496cb79b..ad7af986c 100644 --- a/include/mbedtls/pk.h +++ b/include/mbedtls/pk.h @@ -86,6 +86,10 @@ extern "C" { #if defined(MBEDTLS_USE_PSA_CRYPTO) int mbedtls_pk_psa_err_translate( psa_status_t status ); + +#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) +int mbedtls_pk_ecp_psa_err_translate( psa_status_t status ); +#endif #endif /** diff --git a/library/ecdsa.c b/library/ecdsa.c index 0b612ce8a..a112ada5c 100644 --- a/library/ecdsa.c +++ b/library/ecdsa.c @@ -47,12 +47,38 @@ #include "mbedtls/platform_util.h" #include "mbedtls/error.h" +#if defined(MBEDTLS_USE_PSA_CRYPTO) +#include "mbedtls/pk.h" +#endif + /* Parameter validation macros based on platform_util.h */ #define ECDSA_VALIDATE_RET( cond ) \ MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) #define ECDSA_VALIDATE( cond ) \ MBEDTLS_INTERNAL_VALIDATE( cond ) +#if defined(MBEDTLS_USE_PSA_CRYPTO) +int mbedtls_pk_ecp_psa_err_translate( psa_status_t status ) +{ + switch( status ) + { + case PSA_ERROR_NOT_PERMITTED: + case PSA_ERROR_INVALID_ARGUMENT: + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + case PSA_ERROR_INVALID_HANDLE: + return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); + case PSA_ERROR_BUFFER_TOO_SMALL: + return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); + case PSA_ERROR_INSUFFICIENT_ENTROPY: + return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); + case PSA_ERROR_INVALID_SIGNATURE: + return( MBEDTLS_ERR_ECP_VERIFY_FAILED ); + default: + return( mbedtls_pk_psa_err_translate( status ) ); + } +} +#endif + #if defined(MBEDTLS_ECP_RESTARTABLE) /*