Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer: wrapper functions |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 21 | |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 22 | #include "mbedtls/platform_util.h" |
| 23 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_PK_C) |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 25 | #include "pk_wrap.h" |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 26 | #include "pk_internal.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 27 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 02b10d8 | 2023-03-28 12:33:20 +0200 | [diff] [blame] | 28 | #include "md_psa.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | e511ffc | 2013-08-22 17:33:21 +0200 | [diff] [blame] | 30 | /* Even if RSA not activated, for the sake of RSA-alt */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/rsa.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/ecdsa.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 39 | #endif |
| 40 | |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C) |
| 42 | #include "pkwrite.h" |
Andres Amaya Garcia | e32df08 | 2017-10-25 09:37:04 +0100 | [diff] [blame] | 43 | #endif |
| 44 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 45 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 46 | #include "psa_util_internal.h" |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 47 | #endif |
| 48 | |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 49 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 50 | #include "psa/crypto.h" |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 51 | |
Valerio Setti | 80d0798 | 2023-02-08 13:49:17 +0100 | [diff] [blame] | 52 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 53 | #include "mbedtls/asn1write.h" |
| 54 | #include "mbedtls/asn1.h" |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 55 | #endif |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 56 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 57 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 58 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 59 | |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 60 | #include <limits.h> |
Andres Amaya Garcia | 7c02c50 | 2017-08-04 13:32:15 +0100 | [diff] [blame] | 61 | #include <stdint.h> |
Gilles Peskine | 8a6022e | 2022-10-04 23:01:59 +0200 | [diff] [blame] | 62 | #include <string.h> |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 63 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 64 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Jerry Yu | b02ee18 | 2022-03-16 10:30:41 +0800 | [diff] [blame] | 65 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | int mbedtls_pk_error_from_psa(psa_status_t status) |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 67 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | switch (status) { |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 69 | case PSA_SUCCESS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | return 0; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 71 | case PSA_ERROR_INVALID_HANDLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 73 | case PSA_ERROR_NOT_PERMITTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | return MBEDTLS_ERR_ERROR_GENERIC_ERROR; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 75 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 77 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 79 | case PSA_ERROR_INVALID_ARGUMENT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | return MBEDTLS_ERR_PK_INVALID_ALG; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 81 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 83 | case PSA_ERROR_BAD_STATE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 85 | case PSA_ERROR_COMMUNICATION_FAILURE: |
| 86 | case PSA_ERROR_HARDWARE_FAILURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 88 | case PSA_ERROR_DATA_CORRUPT: |
| 89 | case PSA_ERROR_DATA_INVALID: |
| 90 | case PSA_ERROR_STORAGE_FAILURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 92 | case PSA_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 94 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | return MBEDTLS_ERR_ERROR_GENERIC_ERROR; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 99 | #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \ |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 100 | defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | int mbedtls_pk_error_from_psa_rsa(psa_status_t status) |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 102 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | switch (status) { |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 104 | case PSA_ERROR_NOT_PERMITTED: |
| 105 | case PSA_ERROR_INVALID_ARGUMENT: |
| 106 | case PSA_ERROR_INVALID_HANDLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 108 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 110 | case PSA_ERROR_INSUFFICIENT_ENTROPY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | return MBEDTLS_ERR_RSA_RNG_FAILED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 112 | case PSA_ERROR_INVALID_SIGNATURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 114 | case PSA_ERROR_INVALID_PADDING: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | return MBEDTLS_ERR_RSA_INVALID_PADDING; |
Andrzej Kurek | ba24138 | 2022-12-27 09:17:33 -0500 | [diff] [blame] | 116 | case PSA_SUCCESS: |
| 117 | return 0; |
| 118 | case PSA_ERROR_NOT_SUPPORTED: |
| 119 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 120 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 121 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 122 | case PSA_ERROR_BAD_STATE: |
| 123 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 124 | case PSA_ERROR_COMMUNICATION_FAILURE: |
| 125 | case PSA_ERROR_HARDWARE_FAILURE: |
| 126 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 127 | case PSA_ERROR_DATA_CORRUPT: |
| 128 | case PSA_ERROR_DATA_INVALID: |
| 129 | case PSA_ERROR_STORAGE_FAILURE: |
| 130 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 131 | case PSA_ERROR_CORRUPTION_DETECTED: |
| 132 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 133 | default: |
Andrzej Kurek | ba24138 | 2022-12-27 09:17:33 -0500 | [diff] [blame] | 134 | return MBEDTLS_ERR_ERROR_GENERIC_ERROR; |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 137 | #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */ |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 138 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 139 | |
| 140 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 141 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status) |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 143 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | switch (status) { |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 145 | case PSA_ERROR_NOT_PERMITTED: |
| 146 | case PSA_ERROR_INVALID_ARGUMENT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 148 | case PSA_ERROR_INVALID_HANDLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 150 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 152 | case PSA_ERROR_INSUFFICIENT_ENTROPY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 154 | case PSA_ERROR_INVALID_SIGNATURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | return MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Andrzej Kurek | ba24138 | 2022-12-27 09:17:33 -0500 | [diff] [blame] | 156 | case PSA_SUCCESS: |
| 157 | return 0; |
| 158 | case PSA_ERROR_NOT_SUPPORTED: |
| 159 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 160 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
| 161 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 162 | case PSA_ERROR_BAD_STATE: |
| 163 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 164 | case PSA_ERROR_COMMUNICATION_FAILURE: |
| 165 | case PSA_ERROR_HARDWARE_FAILURE: |
| 166 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 167 | case PSA_ERROR_DATA_CORRUPT: |
| 168 | case PSA_ERROR_DATA_INVALID: |
| 169 | case PSA_ERROR_STORAGE_FAILURE: |
| 170 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 171 | case PSA_ERROR_CORRUPTION_DETECTED: |
| 172 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 173 | default: |
Andrzej Kurek | ba24138 | 2022-12-27 09:17:33 -0500 | [diff] [blame] | 174 | return MBEDTLS_ERR_ERROR_GENERIC_ERROR; |
Jerry Yu | 07869e8 | 2022-03-16 16:40:50 +0800 | [diff] [blame] | 175 | } |
| 176 | } |
Jerry Yu | 7533982 | 2022-03-23 12:06:31 +0800 | [diff] [blame] | 177 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Jerry Yu | 7533982 | 2022-03-23 12:06:31 +0800 | [diff] [blame] | 178 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 179 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Neil Armstrong | 19915c2 | 2022-03-01 15:21:02 +0100 | [diff] [blame] | 180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | static int rsa_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 183 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | return type == MBEDTLS_PK_RSA || |
| 185 | type == MBEDTLS_PK_RSASSA_PSS; |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 186 | } |
| 187 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 188 | static size_t rsa_get_bitlen(mbedtls_pk_context *pk) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 189 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 190 | const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | return 8 * mbedtls_rsa_get_len(rsa); |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 194 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 195 | static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | const unsigned char *hash, size_t hash_len, |
| 197 | const unsigned char *sig, size_t sig_len) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 198 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 199 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 200 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 201 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 202 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 203 | psa_status_t status; |
| 204 | mbedtls_pk_context key; |
| 205 | int key_len; |
Neil Armstrong | 6baea78 | 2022-03-01 13:52:02 +0100 | [diff] [blame] | 206 | unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES]; |
Neil Armstrong | 82cf804 | 2022-03-03 12:30:59 +0100 | [diff] [blame] | 207 | psa_algorithm_t psa_alg_md = |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 208 | PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | size_t rsa_len = mbedtls_rsa_get_len(rsa); |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
| 212 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 213 | } |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | if (sig_len < rsa_len) { |
| 216 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
| 217 | } |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 218 | |
Neil Armstrong | ea54dbe | 2022-03-14 09:26:48 +0100 | [diff] [blame] | 219 | /* mbedtls_pk_write_pubkey_der() expects a full PK context; |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 220 | * re-construct one to make it happy */ |
Neil Armstrong | 253e9e7 | 2022-03-16 15:32:23 +0100 | [diff] [blame] | 221 | key.pk_info = &mbedtls_rsa_info; |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 222 | key.pk_ctx = rsa; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf)); |
| 224 | if (key_len <= 0) { |
| 225 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 226 | } |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 229 | psa_set_key_algorithm(&attributes, psa_alg_md); |
| 230 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | status = psa_import_key(&attributes, |
| 233 | buf + sizeof(buf) - key_len, key_len, |
| 234 | &key_id); |
| 235 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 236 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 237 | goto cleanup; |
| 238 | } |
| 239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len, |
| 241 | sig, sig_len); |
| 242 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 243 | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 244 | goto cleanup; |
| 245 | } |
| 246 | ret = 0; |
| 247 | |
| 248 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | status = psa_destroy_key(key_id); |
| 250 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 251 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | } |
Neil Armstrong | a33280a | 2022-02-24 15:17:47 +0100 | [diff] [blame] | 253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | return ret; |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 255 | } |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 256 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 257 | static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | const unsigned char *hash, size_t hash_len, |
| 259 | const unsigned char *sig, size_t sig_len) |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 260 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 261 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 262 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | size_t rsa_len = mbedtls_rsa_get_len(rsa); |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
| 266 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 267 | } |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | if (sig_len < rsa_len) { |
| 270 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
| 271 | } |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg, |
| 274 | (unsigned int) hash_len, |
| 275 | hash, sig)) != 0) { |
| 276 | return ret; |
| 277 | } |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 278 | |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 279 | /* The buffer contains a valid signature followed by extra data. |
| 280 | * We have a special error code for that so that so that callers can |
| 281 | * use mbedtls_pk_verify() to check "Does the buffer start with a |
| 282 | * valid signature?" and not just "Does the buffer contain a valid |
| 283 | * signature?". */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | if (sig_len > rsa_len) { |
| 285 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 286 | } |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | return 0; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 289 | } |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 290 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 291 | |
Jerry Yu | b02ee18 | 2022-03-16 10:30:41 +0800 | [diff] [blame] | 292 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg, |
| 294 | mbedtls_rsa_context *rsa_ctx, |
| 295 | const unsigned char *hash, size_t hash_len, |
| 296 | unsigned char *sig, size_t sig_size, |
| 297 | size_t *sig_len) |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 298 | { |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 299 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 300 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 301 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 302 | psa_status_t status; |
| 303 | mbedtls_pk_context key; |
| 304 | int key_len; |
Sarvesh Bodakhe | 430a4f3 | 2023-07-27 14:51:25 +0530 | [diff] [blame] | 305 | unsigned char *buf = NULL; |
| 306 | buf = mbedtls_calloc(1, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES); |
| 307 | if (buf == NULL) { |
| 308 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 309 | } |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 310 | mbedtls_pk_info_t pk_info = mbedtls_rsa_info; |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | *sig_len = mbedtls_rsa_get_len(rsa_ctx); |
| 313 | if (sig_size < *sig_len) { |
Sarvesh Bodakhe | 430a4f3 | 2023-07-27 14:51:25 +0530 | [diff] [blame] | 314 | mbedtls_free(buf); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 316 | } |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 317 | |
Neil Armstrong | e4f2868 | 2022-02-24 15:41:39 +0100 | [diff] [blame] | 318 | /* mbedtls_pk_write_key_der() expects a full PK context; |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 319 | * re-construct one to make it happy */ |
| 320 | key.pk_info = &pk_info; |
Jerry Yu | e010de4 | 2022-03-23 11:45:55 +0800 | [diff] [blame] | 321 | key.pk_ctx = rsa_ctx; |
Sarvesh Bodakhe | 430a4f3 | 2023-07-27 14:51:25 +0530 | [diff] [blame] | 322 | key_len = mbedtls_pk_write_key_der(&key, buf, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | if (key_len <= 0) { |
Sarvesh Bodakhe | 430a4f3 | 2023-07-27 14:51:25 +0530 | [diff] [blame] | 324 | mbedtls_free(buf); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 326 | } |
| 327 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 328 | psa_set_key_algorithm(&attributes, alg); |
| 329 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR); |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | status = psa_import_key(&attributes, |
Sarvesh Bodakhe | 430a4f3 | 2023-07-27 14:51:25 +0530 | [diff] [blame] | 332 | buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES - key_len, key_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | &key_id); |
| 334 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 335 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 336 | goto cleanup; |
| 337 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | status = psa_sign_hash(key_id, alg, hash, hash_len, |
| 339 | sig, sig_size, sig_len); |
| 340 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 341 | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 342 | goto cleanup; |
| 343 | } |
| 344 | |
| 345 | ret = 0; |
| 346 | |
| 347 | cleanup: |
Sarvesh Bodakhe | 430a4f3 | 2023-07-27 14:51:25 +0530 | [diff] [blame] | 348 | mbedtls_free(buf); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | status = psa_destroy_key(key_id); |
| 350 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 351 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 352 | } |
| 353 | return ret; |
Neil Armstrong | 9854568 | 2022-02-22 16:12:51 +0100 | [diff] [blame] | 354 | } |
Jerry Yu | 406cf27 | 2022-03-22 11:33:42 +0800 | [diff] [blame] | 355 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 356 | |
| 357 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 358 | static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | const unsigned char *hash, size_t hash_len, |
| 360 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 361 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 362 | { |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 363 | ((void) f_rng); |
| 364 | ((void) p_rng); |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 365 | |
Jerry Yu | bd1b327 | 2022-03-24 13:05:20 +0800 | [diff] [blame] | 366 | psa_algorithm_t psa_md_alg; |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 367 | psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | if (psa_md_alg == 0) { |
| 369 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 370 | } |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN( |
| 373 | psa_md_alg), |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 374 | pk->pk_ctx, hash, hash_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | sig, sig_size, sig_len); |
Jerry Yu | 1d172a3 | 2022-03-12 19:12:05 +0800 | [diff] [blame] | 376 | } |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 377 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 378 | static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | const unsigned char *hash, size_t hash_len, |
| 380 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 381 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 382 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 383 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
| 386 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 387 | } |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | *sig_len = mbedtls_rsa_get_len(rsa); |
| 390 | if (sig_size < *sig_len) { |
| 391 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 392 | } |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng, |
| 395 | md_alg, (unsigned int) hash_len, |
| 396 | hash, sig); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 397 | } |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 398 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 399 | |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 400 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 401 | static int rsa_decrypt_wrap(mbedtls_pk_context *pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | const unsigned char *input, size_t ilen, |
| 403 | unsigned char *output, size_t *olen, size_t osize, |
| 404 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 405 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 406 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 407 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 408 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 409 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 410 | psa_status_t status; |
| 411 | mbedtls_pk_context key; |
| 412 | int key_len; |
Neil Armstrong | b556a42 | 2022-02-25 08:58:12 +0100 | [diff] [blame] | 413 | unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES]; |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 414 | |
| 415 | ((void) f_rng); |
| 416 | ((void) p_rng); |
| 417 | |
| 418 | #if !defined(MBEDTLS_RSA_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | if (rsa->padding != MBEDTLS_RSA_PKCS_V15) { |
| 420 | return MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 421 | } |
Neil Armstrong | 8e80504 | 2022-03-16 15:30:31 +0100 | [diff] [blame] | 422 | #endif /* !MBEDTLS_RSA_ALT */ |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | if (ilen != mbedtls_rsa_get_len(rsa)) { |
| 425 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 426 | } |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 427 | |
| 428 | /* mbedtls_pk_write_key_der() expects a full PK context; |
| 429 | * re-construct one to make it happy */ |
Neil Armstrong | 6b03a3d | 2022-03-16 15:31:07 +0100 | [diff] [blame] | 430 | key.pk_info = &mbedtls_rsa_info; |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 431 | key.pk_ctx = rsa; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf)); |
| 433 | if (key_len <= 0) { |
| 434 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 435 | } |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR); |
| 438 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
| 439 | psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT); |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | status = psa_import_key(&attributes, |
| 442 | buf + sizeof(buf) - key_len, key_len, |
| 443 | &key_id); |
| 444 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 445 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 446 | goto cleanup; |
| 447 | } |
| 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT, |
| 450 | input, ilen, |
| 451 | NULL, 0, |
| 452 | output, osize, olen); |
| 453 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 454 | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 455 | goto cleanup; |
| 456 | } |
| 457 | |
| 458 | ret = 0; |
| 459 | |
| 460 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 461 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
| 462 | status = psa_destroy_key(key_id); |
| 463 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 464 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | } |
Neil Armstrong | f1b564b | 2022-02-24 15:17:47 +0100 | [diff] [blame] | 466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | return ret; |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 468 | } |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 469 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 470 | static int rsa_decrypt_wrap(mbedtls_pk_context *pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | const unsigned char *input, size_t ilen, |
| 472 | unsigned char *output, size_t *olen, size_t osize, |
| 473 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 474 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 475 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
Hanno Becker | 6a1e7e5 | 2017-08-22 13:55:00 +0100 | [diff] [blame] | 476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | if (ilen != mbedtls_rsa_get_len(rsa)) { |
| 478 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 479 | } |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng, |
| 482 | olen, input, output, osize); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 483 | } |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 484 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 485 | |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 486 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 487 | static int rsa_encrypt_wrap(mbedtls_pk_context *pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | const unsigned char *input, size_t ilen, |
| 489 | unsigned char *output, size_t *olen, size_t osize, |
| 490 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 491 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 492 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 493 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 494 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 495 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 496 | psa_status_t status; |
| 497 | mbedtls_pk_context key; |
| 498 | int key_len; |
Neil Armstrong | deb4bfb | 2022-02-25 08:58:12 +0100 | [diff] [blame] | 499 | unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES]; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 500 | |
| 501 | ((void) f_rng); |
| 502 | ((void) p_rng); |
| 503 | |
| 504 | #if !defined(MBEDTLS_RSA_ALT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | if (rsa->padding != MBEDTLS_RSA_PKCS_V15) { |
| 506 | return MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 507 | } |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 508 | #endif |
| 509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | if (mbedtls_rsa_get_len(rsa) > osize) { |
| 511 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 512 | } |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 513 | |
Neil Armstrong | ac014ca | 2022-02-24 15:27:54 +0100 | [diff] [blame] | 514 | /* mbedtls_pk_write_pubkey_der() expects a full PK context; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 515 | * re-construct one to make it happy */ |
Neil Armstrong | da1d80d | 2022-03-16 15:36:32 +0100 | [diff] [blame] | 516 | key.pk_info = &mbedtls_rsa_info; |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 517 | key.pk_ctx = rsa; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf)); |
| 519 | if (key_len <= 0) { |
| 520 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 521 | } |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 524 | psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT); |
| 525 | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | status = psa_import_key(&attributes, |
| 528 | buf + sizeof(buf) - key_len, key_len, |
| 529 | &key_id); |
| 530 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 531 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 532 | goto cleanup; |
| 533 | } |
| 534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT, |
| 536 | input, ilen, |
| 537 | NULL, 0, |
| 538 | output, osize, olen); |
| 539 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 540 | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 541 | goto cleanup; |
| 542 | } |
| 543 | |
| 544 | ret = 0; |
| 545 | |
| 546 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | status = psa_destroy_key(key_id); |
| 548 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 549 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | } |
Neil Armstrong | 7dd3b20 | 2022-02-24 15:29:18 +0100 | [diff] [blame] | 551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | return ret; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 553 | } |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 554 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 555 | static int rsa_encrypt_wrap(mbedtls_pk_context *pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | const unsigned char *input, size_t ilen, |
| 557 | unsigned char *output, size_t *olen, size_t osize, |
| 558 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 559 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 560 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 561 | *olen = mbedtls_rsa_get_len(rsa); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | if (*olen > osize) { |
| 564 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 565 | } |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng, |
| 568 | ilen, input, output); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 569 | } |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 570 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 571 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 572 | static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | int (*f_rng)(void *, unsigned char *, size_t), |
| 574 | void *p_rng) |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 575 | { |
Manuel Pégourié-Gonnard | 39be141 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 576 | (void) f_rng; |
| 577 | (void) p_rng; |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 578 | return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx, |
| 579 | (const mbedtls_rsa_context *) prv->pk_ctx); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | static void *rsa_alloc_wrap(void) |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 583 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context)); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | if (ctx != NULL) { |
| 587 | mbedtls_rsa_init((mbedtls_rsa_context *) ctx); |
| 588 | } |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 589 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | return ctx; |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | static void rsa_free_wrap(void *ctx) |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 594 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | mbedtls_rsa_free((mbedtls_rsa_context *) ctx); |
| 596 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 597 | } |
| 598 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 599 | static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items) |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 600 | { |
Gilles Peskine | 85b1bc6 | 2021-05-25 09:20:26 +0200 | [diff] [blame] | 601 | #if defined(MBEDTLS_RSA_ALT) |
| 602 | /* Not supported */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 603 | (void) pk; |
Gilles Peskine | 85b1bc6 | 2021-05-25 09:20:26 +0200 | [diff] [blame] | 604 | (void) items; |
| 605 | #else |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 606 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
| 607 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 608 | items->type = MBEDTLS_PK_DEBUG_MPI; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 609 | items->name = "rsa.N"; |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 610 | items->value = &(rsa->N); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 611 | |
| 612 | items++; |
| 613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 614 | items->type = MBEDTLS_PK_DEBUG_MPI; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 615 | items->name = "rsa.E"; |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 616 | items->value = &(rsa->E); |
Gilles Peskine | 85b1bc6 | 2021-05-25 09:20:26 +0200 | [diff] [blame] | 617 | #endif |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | const mbedtls_pk_info_t mbedtls_rsa_info = { |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 621 | .type = MBEDTLS_PK_RSA, |
| 622 | .name = "RSA", |
| 623 | .get_bitlen = rsa_get_bitlen, |
| 624 | .can_do = rsa_can_do, |
| 625 | .verify_func = rsa_verify_wrap, |
| 626 | .sign_func = rsa_sign_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 627 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 628 | .verify_rs_func = NULL, |
| 629 | .sign_rs_func = NULL, |
| 630 | .rs_alloc_func = NULL, |
| 631 | .rs_free_func = NULL, |
| 632 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 633 | .decrypt_func = rsa_decrypt_wrap, |
| 634 | .encrypt_func = rsa_encrypt_wrap, |
| 635 | .check_pair_func = rsa_check_pair_wrap, |
| 636 | .ctx_alloc_func = rsa_alloc_wrap, |
| 637 | .ctx_free_func = rsa_free_wrap, |
| 638 | .debug_func = rsa_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 639 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 640 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 641 | |
Valerio Setti | 81d7512 | 2023-06-14 14:49:33 +0200 | [diff] [blame] | 642 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 643 | /* |
| 644 | * Generic EC key |
| 645 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | static int eckey_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 647 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 648 | return type == MBEDTLS_PK_ECKEY || |
| 649 | type == MBEDTLS_PK_ECKEY_DH || |
| 650 | type == MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 651 | } |
| 652 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 653 | static size_t eckey_get_bitlen(mbedtls_pk_context *pk) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 654 | { |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 655 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 656 | return pk->ec_bits; |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 657 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 658 | mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx; |
| 659 | return ecp->grp.pbits; |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 660 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 661 | } |
| 662 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 663 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 664 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 665 | /* |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 666 | * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of |
| 667 | * those integers and convert it to the fixed-length encoding expected by PSA. |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 668 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end, |
| 670 | unsigned char *to, size_t to_len) |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 671 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 672 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 673 | size_t unpadded_len, padding_len; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 675 | if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len, |
| 676 | MBEDTLS_ASN1_INTEGER)) != 0) { |
| 677 | return ret; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 678 | } |
| 679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 680 | while (unpadded_len > 0 && **from == 0x00) { |
| 681 | (*from)++; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 682 | unpadded_len--; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 683 | } |
| 684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 685 | if (unpadded_len > to_len || unpadded_len == 0) { |
| 686 | return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 687 | } |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 688 | |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 689 | padding_len = to_len - unpadded_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | memset(to, 0x00, padding_len); |
| 691 | memcpy(to + padding_len, *from, unpadded_len); |
| 692 | (*from) += unpadded_len; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 694 | return 0; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 698 | * Convert a signature from an ASN.1 sequence of two integers |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 699 | * to a raw {r,s} buffer. Note: the provided sig buffer must be at least |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 700 | * twice as big as int_size. |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 701 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 702 | static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end, |
| 703 | unsigned char *sig, size_t int_size) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 704 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 705 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 706 | size_t tmp_size; |
Andrzej Kurek | 3f864c2 | 2018-11-07 09:30:50 -0500 | [diff] [blame] | 707 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size, |
| 709 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 710 | return ret; |
| 711 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 712 | |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 713 | /* Extract r */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 714 | if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) { |
| 715 | return ret; |
| 716 | } |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 717 | /* Extract s */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) { |
| 719 | return ret; |
| 720 | } |
Andrzej Kurek | 3f864c2 | 2018-11-07 09:30:50 -0500 | [diff] [blame] | 721 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 722 | return 0; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 723 | } |
| 724 | |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 725 | /* Common helper for ECDSA verify using PSA functions. */ |
Valerio Setti | ed7d6af | 2023-06-21 15:42:21 +0200 | [diff] [blame] | 726 | static int ecdsa_verify_psa(unsigned char *key, size_t key_len, |
| 727 | psa_ecc_family_t curve, size_t curve_bits, |
| 728 | const unsigned char *hash, size_t hash_len, |
| 729 | const unsigned char *sig, size_t sig_len) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 730 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 731 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 732 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 733 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 734 | psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY; |
Valerio Setti | ed7d6af | 2023-06-21 15:42:21 +0200 | [diff] [blame] | 735 | size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits); |
| 736 | unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE]; |
| 737 | unsigned char *p; |
| 738 | psa_status_t status; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 740 | if (curve == 0) { |
| 741 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 742 | } |
Andrzej Kurek | b3d1b12 | 2018-11-07 08:18:52 -0500 | [diff] [blame] | 743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve)); |
| 745 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 746 | psa_set_key_algorithm(&attributes, psa_sig_md); |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 747 | |
Valerio Setti | ed7d6af | 2023-06-21 15:42:21 +0200 | [diff] [blame] | 748 | status = psa_import_key(&attributes, key, key_len, &key_id); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 750 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 751 | goto cleanup; |
| 752 | } |
| 753 | |
Valerio Setti | ed7d6af | 2023-06-21 15:42:21 +0200 | [diff] [blame] | 754 | if (signature_len > sizeof(extracted_sig)) { |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 755 | ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 756 | goto cleanup; |
| 757 | } |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 759 | p = (unsigned char *) sig; |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 760 | /* extract_ecdsa_sig's last parameter is the size |
Valerio Setti | f57007d | 2023-05-19 13:54:39 +0200 | [diff] [blame] | 761 | * of each integer to be parsed, so it's actually half |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 762 | * the size of the signature. */ |
Valerio Setti | ed7d6af | 2023-06-21 15:42:21 +0200 | [diff] [blame] | 763 | if ((ret = extract_ecdsa_sig(&p, sig + sig_len, extracted_sig, |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 764 | signature_len/2)) != 0) { |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 765 | goto cleanup; |
| 766 | } |
| 767 | |
Valerio Setti | ed7d6af | 2023-06-21 15:42:21 +0200 | [diff] [blame] | 768 | status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len, |
| 769 | extracted_sig, signature_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 770 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 771 | ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | goto cleanup; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 773 | } |
Andrzej Kurek | ad5d581 | 2018-11-20 07:59:18 -0500 | [diff] [blame] | 774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 775 | if (p != sig + sig_len) { |
Andrzej Kurek | ad5d581 | 2018-11-20 07:59:18 -0500 | [diff] [blame] | 776 | ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 777 | goto cleanup; |
| 778 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 779 | ret = 0; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 780 | |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 781 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 782 | status = psa_destroy_key(key_id); |
| 783 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 784 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | } |
Neil Armstrong | 9dccd86 | 2022-02-24 15:33:13 +0100 | [diff] [blame] | 786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | return ret; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 788 | } |
Valerio Setti | ed7d6af | 2023-06-21 15:42:21 +0200 | [diff] [blame] | 789 | |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 790 | static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk, |
| 791 | mbedtls_md_type_t md_alg, |
| 792 | const unsigned char *hash, size_t hash_len, |
| 793 | const unsigned char *sig, size_t sig_len) |
Valerio Setti | e773077 | 2023-06-21 16:58:40 +0200 | [diff] [blame] | 794 | { |
| 795 | (void) md_alg; |
| 796 | unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; |
| 797 | size_t key_len; |
| 798 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 799 | psa_ecc_family_t curve; |
| 800 | size_t curve_bits; |
| 801 | psa_status_t status; |
| 802 | |
| 803 | status = psa_get_key_attributes(pk->priv_id, &key_attr); |
| 804 | if (status != PSA_SUCCESS) { |
| 805 | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
| 806 | } |
| 807 | curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr)); |
| 808 | curve_bits = psa_get_key_bits(&key_attr); |
| 809 | psa_reset_key_attributes(&key_attr); |
| 810 | |
| 811 | status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len); |
| 812 | if (status != PSA_SUCCESS) { |
| 813 | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
| 814 | } |
| 815 | |
| 816 | return ecdsa_verify_psa(key, key_len, curve, curve_bits, |
| 817 | hash, hash_len, sig, sig_len); |
| 818 | } |
| 819 | |
Valerio Setti | ed7d6af | 2023-06-21 15:42:21 +0200 | [diff] [blame] | 820 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 821 | static int ecdsa_verify_wrap(mbedtls_pk_context *pk, |
| 822 | mbedtls_md_type_t md_alg, |
| 823 | const unsigned char *hash, size_t hash_len, |
| 824 | const unsigned char *sig, size_t sig_len) |
| 825 | { |
| 826 | (void) md_alg; |
| 827 | psa_ecc_family_t curve = pk->ec_family; |
| 828 | size_t curve_bits = pk->ec_bits; |
| 829 | |
| 830 | return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits, |
| 831 | hash, hash_len, sig, sig_len); |
| 832 | } |
| 833 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 834 | static int ecdsa_verify_wrap(mbedtls_pk_context *pk, |
| 835 | mbedtls_md_type_t md_alg, |
| 836 | const unsigned char *hash, size_t hash_len, |
| 837 | const unsigned char *sig, size_t sig_len) |
| 838 | { |
| 839 | (void) md_alg; |
| 840 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 841 | mbedtls_ecp_keypair *ctx = pk->pk_ctx; |
| 842 | unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
| 843 | size_t key_len; |
| 844 | size_t curve_bits; |
| 845 | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
| 846 | |
| 847 | ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q, |
| 848 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 849 | &key_len, key, sizeof(key)); |
| 850 | if (ret != 0) { |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | return ecdsa_verify_psa(key, key_len, curve, curve_bits, |
| 855 | hash, hash_len, sig, sig_len); |
| 856 | } |
| 857 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 858 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 859 | static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | const unsigned char *hash, size_t hash_len, |
| 861 | const unsigned char *sig, size_t sig_len) |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 862 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 863 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 864 | ((void) md_alg); |
| 865 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 866 | ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 867 | hash, hash_len, sig, sig_len); |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 869 | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
| 870 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 871 | } |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 873 | return ret; |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 874 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 875 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 876 | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 877 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 878 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 879 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 880 | /* |
| 881 | * Simultaneously convert and move raw MPI from the beginning of a buffer |
| 882 | * to an ASN.1 MPI at the end of the buffer. |
| 883 | * See also mbedtls_asn1_write_mpi(). |
| 884 | * |
| 885 | * p: pointer to the end of the output buffer |
| 886 | * start: start of the output buffer, and also of the mpi to write at the end |
| 887 | * n_len: length of the mpi to read from start |
| 888 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | static int asn1_write_mpibuf(unsigned char **p, unsigned char *start, |
| 890 | size_t n_len) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 891 | { |
| 892 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 893 | size_t len = 0; |
| 894 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 895 | if ((size_t) (*p - start) < n_len) { |
| 896 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 897 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 898 | |
| 899 | len = n_len; |
| 900 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | memmove(*p, start, len); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 902 | |
| 903 | /* ASN.1 DER encoding requires minimal length, so skip leading 0s. |
| 904 | * Neither r nor s should be 0, but as a failsafe measure, still detect |
| 905 | * that rather than overflowing the buffer in case of a PSA error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | while (len > 0 && **p == 0x00) { |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 907 | ++(*p); |
| 908 | --len; |
| 909 | } |
| 910 | |
| 911 | /* this is only reached if the signature was invalid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | if (len == 0) { |
| 913 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 914 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 915 | |
| 916 | /* if the msb is 1, ASN.1 requires that we prepend a 0. |
| 917 | * Neither r nor s can be 0, so we can assume len > 0 at all times. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 918 | if (**p & 0x80) { |
| 919 | if (*p - start < 1) { |
| 920 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 921 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 922 | |
| 923 | *--(*p) = 0x00; |
| 924 | len += 1; |
| 925 | } |
| 926 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 927 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 928 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 929 | MBEDTLS_ASN1_INTEGER)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 931 | return (int) len; |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* Transcode signature from PSA format to ASN.1 sequence. |
| 935 | * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of |
| 936 | * MPIs, and in-place. |
| 937 | * |
| 938 | * [in/out] sig: the signature pre- and post-transcoding |
| 939 | * [in/out] sig_len: signature length pre- and post-transcoding |
| 940 | * [int] buf_len: the available size the in/out buffer |
| 941 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len, |
| 943 | size_t buf_len) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 944 | { |
| 945 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 946 | size_t len = 0; |
| 947 | const size_t rs_len = *sig_len / 2; |
| 948 | unsigned char *p = sig + buf_len; |
| 949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 950 | MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len)); |
| 951 | MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 953 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len)); |
| 954 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig, |
| 955 | MBEDTLS_ASN1_CONSTRUCTED | |
| 956 | MBEDTLS_ASN1_SEQUENCE)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 958 | memmove(sig, p, len); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 959 | *sig_len = len; |
| 960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | return 0; |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 962 | } |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 963 | |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 964 | /* Common helper for ECDSA sign using PSA functions. */ |
Valerio Setti | 884c1ec | 2023-06-23 12:09:13 +0200 | [diff] [blame] | 965 | static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg, |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 966 | const unsigned char *hash, size_t hash_len, |
| 967 | unsigned char *sig, size_t sig_size, size_t *sig_len) |
| 968 | { |
| 969 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 970 | psa_status_t status; |
Valerio Setti | 884c1ec | 2023-06-23 12:09:13 +0200 | [diff] [blame] | 971 | psa_algorithm_t psa_sig_md; |
| 972 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 973 | psa_algorithm_t alg; |
| 974 | |
| 975 | status = psa_get_key_attributes(key_id, &key_attr); |
| 976 | if (status != PSA_SUCCESS) { |
| 977 | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
| 978 | } |
| 979 | alg = psa_get_key_algorithm(&key_attr); |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 980 | psa_reset_key_attributes(&key_attr); |
Valerio Setti | 884c1ec | 2023-06-23 12:09:13 +0200 | [diff] [blame] | 981 | |
| 982 | if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) { |
| 983 | psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)); |
| 984 | } else { |
| 985 | psa_sig_md = PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)); |
| 986 | } |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 987 | |
| 988 | status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len, |
| 989 | sig, sig_size, sig_len); |
| 990 | if (status != PSA_SUCCESS) { |
| 991 | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
| 992 | } |
| 993 | |
| 994 | ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size); |
| 995 | |
| 996 | return ret; |
| 997 | } |
| 998 | |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 999 | static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk, |
| 1000 | mbedtls_md_type_t md_alg, |
| 1001 | const unsigned char *hash, size_t hash_len, |
| 1002 | unsigned char *sig, size_t sig_size, |
| 1003 | size_t *sig_len, |
| 1004 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1005 | void *p_rng) |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1006 | { |
| 1007 | ((void) f_rng); |
| 1008 | ((void) p_rng); |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1009 | |
Valerio Setti | 884c1ec | 2023-06-23 12:09:13 +0200 | [diff] [blame] | 1010 | return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size, |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1011 | sig_len); |
| 1012 | } |
| 1013 | |
| 1014 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1015 | /* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up |
| 1016 | * using the same function. */ |
| 1017 | #define ecdsa_sign_wrap ecdsa_opaque_sign_wrap |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1018 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1019 | static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | const unsigned char *hash, size_t hash_len, |
| 1021 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1022 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1023 | { |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1024 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1025 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 1026 | psa_status_t status; |
Valerio Setti | ae8c628 | 2023-05-18 18:57:57 +0200 | [diff] [blame] | 1027 | mbedtls_ecp_keypair *ctx = pk->pk_ctx; |
| 1028 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1029 | unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH]; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1030 | size_t curve_bits; |
| 1031 | psa_ecc_family_t curve = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1032 | mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 1033 | size_t key_len = PSA_BITS_TO_BYTES(curve_bits); |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1034 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 1035 | psa_algorithm_t psa_sig_md = |
| 1036 | PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)); |
| 1037 | #else |
| 1038 | psa_algorithm_t psa_sig_md = |
| 1039 | PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)); |
| 1040 | #endif |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1041 | ((void) f_rng); |
| 1042 | ((void) p_rng); |
| 1043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1044 | if (curve == 0) { |
| 1045 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1046 | } |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1047 | |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 1048 | if (key_len > sizeof(buf)) { |
Valerio Setti | b761b15 | 2023-01-31 14:56:04 +0100 | [diff] [blame] | 1049 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1050 | } |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 1051 | ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | if (ret != 0) { |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1053 | goto cleanup; |
| 1054 | } |
| 1055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1056 | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); |
| 1057 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 1058 | psa_set_key_algorithm(&attributes, psa_sig_md); |
| 1059 | |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1060 | status = psa_import_key(&attributes, buf, key_len, &key_id); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1062 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | goto cleanup; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
Valerio Setti | 884c1ec | 2023-06-23 12:09:13 +0200 | [diff] [blame] | 1066 | ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len); |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1067 | |
| 1068 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1069 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
| 1070 | status = psa_destroy_key(key_id); |
| 1071 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1072 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1073 | } |
Neil Armstrong | ff70f0b | 2022-03-03 14:31:17 +0100 | [diff] [blame] | 1074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | return ret; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 1076 | } |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1077 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1078 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1079 | static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | const unsigned char *hash, size_t hash_len, |
| 1081 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1082 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1083 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1084 | return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | md_alg, hash, hash_len, |
| 1086 | sig, sig_size, sig_len, |
| 1087 | f_rng, p_rng); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1088 | } |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1089 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1090 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 1091 | |
Valerio Setti | 80d0798 | 2023-02-08 13:49:17 +0100 | [diff] [blame] | 1092 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1093 | /* Forward declarations */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1094 | static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1095 | const unsigned char *hash, size_t hash_len, |
| 1096 | const unsigned char *sig, size_t sig_len, |
| 1097 | void *rs_ctx); |
| 1098 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1099 | static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1100 | const unsigned char *hash, size_t hash_len, |
| 1101 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1102 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1103 | void *rs_ctx); |
| 1104 | |
| 1105 | /* |
| 1106 | * Restart context for ECDSA operations with ECKEY context |
| 1107 | * |
| 1108 | * We need to store an actual ECDSA context, as we need to pass the same to |
| 1109 | * the underlying ecdsa function, so we can't create it on the fly every time. |
| 1110 | */ |
| 1111 | typedef struct { |
| 1112 | mbedtls_ecdsa_restart_ctx ecdsa_rs; |
| 1113 | mbedtls_ecdsa_context ecdsa_ctx; |
| 1114 | } eckey_restart_ctx; |
| 1115 | |
| 1116 | static void *eckey_rs_alloc(void) |
| 1117 | { |
| 1118 | eckey_restart_ctx *rs_ctx; |
| 1119 | |
| 1120 | void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx)); |
| 1121 | |
| 1122 | if (ctx != NULL) { |
| 1123 | rs_ctx = ctx; |
| 1124 | mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs); |
| 1125 | mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx); |
| 1126 | } |
| 1127 | |
| 1128 | return ctx; |
| 1129 | } |
| 1130 | |
| 1131 | static void eckey_rs_free(void *ctx) |
| 1132 | { |
| 1133 | eckey_restart_ctx *rs_ctx; |
| 1134 | |
| 1135 | if (ctx == NULL) { |
| 1136 | return; |
| 1137 | } |
| 1138 | |
| 1139 | rs_ctx = ctx; |
| 1140 | mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs); |
| 1141 | mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx); |
| 1142 | |
| 1143 | mbedtls_free(ctx); |
| 1144 | } |
| 1145 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1146 | static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1147 | const unsigned char *hash, size_t hash_len, |
| 1148 | const unsigned char *sig, size_t sig_len, |
| 1149 | void *rs_ctx) |
| 1150 | { |
| 1151 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1152 | eckey_restart_ctx *rs = rs_ctx; |
| 1153 | |
| 1154 | /* Should never happen */ |
| 1155 | if (rs == NULL) { |
| 1156 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1157 | } |
| 1158 | |
| 1159 | /* set up our own sub-context if needed (that is, on first run) */ |
| 1160 | if (rs->ecdsa_ctx.grp.pbits == 0) { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1161 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx)); |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1164 | MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1165 | md_alg, hash, hash_len, |
| 1166 | sig, sig_len, &rs->ecdsa_rs)); |
| 1167 | |
| 1168 | cleanup: |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1172 | static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1173 | const unsigned char *hash, size_t hash_len, |
| 1174 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1175 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1176 | void *rs_ctx) |
| 1177 | { |
| 1178 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1179 | eckey_restart_ctx *rs = rs_ctx; |
| 1180 | |
| 1181 | /* Should never happen */ |
| 1182 | if (rs == NULL) { |
| 1183 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1184 | } |
| 1185 | |
| 1186 | /* set up our own sub-context if needed (that is, on first run) */ |
| 1187 | if (rs->ecdsa_ctx.grp.pbits == 0) { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1188 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx)); |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1191 | MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1192 | hash, hash_len, sig, sig_size, sig_len, |
| 1193 | f_rng, p_rng, &rs->ecdsa_rs)); |
| 1194 | |
| 1195 | cleanup: |
| 1196 | return ret; |
| 1197 | } |
Valerio Setti | 80d0798 | 2023-02-08 13:49:17 +0100 | [diff] [blame] | 1198 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1199 | |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1200 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1201 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1202 | static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv) |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1203 | { |
Valerio Setti | 9efa8c4 | 2023-05-19 13:27:30 +0200 | [diff] [blame] | 1204 | psa_status_t status; |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1205 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Valerio Setti | 8eb5526 | 2023-04-04 10:20:53 +0200 | [diff] [blame] | 1206 | uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1207 | size_t prv_key_len; |
Valerio Setti | ae8c628 | 2023-05-18 18:57:57 +0200 | [diff] [blame] | 1208 | mbedtls_svc_key_id_t key_id = prv->priv_id; |
Valerio Setti | 9efa8c4 | 2023-05-19 13:27:30 +0200 | [diff] [blame] | 1209 | |
| 1210 | status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf), |
| 1211 | &prv_key_len); |
| 1212 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
| 1213 | if (ret != 0) { |
| 1214 | return ret; |
| 1215 | } |
| 1216 | |
| 1217 | if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) { |
| 1218 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1219 | } |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 1224 | static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv) |
| 1225 | { |
| 1226 | psa_status_t status; |
| 1227 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1228 | uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
| 1229 | size_t prv_key_len; |
Valerio Setti | 9efa8c4 | 2023-05-19 13:27:30 +0200 | [diff] [blame] | 1230 | psa_status_t destruction_status; |
Valerio Setti | ae8c628 | 2023-05-18 18:57:57 +0200 | [diff] [blame] | 1231 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 1232 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1233 | uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
| 1234 | size_t pub_key_len; |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1235 | size_t curve_bits; |
Valerio Setti | f286664 | 2023-04-06 16:49:54 +0200 | [diff] [blame] | 1236 | const psa_ecc_family_t curve = |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 1237 | mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits); |
Valerio Setti | c1541cb | 2023-05-17 15:49:55 +0200 | [diff] [blame] | 1238 | const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits); |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1239 | |
Valerio Setti | a7cb845 | 2023-05-22 18:39:43 +0200 | [diff] [blame] | 1240 | if (curve == 0) { |
| 1241 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1242 | } |
| 1243 | |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1244 | psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); |
| 1245 | psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT); |
| 1246 | |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 1247 | ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d, |
| 1248 | prv_key_buf, curve_bytes); |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1249 | if (ret != 0) { |
Valerio Setti | 35d1dac | 2023-06-30 18:04:16 +0200 | [diff] [blame] | 1250 | mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf)); |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1251 | return ret; |
| 1252 | } |
| 1253 | |
| 1254 | status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id); |
Valerio Setti | 35d1dac | 2023-06-30 18:04:16 +0200 | [diff] [blame] | 1255 | mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf)); |
Valerio Setti | 1df94f8 | 2023-04-07 08:59:24 +0200 | [diff] [blame] | 1256 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
| 1257 | if (ret != 0) { |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1258 | return ret; |
| 1259 | } |
| 1260 | |
Valerio Setti | 35d1dac | 2023-06-30 18:04:16 +0200 | [diff] [blame] | 1261 | // From now on prv_key_buf is used to store the public key of prv. |
Valerio Setti | 1df94f8 | 2023-04-07 08:59:24 +0200 | [diff] [blame] | 1262 | status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf), |
| 1263 | &prv_key_len); |
| 1264 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
| 1265 | destruction_status = psa_destroy_key(key_id); |
| 1266 | if (ret != 0) { |
| 1267 | return ret; |
| 1268 | } else if (destruction_status != PSA_SUCCESS) { |
| 1269 | return PSA_PK_TO_MBEDTLS_ERR(destruction_status); |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 1272 | ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp, |
| 1273 | &mbedtls_pk_ec_rw(*pub)->Q, |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1274 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 1275 | &pub_key_len, pub_key_buf, |
| 1276 | sizeof(pub_key_buf)); |
| 1277 | if (ret != 0) { |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) { |
| 1282 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1283 | } |
| 1284 | |
| 1285 | return 0; |
| 1286 | } |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1287 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1288 | |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1289 | static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv, |
| 1290 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1291 | void *p_rng) |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1292 | { |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1293 | (void) f_rng; |
| 1294 | (void) p_rng; |
Valerio Setti | 9d65f0e | 2023-04-07 08:53:17 +0200 | [diff] [blame] | 1295 | return eckey_check_pair_psa(pub, prv); |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1296 | } |
| 1297 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1298 | static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv, |
| 1299 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1300 | void *p_rng) |
| 1301 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1302 | return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx, |
| 1303 | (const mbedtls_ecp_keypair *) prv->pk_ctx, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1304 | f_rng, p_rng); |
| 1305 | } |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1306 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1307 | |
Valerio Setti | 88a3aee | 2023-06-29 15:01:10 +0200 | [diff] [blame] | 1308 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1309 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 1310 | /* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up |
| 1311 | * using the same function. */ |
| 1312 | #define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap |
| 1313 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 1314 | static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub, |
| 1315 | mbedtls_pk_context *prv, |
| 1316 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1317 | void *p_rng) |
| 1318 | { |
| 1319 | psa_status_t status; |
| 1320 | uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; |
| 1321 | size_t exp_pub_key_len = 0; |
| 1322 | uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; |
| 1323 | size_t pub_key_len = 0; |
| 1324 | int ret; |
| 1325 | (void) f_rng; |
| 1326 | (void) p_rng; |
| 1327 | |
| 1328 | status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key), |
| 1329 | &exp_pub_key_len); |
| 1330 | if (status != PSA_SUCCESS) { |
| 1331 | ret = psa_pk_status_to_mbedtls(status); |
| 1332 | return ret; |
| 1333 | } |
| 1334 | ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp), |
| 1335 | &(mbedtls_pk_ec_ro(*pub)->Q), |
| 1336 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 1337 | &pub_key_len, pub_key, sizeof(pub_key)); |
| 1338 | if (ret != 0) { |
| 1339 | return ret; |
| 1340 | } |
| 1341 | if ((exp_pub_key_len != pub_key_len) || |
| 1342 | memcmp(exp_pub_key, pub_key, exp_pub_key_len)) { |
| 1343 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1344 | } |
| 1345 | return 0; |
| 1346 | } |
| 1347 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 1348 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1349 | |
Valerio Setti | b536126 | 2023-05-18 18:51:58 +0200 | [diff] [blame] | 1350 | #if !defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1351 | static void *eckey_alloc_wrap(void) |
| 1352 | { |
| 1353 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
| 1354 | |
| 1355 | if (ctx != NULL) { |
| 1356 | mbedtls_ecp_keypair_init(ctx); |
| 1357 | } |
| 1358 | |
| 1359 | return ctx; |
| 1360 | } |
| 1361 | |
| 1362 | static void eckey_free_wrap(void *ctx) |
| 1363 | { |
| 1364 | mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx); |
| 1365 | mbedtls_free(ctx); |
| 1366 | } |
Valerio Setti | b536126 | 2023-05-18 18:51:58 +0200 | [diff] [blame] | 1367 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1368 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1369 | static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items) |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1370 | { |
Valerio Setti | a1b8af6 | 2023-05-17 15:34:57 +0200 | [diff] [blame] | 1371 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 1372 | items->type = MBEDTLS_PK_DEBUG_PSA_EC; |
| 1373 | items->name = "eckey.Q"; |
| 1374 | items->value = pk; |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 1375 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1376 | mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx; |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1377 | items->type = MBEDTLS_PK_DEBUG_ECP; |
| 1378 | items->name = "eckey.Q"; |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1379 | items->value = &(ecp->Q); |
Valerio Setti | 5c26b30 | 2023-06-21 19:47:01 +0200 | [diff] [blame] | 1380 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | const mbedtls_pk_info_t mbedtls_eckey_info = { |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1384 | .type = MBEDTLS_PK_ECKEY, |
| 1385 | .name = "EC", |
| 1386 | .get_bitlen = eckey_get_bitlen, |
| 1387 | .can_do = eckey_can_do, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1388 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1389 | .verify_func = ecdsa_verify_wrap, /* Compatible key structures */ |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1390 | #else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
| 1391 | .verify_func = NULL, |
| 1392 | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1393 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1394 | .sign_func = ecdsa_sign_wrap, /* Compatible key structures */ |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1395 | #else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
| 1396 | .sign_func = NULL, |
| 1397 | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1398 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1399 | .verify_rs_func = eckey_verify_rs_wrap, |
| 1400 | .sign_rs_func = eckey_sign_rs_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1401 | .rs_alloc_func = eckey_rs_alloc, |
| 1402 | .rs_free_func = eckey_rs_free, |
| 1403 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 1404 | .decrypt_func = NULL, |
| 1405 | .encrypt_func = NULL, |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1406 | .check_pair_func = eckey_check_pair_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1407 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 1408 | .ctx_alloc_func = NULL, |
| 1409 | .ctx_free_func = NULL, |
| 1410 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1411 | .ctx_alloc_func = eckey_alloc_wrap, |
| 1412 | .ctx_free_func = eckey_free_wrap, |
Valerio Setti | b536126 | 2023-05-18 18:51:58 +0200 | [diff] [blame] | 1413 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1414 | .debug_func = eckey_debug, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1415 | }; |
| 1416 | |
| 1417 | /* |
| 1418 | * EC key restricted to ECDH |
| 1419 | */ |
| 1420 | static int eckeydh_can_do(mbedtls_pk_type_t type) |
| 1421 | { |
| 1422 | return type == MBEDTLS_PK_ECKEY || |
| 1423 | type == MBEDTLS_PK_ECKEY_DH; |
| 1424 | } |
| 1425 | |
| 1426 | const mbedtls_pk_info_t mbedtls_eckeydh_info = { |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1427 | .type = MBEDTLS_PK_ECKEY_DH, |
| 1428 | .name = "EC_DH", |
| 1429 | .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */ |
| 1430 | .can_do = eckeydh_can_do, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1431 | .verify_func = NULL, |
| 1432 | .sign_func = NULL, |
| 1433 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1434 | .verify_rs_func = NULL, |
| 1435 | .sign_rs_func = NULL, |
| 1436 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 1437 | .decrypt_func = NULL, |
| 1438 | .encrypt_func = NULL, |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1439 | .check_pair_func = eckey_check_pair_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1440 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 1441 | .ctx_alloc_func = NULL, |
| 1442 | .ctx_free_func = NULL, |
| 1443 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1444 | .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */ |
| 1445 | .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */ |
Valerio Setti | b536126 | 2023-05-18 18:51:58 +0200 | [diff] [blame] | 1446 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1447 | .debug_func = eckey_debug, /* Same underlying key structure */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1448 | }; |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1449 | |
| 1450 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
| 1451 | static int ecdsa_can_do(mbedtls_pk_type_t type) |
| 1452 | { |
| 1453 | return type == MBEDTLS_PK_ECDSA; |
| 1454 | } |
| 1455 | |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1456 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1457 | static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | const unsigned char *hash, size_t hash_len, |
| 1459 | const unsigned char *sig, size_t sig_len, |
| 1460 | void *rs_ctx) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1461 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1462 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1463 | ((void) md_alg); |
| 1464 | |
| 1465 | ret = mbedtls_ecdsa_read_signature_restartable( |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1466 | (mbedtls_ecdsa_context *) pk->pk_ctx, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1467 | hash, hash_len, sig, sig_len, |
| 1468 | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
| 1471 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 1472 | } |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1474 | return ret; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1475 | } |
| 1476 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1477 | static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1478 | const unsigned char *hash, size_t hash_len, |
| 1479 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1480 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1481 | void *rs_ctx) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1482 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1483 | return mbedtls_ecdsa_write_signature_restartable( |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1484 | (mbedtls_ecdsa_context *) pk->pk_ctx, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1485 | md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng, |
| 1486 | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1487 | |
| 1488 | } |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1490 | static void *ecdsa_rs_alloc(void) |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1491 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1492 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx)); |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1494 | if (ctx != NULL) { |
| 1495 | mbedtls_ecdsa_restart_init(ctx); |
| 1496 | } |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1498 | return ctx; |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1499 | } |
| 1500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1501 | static void ecdsa_rs_free(void *ctx) |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1502 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1503 | mbedtls_ecdsa_restart_free(ctx); |
| 1504 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1505 | } |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1506 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1507 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1508 | const mbedtls_pk_info_t mbedtls_ecdsa_info = { |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1509 | .type = MBEDTLS_PK_ECDSA, |
| 1510 | .name = "ECDSA", |
| 1511 | .get_bitlen = eckey_get_bitlen, /* Compatible key structures */ |
| 1512 | .can_do = ecdsa_can_do, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1513 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1514 | .verify_func = ecdsa_verify_wrap, /* Compatible key structures */ |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1515 | #else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
| 1516 | .verify_func = NULL, |
| 1517 | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1518 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1519 | .sign_func = ecdsa_sign_wrap, /* Compatible key structures */ |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1520 | #else /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
| 1521 | .sign_func = NULL, |
| 1522 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1523 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1524 | .verify_rs_func = ecdsa_verify_rs_wrap, |
| 1525 | .sign_rs_func = ecdsa_sign_rs_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1526 | .rs_alloc_func = ecdsa_rs_alloc, |
| 1527 | .rs_free_func = ecdsa_rs_free, |
| 1528 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 1529 | .decrypt_func = NULL, |
| 1530 | .encrypt_func = NULL, |
Valerio Setti | bb7603a | 2023-06-21 18:34:54 +0200 | [diff] [blame] | 1531 | .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */ |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1532 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 1533 | .ctx_alloc_func = NULL, |
| 1534 | .ctx_free_func = NULL, |
| 1535 | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1536 | .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */ |
| 1537 | .ctx_free_func = eckey_free_wrap, /* Compatible key structures */ |
Valerio Setti | b536126 | 2023-05-18 18:51:58 +0200 | [diff] [blame] | 1538 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1539 | .debug_func = eckey_debug, /* Compatible key structures */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 1540 | }; |
Valerio Setti | 7ca1318 | 2023-01-27 13:22:42 +0100 | [diff] [blame] | 1541 | #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */ |
Valerio Setti | d9d74c2 | 2023-06-29 15:00:02 +0200 | [diff] [blame] | 1542 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1543 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1544 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1545 | /* |
| 1546 | * Support for alternative RSA-private implementations |
| 1547 | */ |
| 1548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1549 | static int rsa_alt_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1550 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1551 | return type == MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1552 | } |
| 1553 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1554 | static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1555 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1556 | const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1558 | return 8 * rsa_alt->key_len_func(rsa_alt->key); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1561 | static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | const unsigned char *hash, size_t hash_len, |
| 1563 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1564 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1565 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1566 | mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1568 | if (UINT_MAX < hash_len) { |
| 1569 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1570 | } |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 1571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1572 | *sig_len = rsa_alt->key_len_func(rsa_alt->key); |
| 1573 | if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) { |
| 1574 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1575 | } |
| 1576 | if (*sig_len > sig_size) { |
| 1577 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 1578 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng, |
| 1581 | md_alg, (unsigned int) hash_len, hash, sig); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1584 | static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1585 | const unsigned char *input, size_t ilen, |
| 1586 | unsigned char *output, size_t *olen, size_t osize, |
| 1587 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1588 | { |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1589 | mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1590 | |
| 1591 | ((void) f_rng); |
| 1592 | ((void) p_rng); |
| 1593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | if (ilen != rsa_alt->key_len_func(rsa_alt->key)) { |
| 1595 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1596 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1598 | return rsa_alt->decrypt_func(rsa_alt->key, |
| 1599 | olen, input, output, osize); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1602 | #if defined(MBEDTLS_RSA_C) |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1603 | static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1604 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1605 | void *p_rng) |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1606 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1607 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1608 | unsigned char hash[32]; |
| 1609 | size_t sig_len = 0; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1610 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1612 | if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) { |
| 1613 | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1614 | } |
| 1615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1616 | memset(hash, 0x2a, sizeof(hash)); |
| 1617 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1618 | if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1619 | hash, sizeof(hash), |
| 1620 | sig, sizeof(sig), &sig_len, |
| 1621 | f_rng, p_rng)) != 0) { |
| 1622 | return ret; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
valerio | 38992cb | 2023-04-20 09:56:30 +0200 | [diff] [blame] | 1625 | if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1626 | hash, sizeof(hash), sig, sig_len) != 0) { |
| 1627 | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 1628 | } |
| 1629 | |
| 1630 | return 0; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1631 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1632 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1634 | static void *rsa_alt_alloc_wrap(void) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1635 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1636 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context)); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | if (ctx != NULL) { |
| 1639 | memset(ctx, 0, sizeof(mbedtls_rsa_alt_context)); |
| 1640 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | return ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1645 | static void rsa_alt_free_wrap(void *ctx) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1646 | { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 1647 | mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context)); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1648 | } |
| 1649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1650 | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1651 | .type = MBEDTLS_PK_RSA_ALT, |
| 1652 | .name = "RSA-alt", |
| 1653 | .get_bitlen = rsa_alt_get_bitlen, |
| 1654 | .can_do = rsa_alt_can_do, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1655 | .verify_func = NULL, |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1656 | .sign_func = rsa_alt_sign_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1657 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1658 | .verify_rs_func = NULL, |
| 1659 | .sign_rs_func = NULL, |
| 1660 | .rs_alloc_func = NULL, |
| 1661 | .rs_free_func = NULL, |
| 1662 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1663 | .decrypt_func = rsa_alt_decrypt_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1664 | .encrypt_func = NULL, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1665 | #if defined(MBEDTLS_RSA_C) |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1666 | .check_pair_func = rsa_alt_check_pair, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1667 | #else |
| 1668 | .check_pair_func = NULL, |
Manuel Pégourié-Gonnard | 7c13d69 | 2014-11-12 00:01:34 +0100 | [diff] [blame] | 1669 | #endif |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1670 | .ctx_alloc_func = rsa_alt_alloc_wrap, |
| 1671 | .ctx_free_func = rsa_alt_free_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1672 | .debug_func = NULL, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1673 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1674 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 1675 | |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1676 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1677 | static size_t opaque_get_bitlen(mbedtls_pk_context *pk) |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1678 | { |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1679 | size_t bits; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 1680 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1681 | |
Valerio Setti | 4f387ef | 2023-05-02 14:15:59 +0200 | [diff] [blame] | 1682 | if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1683 | return 0; |
| 1684 | } |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1685 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1686 | bits = psa_get_key_bits(&attributes); |
| 1687 | psa_reset_key_attributes(&attributes); |
| 1688 | return bits; |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1689 | } |
| 1690 | |
Valerio Setti | 38913c1 | 2023-06-30 16:18:33 +0200 | [diff] [blame] | 1691 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1692 | static int ecdsa_opaque_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 1693 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1694 | return type == MBEDTLS_PK_ECKEY || |
| 1695 | type == MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 1696 | } |
| 1697 | |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1698 | const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = { |
Valerio Setti | 574a00b | 2023-06-21 19:47:37 +0200 | [diff] [blame] | 1699 | .type = MBEDTLS_PK_OPAQUE, |
| 1700 | .name = "Opaque", |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1701 | .get_bitlen = opaque_get_bitlen, |
| 1702 | .can_do = ecdsa_opaque_can_do, |
Valerio Setti | 4d1daf8 | 2023-06-26 13:31:18 +0200 | [diff] [blame] | 1703 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1704 | .verify_func = ecdsa_opaque_verify_wrap, |
Valerio Setti | 4d1daf8 | 2023-06-26 13:31:18 +0200 | [diff] [blame] | 1705 | #else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
| 1706 | .verify_func = NULL, |
| 1707 | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
| 1708 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1709 | .sign_func = ecdsa_opaque_sign_wrap, |
Valerio Setti | 4d1daf8 | 2023-06-26 13:31:18 +0200 | [diff] [blame] | 1710 | #else /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
| 1711 | .sign_func = NULL, |
| 1712 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1713 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1714 | .verify_rs_func = NULL, |
| 1715 | .sign_rs_func = NULL, |
| 1716 | .rs_alloc_func = NULL, |
| 1717 | .rs_free_func = NULL, |
| 1718 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 1719 | .decrypt_func = NULL, |
| 1720 | .encrypt_func = NULL, |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1721 | .check_pair_func = ecdsa_opaque_check_pair_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1722 | .ctx_alloc_func = NULL, |
| 1723 | .ctx_free_func = NULL, |
| 1724 | .debug_func = NULL, |
Valerio Setti | 574a00b | 2023-06-21 19:47:37 +0200 | [diff] [blame] | 1725 | }; |
Valerio Setti | 38913c1 | 2023-06-30 16:18:33 +0200 | [diff] [blame] | 1726 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Valerio Setti | 574a00b | 2023-06-21 19:47:37 +0200 | [diff] [blame] | 1727 | |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1728 | static int rsa_opaque_can_do(mbedtls_pk_type_t type) |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1729 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1730 | return type == MBEDTLS_PK_RSA || |
| 1731 | type == MBEDTLS_PK_RSASSA_PSS; |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1732 | } |
| 1733 | |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 1734 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1735 | static int rsa_opaque_decrypt(mbedtls_pk_context *pk, |
| 1736 | const unsigned char *input, size_t ilen, |
| 1737 | unsigned char *output, size_t *olen, size_t osize, |
| 1738 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1739 | { |
Valerio Setti | 574a00b | 2023-06-21 19:47:37 +0200 | [diff] [blame] | 1740 | psa_status_t status; |
| 1741 | |
| 1742 | /* PSA has its own RNG */ |
| 1743 | (void) f_rng; |
| 1744 | (void) p_rng; |
| 1745 | |
| 1746 | status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT, |
| 1747 | input, ilen, |
| 1748 | NULL, 0, |
| 1749 | output, osize, olen); |
| 1750 | if (status != PSA_SUCCESS) { |
| 1751 | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
| 1752 | } |
| 1753 | |
| 1754 | return 0; |
| 1755 | } |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 1756 | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */ |
Valerio Setti | 574a00b | 2023-06-21 19:47:37 +0200 | [diff] [blame] | 1757 | |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1758 | static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
| 1759 | const unsigned char *hash, size_t hash_len, |
| 1760 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1761 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Valerio Setti | 574a00b | 2023-06-21 19:47:37 +0200 | [diff] [blame] | 1762 | { |
| 1763 | #if defined(MBEDTLS_RSA_C) |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1764 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1765 | psa_algorithm_t alg; |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1766 | psa_key_type_t type; |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1767 | psa_status_t status; |
| 1768 | |
| 1769 | /* PSA has its own RNG */ |
| 1770 | (void) f_rng; |
| 1771 | (void) p_rng; |
| 1772 | |
Valerio Setti | 4f387ef | 2023-05-02 14:15:59 +0200 | [diff] [blame] | 1773 | status = psa_get_key_attributes(pk->priv_id, &attributes); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1774 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1775 | return PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1776 | } |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1778 | type = psa_get_key_type(&attributes); |
| 1779 | psa_reset_key_attributes(&attributes); |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1781 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 1782 | alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg)); |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1783 | } else { |
| 1784 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 1785 | } |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1786 | |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1787 | /* make the signature */ |
Valerio Setti | 4f387ef | 2023-05-02 14:15:59 +0200 | [diff] [blame] | 1788 | status = psa_sign_hash(pk->priv_id, alg, hash, hash_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1789 | sig, sig_size, sig_len); |
| 1790 | if (status != PSA_SUCCESS) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1792 | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1793 | } else { |
| 1794 | return PSA_PK_TO_MBEDTLS_ERR(status); |
| 1795 | } |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1796 | } |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1797 | |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1798 | return 0; |
Valerio Setti | 574a00b | 2023-06-21 19:47:37 +0200 | [diff] [blame] | 1799 | #else /* !MBEDTLS_RSA_C */ |
| 1800 | ((void) pk); |
| 1801 | ((void) md_alg); |
| 1802 | ((void) hash); |
| 1803 | ((void) hash_len); |
| 1804 | ((void) sig); |
| 1805 | ((void) sig_size); |
| 1806 | ((void) sig_len); |
| 1807 | ((void) f_rng); |
| 1808 | ((void) p_rng); |
| 1809 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Valerio Setti | 4657f10 | 2023-06-21 13:55:16 +0200 | [diff] [blame] | 1810 | #endif /* !MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1811 | } |
| 1812 | |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1813 | const mbedtls_pk_info_t mbedtls_rsa_opaque_info = { |
Valerio Setti | f69514a | 2023-06-21 18:16:49 +0200 | [diff] [blame] | 1814 | .type = MBEDTLS_PK_OPAQUE, |
| 1815 | .name = "Opaque", |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1816 | .get_bitlen = opaque_get_bitlen, |
| 1817 | .can_do = rsa_opaque_can_do, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1818 | .verify_func = NULL, |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1819 | .sign_func = rsa_opaque_sign_wrap, |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1820 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1821 | .verify_rs_func = NULL, |
| 1822 | .sign_rs_func = NULL, |
| 1823 | .rs_alloc_func = NULL, |
| 1824 | .rs_free_func = NULL, |
| 1825 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 1826 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) |
Valerio Setti | 76d0f96 | 2023-06-23 13:32:54 +0200 | [diff] [blame] | 1827 | .decrypt_func = rsa_opaque_decrypt, |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 1828 | #else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */ |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1829 | .decrypt_func = NULL, |
Valerio Setti | f6d4dfb | 2023-07-10 10:55:12 +0200 | [diff] [blame] | 1830 | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */ |
Valerio Setti | 97976e3 | 2023-06-23 14:08:26 +0200 | [diff] [blame] | 1831 | .encrypt_func = NULL, |
| 1832 | .check_pair_func = NULL, |
| 1833 | .ctx_alloc_func = NULL, |
| 1834 | .ctx_free_func = NULL, |
| 1835 | .debug_func = NULL, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1836 | }; |
| 1837 | |
| 1838 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1840 | #endif /* MBEDTLS_PK_C */ |