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" |
Manuel Pégourié-Gonnard | 73692b7 | 2022-07-21 10:40:13 +0200 | [diff] [blame] | 24 | #include "legacy_or_psa.h" |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 25 | |
| 26 | typedef struct |
| 27 | { |
| 28 | 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 | |
| 34 | static const hash_entry hash_table[] = { |
Manuel Pégourié-Gonnard | 73692b7 | 2022-07-21 10:40:13 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_HAS_ALG_MD5_VIA_LOWLEVEL_OR_PSA) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 36 | { PSA_ALG_MD5, MBEDTLS_MD_MD5, 16, 64 }, |
| 37 | #endif |
Manuel Pégourié-Gonnard | 73692b7 | 2022-07-21 10:40:13 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_LOWLEVEL_OR_PSA) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 39 | { PSA_ALG_RIPEMD160, MBEDTLS_MD_RIPEMD160, 20, 64 }, |
| 40 | #endif |
Manuel Pégourié-Gonnard | 73692b7 | 2022-07-21 10:40:13 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 42 | { PSA_ALG_SHA_1, MBEDTLS_MD_SHA1, 20, 64 }, |
| 43 | #endif |
Manuel Pégourié-Gonnard | 73692b7 | 2022-07-21 10:40:13 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 45 | { PSA_ALG_SHA_224, MBEDTLS_MD_SHA224, 28, 64 }, |
| 46 | #endif |
Manuel Pégourié-Gonnard | 73692b7 | 2022-07-21 10:40:13 +0200 | [diff] [blame] | 47 | #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_LOWLEVEL_OR_PSA) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 48 | { PSA_ALG_SHA_256, MBEDTLS_MD_SHA256, 32, 64 }, |
| 49 | #endif |
Manuel Pégourié-Gonnard | 73692b7 | 2022-07-21 10:40:13 +0200 | [diff] [blame] | 50 | #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 51 | { PSA_ALG_SHA_384, MBEDTLS_MD_SHA384, 48, 128 }, |
| 52 | #endif |
Manuel Pégourié-Gonnard | 73692b7 | 2022-07-21 10:40:13 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_LOWLEVEL_OR_PSA) |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 54 | { 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 */ |
| 60 | unsigned char mbedtls_hash_info_get_size( mbedtls_md_type_t md_type ) |
| 61 | { |
| 62 | const hash_entry *entry = hash_table; |
| 63 | while( entry->md_type != MBEDTLS_MD_NONE && |
| 64 | entry->md_type != md_type ) |
| 65 | { |
| 66 | entry++; |
| 67 | } |
| 68 | |
| 69 | return entry->size; |
| 70 | } |
| 71 | |
| 72 | /* Get block size from MD type */ |
| 73 | unsigned char mbedtls_hash_info_get_block_size( mbedtls_md_type_t md_type ) |
| 74 | { |
| 75 | const hash_entry *entry = hash_table; |
| 76 | while( entry->md_type != MBEDTLS_MD_NONE && |
| 77 | entry->md_type != md_type ) |
| 78 | { |
| 79 | entry++; |
| 80 | } |
| 81 | |
| 82 | return entry->block_size; |
| 83 | } |
| 84 | |
| 85 | /* Get PSA from MD */ |
| 86 | psa_algorithm_t mbedtls_hash_info_psa_from_md( mbedtls_md_type_t md_type ) |
| 87 | { |
| 88 | const hash_entry *entry = hash_table; |
| 89 | while( entry->md_type != MBEDTLS_MD_NONE && |
| 90 | entry->md_type != md_type ) |
| 91 | { |
| 92 | entry++; |
| 93 | } |
| 94 | |
| 95 | return entry->psa_alg; |
| 96 | } |
| 97 | |
Manuel Pégourié-Gonnard | de9ffe3 | 2022-07-26 10:20:52 +0200 | [diff] [blame^] | 98 | /* Get MD from PSA */ |
Manuel Pégourié-Gonnard | 4772884 | 2022-07-18 13:00:40 +0200 | [diff] [blame] | 99 | mbedtls_md_type_t mbedtls_hash_info_md_from_psa( psa_algorithm_t psa_alg ) |
| 100 | { |
| 101 | const hash_entry *entry = hash_table; |
| 102 | while( entry->md_type != MBEDTLS_MD_NONE && |
| 103 | entry->psa_alg != psa_alg ) |
| 104 | { |
| 105 | entry++; |
| 106 | } |
| 107 | |
| 108 | return entry->md_type; |
| 109 | } |