Simplify implementation of MD<->PSA translation
Also, add tests and comments due from previous commits.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
diff --git a/library/md.c b/library/md.c
index 932c6d0..d0ea66b 100644
--- a/library/md.c
+++ b/library/md.c
@@ -773,46 +773,15 @@
#if defined(MBEDTLS_PSA_CRYPTO_C)
psa_algorithm_t mbedtls_md_psa_alg_from_type(mbedtls_md_type_t md_type)
{
- switch (md_type) {
- case MBEDTLS_MD_MD5:
- return PSA_ALG_MD5;
- case MBEDTLS_MD_RIPEMD160:
- return PSA_ALG_RIPEMD160;
- case MBEDTLS_MD_SHA1:
- return PSA_ALG_SHA_1;
- case MBEDTLS_MD_SHA224:
- return PSA_ALG_SHA_224;
- case MBEDTLS_MD_SHA256:
- return PSA_ALG_SHA_256;
- case MBEDTLS_MD_SHA384:
- return PSA_ALG_SHA_384;
- case MBEDTLS_MD_SHA512:
- return PSA_ALG_SHA_512;
- default:
- return PSA_ALG_NONE;
+ if (md_type == MBEDTLS_MD_NONE) {
+ return PSA_ALG_NONE;
}
+ return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) md_type;
}
mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa_alg)
{
- switch (psa_alg) {
- case PSA_ALG_MD5:
- return MBEDTLS_MD_MD5;
- case PSA_ALG_RIPEMD160:
- return MBEDTLS_MD_RIPEMD160;
- case PSA_ALG_SHA_1:
- return MBEDTLS_MD_SHA1;
- case PSA_ALG_SHA_224:
- return MBEDTLS_MD_SHA224;
- case PSA_ALG_SHA_256:
- return MBEDTLS_MD_SHA256;
- case PSA_ALG_SHA_384:
- return MBEDTLS_MD_SHA384;
- case PSA_ALG_SHA_512:
- return MBEDTLS_MD_SHA512;
- default:
- return MBEDTLS_MD_NONE;
- }
+ return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK);
}
int mbedtls_md_error_from_psa(psa_status_t status)
diff --git a/library/md_psa.h b/library/md_psa.h
index 3231a60..2f6c701 100644
--- a/library/md_psa.h
+++ b/library/md_psa.h
@@ -33,6 +33,9 @@
*
* \param md_type The type of digest to search for.
*
+ * \warning This function does not check if the algorithm is
+ * supported, it always returns the corresponding identifier.
+ *
* \return The PSA algorithm identifier associated with \p md_type,
* regardless of whether it is supported or not.
*/
@@ -44,6 +47,9 @@
*
* \param psa_alg The PSA algorithm identifier to search for.
*
+ * \warning This function does not check if the algorithm is
+ * supported, it always returns the corresponding identifier.
+ *
* \return The MD type associated with \p psa_alg,
* regardless of whether it is supported or not.
*/