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