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  *
10  *  Note: this internal module will go away when everything becomes based on
11  *  PSA Crypto; it is a helper for the transition while hash algorithms are
12  *  still represented using mbedtls_md_type_t in most places even when PSA is
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"
37 
38 /** \def MBEDTLS_HASH_MAX_SIZE
39  *
40  * Maximum size of a hash based on configuration.
41  */
42 #if defined(MBEDTLS_MD_C) && ( \
43     !defined(MBEDTLS_PSA_CRYPTO_C) || \
44     MBEDTLS_MD_MAX_SIZE >= PSA_HASH_MAX_SIZE )
45 #define MBEDTLS_HASH_MAX_SIZE MBEDTLS_MD_MAX_SIZE
46 #elif defined(MBEDTLS_PSA_CRYPTO_C) && ( \
47     !defined(MBEDTLS_MD_C) || \
48     PSA_HASH_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE )
49 #define MBEDTLS_HASH_MAX_SIZE PSA_HASH_MAX_SIZE
50 #endif
51 
52 /** Get the output length of the given hash type from its MD type.
53  *
54  * \note To get the output length from the PSA alg, use \c PSA_HASH_LENGTH().
55  *
56  * \param md_type   The hash MD type.
57  *
58  * \return          The output length in bytes, or 0 if not known.
59  */
60 unsigned char mbedtls_hash_info_get_size( mbedtls_md_type_t md_type );
61 
62 /** Get the block size of the given hash type from its MD type.
63  *
64  * \note To get the output length from the PSA alg, use
65  *       \c PSA_HASH_BLOCK_LENGTH().
66  *
67  * \param md_type   The hash MD type.
68  *
69  * \return          The block size in bytes, or 0 if not known.
70  */
71 unsigned char mbedtls_hash_info_get_block_size( mbedtls_md_type_t md_type );
72 
73 /** Get the PSA alg from the MD type.
74  *
75  * \param md_type   The hash MD type.
76  *
77  * \return          The corresponding PSA algorithm identifier,
78  *                  or PSA_ALG_NONE if not known.
79  */
80 psa_algorithm_t mbedtls_hash_info_psa_from_md( mbedtls_md_type_t md_type );
81 
82 /** Get the MD type alg from the PSA algorithm identifier.
83  *
84  * \param psa_alg   The PSA hash algorithm.
85  *
86  * \return          The corresponding MD type,
87  *                  or MBEDTLS_MD_NONE if not known.
88  */
89 mbedtls_md_type_t mbedtls_hash_info_md_from_psa( psa_algorithm_t psa_alg );
90 
91 /** Convert PSA status to MD error code.
92  *
93  * \param status    PSA status.
94  *
95  * \return          The corresponding MD error code,
96  */
97 int mbedtls_md_error_from_psa( psa_status_t status );
98 
99 #endif /* MBEDTLS_HASH_INFO_H */
100