blob: 5de8fa65f718ddc3e0e7e5f634342a48e030278d [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é-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059
Andres AG72849872017-01-19 11:24:33 +000060#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010061#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020062
Jerry Yub02ee182022-03-16 10:30:41 +080063#if defined(MBEDTLS_PSA_CRYPTO_C)
Neil Armstrong19915c22022-03-01 15:21:02 +010064int mbedtls_pk_error_from_psa( psa_status_t status )
65{
66 switch( status )
67 {
68 case PSA_SUCCESS:
69 return( 0 );
70 case PSA_ERROR_INVALID_HANDLE:
71 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
72 case PSA_ERROR_NOT_PERMITTED:
73 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
74 case PSA_ERROR_BUFFER_TOO_SMALL:
75 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
76 case PSA_ERROR_NOT_SUPPORTED:
77 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
78 case PSA_ERROR_INVALID_ARGUMENT:
79 return( MBEDTLS_ERR_PK_INVALID_ALG );
80 case PSA_ERROR_INSUFFICIENT_MEMORY:
81 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
82 case PSA_ERROR_BAD_STATE:
83 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
84 case PSA_ERROR_COMMUNICATION_FAILURE:
85 case PSA_ERROR_HARDWARE_FAILURE:
86 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
87 case PSA_ERROR_DATA_CORRUPT:
88 case PSA_ERROR_DATA_INVALID:
89 case PSA_ERROR_STORAGE_FAILURE:
90 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
91 case PSA_ERROR_CORRUPTION_DETECTED:
92 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
93 default:
94 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
95 }
96}
97
Neil Armstrong30beca32022-05-03 15:42:13 +020098#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
99 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong19915c22022-03-01 15:21:02 +0100100int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
101{
102 switch( status )
103 {
104 case PSA_ERROR_NOT_PERMITTED:
105 case PSA_ERROR_INVALID_ARGUMENT:
106 case PSA_ERROR_INVALID_HANDLE:
107 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
108 case PSA_ERROR_BUFFER_TOO_SMALL:
109 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
110 case PSA_ERROR_INSUFFICIENT_ENTROPY:
111 return( MBEDTLS_ERR_RSA_RNG_FAILED );
112 case PSA_ERROR_INVALID_SIGNATURE:
113 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
114 case PSA_ERROR_INVALID_PADDING:
115 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
116 default:
117 return( mbedtls_pk_error_from_psa( status ) );
118 }
119}
Neil Armstrong30beca32022-05-03 15:42:13 +0200120#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800121
122#endif /* MBEDTLS_PSA_CRYPTO_C */
123
124#if defined(MBEDTLS_USE_PSA_CRYPTO)
125
126#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Jerry Yu848ecce2022-03-22 10:58:48 +0800127int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status )
Jerry Yu07869e82022-03-16 16:40:50 +0800128{
129 switch( status )
130 {
131 case PSA_ERROR_NOT_PERMITTED:
132 case PSA_ERROR_INVALID_ARGUMENT:
133 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
134 case PSA_ERROR_INVALID_HANDLE:
135 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
136 case PSA_ERROR_BUFFER_TOO_SMALL:
137 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
138 case PSA_ERROR_INSUFFICIENT_ENTROPY:
139 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
140 case PSA_ERROR_INVALID_SIGNATURE:
141 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
142 default:
143 return( mbedtls_pk_error_from_psa( status ) );
144 }
145}
Jerry Yu75339822022-03-23 12:06:31 +0800146#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800147
Jerry Yu75339822022-03-23 12:06:31 +0800148#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_RSA_C)
151static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 return( type == MBEDTLS_PK_RSA ||
154 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200155}
156
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200157static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200158{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100159 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
160 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200161}
162
Neil Armstrong52f41f82022-02-22 15:30:24 +0100163#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200165 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200166 const unsigned char *sig, size_t sig_len )
167{
Neil Armstrong52f41f82022-02-22 15:30:24 +0100168 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
171 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
172 psa_status_t status;
173 mbedtls_pk_context key;
174 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100175 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100176 psa_algorithm_t psa_alg_md =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200177 PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100178 size_t rsa_len = mbedtls_rsa_get_len( rsa );
179
180#if SIZE_MAX > UINT_MAX
181 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
182 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
183#endif /* SIZE_MAX > UINT_MAX */
184
185 if( sig_len < rsa_len )
186 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
187
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100188 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100189 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100190 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100191 key.pk_ctx = ctx;
192 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
193 if( key_len <= 0 )
194 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
195
196 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
197 psa_set_key_algorithm( &attributes, psa_alg_md );
198 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
199
200 status = psa_import_key( &attributes,
201 buf + sizeof( buf ) - key_len, key_len,
202 &key_id );
203 if( status != PSA_SUCCESS )
204 {
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100205 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100206 goto cleanup;
207 }
208
209 status = psa_verify_hash( key_id, psa_alg_md, hash, hash_len,
210 sig, sig_len );
211 if( status != PSA_SUCCESS )
212 {
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100213 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100214 goto cleanup;
215 }
216 ret = 0;
217
218cleanup:
Neil Armstronga33280a2022-02-24 15:17:47 +0100219 status = psa_destroy_key( key_id );
220 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100221 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronga33280a2022-02-24 15:17:47 +0100222
Neil Armstrong52f41f82022-02-22 15:30:24 +0100223 return( ret );
224}
225#else
226static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
227 const unsigned char *hash, size_t hash_len,
228 const unsigned char *sig, size_t sig_len )
229{
Janos Follath24eed8d2019-11-22 13:21:35 +0000230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100231 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
232 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200233
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100234#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000235 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
236 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100237#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000238
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100239 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200241
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100242 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
243 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100244 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200245 return( ret );
246
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200247 /* The buffer contains a valid signature followed by extra data.
248 * We have a special error code for that so that so that callers can
249 * use mbedtls_pk_verify() to check "Does the buffer start with a
250 * valid signature?" and not just "Does the buffer contain a valid
251 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100252 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200254
255 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200256}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100257#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200258
Jerry Yub02ee182022-03-16 10:30:41 +0800259#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yue010de42022-03-23 11:45:55 +0800260int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
261 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800262 const unsigned char *hash, size_t hash_len,
263 unsigned char *sig, size_t sig_size,
264 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100265{
Neil Armstrong98545682022-02-22 16:12:51 +0100266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
268 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
269 psa_status_t status;
270 mbedtls_pk_context key;
271 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100272 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100273 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100274
Jerry Yue010de42022-03-23 11:45:55 +0800275 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100276 if( sig_size < *sig_len )
277 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
278
Neil Armstronge4f28682022-02-24 15:41:39 +0100279 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100280 * re-construct one to make it happy */
281 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800282 key.pk_ctx = rsa_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100283 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
284 if( key_len <= 0 )
285 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800287 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100288 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
289
290 status = psa_import_key( &attributes,
291 buf + sizeof( buf ) - key_len, key_len,
292 &key_id );
293 if( status != PSA_SUCCESS )
294 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100295 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100296 goto cleanup;
297 }
Jerry Yubf455e72022-03-22 21:39:41 +0800298 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100299 sig, sig_size, sig_len );
300 if( status != PSA_SUCCESS )
301 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100302 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100303 goto cleanup;
304 }
305
306 ret = 0;
307
308cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100309 status = psa_destroy_key( key_id );
310 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100311 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100312 return( ret );
313}
Jerry Yu406cf272022-03-22 11:33:42 +0800314#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800315
316#if defined(MBEDTLS_USE_PSA_CRYPTO)
317static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
318 const unsigned char *hash, size_t hash_len,
319 unsigned char *sig, size_t sig_size, size_t *sig_len,
320 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
321{
Jerry Yu1d172a32022-03-12 19:12:05 +0800322 ((void) f_rng);
323 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800324
Jerry Yubd1b3272022-03-24 13:05:20 +0800325 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200326 psa_md_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yubd1b3272022-03-24 13:05:20 +0800327 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800328 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800329
Jerry Yubd1b3272022-03-24 13:05:20 +0800330 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
331 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800332 ctx, hash, hash_len,
333 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800334}
Neil Armstrong98545682022-02-22 16:12:51 +0100335#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200337 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200338 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200339 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
340{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100341 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
342
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100343#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000344 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
345 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100346#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000347
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100348 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200349 if( sig_size < *sig_len )
350 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200351
Thomas Daubney140184d2021-05-18 16:04:07 +0100352 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
353 md_alg, (unsigned int) hash_len,
354 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200355}
Neil Armstrong98545682022-02-22 16:12:51 +0100356#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200357
Neil Armstrong18f43c72022-02-09 15:32:45 +0100358#if defined(MBEDTLS_USE_PSA_CRYPTO)
359static int rsa_decrypt_wrap( void *ctx,
360 const unsigned char *input, size_t ilen,
361 unsigned char *output, size_t *olen, size_t osize,
362 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
363{
364 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
367 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
368 psa_status_t status;
369 mbedtls_pk_context key;
370 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100371 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100372
373 ((void) f_rng);
374 ((void) p_rng);
375
376#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100377 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
378 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
379#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100380
381 if( ilen != mbedtls_rsa_get_len( rsa ) )
382 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
383
384 /* mbedtls_pk_write_key_der() expects a full PK context;
385 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100386 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100387 key.pk_ctx = ctx;
388 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
389 if( key_len <= 0 )
390 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
391
392 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
393 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100394 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100395
396 status = psa_import_key( &attributes,
397 buf + sizeof( buf ) - key_len, key_len,
398 &key_id );
399 if( status != PSA_SUCCESS )
400 {
Neil Armstronge8780492022-03-03 16:54:16 +0100401 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100402 goto cleanup;
403 }
404
Neil Armstrong8e805042022-03-16 15:30:31 +0100405 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
406 input, ilen,
407 NULL, 0,
408 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100409 if( status != PSA_SUCCESS )
410 {
Neil Armstronge8780492022-03-03 16:54:16 +0100411 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100412 goto cleanup;
413 }
414
415 ret = 0;
416
417cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100418 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100419 status = psa_destroy_key( key_id );
420 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100421 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100422
Neil Armstrong18f43c72022-02-09 15:32:45 +0100423 return( ret );
424}
425#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200426static int rsa_decrypt_wrap( void *ctx,
427 const unsigned char *input, size_t ilen,
428 unsigned char *output, size_t *olen, size_t osize,
429 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
430{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100431 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
432
433 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200435
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100436 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100437 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200438}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100439#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200440
Neil Armstrong96a16a42022-02-10 10:40:11 +0100441#if defined(MBEDTLS_USE_PSA_CRYPTO)
442static int rsa_encrypt_wrap( void *ctx,
443 const unsigned char *input, size_t ilen,
444 unsigned char *output, size_t *olen, size_t osize,
445 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
446{
447 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
450 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
451 psa_status_t status;
452 mbedtls_pk_context key;
453 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100454 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100455
456 ((void) f_rng);
457 ((void) p_rng);
458
459#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100460 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
461 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100462#endif
463
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100464 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100465 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
466
Neil Armstrongac014ca2022-02-24 15:27:54 +0100467 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100468 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100469 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100470 key.pk_ctx = ctx;
471 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
472 if( key_len <= 0 )
473 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
474
475 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100476 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100477 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
478
479 status = psa_import_key( &attributes,
480 buf + sizeof( buf ) - key_len, key_len,
481 &key_id );
482 if( status != PSA_SUCCESS )
483 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100484 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100485 goto cleanup;
486 }
487
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100488 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
489 input, ilen,
490 NULL, 0,
491 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100492 if( status != PSA_SUCCESS )
493 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100494 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100495 goto cleanup;
496 }
497
498 ret = 0;
499
500cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100501 status = psa_destroy_key( key_id );
502 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100503 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100504
Neil Armstrong96a16a42022-02-10 10:40:11 +0100505 return( ret );
506}
507#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200508static int rsa_encrypt_wrap( void *ctx,
509 const unsigned char *input, size_t ilen,
510 unsigned char *output, size_t *olen, size_t osize,
511 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
512{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100513 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
514 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200515
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100516 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100518
Thomas Daubney21772772021-05-13 17:30:32 +0100519 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100520 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200521}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100522#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200523
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200524static int rsa_check_pair_wrap( const void *pub, const void *prv,
525 int (*f_rng)(void *, unsigned char *, size_t),
526 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100527{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200528 (void) f_rng;
529 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
531 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100532}
533
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200534static void *rsa_alloc_wrap( void )
535{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200536 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200537
538 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200539 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200540
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200541 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200542}
543
544static void rsa_free_wrap( void *ctx )
545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
547 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200548}
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200551{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200552#if defined(MBEDTLS_RSA_ALT)
553 /* Not supported */
554 (void) ctx;
555 (void) items;
556#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200558 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200560
561 items++;
562
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.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200566#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200567}
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569const mbedtls_pk_info_t mbedtls_rsa_info = {
570 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200571 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200572 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200573 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200574 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200575 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200576#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200577 NULL,
578 NULL,
579#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200580 rsa_decrypt_wrap,
581 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100582 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200583 rsa_alloc_wrap,
584 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200585#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200586 NULL,
587 NULL,
588#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200589 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200590};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200594/*
595 * Generic EC key
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( type == MBEDTLS_PK_ECKEY ||
600 type == MBEDTLS_PK_ECKEY_DH ||
601 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200602}
603
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200604static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200607}
608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200610/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200612 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200613 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200616 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200617 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200618 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200621 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200622 const unsigned char *sig, size_t sig_len )
623{
Janos Follath24eed8d2019-11-22 13:21:35 +0000624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200630 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200633
634 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200635}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200638 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200639 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200640 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
641{
Janos Follath24eed8d2019-11-22 13:21:35 +0000642 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200648 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
649 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200650 f_rng, p_rng );
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200653
654 return( ret );
655}
656
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200657#if defined(MBEDTLS_ECP_RESTARTABLE)
658/* Forward declarations */
659static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
660 const unsigned char *hash, size_t hash_len,
661 const unsigned char *sig, size_t sig_len,
662 void *rs_ctx );
663
664static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
665 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200666 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200667 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
668 void *rs_ctx );
669
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200670/*
671 * Restart context for ECDSA operations with ECKEY context
672 *
673 * We need to store an actual ECDSA context, as we need to pass the same to
674 * the underlying ecdsa function, so we can't create it on the fly every time.
675 */
676typedef struct
677{
678 mbedtls_ecdsa_restart_ctx ecdsa_rs;
679 mbedtls_ecdsa_context ecdsa_ctx;
680} eckey_restart_ctx;
681
682static void *eckey_rs_alloc( void )
683{
684 eckey_restart_ctx *rs_ctx;
685
686 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
687
688 if( ctx != NULL )
689 {
690 rs_ctx = ctx;
691 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
692 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
693 }
694
695 return( ctx );
696}
697
698static void eckey_rs_free( void *ctx )
699{
700 eckey_restart_ctx *rs_ctx;
701
702 if( ctx == NULL)
703 return;
704
705 rs_ctx = ctx;
706 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
707 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
708
709 mbedtls_free( ctx );
710}
711
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200712static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
713 const unsigned char *hash, size_t hash_len,
714 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200715 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200716{
Janos Follath24eed8d2019-11-22 13:21:35 +0000717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200718 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200719
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200720 /* Should never happen */
721 if( rs == NULL )
722 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200723
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200724 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200725 if( rs->ecdsa_ctx.grp.pbits == 0 )
726 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200727
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200728 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
729 md_alg, hash, hash_len,
730 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200731
732cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200733 return( ret );
734}
735
736static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
737 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200738 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200739 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200740 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200741{
Janos Follath24eed8d2019-11-22 13:21:35 +0000742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200743 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200744
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200745 /* Should never happen */
746 if( rs == NULL )
747 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200748
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200749 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200750 if( rs->ecdsa_ctx.grp.pbits == 0 )
751 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200752
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200753 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200754 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200755 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200756
757cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200758 return( ret );
759}
760#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200762
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200763static int eckey_check_pair( const void *pub, const void *prv,
764 int (*f_rng)(void *, unsigned char *, size_t),
765 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200768 (const mbedtls_ecp_keypair *) prv,
769 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100770}
771
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200772static void *eckey_alloc_wrap( void )
773{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200774 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200775
776 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200778
779 return( ctx );
780}
781
782static void eckey_free_wrap( void *ctx )
783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
785 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200786}
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200789{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200791 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200793}
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795const mbedtls_pk_info_t mbedtls_eckey_info = {
796 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200797 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200798 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200799 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200801 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200802 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200803#if defined(MBEDTLS_ECP_RESTARTABLE)
804 eckey_verify_rs_wrap,
805 eckey_sign_rs_wrap,
806#endif
807#else /* MBEDTLS_ECDSA_C */
808 NULL,
809 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200810#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200811 NULL,
812 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100813 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200814 eckey_alloc_wrap,
815 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200816#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200817 eckey_rs_alloc,
818 eckey_rs_free,
819#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200820 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200821};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200822
823/*
Paul Bakker75342a62014-04-08 17:35:40 +0200824 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200827{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 return( type == MBEDTLS_PK_ECKEY ||
829 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200830}
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832const mbedtls_pk_info_t mbedtls_eckeydh_info = {
833 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200834 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200835 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200836 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200837 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200838 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200839#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200840 NULL,
841 NULL,
842#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200843 NULL,
844 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100845 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200846 eckey_alloc_wrap, /* Same underlying key structure */
847 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200848#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200849 NULL,
850 NULL,
851#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200852 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200853};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#if defined(MBEDTLS_ECDSA_C)
857static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200860}
861
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400862#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400863/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500864 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
865 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500866 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500867static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
868 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500869{
Janos Follath24eed8d2019-11-22 13:21:35 +0000870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500871 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500872
Andrzej Kurek9241d182018-11-20 05:04:35 -0500873 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500874 MBEDTLS_ASN1_INTEGER ) ) != 0 )
875 {
876 return( ret );
877 }
878
Andrzej Kurek9241d182018-11-20 05:04:35 -0500879 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500880 {
881 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500882 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500883 }
884
Andrzej Kurek9241d182018-11-20 05:04:35 -0500885 if( unpadded_len > to_len || unpadded_len == 0 )
886 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500887
Andrzej Kurek9241d182018-11-20 05:04:35 -0500888 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500889 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500890 memcpy( to + padding_len, *from, unpadded_len );
891 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500892
Andrzej Kurek9241d182018-11-20 05:04:35 -0500893 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500894}
895
896/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400897 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500898 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500899 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400900 */
901static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500902 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400903{
Janos Follath24eed8d2019-11-22 13:21:35 +0000904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500905 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500906
Andrzej Kurek9241d182018-11-20 05:04:35 -0500907 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500908 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500909 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400910
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500911 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500912 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
913 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500914 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500915 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
916 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500917
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400918 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400919}
920
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100921static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400922 const unsigned char *hash, size_t hash_len,
923 const unsigned char *sig, size_t sig_len )
924{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100925 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000926 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100928 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200929 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400930 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400931 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100932 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000933 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400934 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700935 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100936 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100937 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100938 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
939 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700940 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400941
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500942 if( curve == 0 )
943 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
944
Hanno Beckerccf574e2019-01-29 08:26:15 +0000945 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500946 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400947 key.pk_info = &pk_info;
948 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000949 p = buf + sizeof( buf );
950 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400951 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500952 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400953
Gilles Peskined2d45c12019-05-27 14:53:13 +0200954 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100955 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200956 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500957
Gilles Peskined2d45c12019-05-27 14:53:13 +0200958 status = psa_import_key( &attributes,
959 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200960 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200961 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400962 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100963 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400964 goto cleanup;
965 }
966
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500967 /* We don't need the exported key anymore and can
968 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500969 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500970 {
971 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
972 goto cleanup;
973 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500974
Hanno Beckera9851112019-01-25 16:37:10 +0000975 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500976 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500977 signature_part_size ) ) != 0 )
978 {
979 goto cleanup;
980 }
981
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100982 status = psa_verify_hash( key_id, psa_sig_md,
983 hash, hash_len,
984 buf, 2 * signature_part_size );
985 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400986 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800987 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400988 goto cleanup;
989 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500990
991 if( p != sig + sig_len )
992 {
993 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
994 goto cleanup;
995 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400996 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400997
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500998cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +0100999 status = psa_destroy_key( key_id );
1000 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +01001001 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +01001002
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001003 return( ret );
1004}
1005#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001007 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001008 const unsigned char *sig, size_t sig_len )
1009{
Janos Follath24eed8d2019-11-22 13:21:35 +00001010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001011 ((void) md_alg);
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001014 hash, hash_len, sig, sig_len );
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1017 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001018
1019 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001020}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001021#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001022
Neil Armstronge9606902022-02-09 14:23:00 +01001023#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001024/*
1025 * Simultaneously convert and move raw MPI from the beginning of a buffer
1026 * to an ASN.1 MPI at the end of the buffer.
1027 * See also mbedtls_asn1_write_mpi().
1028 *
1029 * p: pointer to the end of the output buffer
1030 * start: start of the output buffer, and also of the mpi to write at the end
1031 * n_len: length of the mpi to read from start
1032 */
1033static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1034 size_t n_len )
1035{
1036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1037 size_t len = 0;
1038
1039 if( (size_t)( *p - start ) < n_len )
1040 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1041
1042 len = n_len;
1043 *p -= len;
1044 memmove( *p, start, len );
1045
1046 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1047 * Neither r nor s should be 0, but as a failsafe measure, still detect
1048 * that rather than overflowing the buffer in case of a PSA error. */
1049 while( len > 0 && **p == 0x00 )
1050 {
1051 ++(*p);
1052 --len;
1053 }
1054
1055 /* this is only reached if the signature was invalid */
1056 if( len == 0 )
1057 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1058
1059 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1060 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1061 if( **p & 0x80 )
1062 {
1063 if( *p - start < 1 )
1064 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1065
1066 *--(*p) = 0x00;
1067 len += 1;
1068 }
1069
1070 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1071 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1072 MBEDTLS_ASN1_INTEGER ) );
1073
1074 return( (int) len );
1075}
1076
1077/* Transcode signature from PSA format to ASN.1 sequence.
1078 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1079 * MPIs, and in-place.
1080 *
1081 * [in/out] sig: the signature pre- and post-transcoding
1082 * [in/out] sig_len: signature length pre- and post-transcoding
1083 * [int] buf_len: the available size the in/out buffer
1084 */
Neil Armstronge9606902022-02-09 14:23:00 +01001085static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001086 size_t buf_len )
1087{
1088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1089 size_t len = 0;
1090 const size_t rs_len = *sig_len / 2;
1091 unsigned char *p = sig + buf_len;
1092
1093 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1094 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1095
1096 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1097 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1098 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1099
1100 memmove( sig, p, len );
1101 *sig_len = len;
1102
1103 return( 0 );
1104}
Neil Armstronge9606902022-02-09 14:23:00 +01001105
Neil Armstrong17a06552022-03-18 15:27:38 +01001106/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1107 *
1108 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1109 * [in] end: ASN.1 buffer end
1110 * [out] key_len: the ECDSA privateKey length in bytes
1111 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001112static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1113 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001114{
1115 size_t len;
1116 int ret;
1117
Neil Armstrong17a06552022-03-18 15:27:38 +01001118 /*
1119 * RFC 5915, or SEC1 Appendix C.4
1120 *
1121 * ECPrivateKey ::= SEQUENCE {
1122 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1123 * privateKey OCTET STRING,
1124 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1125 * publicKey [1] BIT STRING OPTIONAL
1126 * }
1127 */
1128
Neil Armstronge9606902022-02-09 14:23:00 +01001129 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001130 MBEDTLS_ASN1_CONSTRUCTED |
1131 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001132 return( ret );
1133
1134 /* version */
1135 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1136 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1137 return( ret );
1138
1139 *buf += len;
1140
1141 /* privateKey */
1142 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1143 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1144 return( ret );
1145
1146 *key_len = len;
1147
1148 return 0;
1149}
1150
1151static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1152 const unsigned char *hash, size_t hash_len,
1153 unsigned char *sig, size_t sig_size, size_t *sig_len,
1154 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1155{
1156 mbedtls_ecdsa_context *ctx = ctx_arg;
1157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1159 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1160 psa_status_t status;
1161 mbedtls_pk_context key;
1162 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001163 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001164 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001165 psa_algorithm_t psa_sig_md =
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001166 PSA_ALG_ECDSA( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001167 size_t curve_bits;
1168 psa_ecc_family_t curve =
1169 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001170
1171 /* PSA has its own RNG */
1172 ((void) f_rng);
1173 ((void) p_rng);
1174
1175 if( curve == 0 )
1176 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1177
1178 /* mbedtls_pk_write_key_der() expects a full PK context;
1179 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001180 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001181 key.pk_ctx = ctx;
1182 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1183 if( key_len <= 0 )
1184 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1185
1186 p = buf + sizeof( buf ) - key_len;
1187 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1188 if( ret != 0 )
1189 goto cleanup;
1190
1191 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1192 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1193 psa_set_key_algorithm( &attributes, psa_sig_md );
1194
1195 status = psa_import_key( &attributes,
1196 p, key_len,
1197 &key_id );
1198 if( status != PSA_SUCCESS )
1199 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001200 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001201 goto cleanup;
1202 }
1203
Neil Armstronge4edcf72022-03-03 16:46:41 +01001204 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1205 sig, sig_size, sig_len );
1206 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001207 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001208 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001209 goto cleanup;
1210 }
1211
1212 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1213
1214cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001215 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001216 status = psa_destroy_key( key_id );
1217 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001218 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001219
Neil Armstronge9606902022-02-09 14:23:00 +01001220 return( ret );
1221}
1222#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001224 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001225 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001226 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001229 md_alg, hash, hash_len,
1230 sig, sig_size, sig_len,
1231 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001232}
Neil Armstronge9606902022-02-09 14:23:00 +01001233#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001234
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001235#if defined(MBEDTLS_ECP_RESTARTABLE)
1236static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1237 const unsigned char *hash, size_t hash_len,
1238 const unsigned char *sig, size_t sig_len,
1239 void *rs_ctx )
1240{
Janos Follath24eed8d2019-11-22 13:21:35 +00001241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001242 ((void) md_alg);
1243
1244 ret = mbedtls_ecdsa_read_signature_restartable(
1245 (mbedtls_ecdsa_context *) ctx,
1246 hash, hash_len, sig, sig_len,
1247 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1248
1249 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1250 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1251
1252 return( ret );
1253}
1254
1255static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1256 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001257 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001258 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1259 void *rs_ctx )
1260{
1261 return( mbedtls_ecdsa_write_signature_restartable(
1262 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001263 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001264 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1265
1266}
1267#endif /* MBEDTLS_ECP_RESTARTABLE */
1268
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001269static void *ecdsa_alloc_wrap( void )
1270{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001271 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001272
1273 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001275
1276 return( ctx );
1277}
1278
1279static void ecdsa_free_wrap( void *ctx )
1280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1282 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001283}
1284
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001285#if defined(MBEDTLS_ECP_RESTARTABLE)
1286static void *ecdsa_rs_alloc( void )
1287{
1288 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1289
1290 if( ctx != NULL )
1291 mbedtls_ecdsa_restart_init( ctx );
1292
1293 return( ctx );
1294}
1295
1296static void ecdsa_rs_free( void *ctx )
1297{
1298 mbedtls_ecdsa_restart_free( ctx );
1299 mbedtls_free( ctx );
1300}
1301#endif /* MBEDTLS_ECP_RESTARTABLE */
1302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1304 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001305 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001306 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001307 ecdsa_can_do,
1308 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001309 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001310#if defined(MBEDTLS_ECP_RESTARTABLE)
1311 ecdsa_verify_rs_wrap,
1312 ecdsa_sign_rs_wrap,
1313#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001314 NULL,
1315 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001316 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001317 ecdsa_alloc_wrap,
1318 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001319#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001320 ecdsa_rs_alloc,
1321 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001322#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001323 eckey_debug, /* Compatible key structures */
1324};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001328/*
1329 * Support for alternative RSA-private implementations
1330 */
1331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001335}
1336
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001337static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001338{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001340
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001341 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001342}
1343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001345 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001346 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001347 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001350
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001351#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001352 if( UINT_MAX < hash_len )
1353 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001354#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001355
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001357 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1358 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001359 if( *sig_len > sig_size )
1360 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001361
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001362 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001363 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001364}
1365
1366static int rsa_alt_decrypt_wrap( void *ctx,
1367 const unsigned char *input, size_t ilen,
1368 unsigned char *output, size_t *olen, size_t osize,
1369 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1370{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001372
1373 ((void) f_rng);
1374 ((void) p_rng);
1375
1376 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001378
1379 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001380 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001381}
1382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001384static int rsa_alt_check_pair( const void *pub, const void *prv,
1385 int (*f_rng)(void *, unsigned char *, size_t),
1386 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001389 unsigned char hash[32];
1390 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001392
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001393 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001395
1396 memset( hash, 0x2a, sizeof( hash ) );
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001399 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001400 sig, sizeof( sig ), &sig_len,
1401 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001402 {
1403 return( ret );
1404 }
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001407 hash, sizeof( hash ), sig, sig_len ) != 0 )
1408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001410 }
1411
1412 return( 0 );
1413}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001415
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001416static void *rsa_alt_alloc_wrap( void )
1417{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001418 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001419
1420 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001422
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001423 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001424}
1425
1426static void rsa_alt_free_wrap( void *ctx )
1427{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001428 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001430}
1431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1433 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001434 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001435 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001436 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001437 NULL,
1438 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001439#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001440 NULL,
1441 NULL,
1442#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001443 rsa_alt_decrypt_wrap,
1444 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001446 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001447#else
1448 NULL,
1449#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001450 rsa_alt_alloc_wrap,
1451 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001452#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001453 NULL,
1454 NULL,
1455#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001456 NULL,
1457};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001460
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001461#if defined(MBEDTLS_USE_PSA_CRYPTO)
1462
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001463static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001464{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001465 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001466
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001467 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001468
1469 return( ctx );
1470}
1471
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001472static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001473{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001474 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001475 mbedtls_free( ctx );
1476}
1477
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001478static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001479{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001480 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001481 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001483
Gilles Peskined2d45c12019-05-27 14:53:13 +02001484 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001485 return( 0 );
1486
Gilles Peskined2d45c12019-05-27 14:53:13 +02001487 bits = psa_get_key_bits( &attributes );
1488 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001489 return( bits );
1490}
1491
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001492static int pk_opaque_ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001493{
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001494 return( type == MBEDTLS_PK_ECKEY ||
1495 type == MBEDTLS_PK_ECDSA );
1496}
1497
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001498static int pk_opaque_rsa_can_do( mbedtls_pk_type_t type )
1499{
Neil Armstrong62d452b2022-04-12 15:11:49 +02001500 return( type == MBEDTLS_PK_RSA ||
1501 type == MBEDTLS_PK_RSASSA_PSS );
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001502}
1503
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001504static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1505 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001506 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001507 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1508{
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001509#if !defined(MBEDTLS_ECDSA_C) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001510 ((void) ctx);
1511 ((void) md_alg);
1512 ((void) hash);
1513 ((void) hash_len);
1514 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001515 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001516 ((void) sig_len);
1517 ((void) f_rng);
1518 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001519 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001520#else /* !MBEDTLS_ECDSA_C && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001521 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001522 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001523 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001524 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001525 psa_status_t status;
1526
1527 /* PSA has its own RNG */
1528 (void) f_rng;
1529 (void) p_rng;
1530
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001531 status = psa_get_key_attributes( *key, &attributes );
1532 if( status != PSA_SUCCESS )
1533 return( mbedtls_pk_error_from_psa( status ) );
1534
1535 type = psa_get_key_type( &attributes );
1536 psa_reset_key_attributes( &attributes );
1537
1538#if defined(MBEDTLS_ECDSA_C)
1539 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001540 alg = PSA_ALG_ECDSA( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001541 else
1542#endif /* MBEDTLS_ECDSA_C */
1543#if defined(MBEDTLS_RSA_C)
1544 if( PSA_KEY_TYPE_IS_RSA( type ) )
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001545 alg = PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001546 else
1547#endif /* MBEDTLS_RSA_C */
1548 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1549
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001550 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001551 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001552 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001553 if( status != PSA_SUCCESS )
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001554 {
1555#if defined(MBEDTLS_ECDSA_C)
1556 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
1557 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
1558 else
1559#endif /* MBEDTLS_ECDSA_C */
1560#if defined(MBEDTLS_RSA_C)
1561 if( PSA_KEY_TYPE_IS_RSA( type ) )
1562 return( mbedtls_pk_error_from_psa_rsa( status ) );
1563 else
1564#endif /* MBEDTLS_RSA_C */
1565 return( mbedtls_pk_error_from_psa( status ) );
1566 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001567
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001568#if defined(MBEDTLS_ECDSA_C)
1569 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
1570 /* transcode it to ASN.1 sequence */
1571 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
1572#endif /* MBEDTLS_ECDSA_C */
1573
1574 return 0;
1575#endif /* !MBEDTLS_ECDSA_C && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001576}
1577
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001578const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001579 MBEDTLS_PK_OPAQUE,
1580 "Opaque",
1581 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001582 pk_opaque_ecdsa_can_do,
1583 NULL, /* verify - will be done later */
1584 pk_opaque_sign_wrap,
1585#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1586 NULL, /* restartable verify - not relevant */
1587 NULL, /* restartable sign - not relevant */
1588#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001589 NULL, /* decrypt - not relevant */
1590 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001591 NULL, /* check_pair - could be done later or left NULL */
1592 pk_opaque_alloc_wrap,
1593 pk_opaque_free_wrap,
1594#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1595 NULL, /* restart alloc - not relevant */
1596 NULL, /* restart free - not relevant */
1597#endif
1598 NULL, /* debug - could be done later, or even left NULL */
1599};
1600
Neil Armstrong30beca32022-05-03 15:42:13 +02001601#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001602static int pk_opaque_rsa_decrypt( void *ctx,
1603 const unsigned char *input, size_t ilen,
1604 unsigned char *output, size_t *olen, size_t osize,
1605 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1606{
1607 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1608 psa_status_t status;
1609
1610 /* PSA has its own RNG */
1611 (void) f_rng;
1612 (void) p_rng;
1613
1614 status = psa_asymmetric_decrypt( *key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1615 input, ilen,
1616 NULL, 0,
1617 output, osize, olen );
1618 if( status != PSA_SUCCESS )
1619 {
1620 return( mbedtls_pk_error_from_psa_rsa( status ) );
1621 }
1622
1623 return 0;
1624}
Neil Armstrong30beca32022-05-03 15:42:13 +02001625#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001626
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001627const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1628 MBEDTLS_PK_OPAQUE,
1629 "Opaque",
1630 pk_opaque_get_bitlen,
1631 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001632 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001633 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001634#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1635 NULL, /* restartable verify - not relevant */
1636 NULL, /* restartable sign - not relevant */
1637#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001638#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001639 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001640#else
1641 NULL, /* decrypt - not available */
1642#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001643 NULL, /* encrypt - will be done later */
1644 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001645 pk_opaque_alloc_wrap,
1646 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001647#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1648 NULL, /* restart alloc - not relevant */
1649 NULL, /* restart free - not relevant */
1650#endif
1651 NULL, /* debug - could be done later, or even left NULL */
1652};
1653
1654#endif /* MBEDTLS_USE_PSA_CRYPTO */
1655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_PK_C */