blob: e234a8de3014f87d5d369f7b9d4e306e7705b67f [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000023#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Neil Armstrong0f49f832022-02-28 15:07:38 +010035#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
36#include "pkwrite.h"
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#endif
42
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "mbedtls/asn1write.h"
45#endif
46
Andres Amaya Garciae32df082017-10-25 09:37:04 +010047#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010049#endif
50
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040052#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010053#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040054#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059#else
60#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020061#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020063#endif
64
Andres AG72849872017-01-19 11:24:33 +000065#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010066#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020067
Jerry Yub02ee182022-03-16 10:30:41 +080068#if defined(MBEDTLS_PSA_CRYPTO_C)
Neil Armstrong19915c22022-03-01 15:21:02 +010069int mbedtls_pk_error_from_psa( psa_status_t status )
70{
71 switch( status )
72 {
73 case PSA_SUCCESS:
74 return( 0 );
75 case PSA_ERROR_INVALID_HANDLE:
76 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
77 case PSA_ERROR_NOT_PERMITTED:
78 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
79 case PSA_ERROR_BUFFER_TOO_SMALL:
80 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
81 case PSA_ERROR_NOT_SUPPORTED:
82 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
83 case PSA_ERROR_INVALID_ARGUMENT:
84 return( MBEDTLS_ERR_PK_INVALID_ALG );
85 case PSA_ERROR_INSUFFICIENT_MEMORY:
86 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
87 case PSA_ERROR_BAD_STATE:
88 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
89 case PSA_ERROR_COMMUNICATION_FAILURE:
90 case PSA_ERROR_HARDWARE_FAILURE:
91 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
92 case PSA_ERROR_DATA_CORRUPT:
93 case PSA_ERROR_DATA_INVALID:
94 case PSA_ERROR_STORAGE_FAILURE:
95 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
96 case PSA_ERROR_CORRUPTION_DETECTED:
97 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
98 default:
99 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
100 }
101}
102
Neil Armstrong30beca32022-05-03 15:42:13 +0200103#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
104 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong19915c22022-03-01 15:21:02 +0100105int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
106{
107 switch( status )
108 {
109 case PSA_ERROR_NOT_PERMITTED:
110 case PSA_ERROR_INVALID_ARGUMENT:
111 case PSA_ERROR_INVALID_HANDLE:
112 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
113 case PSA_ERROR_BUFFER_TOO_SMALL:
114 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
115 case PSA_ERROR_INSUFFICIENT_ENTROPY:
116 return( MBEDTLS_ERR_RSA_RNG_FAILED );
117 case PSA_ERROR_INVALID_SIGNATURE:
118 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
119 case PSA_ERROR_INVALID_PADDING:
120 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
121 default:
122 return( mbedtls_pk_error_from_psa( status ) );
123 }
124}
Neil Armstrong30beca32022-05-03 15:42:13 +0200125#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800126
127#endif /* MBEDTLS_PSA_CRYPTO_C */
128
129#if defined(MBEDTLS_USE_PSA_CRYPTO)
130
131#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Jerry Yu848ecce2022-03-22 10:58:48 +0800132int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status )
Jerry Yu07869e82022-03-16 16:40:50 +0800133{
134 switch( status )
135 {
136 case PSA_ERROR_NOT_PERMITTED:
137 case PSA_ERROR_INVALID_ARGUMENT:
138 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
139 case PSA_ERROR_INVALID_HANDLE:
140 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
141 case PSA_ERROR_BUFFER_TOO_SMALL:
142 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
143 case PSA_ERROR_INSUFFICIENT_ENTROPY:
144 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
145 case PSA_ERROR_INVALID_SIGNATURE:
146 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
147 default:
148 return( mbedtls_pk_error_from_psa( status ) );
149 }
150}
Jerry Yu75339822022-03-23 12:06:31 +0800151#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800152
Jerry Yu75339822022-03-23 12:06:31 +0800153#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_RSA_C)
156static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return( type == MBEDTLS_PK_RSA ||
159 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200160}
161
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200162static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200163{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100164 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
165 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200166}
167
Neil Armstrong52f41f82022-02-22 15:30:24 +0100168#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200170 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200171 const unsigned char *sig, size_t sig_len )
172{
Neil Armstrong52f41f82022-02-22 15:30:24 +0100173 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
176 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
177 psa_status_t status;
178 mbedtls_pk_context key;
179 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100180 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100181 psa_algorithm_t psa_alg_md =
182 PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_psa_translate_md( md_alg ) );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100183 size_t rsa_len = mbedtls_rsa_get_len( rsa );
184
185#if SIZE_MAX > UINT_MAX
186 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
187 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
188#endif /* SIZE_MAX > UINT_MAX */
189
190 if( sig_len < rsa_len )
191 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
192
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100193 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100194 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100195 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100196 key.pk_ctx = ctx;
197 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
198 if( key_len <= 0 )
199 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
200
201 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
202 psa_set_key_algorithm( &attributes, psa_alg_md );
203 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
204
205 status = psa_import_key( &attributes,
206 buf + sizeof( buf ) - key_len, key_len,
207 &key_id );
208 if( status != PSA_SUCCESS )
209 {
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100210 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100211 goto cleanup;
212 }
213
214 status = psa_verify_hash( key_id, psa_alg_md, hash, hash_len,
215 sig, sig_len );
216 if( status != PSA_SUCCESS )
217 {
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100218 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100219 goto cleanup;
220 }
221 ret = 0;
222
223cleanup:
Neil Armstronga33280a2022-02-24 15:17:47 +0100224 status = psa_destroy_key( key_id );
225 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100226 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronga33280a2022-02-24 15:17:47 +0100227
Neil Armstrong52f41f82022-02-22 15:30:24 +0100228 return( ret );
229}
230#else
231static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
232 const unsigned char *hash, size_t hash_len,
233 const unsigned char *sig, size_t sig_len )
234{
Janos Follath24eed8d2019-11-22 13:21:35 +0000235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100236 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
237 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200238
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100239#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000240 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
241 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100242#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000243
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100244 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200246
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100247 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
248 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100249 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200250 return( ret );
251
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200252 /* The buffer contains a valid signature followed by extra data.
253 * We have a special error code for that so that so that callers can
254 * use mbedtls_pk_verify() to check "Does the buffer start with a
255 * valid signature?" and not just "Does the buffer contain a valid
256 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100257 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200259
260 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200261}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100262#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200263
Jerry Yub02ee182022-03-16 10:30:41 +0800264#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yue010de42022-03-23 11:45:55 +0800265int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
266 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800267 const unsigned char *hash, size_t hash_len,
268 unsigned char *sig, size_t sig_size,
269 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100270{
Neil Armstrong98545682022-02-22 16:12:51 +0100271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
273 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
274 psa_status_t status;
275 mbedtls_pk_context key;
276 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100277 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100278 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100279
Jerry Yue010de42022-03-23 11:45:55 +0800280 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100281 if( sig_size < *sig_len )
282 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
283
Neil Armstronge4f28682022-02-24 15:41:39 +0100284 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100285 * re-construct one to make it happy */
286 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800287 key.pk_ctx = rsa_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100288 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
289 if( key_len <= 0 )
290 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100291 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800292 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100293 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
294
295 status = psa_import_key( &attributes,
296 buf + sizeof( buf ) - key_len, key_len,
297 &key_id );
298 if( status != PSA_SUCCESS )
299 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100300 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100301 goto cleanup;
302 }
Jerry Yubf455e72022-03-22 21:39:41 +0800303 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100304 sig, sig_size, sig_len );
305 if( status != PSA_SUCCESS )
306 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100307 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100308 goto cleanup;
309 }
310
311 ret = 0;
312
313cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100314 status = psa_destroy_key( key_id );
315 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100316 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100317 return( ret );
318}
Jerry Yu406cf272022-03-22 11:33:42 +0800319#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800320
321#if defined(MBEDTLS_USE_PSA_CRYPTO)
322static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
323 const unsigned char *hash, size_t hash_len,
324 unsigned char *sig, size_t sig_size, size_t *sig_len,
325 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
326{
Jerry Yu1d172a32022-03-12 19:12:05 +0800327 ((void) f_rng);
328 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800329
Jerry Yubd1b3272022-03-24 13:05:20 +0800330 psa_algorithm_t psa_md_alg;
331 psa_md_alg = mbedtls_psa_translate_md( md_alg );
332 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800333 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800334
Jerry Yubd1b3272022-03-24 13:05:20 +0800335 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
336 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800337 ctx, hash, hash_len,
338 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800339}
Neil Armstrong98545682022-02-22 16:12:51 +0100340#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200342 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200343 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200344 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
345{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100346 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
347
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100348#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000349 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
350 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100351#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000352
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100353 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200354 if( sig_size < *sig_len )
355 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200356
Thomas Daubney140184d2021-05-18 16:04:07 +0100357 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
358 md_alg, (unsigned int) hash_len,
359 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200360}
Neil Armstrong98545682022-02-22 16:12:51 +0100361#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200362
Neil Armstrong18f43c72022-02-09 15:32:45 +0100363#if defined(MBEDTLS_USE_PSA_CRYPTO)
364static int rsa_decrypt_wrap( void *ctx,
365 const unsigned char *input, size_t ilen,
366 unsigned char *output, size_t *olen, size_t osize,
367 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
368{
369 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
372 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
373 psa_status_t status;
374 mbedtls_pk_context key;
375 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100376 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100377
378 ((void) f_rng);
379 ((void) p_rng);
380
381#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100382 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
383 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
384#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100385
386 if( ilen != mbedtls_rsa_get_len( rsa ) )
387 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
388
389 /* mbedtls_pk_write_key_der() expects a full PK context;
390 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100391 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100392 key.pk_ctx = ctx;
393 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
394 if( key_len <= 0 )
395 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
396
397 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
398 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100399 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100400
401 status = psa_import_key( &attributes,
402 buf + sizeof( buf ) - key_len, key_len,
403 &key_id );
404 if( status != PSA_SUCCESS )
405 {
Neil Armstronge8780492022-03-03 16:54:16 +0100406 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100407 goto cleanup;
408 }
409
Neil Armstrong8e805042022-03-16 15:30:31 +0100410 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
411 input, ilen,
412 NULL, 0,
413 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100414 if( status != PSA_SUCCESS )
415 {
Neil Armstronge8780492022-03-03 16:54:16 +0100416 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100417 goto cleanup;
418 }
419
420 ret = 0;
421
422cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100423 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100424 status = psa_destroy_key( key_id );
425 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100426 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100427
Neil Armstrong18f43c72022-02-09 15:32:45 +0100428 return( ret );
429}
430#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200431static int rsa_decrypt_wrap( void *ctx,
432 const unsigned char *input, size_t ilen,
433 unsigned char *output, size_t *olen, size_t osize,
434 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
435{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100436 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
437
438 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200440
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100441 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100442 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200443}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100444#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200445
Neil Armstrong96a16a42022-02-10 10:40:11 +0100446#if defined(MBEDTLS_USE_PSA_CRYPTO)
447static int rsa_encrypt_wrap( void *ctx,
448 const unsigned char *input, size_t ilen,
449 unsigned char *output, size_t *olen, size_t osize,
450 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
451{
452 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
455 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
456 psa_status_t status;
457 mbedtls_pk_context key;
458 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100459 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100460
461 ((void) f_rng);
462 ((void) p_rng);
463
464#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100465 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
466 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100467#endif
468
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100469 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100470 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
471
Neil Armstrongac014ca2022-02-24 15:27:54 +0100472 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100473 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100474 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100475 key.pk_ctx = ctx;
476 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
477 if( key_len <= 0 )
478 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
479
480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100481 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100482 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
483
484 status = psa_import_key( &attributes,
485 buf + sizeof( buf ) - key_len, key_len,
486 &key_id );
487 if( status != PSA_SUCCESS )
488 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100489 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100490 goto cleanup;
491 }
492
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100493 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
494 input, ilen,
495 NULL, 0,
496 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100497 if( status != PSA_SUCCESS )
498 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100499 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100500 goto cleanup;
501 }
502
503 ret = 0;
504
505cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100506 status = psa_destroy_key( key_id );
507 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100508 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100509
Neil Armstrong96a16a42022-02-10 10:40:11 +0100510 return( ret );
511}
512#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200513static int rsa_encrypt_wrap( void *ctx,
514 const unsigned char *input, size_t ilen,
515 unsigned char *output, size_t *olen, size_t osize,
516 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
517{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100518 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
519 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200520
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100521 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100523
Thomas Daubney21772772021-05-13 17:30:32 +0100524 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100525 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200526}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100527#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200528
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200529static int rsa_check_pair_wrap( const void *pub, const void *prv,
530 int (*f_rng)(void *, unsigned char *, size_t),
531 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100532{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200533 (void) f_rng;
534 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
536 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100537}
538
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200539static void *rsa_alloc_wrap( void )
540{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200541 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200542
543 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200544 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200545
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200546 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200547}
548
549static void rsa_free_wrap( void *ctx )
550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
552 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200553}
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200556{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200557#if defined(MBEDTLS_RSA_ALT)
558 /* Not supported */
559 (void) ctx;
560 (void) items;
561#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200563 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200565
566 items++;
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200569 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200571#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200572}
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574const mbedtls_pk_info_t mbedtls_rsa_info = {
575 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200576 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200577 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200578 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200579 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200580 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200581#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200582 NULL,
583 NULL,
584#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200585 rsa_decrypt_wrap,
586 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100587 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200588 rsa_alloc_wrap,
589 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200590#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200591 NULL,
592 NULL,
593#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200594 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200595};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200599/*
600 * Generic EC key
601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200603{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( type == MBEDTLS_PK_ECKEY ||
605 type == MBEDTLS_PK_ECKEY_DH ||
606 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200607}
608
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200609static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200612}
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200615/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200617 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200618 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200621 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200622 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200623 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200626 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200627 const unsigned char *sig, size_t sig_len )
628{
Janos Follath24eed8d2019-11-22 13:21:35 +0000629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200635 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200638
639 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200640}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200643 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200644 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200645 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
646{
Janos Follath24eed8d2019-11-22 13:21:35 +0000647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200653 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
654 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200655 f_rng, p_rng );
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200658
659 return( ret );
660}
661
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200662#if defined(MBEDTLS_ECP_RESTARTABLE)
663/* Forward declarations */
664static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
665 const unsigned char *hash, size_t hash_len,
666 const unsigned char *sig, size_t sig_len,
667 void *rs_ctx );
668
669static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
670 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200671 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200672 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
673 void *rs_ctx );
674
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200675/*
676 * Restart context for ECDSA operations with ECKEY context
677 *
678 * We need to store an actual ECDSA context, as we need to pass the same to
679 * the underlying ecdsa function, so we can't create it on the fly every time.
680 */
681typedef struct
682{
683 mbedtls_ecdsa_restart_ctx ecdsa_rs;
684 mbedtls_ecdsa_context ecdsa_ctx;
685} eckey_restart_ctx;
686
687static void *eckey_rs_alloc( void )
688{
689 eckey_restart_ctx *rs_ctx;
690
691 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
692
693 if( ctx != NULL )
694 {
695 rs_ctx = ctx;
696 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
697 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
698 }
699
700 return( ctx );
701}
702
703static void eckey_rs_free( void *ctx )
704{
705 eckey_restart_ctx *rs_ctx;
706
707 if( ctx == NULL)
708 return;
709
710 rs_ctx = ctx;
711 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
712 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
713
714 mbedtls_free( ctx );
715}
716
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200717static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
718 const unsigned char *hash, size_t hash_len,
719 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200720 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200721{
Janos Follath24eed8d2019-11-22 13:21:35 +0000722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200723 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200724
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200725 /* Should never happen */
726 if( rs == NULL )
727 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200728
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200729 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200730 if( rs->ecdsa_ctx.grp.pbits == 0 )
731 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200732
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200733 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
734 md_alg, hash, hash_len,
735 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200736
737cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200738 return( ret );
739}
740
741static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
742 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200743 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200744 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200745 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200746{
Janos Follath24eed8d2019-11-22 13:21:35 +0000747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200748 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200749
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200750 /* Should never happen */
751 if( rs == NULL )
752 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200753
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200754 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200755 if( rs->ecdsa_ctx.grp.pbits == 0 )
756 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200757
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200758 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200759 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200760 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200761
762cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200763 return( ret );
764}
765#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200767
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200768static int eckey_check_pair( const void *pub, const void *prv,
769 int (*f_rng)(void *, unsigned char *, size_t),
770 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200773 (const mbedtls_ecp_keypair *) prv,
774 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100775}
776
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200777static void *eckey_alloc_wrap( void )
778{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200779 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200780
781 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200783
784 return( ctx );
785}
786
787static void eckey_free_wrap( void *ctx )
788{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
790 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200791}
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200794{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200796 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200798}
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800const mbedtls_pk_info_t mbedtls_eckey_info = {
801 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200802 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200803 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200804 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200806 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200807 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200808#if defined(MBEDTLS_ECP_RESTARTABLE)
809 eckey_verify_rs_wrap,
810 eckey_sign_rs_wrap,
811#endif
812#else /* MBEDTLS_ECDSA_C */
813 NULL,
814 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200815#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200816 NULL,
817 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100818 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200819 eckey_alloc_wrap,
820 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200821#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200822 eckey_rs_alloc,
823 eckey_rs_free,
824#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200825 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200826};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200827
828/*
Paul Bakker75342a62014-04-08 17:35:40 +0200829 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200832{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 return( type == MBEDTLS_PK_ECKEY ||
834 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200835}
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837const mbedtls_pk_info_t mbedtls_eckeydh_info = {
838 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200839 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200840 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200841 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200842 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200843 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200844#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200845 NULL,
846 NULL,
847#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200848 NULL,
849 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100850 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200851 eckey_alloc_wrap, /* Same underlying key structure */
852 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200853#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200854 NULL,
855 NULL,
856#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200857 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200858};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_ECDSA_C)
862static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200863{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200865}
866
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400867#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400868/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500869 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
870 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500871 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500872static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
873 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500874{
Janos Follath24eed8d2019-11-22 13:21:35 +0000875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500876 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500877
Andrzej Kurek9241d182018-11-20 05:04:35 -0500878 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500879 MBEDTLS_ASN1_INTEGER ) ) != 0 )
880 {
881 return( ret );
882 }
883
Andrzej Kurek9241d182018-11-20 05:04:35 -0500884 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500885 {
886 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500887 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500888 }
889
Andrzej Kurek9241d182018-11-20 05:04:35 -0500890 if( unpadded_len > to_len || unpadded_len == 0 )
891 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500892
Andrzej Kurek9241d182018-11-20 05:04:35 -0500893 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500894 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500895 memcpy( to + padding_len, *from, unpadded_len );
896 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500897
Andrzej Kurek9241d182018-11-20 05:04:35 -0500898 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500899}
900
901/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400902 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500903 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500904 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400905 */
906static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500907 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400908{
Janos Follath24eed8d2019-11-22 13:21:35 +0000909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500910 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500911
Andrzej Kurek9241d182018-11-20 05:04:35 -0500912 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500913 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500914 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400915
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500916 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500917 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
918 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500919 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500920 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
921 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500922
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400923 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400924}
925
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100926static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400927 const unsigned char *hash, size_t hash_len,
928 const unsigned char *sig, size_t sig_len )
929{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100930 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100933 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200934 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400935 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400936 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100937 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000938 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400939 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700940 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100941 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100942 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100943 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
944 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700945 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400946
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500947 if( curve == 0 )
948 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
949
Hanno Beckerccf574e2019-01-29 08:26:15 +0000950 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500951 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400952 key.pk_info = &pk_info;
953 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000954 p = buf + sizeof( buf );
955 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400956 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500957 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400958
Gilles Peskined2d45c12019-05-27 14:53:13 +0200959 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100960 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200961 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500962
Gilles Peskined2d45c12019-05-27 14:53:13 +0200963 status = psa_import_key( &attributes,
964 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200965 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200966 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400967 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100968 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400969 goto cleanup;
970 }
971
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500972 /* We don't need the exported key anymore and can
973 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500974 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500975 {
976 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
977 goto cleanup;
978 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500979
Hanno Beckera9851112019-01-25 16:37:10 +0000980 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500981 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500982 signature_part_size ) ) != 0 )
983 {
984 goto cleanup;
985 }
986
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100987 status = psa_verify_hash( key_id, psa_sig_md,
988 hash, hash_len,
989 buf, 2 * signature_part_size );
990 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400991 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800992 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400993 goto cleanup;
994 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500995
996 if( p != sig + sig_len )
997 {
998 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
999 goto cleanup;
1000 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001001 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001002
Andrzej Kurekb7b04782018-11-19 17:01:16 -05001003cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +01001004 status = psa_destroy_key( key_id );
1005 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +01001006 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +01001007
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001008 return( ret );
1009}
1010#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001012 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001013 const unsigned char *sig, size_t sig_len )
1014{
Janos Follath24eed8d2019-11-22 13:21:35 +00001015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001016 ((void) md_alg);
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001019 hash, hash_len, sig, sig_len );
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1022 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001023
1024 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001025}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001026#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001027
Neil Armstronge9606902022-02-09 14:23:00 +01001028#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001029/*
1030 * Simultaneously convert and move raw MPI from the beginning of a buffer
1031 * to an ASN.1 MPI at the end of the buffer.
1032 * See also mbedtls_asn1_write_mpi().
1033 *
1034 * p: pointer to the end of the output buffer
1035 * start: start of the output buffer, and also of the mpi to write at the end
1036 * n_len: length of the mpi to read from start
1037 */
1038static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1039 size_t n_len )
1040{
1041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1042 size_t len = 0;
1043
1044 if( (size_t)( *p - start ) < n_len )
1045 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1046
1047 len = n_len;
1048 *p -= len;
1049 memmove( *p, start, len );
1050
1051 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1052 * Neither r nor s should be 0, but as a failsafe measure, still detect
1053 * that rather than overflowing the buffer in case of a PSA error. */
1054 while( len > 0 && **p == 0x00 )
1055 {
1056 ++(*p);
1057 --len;
1058 }
1059
1060 /* this is only reached if the signature was invalid */
1061 if( len == 0 )
1062 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1063
1064 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1065 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1066 if( **p & 0x80 )
1067 {
1068 if( *p - start < 1 )
1069 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1070
1071 *--(*p) = 0x00;
1072 len += 1;
1073 }
1074
1075 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1076 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1077 MBEDTLS_ASN1_INTEGER ) );
1078
1079 return( (int) len );
1080}
1081
1082/* Transcode signature from PSA format to ASN.1 sequence.
1083 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1084 * MPIs, and in-place.
1085 *
1086 * [in/out] sig: the signature pre- and post-transcoding
1087 * [in/out] sig_len: signature length pre- and post-transcoding
1088 * [int] buf_len: the available size the in/out buffer
1089 */
Neil Armstronge9606902022-02-09 14:23:00 +01001090static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001091 size_t buf_len )
1092{
1093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1094 size_t len = 0;
1095 const size_t rs_len = *sig_len / 2;
1096 unsigned char *p = sig + buf_len;
1097
1098 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1099 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1100
1101 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1102 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1103 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1104
1105 memmove( sig, p, len );
1106 *sig_len = len;
1107
1108 return( 0 );
1109}
Neil Armstronge9606902022-02-09 14:23:00 +01001110
Neil Armstrong17a06552022-03-18 15:27:38 +01001111/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1112 *
1113 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1114 * [in] end: ASN.1 buffer end
1115 * [out] key_len: the ECDSA privateKey length in bytes
1116 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001117static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1118 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001119{
1120 size_t len;
1121 int ret;
1122
Neil Armstrong17a06552022-03-18 15:27:38 +01001123 /*
1124 * RFC 5915, or SEC1 Appendix C.4
1125 *
1126 * ECPrivateKey ::= SEQUENCE {
1127 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1128 * privateKey OCTET STRING,
1129 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1130 * publicKey [1] BIT STRING OPTIONAL
1131 * }
1132 */
1133
Neil Armstronge9606902022-02-09 14:23:00 +01001134 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001135 MBEDTLS_ASN1_CONSTRUCTED |
1136 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001137 return( ret );
1138
1139 /* version */
1140 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1141 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1142 return( ret );
1143
1144 *buf += len;
1145
1146 /* privateKey */
1147 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1148 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1149 return( ret );
1150
1151 *key_len = len;
1152
1153 return 0;
1154}
1155
1156static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1157 const unsigned char *hash, size_t hash_len,
1158 unsigned char *sig, size_t sig_size, size_t *sig_len,
1159 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1160{
1161 mbedtls_ecdsa_context *ctx = ctx_arg;
1162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1164 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1165 psa_status_t status;
1166 mbedtls_pk_context key;
1167 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001168 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001169 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001170 psa_algorithm_t psa_sig_md =
1171 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001172 size_t curve_bits;
1173 psa_ecc_family_t curve =
1174 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001175
1176 /* PSA has its own RNG */
1177 ((void) f_rng);
1178 ((void) p_rng);
1179
1180 if( curve == 0 )
1181 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1182
1183 /* mbedtls_pk_write_key_der() expects a full PK context;
1184 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001185 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001186 key.pk_ctx = ctx;
1187 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1188 if( key_len <= 0 )
1189 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1190
1191 p = buf + sizeof( buf ) - key_len;
1192 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1193 if( ret != 0 )
1194 goto cleanup;
1195
1196 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1197 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1198 psa_set_key_algorithm( &attributes, psa_sig_md );
1199
1200 status = psa_import_key( &attributes,
1201 p, key_len,
1202 &key_id );
1203 if( status != PSA_SUCCESS )
1204 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001205 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001206 goto cleanup;
1207 }
1208
Neil Armstronge4edcf72022-03-03 16:46:41 +01001209 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1210 sig, sig_size, sig_len );
1211 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001212 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001213 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001214 goto cleanup;
1215 }
1216
1217 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1218
1219cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001220 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001221 status = psa_destroy_key( key_id );
1222 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001223 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001224
Neil Armstronge9606902022-02-09 14:23:00 +01001225 return( ret );
1226}
1227#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001229 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001230 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001231 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1232{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001234 md_alg, hash, hash_len,
1235 sig, sig_size, sig_len,
1236 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001237}
Neil Armstronge9606902022-02-09 14:23:00 +01001238#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001239
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001240#if defined(MBEDTLS_ECP_RESTARTABLE)
1241static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1242 const unsigned char *hash, size_t hash_len,
1243 const unsigned char *sig, size_t sig_len,
1244 void *rs_ctx )
1245{
Janos Follath24eed8d2019-11-22 13:21:35 +00001246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001247 ((void) md_alg);
1248
1249 ret = mbedtls_ecdsa_read_signature_restartable(
1250 (mbedtls_ecdsa_context *) ctx,
1251 hash, hash_len, sig, sig_len,
1252 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1253
1254 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1255 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1256
1257 return( ret );
1258}
1259
1260static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1261 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001262 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001263 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1264 void *rs_ctx )
1265{
1266 return( mbedtls_ecdsa_write_signature_restartable(
1267 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001268 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001269 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1270
1271}
1272#endif /* MBEDTLS_ECP_RESTARTABLE */
1273
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001274static void *ecdsa_alloc_wrap( void )
1275{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001276 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001277
1278 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001280
1281 return( ctx );
1282}
1283
1284static void ecdsa_free_wrap( void *ctx )
1285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1287 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001288}
1289
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001290#if defined(MBEDTLS_ECP_RESTARTABLE)
1291static void *ecdsa_rs_alloc( void )
1292{
1293 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1294
1295 if( ctx != NULL )
1296 mbedtls_ecdsa_restart_init( ctx );
1297
1298 return( ctx );
1299}
1300
1301static void ecdsa_rs_free( void *ctx )
1302{
1303 mbedtls_ecdsa_restart_free( ctx );
1304 mbedtls_free( ctx );
1305}
1306#endif /* MBEDTLS_ECP_RESTARTABLE */
1307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1309 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001310 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001311 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001312 ecdsa_can_do,
1313 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001314 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001315#if defined(MBEDTLS_ECP_RESTARTABLE)
1316 ecdsa_verify_rs_wrap,
1317 ecdsa_sign_rs_wrap,
1318#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001319 NULL,
1320 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001321 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001322 ecdsa_alloc_wrap,
1323 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001324#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001325 ecdsa_rs_alloc,
1326 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001327#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001328 eckey_debug, /* Compatible key structures */
1329};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001333/*
1334 * Support for alternative RSA-private implementations
1335 */
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001340}
1341
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001342static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001343{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001345
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001346 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001347}
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001350 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001351 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001352 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1353{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001355
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001356#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001357 if( UINT_MAX < hash_len )
1358 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001359#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001360
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001361 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001362 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1363 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001364 if( *sig_len > sig_size )
1365 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001366
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001367 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001368 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001369}
1370
1371static int rsa_alt_decrypt_wrap( void *ctx,
1372 const unsigned char *input, size_t ilen,
1373 unsigned char *output, size_t *olen, size_t osize,
1374 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001377
1378 ((void) f_rng);
1379 ((void) p_rng);
1380
1381 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001383
1384 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001385 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001386}
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001389static int rsa_alt_check_pair( const void *pub, const void *prv,
1390 int (*f_rng)(void *, unsigned char *, size_t),
1391 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001392{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001394 unsigned char hash[32];
1395 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001397
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001398 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001400
1401 memset( hash, 0x2a, sizeof( hash ) );
1402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001404 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001405 sig, sizeof( sig ), &sig_len,
1406 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001407 {
1408 return( ret );
1409 }
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001412 hash, sizeof( hash ), sig, sig_len ) != 0 )
1413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001415 }
1416
1417 return( 0 );
1418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001420
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001421static void *rsa_alt_alloc_wrap( void )
1422{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001423 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001424
1425 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001427
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001428 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001429}
1430
1431static void rsa_alt_free_wrap( void *ctx )
1432{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001433 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001435}
1436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1438 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001439 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001440 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001441 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001442 NULL,
1443 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001444#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001445 NULL,
1446 NULL,
1447#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001448 rsa_alt_decrypt_wrap,
1449 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001451 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001452#else
1453 NULL,
1454#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001455 rsa_alt_alloc_wrap,
1456 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001457#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001458 NULL,
1459 NULL,
1460#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001461 NULL,
1462};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001465
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001466#if defined(MBEDTLS_USE_PSA_CRYPTO)
1467
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001468static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001469{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001470 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001471
1472 /* no _init() function to call, an calloc() already zeroized */
1473
1474 return( ctx );
1475}
1476
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001477static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001478{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001479 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001480 mbedtls_free( ctx );
1481}
1482
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001483static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001484{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001485 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001486 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001488
Gilles Peskined2d45c12019-05-27 14:53:13 +02001489 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001490 return( 0 );
1491
Gilles Peskined2d45c12019-05-27 14:53:13 +02001492 bits = psa_get_key_bits( &attributes );
1493 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001494 return( bits );
1495}
1496
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001497static int pk_opaque_ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001498{
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001499 return( type == MBEDTLS_PK_ECKEY ||
1500 type == MBEDTLS_PK_ECDSA );
1501}
1502
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001503static int pk_opaque_rsa_can_do( mbedtls_pk_type_t type )
1504{
Neil Armstrong62d452b2022-04-12 15:11:49 +02001505 return( type == MBEDTLS_PK_RSA ||
1506 type == MBEDTLS_PK_RSASSA_PSS );
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001507}
1508
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001509static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1510 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001511 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001512 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1513{
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001514#if !defined(MBEDTLS_ECDSA_C) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001515 ((void) ctx);
1516 ((void) md_alg);
1517 ((void) hash);
1518 ((void) hash_len);
1519 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001520 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001521 ((void) sig_len);
1522 ((void) f_rng);
1523 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001524 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001525#else /* !MBEDTLS_ECDSA_C && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001526 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001528 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001529 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001530 psa_status_t status;
1531
1532 /* PSA has its own RNG */
1533 (void) f_rng;
1534 (void) p_rng;
1535
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001536 status = psa_get_key_attributes( *key, &attributes );
1537 if( status != PSA_SUCCESS )
1538 return( mbedtls_pk_error_from_psa( status ) );
1539
1540 type = psa_get_key_type( &attributes );
1541 psa_reset_key_attributes( &attributes );
1542
1543#if defined(MBEDTLS_ECDSA_C)
1544 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
1545 alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
1546 else
1547#endif /* MBEDTLS_ECDSA_C */
1548#if defined(MBEDTLS_RSA_C)
1549 if( PSA_KEY_TYPE_IS_RSA( type ) )
1550 alg = PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_psa_translate_md( md_alg ) );
1551 else
1552#endif /* MBEDTLS_RSA_C */
1553 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1554
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001555 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001556 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001557 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001558 if( status != PSA_SUCCESS )
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001559 {
1560#if defined(MBEDTLS_ECDSA_C)
1561 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
1562 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
1563 else
1564#endif /* MBEDTLS_ECDSA_C */
1565#if defined(MBEDTLS_RSA_C)
1566 if( PSA_KEY_TYPE_IS_RSA( type ) )
1567 return( mbedtls_pk_error_from_psa_rsa( status ) );
1568 else
1569#endif /* MBEDTLS_RSA_C */
1570 return( mbedtls_pk_error_from_psa( status ) );
1571 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001572
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001573#if defined(MBEDTLS_ECDSA_C)
1574 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
1575 /* transcode it to ASN.1 sequence */
1576 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
1577#endif /* MBEDTLS_ECDSA_C */
1578
1579 return 0;
1580#endif /* !MBEDTLS_ECDSA_C && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001581}
1582
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001583const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001584 MBEDTLS_PK_OPAQUE,
1585 "Opaque",
1586 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001587 pk_opaque_ecdsa_can_do,
1588 NULL, /* verify - will be done later */
1589 pk_opaque_sign_wrap,
1590#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1591 NULL, /* restartable verify - not relevant */
1592 NULL, /* restartable sign - not relevant */
1593#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001594 NULL, /* decrypt - not relevant */
1595 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001596 NULL, /* check_pair - could be done later or left NULL */
1597 pk_opaque_alloc_wrap,
1598 pk_opaque_free_wrap,
1599#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1600 NULL, /* restart alloc - not relevant */
1601 NULL, /* restart free - not relevant */
1602#endif
1603 NULL, /* debug - could be done later, or even left NULL */
1604};
1605
Neil Armstrong30beca32022-05-03 15:42:13 +02001606#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001607static int pk_opaque_rsa_decrypt( void *ctx,
1608 const unsigned char *input, size_t ilen,
1609 unsigned char *output, size_t *olen, size_t osize,
1610 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1611{
1612 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1613 psa_status_t status;
1614
1615 /* PSA has its own RNG */
1616 (void) f_rng;
1617 (void) p_rng;
1618
1619 status = psa_asymmetric_decrypt( *key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1620 input, ilen,
1621 NULL, 0,
1622 output, osize, olen );
1623 if( status != PSA_SUCCESS )
1624 {
1625 return( mbedtls_pk_error_from_psa_rsa( status ) );
1626 }
1627
1628 return 0;
1629}
Neil Armstrong30beca32022-05-03 15:42:13 +02001630#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001631
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001632const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1633 MBEDTLS_PK_OPAQUE,
1634 "Opaque",
1635 pk_opaque_get_bitlen,
1636 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001637 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001638 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001639#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1640 NULL, /* restartable verify - not relevant */
1641 NULL, /* restartable sign - not relevant */
1642#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001643#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001644 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001645#else
1646 NULL, /* decrypt - not available */
1647#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001648 NULL, /* encrypt - will be done later */
1649 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001650 pk_opaque_alloc_wrap,
1651 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001652#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1653 NULL, /* restart alloc - not relevant */
1654 NULL, /* restart free - not relevant */
1655#endif
1656 NULL, /* debug - could be done later, or even left NULL */
1657};
1658
1659#endif /* MBEDTLS_USE_PSA_CRYPTO */
1660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_PK_C */