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 | * (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" |
Przemek Stekiel | 71bf28b | 2022-07-29 12:12:00 +0200 | [diff] [blame] | 24 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 25 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 26 | typedef struct { |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 27 | psa_algorithm_t psa_alg; |
| 28 | mbedtls_md_type_t md_type; |
| 29 | unsigned char size; |
| 30 | unsigned char block_size; |
| 31 | } hash_entry; |
| 32 | |
| 33 | static const hash_entry hash_table[] = { |
Manuel Pégourié-Gonnard | ebef58d | 2023-03-16 12:48:24 +0100 | [diff] [blame] | 34 | #if defined(MBEDTLS_MD_CAN_MD5) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 35 | { PSA_ALG_MD5, MBEDTLS_MD_MD5, 16, 64 }, |
| 36 | #endif |
Manuel Pégourié-Gonnard | ebef58d | 2023-03-16 12:48:24 +0100 | [diff] [blame] | 37 | #if defined(MBEDTLS_MD_CAN_RIPEMD160) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 38 | { PSA_ALG_RIPEMD160, MBEDTLS_MD_RIPEMD160, 20, 64 }, |
| 39 | #endif |
Manuel Pégourié-Gonnard | ebef58d | 2023-03-16 12:48:24 +0100 | [diff] [blame] | 40 | #if defined(MBEDTLS_MD_CAN_SHA1) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 41 | { PSA_ALG_SHA_1, MBEDTLS_MD_SHA1, 20, 64 }, |
| 42 | #endif |
Manuel Pégourié-Gonnard | ebef58d | 2023-03-16 12:48:24 +0100 | [diff] [blame] | 43 | #if defined(MBEDTLS_MD_CAN_SHA224) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 44 | { PSA_ALG_SHA_224, MBEDTLS_MD_SHA224, 28, 64 }, |
| 45 | #endif |
Manuel Pégourié-Gonnard | ebef58d | 2023-03-16 12:48:24 +0100 | [diff] [blame] | 46 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 47 | { PSA_ALG_SHA_256, MBEDTLS_MD_SHA256, 32, 64 }, |
| 48 | #endif |
Manuel Pégourié-Gonnard | ebef58d | 2023-03-16 12:48:24 +0100 | [diff] [blame] | 49 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 50 | { PSA_ALG_SHA_384, MBEDTLS_MD_SHA384, 48, 128 }, |
| 51 | #endif |
Manuel Pégourié-Gonnard | ebef58d | 2023-03-16 12:48:24 +0100 | [diff] [blame] | 52 | #if defined(MBEDTLS_MD_CAN_SHA512) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 53 | { PSA_ALG_SHA_512, MBEDTLS_MD_SHA512, 64, 128 }, |
| 54 | #endif |
| 55 | { PSA_ALG_NONE, MBEDTLS_MD_NONE, 0, 0 }, |
| 56 | }; |
| 57 | |
| 58 | /* Get size from MD type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | 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] | 60 | { |
| 61 | const hash_entry *entry = hash_table; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | while (entry->md_type != MBEDTLS_MD_NONE && |
| 63 | entry->md_type != md_type) { |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 64 | entry++; |
| 65 | } |
| 66 | |
| 67 | return entry->size; |
| 68 | } |
| 69 | |
| 70 | /* Get block size from MD type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | 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] | 72 | { |
| 73 | const hash_entry *entry = hash_table; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | while (entry->md_type != MBEDTLS_MD_NONE && |
| 75 | entry->md_type != md_type) { |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 76 | entry++; |
| 77 | } |
| 78 | |
| 79 | return entry->block_size; |
| 80 | } |
| 81 | |
| 82 | /* Get PSA from MD */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | 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] | 84 | { |
| 85 | const hash_entry *entry = hash_table; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | while (entry->md_type != MBEDTLS_MD_NONE && |
| 87 | entry->md_type != md_type) { |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 88 | entry++; |
| 89 | } |
| 90 | |
| 91 | return entry->psa_alg; |
| 92 | } |
| 93 | |
Manuel Pégourié-Gonnard | de9ffe3 | 2022-07-26 10:20:52 +0200 | [diff] [blame] | 94 | /* Get MD from PSA */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | 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] | 96 | { |
| 97 | const hash_entry *entry = hash_table; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | while (entry->md_type != MBEDTLS_MD_NONE && |
| 99 | entry->psa_alg != psa_alg) { |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 100 | entry++; |
| 101 | } |
| 102 | |
| 103 | return entry->md_type; |
| 104 | } |
Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 105 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 106 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | int mbedtls_md_error_from_psa(psa_status_t status) |
Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 108 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | switch (status) { |
Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 110 | case PSA_SUCCESS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | return 0; |
Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 112 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE; |
Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 114 | case PSA_ERROR_INVALID_ARGUMENT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | return MBEDTLS_ERR_MD_BAD_INPUT_DATA; |
Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 116 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | return MBEDTLS_ERR_MD_ALLOC_FAILED; |
Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 118 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Przemek Stekiel | 2aae040 | 2022-07-29 11:20:07 +0200 | [diff] [blame] | 120 | } |
| 121 | } |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 122 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |