blob: 82b7b26c8ea68b82ab648f3302f6edc4622e0250 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000023#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Neil Armstrong0f49f832022-02-28 15:07:38 +010035#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
36#include "pkwrite.h"
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#endif
42
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "mbedtls/asn1write.h"
45#endif
46
Andres Amaya Garciae32df082017-10-25 09:37:04 +010047#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010049#endif
50
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040052#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010053#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040054#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059#else
60#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020061#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020063#endif
64
Andres AG72849872017-01-19 11:24:33 +000065#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010066#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020067
Jerry Yub02ee182022-03-16 10:30:41 +080068#if defined(MBEDTLS_PSA_CRYPTO_C)
Neil Armstrong19915c22022-03-01 15:21:02 +010069int mbedtls_pk_error_from_psa( psa_status_t status )
70{
71 switch( status )
72 {
73 case PSA_SUCCESS:
74 return( 0 );
75 case PSA_ERROR_INVALID_HANDLE:
76 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
77 case PSA_ERROR_NOT_PERMITTED:
78 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
79 case PSA_ERROR_BUFFER_TOO_SMALL:
80 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
81 case PSA_ERROR_NOT_SUPPORTED:
82 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
83 case PSA_ERROR_INVALID_ARGUMENT:
84 return( MBEDTLS_ERR_PK_INVALID_ALG );
85 case PSA_ERROR_INSUFFICIENT_MEMORY:
86 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
87 case PSA_ERROR_BAD_STATE:
88 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
89 case PSA_ERROR_COMMUNICATION_FAILURE:
90 case PSA_ERROR_HARDWARE_FAILURE:
91 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
92 case PSA_ERROR_DATA_CORRUPT:
93 case PSA_ERROR_DATA_INVALID:
94 case PSA_ERROR_STORAGE_FAILURE:
95 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
96 case PSA_ERROR_CORRUPTION_DETECTED:
97 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
98 default:
99 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
100 }
101}
102
Neil Armstrong19915c22022-03-01 15:21:02 +0100103#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
104int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
105{
106 switch( status )
107 {
108 case PSA_ERROR_NOT_PERMITTED:
109 case PSA_ERROR_INVALID_ARGUMENT:
110 case PSA_ERROR_INVALID_HANDLE:
111 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
112 case PSA_ERROR_BUFFER_TOO_SMALL:
113 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
114 case PSA_ERROR_INSUFFICIENT_ENTROPY:
115 return( MBEDTLS_ERR_RSA_RNG_FAILED );
116 case PSA_ERROR_INVALID_SIGNATURE:
117 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
118 case PSA_ERROR_INVALID_PADDING:
119 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
120 default:
121 return( mbedtls_pk_error_from_psa( status ) );
122 }
123}
124#endif
Jerry Yu07869e82022-03-16 16:40:50 +0800125
126#endif /* MBEDTLS_PSA_CRYPTO_C */
127
128#if defined(MBEDTLS_USE_PSA_CRYPTO)
129
130#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Jerry Yu848ecce2022-03-22 10:58:48 +0800131int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status )
Jerry Yu07869e82022-03-16 16:40:50 +0800132{
133 switch( status )
134 {
135 case PSA_ERROR_NOT_PERMITTED:
136 case PSA_ERROR_INVALID_ARGUMENT:
137 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
138 case PSA_ERROR_INVALID_HANDLE:
139 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
140 case PSA_ERROR_BUFFER_TOO_SMALL:
141 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
142 case PSA_ERROR_INSUFFICIENT_ENTROPY:
143 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
144 case PSA_ERROR_INVALID_SIGNATURE:
145 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
146 default:
147 return( mbedtls_pk_error_from_psa( status ) );
148 }
149}
Jerry Yu75339822022-03-23 12:06:31 +0800150#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800151
Jerry Yu75339822022-03-23 12:06:31 +0800152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_RSA_C)
155static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( type == MBEDTLS_PK_RSA ||
158 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200159}
160
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200161static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200162{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100163 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
164 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200165}
166
Neil Armstrong52f41f82022-02-22 15:30:24 +0100167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200169 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200170 const unsigned char *sig, size_t sig_len )
171{
Neil Armstrong52f41f82022-02-22 15:30:24 +0100172 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
175 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
176 psa_status_t status;
177 mbedtls_pk_context key;
178 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100179 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong52f41f82022-02-22 15:30:24 +0100180 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
181 psa_algorithm_t psa_alg_md = PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_psa_translate_md( md_alg ) );
182 size_t rsa_len = mbedtls_rsa_get_len( rsa );
183
184#if SIZE_MAX > UINT_MAX
185 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
186 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
187#endif /* SIZE_MAX > UINT_MAX */
188
189 if( sig_len < rsa_len )
190 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
191
192 /* mbedtls_pk_write_pubkey() expects a full PK context;
193 * re-construct one to make it happy */
194 key.pk_info = &pk_info;
195 key.pk_ctx = ctx;
196 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
197 if( key_len <= 0 )
198 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
199
200 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
201 psa_set_key_algorithm( &attributes, psa_alg_md );
202 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
203
204 status = psa_import_key( &attributes,
205 buf + sizeof( buf ) - key_len, key_len,
206 &key_id );
207 if( status != PSA_SUCCESS )
208 {
209 ret = mbedtls_psa_err_translate_pk( status );
210 goto cleanup;
211 }
212
213 status = psa_verify_hash( key_id, psa_alg_md, hash, hash_len,
214 sig, sig_len );
215 if( status != PSA_SUCCESS )
216 {
Neil Armstrong059a80c2022-02-24 15:23:42 +0100217 if ( status == PSA_ERROR_INVALID_PADDING )
218 {
219 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
220 }
221 else
222 {
223 ret = mbedtls_psa_err_translate_pk( status );
224 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100225 goto cleanup;
226 }
227 ret = 0;
228
229cleanup:
Neil Armstronga33280a2022-02-24 15:17:47 +0100230 status = psa_destroy_key( key_id );
231 if( ret == 0 && status != PSA_SUCCESS )
232 ret = mbedtls_psa_err_translate_pk( status );
233
Neil Armstrong52f41f82022-02-22 15:30:24 +0100234 return( ret );
235}
236#else
237static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
238 const unsigned char *hash, size_t hash_len,
239 const unsigned char *sig, size_t sig_len )
240{
Janos Follath24eed8d2019-11-22 13:21:35 +0000241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100242 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
243 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200244
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100245#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000246 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
247 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100248#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000249
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100250 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200252
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100253 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
254 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100255 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200256 return( ret );
257
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200258 /* The buffer contains a valid signature followed by extra data.
259 * We have a special error code for that so that so that callers can
260 * use mbedtls_pk_verify() to check "Does the buffer start with a
261 * valid signature?" and not just "Does the buffer contain a valid
262 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100263 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200265
266 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200267}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100268#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200269
Jerry Yub02ee182022-03-16 10:30:41 +0800270#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yue010de42022-03-23 11:45:55 +0800271int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
272 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800273 const unsigned char *hash, size_t hash_len,
274 unsigned char *sig, size_t sig_size,
275 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100276{
Neil Armstrong98545682022-02-22 16:12:51 +0100277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
278 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
279 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
280 psa_status_t status;
281 mbedtls_pk_context key;
282 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100283 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100284 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100285
Jerry Yue010de42022-03-23 11:45:55 +0800286 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100287 if( sig_size < *sig_len )
288 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
289
Neil Armstronge4f28682022-02-24 15:41:39 +0100290 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100291 * re-construct one to make it happy */
292 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800293 key.pk_ctx = rsa_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100294 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
295 if( key_len <= 0 )
296 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800298 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100299 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
300
301 status = psa_import_key( &attributes,
302 buf + sizeof( buf ) - key_len, key_len,
303 &key_id );
304 if( status != PSA_SUCCESS )
305 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100306 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100307 goto cleanup;
308 }
Jerry Yubf455e72022-03-22 21:39:41 +0800309 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100310 sig, sig_size, sig_len );
311 if( status != PSA_SUCCESS )
312 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100313 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100314 goto cleanup;
315 }
316
317 ret = 0;
318
319cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100320 status = psa_destroy_key( key_id );
321 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100322 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100323 return( ret );
324}
Jerry Yu406cf272022-03-22 11:33:42 +0800325#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800326
327#if defined(MBEDTLS_USE_PSA_CRYPTO)
328static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
329 const unsigned char *hash, size_t hash_len,
330 unsigned char *sig, size_t sig_size, size_t *sig_len,
331 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
332{
Jerry Yu1d172a32022-03-12 19:12:05 +0800333 ((void) f_rng);
334 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800335
Jerry Yubd1b3272022-03-24 13:05:20 +0800336 psa_algorithm_t psa_md_alg;
337 psa_md_alg = mbedtls_psa_translate_md( md_alg );
338 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800339 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800340
Jerry Yubd1b3272022-03-24 13:05:20 +0800341 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
342 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800343 ctx, hash, hash_len,
344 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800345}
Neil Armstrong98545682022-02-22 16:12:51 +0100346#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200348 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200349 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200350 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
351{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100352 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
353
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100354#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000355 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
356 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100357#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000358
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100359 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200360 if( sig_size < *sig_len )
361 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200362
Thomas Daubney140184d2021-05-18 16:04:07 +0100363 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
364 md_alg, (unsigned int) hash_len,
365 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200366}
Neil Armstrong98545682022-02-22 16:12:51 +0100367#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200368
Neil Armstrong18f43c72022-02-09 15:32:45 +0100369#if defined(MBEDTLS_USE_PSA_CRYPTO)
370static int rsa_decrypt_wrap( void *ctx,
371 const unsigned char *input, size_t ilen,
372 unsigned char *output, size_t *olen, size_t osize,
373 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
374{
375 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
378 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
379 psa_status_t status;
380 mbedtls_pk_context key;
381 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100382 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100383
384 ((void) f_rng);
385 ((void) p_rng);
386
387#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100388 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
389 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
390#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100391
392 if( ilen != mbedtls_rsa_get_len( rsa ) )
393 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
394
395 /* mbedtls_pk_write_key_der() expects a full PK context;
396 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100397 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100398 key.pk_ctx = ctx;
399 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
400 if( key_len <= 0 )
401 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
402
403 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
404 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100405 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100406
407 status = psa_import_key( &attributes,
408 buf + sizeof( buf ) - key_len, key_len,
409 &key_id );
410 if( status != PSA_SUCCESS )
411 {
Neil Armstronge8780492022-03-03 16:54:16 +0100412 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100413 goto cleanup;
414 }
415
Neil Armstrong8e805042022-03-16 15:30:31 +0100416 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
417 input, ilen,
418 NULL, 0,
419 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100420 if( status != PSA_SUCCESS )
421 {
Neil Armstronge8780492022-03-03 16:54:16 +0100422 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100423 goto cleanup;
424 }
425
426 ret = 0;
427
428cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100429 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100430 status = psa_destroy_key( key_id );
431 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100432 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100433
Neil Armstrong18f43c72022-02-09 15:32:45 +0100434 return( ret );
435}
436#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200437static int rsa_decrypt_wrap( void *ctx,
438 const unsigned char *input, size_t ilen,
439 unsigned char *output, size_t *olen, size_t osize,
440 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
441{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100442 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
443
444 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200446
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100447 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100448 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200449}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100450#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200451
Neil Armstrong96a16a42022-02-10 10:40:11 +0100452#if defined(MBEDTLS_USE_PSA_CRYPTO)
453static int rsa_encrypt_wrap( void *ctx,
454 const unsigned char *input, size_t ilen,
455 unsigned char *output, size_t *olen, size_t osize,
456 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
457{
458 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
461 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
462 psa_status_t status;
463 mbedtls_pk_context key;
464 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100465 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100466
467 ((void) f_rng);
468 ((void) p_rng);
469
470#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100471 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
472 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100473#endif
474
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100475 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100476 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
477
Neil Armstrongac014ca2022-02-24 15:27:54 +0100478 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100479 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100480 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100481 key.pk_ctx = ctx;
482 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
483 if( key_len <= 0 )
484 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
485
486 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100487 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100488 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
489
490 status = psa_import_key( &attributes,
491 buf + sizeof( buf ) - key_len, key_len,
492 &key_id );
493 if( status != PSA_SUCCESS )
494 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100495 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100496 goto cleanup;
497 }
498
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100499 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
500 input, ilen,
501 NULL, 0,
502 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100503 if( status != PSA_SUCCESS )
504 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100505 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100506 goto cleanup;
507 }
508
509 ret = 0;
510
511cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100512 status = psa_destroy_key( key_id );
513 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100514 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100515
Neil Armstrong96a16a42022-02-10 10:40:11 +0100516 return( ret );
517}
518#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200519static int rsa_encrypt_wrap( void *ctx,
520 const unsigned char *input, size_t ilen,
521 unsigned char *output, size_t *olen, size_t osize,
522 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
523{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100524 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
525 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200526
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100527 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100529
Thomas Daubney21772772021-05-13 17:30:32 +0100530 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100531 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200532}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100533#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200534
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200535static int rsa_check_pair_wrap( const void *pub, const void *prv,
536 int (*f_rng)(void *, unsigned char *, size_t),
537 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100538{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200539 (void) f_rng;
540 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
542 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100543}
544
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200545static void *rsa_alloc_wrap( void )
546{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200547 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200548
549 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200550 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200551
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200552 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200553}
554
555static void rsa_free_wrap( void *ctx )
556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
558 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200559}
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200562{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200563#if defined(MBEDTLS_RSA_ALT)
564 /* Not supported */
565 (void) ctx;
566 (void) items;
567#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200569 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200571
572 items++;
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200575 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200577#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200578}
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580const mbedtls_pk_info_t mbedtls_rsa_info = {
581 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200582 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200583 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200584 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200585 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200586 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200587#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200588 NULL,
589 NULL,
590#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200591 rsa_decrypt_wrap,
592 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100593 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200594 rsa_alloc_wrap,
595 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200596#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200597 NULL,
598 NULL,
599#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200600 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200601};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200605/*
606 * Generic EC key
607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( type == MBEDTLS_PK_ECKEY ||
611 type == MBEDTLS_PK_ECKEY_DH ||
612 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200613}
614
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200615static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200616{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200618}
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200621/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200623 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200624 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200627 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200628 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200629 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200632 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200633 const unsigned char *sig, size_t sig_len )
634{
Janos Follath24eed8d2019-11-22 13:21:35 +0000635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200641 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200644
645 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200646}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200649 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200650 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200651 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
652{
Janos Follath24eed8d2019-11-22 13:21:35 +0000653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200659 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
660 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200661 f_rng, p_rng );
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200664
665 return( ret );
666}
667
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200668#if defined(MBEDTLS_ECP_RESTARTABLE)
669/* Forward declarations */
670static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
671 const unsigned char *hash, size_t hash_len,
672 const unsigned char *sig, size_t sig_len,
673 void *rs_ctx );
674
675static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
676 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200677 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200678 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
679 void *rs_ctx );
680
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200681/*
682 * Restart context for ECDSA operations with ECKEY context
683 *
684 * We need to store an actual ECDSA context, as we need to pass the same to
685 * the underlying ecdsa function, so we can't create it on the fly every time.
686 */
687typedef struct
688{
689 mbedtls_ecdsa_restart_ctx ecdsa_rs;
690 mbedtls_ecdsa_context ecdsa_ctx;
691} eckey_restart_ctx;
692
693static void *eckey_rs_alloc( void )
694{
695 eckey_restart_ctx *rs_ctx;
696
697 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
698
699 if( ctx != NULL )
700 {
701 rs_ctx = ctx;
702 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
703 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
704 }
705
706 return( ctx );
707}
708
709static void eckey_rs_free( void *ctx )
710{
711 eckey_restart_ctx *rs_ctx;
712
713 if( ctx == NULL)
714 return;
715
716 rs_ctx = ctx;
717 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
718 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
719
720 mbedtls_free( ctx );
721}
722
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200723static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
724 const unsigned char *hash, size_t hash_len,
725 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200726 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200727{
Janos Follath24eed8d2019-11-22 13:21:35 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200729 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200730
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200731 /* Should never happen */
732 if( rs == NULL )
733 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200734
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200735 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200736 if( rs->ecdsa_ctx.grp.pbits == 0 )
737 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200738
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200739 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
740 md_alg, hash, hash_len,
741 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200742
743cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200744 return( ret );
745}
746
747static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
748 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200749 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200750 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200751 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200752{
Janos Follath24eed8d2019-11-22 13:21:35 +0000753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200754 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200755
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200756 /* Should never happen */
757 if( rs == NULL )
758 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200759
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200760 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200761 if( rs->ecdsa_ctx.grp.pbits == 0 )
762 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200763
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200764 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200765 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200766 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200767
768cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200769 return( ret );
770}
771#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200773
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200774static int eckey_check_pair( const void *pub, const void *prv,
775 int (*f_rng)(void *, unsigned char *, size_t),
776 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200779 (const mbedtls_ecp_keypair *) prv,
780 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100781}
782
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200783static void *eckey_alloc_wrap( void )
784{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200785 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200786
787 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200789
790 return( ctx );
791}
792
793static void eckey_free_wrap( void *ctx )
794{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
796 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200797}
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200800{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200802 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200804}
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806const mbedtls_pk_info_t mbedtls_eckey_info = {
807 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200808 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200809 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200810 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200812 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200813 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200814#if defined(MBEDTLS_ECP_RESTARTABLE)
815 eckey_verify_rs_wrap,
816 eckey_sign_rs_wrap,
817#endif
818#else /* MBEDTLS_ECDSA_C */
819 NULL,
820 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200821#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200822 NULL,
823 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100824 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200825 eckey_alloc_wrap,
826 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200827#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200828 eckey_rs_alloc,
829 eckey_rs_free,
830#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200831 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200832};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200833
834/*
Paul Bakker75342a62014-04-08 17:35:40 +0200835 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 return( type == MBEDTLS_PK_ECKEY ||
840 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200841}
842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843const mbedtls_pk_info_t mbedtls_eckeydh_info = {
844 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200845 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200846 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200847 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200848 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200849 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200850#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200851 NULL,
852 NULL,
853#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200854 NULL,
855 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100856 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200857 eckey_alloc_wrap, /* Same underlying key structure */
858 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200859#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200860 NULL,
861 NULL,
862#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200863 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200864};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_ECDSA_C)
868static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200869{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200871}
872
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400873#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400874/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500875 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
876 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500877 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500878static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
879 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500880{
Janos Follath24eed8d2019-11-22 13:21:35 +0000881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500882 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500883
Andrzej Kurek9241d182018-11-20 05:04:35 -0500884 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500885 MBEDTLS_ASN1_INTEGER ) ) != 0 )
886 {
887 return( ret );
888 }
889
Andrzej Kurek9241d182018-11-20 05:04:35 -0500890 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500891 {
892 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500893 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500894 }
895
Andrzej Kurek9241d182018-11-20 05:04:35 -0500896 if( unpadded_len > to_len || unpadded_len == 0 )
897 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500898
Andrzej Kurek9241d182018-11-20 05:04:35 -0500899 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500900 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500901 memcpy( to + padding_len, *from, unpadded_len );
902 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500903
Andrzej Kurek9241d182018-11-20 05:04:35 -0500904 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500905}
906
907/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400908 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500909 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500910 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400911 */
912static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500913 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400914{
Janos Follath24eed8d2019-11-22 13:21:35 +0000915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500916 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500917
Andrzej Kurek9241d182018-11-20 05:04:35 -0500918 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500919 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500920 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400921
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500922 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500923 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
924 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500925 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500926 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
927 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500928
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400929 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400930}
931
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100932static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400933 const unsigned char *hash, size_t hash_len,
934 const unsigned char *sig, size_t sig_len )
935{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100936 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200938 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100939 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200940 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400941 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400942 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100943 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000944 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400945 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700946 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100947 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100948 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100949 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
950 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700951 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400952
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500953 if( curve == 0 )
954 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
955
Hanno Beckerccf574e2019-01-29 08:26:15 +0000956 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500957 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400958 key.pk_info = &pk_info;
959 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000960 p = buf + sizeof( buf );
961 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400962 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500963 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400964
Gilles Peskined2d45c12019-05-27 14:53:13 +0200965 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100966 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200967 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500968
Gilles Peskined2d45c12019-05-27 14:53:13 +0200969 status = psa_import_key( &attributes,
970 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200971 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200972 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400973 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100974 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400975 goto cleanup;
976 }
977
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500978 /* We don't need the exported key anymore and can
979 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500980 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500981 {
982 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
983 goto cleanup;
984 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500985
Hanno Beckera9851112019-01-25 16:37:10 +0000986 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500987 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500988 signature_part_size ) ) != 0 )
989 {
990 goto cleanup;
991 }
992
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100993 status = psa_verify_hash( key_id, psa_sig_md,
994 hash, hash_len,
995 buf, 2 * signature_part_size );
996 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400997 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800998 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400999 goto cleanup;
1000 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -05001001
1002 if( p != sig + sig_len )
1003 {
1004 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1005 goto cleanup;
1006 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001007 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001008
Andrzej Kurekb7b04782018-11-19 17:01:16 -05001009cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +01001010 status = psa_destroy_key( key_id );
1011 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +01001012 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +01001013
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001014 return( ret );
1015}
1016#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001018 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001019 const unsigned char *sig, size_t sig_len )
1020{
Janos Follath24eed8d2019-11-22 13:21:35 +00001021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001022 ((void) md_alg);
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001025 hash, hash_len, sig, sig_len );
1026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1028 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001029
1030 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001031}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001032#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001033
Neil Armstronge9606902022-02-09 14:23:00 +01001034#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001035/*
1036 * Simultaneously convert and move raw MPI from the beginning of a buffer
1037 * to an ASN.1 MPI at the end of the buffer.
1038 * See also mbedtls_asn1_write_mpi().
1039 *
1040 * p: pointer to the end of the output buffer
1041 * start: start of the output buffer, and also of the mpi to write at the end
1042 * n_len: length of the mpi to read from start
1043 */
1044static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1045 size_t n_len )
1046{
1047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1048 size_t len = 0;
1049
1050 if( (size_t)( *p - start ) < n_len )
1051 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1052
1053 len = n_len;
1054 *p -= len;
1055 memmove( *p, start, len );
1056
1057 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1058 * Neither r nor s should be 0, but as a failsafe measure, still detect
1059 * that rather than overflowing the buffer in case of a PSA error. */
1060 while( len > 0 && **p == 0x00 )
1061 {
1062 ++(*p);
1063 --len;
1064 }
1065
1066 /* this is only reached if the signature was invalid */
1067 if( len == 0 )
1068 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1069
1070 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1071 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1072 if( **p & 0x80 )
1073 {
1074 if( *p - start < 1 )
1075 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1076
1077 *--(*p) = 0x00;
1078 len += 1;
1079 }
1080
1081 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1082 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1083 MBEDTLS_ASN1_INTEGER ) );
1084
1085 return( (int) len );
1086}
1087
1088/* Transcode signature from PSA format to ASN.1 sequence.
1089 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1090 * MPIs, and in-place.
1091 *
1092 * [in/out] sig: the signature pre- and post-transcoding
1093 * [in/out] sig_len: signature length pre- and post-transcoding
1094 * [int] buf_len: the available size the in/out buffer
1095 */
Neil Armstronge9606902022-02-09 14:23:00 +01001096static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001097 size_t buf_len )
1098{
1099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1100 size_t len = 0;
1101 const size_t rs_len = *sig_len / 2;
1102 unsigned char *p = sig + buf_len;
1103
1104 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1105 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1106
1107 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1108 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1109 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1110
1111 memmove( sig, p, len );
1112 *sig_len = len;
1113
1114 return( 0 );
1115}
Neil Armstronge9606902022-02-09 14:23:00 +01001116
Neil Armstrong17a06552022-03-18 15:27:38 +01001117/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1118 *
1119 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1120 * [in] end: ASN.1 buffer end
1121 * [out] key_len: the ECDSA privateKey length in bytes
1122 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001123static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1124 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001125{
1126 size_t len;
1127 int ret;
1128
Neil Armstrong17a06552022-03-18 15:27:38 +01001129 /*
1130 * RFC 5915, or SEC1 Appendix C.4
1131 *
1132 * ECPrivateKey ::= SEQUENCE {
1133 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1134 * privateKey OCTET STRING,
1135 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1136 * publicKey [1] BIT STRING OPTIONAL
1137 * }
1138 */
1139
Neil Armstronge9606902022-02-09 14:23:00 +01001140 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001141 MBEDTLS_ASN1_CONSTRUCTED |
1142 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001143 return( ret );
1144
1145 /* version */
1146 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1147 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1148 return( ret );
1149
1150 *buf += len;
1151
1152 /* privateKey */
1153 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1154 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1155 return( ret );
1156
1157 *key_len = len;
1158
1159 return 0;
1160}
1161
1162static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1163 const unsigned char *hash, size_t hash_len,
1164 unsigned char *sig, size_t sig_size, size_t *sig_len,
1165 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1166{
1167 mbedtls_ecdsa_context *ctx = ctx_arg;
1168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1170 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1171 psa_status_t status;
1172 mbedtls_pk_context key;
1173 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001174 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001175 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001176 psa_algorithm_t psa_sig_md =
1177 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001178 size_t curve_bits;
1179 psa_ecc_family_t curve =
1180 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001181
1182 /* PSA has its own RNG */
1183 ((void) f_rng);
1184 ((void) p_rng);
1185
1186 if( curve == 0 )
1187 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1188
1189 /* mbedtls_pk_write_key_der() expects a full PK context;
1190 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001191 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001192 key.pk_ctx = ctx;
1193 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1194 if( key_len <= 0 )
1195 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1196
1197 p = buf + sizeof( buf ) - key_len;
1198 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1199 if( ret != 0 )
1200 goto cleanup;
1201
1202 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1203 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1204 psa_set_key_algorithm( &attributes, psa_sig_md );
1205
1206 status = psa_import_key( &attributes,
1207 p, key_len,
1208 &key_id );
1209 if( status != PSA_SUCCESS )
1210 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001211 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001212 goto cleanup;
1213 }
1214
Neil Armstronge4edcf72022-03-03 16:46:41 +01001215 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1216 sig, sig_size, sig_len );
1217 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001218 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001219 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001220 goto cleanup;
1221 }
1222
1223 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1224
1225cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001226 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001227 status = psa_destroy_key( key_id );
1228 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001229 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001230
Neil Armstronge9606902022-02-09 14:23:00 +01001231 return( ret );
1232}
1233#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001235 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001236 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001237 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001240 md_alg, hash, hash_len,
1241 sig, sig_size, sig_len,
1242 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001243}
Neil Armstronge9606902022-02-09 14:23:00 +01001244#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001245
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001246#if defined(MBEDTLS_ECP_RESTARTABLE)
1247static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1248 const unsigned char *hash, size_t hash_len,
1249 const unsigned char *sig, size_t sig_len,
1250 void *rs_ctx )
1251{
Janos Follath24eed8d2019-11-22 13:21:35 +00001252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001253 ((void) md_alg);
1254
1255 ret = mbedtls_ecdsa_read_signature_restartable(
1256 (mbedtls_ecdsa_context *) ctx,
1257 hash, hash_len, sig, sig_len,
1258 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1259
1260 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1261 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1262
1263 return( ret );
1264}
1265
1266static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1267 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001268 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001269 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1270 void *rs_ctx )
1271{
1272 return( mbedtls_ecdsa_write_signature_restartable(
1273 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001274 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001275 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1276
1277}
1278#endif /* MBEDTLS_ECP_RESTARTABLE */
1279
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001280static void *ecdsa_alloc_wrap( void )
1281{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001282 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001283
1284 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001286
1287 return( ctx );
1288}
1289
1290static void ecdsa_free_wrap( void *ctx )
1291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1293 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001294}
1295
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001296#if defined(MBEDTLS_ECP_RESTARTABLE)
1297static void *ecdsa_rs_alloc( void )
1298{
1299 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1300
1301 if( ctx != NULL )
1302 mbedtls_ecdsa_restart_init( ctx );
1303
1304 return( ctx );
1305}
1306
1307static void ecdsa_rs_free( void *ctx )
1308{
1309 mbedtls_ecdsa_restart_free( ctx );
1310 mbedtls_free( ctx );
1311}
1312#endif /* MBEDTLS_ECP_RESTARTABLE */
1313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1315 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001316 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001317 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001318 ecdsa_can_do,
1319 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001320 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001321#if defined(MBEDTLS_ECP_RESTARTABLE)
1322 ecdsa_verify_rs_wrap,
1323 ecdsa_sign_rs_wrap,
1324#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001325 NULL,
1326 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001327 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001328 ecdsa_alloc_wrap,
1329 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001330#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001331 ecdsa_rs_alloc,
1332 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001333#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001334 eckey_debug, /* Compatible key structures */
1335};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001339/*
1340 * Support for alternative RSA-private implementations
1341 */
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001346}
1347
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001348static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001351
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001352 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001353}
1354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001357 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001358 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1359{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001361
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001362#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001363 if( UINT_MAX < hash_len )
1364 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001365#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001366
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001367 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001368 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1369 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001370 if( *sig_len > sig_size )
1371 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001372
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001373 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001374 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001375}
1376
1377static int rsa_alt_decrypt_wrap( void *ctx,
1378 const unsigned char *input, size_t ilen,
1379 unsigned char *output, size_t *olen, size_t osize,
1380 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001383
1384 ((void) f_rng);
1385 ((void) p_rng);
1386
1387 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001389
1390 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001391 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001392}
1393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001395static int rsa_alt_check_pair( const void *pub, const void *prv,
1396 int (*f_rng)(void *, unsigned char *, size_t),
1397 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001400 unsigned char hash[32];
1401 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001403
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001404 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001406
1407 memset( hash, 0x2a, sizeof( hash ) );
1408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001410 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001411 sig, sizeof( sig ), &sig_len,
1412 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001413 {
1414 return( ret );
1415 }
1416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001418 hash, sizeof( hash ), sig, sig_len ) != 0 )
1419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001421 }
1422
1423 return( 0 );
1424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001426
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001427static void *rsa_alt_alloc_wrap( void )
1428{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001429 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001430
1431 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001433
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001434 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001435}
1436
1437static void rsa_alt_free_wrap( void *ctx )
1438{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001439 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001441}
1442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1444 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001445 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001446 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001447 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001448 NULL,
1449 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001450#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001451 NULL,
1452 NULL,
1453#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001454 rsa_alt_decrypt_wrap,
1455 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001457 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001458#else
1459 NULL,
1460#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001461 rsa_alt_alloc_wrap,
1462 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001463#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001464 NULL,
1465 NULL,
1466#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001467 NULL,
1468};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001471
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001472#if defined(MBEDTLS_USE_PSA_CRYPTO)
1473
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001474static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001475{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001476 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001477
1478 /* no _init() function to call, an calloc() already zeroized */
1479
1480 return( ctx );
1481}
1482
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001483static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001484{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001485 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001486 mbedtls_free( ctx );
1487}
1488
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001489static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001490{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001491 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001492 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001494
Gilles Peskined2d45c12019-05-27 14:53:13 +02001495 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001496 return( 0 );
1497
Gilles Peskined2d45c12019-05-27 14:53:13 +02001498 bits = psa_get_key_bits( &attributes );
1499 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001500 return( bits );
1501}
1502
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001503static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001504{
1505 /* For now opaque PSA keys can only wrap ECC keypairs,
1506 * as checked by setup_psa().
1507 * Also, ECKEY_DH does not really make sense with the current API. */
1508 return( type == MBEDTLS_PK_ECKEY ||
1509 type == MBEDTLS_PK_ECDSA );
1510}
1511
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001512static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1513 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001514 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001515 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1516{
John Durkopf35069a2020-08-17 22:05:14 -07001517#if !defined(MBEDTLS_ECDSA_C)
1518 ((void) ctx);
1519 ((void) md_alg);
1520 ((void) hash);
1521 ((void) hash_len);
1522 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001523 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001524 ((void) sig_len);
1525 ((void) f_rng);
1526 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001527 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1528#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001529 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001530 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
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
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001537 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001538 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001539 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001540 if( status != PSA_SUCCESS )
Jerry Yu848ecce2022-03-22 10:58:48 +08001541 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001542
1543 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001544 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001545#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001546}
1547
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001548const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1549 MBEDTLS_PK_OPAQUE,
1550 "Opaque",
1551 pk_opaque_get_bitlen,
1552 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001553 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001554 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001555#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1556 NULL, /* restartable verify - not relevant */
1557 NULL, /* restartable sign - not relevant */
1558#endif
1559 NULL, /* decrypt - will be done later */
1560 NULL, /* encrypt - will be done later */
1561 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001562 pk_opaque_alloc_wrap,
1563 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001564#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1565 NULL, /* restart alloc - not relevant */
1566 NULL, /* restart free - not relevant */
1567#endif
1568 NULL, /* debug - could be done later, or even left NULL */
1569};
1570
1571#endif /* MBEDTLS_USE_PSA_CRYPTO */
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_PK_C */