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