blob: 8e00bb1492c2554f0f6c14bc8a36533adecbef1f [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
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22#ifndef MBEDTLS_MD_PSA_H
23#define MBEDTLS_MD_PSA_H
24
25#include "common.h"
26
27#include "mbedtls/md.h"
28#include "psa/crypto.h"
29
30/**
31 * \brief This function returns the PSA algorithm identifier
32 * associated with the given digest type.
33 *
Manuel Pégourié-Gonnard44176b02023-06-07 11:23:26 +020034 * \param md_type The type of digest to search for. Must not be NONE.
35 *
36 * \warning If \p md_type is \c MBEDTLS_MD_NONE, this function will
37 * not return \c PSA_ALG_NONE, but an invalid algorithm.
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020038 *
Manuel Pégourié-Gonnard1f6d2e32023-06-06 12:34:45 +020039 * \warning This function does not check if the algorithm is
40 * supported, it always returns the corresponding identifier.
41 *
Manuel Pégourié-Gonnard70aa2a12023-05-03 12:26:56 +020042 * \return The PSA algorithm identifier associated with \p md_type,
43 * regardless of whether it is supported or not.
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020044 */
Manuel Pégourié-Gonnard001cbc92023-06-07 12:06:06 +020045static inline psa_algorithm_t mbedtls_md_psa_alg_from_type(mbedtls_md_type_t md_type)
46{
47 return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) md_type;
48}
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020049
50/**
51 * \brief This function returns the given digest type
52 * associated with the PSA algorithm identifier.
53 *
54 * \param psa_alg The PSA algorithm identifier to search for.
55 *
Manuel Pégourié-Gonnard1f6d2e32023-06-06 12:34:45 +020056 * \warning This function does not check if the algorithm is
57 * supported, it always returns the corresponding identifier.
58 *
Manuel Pégourié-Gonnard70aa2a12023-05-03 12:26:56 +020059 * \return The MD type associated with \p psa_alg,
60 * regardless of whether it is supported or not.
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020061 */
Manuel Pégourié-Gonnard001cbc92023-06-07 12:06:06 +020062static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa_alg)
63{
64 return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK);
65}
Manuel Pégourié-Gonnard36fb12e2023-03-28 11:33:23 +020066
67/** Convert PSA status to MD error code.
68 *
69 * \param status PSA status.
70 *
71 * \return The corresponding MD error code,
72 */
73int mbedtls_md_error_from_psa(psa_status_t status);
74
75#endif /* MBEDTLS_MD_PSA_H */