blob: 436876a5d526c3b7c4b8ef1ca531e4186a133b43 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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é-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Gilles Peskine8a6022e2022-10-04 23:01:59 +020022#include "mbedtls/platform_util.h"
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000025#include "pk_wrap.h"
Valerio Settia1b8af62023-05-17 15:34:57 +020026#include "pk_internal.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000027#include "mbedtls/error.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020028#include "md_psa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020029
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020030/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020039#endif
40
Gilles Peskine8a6022e2022-10-04 23:01:59 +020041#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C)
42#include "pkwrite.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010043#endif
44
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050045#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020046#include "psa_util_internal.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050047#endif
48
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010049#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040050#include "psa/crypto.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020051
Valerio Setti80d07982023-02-08 13:49:17 +010052#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020053#include "mbedtls/asn1write.h"
54#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020056#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010057
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059
Andres AG72849872017-01-19 11:24:33 +000060#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010061#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020062#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020063
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050064#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yub02ee182022-03-16 10:30:41 +080065#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010066int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010069 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010071 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010073 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010075 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010077 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010079 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010081 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010083 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010085 case PSA_ERROR_COMMUNICATION_FAILURE:
86 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010088 case PSA_ERROR_DATA_CORRUPT:
89 case PSA_ERROR_DATA_INVALID:
90 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010092 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +010094 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010096 }
97}
98
Neil Armstrong30beca32022-05-03 15:42:13 +020099#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
Valerio Settif6d4dfb2023-07-10 10:55:12 +0200100 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +0100102{
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +0100104 case PSA_ERROR_NOT_PERMITTED:
105 case PSA_ERROR_INVALID_ARGUMENT:
106 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100108 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100110 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100112 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100114 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Andrzej Kurekba241382022-12-27 09:17:33 -0500116 case PSA_SUCCESS:
117 return 0;
118 case PSA_ERROR_NOT_SUPPORTED:
119 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
120 case PSA_ERROR_INSUFFICIENT_MEMORY:
121 return MBEDTLS_ERR_PK_ALLOC_FAILED;
122 case PSA_ERROR_BAD_STATE:
123 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
124 case PSA_ERROR_COMMUNICATION_FAILURE:
125 case PSA_ERROR_HARDWARE_FAILURE:
126 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
127 case PSA_ERROR_DATA_CORRUPT:
128 case PSA_ERROR_DATA_INVALID:
129 case PSA_ERROR_STORAGE_FAILURE:
130 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
131 case PSA_ERROR_CORRUPTION_DETECTED:
132 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100133 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500134 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100135 }
136}
Valerio Settif6d4dfb2023-07-10 10:55:12 +0200137#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Jerry Yu07869e82022-03-16 16:40:50 +0800138#endif /* MBEDTLS_PSA_CRYPTO_C */
139
140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu07869e82022-03-16 16:40:50 +0800141#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800143{
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800145 case PSA_ERROR_NOT_PERMITTED:
146 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800148 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800150 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800152 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800154 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Andrzej Kurekba241382022-12-27 09:17:33 -0500156 case PSA_SUCCESS:
157 return 0;
158 case PSA_ERROR_NOT_SUPPORTED:
159 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
160 case PSA_ERROR_INSUFFICIENT_MEMORY:
161 return MBEDTLS_ERR_PK_ALLOC_FAILED;
162 case PSA_ERROR_BAD_STATE:
163 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
164 case PSA_ERROR_COMMUNICATION_FAILURE:
165 case PSA_ERROR_HARDWARE_FAILURE:
166 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
167 case PSA_ERROR_DATA_CORRUPT:
168 case PSA_ERROR_DATA_INVALID:
169 case PSA_ERROR_STORAGE_FAILURE:
170 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
171 case PSA_ERROR_CORRUPTION_DETECTED:
172 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu07869e82022-03-16 16:40:50 +0800173 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500174 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Jerry Yu07869e82022-03-16 16:40:50 +0800175 }
176}
Jerry Yu75339822022-03-23 12:06:31 +0800177#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu75339822022-03-23 12:06:31 +0800178#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500179#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Neil Armstrong19915c22022-03-01 15:21:02 +0100180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100182static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200183{
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return type == MBEDTLS_PK_RSA ||
185 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200186}
187
valerio38992cb2023-04-20 09:56:30 +0200188static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200189{
valerio38992cb2023-04-20 09:56:30 +0200190 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200192}
193
Neil Armstrong52f41f82022-02-22 15:30:24 +0100194#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200195static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 const unsigned char *hash, size_t hash_len,
197 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200198{
valerio38992cb2023-04-20 09:56:30 +0200199 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
202 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
203 psa_status_t status;
204 mbedtls_pk_context key;
205 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100206 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100207 psa_algorithm_t psa_alg_md =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200208 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100210
Dave Rodgman2eab4622023-10-05 13:30:37 +0100211#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
213 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
214 }
Dave Rodgman2eab4622023-10-05 13:30:37 +0100215#endif
Neil Armstrong52f41f82022-02-22 15:30:24 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (sig_len < rsa_len) {
218 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
219 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100220
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100221 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100222 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100223 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200224 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
226 if (key_len <= 0) {
227 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
228 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
231 psa_set_key_algorithm(&attributes, psa_alg_md);
232 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 status = psa_import_key(&attributes,
235 buf + sizeof(buf) - key_len, key_len,
236 &key_id);
237 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500238 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100239 goto cleanup;
240 }
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
243 sig, sig_len);
244 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500245 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100246 goto cleanup;
247 }
248 ret = 0;
249
250cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 status = psa_destroy_key(key_id);
252 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500253 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100257}
Valerio Setti5c26b302023-06-21 19:47:01 +0200258#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200259static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 const unsigned char *hash, size_t hash_len,
261 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100262{
Janos Follath24eed8d2019-11-22 13:21:35 +0000263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200264 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200266
Dave Rodgman02a53d72023-09-28 17:17:07 +0100267#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
269 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
270 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100271#endif
Andres AG72849872017-01-19 11:24:33 +0000272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (sig_len < rsa_len) {
274 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
275 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
278 (unsigned int) hash_len,
279 hash, sig)) != 0) {
280 return ret;
281 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200282
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200283 /* The buffer contains a valid signature followed by extra data.
284 * We have a special error code for that so that so that callers can
285 * use mbedtls_pk_verify() to check "Does the buffer start with a
286 * valid signature?" and not just "Does the buffer contain a valid
287 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (sig_len > rsa_len) {
289 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
290 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200293}
Valerio Setti5c26b302023-06-21 19:47:01 +0200294#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200295
Jerry Yub02ee182022-03-16 10:30:41 +0800296#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100297int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
298 mbedtls_rsa_context *rsa_ctx,
299 const unsigned char *hash, size_t hash_len,
300 unsigned char *sig, size_t sig_size,
301 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100302{
Neil Armstrong98545682022-02-22 16:12:51 +0100303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
305 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
306 psa_status_t status;
307 mbedtls_pk_context key;
308 int key_len;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530309 unsigned char *buf = NULL;
310 buf = mbedtls_calloc(1, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
311 if (buf == NULL) {
312 return MBEDTLS_ERR_PK_ALLOC_FAILED;
313 }
Neil Armstrong98545682022-02-22 16:12:51 +0100314 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
317 if (sig_size < *sig_len) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530318 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
320 }
Neil Armstrong98545682022-02-22 16:12:51 +0100321
Neil Armstronge4f28682022-02-24 15:41:39 +0100322 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100323 * re-construct one to make it happy */
324 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800325 key.pk_ctx = rsa_ctx;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530326 key_len = mbedtls_pk_write_key_der(&key, buf, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (key_len <= 0) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530328 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
330 }
331 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
332 psa_set_key_algorithm(&attributes, alg);
333 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 status = psa_import_key(&attributes,
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530336 buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES - key_len, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 &key_id);
338 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500339 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100340 goto cleanup;
341 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 status = psa_sign_hash(key_id, alg, hash, hash_len,
343 sig, sig_size, sig_len);
344 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500345 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100346 goto cleanup;
347 }
348
349 ret = 0;
350
351cleanup:
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530352 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 status = psa_destroy_key(key_id);
354 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500355 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 }
357 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100358}
Jerry Yu406cf272022-03-22 11:33:42 +0800359#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800360
361#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200362static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 const unsigned char *hash, size_t hash_len,
364 unsigned char *sig, size_t sig_size, size_t *sig_len,
365 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800366{
Jerry Yu1d172a32022-03-12 19:12:05 +0800367 ((void) f_rng);
368 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800369
Jerry Yubd1b3272022-03-24 13:05:20 +0800370 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200371 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (psa_md_alg == 0) {
373 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
374 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
377 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200378 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800380}
Valerio Setti5c26b302023-06-21 19:47:01 +0200381#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200382static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 const unsigned char *hash, size_t hash_len,
384 unsigned char *sig, size_t sig_size, size_t *sig_len,
385 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200386{
valerio38992cb2023-04-20 09:56:30 +0200387 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100388
Dave Rodgman02a53d72023-09-28 17:17:07 +0100389#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
391 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
392 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100393#endif
Andres AG72849872017-01-19 11:24:33 +0000394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 *sig_len = mbedtls_rsa_get_len(rsa);
396 if (sig_size < *sig_len) {
397 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
398 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
401 md_alg, (unsigned int) hash_len,
402 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200403}
Valerio Setti5c26b302023-06-21 19:47:01 +0200404#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200405
Neil Armstrong18f43c72022-02-09 15:32:45 +0100406#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200407static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 const unsigned char *input, size_t ilen,
409 unsigned char *output, size_t *olen, size_t osize,
410 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100411{
valerio38992cb2023-04-20 09:56:30 +0200412 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
415 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
416 psa_status_t status;
417 mbedtls_pk_context key;
418 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100419 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100420
421 ((void) f_rng);
422 ((void) p_rng);
423
424#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
426 return MBEDTLS_ERR_RSA_INVALID_PADDING;
427 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100428#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (ilen != mbedtls_rsa_get_len(rsa)) {
431 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
432 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100433
434 /* mbedtls_pk_write_key_der() expects a full PK context;
435 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100436 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200437 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
439 if (key_len <= 0) {
440 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
441 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
444 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
445 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 status = psa_import_key(&attributes,
448 buf + sizeof(buf) - key_len, key_len,
449 &key_id);
450 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500451 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100452 goto cleanup;
453 }
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
456 input, ilen,
457 NULL, 0,
458 output, osize, olen);
459 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500460 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100461 goto cleanup;
462 }
463
464 ret = 0;
465
466cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 mbedtls_platform_zeroize(buf, sizeof(buf));
468 status = psa_destroy_key(key_id);
469 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500470 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100474}
Valerio Setti5c26b302023-06-21 19:47:01 +0200475#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200476static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 const unsigned char *input, size_t ilen,
478 unsigned char *output, size_t *olen, size_t osize,
479 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200480{
valerio38992cb2023-04-20 09:56:30 +0200481 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (ilen != mbedtls_rsa_get_len(rsa)) {
484 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
485 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
488 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200489}
Valerio Setti5c26b302023-06-21 19:47:01 +0200490#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200491
Neil Armstrong96a16a42022-02-10 10:40:11 +0100492#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200493static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 const unsigned char *input, size_t ilen,
495 unsigned char *output, size_t *olen, size_t osize,
496 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100497{
valerio38992cb2023-04-20 09:56:30 +0200498 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
501 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
502 psa_status_t status;
503 mbedtls_pk_context key;
504 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100505 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100506
507 ((void) f_rng);
508 ((void) p_rng);
509
510#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
512 return MBEDTLS_ERR_RSA_INVALID_PADDING;
513 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100514#endif
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if (mbedtls_rsa_get_len(rsa) > osize) {
517 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
518 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100519
Neil Armstrongac014ca2022-02-24 15:27:54 +0100520 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100521 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100522 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200523 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
525 if (key_len <= 0) {
526 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
527 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
530 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
531 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 status = psa_import_key(&attributes,
534 buf + sizeof(buf) - key_len, key_len,
535 &key_id);
536 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500537 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100538 goto cleanup;
539 }
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
542 input, ilen,
543 NULL, 0,
544 output, osize, olen);
545 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500546 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100547 goto cleanup;
548 }
549
550 ret = 0;
551
552cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 status = psa_destroy_key(key_id);
554 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500555 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100559}
Valerio Setti5c26b302023-06-21 19:47:01 +0200560#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200561static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 const unsigned char *input, size_t ilen,
563 unsigned char *output, size_t *olen, size_t osize,
564 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200565{
valerio38992cb2023-04-20 09:56:30 +0200566 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (*olen > osize) {
570 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
571 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
574 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200575}
Valerio Setti5c26b302023-06-21 19:47:01 +0200576#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200577
valerio38992cb2023-04-20 09:56:30 +0200578static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 int (*f_rng)(void *, unsigned char *, size_t),
580 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100581{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200582 (void) f_rng;
583 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200584 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
585 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100586}
587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200589{
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (ctx != NULL) {
593 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
594 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200597}
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200600{
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
602 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200603}
604
valerio38992cb2023-04-20 09:56:30 +0200605static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200606{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200607#if defined(MBEDTLS_RSA_ALT)
608 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200609 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200610 (void) items;
611#else
valerio38992cb2023-04-20 09:56:30 +0200612 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200615 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200616 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200617
618 items++;
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200621 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200622 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200623#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200624}
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626const mbedtls_pk_info_t mbedtls_rsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +0200627 .type = MBEDTLS_PK_RSA,
628 .name = "RSA",
629 .get_bitlen = rsa_get_bitlen,
630 .can_do = rsa_can_do,
631 .verify_func = rsa_verify_wrap,
632 .sign_func = rsa_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +0200633#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
634 .verify_rs_func = NULL,
635 .sign_rs_func = NULL,
636 .rs_alloc_func = NULL,
637 .rs_free_func = NULL,
638#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +0200639 .decrypt_func = rsa_decrypt_wrap,
640 .encrypt_func = rsa_encrypt_wrap,
641 .check_pair_func = rsa_check_pair_wrap,
642 .ctx_alloc_func = rsa_alloc_wrap,
643 .ctx_free_func = rsa_free_wrap,
644 .debug_func = rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200645};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200647
Valerio Setti81d75122023-06-14 14:49:33 +0200648#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200649/*
650 * Generic EC key
651 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200653{
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 return type == MBEDTLS_PK_ECKEY ||
655 type == MBEDTLS_PK_ECKEY_DH ||
656 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200657}
658
valerio38992cb2023-04-20 09:56:30 +0200659static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200660{
Valerio Settia1b8af62023-05-17 15:34:57 +0200661#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
662 return pk->ec_bits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200663#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200664 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
665 return ecp->grp.pbits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200666#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200667}
668
Valerio Setti1cdddac2023-02-02 13:55:57 +0100669#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400670#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400671/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500672 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
673 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500674 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
676 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500677{
Janos Follath24eed8d2019-11-22 13:21:35 +0000678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500679 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
682 MBEDTLS_ASN1_INTEGER)) != 0) {
683 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500684 }
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 while (unpadded_len > 0 && **from == 0x00) {
687 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500688 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500689 }
690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (unpadded_len > to_len || unpadded_len == 0) {
692 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
693 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500694
Andrzej Kurek9241d182018-11-20 05:04:35 -0500695 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 memset(to, 0x00, padding_len);
697 memcpy(to + padding_len, *from, unpadded_len);
698 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500701}
702
703/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400704 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500705 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500706 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400707 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
709 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400710{
Janos Follath24eed8d2019-11-22 13:21:35 +0000711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500712 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
715 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
716 return ret;
717 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400718
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500719 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
721 return ret;
722 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500723 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
725 return ret;
726 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400729}
730
Valerio Setti76d0f962023-06-23 13:32:54 +0200731/* Common helper for ECDSA verify using PSA functions. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200732static int ecdsa_verify_psa(unsigned char *key, size_t key_len,
733 psa_ecc_family_t curve, size_t curve_bits,
734 const unsigned char *hash, size_t hash_len,
735 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400736{
Janos Follath24eed8d2019-11-22 13:21:35 +0000737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100739 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settia1b8af62023-05-17 15:34:57 +0200740 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Valerio Settied7d6af2023-06-21 15:42:21 +0200741 size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
742 unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
743 unsigned char *p;
744 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (curve == 0) {
747 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
748 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
751 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
752 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500753
Valerio Settied7d6af2023-06-21 15:42:21 +0200754 status = psa_import_key(&attributes, key, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500756 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400757 goto cleanup;
758 }
759
Valerio Settied7d6af2023-06-21 15:42:21 +0200760 if (signature_len > sizeof(extracted_sig)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500761 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
762 goto cleanup;
763 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200766 /* extract_ecdsa_sig's last parameter is the size
Valerio Settif57007d2023-05-19 13:54:39 +0200767 * of each integer to be parsed, so it's actually half
Valerio Settia1b8af62023-05-17 15:34:57 +0200768 * the size of the signature. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200769 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, extracted_sig,
Valerio Settia1b8af62023-05-17 15:34:57 +0200770 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500771 goto cleanup;
772 }
773
Valerio Settied7d6af2023-06-21 15:42:21 +0200774 status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len,
775 extracted_sig, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500777 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400779 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500782 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
783 goto cleanup;
784 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400785 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400786
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500787cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 status = psa_destroy_key(key_id);
789 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500790 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400794}
Valerio Settied7d6af2023-06-21 15:42:21 +0200795
Valerio Setti76d0f962023-06-23 13:32:54 +0200796static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk,
797 mbedtls_md_type_t md_alg,
798 const unsigned char *hash, size_t hash_len,
799 const unsigned char *sig, size_t sig_len)
Valerio Settie7730772023-06-21 16:58:40 +0200800{
801 (void) md_alg;
802 unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
803 size_t key_len;
804 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
805 psa_ecc_family_t curve;
806 size_t curve_bits;
807 psa_status_t status;
808
809 status = psa_get_key_attributes(pk->priv_id, &key_attr);
810 if (status != PSA_SUCCESS) {
811 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
812 }
813 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr));
814 curve_bits = psa_get_key_bits(&key_attr);
815 psa_reset_key_attributes(&key_attr);
816
817 status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len);
818 if (status != PSA_SUCCESS) {
819 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
820 }
821
822 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
823 hash, hash_len, sig, sig_len);
824}
825
Valerio Settied7d6af2023-06-21 15:42:21 +0200826#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
827static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
828 mbedtls_md_type_t md_alg,
829 const unsigned char *hash, size_t hash_len,
830 const unsigned char *sig, size_t sig_len)
831{
832 (void) md_alg;
833 psa_ecc_family_t curve = pk->ec_family;
834 size_t curve_bits = pk->ec_bits;
835
836 return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits,
837 hash, hash_len, sig, sig_len);
838}
839#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
840static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
841 mbedtls_md_type_t md_alg,
842 const unsigned char *hash, size_t hash_len,
843 const unsigned char *sig, size_t sig_len)
844{
845 (void) md_alg;
846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
847 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
848 unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
849 size_t key_len;
850 size_t curve_bits;
851 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
852
853 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
854 MBEDTLS_ECP_PF_UNCOMPRESSED,
855 &key_len, key, sizeof(key));
856 if (ret != 0) {
857 return ret;
858 }
859
860 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
861 hash, hash_len, sig, sig_len);
862}
863#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400864#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200865static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 const unsigned char *hash, size_t hash_len,
867 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200868{
Janos Follath24eed8d2019-11-22 13:21:35 +0000869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200870 ((void) md_alg);
871
valerio38992cb2023-04-20 09:56:30 +0200872 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
876 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
877 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200880}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400881#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100882#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200883
Valerio Setti1cdddac2023-02-02 13:55:57 +0100884#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100885#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100886/*
887 * Simultaneously convert and move raw MPI from the beginning of a buffer
888 * to an ASN.1 MPI at the end of the buffer.
889 * See also mbedtls_asn1_write_mpi().
890 *
891 * p: pointer to the end of the output buffer
892 * start: start of the output buffer, and also of the mpi to write at the end
893 * n_len: length of the mpi to read from start
894 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
896 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100897{
898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
899 size_t len = 0;
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((size_t) (*p - start) < n_len) {
902 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
903 }
Neil Armstrong15021652022-03-01 10:14:17 +0100904
905 len = n_len;
906 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100908
909 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
910 * Neither r nor s should be 0, but as a failsafe measure, still detect
911 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100913 ++(*p);
914 --len;
915 }
916
917 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (len == 0) {
919 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
920 }
Neil Armstrong15021652022-03-01 10:14:17 +0100921
922 /* if the msb is 1, ASN.1 requires that we prepend a 0.
923 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (**p & 0x80) {
925 if (*p - start < 1) {
926 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
927 }
Neil Armstrong15021652022-03-01 10:14:17 +0100928
929 *--(*p) = 0x00;
930 len += 1;
931 }
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
934 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
935 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100938}
939
940/* Transcode signature from PSA format to ASN.1 sequence.
941 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
942 * MPIs, and in-place.
943 *
944 * [in/out] sig: the signature pre- and post-transcoding
945 * [in/out] sig_len: signature length pre- and post-transcoding
946 * [int] buf_len: the available size the in/out buffer
947 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
949 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100950{
951 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
952 size_t len = 0;
953 const size_t rs_len = *sig_len / 2;
954 unsigned char *p = sig + buf_len;
955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
957 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
960 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
961 MBEDTLS_ASN1_CONSTRUCTED |
962 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100965 *sig_len = len;
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100968}
Neil Armstronge9606902022-02-09 14:23:00 +0100969
Valerio Setti76d0f962023-06-23 13:32:54 +0200970/* Common helper for ECDSA sign using PSA functions. */
Valerio Setti884c1ec2023-06-23 12:09:13 +0200971static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
Valerio Setti4657f102023-06-21 13:55:16 +0200972 const unsigned char *hash, size_t hash_len,
973 unsigned char *sig, size_t sig_size, size_t *sig_len)
974{
975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
976 psa_status_t status;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200977 psa_algorithm_t psa_sig_md;
978 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
979 psa_algorithm_t alg;
980
981 status = psa_get_key_attributes(key_id, &key_attr);
982 if (status != PSA_SUCCESS) {
983 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
984 }
985 alg = psa_get_key_algorithm(&key_attr);
Valerio Setti76d0f962023-06-23 13:32:54 +0200986 psa_reset_key_attributes(&key_attr);
Valerio Setti884c1ec2023-06-23 12:09:13 +0200987
988 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) {
989 psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
990 } else {
991 psa_sig_md = PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
992 }
Valerio Setti4657f102023-06-21 13:55:16 +0200993
994 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
995 sig, sig_size, sig_len);
996 if (status != PSA_SUCCESS) {
997 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
998 }
999
1000 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1001
1002 return ret;
1003}
1004
Valerio Setti76d0f962023-06-23 13:32:54 +02001005static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk,
1006 mbedtls_md_type_t md_alg,
1007 const unsigned char *hash, size_t hash_len,
1008 unsigned char *sig, size_t sig_size,
1009 size_t *sig_len,
1010 int (*f_rng)(void *, unsigned char *, size_t),
1011 void *p_rng)
Valerio Setti4657f102023-06-21 13:55:16 +02001012{
1013 ((void) f_rng);
1014 ((void) p_rng);
Valerio Setti4657f102023-06-21 13:55:16 +02001015
Valerio Setti884c1ec2023-06-23 12:09:13 +02001016 return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size,
Valerio Setti4657f102023-06-21 13:55:16 +02001017 sig_len);
1018}
1019
1020#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +02001021/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1022 * using the same function. */
1023#define ecdsa_sign_wrap ecdsa_opaque_sign_wrap
Valerio Setti4657f102023-06-21 13:55:16 +02001024#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001025static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 const unsigned char *hash, size_t hash_len,
1027 unsigned char *sig, size_t sig_size, size_t *sig_len,
1028 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +01001029{
Neil Armstronge9606902022-02-09 14:23:00 +01001030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +01001031 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1032 psa_status_t status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001033 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
1034 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1035 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +01001036 size_t curve_bits;
1037 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +02001039 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti4657f102023-06-21 13:55:16 +02001040#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1041 psa_algorithm_t psa_sig_md =
1042 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
1043#else
1044 psa_algorithm_t psa_sig_md =
1045 PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
1046#endif
Neil Armstronge9606902022-02-09 14:23:00 +01001047 ((void) f_rng);
1048 ((void) p_rng);
1049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 if (curve == 0) {
1051 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1052 }
Neil Armstronge9606902022-02-09 14:23:00 +01001053
Gilles Peskine13caa942022-10-04 22:59:26 +02001054 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +01001055 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 }
Gilles Peskine13caa942022-10-04 22:59:26 +02001057 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +01001059 goto cleanup;
1060 }
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1063 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
1064 psa_set_key_algorithm(&attributes, psa_sig_md);
1065
Valerio Setti4657f102023-06-21 13:55:16 +02001066 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001068 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +01001070 }
1071
Valerio Setti884c1ec2023-06-23 12:09:13 +02001072 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +01001073
1074cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 mbedtls_platform_zeroize(buf, sizeof(buf));
1076 status = psa_destroy_key(key_id);
1077 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001078 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001082}
Valerio Setti4657f102023-06-21 13:55:16 +02001083#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001084#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001085static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 const unsigned char *hash, size_t hash_len,
1087 unsigned char *sig, size_t sig_size, size_t *sig_len,
1088 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001089{
valerio38992cb2023-04-20 09:56:30 +02001090 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 md_alg, hash, hash_len,
1092 sig, sig_size, sig_len,
1093 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001094}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001095#endif /* MBEDTLS_USE_PSA_CRYPTO */
1096#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001097
Valerio Setti80d07982023-02-08 13:49:17 +01001098#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001099/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001100static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001101 const unsigned char *hash, size_t hash_len,
1102 const unsigned char *sig, size_t sig_len,
1103 void *rs_ctx);
1104
valerio38992cb2023-04-20 09:56:30 +02001105static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001106 const unsigned char *hash, size_t hash_len,
1107 unsigned char *sig, size_t sig_size, size_t *sig_len,
1108 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1109 void *rs_ctx);
1110
1111/*
1112 * Restart context for ECDSA operations with ECKEY context
1113 *
1114 * We need to store an actual ECDSA context, as we need to pass the same to
1115 * the underlying ecdsa function, so we can't create it on the fly every time.
1116 */
1117typedef struct {
1118 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1119 mbedtls_ecdsa_context ecdsa_ctx;
1120} eckey_restart_ctx;
1121
1122static void *eckey_rs_alloc(void)
1123{
1124 eckey_restart_ctx *rs_ctx;
1125
1126 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1127
1128 if (ctx != NULL) {
1129 rs_ctx = ctx;
1130 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1131 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1132 }
1133
1134 return ctx;
1135}
1136
1137static void eckey_rs_free(void *ctx)
1138{
1139 eckey_restart_ctx *rs_ctx;
1140
1141 if (ctx == NULL) {
1142 return;
1143 }
1144
1145 rs_ctx = ctx;
1146 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1147 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1148
1149 mbedtls_free(ctx);
1150}
1151
valerio38992cb2023-04-20 09:56:30 +02001152static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001153 const unsigned char *hash, size_t hash_len,
1154 const unsigned char *sig, size_t sig_len,
1155 void *rs_ctx)
1156{
1157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1158 eckey_restart_ctx *rs = rs_ctx;
1159
1160 /* Should never happen */
1161 if (rs == NULL) {
1162 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1163 }
1164
1165 /* set up our own sub-context if needed (that is, on first run) */
1166 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001167 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001168 }
1169
valerio38992cb2023-04-20 09:56:30 +02001170 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001171 md_alg, hash, hash_len,
1172 sig, sig_len, &rs->ecdsa_rs));
1173
1174cleanup:
1175 return ret;
1176}
1177
valerio38992cb2023-04-20 09:56:30 +02001178static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001179 const unsigned char *hash, size_t hash_len,
1180 unsigned char *sig, size_t sig_size, size_t *sig_len,
1181 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1182 void *rs_ctx)
1183{
1184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1185 eckey_restart_ctx *rs = rs_ctx;
1186
1187 /* Should never happen */
1188 if (rs == NULL) {
1189 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1190 }
1191
1192 /* set up our own sub-context if needed (that is, on first run) */
1193 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001194 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001195 }
1196
valerio38992cb2023-04-20 09:56:30 +02001197 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001198 hash, hash_len, sig, sig_size, sig_len,
1199 f_rng, p_rng, &rs->ecdsa_rs));
1200
1201cleanup:
1202 return ret;
1203}
Valerio Setti80d07982023-02-08 13:49:17 +01001204#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001205
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001206#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +02001207#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +02001208static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001209{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001210 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001212 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001213 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +02001214 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001215
1216 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1217 &prv_key_len);
1218 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1219 if (ret != 0) {
1220 return ret;
1221 }
1222
1223 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1224 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1225 }
Valerio Settibb7603a2023-06-21 18:34:54 +02001226
1227 return 0;
1228}
1229#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1230static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
1231{
1232 psa_status_t status;
1233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1234 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1235 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001236 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001237 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1238 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001239 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1240 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001241 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001242 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001243 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001244 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001245
Valerio Settia7cb8452023-05-22 18:39:43 +02001246 if (curve == 0) {
1247 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1248 }
1249
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001250 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1251 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1252
Valerio Settia1b8af62023-05-17 15:34:57 +02001253 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1254 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001255 if (ret != 0) {
Valerio Setti35d1dac2023-06-30 18:04:16 +02001256 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001257 return ret;
1258 }
1259
1260 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti35d1dac2023-06-30 18:04:16 +02001261 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +02001262 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1263 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001264 return ret;
1265 }
1266
Valerio Setti35d1dac2023-06-30 18:04:16 +02001267 // From now on prv_key_buf is used to store the public key of prv.
Valerio Setti1df94f82023-04-07 08:59:24 +02001268 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1269 &prv_key_len);
1270 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1271 destruction_status = psa_destroy_key(key_id);
1272 if (ret != 0) {
1273 return ret;
1274 } else if (destruction_status != PSA_SUCCESS) {
1275 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001276 }
1277
Valerio Settia1b8af62023-05-17 15:34:57 +02001278 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1279 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001280 MBEDTLS_ECP_PF_UNCOMPRESSED,
1281 &pub_key_len, pub_key_buf,
1282 sizeof(pub_key_buf));
1283 if (ret != 0) {
1284 return ret;
1285 }
1286
1287 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1288 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1289 }
1290
1291 return 0;
1292}
Valerio Settibb7603a2023-06-21 18:34:54 +02001293#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001294
Valerio Settibb7603a2023-06-21 18:34:54 +02001295static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1296 int (*f_rng)(void *, unsigned char *, size_t),
1297 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001298{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001299 (void) f_rng;
1300 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001301 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001302}
1303#else /* MBEDTLS_USE_PSA_CRYPTO */
1304static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1305 int (*f_rng)(void *, unsigned char *, size_t),
1306 void *p_rng)
1307{
valerio38992cb2023-04-20 09:56:30 +02001308 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1309 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001310 f_rng, p_rng);
1311}
Valerio Settibb7603a2023-06-21 18:34:54 +02001312#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001313
Valerio Setti88a3aee2023-06-29 15:01:10 +02001314#if defined(MBEDTLS_USE_PSA_CRYPTO)
1315#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1316/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1317 * using the same function. */
1318#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
1319#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1320static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1321 mbedtls_pk_context *prv,
1322 int (*f_rng)(void *, unsigned char *, size_t),
1323 void *p_rng)
1324{
1325 psa_status_t status;
1326 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1327 size_t exp_pub_key_len = 0;
1328 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1329 size_t pub_key_len = 0;
1330 int ret;
1331 (void) f_rng;
1332 (void) p_rng;
1333
1334 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1335 &exp_pub_key_len);
1336 if (status != PSA_SUCCESS) {
1337 ret = psa_pk_status_to_mbedtls(status);
1338 return ret;
1339 }
1340 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1341 &(mbedtls_pk_ec_ro(*pub)->Q),
1342 MBEDTLS_ECP_PF_UNCOMPRESSED,
1343 &pub_key_len, pub_key, sizeof(pub_key));
1344 if (ret != 0) {
1345 return ret;
1346 }
1347 if ((exp_pub_key_len != pub_key_len) ||
1348 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1349 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1350 }
1351 return 0;
1352}
1353#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1354#endif /* MBEDTLS_USE_PSA_CRYPTO */
1355
Valerio Settib5361262023-05-18 18:51:58 +02001356#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001357static void *eckey_alloc_wrap(void)
1358{
1359 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1360
1361 if (ctx != NULL) {
1362 mbedtls_ecp_keypair_init(ctx);
1363 }
1364
1365 return ctx;
1366}
1367
1368static void eckey_free_wrap(void *ctx)
1369{
1370 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1371 mbedtls_free(ctx);
1372}
Valerio Settib5361262023-05-18 18:51:58 +02001373#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001374
valerio38992cb2023-04-20 09:56:30 +02001375static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001376{
Valerio Settia1b8af62023-05-17 15:34:57 +02001377#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1378 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1379 items->name = "eckey.Q";
1380 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001381#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001382 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001383 items->type = MBEDTLS_PK_DEBUG_ECP;
1384 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001385 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001386#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001387}
1388
1389const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001390 .type = MBEDTLS_PK_ECKEY,
1391 .name = "EC",
1392 .get_bitlen = eckey_get_bitlen,
1393 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001394#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001395 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001396#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1397 .verify_func = NULL,
1398#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001399#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001400 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001401#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1402 .sign_func = NULL,
1403#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001404#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001405 .verify_rs_func = eckey_verify_rs_wrap,
1406 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001407 .rs_alloc_func = eckey_rs_alloc,
1408 .rs_free_func = eckey_rs_free,
1409#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1410 .decrypt_func = NULL,
1411 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001412 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001413#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1414 .ctx_alloc_func = NULL,
1415 .ctx_free_func = NULL,
1416#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001417 .ctx_alloc_func = eckey_alloc_wrap,
1418 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001419#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001420 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001421};
1422
1423/*
1424 * EC key restricted to ECDH
1425 */
1426static int eckeydh_can_do(mbedtls_pk_type_t type)
1427{
1428 return type == MBEDTLS_PK_ECKEY ||
1429 type == MBEDTLS_PK_ECKEY_DH;
1430}
1431
1432const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001433 .type = MBEDTLS_PK_ECKEY_DH,
1434 .name = "EC_DH",
1435 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1436 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001437 .verify_func = NULL,
1438 .sign_func = NULL,
1439#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1440 .verify_rs_func = NULL,
1441 .sign_rs_func = NULL,
1442#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1443 .decrypt_func = NULL,
1444 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001445 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001446#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1447 .ctx_alloc_func = NULL,
1448 .ctx_free_func = NULL,
1449#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001450 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1451 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001452#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001453 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001454};
Valerio Setti1cdddac2023-02-02 13:55:57 +01001455
1456#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1457static int ecdsa_can_do(mbedtls_pk_type_t type)
1458{
1459 return type == MBEDTLS_PK_ECDSA;
1460}
1461
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001462#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001463static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 const unsigned char *hash, size_t hash_len,
1465 const unsigned char *sig, size_t sig_len,
1466 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001467{
Janos Follath24eed8d2019-11-22 13:21:35 +00001468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001469 ((void) md_alg);
1470
1471 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001472 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 hash, hash_len, sig, sig_len,
1474 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1477 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1478 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001481}
1482
valerio38992cb2023-04-20 09:56:30 +02001483static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 const unsigned char *hash, size_t hash_len,
1485 unsigned char *sig, size_t sig_size, size_t *sig_len,
1486 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1487 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001488{
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001490 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1492 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001493
1494}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001497{
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (ctx != NULL) {
1501 mbedtls_ecdsa_restart_init(ctx);
1502 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001505}
1506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001508{
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 mbedtls_ecdsa_restart_free(ctx);
1510 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001511}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001512#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001515 .type = MBEDTLS_PK_ECDSA,
1516 .name = "ECDSA",
1517 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1518 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001519#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001520 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001521#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1522 .verify_func = NULL,
1523#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001524#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001525 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001526#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1527 .sign_func = NULL,
1528#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001529#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001530 .verify_rs_func = ecdsa_verify_rs_wrap,
1531 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001532 .rs_alloc_func = ecdsa_rs_alloc,
1533 .rs_free_func = ecdsa_rs_free,
1534#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1535 .decrypt_func = NULL,
1536 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001537 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001538#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1539 .ctx_alloc_func = NULL,
1540 .ctx_free_func = NULL,
1541#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001542 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1543 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001544#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001545 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001546};
Valerio Setti7ca13182023-01-27 13:22:42 +01001547#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Valerio Settid9d74c22023-06-29 15:00:02 +02001548#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001551/*
1552 * Support for alternative RSA-private implementations
1553 */
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001556{
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001558}
1559
valerio38992cb2023-04-20 09:56:30 +02001560static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001561{
valerio38992cb2023-04-20 09:56:30 +02001562 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001565}
1566
valerio38992cb2023-04-20 09:56:30 +02001567static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 const unsigned char *hash, size_t hash_len,
1569 unsigned char *sig, size_t sig_size, size_t *sig_len,
1570 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001571{
valerio38992cb2023-04-20 09:56:30 +02001572 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001573
Dave Rodgman02a53d72023-09-28 17:17:07 +01001574#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (UINT_MAX < hash_len) {
1576 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1577 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001578#endif
Andres AG72849872017-01-19 11:24:33 +00001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1581 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1582 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1583 }
1584 if (*sig_len > sig_size) {
1585 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1586 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1589 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001590}
1591
valerio38992cb2023-04-20 09:56:30 +02001592static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 const unsigned char *input, size_t ilen,
1594 unsigned char *output, size_t *olen, size_t osize,
1595 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001596{
valerio38992cb2023-04-20 09:56:30 +02001597 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001598
1599 ((void) f_rng);
1600 ((void) p_rng);
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1603 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1604 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 return rsa_alt->decrypt_func(rsa_alt->key,
1607 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001608}
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001611static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 int (*f_rng)(void *, unsigned char *, size_t),
1613 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001616 unsigned char hash[32];
1617 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1621 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 memset(hash, 0x2a, sizeof(hash));
1625
valerio38992cb2023-04-20 09:56:30 +02001626 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 hash, sizeof(hash),
1628 sig, sizeof(sig), &sig_len,
1629 f_rng, p_rng)) != 0) {
1630 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001631 }
1632
valerio38992cb2023-04-20 09:56:30 +02001633 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 hash, sizeof(hash), sig, sig_len) != 0) {
1635 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1636 }
1637
1638 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001643{
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if (ctx != NULL) {
1647 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1648 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001651}
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001654{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001655 mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001656}
1657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001659 .type = MBEDTLS_PK_RSA_ALT,
1660 .name = "RSA-alt",
1661 .get_bitlen = rsa_alt_get_bitlen,
1662 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001663 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001664 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001665#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1666 .verify_rs_func = NULL,
1667 .sign_rs_func = NULL,
1668 .rs_alloc_func = NULL,
1669 .rs_free_func = NULL,
1670#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001671 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001672 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001674 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001675#else
1676 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001677#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001678 .ctx_alloc_func = rsa_alt_alloc_wrap,
1679 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001680 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001681};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001683
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001684#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001685static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001686{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001687 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001689
Valerio Setti4f387ef2023-05-02 14:15:59 +02001690 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 return 0;
1692 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 bits = psa_get_key_bits(&attributes);
1695 psa_reset_key_attributes(&attributes);
1696 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001697}
1698
Valerio Setti38913c12023-06-30 16:18:33 +02001699#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti76d0f962023-06-23 13:32:54 +02001700static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001701{
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 return type == MBEDTLS_PK_ECKEY ||
1703 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001704}
1705
Valerio Setti76d0f962023-06-23 13:32:54 +02001706const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001707 .type = MBEDTLS_PK_OPAQUE,
1708 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001709 .get_bitlen = opaque_get_bitlen,
1710 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001711#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001712 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001713#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1714 .verify_func = NULL,
1715#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1716#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001717 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001718#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1719 .sign_func = NULL,
1720#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001721#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1722 .verify_rs_func = NULL,
1723 .sign_rs_func = NULL,
1724 .rs_alloc_func = NULL,
1725 .rs_free_func = NULL,
1726#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1727 .decrypt_func = NULL,
1728 .encrypt_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001729 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001730 .ctx_alloc_func = NULL,
1731 .ctx_free_func = NULL,
1732 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001733};
Valerio Setti38913c12023-06-30 16:18:33 +02001734#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti574a00b2023-06-21 19:47:37 +02001735
Valerio Setti76d0f962023-06-23 13:32:54 +02001736static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001737{
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 return type == MBEDTLS_PK_RSA ||
1739 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001740}
1741
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001742#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001743static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1744 const unsigned char *input, size_t ilen,
1745 unsigned char *output, size_t *olen, size_t osize,
1746 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001747{
Valerio Setti574a00b2023-06-21 19:47:37 +02001748 psa_status_t status;
1749
1750 /* PSA has its own RNG */
1751 (void) f_rng;
1752 (void) p_rng;
1753
1754 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1755 input, ilen,
1756 NULL, 0,
1757 output, osize, olen);
1758 if (status != PSA_SUCCESS) {
1759 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1760 }
1761
1762 return 0;
1763}
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001764#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti574a00b2023-06-21 19:47:37 +02001765
Valerio Setti76d0f962023-06-23 13:32:54 +02001766static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1767 const unsigned char *hash, size_t hash_len,
1768 unsigned char *sig, size_t sig_size, size_t *sig_len,
1769 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001770{
1771#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001773 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001774 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001775 psa_status_t status;
1776
1777 /* PSA has its own RNG */
1778 (void) f_rng;
1779 (void) p_rng;
1780
Valerio Setti4f387ef2023-05-02 14:15:59 +02001781 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001783 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 type = psa_get_key_type(&attributes);
1787 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001790 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001791 } else {
1792 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1793 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001794
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001795 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001796 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 sig, sig_size, sig_len);
1798 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001800 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001801 } else {
1802 return PSA_PK_TO_MBEDTLS_ERR(status);
1803 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001804 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001805
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001806 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001807#else /* !MBEDTLS_RSA_C */
1808 ((void) pk);
1809 ((void) md_alg);
1810 ((void) hash);
1811 ((void) hash_len);
1812 ((void) sig);
1813 ((void) sig_size);
1814 ((void) sig_len);
1815 ((void) f_rng);
1816 ((void) p_rng);
1817 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001818#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001819}
1820
Valerio Setti76d0f962023-06-23 13:32:54 +02001821const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001822 .type = MBEDTLS_PK_OPAQUE,
1823 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001824 .get_bitlen = opaque_get_bitlen,
1825 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001826 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001827 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001828#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1829 .verify_rs_func = NULL,
1830 .sign_rs_func = NULL,
1831 .rs_alloc_func = NULL,
1832 .rs_free_func = NULL,
1833#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001834#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001835 .decrypt_func = rsa_opaque_decrypt,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001836#else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001837 .decrypt_func = NULL,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001838#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001839 .encrypt_func = NULL,
1840 .check_pair_func = NULL,
1841 .ctx_alloc_func = NULL,
1842 .ctx_free_func = NULL,
1843 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001844};
1845
1846#endif /* MBEDTLS_USE_PSA_CRYPTO */
1847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848#endif /* MBEDTLS_PK_C */