blob: b201263b1aee09fcba6d7289ebdc4bff909cfff2 [file] [log] [blame]
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +02001/**
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
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +02009 */
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 *
Manuel Pégourié-Gonnard44176b02023-06-07 11:23:26 +020022 * \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.
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020026 *
Manuel Pégourié-Gonnard1f6d2e32023-06-06 12:34:45 +020027 * \warning This function does not check if the algorithm is
28 * supported, it always returns the corresponding identifier.
29 *
Manuel Pégourié-Gonnard70aa2a12023-05-03 12:26:56 +020030 * \return The PSA algorithm identifier associated with \p md_type,
31 * regardless of whether it is supported or not.
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020032 */
Manuel Pégourié-Gonnard001cbc92023-06-07 12:06:06 +020033static 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}
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020037
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 *
Manuel Pégourié-Gonnard1f6d2e32023-06-06 12:34:45 +020044 * \warning This function does not check if the algorithm is
45 * supported, it always returns the corresponding identifier.
46 *
Manuel Pégourié-Gonnard70aa2a12023-05-03 12:26:56 +020047 * \return The MD type associated with \p psa_alg,
48 * regardless of whether it is supported or not.
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020049 */
Manuel Pégourié-Gonnard001cbc92023-06-07 12:06:06 +020050static 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}
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020054
55/** Convert PSA status to MD error code.
56 *
57 * \param status PSA status.
58 *
59 * \return The corresponding MD error code,
60 */
61int mbedtls_md_error_from_psa(psa_status_t status);
62
63#endif /* MBEDTLS_MD_PSA_H */