Manuel Pégourié-Gonnard | 36fb12e | 2023-03-28 11:33:23 +0200 | [diff] [blame] | 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 |
| 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é-Gonnard | 44176b0 | 2023-06-07 11:23:26 +0200 | [diff] [blame] | 34 | * \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é-Gonnard | 36fb12e | 2023-03-28 11:33:23 +0200 | [diff] [blame] | 38 | * |
Manuel Pégourié-Gonnard | 1f6d2e3 | 2023-06-06 12:34:45 +0200 | [diff] [blame] | 39 | * \warning This function does not check if the algorithm is |
| 40 | * supported, it always returns the corresponding identifier. |
| 41 | * |
Manuel Pégourié-Gonnard | 70aa2a1 | 2023-05-03 12:26:56 +0200 | [diff] [blame] | 42 | * \return The PSA algorithm identifier associated with \p md_type, |
| 43 | * regardless of whether it is supported or not. |
Manuel Pégourié-Gonnard | 36fb12e | 2023-03-28 11:33:23 +0200 | [diff] [blame] | 44 | */ |
Manuel Pégourié-Gonnard | 001cbc9 | 2023-06-07 12:06:06 +0200 | [diff] [blame] | 45 | static 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é-Gonnard | 36fb12e | 2023-03-28 11:33:23 +0200 | [diff] [blame] | 49 | |
| 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é-Gonnard | 1f6d2e3 | 2023-06-06 12:34:45 +0200 | [diff] [blame] | 56 | * \warning This function does not check if the algorithm is |
| 57 | * supported, it always returns the corresponding identifier. |
| 58 | * |
Manuel Pégourié-Gonnard | 70aa2a1 | 2023-05-03 12:26:56 +0200 | [diff] [blame] | 59 | * \return The MD type associated with \p psa_alg, |
| 60 | * regardless of whether it is supported or not. |
Manuel Pégourié-Gonnard | 36fb12e | 2023-03-28 11:33:23 +0200 | [diff] [blame] | 61 | */ |
Manuel Pégourié-Gonnard | 001cbc9 | 2023-06-07 12:06:06 +0200 | [diff] [blame] | 62 | static 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é-Gonnard | 36fb12e | 2023-03-28 11:33:23 +0200 | [diff] [blame] | 66 | |
| 67 | /** Convert PSA status to MD error code. |
| 68 | * |
| 69 | * \param status PSA status. |
| 70 | * |
| 71 | * \return The corresponding MD error code, |
| 72 | */ |
| 73 | int mbedtls_md_error_from_psa(psa_status_t status); |
| 74 | |
| 75 | #endif /* MBEDTLS_MD_PSA_H */ |