From ea761963c52f59ea072f79f0fa0ab365e47651a0 Mon Sep 17 00:00:00 2001 From: Neil Armstrong Date: Mon, 21 Feb 2022 10:42:29 +0100 Subject: [PATCH] Add specialized PSA to mbedtls PK/RSA error mapping function Signed-off-by: Neil Armstrong --- include/mbedtls/pk.h | 4 ++++ library/rsa.c | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/mbedtls/pk.h b/include/mbedtls/pk.h index ad7af986c..189df8536 100644 --- a/include/mbedtls/pk.h +++ b/include/mbedtls/pk.h @@ -90,6 +90,10 @@ 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 + +#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) +int mbedtls_pk_rsa_psa_err_translate( psa_status_t status ); +#endif #endif /** diff --git a/library/rsa.c b/library/rsa.c index 36f487f3a..ef262d9dd 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -66,6 +66,33 @@ #define mbedtls_free free #endif +#if defined(MBEDTLS_USE_PSA_CRYPTO) +#include "mbedtls/pk.h" +#endif + +#if defined(MBEDTLS_USE_PSA_CRYPTO) +int mbedtls_pk_rsa_psa_err_translate( psa_status_t status ) +{ + switch( status ) + { + case PSA_ERROR_NOT_PERMITTED: + case PSA_ERROR_INVALID_ARGUMENT: + case PSA_ERROR_INVALID_HANDLE: + return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); + case PSA_ERROR_BUFFER_TOO_SMALL: + return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE ); + case PSA_ERROR_INSUFFICIENT_ENTROPY: + return( MBEDTLS_ERR_RSA_RNG_FAILED ); + case PSA_ERROR_INVALID_SIGNATURE: + return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); + case PSA_ERROR_INVALID_PADDING: + return( MBEDTLS_ERR_RSA_INVALID_PADDING ); + default: + return( mbedtls_pk_psa_err_translate( status ) ); + } +} +#endif + #if !defined(MBEDTLS_RSA_ALT) /* Parameter validation macros */