| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 1 | /** | 
|  | 2 | * Hash information that's independent from the crypto implementation. | 
|  | 3 | * | 
|  | 4 | *  This can be used by: | 
|  | 5 | *  - code based on PSA | 
|  | 6 | *  - code based on the legacy API | 
|  | 7 | *  - code based on either of them depending on MBEDTLS_USE_PSA_CRYPTO | 
|  | 8 | *  - code based on either of them depending on what's available | 
|  | 9 | * | 
| Manuel Pégourié-Gonnard | de9ffe3 | 2022-07-26 10:20:52 +0200 | [diff] [blame] | 10 | *  Note: this internal module will go away when everything becomes based on | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 11 | *  PSA Crypto; it is a helper for the transition while hash algorithms are | 
| Manuel Pégourié-Gonnard | de9ffe3 | 2022-07-26 10:20:52 +0200 | [diff] [blame] | 12 | *  still represented using mbedtls_md_type_t in most places even when PSA is | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 13 | *  used for the actual crypto computations. | 
|  | 14 | * | 
|  | 15 | *  Copyright The Mbed TLS Contributors | 
|  | 16 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 17 | * | 
|  | 18 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 19 | *  not use this file except in compliance with the License. | 
|  | 20 | *  You may obtain a copy of the License at | 
|  | 21 | * | 
|  | 22 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 23 | * | 
|  | 24 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 25 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 26 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 27 | *  See the License for the specific language governing permissions and | 
|  | 28 | *  limitations under the License. | 
|  | 29 | */ | 
|  | 30 | #ifndef MBEDTLS_HASH_INFO_H | 
|  | 31 | #define MBEDTLS_HASH_INFO_H | 
|  | 32 |  | 
|  | 33 | #include "common.h" | 
|  | 34 |  | 
|  | 35 | #include "mbedtls/md.h" | 
|  | 36 | #include "psa/crypto.h" | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 37 | #include "mbedtls/platform_util.h" | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 38 |  | 
| Przemek Stekiel | c3f2767 | 2022-09-06 13:06:28 +0200 | [diff] [blame] | 39 | /** \def MBEDTLS_HASH_MAX_SIZE | 
|  | 40 | * | 
|  | 41 | * Maximum size of a hash based on configuration. | 
|  | 42 | */ | 
|  | 43 | #if defined(MBEDTLS_MD_C) && ( \ | 
|  | 44 | !defined(MBEDTLS_PSA_CRYPTO_C) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | MBEDTLS_MD_MAX_SIZE >= PSA_HASH_MAX_SIZE) | 
| Przemek Stekiel | c3f2767 | 2022-09-06 13:06:28 +0200 | [diff] [blame] | 46 | #define MBEDTLS_HASH_MAX_SIZE MBEDTLS_MD_MAX_SIZE | 
|  | 47 | #elif defined(MBEDTLS_PSA_CRYPTO_C) && ( \ | 
|  | 48 | !defined(MBEDTLS_MD_C) || \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | PSA_HASH_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE) | 
| Przemek Stekiel | c3f2767 | 2022-09-06 13:06:28 +0200 | [diff] [blame] | 50 | #define MBEDTLS_HASH_MAX_SIZE PSA_HASH_MAX_SIZE | 
|  | 51 | #endif | 
|  | 52 |  | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 53 | /** Get the output length of the given hash type from its MD type. | 
|  | 54 | * | 
|  | 55 | * \note To get the output length from the PSA alg, use \c PSA_HASH_LENGTH(). | 
|  | 56 | * | 
|  | 57 | * \param md_type   The hash MD type. | 
|  | 58 | * | 
|  | 59 | * \return          The output length in bytes, or 0 if not known. | 
|  | 60 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | unsigned char mbedtls_hash_info_get_size(mbedtls_md_type_t md_type); | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 62 |  | 
|  | 63 | /** Get the block size of the given hash type from its MD type. | 
|  | 64 | * | 
|  | 65 | * \note To get the output length from the PSA alg, use | 
|  | 66 | *       \c PSA_HASH_BLOCK_LENGTH(). | 
|  | 67 | * | 
|  | 68 | * \param md_type   The hash MD type. | 
|  | 69 | * | 
|  | 70 | * \return          The block size in bytes, or 0 if not known. | 
|  | 71 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | unsigned char mbedtls_hash_info_get_block_size(mbedtls_md_type_t md_type); | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 73 |  | 
|  | 74 | /** Get the PSA alg from the MD type. | 
|  | 75 | * | 
|  | 76 | * \param md_type   The hash MD type. | 
|  | 77 | * | 
|  | 78 | * \return          The corresponding PSA algorithm identifier, | 
|  | 79 | *                  or PSA_ALG_NONE if not known. | 
|  | 80 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | psa_algorithm_t mbedtls_hash_info_psa_from_md(mbedtls_md_type_t md_type); | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 82 |  | 
|  | 83 | /** Get the MD type alg from the PSA algorithm identifier. | 
|  | 84 | * | 
|  | 85 | * \param psa_alg   The PSA hash algorithm. | 
|  | 86 | * | 
|  | 87 | * \return          The corresponding MD type, | 
|  | 88 | *                  or MBEDTLS_MD_NONE if not known. | 
|  | 89 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | mbedtls_md_type_t mbedtls_hash_info_md_from_psa(psa_algorithm_t psa_alg); | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 91 |  | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 92 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 93 | /** Convert PSA status to MD error code. | 
|  | 94 | * | 
|  | 95 | * \param status    PSA status. | 
|  | 96 | * | 
|  | 97 | * \return          The corresponding MD error code, | 
|  | 98 | */ | 
| Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 99 | int MBEDTLS_DEPRECATED mbedtls_md_error_from_psa(psa_status_t status); | 
|  | 100 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | 
| Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 101 | #endif /* MBEDTLS_HASH_INFO_H */ |