blob: 02331c3457f5bdb21aee225cee9782db5a89dbb5 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000023#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Neil Armstrong0f49f832022-02-28 15:07:38 +010035#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
36#include "pkwrite.h"
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#endif
42
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "mbedtls/asn1write.h"
45#endif
46
Andres Amaya Garciae32df082017-10-25 09:37:04 +010047#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010049#endif
50
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040052#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010053#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040054#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059#else
60#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020061#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_free free
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020063#endif
64
Andres AG72849872017-01-19 11:24:33 +000065#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010066#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020067
Jerry Yub02ee182022-03-16 10:30:41 +080068#if defined(MBEDTLS_PSA_CRYPTO_C)
Neil Armstrong19915c22022-03-01 15:21:02 +010069int mbedtls_pk_error_from_psa( psa_status_t status )
70{
71 switch( status )
72 {
73 case PSA_SUCCESS:
74 return( 0 );
75 case PSA_ERROR_INVALID_HANDLE:
76 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
77 case PSA_ERROR_NOT_PERMITTED:
78 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
79 case PSA_ERROR_BUFFER_TOO_SMALL:
80 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
81 case PSA_ERROR_NOT_SUPPORTED:
82 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
83 case PSA_ERROR_INVALID_ARGUMENT:
84 return( MBEDTLS_ERR_PK_INVALID_ALG );
85 case PSA_ERROR_INSUFFICIENT_MEMORY:
86 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
87 case PSA_ERROR_BAD_STATE:
88 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
89 case PSA_ERROR_COMMUNICATION_FAILURE:
90 case PSA_ERROR_HARDWARE_FAILURE:
91 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
92 case PSA_ERROR_DATA_CORRUPT:
93 case PSA_ERROR_DATA_INVALID:
94 case PSA_ERROR_STORAGE_FAILURE:
95 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
96 case PSA_ERROR_CORRUPTION_DETECTED:
97 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
98 default:
99 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
100 }
101}
102
Neil Armstrong19915c22022-03-01 15:21:02 +0100103#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
104int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
105{
106 switch( status )
107 {
108 case PSA_ERROR_NOT_PERMITTED:
109 case PSA_ERROR_INVALID_ARGUMENT:
110 case PSA_ERROR_INVALID_HANDLE:
111 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
112 case PSA_ERROR_BUFFER_TOO_SMALL:
113 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
114 case PSA_ERROR_INSUFFICIENT_ENTROPY:
115 return( MBEDTLS_ERR_RSA_RNG_FAILED );
116 case PSA_ERROR_INVALID_SIGNATURE:
117 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
118 case PSA_ERROR_INVALID_PADDING:
119 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
120 default:
121 return( mbedtls_pk_error_from_psa( status ) );
122 }
123}
124#endif
Jerry Yu07869e82022-03-16 16:40:50 +0800125
126#endif /* MBEDTLS_PSA_CRYPTO_C */
127
128#if defined(MBEDTLS_USE_PSA_CRYPTO)
129
130#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Jerry Yu848ecce2022-03-22 10:58:48 +0800131int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status )
Jerry Yu07869e82022-03-16 16:40:50 +0800132{
133 switch( status )
134 {
135 case PSA_ERROR_NOT_PERMITTED:
136 case PSA_ERROR_INVALID_ARGUMENT:
137 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
138 case PSA_ERROR_INVALID_HANDLE:
139 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
140 case PSA_ERROR_BUFFER_TOO_SMALL:
141 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
142 case PSA_ERROR_INSUFFICIENT_ENTROPY:
143 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
144 case PSA_ERROR_INVALID_SIGNATURE:
145 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
146 default:
147 return( mbedtls_pk_error_from_psa( status ) );
148 }
149}
Jerry Yu75339822022-03-23 12:06:31 +0800150#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800151
Jerry Yu75339822022-03-23 12:06:31 +0800152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_RSA_C)
155static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( type == MBEDTLS_PK_RSA ||
158 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200159}
160
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200161static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200162{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100163 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
164 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200165}
166
Neil Armstrong52f41f82022-02-22 15:30:24 +0100167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200169 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200170 const unsigned char *sig, size_t sig_len )
171{
Neil Armstrong52f41f82022-02-22 15:30:24 +0100172 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
174 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
175 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
176 psa_status_t status;
177 mbedtls_pk_context key;
178 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100179 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong52f41f82022-02-22 15:30:24 +0100180 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong82cf8042022-03-03 12:30:59 +0100181 psa_algorithm_t psa_alg_md =
182 PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_psa_translate_md( md_alg ) );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100183 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 {
Neil Armstrong059a80c2022-02-24 15:23:42 +0100218 if ( status == PSA_ERROR_INVALID_PADDING )
219 {
220 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
221 }
222 else
223 {
224 ret = mbedtls_psa_err_translate_pk( status );
225 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100226 goto cleanup;
227 }
228 ret = 0;
229
230cleanup:
Neil Armstronga33280a2022-02-24 15:17:47 +0100231 status = psa_destroy_key( key_id );
232 if( ret == 0 && status != PSA_SUCCESS )
233 ret = mbedtls_psa_err_translate_pk( status );
234
Neil Armstrong52f41f82022-02-22 15:30:24 +0100235 return( ret );
236}
237#else
238static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
239 const unsigned char *hash, size_t hash_len,
240 const unsigned char *sig, size_t sig_len )
241{
Janos Follath24eed8d2019-11-22 13:21:35 +0000242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100243 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
244 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200245
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100246#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000247 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
248 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100249#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000250
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100251 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200253
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100254 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
255 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100256 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200257 return( ret );
258
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200259 /* The buffer contains a valid signature followed by extra data.
260 * We have a special error code for that so that so that callers can
261 * use mbedtls_pk_verify() to check "Does the buffer start with a
262 * valid signature?" and not just "Does the buffer contain a valid
263 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100264 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200266
267 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200268}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100269#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200270
Jerry Yub02ee182022-03-16 10:30:41 +0800271#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yue010de42022-03-23 11:45:55 +0800272int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
273 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800274 const unsigned char *hash, size_t hash_len,
275 unsigned char *sig, size_t sig_size,
276 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100277{
Neil Armstrong98545682022-02-22 16:12:51 +0100278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
280 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
281 psa_status_t status;
282 mbedtls_pk_context key;
283 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100284 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100285 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100286
Jerry Yue010de42022-03-23 11:45:55 +0800287 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100288 if( sig_size < *sig_len )
289 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
290
Neil Armstronge4f28682022-02-24 15:41:39 +0100291 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100292 * re-construct one to make it happy */
293 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800294 key.pk_ctx = rsa_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100295 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
296 if( key_len <= 0 )
297 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100298 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800299 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100300 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
301
302 status = psa_import_key( &attributes,
303 buf + sizeof( buf ) - key_len, key_len,
304 &key_id );
305 if( status != PSA_SUCCESS )
306 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100307 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100308 goto cleanup;
309 }
Jerry Yubf455e72022-03-22 21:39:41 +0800310 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100311 sig, sig_size, sig_len );
312 if( status != PSA_SUCCESS )
313 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100314 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100315 goto cleanup;
316 }
317
318 ret = 0;
319
320cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100321 status = psa_destroy_key( key_id );
322 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100323 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100324 return( ret );
325}
Jerry Yu406cf272022-03-22 11:33:42 +0800326#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800327
328#if defined(MBEDTLS_USE_PSA_CRYPTO)
329static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
330 const unsigned char *hash, size_t hash_len,
331 unsigned char *sig, size_t sig_size, size_t *sig_len,
332 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
333{
Jerry Yu1d172a32022-03-12 19:12:05 +0800334 ((void) f_rng);
335 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800336
Jerry Yubd1b3272022-03-24 13:05:20 +0800337 psa_algorithm_t psa_md_alg;
338 psa_md_alg = mbedtls_psa_translate_md( md_alg );
339 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800340 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800341
Jerry Yubd1b3272022-03-24 13:05:20 +0800342 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
343 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800344 ctx, hash, hash_len,
345 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800346}
Neil Armstrong98545682022-02-22 16:12:51 +0100347#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200349 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200350 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200351 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
352{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100353 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
354
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100355#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000356 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
357 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100358#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000359
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100360 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200361 if( sig_size < *sig_len )
362 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200363
Thomas Daubney140184d2021-05-18 16:04:07 +0100364 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
365 md_alg, (unsigned int) hash_len,
366 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200367}
Neil Armstrong98545682022-02-22 16:12:51 +0100368#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200369
Neil Armstrong18f43c72022-02-09 15:32:45 +0100370#if defined(MBEDTLS_USE_PSA_CRYPTO)
371static int rsa_decrypt_wrap( void *ctx,
372 const unsigned char *input, size_t ilen,
373 unsigned char *output, size_t *olen, size_t osize,
374 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
375{
376 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
379 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
380 psa_status_t status;
381 mbedtls_pk_context key;
382 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100383 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100384
385 ((void) f_rng);
386 ((void) p_rng);
387
388#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100389 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
390 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
391#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100392
393 if( ilen != mbedtls_rsa_get_len( rsa ) )
394 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
395
396 /* mbedtls_pk_write_key_der() expects a full PK context;
397 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100398 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100399 key.pk_ctx = ctx;
400 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
401 if( key_len <= 0 )
402 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
403
404 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
405 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100406 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100407
408 status = psa_import_key( &attributes,
409 buf + sizeof( buf ) - key_len, key_len,
410 &key_id );
411 if( status != PSA_SUCCESS )
412 {
Neil Armstronge8780492022-03-03 16:54:16 +0100413 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100414 goto cleanup;
415 }
416
Neil Armstrong8e805042022-03-16 15:30:31 +0100417 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
418 input, ilen,
419 NULL, 0,
420 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100421 if( status != PSA_SUCCESS )
422 {
Neil Armstronge8780492022-03-03 16:54:16 +0100423 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100424 goto cleanup;
425 }
426
427 ret = 0;
428
429cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100430 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100431 status = psa_destroy_key( key_id );
432 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100433 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100434
Neil Armstrong18f43c72022-02-09 15:32:45 +0100435 return( ret );
436}
437#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200438static int rsa_decrypt_wrap( void *ctx,
439 const unsigned char *input, size_t ilen,
440 unsigned char *output, size_t *olen, size_t osize,
441 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
442{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100443 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
444
445 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200447
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100448 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100449 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200450}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100451#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200452
Neil Armstrong96a16a42022-02-10 10:40:11 +0100453#if defined(MBEDTLS_USE_PSA_CRYPTO)
454static int rsa_encrypt_wrap( void *ctx,
455 const unsigned char *input, size_t ilen,
456 unsigned char *output, size_t *olen, size_t osize,
457 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
458{
459 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
462 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
463 psa_status_t status;
464 mbedtls_pk_context key;
465 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100466 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100467
468 ((void) f_rng);
469 ((void) p_rng);
470
471#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100472 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
473 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100474#endif
475
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100476 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100477 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
478
Neil Armstrongac014ca2022-02-24 15:27:54 +0100479 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100480 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100481 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100482 key.pk_ctx = ctx;
483 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
484 if( key_len <= 0 )
485 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
486
487 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100488 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100489 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
490
491 status = psa_import_key( &attributes,
492 buf + sizeof( buf ) - key_len, key_len,
493 &key_id );
494 if( status != PSA_SUCCESS )
495 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100496 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100497 goto cleanup;
498 }
499
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100500 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
501 input, ilen,
502 NULL, 0,
503 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100504 if( status != PSA_SUCCESS )
505 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100506 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100507 goto cleanup;
508 }
509
510 ret = 0;
511
512cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100513 status = psa_destroy_key( key_id );
514 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100515 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100516
Neil Armstrong96a16a42022-02-10 10:40:11 +0100517 return( ret );
518}
519#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200520static int rsa_encrypt_wrap( void *ctx,
521 const unsigned char *input, size_t ilen,
522 unsigned char *output, size_t *olen, size_t osize,
523 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
524{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100525 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
526 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200527
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100528 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100530
Thomas Daubney21772772021-05-13 17:30:32 +0100531 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100532 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200533}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100534#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200535
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200536static int rsa_check_pair_wrap( const void *pub, const void *prv,
537 int (*f_rng)(void *, unsigned char *, size_t),
538 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100539{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200540 (void) f_rng;
541 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
543 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100544}
545
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200546static void *rsa_alloc_wrap( void )
547{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200548 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200549
550 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200551 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200552
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200553 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200554}
555
556static void rsa_free_wrap( void *ctx )
557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
559 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200560}
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200563{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200564#if defined(MBEDTLS_RSA_ALT)
565 /* Not supported */
566 (void) ctx;
567 (void) items;
568#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200570 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200572
573 items++;
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200576 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200578#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200579}
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581const mbedtls_pk_info_t mbedtls_rsa_info = {
582 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200583 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200584 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200585 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200586 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200587 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200588#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200589 NULL,
590 NULL,
591#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200592 rsa_decrypt_wrap,
593 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100594 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200595 rsa_alloc_wrap,
596 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200597#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200598 NULL,
599 NULL,
600#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200601 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200602};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200606/*
607 * Generic EC key
608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200610{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 return( type == MBEDTLS_PK_ECKEY ||
612 type == MBEDTLS_PK_ECKEY_DH ||
613 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200614}
615
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200616static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200617{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200619}
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200622/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200624 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200625 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200628 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200629 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200630 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200633 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200634 const unsigned char *sig, size_t sig_len )
635{
Janos Follath24eed8d2019-11-22 13:21:35 +0000636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200642 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200645
646 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200647}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200650 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200651 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200652 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
653{
Janos Follath24eed8d2019-11-22 13:21:35 +0000654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200660 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
661 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200662 f_rng, p_rng );
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200665
666 return( ret );
667}
668
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200669#if defined(MBEDTLS_ECP_RESTARTABLE)
670/* Forward declarations */
671static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
672 const unsigned char *hash, size_t hash_len,
673 const unsigned char *sig, size_t sig_len,
674 void *rs_ctx );
675
676static int ecdsa_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,
680 void *rs_ctx );
681
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200682/*
683 * Restart context for ECDSA operations with ECKEY context
684 *
685 * We need to store an actual ECDSA context, as we need to pass the same to
686 * the underlying ecdsa function, so we can't create it on the fly every time.
687 */
688typedef struct
689{
690 mbedtls_ecdsa_restart_ctx ecdsa_rs;
691 mbedtls_ecdsa_context ecdsa_ctx;
692} eckey_restart_ctx;
693
694static void *eckey_rs_alloc( void )
695{
696 eckey_restart_ctx *rs_ctx;
697
698 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
699
700 if( ctx != NULL )
701 {
702 rs_ctx = ctx;
703 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
704 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
705 }
706
707 return( ctx );
708}
709
710static void eckey_rs_free( void *ctx )
711{
712 eckey_restart_ctx *rs_ctx;
713
714 if( ctx == NULL)
715 return;
716
717 rs_ctx = ctx;
718 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
719 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
720
721 mbedtls_free( ctx );
722}
723
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200724static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
725 const unsigned char *hash, size_t hash_len,
726 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200727 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200728{
Janos Follath24eed8d2019-11-22 13:21:35 +0000729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200730 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200731
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200732 /* Should never happen */
733 if( rs == NULL )
734 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200735
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200736 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200737 if( rs->ecdsa_ctx.grp.pbits == 0 )
738 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200739
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200740 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
741 md_alg, hash, hash_len,
742 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200743
744cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200745 return( ret );
746}
747
748static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
749 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200750 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200751 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200752 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200753{
Janos Follath24eed8d2019-11-22 13:21:35 +0000754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200755 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200756
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200757 /* Should never happen */
758 if( rs == NULL )
759 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200760
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200761 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200762 if( rs->ecdsa_ctx.grp.pbits == 0 )
763 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200764
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200765 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200766 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200767 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200768
769cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200770 return( ret );
771}
772#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200774
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200775static int eckey_check_pair( const void *pub, const void *prv,
776 int (*f_rng)(void *, unsigned char *, size_t),
777 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200780 (const mbedtls_ecp_keypair *) prv,
781 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100782}
783
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200784static void *eckey_alloc_wrap( void )
785{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200786 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200787
788 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200790
791 return( ctx );
792}
793
794static void eckey_free_wrap( void *ctx )
795{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
797 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200798}
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200801{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200803 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200805}
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807const mbedtls_pk_info_t mbedtls_eckey_info = {
808 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200809 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200810 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200811 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200813 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200814 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200815#if defined(MBEDTLS_ECP_RESTARTABLE)
816 eckey_verify_rs_wrap,
817 eckey_sign_rs_wrap,
818#endif
819#else /* MBEDTLS_ECDSA_C */
820 NULL,
821 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200822#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200823 NULL,
824 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100825 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200826 eckey_alloc_wrap,
827 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200828#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200829 eckey_rs_alloc,
830 eckey_rs_free,
831#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200832 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200833};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200834
835/*
Paul Bakker75342a62014-04-08 17:35:40 +0200836 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200839{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 return( type == MBEDTLS_PK_ECKEY ||
841 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200842}
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844const mbedtls_pk_info_t mbedtls_eckeydh_info = {
845 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200846 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200847 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200848 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200849 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200850 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200851#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200852 NULL,
853 NULL,
854#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200855 NULL,
856 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100857 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200858 eckey_alloc_wrap, /* Same underlying key structure */
859 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200860#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200861 NULL,
862 NULL,
863#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200864 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200865};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_ECDSA_C)
869static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200872}
873
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400874#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400875/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500876 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
877 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500878 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500879static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
880 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500881{
Janos Follath24eed8d2019-11-22 13:21:35 +0000882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500883 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500884
Andrzej Kurek9241d182018-11-20 05:04:35 -0500885 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500886 MBEDTLS_ASN1_INTEGER ) ) != 0 )
887 {
888 return( ret );
889 }
890
Andrzej Kurek9241d182018-11-20 05:04:35 -0500891 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500892 {
893 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500894 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500895 }
896
Andrzej Kurek9241d182018-11-20 05:04:35 -0500897 if( unpadded_len > to_len || unpadded_len == 0 )
898 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500899
Andrzej Kurek9241d182018-11-20 05:04:35 -0500900 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500901 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500902 memcpy( to + padding_len, *from, unpadded_len );
903 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500904
Andrzej Kurek9241d182018-11-20 05:04:35 -0500905 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500906}
907
908/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400909 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500910 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500911 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400912 */
913static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500914 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400915{
Janos Follath24eed8d2019-11-22 13:21:35 +0000916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500917 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500918
Andrzej Kurek9241d182018-11-20 05:04:35 -0500919 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500920 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500921 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400922
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500923 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500924 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
925 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500926 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500927 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
928 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500929
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400930 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400931}
932
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100933static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400934 const unsigned char *hash, size_t hash_len,
935 const unsigned char *sig, size_t sig_len )
936{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100937 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100940 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200941 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400942 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400943 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100944 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000945 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400946 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700947 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100948 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100949 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100950 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
951 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700952 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400953
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500954 if( curve == 0 )
955 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
956
Hanno Beckerccf574e2019-01-29 08:26:15 +0000957 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500958 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400959 key.pk_info = &pk_info;
960 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000961 p = buf + sizeof( buf );
962 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400963 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500964 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400965
Gilles Peskined2d45c12019-05-27 14:53:13 +0200966 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100967 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200968 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500969
Gilles Peskined2d45c12019-05-27 14:53:13 +0200970 status = psa_import_key( &attributes,
971 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200972 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200973 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400974 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100975 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400976 goto cleanup;
977 }
978
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500979 /* We don't need the exported key anymore and can
980 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500981 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500982 {
983 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
984 goto cleanup;
985 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500986
Hanno Beckera9851112019-01-25 16:37:10 +0000987 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500988 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500989 signature_part_size ) ) != 0 )
990 {
991 goto cleanup;
992 }
993
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100994 status = psa_verify_hash( key_id, psa_sig_md,
995 hash, hash_len,
996 buf, 2 * signature_part_size );
997 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400998 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800999 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001000 goto cleanup;
1001 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -05001002
1003 if( p != sig + sig_len )
1004 {
1005 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1006 goto cleanup;
1007 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001008 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001009
Andrzej Kurekb7b04782018-11-19 17:01:16 -05001010cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +01001011 status = psa_destroy_key( key_id );
1012 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +01001013 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +01001014
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001015 return( ret );
1016}
1017#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001019 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001020 const unsigned char *sig, size_t sig_len )
1021{
Janos Follath24eed8d2019-11-22 13:21:35 +00001022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001023 ((void) md_alg);
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001026 hash, hash_len, sig, sig_len );
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1029 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001030
1031 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001032}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001033#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001034
Neil Armstronge9606902022-02-09 14:23:00 +01001035#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001036/*
1037 * Simultaneously convert and move raw MPI from the beginning of a buffer
1038 * to an ASN.1 MPI at the end of the buffer.
1039 * See also mbedtls_asn1_write_mpi().
1040 *
1041 * p: pointer to the end of the output buffer
1042 * start: start of the output buffer, and also of the mpi to write at the end
1043 * n_len: length of the mpi to read from start
1044 */
1045static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1046 size_t n_len )
1047{
1048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1049 size_t len = 0;
1050
1051 if( (size_t)( *p - start ) < n_len )
1052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1053
1054 len = n_len;
1055 *p -= len;
1056 memmove( *p, start, len );
1057
1058 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1059 * Neither r nor s should be 0, but as a failsafe measure, still detect
1060 * that rather than overflowing the buffer in case of a PSA error. */
1061 while( len > 0 && **p == 0x00 )
1062 {
1063 ++(*p);
1064 --len;
1065 }
1066
1067 /* this is only reached if the signature was invalid */
1068 if( len == 0 )
1069 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1070
1071 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1072 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1073 if( **p & 0x80 )
1074 {
1075 if( *p - start < 1 )
1076 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1077
1078 *--(*p) = 0x00;
1079 len += 1;
1080 }
1081
1082 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1083 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1084 MBEDTLS_ASN1_INTEGER ) );
1085
1086 return( (int) len );
1087}
1088
1089/* Transcode signature from PSA format to ASN.1 sequence.
1090 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1091 * MPIs, and in-place.
1092 *
1093 * [in/out] sig: the signature pre- and post-transcoding
1094 * [in/out] sig_len: signature length pre- and post-transcoding
1095 * [int] buf_len: the available size the in/out buffer
1096 */
Neil Armstronge9606902022-02-09 14:23:00 +01001097static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001098 size_t buf_len )
1099{
1100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1101 size_t len = 0;
1102 const size_t rs_len = *sig_len / 2;
1103 unsigned char *p = sig + buf_len;
1104
1105 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1106 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1107
1108 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1109 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1110 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1111
1112 memmove( sig, p, len );
1113 *sig_len = len;
1114
1115 return( 0 );
1116}
Neil Armstronge9606902022-02-09 14:23:00 +01001117
Neil Armstrong17a06552022-03-18 15:27:38 +01001118/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1119 *
1120 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1121 * [in] end: ASN.1 buffer end
1122 * [out] key_len: the ECDSA privateKey length in bytes
1123 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001124static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1125 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001126{
1127 size_t len;
1128 int ret;
1129
Neil Armstrong17a06552022-03-18 15:27:38 +01001130 /*
1131 * RFC 5915, or SEC1 Appendix C.4
1132 *
1133 * ECPrivateKey ::= SEQUENCE {
1134 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1135 * privateKey OCTET STRING,
1136 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1137 * publicKey [1] BIT STRING OPTIONAL
1138 * }
1139 */
1140
Neil Armstronge9606902022-02-09 14:23:00 +01001141 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001142 MBEDTLS_ASN1_CONSTRUCTED |
1143 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001144 return( ret );
1145
1146 /* version */
1147 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1148 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1149 return( ret );
1150
1151 *buf += len;
1152
1153 /* privateKey */
1154 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1155 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1156 return( ret );
1157
1158 *key_len = len;
1159
1160 return 0;
1161}
1162
1163static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1164 const unsigned char *hash, size_t hash_len,
1165 unsigned char *sig, size_t sig_size, size_t *sig_len,
1166 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1167{
1168 mbedtls_ecdsa_context *ctx = ctx_arg;
1169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1171 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1172 psa_status_t status;
1173 mbedtls_pk_context key;
1174 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001175 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001176 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001177 psa_algorithm_t psa_sig_md =
1178 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001179 size_t curve_bits;
1180 psa_ecc_family_t curve =
1181 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001182
1183 /* PSA has its own RNG */
1184 ((void) f_rng);
1185 ((void) p_rng);
1186
1187 if( curve == 0 )
1188 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1189
1190 /* mbedtls_pk_write_key_der() expects a full PK context;
1191 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001192 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001193 key.pk_ctx = ctx;
1194 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1195 if( key_len <= 0 )
1196 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1197
1198 p = buf + sizeof( buf ) - key_len;
1199 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1200 if( ret != 0 )
1201 goto cleanup;
1202
1203 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1205 psa_set_key_algorithm( &attributes, psa_sig_md );
1206
1207 status = psa_import_key( &attributes,
1208 p, key_len,
1209 &key_id );
1210 if( status != PSA_SUCCESS )
1211 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001212 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001213 goto cleanup;
1214 }
1215
Neil Armstronge4edcf72022-03-03 16:46:41 +01001216 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1217 sig, sig_size, sig_len );
1218 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001219 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001220 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001221 goto cleanup;
1222 }
1223
1224 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1225
1226cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001227 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001228 status = psa_destroy_key( key_id );
1229 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001230 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001231
Neil Armstronge9606902022-02-09 14:23:00 +01001232 return( ret );
1233}
1234#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001236 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001237 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001238 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001241 md_alg, hash, hash_len,
1242 sig, sig_size, sig_len,
1243 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001244}
Neil Armstronge9606902022-02-09 14:23:00 +01001245#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001246
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001247#if defined(MBEDTLS_ECP_RESTARTABLE)
1248static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1249 const unsigned char *hash, size_t hash_len,
1250 const unsigned char *sig, size_t sig_len,
1251 void *rs_ctx )
1252{
Janos Follath24eed8d2019-11-22 13:21:35 +00001253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001254 ((void) md_alg);
1255
1256 ret = mbedtls_ecdsa_read_signature_restartable(
1257 (mbedtls_ecdsa_context *) ctx,
1258 hash, hash_len, sig, sig_len,
1259 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1260
1261 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1262 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1263
1264 return( ret );
1265}
1266
1267static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1268 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001269 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001270 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1271 void *rs_ctx )
1272{
1273 return( mbedtls_ecdsa_write_signature_restartable(
1274 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001275 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001276 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1277
1278}
1279#endif /* MBEDTLS_ECP_RESTARTABLE */
1280
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001281static void *ecdsa_alloc_wrap( void )
1282{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001283 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001284
1285 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001287
1288 return( ctx );
1289}
1290
1291static void ecdsa_free_wrap( void *ctx )
1292{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1294 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001295}
1296
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001297#if defined(MBEDTLS_ECP_RESTARTABLE)
1298static void *ecdsa_rs_alloc( void )
1299{
1300 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1301
1302 if( ctx != NULL )
1303 mbedtls_ecdsa_restart_init( ctx );
1304
1305 return( ctx );
1306}
1307
1308static void ecdsa_rs_free( void *ctx )
1309{
1310 mbedtls_ecdsa_restart_free( ctx );
1311 mbedtls_free( ctx );
1312}
1313#endif /* MBEDTLS_ECP_RESTARTABLE */
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1316 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001317 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001318 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001319 ecdsa_can_do,
1320 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001321 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001322#if defined(MBEDTLS_ECP_RESTARTABLE)
1323 ecdsa_verify_rs_wrap,
1324 ecdsa_sign_rs_wrap,
1325#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001326 NULL,
1327 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001328 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001329 ecdsa_alloc_wrap,
1330 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001331#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001332 ecdsa_rs_alloc,
1333 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001334#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001335 eckey_debug, /* Compatible key structures */
1336};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001340/*
1341 * Support for alternative RSA-private implementations
1342 */
1343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001345{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001347}
1348
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001349static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001352
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001353 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001354}
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001357 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001358 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001359 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001362
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001363#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001364 if( UINT_MAX < hash_len )
1365 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001366#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001367
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001368 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001369 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1370 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001371 if( *sig_len > sig_size )
1372 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001373
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001374 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001375 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001376}
1377
1378static int rsa_alt_decrypt_wrap( void *ctx,
1379 const unsigned char *input, size_t ilen,
1380 unsigned char *output, size_t *olen, size_t osize,
1381 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1382{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001384
1385 ((void) f_rng);
1386 ((void) p_rng);
1387
1388 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001390
1391 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001392 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001393}
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001396static int rsa_alt_check_pair( const void *pub, const void *prv,
1397 int (*f_rng)(void *, unsigned char *, size_t),
1398 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001401 unsigned char hash[32];
1402 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001404
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001405 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001407
1408 memset( hash, 0x2a, sizeof( hash ) );
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001411 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001412 sig, sizeof( sig ), &sig_len,
1413 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001414 {
1415 return( ret );
1416 }
1417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001419 hash, sizeof( hash ), sig, sig_len ) != 0 )
1420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001422 }
1423
1424 return( 0 );
1425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001427
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001428static void *rsa_alt_alloc_wrap( void )
1429{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001430 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001431
1432 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001434
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001435 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001436}
1437
1438static void rsa_alt_free_wrap( void *ctx )
1439{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001440 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001442}
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1445 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001446 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001447 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001448 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001449 NULL,
1450 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001451#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001452 NULL,
1453 NULL,
1454#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001455 rsa_alt_decrypt_wrap,
1456 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001458 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001459#else
1460 NULL,
1461#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001462 rsa_alt_alloc_wrap,
1463 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001464#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001465 NULL,
1466 NULL,
1467#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001468 NULL,
1469};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001472
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001473#if defined(MBEDTLS_USE_PSA_CRYPTO)
1474
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001475static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001476{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001477 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001478
1479 /* no _init() function to call, an calloc() already zeroized */
1480
1481 return( ctx );
1482}
1483
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001484static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001485{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001486 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001487 mbedtls_free( ctx );
1488}
1489
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001490static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001491{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001492 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001493 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001495
Gilles Peskined2d45c12019-05-27 14:53:13 +02001496 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001497 return( 0 );
1498
Gilles Peskined2d45c12019-05-27 14:53:13 +02001499 bits = psa_get_key_bits( &attributes );
1500 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001501 return( bits );
1502}
1503
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001504static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001505{
1506 /* For now opaque PSA keys can only wrap ECC keypairs,
1507 * as checked by setup_psa().
1508 * Also, ECKEY_DH does not really make sense with the current API. */
1509 return( type == MBEDTLS_PK_ECKEY ||
1510 type == MBEDTLS_PK_ECDSA );
1511}
1512
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001513static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1514 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001515 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001516 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1517{
John Durkopf35069a2020-08-17 22:05:14 -07001518#if !defined(MBEDTLS_ECDSA_C)
1519 ((void) ctx);
1520 ((void) md_alg);
1521 ((void) hash);
1522 ((void) hash_len);
1523 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001524 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001525 ((void) sig_len);
1526 ((void) f_rng);
1527 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001528 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1529#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001530 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001531 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001532 psa_status_t status;
1533
1534 /* PSA has its own RNG */
1535 (void) f_rng;
1536 (void) p_rng;
1537
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001538 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001539 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001540 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001541 if( status != PSA_SUCCESS )
Jerry Yu848ecce2022-03-22 10:58:48 +08001542 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001543
1544 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001545 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001546#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001547}
1548
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001549const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1550 MBEDTLS_PK_OPAQUE,
1551 "Opaque",
1552 pk_opaque_get_bitlen,
1553 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001554 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001555 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001556#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1557 NULL, /* restartable verify - not relevant */
1558 NULL, /* restartable sign - not relevant */
1559#endif
1560 NULL, /* decrypt - will be done later */
1561 NULL, /* encrypt - will be done later */
1562 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001563 pk_opaque_alloc_wrap,
1564 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001565#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1566 NULL, /* restart alloc - not relevant */
1567 NULL, /* restart free - not relevant */
1568#endif
1569 NULL, /* debug - could be done later, or even left NULL */
1570};
1571
1572#endif /* MBEDTLS_USE_PSA_CRYPTO */
1573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#endif /* MBEDTLS_PK_C */