Unify PSA to Mbed TLS error translation
Move all error translation utilities to psa_util.c.
Introduce macros and functions to avoid having
a local copy of the error translating function in
each place.
Identify overlapping errors and introduce a
generic function.
Provide a single macro for all error translations
(unless one file needs a couple of different ones).
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
diff --git a/include/mbedtls/psa_util.h b/include/mbedtls/psa_util.h
index f6070dc..b0ad310 100644
--- a/include/mbedtls/psa_util.h
+++ b/include/mbedtls/psa_util.h
@@ -344,6 +344,41 @@
#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
-#endif /* MBEDTLS_PSA_CRYPTO_C */
+/* PSA errors use int32_t, while Mbed TLS ones use int16_t. psa_status_t
+ * is enough to store either of them. */
+#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_MD5_C)
+extern psa_status_t psa_to_md_errors[8];
+#endif
+#if defined(MBEDTLS_LMS_C)
+extern psa_status_t psa_to_lms_errors[6];
+#endif
+
+#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
+extern psa_status_t psa_to_ssl_errors[14];
+#endif
+
+#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
+ defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
+extern psa_status_t psa_to_pk_rsa_errors[16];
+#endif
+
+#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
+ defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
+extern psa_status_t psa_to_pk_ecdsa_errors[14];
+#endif
+
+int psa_generic_status_to_mbedtls(psa_status_t status);
+
+int psa_status_to_mbedtls(psa_status_t status,
+ psa_status_t *local_translations,
+ size_t local_errors_num,
+ int (*fallback_f)(psa_status_t));
+
+int psa_pk_status_to_mbedtls(psa_status_t status);
+
+#define PSA_TO_MBEDTLS_ERR_LIST(status, error_list, fallback_f) \
+ psa_status_to_mbedtls(status, error_list, sizeof(error_list), fallback_f)
+
+#endif /* MBEDTLS_PSA_CRYPTO_C */
#endif /* MBEDTLS_PSA_UTIL_H */