blob: 2c678367474a721d8361ca14c16ed490e0918cf0 [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;
Manuel Pégourié-Gonnard564bc1b2023-08-02 12:05:16 +0200979 psa_algorithm_t alg, alg2;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200980
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);
Manuel Pégourié-Gonnard564bc1b2023-08-02 12:05:16 +0200986 alg2 = psa_get_key_enrollment_algorithm(&key_attr);
Valerio Setti76d0f962023-06-23 13:32:54 +0200987 psa_reset_key_attributes(&key_attr);
Valerio Setti884c1ec2023-06-23 12:09:13 +0200988
Manuel Pégourié-Gonnard564bc1b2023-08-02 12:05:16 +0200989 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) || PSA_ALG_IS_DETERMINISTIC_ECDSA(alg2)) {
Valerio Setti884c1ec2023-06-23 12:09:13 +0200990 psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
991 } else {
992 psa_sig_md = PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
993 }
Valerio Setti4657f102023-06-21 13:55:16 +0200994
995 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
996 sig, sig_size, sig_len);
997 if (status != PSA_SUCCESS) {
998 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
999 }
1000
1001 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1002
1003 return ret;
1004}
1005
Valerio Setti76d0f962023-06-23 13:32:54 +02001006static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk,
1007 mbedtls_md_type_t md_alg,
1008 const unsigned char *hash, size_t hash_len,
1009 unsigned char *sig, size_t sig_size,
1010 size_t *sig_len,
1011 int (*f_rng)(void *, unsigned char *, size_t),
1012 void *p_rng)
Valerio Setti4657f102023-06-21 13:55:16 +02001013{
1014 ((void) f_rng);
1015 ((void) p_rng);
Valerio Setti4657f102023-06-21 13:55:16 +02001016
Valerio Setti884c1ec2023-06-23 12:09:13 +02001017 return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size,
Valerio Setti4657f102023-06-21 13:55:16 +02001018 sig_len);
1019}
1020
1021#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +02001022/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1023 * using the same function. */
1024#define ecdsa_sign_wrap ecdsa_opaque_sign_wrap
Valerio Setti4657f102023-06-21 13:55:16 +02001025#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001026static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 const unsigned char *hash, size_t hash_len,
1028 unsigned char *sig, size_t sig_size, size_t *sig_len,
1029 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +01001030{
Neil Armstronge9606902022-02-09 14:23:00 +01001031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +01001032 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1033 psa_status_t status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001034 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
1035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1036 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +01001037 size_t curve_bits;
1038 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +02001040 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +02001041 psa_algorithm_t psa_hash = mbedtls_md_psa_alg_from_type(md_alg);
1042 psa_algorithm_t psa_sig_md = MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(psa_hash);
Neil Armstronge9606902022-02-09 14:23:00 +01001043 ((void) f_rng);
1044 ((void) p_rng);
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (curve == 0) {
1047 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1048 }
Neil Armstronge9606902022-02-09 14:23:00 +01001049
Gilles Peskine13caa942022-10-04 22:59:26 +02001050 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +01001051 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 }
Gilles Peskine13caa942022-10-04 22:59:26 +02001053 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +01001055 goto cleanup;
1056 }
1057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1059 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
1060 psa_set_key_algorithm(&attributes, psa_sig_md);
1061
Valerio Setti4657f102023-06-21 13:55:16 +02001062 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001064 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +01001066 }
1067
Valerio Setti884c1ec2023-06-23 12:09:13 +02001068 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +01001069
1070cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 mbedtls_platform_zeroize(buf, sizeof(buf));
1072 status = psa_destroy_key(key_id);
1073 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001074 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001078}
Valerio Setti4657f102023-06-21 13:55:16 +02001079#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001080#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001081static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 const unsigned char *hash, size_t hash_len,
1083 unsigned char *sig, size_t sig_size, size_t *sig_len,
1084 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001085{
valerio38992cb2023-04-20 09:56:30 +02001086 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 md_alg, hash, hash_len,
1088 sig, sig_size, sig_len,
1089 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001090}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001091#endif /* MBEDTLS_USE_PSA_CRYPTO */
1092#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001093
Valerio Setti80d07982023-02-08 13:49:17 +01001094#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001095/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001096static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001097 const unsigned char *hash, size_t hash_len,
1098 const unsigned char *sig, size_t sig_len,
1099 void *rs_ctx);
1100
valerio38992cb2023-04-20 09:56:30 +02001101static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001102 const unsigned char *hash, size_t hash_len,
1103 unsigned char *sig, size_t sig_size, size_t *sig_len,
1104 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1105 void *rs_ctx);
1106
1107/*
1108 * Restart context for ECDSA operations with ECKEY context
1109 *
1110 * We need to store an actual ECDSA context, as we need to pass the same to
1111 * the underlying ecdsa function, so we can't create it on the fly every time.
1112 */
1113typedef struct {
1114 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1115 mbedtls_ecdsa_context ecdsa_ctx;
1116} eckey_restart_ctx;
1117
1118static void *eckey_rs_alloc(void)
1119{
1120 eckey_restart_ctx *rs_ctx;
1121
1122 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1123
1124 if (ctx != NULL) {
1125 rs_ctx = ctx;
1126 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1127 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1128 }
1129
1130 return ctx;
1131}
1132
1133static void eckey_rs_free(void *ctx)
1134{
1135 eckey_restart_ctx *rs_ctx;
1136
1137 if (ctx == NULL) {
1138 return;
1139 }
1140
1141 rs_ctx = ctx;
1142 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1143 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1144
1145 mbedtls_free(ctx);
1146}
1147
valerio38992cb2023-04-20 09:56:30 +02001148static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001149 const unsigned char *hash, size_t hash_len,
1150 const unsigned char *sig, size_t sig_len,
1151 void *rs_ctx)
1152{
1153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1154 eckey_restart_ctx *rs = rs_ctx;
1155
1156 /* Should never happen */
1157 if (rs == NULL) {
1158 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1159 }
1160
1161 /* set up our own sub-context if needed (that is, on first run) */
1162 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001163 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001164 }
1165
valerio38992cb2023-04-20 09:56:30 +02001166 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001167 md_alg, hash, hash_len,
1168 sig, sig_len, &rs->ecdsa_rs));
1169
1170cleanup:
1171 return ret;
1172}
1173
valerio38992cb2023-04-20 09:56:30 +02001174static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001175 const unsigned char *hash, size_t hash_len,
1176 unsigned char *sig, size_t sig_size, size_t *sig_len,
1177 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1178 void *rs_ctx)
1179{
1180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1181 eckey_restart_ctx *rs = rs_ctx;
1182
1183 /* Should never happen */
1184 if (rs == NULL) {
1185 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1186 }
1187
1188 /* set up our own sub-context if needed (that is, on first run) */
1189 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001190 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001191 }
1192
valerio38992cb2023-04-20 09:56:30 +02001193 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001194 hash, hash_len, sig, sig_size, sig_len,
1195 f_rng, p_rng, &rs->ecdsa_rs));
1196
1197cleanup:
1198 return ret;
1199}
Valerio Setti80d07982023-02-08 13:49:17 +01001200#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001201
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001202#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +02001203#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +02001204static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001205{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001206 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001208 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001209 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +02001210 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001211
1212 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1213 &prv_key_len);
1214 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1215 if (ret != 0) {
1216 return ret;
1217 }
1218
1219 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1220 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1221 }
Valerio Settibb7603a2023-06-21 18:34:54 +02001222
1223 return 0;
1224}
1225#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1226static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
1227{
1228 psa_status_t status;
1229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1230 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1231 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001232 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001233 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1234 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001235 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1236 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001237 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001238 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001239 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001240 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001241
Valerio Settia7cb8452023-05-22 18:39:43 +02001242 if (curve == 0) {
1243 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1244 }
1245
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001246 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1247 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1248
Valerio Settia1b8af62023-05-17 15:34:57 +02001249 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1250 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001251 if (ret != 0) {
Valerio Setti35d1dac2023-06-30 18:04:16 +02001252 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001253 return ret;
1254 }
1255
1256 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti35d1dac2023-06-30 18:04:16 +02001257 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +02001258 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1259 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001260 return ret;
1261 }
1262
Valerio Setti35d1dac2023-06-30 18:04:16 +02001263 // From now on prv_key_buf is used to store the public key of prv.
Valerio Setti1df94f82023-04-07 08:59:24 +02001264 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1265 &prv_key_len);
1266 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1267 destruction_status = psa_destroy_key(key_id);
1268 if (ret != 0) {
1269 return ret;
1270 } else if (destruction_status != PSA_SUCCESS) {
1271 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001272 }
1273
Valerio Settia1b8af62023-05-17 15:34:57 +02001274 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1275 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001276 MBEDTLS_ECP_PF_UNCOMPRESSED,
1277 &pub_key_len, pub_key_buf,
1278 sizeof(pub_key_buf));
1279 if (ret != 0) {
1280 return ret;
1281 }
1282
1283 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1284 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1285 }
1286
1287 return 0;
1288}
Valerio Settibb7603a2023-06-21 18:34:54 +02001289#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001290
Valerio Settibb7603a2023-06-21 18:34:54 +02001291static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1292 int (*f_rng)(void *, unsigned char *, size_t),
1293 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001294{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001295 (void) f_rng;
1296 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001297 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001298}
1299#else /* MBEDTLS_USE_PSA_CRYPTO */
1300static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1301 int (*f_rng)(void *, unsigned char *, size_t),
1302 void *p_rng)
1303{
valerio38992cb2023-04-20 09:56:30 +02001304 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1305 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001306 f_rng, p_rng);
1307}
Valerio Settibb7603a2023-06-21 18:34:54 +02001308#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001309
Valerio Setti88a3aee2023-06-29 15:01:10 +02001310#if defined(MBEDTLS_USE_PSA_CRYPTO)
1311#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1312/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1313 * using the same function. */
1314#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
1315#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1316static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1317 mbedtls_pk_context *prv,
1318 int (*f_rng)(void *, unsigned char *, size_t),
1319 void *p_rng)
1320{
1321 psa_status_t status;
1322 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1323 size_t exp_pub_key_len = 0;
1324 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1325 size_t pub_key_len = 0;
1326 int ret;
1327 (void) f_rng;
1328 (void) p_rng;
1329
1330 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1331 &exp_pub_key_len);
1332 if (status != PSA_SUCCESS) {
1333 ret = psa_pk_status_to_mbedtls(status);
1334 return ret;
1335 }
1336 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1337 &(mbedtls_pk_ec_ro(*pub)->Q),
1338 MBEDTLS_ECP_PF_UNCOMPRESSED,
1339 &pub_key_len, pub_key, sizeof(pub_key));
1340 if (ret != 0) {
1341 return ret;
1342 }
1343 if ((exp_pub_key_len != pub_key_len) ||
1344 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1345 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1346 }
1347 return 0;
1348}
1349#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1350#endif /* MBEDTLS_USE_PSA_CRYPTO */
1351
Valerio Settib5361262023-05-18 18:51:58 +02001352#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001353static void *eckey_alloc_wrap(void)
1354{
1355 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1356
1357 if (ctx != NULL) {
1358 mbedtls_ecp_keypair_init(ctx);
1359 }
1360
1361 return ctx;
1362}
1363
1364static void eckey_free_wrap(void *ctx)
1365{
1366 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1367 mbedtls_free(ctx);
1368}
Valerio Settib5361262023-05-18 18:51:58 +02001369#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001370
valerio38992cb2023-04-20 09:56:30 +02001371static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001372{
Valerio Settia1b8af62023-05-17 15:34:57 +02001373#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1374 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1375 items->name = "eckey.Q";
1376 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001377#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001378 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001379 items->type = MBEDTLS_PK_DEBUG_ECP;
1380 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001381 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001382#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001383}
1384
1385const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001386 .type = MBEDTLS_PK_ECKEY,
1387 .name = "EC",
1388 .get_bitlen = eckey_get_bitlen,
1389 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001390#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001391 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001392#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1393 .verify_func = NULL,
1394#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001395#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001396 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001397#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1398 .sign_func = NULL,
1399#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001400#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001401 .verify_rs_func = eckey_verify_rs_wrap,
1402 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001403 .rs_alloc_func = eckey_rs_alloc,
1404 .rs_free_func = eckey_rs_free,
1405#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1406 .decrypt_func = NULL,
1407 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001408 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001409#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1410 .ctx_alloc_func = NULL,
1411 .ctx_free_func = NULL,
1412#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001413 .ctx_alloc_func = eckey_alloc_wrap,
1414 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001415#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001416 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001417};
1418
1419/*
1420 * EC key restricted to ECDH
1421 */
1422static int eckeydh_can_do(mbedtls_pk_type_t type)
1423{
1424 return type == MBEDTLS_PK_ECKEY ||
1425 type == MBEDTLS_PK_ECKEY_DH;
1426}
1427
1428const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001429 .type = MBEDTLS_PK_ECKEY_DH,
1430 .name = "EC_DH",
1431 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1432 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001433 .verify_func = NULL,
1434 .sign_func = NULL,
1435#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1436 .verify_rs_func = NULL,
1437 .sign_rs_func = NULL,
1438#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1439 .decrypt_func = NULL,
1440 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001441 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001442#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1443 .ctx_alloc_func = NULL,
1444 .ctx_free_func = NULL,
1445#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001446 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1447 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001448#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001449 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001450};
Valerio Setti1cdddac2023-02-02 13:55:57 +01001451
1452#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1453static int ecdsa_can_do(mbedtls_pk_type_t type)
1454{
1455 return type == MBEDTLS_PK_ECDSA;
1456}
1457
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001458#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001459static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 const unsigned char *hash, size_t hash_len,
1461 const unsigned char *sig, size_t sig_len,
1462 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001463{
Janos Follath24eed8d2019-11-22 13:21:35 +00001464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001465 ((void) md_alg);
1466
1467 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001468 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 hash, hash_len, sig, sig_len,
1470 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1473 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1474 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001477}
1478
valerio38992cb2023-04-20 09:56:30 +02001479static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 const unsigned char *hash, size_t hash_len,
1481 unsigned char *sig, size_t sig_size, size_t *sig_len,
1482 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1483 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001484{
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001486 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1488 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001489
1490}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001493{
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (ctx != NULL) {
1497 mbedtls_ecdsa_restart_init(ctx);
1498 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001501}
1502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001504{
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 mbedtls_ecdsa_restart_free(ctx);
1506 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001507}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001508#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001511 .type = MBEDTLS_PK_ECDSA,
1512 .name = "ECDSA",
1513 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1514 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001515#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001516 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001517#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1518 .verify_func = NULL,
1519#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001520#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001521 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001522#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1523 .sign_func = NULL,
1524#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001525#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001526 .verify_rs_func = ecdsa_verify_rs_wrap,
1527 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001528 .rs_alloc_func = ecdsa_rs_alloc,
1529 .rs_free_func = ecdsa_rs_free,
1530#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1531 .decrypt_func = NULL,
1532 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001533 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001534#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1535 .ctx_alloc_func = NULL,
1536 .ctx_free_func = NULL,
1537#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001538 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1539 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001540#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001541 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001542};
Valerio Setti7ca13182023-01-27 13:22:42 +01001543#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Valerio Settid9d74c22023-06-29 15:00:02 +02001544#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001547/*
1548 * Support for alternative RSA-private implementations
1549 */
1550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001552{
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001554}
1555
valerio38992cb2023-04-20 09:56:30 +02001556static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001557{
valerio38992cb2023-04-20 09:56:30 +02001558 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001561}
1562
valerio38992cb2023-04-20 09:56:30 +02001563static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 const unsigned char *hash, size_t hash_len,
1565 unsigned char *sig, size_t sig_size, size_t *sig_len,
1566 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001567{
valerio38992cb2023-04-20 09:56:30 +02001568 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001569
Dave Rodgman02a53d72023-09-28 17:17:07 +01001570#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 if (UINT_MAX < hash_len) {
1572 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1573 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001574#endif
Andres AG72849872017-01-19 11:24:33 +00001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1577 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1578 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1579 }
1580 if (*sig_len > sig_size) {
1581 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1582 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1585 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001586}
1587
valerio38992cb2023-04-20 09:56:30 +02001588static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 const unsigned char *input, size_t ilen,
1590 unsigned char *output, size_t *olen, size_t osize,
1591 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001592{
valerio38992cb2023-04-20 09:56:30 +02001593 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001594
1595 ((void) f_rng);
1596 ((void) p_rng);
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1599 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1600 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return rsa_alt->decrypt_func(rsa_alt->key,
1603 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001604}
1605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001607static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 int (*f_rng)(void *, unsigned char *, size_t),
1609 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001612 unsigned char hash[32];
1613 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1617 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001618 }
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 memset(hash, 0x2a, sizeof(hash));
1621
valerio38992cb2023-04-20 09:56:30 +02001622 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 hash, sizeof(hash),
1624 sig, sizeof(sig), &sig_len,
1625 f_rng, p_rng)) != 0) {
1626 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001627 }
1628
valerio38992cb2023-04-20 09:56:30 +02001629 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 hash, sizeof(hash), sig, sig_len) != 0) {
1631 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1632 }
1633
1634 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001635}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001639{
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (ctx != NULL) {
1643 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1644 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001647}
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001650{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001651 mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001652}
1653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001655 .type = MBEDTLS_PK_RSA_ALT,
1656 .name = "RSA-alt",
1657 .get_bitlen = rsa_alt_get_bitlen,
1658 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001659 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001660 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001661#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1662 .verify_rs_func = NULL,
1663 .sign_rs_func = NULL,
1664 .rs_alloc_func = NULL,
1665 .rs_free_func = NULL,
1666#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001667 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001668 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001670 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001671#else
1672 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001673#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001674 .ctx_alloc_func = rsa_alt_alloc_wrap,
1675 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001676 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001677};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001679
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001680#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001681static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001682{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001683 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001685
Valerio Setti4f387ef2023-05-02 14:15:59 +02001686 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 return 0;
1688 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 bits = psa_get_key_bits(&attributes);
1691 psa_reset_key_attributes(&attributes);
1692 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001693}
1694
Valerio Setti38913c12023-06-30 16:18:33 +02001695#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti76d0f962023-06-23 13:32:54 +02001696static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001697{
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 return type == MBEDTLS_PK_ECKEY ||
1699 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001700}
1701
Valerio Setti76d0f962023-06-23 13:32:54 +02001702const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001703 .type = MBEDTLS_PK_OPAQUE,
1704 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001705 .get_bitlen = opaque_get_bitlen,
1706 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001707#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001708 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001709#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1710 .verify_func = NULL,
1711#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1712#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001713 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001714#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1715 .sign_func = NULL,
1716#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001717#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1718 .verify_rs_func = NULL,
1719 .sign_rs_func = NULL,
1720 .rs_alloc_func = NULL,
1721 .rs_free_func = NULL,
1722#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1723 .decrypt_func = NULL,
1724 .encrypt_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001725 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001726 .ctx_alloc_func = NULL,
1727 .ctx_free_func = NULL,
1728 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001729};
Valerio Setti38913c12023-06-30 16:18:33 +02001730#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti574a00b2023-06-21 19:47:37 +02001731
Valerio Setti76d0f962023-06-23 13:32:54 +02001732static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001733{
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 return type == MBEDTLS_PK_RSA ||
1735 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001736}
1737
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001738#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001739static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1740 const unsigned char *input, size_t ilen,
1741 unsigned char *output, size_t *olen, size_t osize,
1742 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001743{
Valerio Setti574a00b2023-06-21 19:47:37 +02001744 psa_status_t status;
1745
1746 /* PSA has its own RNG */
1747 (void) f_rng;
1748 (void) p_rng;
1749
1750 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1751 input, ilen,
1752 NULL, 0,
1753 output, osize, olen);
1754 if (status != PSA_SUCCESS) {
1755 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1756 }
1757
1758 return 0;
1759}
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001760#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti574a00b2023-06-21 19:47:37 +02001761
Valerio Setti76d0f962023-06-23 13:32:54 +02001762static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1763 const unsigned char *hash, size_t hash_len,
1764 unsigned char *sig, size_t sig_size, size_t *sig_len,
1765 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001766{
1767#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001769 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001770 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001771 psa_status_t status;
1772
1773 /* PSA has its own RNG */
1774 (void) f_rng;
1775 (void) p_rng;
1776
Valerio Setti4f387ef2023-05-02 14:15:59 +02001777 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001779 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 type = psa_get_key_type(&attributes);
1783 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001786 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001787 } else {
1788 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1789 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001790
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001791 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001792 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 sig, sig_size, sig_len);
1794 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001796 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001797 } else {
1798 return PSA_PK_TO_MBEDTLS_ERR(status);
1799 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001800 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001801
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001802 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001803#else /* !MBEDTLS_RSA_C */
1804 ((void) pk);
1805 ((void) md_alg);
1806 ((void) hash);
1807 ((void) hash_len);
1808 ((void) sig);
1809 ((void) sig_size);
1810 ((void) sig_len);
1811 ((void) f_rng);
1812 ((void) p_rng);
1813 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001814#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001815}
1816
Valerio Setti76d0f962023-06-23 13:32:54 +02001817const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001818 .type = MBEDTLS_PK_OPAQUE,
1819 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001820 .get_bitlen = opaque_get_bitlen,
1821 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001822 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001823 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001824#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1825 .verify_rs_func = NULL,
1826 .sign_rs_func = NULL,
1827 .rs_alloc_func = NULL,
1828 .rs_free_func = NULL,
1829#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001830#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001831 .decrypt_func = rsa_opaque_decrypt,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001832#else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001833 .decrypt_func = NULL,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001834#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001835 .encrypt_func = NULL,
1836 .check_pair_func = NULL,
1837 .ctx_alloc_func = NULL,
1838 .ctx_free_func = NULL,
1839 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001840};
1841
1842#endif /* MBEDTLS_USE_PSA_CRYPTO */
1843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844#endif /* MBEDTLS_PK_C */