blob: 92e9bf4bc46489c5a2e889a1f858fb5de6bca070 [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
Neil Armstrong19915c22022-03-01 15:21:02 +010068#if defined(MBEDTLS_USE_PSA_CRYPTO)
69int 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
103#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
104int mbedtls_pk_error_from_psa_ecdca( psa_status_t status )
105{
106 switch( status )
107 {
108 case PSA_ERROR_NOT_PERMITTED:
109 case PSA_ERROR_INVALID_ARGUMENT:
110 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
111 case PSA_ERROR_INVALID_HANDLE:
112 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
113 case PSA_ERROR_BUFFER_TOO_SMALL:
114 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
115 case PSA_ERROR_INSUFFICIENT_ENTROPY:
116 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
117 case PSA_ERROR_INVALID_SIGNATURE:
118 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
119 default:
120 return( mbedtls_pk_error_from_psa( status ) );
121 }
122}
123#endif
124
125#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
126int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
127{
128 switch( status )
129 {
130 case PSA_ERROR_NOT_PERMITTED:
131 case PSA_ERROR_INVALID_ARGUMENT:
132 case PSA_ERROR_INVALID_HANDLE:
133 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
134 case PSA_ERROR_BUFFER_TOO_SMALL:
135 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
136 case PSA_ERROR_INSUFFICIENT_ENTROPY:
137 return( MBEDTLS_ERR_RSA_RNG_FAILED );
138 case PSA_ERROR_INVALID_SIGNATURE:
139 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
140 case PSA_ERROR_INVALID_PADDING:
141 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
142 default:
143 return( mbedtls_pk_error_from_psa( status ) );
144 }
145}
146#endif
147#endif
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_RSA_C)
150static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 return( type == MBEDTLS_PK_RSA ||
153 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200154}
155
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200156static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200157{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100158 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
159 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200160}
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200163 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200164 const unsigned char *sig, size_t sig_len )
165{
Janos Follath24eed8d2019-11-22 13:21:35 +0000166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100167 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
168 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200169
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100170#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000171 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
172 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100173#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000174
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100175 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200177
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100178 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
179 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100180 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200181 return( ret );
182
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200183 /* The buffer contains a valid signature followed by extra data.
184 * We have a special error code for that so that so that callers can
185 * use mbedtls_pk_verify() to check "Does the buffer start with a
186 * valid signature?" and not just "Does the buffer contain a valid
187 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100188 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200190
191 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200192}
193
Neil Armstrong98545682022-02-22 16:12:51 +0100194#if defined(MBEDTLS_USE_PSA_CRYPTO)
195static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
196 const unsigned char *hash, size_t hash_len,
197 unsigned char *sig, size_t sig_size, size_t *sig_len,
198 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
199{
200 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
203 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
204 psa_status_t status;
205 mbedtls_pk_context key;
206 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100207 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100208 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong66fa7692022-03-03 12:32:15 +0100209 psa_algorithm_t psa_alg_md =
210 PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_psa_translate_md( md_alg ) );
Neil Armstrong98545682022-02-22 16:12:51 +0100211
212 ((void) f_rng);
213 ((void) p_rng);
214
215#if SIZE_MAX > UINT_MAX
216 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
217 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
218#endif /* SIZE_MAX > UINT_MAX */
219
220 *sig_len = mbedtls_rsa_get_len( rsa );
221 if( sig_size < *sig_len )
222 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
223
Neil Armstronge4f28682022-02-24 15:41:39 +0100224 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100225 * re-construct one to make it happy */
226 key.pk_info = &pk_info;
227 key.pk_ctx = ctx;
228 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
229 if( key_len <= 0 )
230 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
231
232 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
233 psa_set_key_algorithm( &attributes, psa_alg_md );
234 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
235
236 status = psa_import_key( &attributes,
237 buf + sizeof( buf ) - key_len, key_len,
238 &key_id );
239 if( status != PSA_SUCCESS )
240 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100241 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100242 goto cleanup;
243 }
244
245 status = psa_sign_hash( key_id, psa_alg_md, hash, hash_len,
246 sig, sig_size, sig_len );
247 if( status != PSA_SUCCESS )
248 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100249 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100250 goto cleanup;
251 }
252
253 ret = 0;
254
255cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100256 status = psa_destroy_key( key_id );
257 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100258 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong48a98332022-02-24 16:56:46 +0100259
Neil Armstrong98545682022-02-22 16:12:51 +0100260 return( ret );
261}
262#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200264 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200265 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200266 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
267{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100268 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
269
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100270#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000271 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
272 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100273#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000274
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100275 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200276 if( sig_size < *sig_len )
277 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200278
Thomas Daubney140184d2021-05-18 16:04:07 +0100279 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
280 md_alg, (unsigned int) hash_len,
281 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200282}
Neil Armstrong98545682022-02-22 16:12:51 +0100283#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200284
Neil Armstrong18f43c72022-02-09 15:32:45 +0100285#if defined(MBEDTLS_USE_PSA_CRYPTO)
286static int rsa_decrypt_wrap( void *ctx,
287 const unsigned char *input, size_t ilen,
288 unsigned char *output, size_t *olen, size_t osize,
289 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
290{
291 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
294 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
295 psa_status_t status;
296 mbedtls_pk_context key;
297 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100298 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100299
300 ((void) f_rng);
301 ((void) p_rng);
302
303#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100304 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
305 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
306#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100307
308 if( ilen != mbedtls_rsa_get_len( rsa ) )
309 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
310
311 /* mbedtls_pk_write_key_der() expects a full PK context;
312 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100313 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100314 key.pk_ctx = ctx;
315 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
316 if( key_len <= 0 )
317 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
318
319 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
320 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100321 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100322
323 status = psa_import_key( &attributes,
324 buf + sizeof( buf ) - key_len, key_len,
325 &key_id );
326 if( status != PSA_SUCCESS )
327 {
Neil Armstronge8780492022-03-03 16:54:16 +0100328 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100329 goto cleanup;
330 }
331
Neil Armstrong8e805042022-03-16 15:30:31 +0100332 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
333 input, ilen,
334 NULL, 0,
335 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100336 if( status != PSA_SUCCESS )
337 {
Neil Armstronge8780492022-03-03 16:54:16 +0100338 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100339 goto cleanup;
340 }
341
342 ret = 0;
343
344cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100345 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100346 status = psa_destroy_key( key_id );
347 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100348 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100349
Neil Armstrong18f43c72022-02-09 15:32:45 +0100350 return( ret );
351}
352#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200353static int rsa_decrypt_wrap( void *ctx,
354 const unsigned char *input, size_t ilen,
355 unsigned char *output, size_t *olen, size_t osize,
356 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
357{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100358 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
359
360 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200362
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100363 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100364 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200365}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100366#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200367
Neil Armstrong96a16a42022-02-10 10:40:11 +0100368#if defined(MBEDTLS_USE_PSA_CRYPTO)
369static int rsa_encrypt_wrap( void *ctx,
370 const unsigned char *input, size_t ilen,
371 unsigned char *output, size_t *olen, size_t osize,
372 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
373{
374 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
377 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
378 psa_status_t status;
379 mbedtls_pk_context key;
380 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100381 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100382
383 ((void) f_rng);
384 ((void) p_rng);
385
386#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100387 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
388 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100389#endif
390
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100391 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100392 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
393
Neil Armstrongac014ca2022-02-24 15:27:54 +0100394 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100395 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100396 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100397 key.pk_ctx = ctx;
398 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
399 if( key_len <= 0 )
400 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
401
402 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100403 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100404 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
405
406 status = psa_import_key( &attributes,
407 buf + sizeof( buf ) - key_len, key_len,
408 &key_id );
409 if( status != PSA_SUCCESS )
410 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100411 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100412 goto cleanup;
413 }
414
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100415 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
416 input, ilen,
417 NULL, 0,
418 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100419 if( status != PSA_SUCCESS )
420 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100421 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100422 goto cleanup;
423 }
424
425 ret = 0;
426
427cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100428 status = psa_destroy_key( key_id );
429 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100430 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100431
Neil Armstrong96a16a42022-02-10 10:40:11 +0100432 return( ret );
433}
434#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200435static int rsa_encrypt_wrap( void *ctx,
436 const unsigned char *input, size_t ilen,
437 unsigned char *output, size_t *olen, size_t osize,
438 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
439{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100440 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
441 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200442
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100443 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100445
Thomas Daubney21772772021-05-13 17:30:32 +0100446 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100447 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200448}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100449#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200450
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200451static int rsa_check_pair_wrap( const void *pub, const void *prv,
452 int (*f_rng)(void *, unsigned char *, size_t),
453 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100454{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200455 (void) f_rng;
456 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
458 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100459}
460
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200461static void *rsa_alloc_wrap( void )
462{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200463 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200464
465 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200466 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200467
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200468 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200469}
470
471static void rsa_free_wrap( void *ctx )
472{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
474 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200475}
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200478{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200479#if defined(MBEDTLS_RSA_ALT)
480 /* Not supported */
481 (void) ctx;
482 (void) items;
483#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200485 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200487
488 items++;
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200491 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200493#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200494}
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496const mbedtls_pk_info_t mbedtls_rsa_info = {
497 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200498 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200499 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200500 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200501 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200502 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200503#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200504 NULL,
505 NULL,
506#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200507 rsa_decrypt_wrap,
508 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100509 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200510 rsa_alloc_wrap,
511 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200512#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200513 NULL,
514 NULL,
515#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200516 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200517};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200521/*
522 * Generic EC key
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( type == MBEDTLS_PK_ECKEY ||
527 type == MBEDTLS_PK_ECKEY_DH ||
528 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200529}
530
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200531static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200534}
535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200537/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200539 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200540 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200543 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200544 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200545 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200548 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200549 const unsigned char *sig, size_t sig_len )
550{
Janos Follath24eed8d2019-11-22 13:21:35 +0000551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200557 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200560
561 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200562}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200565 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200566 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200567 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
568{
Janos Follath24eed8d2019-11-22 13:21:35 +0000569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200575 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
576 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200577 f_rng, p_rng );
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200580
581 return( ret );
582}
583
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200584#if defined(MBEDTLS_ECP_RESTARTABLE)
585/* Forward declarations */
586static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
587 const unsigned char *hash, size_t hash_len,
588 const unsigned char *sig, size_t sig_len,
589 void *rs_ctx );
590
591static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
592 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200593 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200594 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
595 void *rs_ctx );
596
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200597/*
598 * Restart context for ECDSA operations with ECKEY context
599 *
600 * We need to store an actual ECDSA context, as we need to pass the same to
601 * the underlying ecdsa function, so we can't create it on the fly every time.
602 */
603typedef struct
604{
605 mbedtls_ecdsa_restart_ctx ecdsa_rs;
606 mbedtls_ecdsa_context ecdsa_ctx;
607} eckey_restart_ctx;
608
609static void *eckey_rs_alloc( void )
610{
611 eckey_restart_ctx *rs_ctx;
612
613 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
614
615 if( ctx != NULL )
616 {
617 rs_ctx = ctx;
618 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
619 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
620 }
621
622 return( ctx );
623}
624
625static void eckey_rs_free( void *ctx )
626{
627 eckey_restart_ctx *rs_ctx;
628
629 if( ctx == NULL)
630 return;
631
632 rs_ctx = ctx;
633 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
634 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
635
636 mbedtls_free( ctx );
637}
638
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200639static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
640 const unsigned char *hash, size_t hash_len,
641 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200642 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200643{
Janos Follath24eed8d2019-11-22 13:21:35 +0000644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200645 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200646
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200647 /* Should never happen */
648 if( rs == NULL )
649 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200650
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200651 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200652 if( rs->ecdsa_ctx.grp.pbits == 0 )
653 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200654
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200655 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
656 md_alg, hash, hash_len,
657 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200658
659cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200660 return( ret );
661}
662
663static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
664 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200665 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200666 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200667 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200668{
Janos Follath24eed8d2019-11-22 13:21:35 +0000669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200670 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200671
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200672 /* Should never happen */
673 if( rs == NULL )
674 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200675
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200676 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200677 if( rs->ecdsa_ctx.grp.pbits == 0 )
678 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200679
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200680 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200681 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200682 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200683
684cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200685 return( ret );
686}
687#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200689
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200690static int eckey_check_pair( const void *pub, const void *prv,
691 int (*f_rng)(void *, unsigned char *, size_t),
692 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200695 (const mbedtls_ecp_keypair *) prv,
696 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100697}
698
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200699static void *eckey_alloc_wrap( void )
700{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200701 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200702
703 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200705
706 return( ctx );
707}
708
709static void eckey_free_wrap( void *ctx )
710{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
712 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200713}
714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200718 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200720}
721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722const mbedtls_pk_info_t mbedtls_eckey_info = {
723 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200724 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200725 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200726 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200728 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200729 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200730#if defined(MBEDTLS_ECP_RESTARTABLE)
731 eckey_verify_rs_wrap,
732 eckey_sign_rs_wrap,
733#endif
734#else /* MBEDTLS_ECDSA_C */
735 NULL,
736 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200737#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200738 NULL,
739 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100740 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200741 eckey_alloc_wrap,
742 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200743#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200744 eckey_rs_alloc,
745 eckey_rs_free,
746#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200747 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200748};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200749
750/*
Paul Bakker75342a62014-04-08 17:35:40 +0200751 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200754{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 return( type == MBEDTLS_PK_ECKEY ||
756 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200757}
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759const mbedtls_pk_info_t mbedtls_eckeydh_info = {
760 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200761 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200762 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200763 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200764 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200765 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200766#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200767 NULL,
768 NULL,
769#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200770 NULL,
771 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100772 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200773 eckey_alloc_wrap, /* Same underlying key structure */
774 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200775#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200776 NULL,
777 NULL,
778#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200779 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200780};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#if defined(MBEDTLS_ECDSA_C)
784static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200785{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200787}
788
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400789#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400790/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500791 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
792 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500793 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500794static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
795 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500796{
Janos Follath24eed8d2019-11-22 13:21:35 +0000797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500798 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500799
Andrzej Kurek9241d182018-11-20 05:04:35 -0500800 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500801 MBEDTLS_ASN1_INTEGER ) ) != 0 )
802 {
803 return( ret );
804 }
805
Andrzej Kurek9241d182018-11-20 05:04:35 -0500806 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500807 {
808 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500809 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500810 }
811
Andrzej Kurek9241d182018-11-20 05:04:35 -0500812 if( unpadded_len > to_len || unpadded_len == 0 )
813 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500814
Andrzej Kurek9241d182018-11-20 05:04:35 -0500815 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500816 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500817 memcpy( to + padding_len, *from, unpadded_len );
818 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500819
Andrzej Kurek9241d182018-11-20 05:04:35 -0500820 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500821}
822
823/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400824 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500825 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500826 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400827 */
828static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500829 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400830{
Janos Follath24eed8d2019-11-22 13:21:35 +0000831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500832 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500833
Andrzej Kurek9241d182018-11-20 05:04:35 -0500834 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500835 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500836 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400837
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500838 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500839 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
840 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500841 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500842 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
843 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500844
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400845 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400846}
847
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100848static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400849 const unsigned char *hash, size_t hash_len,
850 const unsigned char *sig, size_t sig_len )
851{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100852 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200854 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100855 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200856 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400857 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400858 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100859 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000860 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400861 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700862 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100863 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100864 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100865 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
866 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700867 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400868
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500869 if( curve == 0 )
870 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
871
Hanno Beckerccf574e2019-01-29 08:26:15 +0000872 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500873 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400874 key.pk_info = &pk_info;
875 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000876 p = buf + sizeof( buf );
877 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400878 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500879 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400880
Gilles Peskined2d45c12019-05-27 14:53:13 +0200881 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200883 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500884
Gilles Peskined2d45c12019-05-27 14:53:13 +0200885 status = psa_import_key( &attributes,
886 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200887 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200888 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400889 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100890 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400891 goto cleanup;
892 }
893
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500894 /* We don't need the exported key anymore and can
895 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500896 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500897 {
898 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
899 goto cleanup;
900 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500901
Hanno Beckera9851112019-01-25 16:37:10 +0000902 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500903 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500904 signature_part_size ) ) != 0 )
905 {
906 goto cleanup;
907 }
908
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100909 status = psa_verify_hash( key_id, psa_sig_md,
910 hash, hash_len,
911 buf, 2 * signature_part_size );
912 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400913 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100914 ret = mbedtls_pk_error_from_psa_ecdca( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400915 goto cleanup;
916 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500917
918 if( p != sig + sig_len )
919 {
920 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
921 goto cleanup;
922 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400923 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400924
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500925cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +0100926 status = psa_destroy_key( key_id );
927 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100928 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +0100929
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400930 return( ret );
931}
932#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200934 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200935 const unsigned char *sig, size_t sig_len )
936{
Janos Follath24eed8d2019-11-22 13:21:35 +0000937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200938 ((void) md_alg);
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200941 hash, hash_len, sig, sig_len );
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
944 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200945
946 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200947}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400948#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200949
Neil Armstronge9606902022-02-09 14:23:00 +0100950#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100951/*
952 * Simultaneously convert and move raw MPI from the beginning of a buffer
953 * to an ASN.1 MPI at the end of the buffer.
954 * See also mbedtls_asn1_write_mpi().
955 *
956 * p: pointer to the end of the output buffer
957 * start: start of the output buffer, and also of the mpi to write at the end
958 * n_len: length of the mpi to read from start
959 */
960static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
961 size_t n_len )
962{
963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
964 size_t len = 0;
965
966 if( (size_t)( *p - start ) < n_len )
967 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
968
969 len = n_len;
970 *p -= len;
971 memmove( *p, start, len );
972
973 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
974 * Neither r nor s should be 0, but as a failsafe measure, still detect
975 * that rather than overflowing the buffer in case of a PSA error. */
976 while( len > 0 && **p == 0x00 )
977 {
978 ++(*p);
979 --len;
980 }
981
982 /* this is only reached if the signature was invalid */
983 if( len == 0 )
984 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
985
986 /* if the msb is 1, ASN.1 requires that we prepend a 0.
987 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
988 if( **p & 0x80 )
989 {
990 if( *p - start < 1 )
991 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
992
993 *--(*p) = 0x00;
994 len += 1;
995 }
996
997 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
998 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
999 MBEDTLS_ASN1_INTEGER ) );
1000
1001 return( (int) len );
1002}
1003
1004/* Transcode signature from PSA format to ASN.1 sequence.
1005 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1006 * MPIs, and in-place.
1007 *
1008 * [in/out] sig: the signature pre- and post-transcoding
1009 * [in/out] sig_len: signature length pre- and post-transcoding
1010 * [int] buf_len: the available size the in/out buffer
1011 */
Neil Armstronge9606902022-02-09 14:23:00 +01001012static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001013 size_t buf_len )
1014{
1015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1016 size_t len = 0;
1017 const size_t rs_len = *sig_len / 2;
1018 unsigned char *p = sig + buf_len;
1019
1020 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1021 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1022
1023 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1024 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1025 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1026
1027 memmove( sig, p, len );
1028 *sig_len = len;
1029
1030 return( 0 );
1031}
Neil Armstronge9606902022-02-09 14:23:00 +01001032
Neil Armstrong17a06552022-03-18 15:27:38 +01001033/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1034 *
1035 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1036 * [in] end: ASN.1 buffer end
1037 * [out] key_len: the ECDSA privateKey length in bytes
1038 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001039static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1040 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001041{
1042 size_t len;
1043 int ret;
1044
Neil Armstrong17a06552022-03-18 15:27:38 +01001045 /*
1046 * RFC 5915, or SEC1 Appendix C.4
1047 *
1048 * ECPrivateKey ::= SEQUENCE {
1049 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1050 * privateKey OCTET STRING,
1051 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1052 * publicKey [1] BIT STRING OPTIONAL
1053 * }
1054 */
1055
Neil Armstronge9606902022-02-09 14:23:00 +01001056 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001057 MBEDTLS_ASN1_CONSTRUCTED |
1058 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001059 return( ret );
1060
1061 /* version */
1062 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1063 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1064 return( ret );
1065
1066 *buf += len;
1067
1068 /* privateKey */
1069 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1070 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1071 return( ret );
1072
1073 *key_len = len;
1074
1075 return 0;
1076}
1077
1078static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1079 const unsigned char *hash, size_t hash_len,
1080 unsigned char *sig, size_t sig_size, size_t *sig_len,
1081 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1082{
1083 mbedtls_ecdsa_context *ctx = ctx_arg;
1084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1086 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1087 psa_status_t status;
1088 mbedtls_pk_context key;
1089 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001090 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001091 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001092 psa_algorithm_t psa_sig_md =
1093 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001094 size_t curve_bits;
1095 psa_ecc_family_t curve =
1096 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001097
1098 /* PSA has its own RNG */
1099 ((void) f_rng);
1100 ((void) p_rng);
1101
1102 if( curve == 0 )
1103 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1104
1105 /* mbedtls_pk_write_key_der() expects a full PK context;
1106 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001107 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001108 key.pk_ctx = ctx;
1109 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1110 if( key_len <= 0 )
1111 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1112
1113 p = buf + sizeof( buf ) - key_len;
1114 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1115 if( ret != 0 )
1116 goto cleanup;
1117
1118 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1119 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1120 psa_set_key_algorithm( &attributes, psa_sig_md );
1121
1122 status = psa_import_key( &attributes,
1123 p, key_len,
1124 &key_id );
1125 if( status != PSA_SUCCESS )
1126 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001127 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001128 goto cleanup;
1129 }
1130
Neil Armstronge4edcf72022-03-03 16:46:41 +01001131 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1132 sig, sig_size, sig_len );
1133 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001134 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001135 ret = mbedtls_pk_error_from_psa_ecdca( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001136 goto cleanup;
1137 }
1138
1139 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1140
1141cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001142 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001143 status = psa_destroy_key( key_id );
1144 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001145 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001146
Neil Armstronge9606902022-02-09 14:23:00 +01001147 return( ret );
1148}
1149#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001151 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001152 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001153 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001156 md_alg, hash, hash_len,
1157 sig, sig_size, sig_len,
1158 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001159}
Neil Armstronge9606902022-02-09 14:23:00 +01001160#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001161
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001162#if defined(MBEDTLS_ECP_RESTARTABLE)
1163static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1164 const unsigned char *hash, size_t hash_len,
1165 const unsigned char *sig, size_t sig_len,
1166 void *rs_ctx )
1167{
Janos Follath24eed8d2019-11-22 13:21:35 +00001168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001169 ((void) md_alg);
1170
1171 ret = mbedtls_ecdsa_read_signature_restartable(
1172 (mbedtls_ecdsa_context *) ctx,
1173 hash, hash_len, sig, sig_len,
1174 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1175
1176 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1177 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1178
1179 return( ret );
1180}
1181
1182static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1183 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001184 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001185 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1186 void *rs_ctx )
1187{
1188 return( mbedtls_ecdsa_write_signature_restartable(
1189 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001190 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001191 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1192
1193}
1194#endif /* MBEDTLS_ECP_RESTARTABLE */
1195
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001196static void *ecdsa_alloc_wrap( void )
1197{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001198 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001199
1200 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001202
1203 return( ctx );
1204}
1205
1206static void ecdsa_free_wrap( void *ctx )
1207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1209 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001210}
1211
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001212#if defined(MBEDTLS_ECP_RESTARTABLE)
1213static void *ecdsa_rs_alloc( void )
1214{
1215 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1216
1217 if( ctx != NULL )
1218 mbedtls_ecdsa_restart_init( ctx );
1219
1220 return( ctx );
1221}
1222
1223static void ecdsa_rs_free( void *ctx )
1224{
1225 mbedtls_ecdsa_restart_free( ctx );
1226 mbedtls_free( ctx );
1227}
1228#endif /* MBEDTLS_ECP_RESTARTABLE */
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1231 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001232 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001233 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001234 ecdsa_can_do,
1235 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001236 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001237#if defined(MBEDTLS_ECP_RESTARTABLE)
1238 ecdsa_verify_rs_wrap,
1239 ecdsa_sign_rs_wrap,
1240#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001241 NULL,
1242 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001243 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001244 ecdsa_alloc_wrap,
1245 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001246#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001247 ecdsa_rs_alloc,
1248 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001249#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001250 eckey_debug, /* Compatible key structures */
1251};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001255/*
1256 * Support for alternative RSA-private implementations
1257 */
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001262}
1263
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001264static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001265{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001267
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001268 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001269}
1270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001272 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001273 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001274 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001277
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001278#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001279 if( UINT_MAX < hash_len )
1280 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001281#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001282
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001283 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001284 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1285 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001286 if( *sig_len > sig_size )
1287 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001288
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001289 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001290 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001291}
1292
1293static int rsa_alt_decrypt_wrap( void *ctx,
1294 const unsigned char *input, size_t ilen,
1295 unsigned char *output, size_t *olen, size_t osize,
1296 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001299
1300 ((void) f_rng);
1301 ((void) p_rng);
1302
1303 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001305
1306 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001307 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001308}
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001311static int rsa_alt_check_pair( const void *pub, const void *prv,
1312 int (*f_rng)(void *, unsigned char *, size_t),
1313 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001316 unsigned char hash[32];
1317 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001319
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001320 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001322
1323 memset( hash, 0x2a, sizeof( hash ) );
1324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001326 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001327 sig, sizeof( sig ), &sig_len,
1328 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001329 {
1330 return( ret );
1331 }
1332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001334 hash, sizeof( hash ), sig, sig_len ) != 0 )
1335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001337 }
1338
1339 return( 0 );
1340}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001342
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001343static void *rsa_alt_alloc_wrap( void )
1344{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001345 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001346
1347 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001350 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001351}
1352
1353static void rsa_alt_free_wrap( void *ctx )
1354{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001355 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001357}
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1360 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001361 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001362 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001363 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001364 NULL,
1365 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001366#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001367 NULL,
1368 NULL,
1369#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001370 rsa_alt_decrypt_wrap,
1371 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001373 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001374#else
1375 NULL,
1376#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001377 rsa_alt_alloc_wrap,
1378 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001379#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001380 NULL,
1381 NULL,
1382#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001383 NULL,
1384};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001387
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001388#if defined(MBEDTLS_USE_PSA_CRYPTO)
1389
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001390static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001391{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001392 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001393
1394 /* no _init() function to call, an calloc() already zeroized */
1395
1396 return( ctx );
1397}
1398
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001399static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001400{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001401 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001402 mbedtls_free( ctx );
1403}
1404
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001405static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001406{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001407 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001408 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001410
Gilles Peskined2d45c12019-05-27 14:53:13 +02001411 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001412 return( 0 );
1413
Gilles Peskined2d45c12019-05-27 14:53:13 +02001414 bits = psa_get_key_bits( &attributes );
1415 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001416 return( bits );
1417}
1418
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001419static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001420{
1421 /* For now opaque PSA keys can only wrap ECC keypairs,
1422 * as checked by setup_psa().
1423 * Also, ECKEY_DH does not really make sense with the current API. */
1424 return( type == MBEDTLS_PK_ECKEY ||
1425 type == MBEDTLS_PK_ECDSA );
1426}
1427
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001428static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1429 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001430 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001431 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1432{
John Durkopf35069a2020-08-17 22:05:14 -07001433#if !defined(MBEDTLS_ECDSA_C)
1434 ((void) ctx);
1435 ((void) md_alg);
1436 ((void) hash);
1437 ((void) hash_len);
1438 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001439 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001440 ((void) sig_len);
1441 ((void) f_rng);
1442 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001443 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1444#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001445 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001446 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001447 psa_status_t status;
1448
1449 /* PSA has its own RNG */
1450 (void) f_rng;
1451 (void) p_rng;
1452
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001453 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001454 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001455 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001456 if( status != PSA_SUCCESS )
Neil Armstrong19915c22022-03-01 15:21:02 +01001457 return( mbedtls_pk_error_from_psa_ecdca( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001458
1459 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001460 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001461#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001462}
1463
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001464const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1465 MBEDTLS_PK_OPAQUE,
1466 "Opaque",
1467 pk_opaque_get_bitlen,
1468 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001469 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001470 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001471#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1472 NULL, /* restartable verify - not relevant */
1473 NULL, /* restartable sign - not relevant */
1474#endif
1475 NULL, /* decrypt - will be done later */
1476 NULL, /* encrypt - will be done later */
1477 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001478 pk_opaque_alloc_wrap,
1479 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001480#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1481 NULL, /* restart alloc - not relevant */
1482 NULL, /* restart free - not relevant */
1483#endif
1484 NULL, /* debug - could be done later, or even left NULL */
1485};
1486
1487#endif /* MBEDTLS_USE_PSA_CRYPTO */
1488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_PK_C */