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