blob: b3db3d764e1789fe1937a3f95ebf7c8babd85ca4 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000023#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Neil Armstrong0f49f832022-02-28 15:07:38 +010035#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
36#include "pkwrite.h"
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#endif
42
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "mbedtls/asn1write.h"
45#endif
46
Andres Amaya Garciae32df082017-10-25 09:37:04 +010047#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010049#endif
50
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040052#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010053#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040054#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059#else
60#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020061#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020063#endif
64
Andres AG72849872017-01-19 11:24:33 +000065#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010066#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020067
Jerry Yub02ee182022-03-16 10:30:41 +080068#if defined(MBEDTLS_PSA_CRYPTO_C)
Neil Armstrong19915c22022-03-01 15:21:02 +010069int mbedtls_pk_error_from_psa( psa_status_t status )
70{
71 switch( status )
72 {
73 case PSA_SUCCESS:
74 return( 0 );
75 case PSA_ERROR_INVALID_HANDLE:
76 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
77 case PSA_ERROR_NOT_PERMITTED:
78 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
79 case PSA_ERROR_BUFFER_TOO_SMALL:
80 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
81 case PSA_ERROR_NOT_SUPPORTED:
82 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
83 case PSA_ERROR_INVALID_ARGUMENT:
84 return( MBEDTLS_ERR_PK_INVALID_ALG );
85 case PSA_ERROR_INSUFFICIENT_MEMORY:
86 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
87 case PSA_ERROR_BAD_STATE:
88 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
89 case PSA_ERROR_COMMUNICATION_FAILURE:
90 case PSA_ERROR_HARDWARE_FAILURE:
91 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
92 case PSA_ERROR_DATA_CORRUPT:
93 case PSA_ERROR_DATA_INVALID:
94 case PSA_ERROR_STORAGE_FAILURE:
95 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
96 case PSA_ERROR_CORRUPTION_DETECTED:
97 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
98 default:
99 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
100 }
101}
102
Neil Armstrong19915c22022-03-01 15:21:02 +0100103#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
104int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
105{
106 switch( status )
107 {
108 case PSA_ERROR_NOT_PERMITTED:
109 case PSA_ERROR_INVALID_ARGUMENT:
110 case PSA_ERROR_INVALID_HANDLE:
111 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
112 case PSA_ERROR_BUFFER_TOO_SMALL:
113 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
114 case PSA_ERROR_INSUFFICIENT_ENTROPY:
115 return( MBEDTLS_ERR_RSA_RNG_FAILED );
116 case PSA_ERROR_INVALID_SIGNATURE:
117 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
118 case PSA_ERROR_INVALID_PADDING:
119 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
120 default:
121 return( mbedtls_pk_error_from_psa( status ) );
122 }
123}
124#endif
Jerry Yu07869e82022-03-16 16:40:50 +0800125
126#endif /* MBEDTLS_PSA_CRYPTO_C */
127
128#if defined(MBEDTLS_USE_PSA_CRYPTO)
129
130#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Jerry Yu848ecce2022-03-22 10:58:48 +0800131int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status )
Jerry Yu07869e82022-03-16 16:40:50 +0800132{
133 switch( status )
134 {
135 case PSA_ERROR_NOT_PERMITTED:
136 case PSA_ERROR_INVALID_ARGUMENT:
137 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
138 case PSA_ERROR_INVALID_HANDLE:
139 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
140 case PSA_ERROR_BUFFER_TOO_SMALL:
141 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
142 case PSA_ERROR_INSUFFICIENT_ENTROPY:
143 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
144 case PSA_ERROR_INVALID_SIGNATURE:
145 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
146 default:
147 return( mbedtls_pk_error_from_psa( status ) );
148 }
149}
Jerry Yu75339822022-03-23 12:06:31 +0800150#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800151
Jerry Yu75339822022-03-23 12:06:31 +0800152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_RSA_C)
155static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( type == MBEDTLS_PK_RSA ||
158 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200159}
160
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200161static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200162{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100163 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
164 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200165}
166
Neil Armstrong52f41f82022-02-22 15:30:24 +0100167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200169 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200170 const unsigned char *sig, size_t sig_len )
171{
Neil Armstrong52f41f82022-02-22 15:30:24 +0100172 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
175 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
176 psa_status_t status;
177 mbedtls_pk_context key;
178 int key_len;
179 /* see RSA_PUR_DER_MAX_BYTES in pkwrite.c */
180 unsigned char buf[38 + 2 * MBEDTLS_MPI_MAX_SIZE];
181 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
182 psa_algorithm_t psa_alg_md = PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_psa_translate_md( md_alg ) );
183 size_t rsa_len = mbedtls_rsa_get_len( rsa );
184
185#if SIZE_MAX > UINT_MAX
186 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
187 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
188#endif /* SIZE_MAX > UINT_MAX */
189
190 if( sig_len < rsa_len )
191 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
192
193 /* mbedtls_pk_write_pubkey() expects a full PK context;
194 * re-construct one to make it happy */
195 key.pk_info = &pk_info;
196 key.pk_ctx = ctx;
197 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
198 if( key_len <= 0 )
199 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
200
201 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
202 psa_set_key_algorithm( &attributes, psa_alg_md );
203 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
204
205 status = psa_import_key( &attributes,
206 buf + sizeof( buf ) - key_len, key_len,
207 &key_id );
208 if( status != PSA_SUCCESS )
209 {
210 ret = mbedtls_psa_err_translate_pk( status );
211 goto cleanup;
212 }
213
214 status = psa_verify_hash( key_id, psa_alg_md, hash, hash_len,
215 sig, sig_len );
216 if( status != PSA_SUCCESS )
217 {
218 ret = mbedtls_psa_err_translate_pk( status );
219 goto cleanup;
220 }
221 ret = 0;
222
223cleanup:
224 psa_destroy_key( key_id );
225 return( ret );
226}
227#else
228static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
229 const unsigned char *hash, size_t hash_len,
230 const unsigned char *sig, size_t sig_len )
231{
Janos Follath24eed8d2019-11-22 13:21:35 +0000232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100233 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
234 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200235
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100236#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000237 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
238 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100239#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000240
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100241 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200243
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100244 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
245 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100246 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200247 return( ret );
248
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200249 /* The buffer contains a valid signature followed by extra data.
250 * We have a special error code for that so that so that callers can
251 * use mbedtls_pk_verify() to check "Does the buffer start with a
252 * valid signature?" and not just "Does the buffer contain a valid
253 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100254 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200256
257 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200258}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100259#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200260
Jerry Yub02ee182022-03-16 10:30:41 +0800261#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yue010de42022-03-23 11:45:55 +0800262int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
263 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800264 const unsigned char *hash, size_t hash_len,
265 unsigned char *sig, size_t sig_size,
266 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100267{
Neil Armstrong98545682022-02-22 16:12:51 +0100268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
270 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
271 psa_status_t status;
272 mbedtls_pk_context key;
273 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100274 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100275 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100276
Jerry Yue010de42022-03-23 11:45:55 +0800277 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100278 if( sig_size < *sig_len )
279 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
280
Neil Armstronge4f28682022-02-24 15:41:39 +0100281 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100282 * re-construct one to make it happy */
283 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800284 key.pk_ctx = rsa_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100285 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
286 if( key_len <= 0 )
287 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800289 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100290 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
291
292 status = psa_import_key( &attributes,
293 buf + sizeof( buf ) - key_len, key_len,
294 &key_id );
295 if( status != PSA_SUCCESS )
296 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100297 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100298 goto cleanup;
299 }
Jerry Yubf455e72022-03-22 21:39:41 +0800300 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100301 sig, sig_size, sig_len );
302 if( status != PSA_SUCCESS )
303 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100304 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100305 goto cleanup;
306 }
307
308 ret = 0;
309
310cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100311 status = psa_destroy_key( key_id );
312 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100313 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100314 return( ret );
315}
Jerry Yu406cf272022-03-22 11:33:42 +0800316#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800317
318#if defined(MBEDTLS_USE_PSA_CRYPTO)
319static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
320 const unsigned char *hash, size_t hash_len,
321 unsigned char *sig, size_t sig_size, size_t *sig_len,
322 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
323{
Jerry Yu1d172a32022-03-12 19:12:05 +0800324 ((void) f_rng);
325 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800326
Jerry Yubd1b3272022-03-24 13:05:20 +0800327 psa_algorithm_t psa_md_alg;
328 psa_md_alg = mbedtls_psa_translate_md( md_alg );
329 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800330 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800331
Jerry Yubd1b3272022-03-24 13:05:20 +0800332 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
333 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800334 ctx, hash, hash_len,
335 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800336}
Neil Armstrong98545682022-02-22 16:12:51 +0100337#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200339 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200340 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200341 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
342{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100343 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
344
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100345#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000346 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
347 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100348#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000349
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100350 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200351 if( sig_size < *sig_len )
352 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200353
Thomas Daubney140184d2021-05-18 16:04:07 +0100354 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
355 md_alg, (unsigned int) hash_len,
356 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200357}
Neil Armstrong98545682022-02-22 16:12:51 +0100358#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200359
Neil Armstrong18f43c72022-02-09 15:32:45 +0100360#if defined(MBEDTLS_USE_PSA_CRYPTO)
361static int rsa_decrypt_wrap( void *ctx,
362 const unsigned char *input, size_t ilen,
363 unsigned char *output, size_t *olen, size_t osize,
364 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
365{
366 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
369 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
370 psa_status_t status;
371 mbedtls_pk_context key;
372 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100373 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100374
375 ((void) f_rng);
376 ((void) p_rng);
377
378#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100379 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
380 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
381#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100382
383 if( ilen != mbedtls_rsa_get_len( rsa ) )
384 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
385
386 /* mbedtls_pk_write_key_der() expects a full PK context;
387 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100388 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100389 key.pk_ctx = ctx;
390 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
391 if( key_len <= 0 )
392 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
393
394 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100396 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100397
398 status = psa_import_key( &attributes,
399 buf + sizeof( buf ) - key_len, key_len,
400 &key_id );
401 if( status != PSA_SUCCESS )
402 {
Neil Armstronge8780492022-03-03 16:54:16 +0100403 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100404 goto cleanup;
405 }
406
Neil Armstrong8e805042022-03-16 15:30:31 +0100407 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
408 input, ilen,
409 NULL, 0,
410 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100411 if( status != PSA_SUCCESS )
412 {
Neil Armstronge8780492022-03-03 16:54:16 +0100413 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100414 goto cleanup;
415 }
416
417 ret = 0;
418
419cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100420 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100421 status = psa_destroy_key( key_id );
422 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100423 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100424
Neil Armstrong18f43c72022-02-09 15:32:45 +0100425 return( ret );
426}
427#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200428static int rsa_decrypt_wrap( void *ctx,
429 const unsigned char *input, size_t ilen,
430 unsigned char *output, size_t *olen, size_t osize,
431 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
432{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100433 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
434
435 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200437
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100438 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100439 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200440}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100441#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200442
Neil Armstrong96a16a42022-02-10 10:40:11 +0100443#if defined(MBEDTLS_USE_PSA_CRYPTO)
444static int rsa_encrypt_wrap( void *ctx,
445 const unsigned char *input, size_t ilen,
446 unsigned char *output, size_t *olen, size_t osize,
447 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
448{
449 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
452 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
453 psa_status_t status;
454 mbedtls_pk_context key;
455 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100456 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100457
458 ((void) f_rng);
459 ((void) p_rng);
460
461#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100462 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
463 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100464#endif
465
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100466 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100467 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
468
Neil Armstrongac014ca2022-02-24 15:27:54 +0100469 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100470 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100471 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100472 key.pk_ctx = ctx;
473 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
474 if( key_len <= 0 )
475 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
476
477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100478 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100479 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
480
481 status = psa_import_key( &attributes,
482 buf + sizeof( buf ) - key_len, key_len,
483 &key_id );
484 if( status != PSA_SUCCESS )
485 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100486 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100487 goto cleanup;
488 }
489
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100490 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
491 input, ilen,
492 NULL, 0,
493 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100494 if( status != PSA_SUCCESS )
495 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100496 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100497 goto cleanup;
498 }
499
500 ret = 0;
501
502cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100503 status = psa_destroy_key( key_id );
504 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100505 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100506
Neil Armstrong96a16a42022-02-10 10:40:11 +0100507 return( ret );
508}
509#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200510static int rsa_encrypt_wrap( void *ctx,
511 const unsigned char *input, size_t ilen,
512 unsigned char *output, size_t *olen, size_t osize,
513 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
514{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100515 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
516 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200517
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100518 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100520
Thomas Daubney21772772021-05-13 17:30:32 +0100521 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100522 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200523}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100524#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200525
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200526static int rsa_check_pair_wrap( const void *pub, const void *prv,
527 int (*f_rng)(void *, unsigned char *, size_t),
528 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100529{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200530 (void) f_rng;
531 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
533 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100534}
535
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200536static void *rsa_alloc_wrap( void )
537{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200538 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200539
540 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200541 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200542
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200543 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200544}
545
546static void rsa_free_wrap( void *ctx )
547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
549 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200550}
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200553{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200554#if defined(MBEDTLS_RSA_ALT)
555 /* Not supported */
556 (void) ctx;
557 (void) items;
558#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200560 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200562
563 items++;
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200566 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200568#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200569}
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571const mbedtls_pk_info_t mbedtls_rsa_info = {
572 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200573 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200574 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200575 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200576 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200577 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200578#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200579 NULL,
580 NULL,
581#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200582 rsa_decrypt_wrap,
583 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100584 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200585 rsa_alloc_wrap,
586 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200587#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200588 NULL,
589 NULL,
590#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200591 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200592};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200596/*
597 * Generic EC key
598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( type == MBEDTLS_PK_ECKEY ||
602 type == MBEDTLS_PK_ECKEY_DH ||
603 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200604}
605
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200606static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200609}
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200612/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200614 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200615 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200618 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200619 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200620 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200623 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200624 const unsigned char *sig, size_t sig_len )
625{
Janos Follath24eed8d2019-11-22 13:21:35 +0000626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200632 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200635
636 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200637}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200640 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200641 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200642 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
643{
Janos Follath24eed8d2019-11-22 13:21:35 +0000644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200650 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
651 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200652 f_rng, p_rng );
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200655
656 return( ret );
657}
658
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200659#if defined(MBEDTLS_ECP_RESTARTABLE)
660/* Forward declarations */
661static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
662 const unsigned char *hash, size_t hash_len,
663 const unsigned char *sig, size_t sig_len,
664 void *rs_ctx );
665
666static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
667 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200668 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200669 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
670 void *rs_ctx );
671
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200672/*
673 * Restart context for ECDSA operations with ECKEY context
674 *
675 * We need to store an actual ECDSA context, as we need to pass the same to
676 * the underlying ecdsa function, so we can't create it on the fly every time.
677 */
678typedef struct
679{
680 mbedtls_ecdsa_restart_ctx ecdsa_rs;
681 mbedtls_ecdsa_context ecdsa_ctx;
682} eckey_restart_ctx;
683
684static void *eckey_rs_alloc( void )
685{
686 eckey_restart_ctx *rs_ctx;
687
688 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
689
690 if( ctx != NULL )
691 {
692 rs_ctx = ctx;
693 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
694 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
695 }
696
697 return( ctx );
698}
699
700static void eckey_rs_free( void *ctx )
701{
702 eckey_restart_ctx *rs_ctx;
703
704 if( ctx == NULL)
705 return;
706
707 rs_ctx = ctx;
708 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
709 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
710
711 mbedtls_free( ctx );
712}
713
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200714static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
715 const unsigned char *hash, size_t hash_len,
716 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200717 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200718{
Janos Follath24eed8d2019-11-22 13:21:35 +0000719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200720 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200721
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200722 /* Should never happen */
723 if( rs == NULL )
724 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200725
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200726 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200727 if( rs->ecdsa_ctx.grp.pbits == 0 )
728 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200729
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200730 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
731 md_alg, hash, hash_len,
732 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200733
734cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200735 return( ret );
736}
737
738static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
739 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200740 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200741 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200742 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200743{
Janos Follath24eed8d2019-11-22 13:21:35 +0000744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200745 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200746
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200747 /* Should never happen */
748 if( rs == NULL )
749 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200750
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200751 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200752 if( rs->ecdsa_ctx.grp.pbits == 0 )
753 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200754
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200755 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200756 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200757 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200758
759cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200760 return( ret );
761}
762#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200764
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200765static int eckey_check_pair( const void *pub, const void *prv,
766 int (*f_rng)(void *, unsigned char *, size_t),
767 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100768{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200770 (const mbedtls_ecp_keypair *) prv,
771 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100772}
773
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200774static void *eckey_alloc_wrap( void )
775{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200776 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200777
778 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200780
781 return( ctx );
782}
783
784static void eckey_free_wrap( void *ctx )
785{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
787 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200788}
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200793 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200795}
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797const mbedtls_pk_info_t mbedtls_eckey_info = {
798 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200799 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200800 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200801 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200803 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200804 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200805#if defined(MBEDTLS_ECP_RESTARTABLE)
806 eckey_verify_rs_wrap,
807 eckey_sign_rs_wrap,
808#endif
809#else /* MBEDTLS_ECDSA_C */
810 NULL,
811 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200812#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200813 NULL,
814 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100815 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200816 eckey_alloc_wrap,
817 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200818#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200819 eckey_rs_alloc,
820 eckey_rs_free,
821#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200822 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200823};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200824
825/*
Paul Bakker75342a62014-04-08 17:35:40 +0200826 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200827 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 return( type == MBEDTLS_PK_ECKEY ||
831 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200832}
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834const mbedtls_pk_info_t mbedtls_eckeydh_info = {
835 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200836 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200837 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200838 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200839 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200840 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200841#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200842 NULL,
843 NULL,
844#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200845 NULL,
846 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100847 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200848 eckey_alloc_wrap, /* Same underlying key structure */
849 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200850#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200851 NULL,
852 NULL,
853#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200854 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200855};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_ECDSA_C)
859static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200862}
863
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400864#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400865/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500866 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
867 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500868 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500869static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
870 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500871{
Janos Follath24eed8d2019-11-22 13:21:35 +0000872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500873 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500874
Andrzej Kurek9241d182018-11-20 05:04:35 -0500875 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500876 MBEDTLS_ASN1_INTEGER ) ) != 0 )
877 {
878 return( ret );
879 }
880
Andrzej Kurek9241d182018-11-20 05:04:35 -0500881 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500882 {
883 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500884 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500885 }
886
Andrzej Kurek9241d182018-11-20 05:04:35 -0500887 if( unpadded_len > to_len || unpadded_len == 0 )
888 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500889
Andrzej Kurek9241d182018-11-20 05:04:35 -0500890 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500891 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500892 memcpy( to + padding_len, *from, unpadded_len );
893 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500894
Andrzej Kurek9241d182018-11-20 05:04:35 -0500895 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500896}
897
898/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400899 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500900 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500901 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400902 */
903static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500904 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400905{
Janos Follath24eed8d2019-11-22 13:21:35 +0000906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500907 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500908
Andrzej Kurek9241d182018-11-20 05:04:35 -0500909 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500910 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500911 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400912
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500913 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500914 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
915 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500916 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500917 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
918 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500919
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400920 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400921}
922
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100923static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400924 const unsigned char *hash, size_t hash_len,
925 const unsigned char *sig, size_t sig_len )
926{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100927 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000928 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100930 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200931 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400932 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400933 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100934 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000935 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400936 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700937 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100938 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100939 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100940 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
941 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700942 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400943
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500944 if( curve == 0 )
945 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
946
Hanno Beckerccf574e2019-01-29 08:26:15 +0000947 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500948 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400949 key.pk_info = &pk_info;
950 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000951 p = buf + sizeof( buf );
952 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400953 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500954 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400955
Gilles Peskined2d45c12019-05-27 14:53:13 +0200956 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100957 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200958 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500959
Gilles Peskined2d45c12019-05-27 14:53:13 +0200960 status = psa_import_key( &attributes,
961 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200962 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200963 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400964 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100965 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400966 goto cleanup;
967 }
968
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500969 /* We don't need the exported key anymore and can
970 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500971 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500972 {
973 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
974 goto cleanup;
975 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500976
Hanno Beckera9851112019-01-25 16:37:10 +0000977 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500978 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500979 signature_part_size ) ) != 0 )
980 {
981 goto cleanup;
982 }
983
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100984 status = psa_verify_hash( key_id, psa_sig_md,
985 hash, hash_len,
986 buf, 2 * signature_part_size );
987 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400988 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800989 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400990 goto cleanup;
991 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500992
993 if( p != sig + sig_len )
994 {
995 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
996 goto cleanup;
997 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400998 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400999
Andrzej Kurekb7b04782018-11-19 17:01:16 -05001000cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +01001001 status = psa_destroy_key( key_id );
1002 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +01001003 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +01001004
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001005 return( ret );
1006}
1007#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001009 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001010 const unsigned char *sig, size_t sig_len )
1011{
Janos Follath24eed8d2019-11-22 13:21:35 +00001012 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001013 ((void) md_alg);
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001016 hash, hash_len, sig, sig_len );
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1019 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001020
1021 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001022}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001023#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001024
Neil Armstronge9606902022-02-09 14:23:00 +01001025#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001026/*
1027 * Simultaneously convert and move raw MPI from the beginning of a buffer
1028 * to an ASN.1 MPI at the end of the buffer.
1029 * See also mbedtls_asn1_write_mpi().
1030 *
1031 * p: pointer to the end of the output buffer
1032 * start: start of the output buffer, and also of the mpi to write at the end
1033 * n_len: length of the mpi to read from start
1034 */
1035static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1036 size_t n_len )
1037{
1038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1039 size_t len = 0;
1040
1041 if( (size_t)( *p - start ) < n_len )
1042 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1043
1044 len = n_len;
1045 *p -= len;
1046 memmove( *p, start, len );
1047
1048 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1049 * Neither r nor s should be 0, but as a failsafe measure, still detect
1050 * that rather than overflowing the buffer in case of a PSA error. */
1051 while( len > 0 && **p == 0x00 )
1052 {
1053 ++(*p);
1054 --len;
1055 }
1056
1057 /* this is only reached if the signature was invalid */
1058 if( len == 0 )
1059 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1060
1061 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1062 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1063 if( **p & 0x80 )
1064 {
1065 if( *p - start < 1 )
1066 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1067
1068 *--(*p) = 0x00;
1069 len += 1;
1070 }
1071
1072 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1073 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1074 MBEDTLS_ASN1_INTEGER ) );
1075
1076 return( (int) len );
1077}
1078
1079/* Transcode signature from PSA format to ASN.1 sequence.
1080 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1081 * MPIs, and in-place.
1082 *
1083 * [in/out] sig: the signature pre- and post-transcoding
1084 * [in/out] sig_len: signature length pre- and post-transcoding
1085 * [int] buf_len: the available size the in/out buffer
1086 */
Neil Armstronge9606902022-02-09 14:23:00 +01001087static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001088 size_t buf_len )
1089{
1090 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1091 size_t len = 0;
1092 const size_t rs_len = *sig_len / 2;
1093 unsigned char *p = sig + buf_len;
1094
1095 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1096 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1097
1098 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1099 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1100 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1101
1102 memmove( sig, p, len );
1103 *sig_len = len;
1104
1105 return( 0 );
1106}
Neil Armstronge9606902022-02-09 14:23:00 +01001107
Neil Armstrong17a06552022-03-18 15:27:38 +01001108/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1109 *
1110 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1111 * [in] end: ASN.1 buffer end
1112 * [out] key_len: the ECDSA privateKey length in bytes
1113 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001114static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1115 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001116{
1117 size_t len;
1118 int ret;
1119
Neil Armstrong17a06552022-03-18 15:27:38 +01001120 /*
1121 * RFC 5915, or SEC1 Appendix C.4
1122 *
1123 * ECPrivateKey ::= SEQUENCE {
1124 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1125 * privateKey OCTET STRING,
1126 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1127 * publicKey [1] BIT STRING OPTIONAL
1128 * }
1129 */
1130
Neil Armstronge9606902022-02-09 14:23:00 +01001131 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001132 MBEDTLS_ASN1_CONSTRUCTED |
1133 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001134 return( ret );
1135
1136 /* version */
1137 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1138 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1139 return( ret );
1140
1141 *buf += len;
1142
1143 /* privateKey */
1144 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1145 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1146 return( ret );
1147
1148 *key_len = len;
1149
1150 return 0;
1151}
1152
1153static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1154 const unsigned char *hash, size_t hash_len,
1155 unsigned char *sig, size_t sig_size, size_t *sig_len,
1156 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1157{
1158 mbedtls_ecdsa_context *ctx = ctx_arg;
1159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1161 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1162 psa_status_t status;
1163 mbedtls_pk_context key;
1164 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001165 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001166 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001167 psa_algorithm_t psa_sig_md =
1168 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001169 size_t curve_bits;
1170 psa_ecc_family_t curve =
1171 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001172
1173 /* PSA has its own RNG */
1174 ((void) f_rng);
1175 ((void) p_rng);
1176
1177 if( curve == 0 )
1178 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1179
1180 /* mbedtls_pk_write_key_der() expects a full PK context;
1181 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001182 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001183 key.pk_ctx = ctx;
1184 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1185 if( key_len <= 0 )
1186 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1187
1188 p = buf + sizeof( buf ) - key_len;
1189 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1190 if( ret != 0 )
1191 goto cleanup;
1192
1193 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1194 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1195 psa_set_key_algorithm( &attributes, psa_sig_md );
1196
1197 status = psa_import_key( &attributes,
1198 p, key_len,
1199 &key_id );
1200 if( status != PSA_SUCCESS )
1201 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001202 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001203 goto cleanup;
1204 }
1205
Neil Armstronge4edcf72022-03-03 16:46:41 +01001206 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1207 sig, sig_size, sig_len );
1208 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001209 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001210 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001211 goto cleanup;
1212 }
1213
1214 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1215
1216cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001217 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001218 status = psa_destroy_key( key_id );
1219 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001220 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001221
Neil Armstronge9606902022-02-09 14:23:00 +01001222 return( ret );
1223}
1224#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001226 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001227 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001228 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001231 md_alg, hash, hash_len,
1232 sig, sig_size, sig_len,
1233 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001234}
Neil Armstronge9606902022-02-09 14:23:00 +01001235#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001236
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001237#if defined(MBEDTLS_ECP_RESTARTABLE)
1238static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1239 const unsigned char *hash, size_t hash_len,
1240 const unsigned char *sig, size_t sig_len,
1241 void *rs_ctx )
1242{
Janos Follath24eed8d2019-11-22 13:21:35 +00001243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001244 ((void) md_alg);
1245
1246 ret = mbedtls_ecdsa_read_signature_restartable(
1247 (mbedtls_ecdsa_context *) ctx,
1248 hash, hash_len, sig, sig_len,
1249 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1250
1251 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1252 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1253
1254 return( ret );
1255}
1256
1257static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1258 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001259 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001260 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1261 void *rs_ctx )
1262{
1263 return( mbedtls_ecdsa_write_signature_restartable(
1264 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001265 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001266 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1267
1268}
1269#endif /* MBEDTLS_ECP_RESTARTABLE */
1270
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001271static void *ecdsa_alloc_wrap( void )
1272{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001273 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001274
1275 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001277
1278 return( ctx );
1279}
1280
1281static void ecdsa_free_wrap( void *ctx )
1282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1284 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001285}
1286
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001287#if defined(MBEDTLS_ECP_RESTARTABLE)
1288static void *ecdsa_rs_alloc( void )
1289{
1290 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1291
1292 if( ctx != NULL )
1293 mbedtls_ecdsa_restart_init( ctx );
1294
1295 return( ctx );
1296}
1297
1298static void ecdsa_rs_free( void *ctx )
1299{
1300 mbedtls_ecdsa_restart_free( ctx );
1301 mbedtls_free( ctx );
1302}
1303#endif /* MBEDTLS_ECP_RESTARTABLE */
1304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1306 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001307 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001308 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001309 ecdsa_can_do,
1310 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001311 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001312#if defined(MBEDTLS_ECP_RESTARTABLE)
1313 ecdsa_verify_rs_wrap,
1314 ecdsa_sign_rs_wrap,
1315#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001316 NULL,
1317 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001318 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001319 ecdsa_alloc_wrap,
1320 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001321#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001322 ecdsa_rs_alloc,
1323 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001324#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001325 eckey_debug, /* Compatible key structures */
1326};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001330/*
1331 * Support for alternative RSA-private implementations
1332 */
1333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001337}
1338
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001339static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001342
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001343 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001344}
1345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001347 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001348 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001352
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001353#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001354 if( UINT_MAX < hash_len )
1355 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001356#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001357
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001358 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001359 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1360 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001361 if( *sig_len > sig_size )
1362 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001363
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001364 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001365 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001366}
1367
1368static int rsa_alt_decrypt_wrap( void *ctx,
1369 const unsigned char *input, size_t ilen,
1370 unsigned char *output, size_t *olen, size_t osize,
1371 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001374
1375 ((void) f_rng);
1376 ((void) p_rng);
1377
1378 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001380
1381 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001382 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001383}
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001386static int rsa_alt_check_pair( const void *pub, const void *prv,
1387 int (*f_rng)(void *, unsigned char *, size_t),
1388 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001391 unsigned char hash[32];
1392 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001394
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001395 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001397
1398 memset( hash, 0x2a, sizeof( hash ) );
1399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001401 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001402 sig, sizeof( sig ), &sig_len,
1403 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001404 {
1405 return( ret );
1406 }
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001409 hash, sizeof( hash ), sig, sig_len ) != 0 )
1410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001412 }
1413
1414 return( 0 );
1415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001417
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001418static void *rsa_alt_alloc_wrap( void )
1419{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001420 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001421
1422 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001424
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001425 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001426}
1427
1428static void rsa_alt_free_wrap( void *ctx )
1429{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001430 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001432}
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1435 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001436 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001437 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001438 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001439 NULL,
1440 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001441#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001442 NULL,
1443 NULL,
1444#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001445 rsa_alt_decrypt_wrap,
1446 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001448 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001449#else
1450 NULL,
1451#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001452 rsa_alt_alloc_wrap,
1453 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001454#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001455 NULL,
1456 NULL,
1457#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001458 NULL,
1459};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001462
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001463#if defined(MBEDTLS_USE_PSA_CRYPTO)
1464
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001465static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001466{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001467 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001468
1469 /* no _init() function to call, an calloc() already zeroized */
1470
1471 return( ctx );
1472}
1473
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001474static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001475{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001476 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001477 mbedtls_free( ctx );
1478}
1479
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001480static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001481{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001482 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001483 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001485
Gilles Peskined2d45c12019-05-27 14:53:13 +02001486 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001487 return( 0 );
1488
Gilles Peskined2d45c12019-05-27 14:53:13 +02001489 bits = psa_get_key_bits( &attributes );
1490 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001491 return( bits );
1492}
1493
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001494static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001495{
1496 /* For now opaque PSA keys can only wrap ECC keypairs,
1497 * as checked by setup_psa().
1498 * Also, ECKEY_DH does not really make sense with the current API. */
1499 return( type == MBEDTLS_PK_ECKEY ||
1500 type == MBEDTLS_PK_ECDSA );
1501}
1502
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001503static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1504 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001505 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001506 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1507{
John Durkopf35069a2020-08-17 22:05:14 -07001508#if !defined(MBEDTLS_ECDSA_C)
1509 ((void) ctx);
1510 ((void) md_alg);
1511 ((void) hash);
1512 ((void) hash_len);
1513 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001514 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001515 ((void) sig_len);
1516 ((void) f_rng);
1517 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001518 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1519#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001520 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001521 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001522 psa_status_t status;
1523
1524 /* PSA has its own RNG */
1525 (void) f_rng;
1526 (void) p_rng;
1527
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001528 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001529 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001530 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001531 if( status != PSA_SUCCESS )
Jerry Yu848ecce2022-03-22 10:58:48 +08001532 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001533
1534 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001535 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001536#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001537}
1538
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001539const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1540 MBEDTLS_PK_OPAQUE,
1541 "Opaque",
1542 pk_opaque_get_bitlen,
1543 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001544 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001545 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001546#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1547 NULL, /* restartable verify - not relevant */
1548 NULL, /* restartable sign - not relevant */
1549#endif
1550 NULL, /* decrypt - will be done later */
1551 NULL, /* encrypt - will be done later */
1552 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001553 pk_opaque_alloc_wrap,
1554 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001555#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1556 NULL, /* restart alloc - not relevant */
1557 NULL, /* restart free - not relevant */
1558#endif
1559 NULL, /* debug - could be done later, or even left NULL */
1560};
1561
1562#endif /* MBEDTLS_USE_PSA_CRYPTO */
1563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_PK_C */