blob: 2569b9c72915728565ae4332357dcb5cf95f35d9 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000023#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Neil Armstrong0f49f832022-02-28 15:07:38 +010035#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
36#include "pkwrite.h"
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#endif
42
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "mbedtls/asn1write.h"
45#endif
46
Andres Amaya Garciae32df082017-10-25 09:37:04 +010047#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010049#endif
50
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040052#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010053#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040054#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059#else
60#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020061#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020063#endif
64
Andres AG72849872017-01-19 11:24:33 +000065#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010066#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020067
Jerry Yub02ee182022-03-16 10:30:41 +080068#if defined(MBEDTLS_PSA_CRYPTO_C)
Neil Armstrong19915c22022-03-01 15:21:02 +010069int mbedtls_pk_error_from_psa( psa_status_t status )
70{
71 switch( status )
72 {
73 case PSA_SUCCESS:
74 return( 0 );
75 case PSA_ERROR_INVALID_HANDLE:
76 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
77 case PSA_ERROR_NOT_PERMITTED:
78 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
79 case PSA_ERROR_BUFFER_TOO_SMALL:
80 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
81 case PSA_ERROR_NOT_SUPPORTED:
82 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
83 case PSA_ERROR_INVALID_ARGUMENT:
84 return( MBEDTLS_ERR_PK_INVALID_ALG );
85 case PSA_ERROR_INSUFFICIENT_MEMORY:
86 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
87 case PSA_ERROR_BAD_STATE:
88 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
89 case PSA_ERROR_COMMUNICATION_FAILURE:
90 case PSA_ERROR_HARDWARE_FAILURE:
91 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
92 case PSA_ERROR_DATA_CORRUPT:
93 case PSA_ERROR_DATA_INVALID:
94 case PSA_ERROR_STORAGE_FAILURE:
95 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
96 case PSA_ERROR_CORRUPTION_DETECTED:
97 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
98 default:
99 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
100 }
101}
102
Neil Armstrong19915c22022-03-01 15:21:02 +0100103#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
104int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
105{
106 switch( status )
107 {
108 case PSA_ERROR_NOT_PERMITTED:
109 case PSA_ERROR_INVALID_ARGUMENT:
110 case PSA_ERROR_INVALID_HANDLE:
111 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
112 case PSA_ERROR_BUFFER_TOO_SMALL:
113 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
114 case PSA_ERROR_INSUFFICIENT_ENTROPY:
115 return( MBEDTLS_ERR_RSA_RNG_FAILED );
116 case PSA_ERROR_INVALID_SIGNATURE:
117 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
118 case PSA_ERROR_INVALID_PADDING:
119 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
120 default:
121 return( mbedtls_pk_error_from_psa( status ) );
122 }
123}
124#endif
Jerry Yu07869e82022-03-16 16:40:50 +0800125
126#endif /* MBEDTLS_PSA_CRYPTO_C */
127
128#if defined(MBEDTLS_USE_PSA_CRYPTO)
129
130#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Jerry Yu848ecce2022-03-22 10:58:48 +0800131int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status )
Jerry Yu07869e82022-03-16 16:40:50 +0800132{
133 switch( status )
134 {
135 case PSA_ERROR_NOT_PERMITTED:
136 case PSA_ERROR_INVALID_ARGUMENT:
137 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
138 case PSA_ERROR_INVALID_HANDLE:
139 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
140 case PSA_ERROR_BUFFER_TOO_SMALL:
141 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
142 case PSA_ERROR_INSUFFICIENT_ENTROPY:
143 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
144 case PSA_ERROR_INVALID_SIGNATURE:
145 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
146 default:
147 return( mbedtls_pk_error_from_psa( status ) );
148 }
149}
Jerry Yu75339822022-03-23 12:06:31 +0800150#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800151
Jerry Yu75339822022-03-23 12:06:31 +0800152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_RSA_C)
155static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( type == MBEDTLS_PK_RSA ||
158 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200159}
160
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200161static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200162{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100163 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
164 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200165}
166
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 Yue010de42022-03-23 11:45:55 +0800200int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
201 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800202 const unsigned char *hash, size_t hash_len,
203 unsigned char *sig, size_t sig_size,
204 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100205{
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
Jerry Yue010de42022-03-23 11:45:55 +0800215 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100216 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 Yue010de42022-03-23 11:45:55 +0800222 key.pk_ctx = rsa_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{
Jerry Yu1d172a32022-03-12 19:12:05 +0800262 ((void) f_rng);
263 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800264
Jerry Yubd1b3272022-03-24 13:05:20 +0800265 psa_algorithm_t psa_md_alg;
266 psa_md_alg = mbedtls_psa_translate_md( md_alg );
267 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800268 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800269
Jerry Yubd1b3272022-03-24 13:05:20 +0800270 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
271 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800272 ctx, hash, hash_len,
273 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800274}
Neil Armstrong98545682022-02-22 16:12:51 +0100275#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200277 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200278 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200279 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
280{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100281 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
282
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100283#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000284 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
285 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100286#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000287
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100288 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200289 if( sig_size < *sig_len )
290 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200291
Thomas Daubney140184d2021-05-18 16:04:07 +0100292 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
293 md_alg, (unsigned int) hash_len,
294 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200295}
Neil Armstrong98545682022-02-22 16:12:51 +0100296#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200297
Neil Armstrong18f43c72022-02-09 15:32:45 +0100298#if defined(MBEDTLS_USE_PSA_CRYPTO)
299static int rsa_decrypt_wrap( void *ctx,
300 const unsigned char *input, size_t ilen,
301 unsigned char *output, size_t *olen, size_t osize,
302 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
303{
304 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
307 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
308 psa_status_t status;
309 mbedtls_pk_context key;
310 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100311 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100312
313 ((void) f_rng);
314 ((void) p_rng);
315
316#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100317 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
318 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
319#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100320
321 if( ilen != mbedtls_rsa_get_len( rsa ) )
322 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
323
324 /* mbedtls_pk_write_key_der() expects a full PK context;
325 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100326 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100327 key.pk_ctx = ctx;
328 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
329 if( key_len <= 0 )
330 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
331
332 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100334 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100335
336 status = psa_import_key( &attributes,
337 buf + sizeof( buf ) - key_len, key_len,
338 &key_id );
339 if( status != PSA_SUCCESS )
340 {
Neil Armstronge8780492022-03-03 16:54:16 +0100341 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100342 goto cleanup;
343 }
344
Neil Armstrong8e805042022-03-16 15:30:31 +0100345 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
346 input, ilen,
347 NULL, 0,
348 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100349 if( status != PSA_SUCCESS )
350 {
Neil Armstronge8780492022-03-03 16:54:16 +0100351 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100352 goto cleanup;
353 }
354
355 ret = 0;
356
357cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100358 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100359 status = psa_destroy_key( key_id );
360 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100361 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100362
Neil Armstrong18f43c72022-02-09 15:32:45 +0100363 return( ret );
364}
365#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200366static int rsa_decrypt_wrap( void *ctx,
367 const unsigned char *input, size_t ilen,
368 unsigned char *output, size_t *olen, size_t osize,
369 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
370{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100371 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
372
373 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200375
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100376 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100377 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200378}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100379#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200380
Neil Armstrong96a16a42022-02-10 10:40:11 +0100381#if defined(MBEDTLS_USE_PSA_CRYPTO)
382static int rsa_encrypt_wrap( void *ctx,
383 const unsigned char *input, size_t ilen,
384 unsigned char *output, size_t *olen, size_t osize,
385 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
386{
387 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
390 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
391 psa_status_t status;
392 mbedtls_pk_context key;
393 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100394 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100395
396 ((void) f_rng);
397 ((void) p_rng);
398
399#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100400 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
401 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100402#endif
403
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100404 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100405 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
406
Neil Armstrongac014ca2022-02-24 15:27:54 +0100407 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100408 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100409 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100410 key.pk_ctx = ctx;
411 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
412 if( key_len <= 0 )
413 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
414
415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100416 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100417 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
418
419 status = psa_import_key( &attributes,
420 buf + sizeof( buf ) - key_len, key_len,
421 &key_id );
422 if( status != PSA_SUCCESS )
423 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100424 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100425 goto cleanup;
426 }
427
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100428 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
429 input, ilen,
430 NULL, 0,
431 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100432 if( status != PSA_SUCCESS )
433 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100434 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100435 goto cleanup;
436 }
437
438 ret = 0;
439
440cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100441 status = psa_destroy_key( key_id );
442 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100443 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100444
Neil Armstrong96a16a42022-02-10 10:40:11 +0100445 return( ret );
446}
447#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200448static int rsa_encrypt_wrap( void *ctx,
449 const unsigned char *input, size_t ilen,
450 unsigned char *output, size_t *olen, size_t osize,
451 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
452{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100453 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
454 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200455
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100456 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100458
Thomas Daubney21772772021-05-13 17:30:32 +0100459 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100460 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200461}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100462#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200463
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200464static int rsa_check_pair_wrap( const void *pub, const void *prv,
465 int (*f_rng)(void *, unsigned char *, size_t),
466 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100467{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200468 (void) f_rng;
469 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
471 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100472}
473
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200474static void *rsa_alloc_wrap( void )
475{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200476 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200477
478 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200479 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200480
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200481 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200482}
483
484static void rsa_free_wrap( void *ctx )
485{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
487 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200488}
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200491{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200492#if defined(MBEDTLS_RSA_ALT)
493 /* Not supported */
494 (void) ctx;
495 (void) items;
496#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200498 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200500
501 items++;
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200504 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200506#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200507}
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509const mbedtls_pk_info_t mbedtls_rsa_info = {
510 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200511 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200512 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200513 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200514 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200515 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200516#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200517 NULL,
518 NULL,
519#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200520 rsa_decrypt_wrap,
521 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100522 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200523 rsa_alloc_wrap,
524 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200525#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200526 NULL,
527 NULL,
528#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200529 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200530};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200534/*
535 * Generic EC key
536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200538{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( type == MBEDTLS_PK_ECKEY ||
540 type == MBEDTLS_PK_ECKEY_DH ||
541 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200542}
543
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200544static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200547}
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200550/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200552 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200553 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200556 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200557 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200558 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200561 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200562 const unsigned char *sig, size_t sig_len )
563{
Janos Follath24eed8d2019-11-22 13:21:35 +0000564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200570 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200573
574 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200575}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200578 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200579 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200580 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
581{
Janos Follath24eed8d2019-11-22 13:21:35 +0000582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200588 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
589 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200590 f_rng, p_rng );
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200593
594 return( ret );
595}
596
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200597#if defined(MBEDTLS_ECP_RESTARTABLE)
598/* Forward declarations */
599static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
600 const unsigned char *hash, size_t hash_len,
601 const unsigned char *sig, size_t sig_len,
602 void *rs_ctx );
603
604static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
605 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200606 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200607 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
608 void *rs_ctx );
609
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200610/*
611 * Restart context for ECDSA operations with ECKEY context
612 *
613 * We need to store an actual ECDSA context, as we need to pass the same to
614 * the underlying ecdsa function, so we can't create it on the fly every time.
615 */
616typedef struct
617{
618 mbedtls_ecdsa_restart_ctx ecdsa_rs;
619 mbedtls_ecdsa_context ecdsa_ctx;
620} eckey_restart_ctx;
621
622static void *eckey_rs_alloc( void )
623{
624 eckey_restart_ctx *rs_ctx;
625
626 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
627
628 if( ctx != NULL )
629 {
630 rs_ctx = ctx;
631 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
632 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
633 }
634
635 return( ctx );
636}
637
638static void eckey_rs_free( void *ctx )
639{
640 eckey_restart_ctx *rs_ctx;
641
642 if( ctx == NULL)
643 return;
644
645 rs_ctx = ctx;
646 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
647 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
648
649 mbedtls_free( ctx );
650}
651
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200652static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
653 const unsigned char *hash, size_t hash_len,
654 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200655 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200656{
Janos Follath24eed8d2019-11-22 13:21:35 +0000657 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200658 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200659
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200660 /* Should never happen */
661 if( rs == NULL )
662 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200663
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200664 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200665 if( rs->ecdsa_ctx.grp.pbits == 0 )
666 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200667
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200668 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
669 md_alg, hash, hash_len,
670 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200671
672cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200673 return( ret );
674}
675
676static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
677 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200678 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200679 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200680 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200681{
Janos Follath24eed8d2019-11-22 13:21:35 +0000682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200683 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200684
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200685 /* Should never happen */
686 if( rs == NULL )
687 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200688
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200689 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200690 if( rs->ecdsa_ctx.grp.pbits == 0 )
691 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200692
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200693 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200694 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200695 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200696
697cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200698 return( ret );
699}
700#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200702
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200703static int eckey_check_pair( const void *pub, const void *prv,
704 int (*f_rng)(void *, unsigned char *, size_t),
705 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200708 (const mbedtls_ecp_keypair *) prv,
709 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100710}
711
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200712static void *eckey_alloc_wrap( void )
713{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200714 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200715
716 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200718
719 return( ctx );
720}
721
722static void eckey_free_wrap( void *ctx )
723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
725 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200726}
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200729{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200731 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200733}
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735const mbedtls_pk_info_t mbedtls_eckey_info = {
736 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200737 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200738 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200739 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200741 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200742 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200743#if defined(MBEDTLS_ECP_RESTARTABLE)
744 eckey_verify_rs_wrap,
745 eckey_sign_rs_wrap,
746#endif
747#else /* MBEDTLS_ECDSA_C */
748 NULL,
749 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200750#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200751 NULL,
752 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100753 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200754 eckey_alloc_wrap,
755 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200756#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200757 eckey_rs_alloc,
758 eckey_rs_free,
759#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200760 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200761};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200762
763/*
Paul Bakker75342a62014-04-08 17:35:40 +0200764 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 return( type == MBEDTLS_PK_ECKEY ||
769 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200770}
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772const mbedtls_pk_info_t mbedtls_eckeydh_info = {
773 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200774 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200775 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200776 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200777 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200778 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200779#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200780 NULL,
781 NULL,
782#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200783 NULL,
784 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100785 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200786 eckey_alloc_wrap, /* Same underlying key structure */
787 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200788#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200789 NULL,
790 NULL,
791#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200792 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200793};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_ECDSA_C)
797static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200798{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200800}
801
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400802#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400803/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500804 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
805 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500806 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500807static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
808 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500809{
Janos Follath24eed8d2019-11-22 13:21:35 +0000810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500811 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500812
Andrzej Kurek9241d182018-11-20 05:04:35 -0500813 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500814 MBEDTLS_ASN1_INTEGER ) ) != 0 )
815 {
816 return( ret );
817 }
818
Andrzej Kurek9241d182018-11-20 05:04:35 -0500819 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500820 {
821 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500822 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500823 }
824
Andrzej Kurek9241d182018-11-20 05:04:35 -0500825 if( unpadded_len > to_len || unpadded_len == 0 )
826 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500827
Andrzej Kurek9241d182018-11-20 05:04:35 -0500828 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500829 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500830 memcpy( to + padding_len, *from, unpadded_len );
831 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500832
Andrzej Kurek9241d182018-11-20 05:04:35 -0500833 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500834}
835
836/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400837 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500838 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500839 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400840 */
841static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500842 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400843{
Janos Follath24eed8d2019-11-22 13:21:35 +0000844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500845 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500846
Andrzej Kurek9241d182018-11-20 05:04:35 -0500847 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500848 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500849 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400850
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500851 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500852 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
853 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500854 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500855 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
856 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500857
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400858 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400859}
860
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100861static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400862 const unsigned char *hash, size_t hash_len,
863 const unsigned char *sig, size_t sig_len )
864{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100865 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100868 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200869 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400870 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400871 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100872 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000873 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400874 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700875 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100876 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100877 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100878 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
879 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700880 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400881
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500882 if( curve == 0 )
883 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
884
Hanno Beckerccf574e2019-01-29 08:26:15 +0000885 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500886 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400887 key.pk_info = &pk_info;
888 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000889 p = buf + sizeof( buf );
890 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400891 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500892 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400893
Gilles Peskined2d45c12019-05-27 14:53:13 +0200894 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200896 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500897
Gilles Peskined2d45c12019-05-27 14:53:13 +0200898 status = psa_import_key( &attributes,
899 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200900 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200901 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400902 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100903 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400904 goto cleanup;
905 }
906
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500907 /* We don't need the exported key anymore and can
908 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500909 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500910 {
911 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
912 goto cleanup;
913 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500914
Hanno Beckera9851112019-01-25 16:37:10 +0000915 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500916 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500917 signature_part_size ) ) != 0 )
918 {
919 goto cleanup;
920 }
921
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100922 status = psa_verify_hash( key_id, psa_sig_md,
923 hash, hash_len,
924 buf, 2 * signature_part_size );
925 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400926 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800927 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400928 goto cleanup;
929 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500930
931 if( p != sig + sig_len )
932 {
933 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
934 goto cleanup;
935 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400936 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400937
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500938cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +0100939 status = psa_destroy_key( key_id );
940 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100941 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +0100942
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400943 return( ret );
944}
945#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200947 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200948 const unsigned char *sig, size_t sig_len )
949{
Janos Follath24eed8d2019-11-22 13:21:35 +0000950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200951 ((void) md_alg);
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200954 hash, hash_len, sig, sig_len );
955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
957 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200958
959 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200960}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400961#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200962
Neil Armstronge9606902022-02-09 14:23:00 +0100963#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100964/*
965 * Simultaneously convert and move raw MPI from the beginning of a buffer
966 * to an ASN.1 MPI at the end of the buffer.
967 * See also mbedtls_asn1_write_mpi().
968 *
969 * p: pointer to the end of the output buffer
970 * start: start of the output buffer, and also of the mpi to write at the end
971 * n_len: length of the mpi to read from start
972 */
973static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
974 size_t n_len )
975{
976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
977 size_t len = 0;
978
979 if( (size_t)( *p - start ) < n_len )
980 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
981
982 len = n_len;
983 *p -= len;
984 memmove( *p, start, len );
985
986 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
987 * Neither r nor s should be 0, but as a failsafe measure, still detect
988 * that rather than overflowing the buffer in case of a PSA error. */
989 while( len > 0 && **p == 0x00 )
990 {
991 ++(*p);
992 --len;
993 }
994
995 /* this is only reached if the signature was invalid */
996 if( len == 0 )
997 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
998
999 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1000 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1001 if( **p & 0x80 )
1002 {
1003 if( *p - start < 1 )
1004 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1005
1006 *--(*p) = 0x00;
1007 len += 1;
1008 }
1009
1010 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1011 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1012 MBEDTLS_ASN1_INTEGER ) );
1013
1014 return( (int) len );
1015}
1016
1017/* Transcode signature from PSA format to ASN.1 sequence.
1018 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1019 * MPIs, and in-place.
1020 *
1021 * [in/out] sig: the signature pre- and post-transcoding
1022 * [in/out] sig_len: signature length pre- and post-transcoding
1023 * [int] buf_len: the available size the in/out buffer
1024 */
Neil Armstronge9606902022-02-09 14:23:00 +01001025static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001026 size_t buf_len )
1027{
1028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1029 size_t len = 0;
1030 const size_t rs_len = *sig_len / 2;
1031 unsigned char *p = sig + buf_len;
1032
1033 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1034 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1035
1036 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1037 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1038 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1039
1040 memmove( sig, p, len );
1041 *sig_len = len;
1042
1043 return( 0 );
1044}
Neil Armstronge9606902022-02-09 14:23:00 +01001045
Neil Armstrong17a06552022-03-18 15:27:38 +01001046/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1047 *
1048 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1049 * [in] end: ASN.1 buffer end
1050 * [out] key_len: the ECDSA privateKey length in bytes
1051 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001052static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1053 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001054{
1055 size_t len;
1056 int ret;
1057
Neil Armstrong17a06552022-03-18 15:27:38 +01001058 /*
1059 * RFC 5915, or SEC1 Appendix C.4
1060 *
1061 * ECPrivateKey ::= SEQUENCE {
1062 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1063 * privateKey OCTET STRING,
1064 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1065 * publicKey [1] BIT STRING OPTIONAL
1066 * }
1067 */
1068
Neil Armstronge9606902022-02-09 14:23:00 +01001069 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001070 MBEDTLS_ASN1_CONSTRUCTED |
1071 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001072 return( ret );
1073
1074 /* version */
1075 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1076 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1077 return( ret );
1078
1079 *buf += len;
1080
1081 /* privateKey */
1082 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1083 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1084 return( ret );
1085
1086 *key_len = len;
1087
1088 return 0;
1089}
1090
1091static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1092 const unsigned char *hash, size_t hash_len,
1093 unsigned char *sig, size_t sig_size, size_t *sig_len,
1094 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1095{
1096 mbedtls_ecdsa_context *ctx = ctx_arg;
1097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1099 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1100 psa_status_t status;
1101 mbedtls_pk_context key;
1102 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001103 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001104 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001105 psa_algorithm_t psa_sig_md =
1106 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001107 size_t curve_bits;
1108 psa_ecc_family_t curve =
1109 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001110
1111 /* PSA has its own RNG */
1112 ((void) f_rng);
1113 ((void) p_rng);
1114
1115 if( curve == 0 )
1116 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1117
1118 /* mbedtls_pk_write_key_der() expects a full PK context;
1119 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001120 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001121 key.pk_ctx = ctx;
1122 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1123 if( key_len <= 0 )
1124 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1125
1126 p = buf + sizeof( buf ) - key_len;
1127 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1128 if( ret != 0 )
1129 goto cleanup;
1130
1131 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1132 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1133 psa_set_key_algorithm( &attributes, psa_sig_md );
1134
1135 status = psa_import_key( &attributes,
1136 p, key_len,
1137 &key_id );
1138 if( status != PSA_SUCCESS )
1139 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001140 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001141 goto cleanup;
1142 }
1143
Neil Armstronge4edcf72022-03-03 16:46:41 +01001144 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1145 sig, sig_size, sig_len );
1146 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001147 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001148 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001149 goto cleanup;
1150 }
1151
1152 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1153
1154cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001155 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001156 status = psa_destroy_key( key_id );
1157 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001158 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001159
Neil Armstronge9606902022-02-09 14:23:00 +01001160 return( ret );
1161}
1162#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001164 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001165 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001166 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001169 md_alg, hash, hash_len,
1170 sig, sig_size, sig_len,
1171 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001172}
Neil Armstronge9606902022-02-09 14:23:00 +01001173#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001174
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001175#if defined(MBEDTLS_ECP_RESTARTABLE)
1176static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1177 const unsigned char *hash, size_t hash_len,
1178 const unsigned char *sig, size_t sig_len,
1179 void *rs_ctx )
1180{
Janos Follath24eed8d2019-11-22 13:21:35 +00001181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001182 ((void) md_alg);
1183
1184 ret = mbedtls_ecdsa_read_signature_restartable(
1185 (mbedtls_ecdsa_context *) ctx,
1186 hash, hash_len, sig, sig_len,
1187 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1188
1189 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1190 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1191
1192 return( ret );
1193}
1194
1195static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1196 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001197 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001198 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1199 void *rs_ctx )
1200{
1201 return( mbedtls_ecdsa_write_signature_restartable(
1202 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001203 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001204 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1205
1206}
1207#endif /* MBEDTLS_ECP_RESTARTABLE */
1208
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001209static void *ecdsa_alloc_wrap( void )
1210{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001211 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001212
1213 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001215
1216 return( ctx );
1217}
1218
1219static void ecdsa_free_wrap( void *ctx )
1220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1222 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001223}
1224
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001225#if defined(MBEDTLS_ECP_RESTARTABLE)
1226static void *ecdsa_rs_alloc( void )
1227{
1228 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1229
1230 if( ctx != NULL )
1231 mbedtls_ecdsa_restart_init( ctx );
1232
1233 return( ctx );
1234}
1235
1236static void ecdsa_rs_free( void *ctx )
1237{
1238 mbedtls_ecdsa_restart_free( ctx );
1239 mbedtls_free( ctx );
1240}
1241#endif /* MBEDTLS_ECP_RESTARTABLE */
1242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1244 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001245 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001246 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001247 ecdsa_can_do,
1248 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001249 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001250#if defined(MBEDTLS_ECP_RESTARTABLE)
1251 ecdsa_verify_rs_wrap,
1252 ecdsa_sign_rs_wrap,
1253#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001254 NULL,
1255 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001256 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001257 ecdsa_alloc_wrap,
1258 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001259#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001260 ecdsa_rs_alloc,
1261 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001262#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001263 eckey_debug, /* Compatible key structures */
1264};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001268/*
1269 * Support for alternative RSA-private implementations
1270 */
1271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001275}
1276
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001277static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001278{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001280
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001281 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001282}
1283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001285 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001286 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001287 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001290
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001291#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001292 if( UINT_MAX < hash_len )
1293 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001294#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001295
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001296 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001297 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1298 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001299 if( *sig_len > sig_size )
1300 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001301
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001302 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001303 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001304}
1305
1306static int rsa_alt_decrypt_wrap( void *ctx,
1307 const unsigned char *input, size_t ilen,
1308 unsigned char *output, size_t *olen, size_t osize,
1309 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1310{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001312
1313 ((void) f_rng);
1314 ((void) p_rng);
1315
1316 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001318
1319 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001320 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001321}
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001324static int rsa_alt_check_pair( const void *pub, const void *prv,
1325 int (*f_rng)(void *, unsigned char *, size_t),
1326 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001329 unsigned char hash[32];
1330 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001332
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001333 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001335
1336 memset( hash, 0x2a, sizeof( hash ) );
1337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001339 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001340 sig, sizeof( sig ), &sig_len,
1341 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001342 {
1343 return( ret );
1344 }
1345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001347 hash, sizeof( hash ), sig, sig_len ) != 0 )
1348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001350 }
1351
1352 return( 0 );
1353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001355
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356static void *rsa_alt_alloc_wrap( void )
1357{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001358 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001359
1360 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001362
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001363 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001364}
1365
1366static void rsa_alt_free_wrap( void *ctx )
1367{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001368 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001370}
1371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1373 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001374 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001375 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001376 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001377 NULL,
1378 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001379#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001380 NULL,
1381 NULL,
1382#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001383 rsa_alt_decrypt_wrap,
1384 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001386 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001387#else
1388 NULL,
1389#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001390 rsa_alt_alloc_wrap,
1391 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001392#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001393 NULL,
1394 NULL,
1395#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001396 NULL,
1397};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001400
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001401#if defined(MBEDTLS_USE_PSA_CRYPTO)
1402
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001403static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001404{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001405 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001406
1407 /* no _init() function to call, an calloc() already zeroized */
1408
1409 return( ctx );
1410}
1411
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001412static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001413{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001414 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001415 mbedtls_free( ctx );
1416}
1417
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001418static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001419{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001420 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001421 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001423
Gilles Peskined2d45c12019-05-27 14:53:13 +02001424 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001425 return( 0 );
1426
Gilles Peskined2d45c12019-05-27 14:53:13 +02001427 bits = psa_get_key_bits( &attributes );
1428 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001429 return( bits );
1430}
1431
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001432static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001433{
1434 /* For now opaque PSA keys can only wrap ECC keypairs,
1435 * as checked by setup_psa().
1436 * Also, ECKEY_DH does not really make sense with the current API. */
1437 return( type == MBEDTLS_PK_ECKEY ||
1438 type == MBEDTLS_PK_ECDSA );
1439}
1440
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001441static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1442 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001443 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001444 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1445{
John Durkopf35069a2020-08-17 22:05:14 -07001446#if !defined(MBEDTLS_ECDSA_C)
1447 ((void) ctx);
1448 ((void) md_alg);
1449 ((void) hash);
1450 ((void) hash_len);
1451 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001452 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001453 ((void) sig_len);
1454 ((void) f_rng);
1455 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001456 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1457#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001458 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001459 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001460 psa_status_t status;
1461
1462 /* PSA has its own RNG */
1463 (void) f_rng;
1464 (void) p_rng;
1465
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001466 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001467 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001468 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001469 if( status != PSA_SUCCESS )
Jerry Yu848ecce2022-03-22 10:58:48 +08001470 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001471
1472 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001473 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001474#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001475}
1476
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001477const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1478 MBEDTLS_PK_OPAQUE,
1479 "Opaque",
1480 pk_opaque_get_bitlen,
1481 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001482 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001483 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001484#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1485 NULL, /* restartable verify - not relevant */
1486 NULL, /* restartable sign - not relevant */
1487#endif
1488 NULL, /* decrypt - will be done later */
1489 NULL, /* encrypt - will be done later */
1490 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001491 pk_opaque_alloc_wrap,
1492 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001493#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1494 NULL, /* restart alloc - not relevant */
1495 NULL, /* restart free - not relevant */
1496#endif
1497 NULL, /* debug - could be done later, or even left NULL */
1498};
1499
1500#endif /* MBEDTLS_USE_PSA_CRYPTO */
1501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_PK_C */