blob: 0e445b6cf8217ecadb2abd950f559b1e3548b03d [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é-Gonnard07018f92022-09-15 11:29:35 +020024#include "mbedtls/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
Gilles Peskine449bd832023-01-11 14:50:10 +010027typedef struct {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020028 psa_algorithm_t psa_alg;
29 mbedtls_md_type_t md_type;
30 unsigned char size;
31 unsigned char block_size;
32} hash_entry;
33
34static const hash_entry hash_table[] = {
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020035#if defined(MBEDTLS_HAS_ALG_MD5_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020036 { PSA_ALG_MD5, MBEDTLS_MD_MD5, 16, 64 },
37#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020038#if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020039 { PSA_ALG_RIPEMD160, MBEDTLS_MD_RIPEMD160, 20, 64 },
40#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020041#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020042 { PSA_ALG_SHA_1, MBEDTLS_MD_SHA1, 20, 64 },
43#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020044#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020045 { PSA_ALG_SHA_224, MBEDTLS_MD_SHA224, 28, 64 },
46#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020047#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020048 { PSA_ALG_SHA_256, MBEDTLS_MD_SHA256, 32, 64 },
49#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020050#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020051 { PSA_ALG_SHA_384, MBEDTLS_MD_SHA384, 48, 128 },
52#endif
Manuel Pégourié-Gonnard73692b72022-07-21 10:40:13 +020053#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020054 { PSA_ALG_SHA_512, MBEDTLS_MD_SHA512, 64, 128 },
55#endif
56 { PSA_ALG_NONE, MBEDTLS_MD_NONE, 0, 0 },
57};
58
59/* Get size from MD type */
Gilles Peskine449bd832023-01-11 14:50:10 +010060unsigned char mbedtls_hash_info_get_size(mbedtls_md_type_t md_type)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020061{
62 const hash_entry *entry = hash_table;
Gilles Peskine449bd832023-01-11 14:50:10 +010063 while (entry->md_type != MBEDTLS_MD_NONE &&
64 entry->md_type != md_type) {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020065 entry++;
66 }
67
68 return entry->size;
69}
70
71/* Get block size from MD type */
Gilles Peskine449bd832023-01-11 14:50:10 +010072unsigned char mbedtls_hash_info_get_block_size(mbedtls_md_type_t md_type)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020073{
74 const hash_entry *entry = hash_table;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 while (entry->md_type != MBEDTLS_MD_NONE &&
76 entry->md_type != md_type) {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020077 entry++;
78 }
79
80 return entry->block_size;
81}
82
83/* Get PSA from MD */
Gilles Peskine449bd832023-01-11 14:50:10 +010084psa_algorithm_t mbedtls_hash_info_psa_from_md(mbedtls_md_type_t md_type)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020085{
86 const hash_entry *entry = hash_table;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 while (entry->md_type != MBEDTLS_MD_NONE &&
88 entry->md_type != md_type) {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020089 entry++;
90 }
91
92 return entry->psa_alg;
93}
94
Manuel Pégourié-Gonnardde9ffe32022-07-26 10:20:52 +020095/* Get MD from PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +010096mbedtls_md_type_t mbedtls_hash_info_md_from_psa(psa_algorithm_t psa_alg)
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020097{
98 const hash_entry *entry = hash_table;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 while (entry->md_type != MBEDTLS_MD_NONE &&
100 entry->psa_alg != psa_alg) {
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +0200101 entry++;
102 }
103
104 return entry->md_type;
105}
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200106
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500107#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100108int mbedtls_md_error_from_psa(psa_status_t status)
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200109{
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 switch (status) {
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200111 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return 0;
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200113 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200115 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200117 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200119 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Przemek Stekiel2aae0402022-07-29 11:20:07 +0200121 }
122}
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500123#endif /* !MBEDTLS_DEPRECATED_REMOVED */