blob: 366ca3f5a22734b2bba8149743cca9cedd87dfab [file] [log] [blame]
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +02001/*
2 * Hash information that's independent from the crypto implementation.
3 *
4 * (See the corresponding header file for usage notes.)
5 */
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
23#include "hash_info.h"
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020024#include "legacy_or_psa.h"
Przemek Stekiel71bf28b2022-07-29 12:12:00 +020025#include "mbedtls/error.h"
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020026
27typedef struct
28{
29 psa_algorithm_t psa_alg;
30 mbedtls_md_type_t md_type;
31 unsigned char size;
32 unsigned char block_size;
33} hash_entry;
34
35static const hash_entry hash_table[] = {
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020036#if defined(MBEDTLS_HAS_ALG_MD5_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020037 { PSA_ALG_MD5, MBEDTLS_MD_MD5, 16, 64 },
38#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020039#if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020040 { PSA_ALG_RIPEMD160, MBEDTLS_MD_RIPEMD160, 20, 64 },
41#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020042#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020043 { PSA_ALG_SHA_1, MBEDTLS_MD_SHA1, 20, 64 },
44#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020045#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020046 { PSA_ALG_SHA_224, MBEDTLS_MD_SHA224, 28, 64 },
47#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020048#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020049 { PSA_ALG_SHA_256, MBEDTLS_MD_SHA256, 32, 64 },
50#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020051#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020052 { PSA_ALG_SHA_384, MBEDTLS_MD_SHA384, 48, 128 },
53#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020054#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020055 { PSA_ALG_SHA_512, MBEDTLS_MD_SHA512, 64, 128 },
56#endif
57 { PSA_ALG_NONE, MBEDTLS_MD_NONE, 0, 0 },
58};
59
60/* Get size from MD type */
61unsigned char mbedtls_hash_info_get_size( mbedtls_md_type_t md_type )
62{
63 const hash_entry *entry = hash_table;
64 while( entry->md_type != MBEDTLS_MD_NONE &&
65 entry->md_type != md_type )
66 {
67 entry++;
68 }
69
70 return entry->size;
71}
72
73/* Get block size from MD type */
74unsigned char mbedtls_hash_info_get_block_size( mbedtls_md_type_t md_type )
75{
76 const hash_entry *entry = hash_table;
77 while( entry->md_type != MBEDTLS_MD_NONE &&
78 entry->md_type != md_type )
79 {
80 entry++;
81 }
82
83 return entry->block_size;
84}
85
86/* Get PSA from MD */
87psa_algorithm_t mbedtls_hash_info_psa_from_md( mbedtls_md_type_t md_type )
88{
89 const hash_entry *entry = hash_table;
90 while( entry->md_type != MBEDTLS_MD_NONE &&
91 entry->md_type != md_type )
92 {
93 entry++;
94 }
95
96 return entry->psa_alg;
97}
98
Manuel Pégourié-Gonnardde9ffe32022-07-26 10:20:52 +020099/* Get MD from PSA */
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +0200100mbedtls_md_type_t mbedtls_hash_info_md_from_psa( psa_algorithm_t psa_alg )
101{
102 const hash_entry *entry = hash_table;
103 while( entry->md_type != MBEDTLS_MD_NONE &&
104 entry->psa_alg != psa_alg )
105 {
106 entry++;
107 }
108
109 return entry->md_type;
110}
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200111
112int mbedtls_md_error_from_psa( psa_status_t status )
113{
114 switch( status )
115 {
116 case PSA_SUCCESS:
117 return( 0 );
118 case PSA_ERROR_NOT_SUPPORTED:
119 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
120 case PSA_ERROR_INVALID_ARGUMENT:
121 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
122 case PSA_ERROR_INSUFFICIENT_MEMORY:
123 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
124 default:
125 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
126 }
127}