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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | static size_t rsa_get_bitlen(const void *ctx) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 195 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx; |
| 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) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 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 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) 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; |
Neil Armstrong | 52f41f8 | 2022-02-22 15:30:24 +0100 | [diff] [blame] | 228 | key.pk_ctx = ctx; |
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 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 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; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
| 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) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 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), |
| 373 | ctx, hash, hash_len, |
| 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 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 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 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) 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) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | static int rsa_decrypt_wrap(void *ctx, |
| 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 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) 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; |
Neil Armstrong | 18f43c7 | 2022-02-09 15:32:45 +0100 | [diff] [blame] | 430 | key.pk_ctx = ctx; |
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 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 469 | static int rsa_decrypt_wrap(void *ctx, |
| 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 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) 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) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | static int rsa_encrypt_wrap(void *ctx, |
| 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 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) 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; |
Neil Armstrong | 96a16a4 | 2022-02-10 10:40:11 +0100 | [diff] [blame] | 516 | key.pk_ctx = ctx; |
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 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | static int rsa_encrypt_wrap(void *ctx, |
| 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 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
| 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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | static int rsa_check_pair_wrap(const void *pub, const void *prv, |
| 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; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub, |
| 578 | (const mbedtls_rsa_context *) prv); |
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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | static void rsa_debug(const void *ctx, 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 */ |
| 602 | (void) ctx; |
| 603 | (void) items; |
| 604 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | items->type = MBEDTLS_PK_DEBUG_MPI; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 606 | items->name = "rsa.N"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | items->value = &(((mbedtls_rsa_context *) ctx)->N); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 608 | |
| 609 | items++; |
| 610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 611 | items->type = MBEDTLS_PK_DEBUG_MPI; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 612 | items->name = "rsa.E"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | items->value = &(((mbedtls_rsa_context *) ctx)->E); |
Gilles Peskine | 85b1bc6 | 2021-05-25 09:20:26 +0200 | [diff] [blame] | 614 | #endif |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | const mbedtls_pk_info_t mbedtls_rsa_info = { |
| 618 | MBEDTLS_PK_RSA, |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 619 | "RSA", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 620 | rsa_get_bitlen, |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 621 | rsa_can_do, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 622 | rsa_verify_wrap, |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 623 | rsa_sign_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 624 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 625 | NULL, |
| 626 | NULL, |
| 627 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 628 | rsa_decrypt_wrap, |
| 629 | rsa_encrypt_wrap, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 630 | rsa_check_pair_wrap, |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 631 | rsa_alloc_wrap, |
| 632 | rsa_free_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 633 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 634 | NULL, |
| 635 | NULL, |
| 636 | #endif |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 637 | rsa_debug, |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 638 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 639 | #endif /* MBEDTLS_RSA_C */ |
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 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 835eb59 | 2013-08-12 18:51:26 +0200 | [diff] [blame] | 642 | /* |
| 643 | * Generic EC key |
| 644 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | static int eckey_can_do(mbedtls_pk_type_t type) |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 646 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | return type == MBEDTLS_PK_ECKEY || |
| 648 | type == MBEDTLS_PK_ECKEY_DH || |
| 649 | type == MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | static size_t eckey_get_bitlen(const void *ctx) |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 653 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | return ((mbedtls_ecp_keypair *) ctx)->grp.pbits; |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 657 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 658 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 659 | /* |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 660 | * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of |
| 661 | * 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] | 662 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 663 | static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end, |
| 664 | unsigned char *to, size_t to_len) |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 665 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 666 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 667 | size_t unpadded_len, padding_len; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 668 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len, |
| 670 | MBEDTLS_ASN1_INTEGER)) != 0) { |
| 671 | return ret; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 672 | } |
| 673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | while (unpadded_len > 0 && **from == 0x00) { |
| 675 | (*from)++; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 676 | unpadded_len--; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 677 | } |
| 678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | if (unpadded_len > to_len || unpadded_len == 0) { |
| 680 | return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 681 | } |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 682 | |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 683 | padding_len = to_len - unpadded_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 684 | memset(to, 0x00, padding_len); |
| 685 | memcpy(to + padding_len, *from, unpadded_len); |
| 686 | (*from) += unpadded_len; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 687 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 688 | return 0; |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 692 | * Convert a signature from an ASN.1 sequence of two integers |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 693 | * 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] | 694 | * twice as big as int_size. |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 695 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end, |
| 697 | unsigned char *sig, size_t int_size) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 698 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 699 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 700 | size_t tmp_size; |
Andrzej Kurek | 3f864c2 | 2018-11-07 09:30:50 -0500 | [diff] [blame] | 701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 702 | if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size, |
| 703 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 704 | return ret; |
| 705 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 706 | |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 707 | /* Extract r */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) { |
| 709 | return ret; |
| 710 | } |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 711 | /* Extract s */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 712 | if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) { |
| 713 | return ret; |
| 714 | } |
Andrzej Kurek | 3f864c2 | 2018-11-07 09:30:50 -0500 | [diff] [blame] | 715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 716 | return 0; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 717 | } |
| 718 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg, |
| 720 | const unsigned char *hash, size_t hash_len, |
| 721 | const unsigned char *sig, size_t sig_len) |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 722 | { |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 723 | mbedtls_ecp_keypair *ctx = ctx_arg; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 724 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 725 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 726 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 727 | psa_status_t status; |
Gilles Peskine | b4a87b0 | 2022-10-04 22:54:26 +0200 | [diff] [blame] | 728 | size_t key_len; |
Valerio Setti | 5c032b5 | 2023-02-02 15:10:32 +0100 | [diff] [blame] | 729 | /* This buffer will initially contain the public key and then the signature |
| 730 | * but at different points in time. For all curves except secp224k1, which |
| 731 | * is not currently supported in PSA, the public key is one byte longer |
| 732 | * (header byte + 2 numbers, while the signature is only 2 numbers), |
| 733 | * so use that as the buffer size. */ |
Valerio Setti | 1337a4f | 2023-01-30 15:54:55 +0100 | [diff] [blame] | 734 | unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
Hanno Becker | a985111 | 2019-01-25 16:37:10 +0000 | [diff] [blame] | 735 | unsigned char *p; |
John Durkop | 2ec2eaa | 2020-08-24 18:29:15 -0700 | [diff] [blame] | 736 | psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY; |
Gilles Peskine | fc2459d | 2019-12-12 17:50:44 +0100 | [diff] [blame] | 737 | size_t curve_bits; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 738 | psa_ecc_family_t curve = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
| 740 | const size_t signature_part_size = (ctx->grp.nbits + 7) / 8; |
John Durkop | 2ec2eaa | 2020-08-24 18:29:15 -0700 | [diff] [blame] | 741 | ((void) md_alg); |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 743 | if (curve == 0) { |
| 744 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 745 | } |
Andrzej Kurek | b3d1b12 | 2018-11-07 08:18:52 -0500 | [diff] [blame] | 746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve)); |
| 748 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 749 | psa_set_key_algorithm(&attributes, psa_sig_md); |
Andrzej Kurek | 2349c4d | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 750 | |
Gilles Peskine | b4a87b0 | 2022-10-04 22:54:26 +0200 | [diff] [blame] | 751 | ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q, |
| 752 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 753 | &key_len, buf, sizeof(buf)); |
| 754 | if (ret != 0) { |
| 755 | goto cleanup; |
Andrzej Kurek | 9241d18 | 2018-11-20 05:04:35 -0500 | [diff] [blame] | 756 | } |
| 757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | status = psa_import_key(&attributes, |
Gilles Peskine | b4a87b0 | 2022-10-04 22:54:26 +0200 | [diff] [blame] | 759 | buf, key_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 760 | &key_id); |
| 761 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 762 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 763 | goto cleanup; |
| 764 | } |
| 765 | |
Andrzej Kurek | eeac03b | 2018-11-20 06:39:06 -0500 | [diff] [blame] | 766 | /* We don't need the exported key anymore and can |
| 767 | * reuse its buffer for signature extraction. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | if (2 * signature_part_size > sizeof(buf)) { |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 769 | ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 770 | goto cleanup; |
| 771 | } |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 773 | p = (unsigned char *) sig; |
| 774 | if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf, |
| 775 | signature_part_size)) != 0) { |
Andrzej Kurek | b6016c5 | 2018-11-19 17:41:58 -0500 | [diff] [blame] | 776 | goto cleanup; |
| 777 | } |
| 778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 779 | status = psa_verify_hash(key_id, psa_sig_md, |
| 780 | hash, hash_len, |
| 781 | buf, 2 * signature_part_size); |
| 782 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 783 | ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 784 | goto cleanup; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 785 | } |
Andrzej Kurek | ad5d581 | 2018-11-20 07:59:18 -0500 | [diff] [blame] | 786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | if (p != sig + sig_len) { |
Andrzej Kurek | ad5d581 | 2018-11-20 07:59:18 -0500 | [diff] [blame] | 788 | ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 789 | goto cleanup; |
| 790 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 791 | ret = 0; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 792 | |
Andrzej Kurek | b7b0478 | 2018-11-19 17:01:16 -0500 | [diff] [blame] | 793 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | status = psa_destroy_key(key_id); |
| 795 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 796 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | } |
Neil Armstrong | 9dccd86 | 2022-02-24 15:33:13 +0100 | [diff] [blame] | 798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 799 | return ret; |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 800 | } |
| 801 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 802 | static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 803 | const unsigned char *hash, size_t hash_len, |
| 804 | const unsigned char *sig, size_t sig_len) |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 805 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 806 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 807 | ((void) md_alg); |
| 808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx, |
| 810 | hash, hash_len, sig, sig_len); |
Manuel Pégourié-Gonnard | 2abed84 | 2014-04-08 12:40:15 +0200 | [diff] [blame] | 811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 812 | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
| 813 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 814 | } |
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 | return ret; |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 817 | } |
Andrzej Kurek | 8b036a6 | 2018-10-31 05:16:46 -0400 | [diff] [blame] | 818 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 819 | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 820 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 821 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 822 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 823 | /* |
| 824 | * Simultaneously convert and move raw MPI from the beginning of a buffer |
| 825 | * to an ASN.1 MPI at the end of the buffer. |
| 826 | * See also mbedtls_asn1_write_mpi(). |
| 827 | * |
| 828 | * p: pointer to the end of the output buffer |
| 829 | * start: start of the output buffer, and also of the mpi to write at the end |
| 830 | * n_len: length of the mpi to read from start |
| 831 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 832 | static int asn1_write_mpibuf(unsigned char **p, unsigned char *start, |
| 833 | size_t n_len) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 834 | { |
| 835 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 836 | size_t len = 0; |
| 837 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | if ((size_t) (*p - start) < n_len) { |
| 839 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 840 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 841 | |
| 842 | len = n_len; |
| 843 | *p -= len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 844 | memmove(*p, start, len); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 845 | |
| 846 | /* ASN.1 DER encoding requires minimal length, so skip leading 0s. |
| 847 | * Neither r nor s should be 0, but as a failsafe measure, still detect |
| 848 | * that rather than overflowing the buffer in case of a PSA error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 849 | while (len > 0 && **p == 0x00) { |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 850 | ++(*p); |
| 851 | --len; |
| 852 | } |
| 853 | |
| 854 | /* this is only reached if the signature was invalid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | if (len == 0) { |
| 856 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 857 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 858 | |
| 859 | /* if the msb is 1, ASN.1 requires that we prepend a 0. |
| 860 | * 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] | 861 | if (**p & 0x80) { |
| 862 | if (*p - start < 1) { |
| 863 | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
| 864 | } |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 865 | |
| 866 | *--(*p) = 0x00; |
| 867 | len += 1; |
| 868 | } |
| 869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 871 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 872 | MBEDTLS_ASN1_INTEGER)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 874 | return (int) len; |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* Transcode signature from PSA format to ASN.1 sequence. |
| 878 | * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of |
| 879 | * MPIs, and in-place. |
| 880 | * |
| 881 | * [in/out] sig: the signature pre- and post-transcoding |
| 882 | * [in/out] sig_len: signature length pre- and post-transcoding |
| 883 | * [int] buf_len: the available size the in/out buffer |
| 884 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len, |
| 886 | size_t buf_len) |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 887 | { |
| 888 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 889 | size_t len = 0; |
| 890 | const size_t rs_len = *sig_len / 2; |
| 891 | unsigned char *p = sig + buf_len; |
| 892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 893 | MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len)); |
| 894 | MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 895 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 896 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len)); |
| 897 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig, |
| 898 | MBEDTLS_ASN1_CONSTRUCTED | |
| 899 | MBEDTLS_ASN1_SEQUENCE)); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | memmove(sig, p, len); |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 902 | *sig_len = len; |
| 903 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 904 | return 0; |
Neil Armstrong | 1502165 | 2022-03-01 10:14:17 +0100 | [diff] [blame] | 905 | } |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 907 | static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg, |
| 908 | const unsigned char *hash, size_t hash_len, |
| 909 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 910 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 911 | { |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 912 | mbedtls_ecp_keypair *ctx = ctx_arg; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 913 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 914 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 915 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 916 | psa_status_t status; |
Valerio Setti | 1337a4f | 2023-01-30 15:54:55 +0100 | [diff] [blame] | 917 | unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH]; |
Manuel Pégourié-Gonnard | 79ae7eb | 2022-12-05 12:55:51 +0100 | [diff] [blame] | 918 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Valerio Setti | 5bc5224 | 2023-01-30 15:48:28 +0100 | [diff] [blame] | 919 | psa_algorithm_t psa_sig_md = |
Valerio Setti | b761b15 | 2023-01-31 14:56:04 +0100 | [diff] [blame] | 920 | 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] | 921 | #else |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 922 | psa_algorithm_t psa_sig_md = |
Valerio Setti | b761b15 | 2023-01-31 14:56:04 +0100 | [diff] [blame] | 923 | 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] | 924 | #endif |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 925 | size_t curve_bits; |
| 926 | psa_ecc_family_t curve = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 927 | mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 928 | size_t key_len = PSA_BITS_TO_BYTES(curve_bits); |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 929 | |
| 930 | /* PSA has its own RNG */ |
| 931 | ((void) f_rng); |
| 932 | ((void) p_rng); |
| 933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 934 | if (curve == 0) { |
| 935 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 936 | } |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 937 | |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 938 | if (key_len > sizeof(buf)) { |
Valerio Setti | b761b15 | 2023-01-31 14:56:04 +0100 | [diff] [blame] | 939 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 940 | } |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 941 | ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | if (ret != 0) { |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 943 | goto cleanup; |
| 944 | } |
| 945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 946 | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); |
| 947 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 948 | psa_set_key_algorithm(&attributes, psa_sig_md); |
| 949 | |
| 950 | status = psa_import_key(&attributes, |
Gilles Peskine | 13caa94 | 2022-10-04 22:59:26 +0200 | [diff] [blame] | 951 | buf, key_len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 952 | &key_id); |
| 953 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 954 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 955 | goto cleanup; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 956 | } |
| 957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 958 | status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len, |
| 959 | sig, sig_size, sig_len); |
| 960 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 961 | ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 962 | goto cleanup; |
| 963 | } |
| 964 | |
| 965 | ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size); |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 966 | |
| 967 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 968 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
| 969 | status = psa_destroy_key(key_id); |
| 970 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 971 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 972 | } |
Neil Armstrong | ff70f0b | 2022-03-03 14:31:17 +0100 | [diff] [blame] | 973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 974 | return ret; |
Neil Armstrong | e960690 | 2022-02-09 14:23:00 +0100 | [diff] [blame] | 975 | } |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 976 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 978 | const unsigned char *hash, size_t hash_len, |
| 979 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 980 | 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] | 981 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 982 | return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx, |
| 983 | md_alg, hash, hash_len, |
| 984 | sig, sig_size, sig_len, |
| 985 | f_rng, p_rng); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 986 | } |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 987 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 988 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 989 | |
Valerio Setti | 80d0798 | 2023-02-08 13:49:17 +0100 | [diff] [blame] | 990 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 991 | /* Forward declarations */ |
| 992 | static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 993 | const unsigned char *hash, size_t hash_len, |
| 994 | const unsigned char *sig, size_t sig_len, |
| 995 | void *rs_ctx); |
| 996 | |
| 997 | static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 998 | const unsigned char *hash, size_t hash_len, |
| 999 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1000 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1001 | void *rs_ctx); |
| 1002 | |
| 1003 | /* |
| 1004 | * Restart context for ECDSA operations with ECKEY context |
| 1005 | * |
| 1006 | * We need to store an actual ECDSA context, as we need to pass the same to |
| 1007 | * the underlying ecdsa function, so we can't create it on the fly every time. |
| 1008 | */ |
| 1009 | typedef struct { |
| 1010 | mbedtls_ecdsa_restart_ctx ecdsa_rs; |
| 1011 | mbedtls_ecdsa_context ecdsa_ctx; |
| 1012 | } eckey_restart_ctx; |
| 1013 | |
| 1014 | static void *eckey_rs_alloc(void) |
| 1015 | { |
| 1016 | eckey_restart_ctx *rs_ctx; |
| 1017 | |
| 1018 | void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx)); |
| 1019 | |
| 1020 | if (ctx != NULL) { |
| 1021 | rs_ctx = ctx; |
| 1022 | mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs); |
| 1023 | mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx); |
| 1024 | } |
| 1025 | |
| 1026 | return ctx; |
| 1027 | } |
| 1028 | |
| 1029 | static void eckey_rs_free(void *ctx) |
| 1030 | { |
| 1031 | eckey_restart_ctx *rs_ctx; |
| 1032 | |
| 1033 | if (ctx == NULL) { |
| 1034 | return; |
| 1035 | } |
| 1036 | |
| 1037 | rs_ctx = ctx; |
| 1038 | mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs); |
| 1039 | mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx); |
| 1040 | |
| 1041 | mbedtls_free(ctx); |
| 1042 | } |
| 1043 | |
| 1044 | static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1045 | const unsigned char *hash, size_t hash_len, |
| 1046 | const unsigned char *sig, size_t sig_len, |
| 1047 | void *rs_ctx) |
| 1048 | { |
| 1049 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1050 | eckey_restart_ctx *rs = rs_ctx; |
| 1051 | |
| 1052 | /* Should never happen */ |
| 1053 | if (rs == NULL) { |
| 1054 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1055 | } |
| 1056 | |
| 1057 | /* set up our own sub-context if needed (that is, on first run) */ |
| 1058 | if (rs->ecdsa_ctx.grp.pbits == 0) { |
| 1059 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx)); |
| 1060 | } |
| 1061 | |
| 1062 | MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx, |
| 1063 | md_alg, hash, hash_len, |
| 1064 | sig, sig_len, &rs->ecdsa_rs)); |
| 1065 | |
| 1066 | cleanup: |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1071 | const unsigned char *hash, size_t hash_len, |
| 1072 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1073 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1074 | void *rs_ctx) |
| 1075 | { |
| 1076 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1077 | eckey_restart_ctx *rs = rs_ctx; |
| 1078 | |
| 1079 | /* Should never happen */ |
| 1080 | if (rs == NULL) { |
| 1081 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1082 | } |
| 1083 | |
| 1084 | /* set up our own sub-context if needed (that is, on first run) */ |
| 1085 | if (rs->ecdsa_ctx.grp.pbits == 0) { |
| 1086 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx)); |
| 1087 | } |
| 1088 | |
| 1089 | MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg, |
| 1090 | hash, hash_len, sig, sig_size, sig_len, |
| 1091 | f_rng, p_rng, &rs->ecdsa_rs)); |
| 1092 | |
| 1093 | cleanup: |
| 1094 | return ret; |
| 1095 | } |
Valerio Setti | 80d0798 | 2023-02-08 13:49:17 +0100 | [diff] [blame] | 1096 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1097 | |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1098 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1099 | /* |
| 1100 | * Alternative function used to verify that the EC private/public key pair |
| 1101 | * is valid using PSA functions instead of ECP ones. |
| 1102 | * The flow is: |
| 1103 | * - import the private key "prv" to PSA and export its public part |
| 1104 | * - write the raw content of public key "pub" to a local buffer |
| 1105 | * - compare the two buffers |
| 1106 | */ |
Valerio Setti | 9d65f0e | 2023-04-07 08:53:17 +0200 | [diff] [blame] | 1107 | static int eckey_check_pair_psa(const mbedtls_ecp_keypair *pub, |
| 1108 | const mbedtls_ecp_keypair *prv) |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1109 | { |
Valerio Setti | 1df94f8 | 2023-04-07 08:59:24 +0200 | [diff] [blame] | 1110 | psa_status_t status, destruction_status; |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1111 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 1112 | mbedtls_ecp_keypair *prv_ctx = (mbedtls_ecp_keypair *) prv; |
| 1113 | mbedtls_ecp_keypair *pub_ctx = (mbedtls_ecp_keypair *) pub; |
| 1114 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Valerio Setti | 8eb5526 | 2023-04-04 10:20:53 +0200 | [diff] [blame] | 1115 | /* We are using MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH for the size of this |
| 1116 | * buffer because it will be used to hold the private key at first and |
| 1117 | * then its public part (but not at the same time). */ |
| 1118 | uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1119 | size_t prv_key_len; |
| 1120 | uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
| 1121 | size_t pub_key_len; |
| 1122 | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
| 1123 | size_t curve_bits; |
Valerio Setti | f286664 | 2023-04-06 16:49:54 +0200 | [diff] [blame] | 1124 | const psa_ecc_family_t curve = |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1125 | mbedtls_ecc_group_to_psa(prv_ctx->grp.id, &curve_bits); |
Valerio Setti | f286664 | 2023-04-06 16:49:54 +0200 | [diff] [blame] | 1126 | const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits); |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1127 | |
| 1128 | psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); |
| 1129 | psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT); |
| 1130 | |
| 1131 | ret = mbedtls_mpi_write_binary(&prv_ctx->d, prv_key_buf, curve_bytes); |
| 1132 | if (ret != 0) { |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | 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] | 1137 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
| 1138 | if (ret != 0) { |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1139 | return ret; |
| 1140 | } |
| 1141 | |
| 1142 | mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf)); |
| 1143 | |
Valerio Setti | 1df94f8 | 2023-04-07 08:59:24 +0200 | [diff] [blame] | 1144 | status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf), |
| 1145 | &prv_key_len); |
| 1146 | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
| 1147 | destruction_status = psa_destroy_key(key_id); |
| 1148 | if (ret != 0) { |
| 1149 | return ret; |
| 1150 | } else if (destruction_status != PSA_SUCCESS) { |
| 1151 | return PSA_PK_TO_MBEDTLS_ERR(destruction_status); |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | ret = mbedtls_ecp_point_write_binary(&pub_ctx->grp, &pub_ctx->Q, |
| 1155 | MBEDTLS_ECP_PF_UNCOMPRESSED, |
| 1156 | &pub_key_len, pub_key_buf, |
| 1157 | sizeof(pub_key_buf)); |
| 1158 | if (ret != 0) { |
| 1159 | return ret; |
| 1160 | } |
| 1161 | |
| 1162 | if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) { |
| 1163 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1164 | } |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1169 | |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1170 | static int eckey_check_pair(const void *pub, const void *prv, |
| 1171 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1172 | void *p_rng) |
| 1173 | { |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1174 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1175 | (void) f_rng; |
| 1176 | (void) p_rng; |
Valerio Setti | 9d65f0e | 2023-04-07 08:53:17 +0200 | [diff] [blame] | 1177 | return eckey_check_pair_psa(pub, prv); |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1178 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1179 | return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub, |
| 1180 | (const mbedtls_ecp_keypair *) prv, |
| 1181 | f_rng, p_rng); |
Valerio Setti | 0fe1ee2 | 2023-04-03 14:42:22 +0200 | [diff] [blame] | 1182 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1183 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static void *eckey_alloc_wrap(void) |
| 1187 | { |
| 1188 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
| 1189 | |
| 1190 | if (ctx != NULL) { |
| 1191 | mbedtls_ecp_keypair_init(ctx); |
| 1192 | } |
| 1193 | |
| 1194 | return ctx; |
| 1195 | } |
| 1196 | |
| 1197 | static void eckey_free_wrap(void *ctx) |
| 1198 | { |
| 1199 | mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx); |
| 1200 | mbedtls_free(ctx); |
| 1201 | } |
| 1202 | |
| 1203 | static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items) |
| 1204 | { |
| 1205 | items->type = MBEDTLS_PK_DEBUG_ECP; |
| 1206 | items->name = "eckey.Q"; |
| 1207 | items->value = &(((mbedtls_ecp_keypair *) ctx)->Q); |
| 1208 | } |
| 1209 | |
| 1210 | const mbedtls_pk_info_t mbedtls_eckey_info = { |
| 1211 | MBEDTLS_PK_ECKEY, |
| 1212 | "EC", |
| 1213 | eckey_get_bitlen, |
| 1214 | eckey_can_do, |
| 1215 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
| 1216 | ecdsa_verify_wrap, /* Compatible key structures */ |
| 1217 | #else |
| 1218 | NULL, |
| 1219 | #endif |
| 1220 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
| 1221 | ecdsa_sign_wrap, /* Compatible key structures */ |
| 1222 | #else |
| 1223 | NULL, |
| 1224 | #endif |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1225 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1226 | eckey_verify_rs_wrap, |
| 1227 | eckey_sign_rs_wrap, |
| 1228 | #endif |
| 1229 | NULL, |
| 1230 | NULL, |
| 1231 | eckey_check_pair, |
| 1232 | eckey_alloc_wrap, |
| 1233 | eckey_free_wrap, |
| 1234 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1235 | eckey_rs_alloc, |
| 1236 | eckey_rs_free, |
| 1237 | #endif |
| 1238 | eckey_debug, |
| 1239 | }; |
| 1240 | |
| 1241 | /* |
| 1242 | * EC key restricted to ECDH |
| 1243 | */ |
| 1244 | static int eckeydh_can_do(mbedtls_pk_type_t type) |
| 1245 | { |
| 1246 | return type == MBEDTLS_PK_ECKEY || |
| 1247 | type == MBEDTLS_PK_ECKEY_DH; |
| 1248 | } |
| 1249 | |
| 1250 | const mbedtls_pk_info_t mbedtls_eckeydh_info = { |
| 1251 | MBEDTLS_PK_ECKEY_DH, |
| 1252 | "EC_DH", |
| 1253 | eckey_get_bitlen, /* Same underlying key structure */ |
| 1254 | eckeydh_can_do, |
| 1255 | NULL, |
| 1256 | NULL, |
| 1257 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1258 | NULL, |
| 1259 | NULL, |
| 1260 | #endif |
| 1261 | NULL, |
| 1262 | NULL, |
| 1263 | eckey_check_pair, |
| 1264 | eckey_alloc_wrap, /* Same underlying key structure */ |
| 1265 | eckey_free_wrap, /* Same underlying key structure */ |
| 1266 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1267 | NULL, |
| 1268 | NULL, |
| 1269 | #endif |
| 1270 | eckey_debug, /* Same underlying key structure */ |
| 1271 | }; |
| 1272 | #endif /* MBEDTLS_ECP_C */ |
| 1273 | |
| 1274 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
| 1275 | static int ecdsa_can_do(mbedtls_pk_type_t type) |
| 1276 | { |
| 1277 | return type == MBEDTLS_PK_ECDSA; |
| 1278 | } |
| 1279 | |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1280 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1282 | const unsigned char *hash, size_t hash_len, |
| 1283 | const unsigned char *sig, size_t sig_len, |
| 1284 | void *rs_ctx) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1285 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1286 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1287 | ((void) md_alg); |
| 1288 | |
| 1289 | ret = mbedtls_ecdsa_read_signature_restartable( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1290 | (mbedtls_ecdsa_context *) ctx, |
| 1291 | hash, hash_len, sig, sig_len, |
| 1292 | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1293 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1294 | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
| 1295 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
| 1296 | } |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1298 | return ret; |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1301 | static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1302 | const unsigned char *hash, size_t hash_len, |
| 1303 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1304 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 1305 | void *rs_ctx) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1306 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1307 | return mbedtls_ecdsa_write_signature_restartable( |
| 1308 | (mbedtls_ecdsa_context *) ctx, |
| 1309 | md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng, |
| 1310 | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1311 | |
| 1312 | } |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | static void *ecdsa_rs_alloc(void) |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1315 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1316 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx)); |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1318 | if (ctx != NULL) { |
| 1319 | mbedtls_ecdsa_restart_init(ctx); |
| 1320 | } |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1322 | return ctx; |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | static void ecdsa_rs_free(void *ctx) |
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 | mbedtls_ecdsa_restart_free(ctx); |
| 1328 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1329 | } |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1330 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1331 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1332 | const mbedtls_pk_info_t mbedtls_ecdsa_info = { |
| 1333 | MBEDTLS_PK_ECDSA, |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 1334 | "ECDSA", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 1335 | eckey_get_bitlen, /* Compatible key structures */ |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 1336 | ecdsa_can_do, |
Valerio Setti | 1cdddac | 2023-02-02 13:55:57 +0100 | [diff] [blame] | 1337 | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
| 1338 | ecdsa_verify_wrap, /* Compatible key structures */ |
| 1339 | #else |
| 1340 | NULL, |
| 1341 | #endif |
| 1342 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
| 1343 | ecdsa_sign_wrap, /* Compatible key structures */ |
| 1344 | #else |
| 1345 | NULL, |
| 1346 | #endif |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1347 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1348 | ecdsa_verify_rs_wrap, |
| 1349 | ecdsa_sign_rs_wrap, |
| 1350 | #endif |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 1351 | NULL, |
| 1352 | NULL, |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 1353 | eckey_check_pair, /* Compatible key structures */ |
Valerio Setti | 24138d9 | 2023-01-27 14:24:09 +0100 | [diff] [blame] | 1354 | eckey_alloc_wrap, /* Compatible key structures */ |
| 1355 | eckey_free_wrap, /* Compatible key structures */ |
Valerio Setti | 5b16e9e | 2023-02-07 08:08:53 +0100 | [diff] [blame] | 1356 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | fe68770 | 2017-08-18 17:04:07 +0200 | [diff] [blame] | 1357 | ecdsa_rs_alloc, |
| 1358 | ecdsa_rs_free, |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1359 | #endif |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 1360 | eckey_debug, /* Compatible key structures */ |
| 1361 | }; |
Valerio Setti | 7ca1318 | 2023-01-27 13:22:42 +0100 | [diff] [blame] | 1362 | #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1364 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1365 | /* |
| 1366 | * Support for alternative RSA-private implementations |
| 1367 | */ |
| 1368 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1369 | 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] | 1370 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1371 | return type == MBEDTLS_PK_RSA; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | static size_t rsa_alt_get_bitlen(const void *ctx) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1375 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1378 | return 8 * rsa_alt->key_len_func(rsa_alt->key); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1379 | } |
| 1380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1381 | static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1382 | const unsigned char *hash, size_t hash_len, |
| 1383 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1384 | 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] | 1385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1386 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1388 | if (UINT_MAX < hash_len) { |
| 1389 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1390 | } |
Andres AG | 7284987 | 2017-01-19 11:24:33 +0000 | [diff] [blame] | 1391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | *sig_len = rsa_alt->key_len_func(rsa_alt->key); |
| 1393 | if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) { |
| 1394 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
| 1395 | } |
| 1396 | if (*sig_len > sig_size) { |
| 1397 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
| 1398 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1400 | return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng, |
| 1401 | md_alg, (unsigned int) hash_len, hash, sig); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1404 | static int rsa_alt_decrypt_wrap(void *ctx, |
| 1405 | const unsigned char *input, size_t ilen, |
| 1406 | unsigned char *output, size_t *olen, size_t osize, |
| 1407 | 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] | 1408 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1410 | |
| 1411 | ((void) f_rng); |
| 1412 | ((void) p_rng); |
| 1413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1414 | if (ilen != rsa_alt->key_len_func(rsa_alt->key)) { |
| 1415 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
| 1416 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | return rsa_alt->decrypt_func(rsa_alt->key, |
| 1419 | olen, input, output, osize); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1422 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1423 | static int rsa_alt_check_pair(const void *pub, const void *prv, |
| 1424 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1425 | void *p_rng) |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1426 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1427 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1428 | unsigned char hash[32]; |
| 1429 | size_t sig_len = 0; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1430 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1432 | if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) { |
| 1433 | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1434 | } |
| 1435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1436 | memset(hash, 0x2a, sizeof(hash)); |
| 1437 | |
| 1438 | if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE, |
| 1439 | hash, sizeof(hash), |
| 1440 | sig, sizeof(sig), &sig_len, |
| 1441 | f_rng, p_rng)) != 0) { |
| 1442 | return ret; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1445 | if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE, |
| 1446 | hash, sizeof(hash), sig, sig_len) != 0) { |
| 1447 | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
| 1448 | } |
| 1449 | |
| 1450 | return 0; |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1451 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1452 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1454 | static void *rsa_alt_alloc_wrap(void) |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1455 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1456 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context)); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | if (ctx != NULL) { |
| 1459 | memset(ctx, 0, sizeof(mbedtls_rsa_alt_context)); |
| 1460 | } |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1462 | return ctx; |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1465 | static void rsa_alt_free_wrap(void *ctx) |
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 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context)); |
| 1468 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { |
| 1472 | MBEDTLS_PK_RSA_ALT, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1473 | "RSA-alt", |
Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 1474 | rsa_alt_get_bitlen, |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 1475 | rsa_alt_can_do, |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1476 | NULL, |
| 1477 | rsa_alt_sign_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1478 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 1479 | NULL, |
| 1480 | NULL, |
| 1481 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1482 | rsa_alt_decrypt_wrap, |
| 1483 | NULL, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1484 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 1485 | rsa_alt_check_pair, |
Manuel Pégourié-Gonnard | 7c13d69 | 2014-11-12 00:01:34 +0100 | [diff] [blame] | 1486 | #else |
| 1487 | NULL, |
| 1488 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1489 | rsa_alt_alloc_wrap, |
| 1490 | rsa_alt_free_wrap, |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 1491 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 1492 | NULL, |
| 1493 | NULL, |
| 1494 | #endif |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 1495 | NULL, |
| 1496 | }; |
Manuel Pégourié-Gonnard | c40b4c3 | 2013-08-22 13:29:31 +0200 | [diff] [blame] | 1497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1498 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 1499 | |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1500 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1502 | static void *pk_opaque_alloc_wrap(void) |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1503 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1504 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t)); |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1505 | |
Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 1506 | /* no _init() function to call, as calloc() already zeroized */ |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1508 | return ctx; |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1511 | static void pk_opaque_free_wrap(void *ctx) |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1512 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1513 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t)); |
| 1514 | mbedtls_free(ctx); |
Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 1515 | } |
| 1516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1517 | static size_t pk_opaque_get_bitlen(const void *ctx) |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1518 | { |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1519 | const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx; |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1520 | size_t bits; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 1521 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1523 | if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) { |
| 1524 | return 0; |
| 1525 | } |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | bits = psa_get_key_bits(&attributes); |
| 1528 | psa_reset_key_attributes(&attributes); |
| 1529 | return bits; |
Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 1530 | } |
| 1531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1532 | 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] | 1533 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1534 | return type == MBEDTLS_PK_ECKEY || |
| 1535 | type == MBEDTLS_PK_ECDSA; |
Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 1536 | } |
| 1537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1538 | static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type) |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1539 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1540 | return type == MBEDTLS_PK_RSA || |
| 1541 | type == MBEDTLS_PK_RSASSA_PSS; |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1544 | static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
| 1545 | const unsigned char *hash, size_t hash_len, |
| 1546 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 1547 | 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] | 1548 | { |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1549 | #if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C) |
John Durkop | f35069a | 2020-08-17 22:05:14 -0700 | [diff] [blame] | 1550 | ((void) ctx); |
| 1551 | ((void) md_alg); |
| 1552 | ((void) hash); |
| 1553 | ((void) hash_len); |
| 1554 | ((void) sig); |
Gilles Peskine | f00f152 | 2021-06-22 00:09:00 +0200 | [diff] [blame] | 1555 | ((void) sig_size); |
John Durkop | f35069a | 2020-08-17 22:05:14 -0700 | [diff] [blame] | 1556 | ((void) sig_len); |
| 1557 | ((void) f_rng); |
| 1558 | ((void) p_rng); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1560 | #else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */ |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 1561 | const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx; |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1562 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1563 | psa_algorithm_t alg; |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1564 | psa_key_type_t type; |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1565 | psa_status_t status; |
| 1566 | |
| 1567 | /* PSA has its own RNG */ |
| 1568 | (void) f_rng; |
| 1569 | (void) p_rng; |
| 1570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1571 | status = psa_get_key_attributes(*key, &attributes); |
| 1572 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1573 | return PSA_PK_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | } |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1576 | type = psa_get_key_type(&attributes); |
| 1577 | psa_reset_key_attributes(&attributes); |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1578 | |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1579 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1580 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
| 1581 | alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg)); |
| 1582 | } else |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1583 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1584 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1585 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
| 1586 | alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg)); |
| 1587 | } else |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1588 | #endif /* MBEDTLS_RSA_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1590 | |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1591 | /* make the signature */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1592 | status = psa_sign_hash(*key, alg, hash, hash_len, |
| 1593 | sig, sig_size, sig_len); |
| 1594 | if (status != PSA_SUCCESS) { |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1595 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1596 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1597 | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1598 | } else |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1599 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1600 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1601 | if (PSA_KEY_TYPE_IS_RSA(type)) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1602 | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | } else |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1604 | #endif /* MBEDTLS_RSA_C */ |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1605 | return PSA_PK_TO_MBEDTLS_ERR(status); |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1606 | } |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1607 | |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1608 | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1609 | if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) { |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1610 | /* transcode it to ASN.1 sequence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1611 | return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size); |
| 1612 | } |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1613 | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
Neil Armstrong | b980c9b | 2022-03-15 16:19:16 +0100 | [diff] [blame] | 1614 | |
| 1615 | return 0; |
Valerio Setti | ab363d9 | 2023-01-26 14:31:54 +0100 | [diff] [blame] | 1616 | #endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | d8454bc | 2018-11-13 10:32:00 +0100 | [diff] [blame] | 1617 | } |
| 1618 | |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1619 | const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = { |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 1620 | MBEDTLS_PK_OPAQUE, |
| 1621 | "Opaque", |
| 1622 | pk_opaque_get_bitlen, |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1623 | pk_opaque_ecdsa_can_do, |
| 1624 | NULL, /* verify - will be done later */ |
| 1625 | pk_opaque_sign_wrap, |
| 1626 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1627 | NULL, /* restartable verify - not relevant */ |
| 1628 | NULL, /* restartable sign - not relevant */ |
| 1629 | #endif |
Neil Armstrong | 95a8923 | 2022-04-08 15:13:51 +0200 | [diff] [blame] | 1630 | NULL, /* decrypt - not relevant */ |
| 1631 | NULL, /* encrypt - not relevant */ |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1632 | NULL, /* check_pair - could be done later or left NULL */ |
| 1633 | pk_opaque_alloc_wrap, |
| 1634 | pk_opaque_free_wrap, |
| 1635 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1636 | NULL, /* restart alloc - not relevant */ |
| 1637 | NULL, /* restart free - not relevant */ |
| 1638 | #endif |
| 1639 | NULL, /* debug - could be done later, or even left NULL */ |
| 1640 | }; |
| 1641 | |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 1642 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1643 | static int pk_opaque_rsa_decrypt(void *ctx, |
| 1644 | const unsigned char *input, size_t ilen, |
| 1645 | unsigned char *output, size_t *olen, size_t osize, |
| 1646 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Neil Armstrong | 1082818 | 2022-04-22 15:02:27 +0200 | [diff] [blame] | 1647 | { |
| 1648 | const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx; |
| 1649 | psa_status_t status; |
| 1650 | |
| 1651 | /* PSA has its own RNG */ |
| 1652 | (void) f_rng; |
| 1653 | (void) p_rng; |
| 1654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1655 | status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT, |
| 1656 | input, ilen, |
| 1657 | NULL, 0, |
| 1658 | output, osize, olen); |
| 1659 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1660 | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 1082818 | 2022-04-22 15:02:27 +0200 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | return 0; |
| 1664 | } |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 1665 | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */ |
Neil Armstrong | 1082818 | 2022-04-22 15:02:27 +0200 | [diff] [blame] | 1666 | |
Neil Armstrong | eabbf9d | 2022-03-15 12:01:26 +0100 | [diff] [blame] | 1667 | const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = { |
| 1668 | MBEDTLS_PK_OPAQUE, |
| 1669 | "Opaque", |
| 1670 | pk_opaque_get_bitlen, |
| 1671 | pk_opaque_rsa_can_do, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1672 | NULL, /* verify - will be done later */ |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 1673 | pk_opaque_sign_wrap, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1674 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1675 | NULL, /* restartable verify - not relevant */ |
| 1676 | NULL, /* restartable sign - not relevant */ |
| 1677 | #endif |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 1678 | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) |
Neil Armstrong | 1082818 | 2022-04-22 15:02:27 +0200 | [diff] [blame] | 1679 | pk_opaque_rsa_decrypt, |
Neil Armstrong | 30beca3 | 2022-05-03 15:42:13 +0200 | [diff] [blame] | 1680 | #else |
| 1681 | NULL, /* decrypt - not available */ |
| 1682 | #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */ |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1683 | NULL, /* encrypt - will be done later */ |
| 1684 | NULL, /* check_pair - could be done later or left NULL */ |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 1685 | pk_opaque_alloc_wrap, |
| 1686 | pk_opaque_free_wrap, |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 1687 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 1688 | NULL, /* restart alloc - not relevant */ |
| 1689 | NULL, /* restart free - not relevant */ |
| 1690 | #endif |
| 1691 | NULL, /* debug - could be done later, or even left NULL */ |
| 1692 | }; |
| 1693 | |
| 1694 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1696 | #endif /* MBEDTLS_PK_C */ |