blob: 76154ed3ace456b9888ad2782bea8ee37e5ceaa5 [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}
150#endif
151
Neil Armstrong19915c22022-03-01 15:21:02 +0100152#endif
153
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200168 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200169 const unsigned char *sig, size_t sig_len )
170{
Janos Follath24eed8d2019-11-22 13:21:35 +0000171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100172 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
173 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200174
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100175#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000176 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
177 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100178#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000179
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100180 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200182
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100183 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
184 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100185 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200186 return( ret );
187
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200188 /* The buffer contains a valid signature followed by extra data.
189 * We have a special error code for that so that so that callers can
190 * use mbedtls_pk_verify() to check "Does the buffer start with a
191 * valid signature?" and not just "Does the buffer contain a valid
192 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100193 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200195
196 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200197}
198
Jerry Yub02ee182022-03-16 10:30:41 +0800199#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yubf455e72022-03-22 21:39:41 +0800200int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg, void *pk_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800201 const unsigned char *hash, size_t hash_len,
202 unsigned char *sig, size_t sig_size,
203 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100204{
Jerry Yu1d172a32022-03-12 19:12:05 +0800205 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) pk_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
208 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
209 psa_status_t status;
210 mbedtls_pk_context key;
211 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100212 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100213 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100214
215 *sig_len = mbedtls_rsa_get_len( rsa );
216 if( sig_size < *sig_len )
217 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
218
Neil Armstronge4f28682022-02-24 15:41:39 +0100219 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100220 * re-construct one to make it happy */
221 key.pk_info = &pk_info;
Jerry Yu1d172a32022-03-12 19:12:05 +0800222 key.pk_ctx = pk_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100223 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
224 if( key_len <= 0 )
225 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100226 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800227 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100228 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
229
230 status = psa_import_key( &attributes,
231 buf + sizeof( buf ) - key_len, key_len,
232 &key_id );
233 if( status != PSA_SUCCESS )
234 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100235 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100236 goto cleanup;
237 }
Jerry Yubf455e72022-03-22 21:39:41 +0800238 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100239 sig, sig_size, sig_len );
240 if( status != PSA_SUCCESS )
241 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100242 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100243 goto cleanup;
244 }
245
246 ret = 0;
247
248cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100249 status = psa_destroy_key( key_id );
250 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100251 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100252 return( ret );
253}
Jerry Yu406cf272022-03-22 11:33:42 +0800254#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800255
256#if defined(MBEDTLS_USE_PSA_CRYPTO)
257static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
258 const unsigned char *hash, size_t hash_len,
259 unsigned char *sig, size_t sig_size, size_t *sig_len,
260 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
261{
262
263 ((void) f_rng);
264 ((void) p_rng);
265 ((void) md_alg);
266
267#if SIZE_MAX > UINT_MAX
268 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
269 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
270#endif /* SIZE_MAX > UINT_MAX */
271
Jerry Yu89107d12022-03-22 14:20:15 +0800272 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
273 mbedtls_psa_translate_md( md_alg ) ),
274 ctx, hash, hash_len,
275 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800276}
Neil Armstrong98545682022-02-22 16:12:51 +0100277#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200279 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200280 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200281 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
282{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100283 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
284
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100285#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000286 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
287 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100288#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000289
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100290 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200291 if( sig_size < *sig_len )
292 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200293
Thomas Daubney140184d2021-05-18 16:04:07 +0100294 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
295 md_alg, (unsigned int) hash_len,
296 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200297}
Neil Armstrong98545682022-02-22 16:12:51 +0100298#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200299
Neil Armstrong18f43c72022-02-09 15:32:45 +0100300#if defined(MBEDTLS_USE_PSA_CRYPTO)
301static int rsa_decrypt_wrap( void *ctx,
302 const unsigned char *input, size_t ilen,
303 unsigned char *output, size_t *olen, size_t osize,
304 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
305{
306 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
309 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
310 psa_status_t status;
311 mbedtls_pk_context key;
312 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100313 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100314
315 ((void) f_rng);
316 ((void) p_rng);
317
318#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100319 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
320 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
321#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100322
323 if( ilen != mbedtls_rsa_get_len( rsa ) )
324 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
325
326 /* mbedtls_pk_write_key_der() expects a full PK context;
327 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100328 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100329 key.pk_ctx = ctx;
330 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
331 if( key_len <= 0 )
332 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
333
334 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
335 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100336 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100337
338 status = psa_import_key( &attributes,
339 buf + sizeof( buf ) - key_len, key_len,
340 &key_id );
341 if( status != PSA_SUCCESS )
342 {
Neil Armstronge8780492022-03-03 16:54:16 +0100343 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100344 goto cleanup;
345 }
346
Neil Armstrong8e805042022-03-16 15:30:31 +0100347 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
348 input, ilen,
349 NULL, 0,
350 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100351 if( status != PSA_SUCCESS )
352 {
Neil Armstronge8780492022-03-03 16:54:16 +0100353 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100354 goto cleanup;
355 }
356
357 ret = 0;
358
359cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100360 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100361 status = psa_destroy_key( key_id );
362 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100363 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100364
Neil Armstrong18f43c72022-02-09 15:32:45 +0100365 return( ret );
366}
367#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200368static int rsa_decrypt_wrap( void *ctx,
369 const unsigned char *input, size_t ilen,
370 unsigned char *output, size_t *olen, size_t osize,
371 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
372{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100373 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
374
375 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200377
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100378 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100379 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200380}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100381#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200382
Neil Armstrong96a16a42022-02-10 10:40:11 +0100383#if defined(MBEDTLS_USE_PSA_CRYPTO)
384static int rsa_encrypt_wrap( void *ctx,
385 const unsigned char *input, size_t ilen,
386 unsigned char *output, size_t *olen, size_t osize,
387 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
388{
389 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
392 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
393 psa_status_t status;
394 mbedtls_pk_context key;
395 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100396 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100397
398 ((void) f_rng);
399 ((void) p_rng);
400
401#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100402 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
403 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100404#endif
405
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100406 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100407 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
408
Neil Armstrongac014ca2022-02-24 15:27:54 +0100409 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100410 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100411 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100412 key.pk_ctx = ctx;
413 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
414 if( key_len <= 0 )
415 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
416
417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100418 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100419 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
420
421 status = psa_import_key( &attributes,
422 buf + sizeof( buf ) - key_len, key_len,
423 &key_id );
424 if( status != PSA_SUCCESS )
425 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100426 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100427 goto cleanup;
428 }
429
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100430 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
431 input, ilen,
432 NULL, 0,
433 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100434 if( status != PSA_SUCCESS )
435 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100436 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100437 goto cleanup;
438 }
439
440 ret = 0;
441
442cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100443 status = psa_destroy_key( key_id );
444 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100445 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100446
Neil Armstrong96a16a42022-02-10 10:40:11 +0100447 return( ret );
448}
449#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200450static int rsa_encrypt_wrap( void *ctx,
451 const unsigned char *input, size_t ilen,
452 unsigned char *output, size_t *olen, size_t osize,
453 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
454{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100455 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
456 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200457
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100458 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100460
Thomas Daubney21772772021-05-13 17:30:32 +0100461 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100462 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200463}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100464#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200465
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200466static int rsa_check_pair_wrap( const void *pub, const void *prv,
467 int (*f_rng)(void *, unsigned char *, size_t),
468 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100469{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200470 (void) f_rng;
471 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
473 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100474}
475
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200476static void *rsa_alloc_wrap( void )
477{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200478 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200479
480 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200481 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200482
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200483 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200484}
485
486static void rsa_free_wrap( void *ctx )
487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
489 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200490}
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200493{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200494#if defined(MBEDTLS_RSA_ALT)
495 /* Not supported */
496 (void) ctx;
497 (void) items;
498#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200500 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200502
503 items++;
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200506 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200508#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200509}
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511const mbedtls_pk_info_t mbedtls_rsa_info = {
512 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200513 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200514 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200515 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200516 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200517 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200518#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200519 NULL,
520 NULL,
521#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200522 rsa_decrypt_wrap,
523 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100524 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200525 rsa_alloc_wrap,
526 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200527#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200528 NULL,
529 NULL,
530#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200531 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200532};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200536/*
537 * Generic EC key
538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200540{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( type == MBEDTLS_PK_ECKEY ||
542 type == MBEDTLS_PK_ECKEY_DH ||
543 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200544}
545
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200546static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200549}
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200552/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200554 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200555 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200558 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200559 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200560 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200563 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200564 const unsigned char *sig, size_t sig_len )
565{
Janos Follath24eed8d2019-11-22 13:21:35 +0000566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200572 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200575
576 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200577}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200580 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200581 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200582 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
583{
Janos Follath24eed8d2019-11-22 13:21:35 +0000584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200590 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
591 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200592 f_rng, p_rng );
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200595
596 return( ret );
597}
598
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200599#if defined(MBEDTLS_ECP_RESTARTABLE)
600/* Forward declarations */
601static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
602 const unsigned char *hash, size_t hash_len,
603 const unsigned char *sig, size_t sig_len,
604 void *rs_ctx );
605
606static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
607 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200608 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200609 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
610 void *rs_ctx );
611
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200612/*
613 * Restart context for ECDSA operations with ECKEY context
614 *
615 * We need to store an actual ECDSA context, as we need to pass the same to
616 * the underlying ecdsa function, so we can't create it on the fly every time.
617 */
618typedef struct
619{
620 mbedtls_ecdsa_restart_ctx ecdsa_rs;
621 mbedtls_ecdsa_context ecdsa_ctx;
622} eckey_restart_ctx;
623
624static void *eckey_rs_alloc( void )
625{
626 eckey_restart_ctx *rs_ctx;
627
628 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
629
630 if( ctx != NULL )
631 {
632 rs_ctx = ctx;
633 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
634 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
635 }
636
637 return( ctx );
638}
639
640static void eckey_rs_free( void *ctx )
641{
642 eckey_restart_ctx *rs_ctx;
643
644 if( ctx == NULL)
645 return;
646
647 rs_ctx = ctx;
648 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
649 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
650
651 mbedtls_free( ctx );
652}
653
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200654static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
655 const unsigned char *hash, size_t hash_len,
656 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200657 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200658{
Janos Follath24eed8d2019-11-22 13:21:35 +0000659 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200660 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200661
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200662 /* Should never happen */
663 if( rs == NULL )
664 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200665
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200666 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200667 if( rs->ecdsa_ctx.grp.pbits == 0 )
668 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200669
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200670 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
671 md_alg, hash, hash_len,
672 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200673
674cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200675 return( ret );
676}
677
678static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
679 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200680 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200681 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200682 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200683{
Janos Follath24eed8d2019-11-22 13:21:35 +0000684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200685 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200686
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200687 /* Should never happen */
688 if( rs == NULL )
689 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200690
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200691 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200692 if( rs->ecdsa_ctx.grp.pbits == 0 )
693 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200694
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200695 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200696 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200697 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200698
699cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200700 return( ret );
701}
702#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200704
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200705static int eckey_check_pair( const void *pub, const void *prv,
706 int (*f_rng)(void *, unsigned char *, size_t),
707 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200710 (const mbedtls_ecp_keypair *) prv,
711 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100712}
713
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200714static void *eckey_alloc_wrap( void )
715{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200716 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200717
718 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200720
721 return( ctx );
722}
723
724static void eckey_free_wrap( void *ctx )
725{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
727 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200728}
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200733 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200735}
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737const mbedtls_pk_info_t mbedtls_eckey_info = {
738 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200739 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200740 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200741 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200743 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200744 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200745#if defined(MBEDTLS_ECP_RESTARTABLE)
746 eckey_verify_rs_wrap,
747 eckey_sign_rs_wrap,
748#endif
749#else /* MBEDTLS_ECDSA_C */
750 NULL,
751 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200752#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200753 NULL,
754 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100755 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200756 eckey_alloc_wrap,
757 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200758#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200759 eckey_rs_alloc,
760 eckey_rs_free,
761#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200762 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200763};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200764
765/*
Paul Bakker75342a62014-04-08 17:35:40 +0200766 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( type == MBEDTLS_PK_ECKEY ||
771 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200772}
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774const mbedtls_pk_info_t mbedtls_eckeydh_info = {
775 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200776 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200777 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200778 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200779 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200780 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200781#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200782 NULL,
783 NULL,
784#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200785 NULL,
786 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100787 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200788 eckey_alloc_wrap, /* Same underlying key structure */
789 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200790#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200791 NULL,
792 NULL,
793#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200794 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200795};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_ECDSA_C)
799static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200800{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200802}
803
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400804#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400805/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500806 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
807 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500808 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500809static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
810 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500811{
Janos Follath24eed8d2019-11-22 13:21:35 +0000812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500813 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500814
Andrzej Kurek9241d182018-11-20 05:04:35 -0500815 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500816 MBEDTLS_ASN1_INTEGER ) ) != 0 )
817 {
818 return( ret );
819 }
820
Andrzej Kurek9241d182018-11-20 05:04:35 -0500821 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500822 {
823 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500824 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500825 }
826
Andrzej Kurek9241d182018-11-20 05:04:35 -0500827 if( unpadded_len > to_len || unpadded_len == 0 )
828 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500829
Andrzej Kurek9241d182018-11-20 05:04:35 -0500830 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500831 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500832 memcpy( to + padding_len, *from, unpadded_len );
833 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500834
Andrzej Kurek9241d182018-11-20 05:04:35 -0500835 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500836}
837
838/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400839 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500840 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500841 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400842 */
843static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500844 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400845{
Janos Follath24eed8d2019-11-22 13:21:35 +0000846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500847 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500848
Andrzej Kurek9241d182018-11-20 05:04:35 -0500849 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500850 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500851 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400852
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500853 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500854 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
855 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500856 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500857 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
858 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500859
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400860 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400861}
862
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100863static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400864 const unsigned char *hash, size_t hash_len,
865 const unsigned char *sig, size_t sig_len )
866{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100867 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100870 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200871 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400872 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400873 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100874 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000875 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400876 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700877 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100878 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100879 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100880 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
881 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700882 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400883
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500884 if( curve == 0 )
885 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
886
Hanno Beckerccf574e2019-01-29 08:26:15 +0000887 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500888 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400889 key.pk_info = &pk_info;
890 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000891 p = buf + sizeof( buf );
892 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400893 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500894 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400895
Gilles Peskined2d45c12019-05-27 14:53:13 +0200896 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100897 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200898 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500899
Gilles Peskined2d45c12019-05-27 14:53:13 +0200900 status = psa_import_key( &attributes,
901 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200902 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200903 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400904 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100905 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400906 goto cleanup;
907 }
908
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500909 /* We don't need the exported key anymore and can
910 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500911 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500912 {
913 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
914 goto cleanup;
915 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500916
Hanno Beckera9851112019-01-25 16:37:10 +0000917 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500918 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500919 signature_part_size ) ) != 0 )
920 {
921 goto cleanup;
922 }
923
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100924 status = psa_verify_hash( key_id, psa_sig_md,
925 hash, hash_len,
926 buf, 2 * signature_part_size );
927 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400928 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800929 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400930 goto cleanup;
931 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500932
933 if( p != sig + sig_len )
934 {
935 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
936 goto cleanup;
937 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400938 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400939
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500940cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +0100941 status = psa_destroy_key( key_id );
942 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100943 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +0100944
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400945 return( ret );
946}
947#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200949 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200950 const unsigned char *sig, size_t sig_len )
951{
Janos Follath24eed8d2019-11-22 13:21:35 +0000952 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200953 ((void) md_alg);
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200956 hash, hash_len, sig, sig_len );
957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
959 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200960
961 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200962}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400963#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200964
Neil Armstronge9606902022-02-09 14:23:00 +0100965#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100966/*
967 * Simultaneously convert and move raw MPI from the beginning of a buffer
968 * to an ASN.1 MPI at the end of the buffer.
969 * See also mbedtls_asn1_write_mpi().
970 *
971 * p: pointer to the end of the output buffer
972 * start: start of the output buffer, and also of the mpi to write at the end
973 * n_len: length of the mpi to read from start
974 */
975static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
976 size_t n_len )
977{
978 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
979 size_t len = 0;
980
981 if( (size_t)( *p - start ) < n_len )
982 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
983
984 len = n_len;
985 *p -= len;
986 memmove( *p, start, len );
987
988 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
989 * Neither r nor s should be 0, but as a failsafe measure, still detect
990 * that rather than overflowing the buffer in case of a PSA error. */
991 while( len > 0 && **p == 0x00 )
992 {
993 ++(*p);
994 --len;
995 }
996
997 /* this is only reached if the signature was invalid */
998 if( len == 0 )
999 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1000
1001 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1002 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1003 if( **p & 0x80 )
1004 {
1005 if( *p - start < 1 )
1006 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1007
1008 *--(*p) = 0x00;
1009 len += 1;
1010 }
1011
1012 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1013 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1014 MBEDTLS_ASN1_INTEGER ) );
1015
1016 return( (int) len );
1017}
1018
1019/* Transcode signature from PSA format to ASN.1 sequence.
1020 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1021 * MPIs, and in-place.
1022 *
1023 * [in/out] sig: the signature pre- and post-transcoding
1024 * [in/out] sig_len: signature length pre- and post-transcoding
1025 * [int] buf_len: the available size the in/out buffer
1026 */
Neil Armstronge9606902022-02-09 14:23:00 +01001027static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001028 size_t buf_len )
1029{
1030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1031 size_t len = 0;
1032 const size_t rs_len = *sig_len / 2;
1033 unsigned char *p = sig + buf_len;
1034
1035 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1036 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1037
1038 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1039 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1040 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1041
1042 memmove( sig, p, len );
1043 *sig_len = len;
1044
1045 return( 0 );
1046}
Neil Armstronge9606902022-02-09 14:23:00 +01001047
Neil Armstrong17a06552022-03-18 15:27:38 +01001048/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1049 *
1050 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1051 * [in] end: ASN.1 buffer end
1052 * [out] key_len: the ECDSA privateKey length in bytes
1053 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001054static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1055 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001056{
1057 size_t len;
1058 int ret;
1059
Neil Armstrong17a06552022-03-18 15:27:38 +01001060 /*
1061 * RFC 5915, or SEC1 Appendix C.4
1062 *
1063 * ECPrivateKey ::= SEQUENCE {
1064 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1065 * privateKey OCTET STRING,
1066 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1067 * publicKey [1] BIT STRING OPTIONAL
1068 * }
1069 */
1070
Neil Armstronge9606902022-02-09 14:23:00 +01001071 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001072 MBEDTLS_ASN1_CONSTRUCTED |
1073 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001074 return( ret );
1075
1076 /* version */
1077 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1078 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1079 return( ret );
1080
1081 *buf += len;
1082
1083 /* privateKey */
1084 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1085 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1086 return( ret );
1087
1088 *key_len = len;
1089
1090 return 0;
1091}
1092
1093static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1094 const unsigned char *hash, size_t hash_len,
1095 unsigned char *sig, size_t sig_size, size_t *sig_len,
1096 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1097{
1098 mbedtls_ecdsa_context *ctx = ctx_arg;
1099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1101 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1102 psa_status_t status;
1103 mbedtls_pk_context key;
1104 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001105 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001106 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001107 psa_algorithm_t psa_sig_md =
1108 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001109 size_t curve_bits;
1110 psa_ecc_family_t curve =
1111 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001112
1113 /* PSA has its own RNG */
1114 ((void) f_rng);
1115 ((void) p_rng);
1116
1117 if( curve == 0 )
1118 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1119
1120 /* mbedtls_pk_write_key_der() expects a full PK context;
1121 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001122 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001123 key.pk_ctx = ctx;
1124 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1125 if( key_len <= 0 )
1126 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1127
1128 p = buf + sizeof( buf ) - key_len;
1129 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1130 if( ret != 0 )
1131 goto cleanup;
1132
1133 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1134 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1135 psa_set_key_algorithm( &attributes, psa_sig_md );
1136
1137 status = psa_import_key( &attributes,
1138 p, key_len,
1139 &key_id );
1140 if( status != PSA_SUCCESS )
1141 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001142 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001143 goto cleanup;
1144 }
1145
Neil Armstronge4edcf72022-03-03 16:46:41 +01001146 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1147 sig, sig_size, sig_len );
1148 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001149 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001150 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001151 goto cleanup;
1152 }
1153
1154 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1155
1156cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001157 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001158 status = psa_destroy_key( key_id );
1159 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001160 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001161
Neil Armstronge9606902022-02-09 14:23:00 +01001162 return( ret );
1163}
1164#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001166 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001167 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001168 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001171 md_alg, hash, hash_len,
1172 sig, sig_size, sig_len,
1173 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001174}
Neil Armstronge9606902022-02-09 14:23:00 +01001175#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001176
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001177#if defined(MBEDTLS_ECP_RESTARTABLE)
1178static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1179 const unsigned char *hash, size_t hash_len,
1180 const unsigned char *sig, size_t sig_len,
1181 void *rs_ctx )
1182{
Janos Follath24eed8d2019-11-22 13:21:35 +00001183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001184 ((void) md_alg);
1185
1186 ret = mbedtls_ecdsa_read_signature_restartable(
1187 (mbedtls_ecdsa_context *) ctx,
1188 hash, hash_len, sig, sig_len,
1189 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1190
1191 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1192 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1193
1194 return( ret );
1195}
1196
1197static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1198 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001199 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001200 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1201 void *rs_ctx )
1202{
1203 return( mbedtls_ecdsa_write_signature_restartable(
1204 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001205 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001206 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1207
1208}
1209#endif /* MBEDTLS_ECP_RESTARTABLE */
1210
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001211static void *ecdsa_alloc_wrap( void )
1212{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001213 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001214
1215 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001217
1218 return( ctx );
1219}
1220
1221static void ecdsa_free_wrap( void *ctx )
1222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1224 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001225}
1226
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001227#if defined(MBEDTLS_ECP_RESTARTABLE)
1228static void *ecdsa_rs_alloc( void )
1229{
1230 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1231
1232 if( ctx != NULL )
1233 mbedtls_ecdsa_restart_init( ctx );
1234
1235 return( ctx );
1236}
1237
1238static void ecdsa_rs_free( void *ctx )
1239{
1240 mbedtls_ecdsa_restart_free( ctx );
1241 mbedtls_free( ctx );
1242}
1243#endif /* MBEDTLS_ECP_RESTARTABLE */
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1246 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001247 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001248 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001249 ecdsa_can_do,
1250 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001251 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001252#if defined(MBEDTLS_ECP_RESTARTABLE)
1253 ecdsa_verify_rs_wrap,
1254 ecdsa_sign_rs_wrap,
1255#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001256 NULL,
1257 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001258 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001259 ecdsa_alloc_wrap,
1260 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001261#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001262 ecdsa_rs_alloc,
1263 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001264#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001265 eckey_debug, /* Compatible key structures */
1266};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001270/*
1271 * Support for alternative RSA-private implementations
1272 */
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001277}
1278
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001279static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001282
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001283 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001284}
1285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001287 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001288 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001289 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001292
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001293#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001294 if( UINT_MAX < hash_len )
1295 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001296#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001297
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001298 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001299 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1300 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001301 if( *sig_len > sig_size )
1302 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001303
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001304 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001305 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001306}
1307
1308static int rsa_alt_decrypt_wrap( void *ctx,
1309 const unsigned char *input, size_t ilen,
1310 unsigned char *output, size_t *olen, size_t osize,
1311 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001314
1315 ((void) f_rng);
1316 ((void) p_rng);
1317
1318 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001320
1321 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001322 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001323}
1324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001326static int rsa_alt_check_pair( const void *pub, const void *prv,
1327 int (*f_rng)(void *, unsigned char *, size_t),
1328 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001331 unsigned char hash[32];
1332 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001334
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001335 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
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 memset( hash, 0x2a, sizeof( hash ) );
1339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001341 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001342 sig, sizeof( sig ), &sig_len,
1343 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001344 {
1345 return( ret );
1346 }
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001349 hash, sizeof( hash ), sig, sig_len ) != 0 )
1350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001352 }
1353
1354 return( 0 );
1355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001357
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001358static void *rsa_alt_alloc_wrap( void )
1359{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001360 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001361
1362 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001364
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001365 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001366}
1367
1368static void rsa_alt_free_wrap( void *ctx )
1369{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001370 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001372}
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1375 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001376 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001377 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001378 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001379 NULL,
1380 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001381#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001382 NULL,
1383 NULL,
1384#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001385 rsa_alt_decrypt_wrap,
1386 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001388 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001389#else
1390 NULL,
1391#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001392 rsa_alt_alloc_wrap,
1393 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001394#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001395 NULL,
1396 NULL,
1397#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001398 NULL,
1399};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001402
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001403#if defined(MBEDTLS_USE_PSA_CRYPTO)
1404
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001405static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001406{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001407 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001408
1409 /* no _init() function to call, an calloc() already zeroized */
1410
1411 return( ctx );
1412}
1413
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001414static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001415{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001416 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001417 mbedtls_free( ctx );
1418}
1419
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001420static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001421{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001422 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001423 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001425
Gilles Peskined2d45c12019-05-27 14:53:13 +02001426 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001427 return( 0 );
1428
Gilles Peskined2d45c12019-05-27 14:53:13 +02001429 bits = psa_get_key_bits( &attributes );
1430 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001431 return( bits );
1432}
1433
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001434static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001435{
1436 /* For now opaque PSA keys can only wrap ECC keypairs,
1437 * as checked by setup_psa().
1438 * Also, ECKEY_DH does not really make sense with the current API. */
1439 return( type == MBEDTLS_PK_ECKEY ||
1440 type == MBEDTLS_PK_ECDSA );
1441}
1442
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001443static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1444 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001445 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001446 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1447{
John Durkopf35069a2020-08-17 22:05:14 -07001448#if !defined(MBEDTLS_ECDSA_C)
1449 ((void) ctx);
1450 ((void) md_alg);
1451 ((void) hash);
1452 ((void) hash_len);
1453 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001454 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001455 ((void) sig_len);
1456 ((void) f_rng);
1457 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001458 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1459#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001460 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001461 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001462 psa_status_t status;
1463
1464 /* PSA has its own RNG */
1465 (void) f_rng;
1466 (void) p_rng;
1467
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001468 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001469 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001470 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001471 if( status != PSA_SUCCESS )
Jerry Yu848ecce2022-03-22 10:58:48 +08001472 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001473
1474 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001475 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001476#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001477}
1478
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001479const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1480 MBEDTLS_PK_OPAQUE,
1481 "Opaque",
1482 pk_opaque_get_bitlen,
1483 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001484 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001485 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001486#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1487 NULL, /* restartable verify - not relevant */
1488 NULL, /* restartable sign - not relevant */
1489#endif
1490 NULL, /* decrypt - will be done later */
1491 NULL, /* encrypt - will be done later */
1492 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001493 pk_opaque_alloc_wrap,
1494 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001495#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1496 NULL, /* restart alloc - not relevant */
1497 NULL, /* restart free - not relevant */
1498#endif
1499 NULL, /* debug - could be done later, or even left NULL */
1500};
1501
1502#endif /* MBEDTLS_USE_PSA_CRYPTO */
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_PK_C */