1 /**
2  * Translation between MD and PSA identifiers (algorithms, errors).
3  *
4  *  Note: this internal module will go away when everything becomes based on
5  *  PSA Crypto; it is a helper for the transition period.
6  *
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 #ifndef MBEDTLS_MD_PSA_H
11 #define MBEDTLS_MD_PSA_H
12 
13 #include "common.h"
14 
15 #include "mbedtls/md.h"
16 #include "psa/crypto.h"
17 
18 /**
19  * \brief           This function returns the PSA algorithm identifier
20  *                  associated with the given digest type.
21  *
22  * \param md_type   The type of digest to search for. Must not be NONE.
23  *
24  * \warning         If \p md_type is \c MBEDTLS_MD_NONE, this function will
25  *                  not return \c PSA_ALG_NONE, but an invalid algorithm.
26  *
27  * \warning         This function does not check if the algorithm is
28  *                  supported, it always returns the corresponding identifier.
29  *
30  * \return          The PSA algorithm identifier associated with \p md_type,
31  *                  regardless of whether it is supported or not.
32  */
mbedtls_md_psa_alg_from_type(mbedtls_md_type_t md_type)33 static inline psa_algorithm_t mbedtls_md_psa_alg_from_type(mbedtls_md_type_t md_type)
34 {
35     return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) md_type;
36 }
37 
38 /**
39  * \brief           This function returns the given digest type
40  *                  associated with the PSA algorithm identifier.
41  *
42  * \param psa_alg   The PSA algorithm identifier to search for.
43  *
44  * \warning         This function does not check if the algorithm is
45  *                  supported, it always returns the corresponding identifier.
46  *
47  * \return          The MD type associated with \p psa_alg,
48  *                  regardless of whether it is supported or not.
49  */
mbedtls_md_type_from_psa_alg(psa_algorithm_t psa_alg)50 static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa_alg)
51 {
52     return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK);
53 }
54 
55 /** Convert PSA status to MD error code.
56  *
57  * \param status    PSA status.
58  *
59  * \return          The corresponding MD error code,
60  */
61 int mbedtls_md_error_from_psa(psa_status_t status);
62 
63 #endif /* MBEDTLS_MD_PSA_H */
64