blob: 2d4f4f2218e7b33c11ffa9f35571d0e304781db7 [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é-Gonnardabac0372022-07-18 13:41:11 +020055#include "hash_info.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010056#endif
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020060#else
61#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020062#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020064#endif
65
Andres AG72849872017-01-19 11:24:33 +000066#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010067#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020068
Jerry Yub02ee182022-03-16 10:30:41 +080069#if defined(MBEDTLS_PSA_CRYPTO_C)
Neil Armstrong19915c22022-03-01 15:21:02 +010070int mbedtls_pk_error_from_psa( psa_status_t status )
71{
72 switch( status )
73 {
74 case PSA_SUCCESS:
75 return( 0 );
76 case PSA_ERROR_INVALID_HANDLE:
77 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
78 case PSA_ERROR_NOT_PERMITTED:
79 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
80 case PSA_ERROR_BUFFER_TOO_SMALL:
81 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
82 case PSA_ERROR_NOT_SUPPORTED:
83 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
84 case PSA_ERROR_INVALID_ARGUMENT:
85 return( MBEDTLS_ERR_PK_INVALID_ALG );
86 case PSA_ERROR_INSUFFICIENT_MEMORY:
87 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
88 case PSA_ERROR_BAD_STATE:
89 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
90 case PSA_ERROR_COMMUNICATION_FAILURE:
91 case PSA_ERROR_HARDWARE_FAILURE:
92 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
93 case PSA_ERROR_DATA_CORRUPT:
94 case PSA_ERROR_DATA_INVALID:
95 case PSA_ERROR_STORAGE_FAILURE:
96 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
97 case PSA_ERROR_CORRUPTION_DETECTED:
98 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
99 default:
100 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
101 }
102}
103
Neil Armstrong30beca32022-05-03 15:42:13 +0200104#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
105 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong19915c22022-03-01 15:21:02 +0100106int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
107{
108 switch( status )
109 {
110 case PSA_ERROR_NOT_PERMITTED:
111 case PSA_ERROR_INVALID_ARGUMENT:
112 case PSA_ERROR_INVALID_HANDLE:
113 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
114 case PSA_ERROR_BUFFER_TOO_SMALL:
115 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
116 case PSA_ERROR_INSUFFICIENT_ENTROPY:
117 return( MBEDTLS_ERR_RSA_RNG_FAILED );
118 case PSA_ERROR_INVALID_SIGNATURE:
119 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
120 case PSA_ERROR_INVALID_PADDING:
121 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
122 default:
123 return( mbedtls_pk_error_from_psa( status ) );
124 }
125}
Neil Armstrong30beca32022-05-03 15:42:13 +0200126#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800127
128#endif /* MBEDTLS_PSA_CRYPTO_C */
129
130#if defined(MBEDTLS_USE_PSA_CRYPTO)
131
132#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Jerry Yu848ecce2022-03-22 10:58:48 +0800133int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status )
Jerry Yu07869e82022-03-16 16:40:50 +0800134{
135 switch( status )
136 {
137 case PSA_ERROR_NOT_PERMITTED:
138 case PSA_ERROR_INVALID_ARGUMENT:
139 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
140 case PSA_ERROR_INVALID_HANDLE:
141 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
142 case PSA_ERROR_BUFFER_TOO_SMALL:
143 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
144 case PSA_ERROR_INSUFFICIENT_ENTROPY:
145 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
146 case PSA_ERROR_INVALID_SIGNATURE:
147 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
148 default:
149 return( mbedtls_pk_error_from_psa( status ) );
150 }
151}
Jerry Yu75339822022-03-23 12:06:31 +0800152#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800153
Jerry Yu75339822022-03-23 12:06:31 +0800154#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_RSA_C)
157static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 return( type == MBEDTLS_PK_RSA ||
160 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200161}
162
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200163static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200164{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100165 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
166 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200167}
168
Neil Armstrong52f41f82022-02-22 15:30:24 +0100169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200171 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200172 const unsigned char *sig, size_t sig_len )
173{
Neil Armstrong52f41f82022-02-22 15:30:24 +0100174 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
177 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
178 psa_status_t status;
179 mbedtls_pk_context key;
180 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100181 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100182 psa_algorithm_t psa_alg_md =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200183 PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100184 size_t rsa_len = mbedtls_rsa_get_len( rsa );
185
186#if SIZE_MAX > UINT_MAX
187 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
188 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
189#endif /* SIZE_MAX > UINT_MAX */
190
191 if( sig_len < rsa_len )
192 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
193
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100194 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100195 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100196 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100197 key.pk_ctx = ctx;
198 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
199 if( key_len <= 0 )
200 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
201
202 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
203 psa_set_key_algorithm( &attributes, psa_alg_md );
204 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
205
206 status = psa_import_key( &attributes,
207 buf + sizeof( buf ) - key_len, key_len,
208 &key_id );
209 if( status != PSA_SUCCESS )
210 {
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100211 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100212 goto cleanup;
213 }
214
215 status = psa_verify_hash( key_id, psa_alg_md, hash, hash_len,
216 sig, sig_len );
217 if( status != PSA_SUCCESS )
218 {
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100219 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100220 goto cleanup;
221 }
222 ret = 0;
223
224cleanup:
Neil Armstronga33280a2022-02-24 15:17:47 +0100225 status = psa_destroy_key( key_id );
226 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100227 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronga33280a2022-02-24 15:17:47 +0100228
Neil Armstrong52f41f82022-02-22 15:30:24 +0100229 return( ret );
230}
231#else
232static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
233 const unsigned char *hash, size_t hash_len,
234 const unsigned char *sig, size_t sig_len )
235{
Janos Follath24eed8d2019-11-22 13:21:35 +0000236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100237 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
238 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200239
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100240#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000241 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
242 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100243#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000244
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100245 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200247
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100248 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
249 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100250 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200251 return( ret );
252
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200253 /* The buffer contains a valid signature followed by extra data.
254 * We have a special error code for that so that so that callers can
255 * use mbedtls_pk_verify() to check "Does the buffer start with a
256 * valid signature?" and not just "Does the buffer contain a valid
257 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100258 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200260
261 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200262}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100263#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200264
Jerry Yub02ee182022-03-16 10:30:41 +0800265#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yue010de42022-03-23 11:45:55 +0800266int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
267 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800268 const unsigned char *hash, size_t hash_len,
269 unsigned char *sig, size_t sig_size,
270 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100271{
Neil Armstrong98545682022-02-22 16:12:51 +0100272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
274 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
275 psa_status_t status;
276 mbedtls_pk_context key;
277 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100278 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100279 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100280
Jerry Yue010de42022-03-23 11:45:55 +0800281 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100282 if( sig_size < *sig_len )
283 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
284
Neil Armstronge4f28682022-02-24 15:41:39 +0100285 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100286 * re-construct one to make it happy */
287 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800288 key.pk_ctx = rsa_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100289 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
290 if( key_len <= 0 )
291 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100292 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800293 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100294 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
295
296 status = psa_import_key( &attributes,
297 buf + sizeof( buf ) - key_len, key_len,
298 &key_id );
299 if( status != PSA_SUCCESS )
300 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100301 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100302 goto cleanup;
303 }
Jerry Yubf455e72022-03-22 21:39:41 +0800304 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100305 sig, sig_size, sig_len );
306 if( status != PSA_SUCCESS )
307 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100308 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100309 goto cleanup;
310 }
311
312 ret = 0;
313
314cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100315 status = psa_destroy_key( key_id );
316 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100317 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100318 return( ret );
319}
Jerry Yu406cf272022-03-22 11:33:42 +0800320#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800321
322#if defined(MBEDTLS_USE_PSA_CRYPTO)
323static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
324 const unsigned char *hash, size_t hash_len,
325 unsigned char *sig, size_t sig_size, size_t *sig_len,
326 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
327{
Jerry Yu1d172a32022-03-12 19:12:05 +0800328 ((void) f_rng);
329 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800330
Jerry Yubd1b3272022-03-24 13:05:20 +0800331 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200332 psa_md_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yubd1b3272022-03-24 13:05:20 +0800333 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800334 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800335
Jerry Yubd1b3272022-03-24 13:05:20 +0800336 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
337 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800338 ctx, hash, hash_len,
339 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800340}
Neil Armstrong98545682022-02-22 16:12:51 +0100341#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200343 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200344 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200345 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
346{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100347 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
348
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100349#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000350 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
351 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100352#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000353
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100354 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200355 if( sig_size < *sig_len )
356 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200357
Thomas Daubney140184d2021-05-18 16:04:07 +0100358 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
359 md_alg, (unsigned int) hash_len,
360 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200361}
Neil Armstrong98545682022-02-22 16:12:51 +0100362#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200363
Neil Armstrong18f43c72022-02-09 15:32:45 +0100364#if defined(MBEDTLS_USE_PSA_CRYPTO)
365static int rsa_decrypt_wrap( void *ctx,
366 const unsigned char *input, size_t ilen,
367 unsigned char *output, size_t *olen, size_t osize,
368 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
369{
370 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
373 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
374 psa_status_t status;
375 mbedtls_pk_context key;
376 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100377 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100378
379 ((void) f_rng);
380 ((void) p_rng);
381
382#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100383 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
384 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
385#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100386
387 if( ilen != mbedtls_rsa_get_len( rsa ) )
388 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
389
390 /* mbedtls_pk_write_key_der() expects a full PK context;
391 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100392 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100393 key.pk_ctx = ctx;
394 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
395 if( key_len <= 0 )
396 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
397
398 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
399 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100400 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100401
402 status = psa_import_key( &attributes,
403 buf + sizeof( buf ) - key_len, key_len,
404 &key_id );
405 if( status != PSA_SUCCESS )
406 {
Neil Armstronge8780492022-03-03 16:54:16 +0100407 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100408 goto cleanup;
409 }
410
Neil Armstrong8e805042022-03-16 15:30:31 +0100411 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
412 input, ilen,
413 NULL, 0,
414 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100415 if( status != PSA_SUCCESS )
416 {
Neil Armstronge8780492022-03-03 16:54:16 +0100417 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100418 goto cleanup;
419 }
420
421 ret = 0;
422
423cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100424 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100425 status = psa_destroy_key( key_id );
426 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100427 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100428
Neil Armstrong18f43c72022-02-09 15:32:45 +0100429 return( ret );
430}
431#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200432static int rsa_decrypt_wrap( void *ctx,
433 const unsigned char *input, size_t ilen,
434 unsigned char *output, size_t *olen, size_t osize,
435 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
436{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100437 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
438
439 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200441
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100442 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100443 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200444}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100445#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200446
Neil Armstrong96a16a42022-02-10 10:40:11 +0100447#if defined(MBEDTLS_USE_PSA_CRYPTO)
448static int rsa_encrypt_wrap( void *ctx,
449 const unsigned char *input, size_t ilen,
450 unsigned char *output, size_t *olen, size_t osize,
451 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
452{
453 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
456 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
457 psa_status_t status;
458 mbedtls_pk_context key;
459 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100460 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100461
462 ((void) f_rng);
463 ((void) p_rng);
464
465#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100466 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
467 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100468#endif
469
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100470 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100471 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
472
Neil Armstrongac014ca2022-02-24 15:27:54 +0100473 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100474 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100475 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100476 key.pk_ctx = ctx;
477 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
478 if( key_len <= 0 )
479 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
480
481 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100482 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100483 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
484
485 status = psa_import_key( &attributes,
486 buf + sizeof( buf ) - key_len, key_len,
487 &key_id );
488 if( status != PSA_SUCCESS )
489 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100490 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100491 goto cleanup;
492 }
493
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100494 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
495 input, ilen,
496 NULL, 0,
497 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100498 if( status != PSA_SUCCESS )
499 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100500 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100501 goto cleanup;
502 }
503
504 ret = 0;
505
506cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100507 status = psa_destroy_key( key_id );
508 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100509 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100510
Neil Armstrong96a16a42022-02-10 10:40:11 +0100511 return( ret );
512}
513#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200514static int rsa_encrypt_wrap( void *ctx,
515 const unsigned char *input, size_t ilen,
516 unsigned char *output, size_t *olen, size_t osize,
517 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
518{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100519 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
520 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200521
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100522 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100524
Thomas Daubney21772772021-05-13 17:30:32 +0100525 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100526 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200527}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100528#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200529
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200530static int rsa_check_pair_wrap( const void *pub, const void *prv,
531 int (*f_rng)(void *, unsigned char *, size_t),
532 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100533{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200534 (void) f_rng;
535 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
537 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100538}
539
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200540static void *rsa_alloc_wrap( void )
541{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200542 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200543
544 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200545 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200546
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200547 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200548}
549
550static void rsa_free_wrap( void *ctx )
551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
553 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200554}
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200557{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200558#if defined(MBEDTLS_RSA_ALT)
559 /* Not supported */
560 (void) ctx;
561 (void) items;
562#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200564 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200566
567 items++;
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200570 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200572#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200573}
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575const mbedtls_pk_info_t mbedtls_rsa_info = {
576 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200577 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200578 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200579 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200580 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200581 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200582#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200583 NULL,
584 NULL,
585#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200586 rsa_decrypt_wrap,
587 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100588 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200589 rsa_alloc_wrap,
590 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200591#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200592 NULL,
593 NULL,
594#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200595 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200596};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200600/*
601 * Generic EC key
602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200604{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( type == MBEDTLS_PK_ECKEY ||
606 type == MBEDTLS_PK_ECKEY_DH ||
607 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200608}
609
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200610static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200611{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200613}
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200616/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200618 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200619 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200622 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200623 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200624 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200627 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200628 const unsigned char *sig, size_t sig_len )
629{
Janos Follath24eed8d2019-11-22 13:21:35 +0000630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200636 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200639
640 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200641}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200644 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200645 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200646 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
647{
Janos Follath24eed8d2019-11-22 13:21:35 +0000648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200654 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
655 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200656 f_rng, p_rng );
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200659
660 return( ret );
661}
662
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200663#if defined(MBEDTLS_ECP_RESTARTABLE)
664/* Forward declarations */
665static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
666 const unsigned char *hash, size_t hash_len,
667 const unsigned char *sig, size_t sig_len,
668 void *rs_ctx );
669
670static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
671 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200672 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200673 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
674 void *rs_ctx );
675
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200676/*
677 * Restart context for ECDSA operations with ECKEY context
678 *
679 * We need to store an actual ECDSA context, as we need to pass the same to
680 * the underlying ecdsa function, so we can't create it on the fly every time.
681 */
682typedef struct
683{
684 mbedtls_ecdsa_restart_ctx ecdsa_rs;
685 mbedtls_ecdsa_context ecdsa_ctx;
686} eckey_restart_ctx;
687
688static void *eckey_rs_alloc( void )
689{
690 eckey_restart_ctx *rs_ctx;
691
692 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
693
694 if( ctx != NULL )
695 {
696 rs_ctx = ctx;
697 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
698 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
699 }
700
701 return( ctx );
702}
703
704static void eckey_rs_free( void *ctx )
705{
706 eckey_restart_ctx *rs_ctx;
707
708 if( ctx == NULL)
709 return;
710
711 rs_ctx = ctx;
712 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
713 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
714
715 mbedtls_free( ctx );
716}
717
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200718static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
719 const unsigned char *hash, size_t hash_len,
720 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200721 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200722{
Janos Follath24eed8d2019-11-22 13:21:35 +0000723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200724 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200725
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200726 /* Should never happen */
727 if( rs == NULL )
728 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200729
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200730 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200731 if( rs->ecdsa_ctx.grp.pbits == 0 )
732 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200733
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200734 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
735 md_alg, hash, hash_len,
736 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200737
738cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200739 return( ret );
740}
741
742static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
743 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200744 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200745 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200746 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200747{
Janos Follath24eed8d2019-11-22 13:21:35 +0000748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200749 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200750
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200751 /* Should never happen */
752 if( rs == NULL )
753 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200754
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200755 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200756 if( rs->ecdsa_ctx.grp.pbits == 0 )
757 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200758
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200759 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200760 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200761 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200762
763cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200764 return( ret );
765}
766#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200768
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200769static int eckey_check_pair( const void *pub, const void *prv,
770 int (*f_rng)(void *, unsigned char *, size_t),
771 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200774 (const mbedtls_ecp_keypair *) prv,
775 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100776}
777
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200778static void *eckey_alloc_wrap( void )
779{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200780 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200781
782 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200784
785 return( ctx );
786}
787
788static void eckey_free_wrap( void *ctx )
789{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
791 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200792}
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200795{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200797 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200799}
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801const mbedtls_pk_info_t mbedtls_eckey_info = {
802 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200803 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200804 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200805 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200807 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200808 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200809#if defined(MBEDTLS_ECP_RESTARTABLE)
810 eckey_verify_rs_wrap,
811 eckey_sign_rs_wrap,
812#endif
813#else /* MBEDTLS_ECDSA_C */
814 NULL,
815 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200816#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200817 NULL,
818 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100819 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200820 eckey_alloc_wrap,
821 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200822#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200823 eckey_rs_alloc,
824 eckey_rs_free,
825#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200826 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200827};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200828
829/*
Paul Bakker75342a62014-04-08 17:35:40 +0200830 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 return( type == MBEDTLS_PK_ECKEY ||
835 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200836}
837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838const mbedtls_pk_info_t mbedtls_eckeydh_info = {
839 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200840 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200841 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200842 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200843 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200844 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200845#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200846 NULL,
847 NULL,
848#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200849 NULL,
850 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100851 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200852 eckey_alloc_wrap, /* Same underlying key structure */
853 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200854#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200855 NULL,
856 NULL,
857#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200858 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200859};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_ECDSA_C)
863static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200864{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200866}
867
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400868#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400869/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500870 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
871 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500872 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500873static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
874 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500875{
Janos Follath24eed8d2019-11-22 13:21:35 +0000876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500877 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500878
Andrzej Kurek9241d182018-11-20 05:04:35 -0500879 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500880 MBEDTLS_ASN1_INTEGER ) ) != 0 )
881 {
882 return( ret );
883 }
884
Andrzej Kurek9241d182018-11-20 05:04:35 -0500885 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500886 {
887 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500888 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500889 }
890
Andrzej Kurek9241d182018-11-20 05:04:35 -0500891 if( unpadded_len > to_len || unpadded_len == 0 )
892 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500893
Andrzej Kurek9241d182018-11-20 05:04:35 -0500894 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500895 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500896 memcpy( to + padding_len, *from, unpadded_len );
897 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500898
Andrzej Kurek9241d182018-11-20 05:04:35 -0500899 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500900}
901
902/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400903 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500904 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500905 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400906 */
907static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500908 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400909{
Janos Follath24eed8d2019-11-22 13:21:35 +0000910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500911 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500912
Andrzej Kurek9241d182018-11-20 05:04:35 -0500913 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500914 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500915 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400916
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500917 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500918 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
919 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500920 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500921 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
922 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500923
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400924 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400925}
926
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100927static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400928 const unsigned char *hash, size_t hash_len,
929 const unsigned char *sig, size_t sig_len )
930{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100931 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100934 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200935 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400936 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400937 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100938 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000939 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400940 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700941 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100942 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100943 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100944 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
945 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700946 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400947
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500948 if( curve == 0 )
949 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
950
Hanno Beckerccf574e2019-01-29 08:26:15 +0000951 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500952 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400953 key.pk_info = &pk_info;
954 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000955 p = buf + sizeof( buf );
956 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400957 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500958 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400959
Gilles Peskined2d45c12019-05-27 14:53:13 +0200960 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200962 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500963
Gilles Peskined2d45c12019-05-27 14:53:13 +0200964 status = psa_import_key( &attributes,
965 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200966 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200967 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400968 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100969 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400970 goto cleanup;
971 }
972
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500973 /* We don't need the exported key anymore and can
974 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500975 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500976 {
977 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
978 goto cleanup;
979 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500980
Hanno Beckera9851112019-01-25 16:37:10 +0000981 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500982 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500983 signature_part_size ) ) != 0 )
984 {
985 goto cleanup;
986 }
987
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100988 status = psa_verify_hash( key_id, psa_sig_md,
989 hash, hash_len,
990 buf, 2 * signature_part_size );
991 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400992 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800993 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400994 goto cleanup;
995 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500996
997 if( p != sig + sig_len )
998 {
999 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1000 goto cleanup;
1001 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001002 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001003
Andrzej Kurekb7b04782018-11-19 17:01:16 -05001004cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +01001005 status = psa_destroy_key( key_id );
1006 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +01001007 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +01001008
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001009 return( ret );
1010}
1011#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001013 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001014 const unsigned char *sig, size_t sig_len )
1015{
Janos Follath24eed8d2019-11-22 13:21:35 +00001016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001017 ((void) md_alg);
1018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001020 hash, hash_len, sig, sig_len );
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1023 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001024
1025 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001026}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001027#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001028
Neil Armstronge9606902022-02-09 14:23:00 +01001029#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001030/*
1031 * Simultaneously convert and move raw MPI from the beginning of a buffer
1032 * to an ASN.1 MPI at the end of the buffer.
1033 * See also mbedtls_asn1_write_mpi().
1034 *
1035 * p: pointer to the end of the output buffer
1036 * start: start of the output buffer, and also of the mpi to write at the end
1037 * n_len: length of the mpi to read from start
1038 */
1039static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1040 size_t n_len )
1041{
1042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1043 size_t len = 0;
1044
1045 if( (size_t)( *p - start ) < n_len )
1046 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1047
1048 len = n_len;
1049 *p -= len;
1050 memmove( *p, start, len );
1051
1052 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1053 * Neither r nor s should be 0, but as a failsafe measure, still detect
1054 * that rather than overflowing the buffer in case of a PSA error. */
1055 while( len > 0 && **p == 0x00 )
1056 {
1057 ++(*p);
1058 --len;
1059 }
1060
1061 /* this is only reached if the signature was invalid */
1062 if( len == 0 )
1063 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1064
1065 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1066 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1067 if( **p & 0x80 )
1068 {
1069 if( *p - start < 1 )
1070 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1071
1072 *--(*p) = 0x00;
1073 len += 1;
1074 }
1075
1076 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1077 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1078 MBEDTLS_ASN1_INTEGER ) );
1079
1080 return( (int) len );
1081}
1082
1083/* Transcode signature from PSA format to ASN.1 sequence.
1084 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1085 * MPIs, and in-place.
1086 *
1087 * [in/out] sig: the signature pre- and post-transcoding
1088 * [in/out] sig_len: signature length pre- and post-transcoding
1089 * [int] buf_len: the available size the in/out buffer
1090 */
Neil Armstronge9606902022-02-09 14:23:00 +01001091static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001092 size_t buf_len )
1093{
1094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1095 size_t len = 0;
1096 const size_t rs_len = *sig_len / 2;
1097 unsigned char *p = sig + buf_len;
1098
1099 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1100 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1101
1102 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1103 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1104 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1105
1106 memmove( sig, p, len );
1107 *sig_len = len;
1108
1109 return( 0 );
1110}
Neil Armstronge9606902022-02-09 14:23:00 +01001111
Neil Armstrong17a06552022-03-18 15:27:38 +01001112/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1113 *
1114 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1115 * [in] end: ASN.1 buffer end
1116 * [out] key_len: the ECDSA privateKey length in bytes
1117 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001118static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1119 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001120{
1121 size_t len;
1122 int ret;
1123
Neil Armstrong17a06552022-03-18 15:27:38 +01001124 /*
1125 * RFC 5915, or SEC1 Appendix C.4
1126 *
1127 * ECPrivateKey ::= SEQUENCE {
1128 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1129 * privateKey OCTET STRING,
1130 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1131 * publicKey [1] BIT STRING OPTIONAL
1132 * }
1133 */
1134
Neil Armstronge9606902022-02-09 14:23:00 +01001135 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001136 MBEDTLS_ASN1_CONSTRUCTED |
1137 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001138 return( ret );
1139
1140 /* version */
1141 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1142 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1143 return( ret );
1144
1145 *buf += len;
1146
1147 /* privateKey */
1148 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1149 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1150 return( ret );
1151
1152 *key_len = len;
1153
1154 return 0;
1155}
1156
1157static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1158 const unsigned char *hash, size_t hash_len,
1159 unsigned char *sig, size_t sig_size, size_t *sig_len,
1160 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1161{
1162 mbedtls_ecdsa_context *ctx = ctx_arg;
1163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1165 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1166 psa_status_t status;
1167 mbedtls_pk_context key;
1168 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001169 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001170 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001171 psa_algorithm_t psa_sig_md =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001172 PSA_ALG_ECDSA( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001173 size_t curve_bits;
1174 psa_ecc_family_t curve =
1175 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001176
1177 /* PSA has its own RNG */
1178 ((void) f_rng);
1179 ((void) p_rng);
1180
1181 if( curve == 0 )
1182 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1183
1184 /* mbedtls_pk_write_key_der() expects a full PK context;
1185 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001186 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001187 key.pk_ctx = ctx;
1188 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1189 if( key_len <= 0 )
1190 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1191
1192 p = buf + sizeof( buf ) - key_len;
1193 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1194 if( ret != 0 )
1195 goto cleanup;
1196
1197 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1199 psa_set_key_algorithm( &attributes, psa_sig_md );
1200
1201 status = psa_import_key( &attributes,
1202 p, key_len,
1203 &key_id );
1204 if( status != PSA_SUCCESS )
1205 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001206 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001207 goto cleanup;
1208 }
1209
Neil Armstronge4edcf72022-03-03 16:46:41 +01001210 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1211 sig, sig_size, sig_len );
1212 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001213 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001214 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001215 goto cleanup;
1216 }
1217
1218 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1219
1220cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001221 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001222 status = psa_destroy_key( key_id );
1223 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001224 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001225
Neil Armstronge9606902022-02-09 14:23:00 +01001226 return( ret );
1227}
1228#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001230 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001231 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001232 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1233{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001235 md_alg, hash, hash_len,
1236 sig, sig_size, sig_len,
1237 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001238}
Neil Armstronge9606902022-02-09 14:23:00 +01001239#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001240
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001241#if defined(MBEDTLS_ECP_RESTARTABLE)
1242static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1243 const unsigned char *hash, size_t hash_len,
1244 const unsigned char *sig, size_t sig_len,
1245 void *rs_ctx )
1246{
Janos Follath24eed8d2019-11-22 13:21:35 +00001247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001248 ((void) md_alg);
1249
1250 ret = mbedtls_ecdsa_read_signature_restartable(
1251 (mbedtls_ecdsa_context *) ctx,
1252 hash, hash_len, sig, sig_len,
1253 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1254
1255 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1256 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1257
1258 return( ret );
1259}
1260
1261static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1262 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001263 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001264 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1265 void *rs_ctx )
1266{
1267 return( mbedtls_ecdsa_write_signature_restartable(
1268 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001269 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001270 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1271
1272}
1273#endif /* MBEDTLS_ECP_RESTARTABLE */
1274
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001275static void *ecdsa_alloc_wrap( void )
1276{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001277 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001278
1279 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001281
1282 return( ctx );
1283}
1284
1285static void ecdsa_free_wrap( void *ctx )
1286{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1288 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001289}
1290
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001291#if defined(MBEDTLS_ECP_RESTARTABLE)
1292static void *ecdsa_rs_alloc( void )
1293{
1294 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1295
1296 if( ctx != NULL )
1297 mbedtls_ecdsa_restart_init( ctx );
1298
1299 return( ctx );
1300}
1301
1302static void ecdsa_rs_free( void *ctx )
1303{
1304 mbedtls_ecdsa_restart_free( ctx );
1305 mbedtls_free( ctx );
1306}
1307#endif /* MBEDTLS_ECP_RESTARTABLE */
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1310 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001311 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001312 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001313 ecdsa_can_do,
1314 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001315 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001316#if defined(MBEDTLS_ECP_RESTARTABLE)
1317 ecdsa_verify_rs_wrap,
1318 ecdsa_sign_rs_wrap,
1319#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001320 NULL,
1321 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001322 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001323 ecdsa_alloc_wrap,
1324 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001325#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001326 ecdsa_rs_alloc,
1327 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001328#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001329 eckey_debug, /* Compatible key structures */
1330};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001334/*
1335 * Support for alternative RSA-private implementations
1336 */
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001339{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001341}
1342
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001343static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001346
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001347 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001348}
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001351 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001352 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001353 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001357#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001358 if( UINT_MAX < hash_len )
1359 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001360#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001361
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001362 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001363 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1364 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001365 if( *sig_len > sig_size )
1366 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001367
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001368 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001369 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001370}
1371
1372static int rsa_alt_decrypt_wrap( void *ctx,
1373 const unsigned char *input, size_t ilen,
1374 unsigned char *output, size_t *olen, size_t osize,
1375 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001378
1379 ((void) f_rng);
1380 ((void) p_rng);
1381
1382 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001384
1385 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001386 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001387}
1388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001390static int rsa_alt_check_pair( const void *pub, const void *prv,
1391 int (*f_rng)(void *, unsigned char *, size_t),
1392 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001395 unsigned char hash[32];
1396 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001398
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001399 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001401
1402 memset( hash, 0x2a, sizeof( hash ) );
1403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001405 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001406 sig, sizeof( sig ), &sig_len,
1407 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001408 {
1409 return( ret );
1410 }
1411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001413 hash, sizeof( hash ), sig, sig_len ) != 0 )
1414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001416 }
1417
1418 return( 0 );
1419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001421
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001422static void *rsa_alt_alloc_wrap( void )
1423{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001424 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001425
1426 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001428
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001429 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001430}
1431
1432static void rsa_alt_free_wrap( void *ctx )
1433{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001434 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001436}
1437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1439 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001440 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001441 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001442 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001443 NULL,
1444 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001445#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001446 NULL,
1447 NULL,
1448#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001449 rsa_alt_decrypt_wrap,
1450 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001452 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001453#else
1454 NULL,
1455#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001456 rsa_alt_alloc_wrap,
1457 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001458#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001459 NULL,
1460 NULL,
1461#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001462 NULL,
1463};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001466
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001467#if defined(MBEDTLS_USE_PSA_CRYPTO)
1468
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001469static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001470{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001471 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001472
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001473 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001474
1475 return( ctx );
1476}
1477
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001478static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001479{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001480 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001481 mbedtls_free( ctx );
1482}
1483
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001484static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001485{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001486 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001487 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001489
Gilles Peskined2d45c12019-05-27 14:53:13 +02001490 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001491 return( 0 );
1492
Gilles Peskined2d45c12019-05-27 14:53:13 +02001493 bits = psa_get_key_bits( &attributes );
1494 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001495 return( bits );
1496}
1497
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001498static int pk_opaque_ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001499{
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001500 return( type == MBEDTLS_PK_ECKEY ||
1501 type == MBEDTLS_PK_ECDSA );
1502}
1503
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001504static int pk_opaque_rsa_can_do( mbedtls_pk_type_t type )
1505{
Neil Armstrong62d452b2022-04-12 15:11:49 +02001506 return( type == MBEDTLS_PK_RSA ||
1507 type == MBEDTLS_PK_RSASSA_PSS );
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001508}
1509
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001510static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1511 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001512 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001513 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1514{
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001515#if !defined(MBEDTLS_ECDSA_C) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001516 ((void) ctx);
1517 ((void) md_alg);
1518 ((void) hash);
1519 ((void) hash_len);
1520 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001521 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001522 ((void) sig_len);
1523 ((void) f_rng);
1524 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001525 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001526#else /* !MBEDTLS_ECDSA_C && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001527 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001529 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001530 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001531 psa_status_t status;
1532
1533 /* PSA has its own RNG */
1534 (void) f_rng;
1535 (void) p_rng;
1536
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001537 status = psa_get_key_attributes( *key, &attributes );
1538 if( status != PSA_SUCCESS )
1539 return( mbedtls_pk_error_from_psa( status ) );
1540
1541 type = psa_get_key_type( &attributes );
1542 psa_reset_key_attributes( &attributes );
1543
1544#if defined(MBEDTLS_ECDSA_C)
1545 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001546 alg = PSA_ALG_ECDSA( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001547 else
1548#endif /* MBEDTLS_ECDSA_C */
1549#if defined(MBEDTLS_RSA_C)
1550 if( PSA_KEY_TYPE_IS_RSA( type ) )
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001551 alg = PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001552 else
1553#endif /* MBEDTLS_RSA_C */
1554 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1555
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001556 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001557 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001558 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001559 if( status != PSA_SUCCESS )
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001560 {
1561#if defined(MBEDTLS_ECDSA_C)
1562 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
1563 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
1564 else
1565#endif /* MBEDTLS_ECDSA_C */
1566#if defined(MBEDTLS_RSA_C)
1567 if( PSA_KEY_TYPE_IS_RSA( type ) )
1568 return( mbedtls_pk_error_from_psa_rsa( status ) );
1569 else
1570#endif /* MBEDTLS_RSA_C */
1571 return( mbedtls_pk_error_from_psa( status ) );
1572 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001573
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001574#if defined(MBEDTLS_ECDSA_C)
1575 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
1576 /* transcode it to ASN.1 sequence */
1577 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
1578#endif /* MBEDTLS_ECDSA_C */
1579
1580 return 0;
1581#endif /* !MBEDTLS_ECDSA_C && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001582}
1583
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001584const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001585 MBEDTLS_PK_OPAQUE,
1586 "Opaque",
1587 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001588 pk_opaque_ecdsa_can_do,
1589 NULL, /* verify - will be done later */
1590 pk_opaque_sign_wrap,
1591#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1592 NULL, /* restartable verify - not relevant */
1593 NULL, /* restartable sign - not relevant */
1594#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001595 NULL, /* decrypt - not relevant */
1596 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001597 NULL, /* check_pair - could be done later or left NULL */
1598 pk_opaque_alloc_wrap,
1599 pk_opaque_free_wrap,
1600#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1601 NULL, /* restart alloc - not relevant */
1602 NULL, /* restart free - not relevant */
1603#endif
1604 NULL, /* debug - could be done later, or even left NULL */
1605};
1606
Neil Armstrong30beca32022-05-03 15:42:13 +02001607#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001608static int pk_opaque_rsa_decrypt( void *ctx,
1609 const unsigned char *input, size_t ilen,
1610 unsigned char *output, size_t *olen, size_t osize,
1611 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1612{
1613 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1614 psa_status_t status;
1615
1616 /* PSA has its own RNG */
1617 (void) f_rng;
1618 (void) p_rng;
1619
1620 status = psa_asymmetric_decrypt( *key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1621 input, ilen,
1622 NULL, 0,
1623 output, osize, olen );
1624 if( status != PSA_SUCCESS )
1625 {
1626 return( mbedtls_pk_error_from_psa_rsa( status ) );
1627 }
1628
1629 return 0;
1630}
Neil Armstrong30beca32022-05-03 15:42:13 +02001631#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001632
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001633const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1634 MBEDTLS_PK_OPAQUE,
1635 "Opaque",
1636 pk_opaque_get_bitlen,
1637 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001638 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001639 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001640#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1641 NULL, /* restartable verify - not relevant */
1642 NULL, /* restartable sign - not relevant */
1643#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001644#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001645 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001646#else
1647 NULL, /* decrypt - not available */
1648#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001649 NULL, /* encrypt - will be done later */
1650 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001651 pk_opaque_alloc_wrap,
1652 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001653#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1654 NULL, /* restart alloc - not relevant */
1655 NULL, /* restart free - not relevant */
1656#endif
1657 NULL, /* debug - could be done later, or even left NULL */
1658};
1659
1660#endif /* MBEDTLS_USE_PSA_CRYPTO */
1661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#endif /* MBEDTLS_PK_C */