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