blob: 71b60398dd003535dc957a46622ce0a15aa90fc3 [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 {
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:
231 psa_destroy_key( key_id );
232 return( ret );
233}
234#else
235static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
236 const unsigned char *hash, size_t hash_len,
237 const unsigned char *sig, size_t sig_len )
238{
Janos Follath24eed8d2019-11-22 13:21:35 +0000239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100240 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
241 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200242
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100243#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000244 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
245 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100246#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000247
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100248 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200250
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100251 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
252 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100253 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200254 return( ret );
255
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200256 /* The buffer contains a valid signature followed by extra data.
257 * We have a special error code for that so that so that callers can
258 * use mbedtls_pk_verify() to check "Does the buffer start with a
259 * valid signature?" and not just "Does the buffer contain a valid
260 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100261 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200263
264 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200265}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100266#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200267
Jerry Yub02ee182022-03-16 10:30:41 +0800268#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yue010de42022-03-23 11:45:55 +0800269int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
270 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800271 const unsigned char *hash, size_t hash_len,
272 unsigned char *sig, size_t sig_size,
273 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100274{
Neil Armstrong98545682022-02-22 16:12:51 +0100275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
277 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
278 psa_status_t status;
279 mbedtls_pk_context key;
280 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100281 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100282 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100283
Jerry Yue010de42022-03-23 11:45:55 +0800284 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100285 if( sig_size < *sig_len )
286 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
287
Neil Armstronge4f28682022-02-24 15:41:39 +0100288 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100289 * re-construct one to make it happy */
290 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800291 key.pk_ctx = rsa_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100292 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
293 if( key_len <= 0 )
294 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100295 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800296 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100297 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
298
299 status = psa_import_key( &attributes,
300 buf + sizeof( buf ) - key_len, key_len,
301 &key_id );
302 if( status != PSA_SUCCESS )
303 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100304 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100305 goto cleanup;
306 }
Jerry Yubf455e72022-03-22 21:39:41 +0800307 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100308 sig, sig_size, sig_len );
309 if( status != PSA_SUCCESS )
310 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100311 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100312 goto cleanup;
313 }
314
315 ret = 0;
316
317cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100318 status = psa_destroy_key( key_id );
319 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100320 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100321 return( ret );
322}
Jerry Yu406cf272022-03-22 11:33:42 +0800323#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800324
325#if defined(MBEDTLS_USE_PSA_CRYPTO)
326static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
327 const unsigned char *hash, size_t hash_len,
328 unsigned char *sig, size_t sig_size, size_t *sig_len,
329 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
330{
Jerry Yu1d172a32022-03-12 19:12:05 +0800331 ((void) f_rng);
332 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800333
Jerry Yubd1b3272022-03-24 13:05:20 +0800334 psa_algorithm_t psa_md_alg;
335 psa_md_alg = mbedtls_psa_translate_md( md_alg );
336 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800337 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800338
Jerry Yubd1b3272022-03-24 13:05:20 +0800339 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
340 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800341 ctx, hash, hash_len,
342 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800343}
Neil Armstrong98545682022-02-22 16:12:51 +0100344#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200346 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200347 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200348 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
349{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100350 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
351
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100352#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000353 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
354 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100355#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000356
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100357 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200358 if( sig_size < *sig_len )
359 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200360
Thomas Daubney140184d2021-05-18 16:04:07 +0100361 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
362 md_alg, (unsigned int) hash_len,
363 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200364}
Neil Armstrong98545682022-02-22 16:12:51 +0100365#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200366
Neil Armstrong18f43c72022-02-09 15:32:45 +0100367#if defined(MBEDTLS_USE_PSA_CRYPTO)
368static int rsa_decrypt_wrap( void *ctx,
369 const unsigned char *input, size_t ilen,
370 unsigned char *output, size_t *olen, size_t osize,
371 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
372{
373 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
376 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
377 psa_status_t status;
378 mbedtls_pk_context key;
379 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100380 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100381
382 ((void) f_rng);
383 ((void) p_rng);
384
385#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100386 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
387 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
388#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100389
390 if( ilen != mbedtls_rsa_get_len( rsa ) )
391 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
392
393 /* mbedtls_pk_write_key_der() expects a full PK context;
394 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100395 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100396 key.pk_ctx = ctx;
397 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
398 if( key_len <= 0 )
399 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
400
401 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
402 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100403 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100404
405 status = psa_import_key( &attributes,
406 buf + sizeof( buf ) - key_len, key_len,
407 &key_id );
408 if( status != PSA_SUCCESS )
409 {
Neil Armstronge8780492022-03-03 16:54:16 +0100410 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100411 goto cleanup;
412 }
413
Neil Armstrong8e805042022-03-16 15:30:31 +0100414 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
415 input, ilen,
416 NULL, 0,
417 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100418 if( status != PSA_SUCCESS )
419 {
Neil Armstronge8780492022-03-03 16:54:16 +0100420 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100421 goto cleanup;
422 }
423
424 ret = 0;
425
426cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100427 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100428 status = psa_destroy_key( key_id );
429 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100430 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100431
Neil Armstrong18f43c72022-02-09 15:32:45 +0100432 return( ret );
433}
434#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200435static int rsa_decrypt_wrap( void *ctx,
436 const unsigned char *input, size_t ilen,
437 unsigned char *output, size_t *olen, size_t osize,
438 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
439{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100440 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
441
442 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200444
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100445 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100446 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200447}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100448#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200449
Neil Armstrong96a16a42022-02-10 10:40:11 +0100450#if defined(MBEDTLS_USE_PSA_CRYPTO)
451static int rsa_encrypt_wrap( void *ctx,
452 const unsigned char *input, size_t ilen,
453 unsigned char *output, size_t *olen, size_t osize,
454 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
455{
456 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
459 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
460 psa_status_t status;
461 mbedtls_pk_context key;
462 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100463 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100464
465 ((void) f_rng);
466 ((void) p_rng);
467
468#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100469 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
470 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100471#endif
472
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100473 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100474 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
475
Neil Armstrongac014ca2022-02-24 15:27:54 +0100476 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100477 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100478 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100479 key.pk_ctx = ctx;
480 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
481 if( key_len <= 0 )
482 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
483
484 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100485 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100486 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
487
488 status = psa_import_key( &attributes,
489 buf + sizeof( buf ) - key_len, key_len,
490 &key_id );
491 if( status != PSA_SUCCESS )
492 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100493 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100494 goto cleanup;
495 }
496
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100497 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
498 input, ilen,
499 NULL, 0,
500 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100501 if( status != PSA_SUCCESS )
502 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100503 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100504 goto cleanup;
505 }
506
507 ret = 0;
508
509cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100510 status = psa_destroy_key( key_id );
511 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100512 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100513
Neil Armstrong96a16a42022-02-10 10:40:11 +0100514 return( ret );
515}
516#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200517static int rsa_encrypt_wrap( void *ctx,
518 const unsigned char *input, size_t ilen,
519 unsigned char *output, size_t *olen, size_t osize,
520 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
521{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100522 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
523 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200524
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100525 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100527
Thomas Daubney21772772021-05-13 17:30:32 +0100528 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100529 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200530}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100531#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200532
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200533static int rsa_check_pair_wrap( const void *pub, const void *prv,
534 int (*f_rng)(void *, unsigned char *, size_t),
535 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100536{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200537 (void) f_rng;
538 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
540 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100541}
542
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200543static void *rsa_alloc_wrap( void )
544{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200545 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200546
547 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200548 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200549
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200550 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200551}
552
553static void rsa_free_wrap( void *ctx )
554{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
556 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200557}
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200560{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200561#if defined(MBEDTLS_RSA_ALT)
562 /* Not supported */
563 (void) ctx;
564 (void) items;
565#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200567 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200569
570 items++;
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200573 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200575#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200576}
577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578const mbedtls_pk_info_t mbedtls_rsa_info = {
579 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200580 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200581 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200582 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200583 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200584 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200585#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200586 NULL,
587 NULL,
588#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200589 rsa_decrypt_wrap,
590 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100591 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200592 rsa_alloc_wrap,
593 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200594#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200595 NULL,
596 NULL,
597#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200598 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200599};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200603/*
604 * Generic EC key
605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( type == MBEDTLS_PK_ECKEY ||
609 type == MBEDTLS_PK_ECKEY_DH ||
610 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200611}
612
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200613static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200616}
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200619/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200621 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200622 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200625 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200626 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200627 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200630 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200631 const unsigned char *sig, size_t sig_len )
632{
Janos Follath24eed8d2019-11-22 13:21:35 +0000633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200639 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200642
643 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200644}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200647 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200648 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200649 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
650{
Janos Follath24eed8d2019-11-22 13:21:35 +0000651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200657 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
658 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200659 f_rng, p_rng );
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200662
663 return( ret );
664}
665
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200666#if defined(MBEDTLS_ECP_RESTARTABLE)
667/* Forward declarations */
668static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
669 const unsigned char *hash, size_t hash_len,
670 const unsigned char *sig, size_t sig_len,
671 void *rs_ctx );
672
673static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
674 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200675 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200676 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
677 void *rs_ctx );
678
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200679/*
680 * Restart context for ECDSA operations with ECKEY context
681 *
682 * We need to store an actual ECDSA context, as we need to pass the same to
683 * the underlying ecdsa function, so we can't create it on the fly every time.
684 */
685typedef struct
686{
687 mbedtls_ecdsa_restart_ctx ecdsa_rs;
688 mbedtls_ecdsa_context ecdsa_ctx;
689} eckey_restart_ctx;
690
691static void *eckey_rs_alloc( void )
692{
693 eckey_restart_ctx *rs_ctx;
694
695 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
696
697 if( ctx != NULL )
698 {
699 rs_ctx = ctx;
700 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
701 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
702 }
703
704 return( ctx );
705}
706
707static void eckey_rs_free( void *ctx )
708{
709 eckey_restart_ctx *rs_ctx;
710
711 if( ctx == NULL)
712 return;
713
714 rs_ctx = ctx;
715 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
716 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
717
718 mbedtls_free( ctx );
719}
720
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200721static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
722 const unsigned char *hash, size_t hash_len,
723 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200724 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200725{
Janos Follath24eed8d2019-11-22 13:21:35 +0000726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200727 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200728
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200729 /* Should never happen */
730 if( rs == NULL )
731 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200732
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200733 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200734 if( rs->ecdsa_ctx.grp.pbits == 0 )
735 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200736
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200737 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
738 md_alg, hash, hash_len,
739 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200740
741cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200742 return( ret );
743}
744
745static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
746 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200747 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200748 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200749 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200750{
Janos Follath24eed8d2019-11-22 13:21:35 +0000751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200752 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200753
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200754 /* Should never happen */
755 if( rs == NULL )
756 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200757
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200758 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200759 if( rs->ecdsa_ctx.grp.pbits == 0 )
760 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200761
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200762 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200763 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200764 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200765
766cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200767 return( ret );
768}
769#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200771
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200772static int eckey_check_pair( const void *pub, const void *prv,
773 int (*f_rng)(void *, unsigned char *, size_t),
774 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200777 (const mbedtls_ecp_keypair *) prv,
778 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100779}
780
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200781static void *eckey_alloc_wrap( void )
782{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200783 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200784
785 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200787
788 return( ctx );
789}
790
791static void eckey_free_wrap( void *ctx )
792{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
794 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200795}
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200798{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200800 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200802}
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804const mbedtls_pk_info_t mbedtls_eckey_info = {
805 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200806 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200807 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200808 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200810 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200811 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200812#if defined(MBEDTLS_ECP_RESTARTABLE)
813 eckey_verify_rs_wrap,
814 eckey_sign_rs_wrap,
815#endif
816#else /* MBEDTLS_ECDSA_C */
817 NULL,
818 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200819#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200820 NULL,
821 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100822 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200823 eckey_alloc_wrap,
824 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200825#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200826 eckey_rs_alloc,
827 eckey_rs_free,
828#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200829 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200830};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200831
832/*
Paul Bakker75342a62014-04-08 17:35:40 +0200833 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 return( type == MBEDTLS_PK_ECKEY ||
838 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200839}
840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841const mbedtls_pk_info_t mbedtls_eckeydh_info = {
842 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200843 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200844 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200845 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200846 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200847 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200848#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200849 NULL,
850 NULL,
851#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200852 NULL,
853 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100854 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200855 eckey_alloc_wrap, /* Same underlying key structure */
856 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200857#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200858 NULL,
859 NULL,
860#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200861 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200862};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865#if defined(MBEDTLS_ECDSA_C)
866static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200867{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200869}
870
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400871#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400872/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500873 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
874 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500875 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500876static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
877 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500878{
Janos Follath24eed8d2019-11-22 13:21:35 +0000879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500880 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500881
Andrzej Kurek9241d182018-11-20 05:04:35 -0500882 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500883 MBEDTLS_ASN1_INTEGER ) ) != 0 )
884 {
885 return( ret );
886 }
887
Andrzej Kurek9241d182018-11-20 05:04:35 -0500888 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500889 {
890 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500891 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500892 }
893
Andrzej Kurek9241d182018-11-20 05:04:35 -0500894 if( unpadded_len > to_len || unpadded_len == 0 )
895 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500896
Andrzej Kurek9241d182018-11-20 05:04:35 -0500897 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500898 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500899 memcpy( to + padding_len, *from, unpadded_len );
900 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500901
Andrzej Kurek9241d182018-11-20 05:04:35 -0500902 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500903}
904
905/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400906 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500907 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500908 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400909 */
910static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500911 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400912{
Janos Follath24eed8d2019-11-22 13:21:35 +0000913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500914 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500915
Andrzej Kurek9241d182018-11-20 05:04:35 -0500916 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500917 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500918 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400919
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500920 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500921 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
922 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500923 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500924 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
925 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500926
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400927 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400928}
929
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100930static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400931 const unsigned char *hash, size_t hash_len,
932 const unsigned char *sig, size_t sig_len )
933{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100934 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000935 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100937 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200938 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400939 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400940 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100941 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000942 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400943 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700944 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100945 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100946 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100947 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
948 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700949 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400950
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500951 if( curve == 0 )
952 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
953
Hanno Beckerccf574e2019-01-29 08:26:15 +0000954 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500955 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400956 key.pk_info = &pk_info;
957 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000958 p = buf + sizeof( buf );
959 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400960 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500961 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400962
Gilles Peskined2d45c12019-05-27 14:53:13 +0200963 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100964 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200965 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500966
Gilles Peskined2d45c12019-05-27 14:53:13 +0200967 status = psa_import_key( &attributes,
968 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200969 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200970 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400971 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100972 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400973 goto cleanup;
974 }
975
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500976 /* We don't need the exported key anymore and can
977 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500978 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500979 {
980 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
981 goto cleanup;
982 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500983
Hanno Beckera9851112019-01-25 16:37:10 +0000984 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500985 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500986 signature_part_size ) ) != 0 )
987 {
988 goto cleanup;
989 }
990
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100991 status = psa_verify_hash( key_id, psa_sig_md,
992 hash, hash_len,
993 buf, 2 * signature_part_size );
994 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400995 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800996 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400997 goto cleanup;
998 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500999
1000 if( p != sig + sig_len )
1001 {
1002 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1003 goto cleanup;
1004 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001005 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001006
Andrzej Kurekb7b04782018-11-19 17:01:16 -05001007cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +01001008 status = psa_destroy_key( key_id );
1009 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +01001010 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +01001011
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001012 return( ret );
1013}
1014#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001016 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001017 const unsigned char *sig, size_t sig_len )
1018{
Janos Follath24eed8d2019-11-22 13:21:35 +00001019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001020 ((void) md_alg);
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001023 hash, hash_len, sig, sig_len );
1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1026 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001027
1028 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001029}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001030#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001031
Neil Armstronge9606902022-02-09 14:23:00 +01001032#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001033/*
1034 * Simultaneously convert and move raw MPI from the beginning of a buffer
1035 * to an ASN.1 MPI at the end of the buffer.
1036 * See also mbedtls_asn1_write_mpi().
1037 *
1038 * p: pointer to the end of the output buffer
1039 * start: start of the output buffer, and also of the mpi to write at the end
1040 * n_len: length of the mpi to read from start
1041 */
1042static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1043 size_t n_len )
1044{
1045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1046 size_t len = 0;
1047
1048 if( (size_t)( *p - start ) < n_len )
1049 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1050
1051 len = n_len;
1052 *p -= len;
1053 memmove( *p, start, len );
1054
1055 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1056 * Neither r nor s should be 0, but as a failsafe measure, still detect
1057 * that rather than overflowing the buffer in case of a PSA error. */
1058 while( len > 0 && **p == 0x00 )
1059 {
1060 ++(*p);
1061 --len;
1062 }
1063
1064 /* this is only reached if the signature was invalid */
1065 if( len == 0 )
1066 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1067
1068 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1069 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1070 if( **p & 0x80 )
1071 {
1072 if( *p - start < 1 )
1073 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1074
1075 *--(*p) = 0x00;
1076 len += 1;
1077 }
1078
1079 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1080 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1081 MBEDTLS_ASN1_INTEGER ) );
1082
1083 return( (int) len );
1084}
1085
1086/* Transcode signature from PSA format to ASN.1 sequence.
1087 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1088 * MPIs, and in-place.
1089 *
1090 * [in/out] sig: the signature pre- and post-transcoding
1091 * [in/out] sig_len: signature length pre- and post-transcoding
1092 * [int] buf_len: the available size the in/out buffer
1093 */
Neil Armstronge9606902022-02-09 14:23:00 +01001094static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001095 size_t buf_len )
1096{
1097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1098 size_t len = 0;
1099 const size_t rs_len = *sig_len / 2;
1100 unsigned char *p = sig + buf_len;
1101
1102 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1103 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1104
1105 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1106 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1107 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1108
1109 memmove( sig, p, len );
1110 *sig_len = len;
1111
1112 return( 0 );
1113}
Neil Armstronge9606902022-02-09 14:23:00 +01001114
Neil Armstrong17a06552022-03-18 15:27:38 +01001115/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1116 *
1117 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1118 * [in] end: ASN.1 buffer end
1119 * [out] key_len: the ECDSA privateKey length in bytes
1120 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001121static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1122 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001123{
1124 size_t len;
1125 int ret;
1126
Neil Armstrong17a06552022-03-18 15:27:38 +01001127 /*
1128 * RFC 5915, or SEC1 Appendix C.4
1129 *
1130 * ECPrivateKey ::= SEQUENCE {
1131 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1132 * privateKey OCTET STRING,
1133 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1134 * publicKey [1] BIT STRING OPTIONAL
1135 * }
1136 */
1137
Neil Armstronge9606902022-02-09 14:23:00 +01001138 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001139 MBEDTLS_ASN1_CONSTRUCTED |
1140 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001141 return( ret );
1142
1143 /* version */
1144 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1145 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1146 return( ret );
1147
1148 *buf += len;
1149
1150 /* privateKey */
1151 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1152 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1153 return( ret );
1154
1155 *key_len = len;
1156
1157 return 0;
1158}
1159
1160static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1161 const unsigned char *hash, size_t hash_len,
1162 unsigned char *sig, size_t sig_size, size_t *sig_len,
1163 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1164{
1165 mbedtls_ecdsa_context *ctx = ctx_arg;
1166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1168 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1169 psa_status_t status;
1170 mbedtls_pk_context key;
1171 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001172 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001173 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001174 psa_algorithm_t psa_sig_md =
1175 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001176 size_t curve_bits;
1177 psa_ecc_family_t curve =
1178 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001179
1180 /* PSA has its own RNG */
1181 ((void) f_rng);
1182 ((void) p_rng);
1183
1184 if( curve == 0 )
1185 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1186
1187 /* mbedtls_pk_write_key_der() expects a full PK context;
1188 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001189 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001190 key.pk_ctx = ctx;
1191 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1192 if( key_len <= 0 )
1193 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1194
1195 p = buf + sizeof( buf ) - key_len;
1196 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1197 if( ret != 0 )
1198 goto cleanup;
1199
1200 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1201 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1202 psa_set_key_algorithm( &attributes, psa_sig_md );
1203
1204 status = psa_import_key( &attributes,
1205 p, key_len,
1206 &key_id );
1207 if( status != PSA_SUCCESS )
1208 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001209 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001210 goto cleanup;
1211 }
1212
Neil Armstronge4edcf72022-03-03 16:46:41 +01001213 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1214 sig, sig_size, sig_len );
1215 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001216 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001217 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001218 goto cleanup;
1219 }
1220
1221 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1222
1223cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001224 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001225 status = psa_destroy_key( key_id );
1226 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001227 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001228
Neil Armstronge9606902022-02-09 14:23:00 +01001229 return( ret );
1230}
1231#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001233 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001234 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001235 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1236{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001238 md_alg, hash, hash_len,
1239 sig, sig_size, sig_len,
1240 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001241}
Neil Armstronge9606902022-02-09 14:23:00 +01001242#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001243
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001244#if defined(MBEDTLS_ECP_RESTARTABLE)
1245static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1246 const unsigned char *hash, size_t hash_len,
1247 const unsigned char *sig, size_t sig_len,
1248 void *rs_ctx )
1249{
Janos Follath24eed8d2019-11-22 13:21:35 +00001250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001251 ((void) md_alg);
1252
1253 ret = mbedtls_ecdsa_read_signature_restartable(
1254 (mbedtls_ecdsa_context *) ctx,
1255 hash, hash_len, sig, sig_len,
1256 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1257
1258 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1259 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1260
1261 return( ret );
1262}
1263
1264static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1265 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001266 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001267 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1268 void *rs_ctx )
1269{
1270 return( mbedtls_ecdsa_write_signature_restartable(
1271 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001272 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001273 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1274
1275}
1276#endif /* MBEDTLS_ECP_RESTARTABLE */
1277
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001278static void *ecdsa_alloc_wrap( void )
1279{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001280 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001281
1282 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001284
1285 return( ctx );
1286}
1287
1288static void ecdsa_free_wrap( void *ctx )
1289{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1291 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001292}
1293
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001294#if defined(MBEDTLS_ECP_RESTARTABLE)
1295static void *ecdsa_rs_alloc( void )
1296{
1297 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1298
1299 if( ctx != NULL )
1300 mbedtls_ecdsa_restart_init( ctx );
1301
1302 return( ctx );
1303}
1304
1305static void ecdsa_rs_free( void *ctx )
1306{
1307 mbedtls_ecdsa_restart_free( ctx );
1308 mbedtls_free( ctx );
1309}
1310#endif /* MBEDTLS_ECP_RESTARTABLE */
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1313 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001314 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001315 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001316 ecdsa_can_do,
1317 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001318 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001319#if defined(MBEDTLS_ECP_RESTARTABLE)
1320 ecdsa_verify_rs_wrap,
1321 ecdsa_sign_rs_wrap,
1322#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001323 NULL,
1324 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001325 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001326 ecdsa_alloc_wrap,
1327 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001328#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001329 ecdsa_rs_alloc,
1330 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001331#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001332 eckey_debug, /* Compatible key structures */
1333};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001337/*
1338 * Support for alternative RSA-private implementations
1339 */
1340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001344}
1345
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001346static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001350 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001351}
1352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001354 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001355 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001359
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001360#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001361 if( UINT_MAX < hash_len )
1362 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001363#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001364
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001365 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001366 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1367 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001368 if( *sig_len > sig_size )
1369 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001370
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001371 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001372 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001373}
1374
1375static int rsa_alt_decrypt_wrap( void *ctx,
1376 const unsigned char *input, size_t ilen,
1377 unsigned char *output, size_t *olen, size_t osize,
1378 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1379{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001381
1382 ((void) f_rng);
1383 ((void) p_rng);
1384
1385 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001387
1388 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001389 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001390}
1391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001393static int rsa_alt_check_pair( const void *pub, const void *prv,
1394 int (*f_rng)(void *, unsigned char *, size_t),
1395 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001398 unsigned char hash[32];
1399 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001401
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001402 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001404
1405 memset( hash, 0x2a, sizeof( hash ) );
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001408 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001409 sig, sizeof( sig ), &sig_len,
1410 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001411 {
1412 return( ret );
1413 }
1414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001416 hash, sizeof( hash ), sig, sig_len ) != 0 )
1417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001419 }
1420
1421 return( 0 );
1422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001424
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001425static void *rsa_alt_alloc_wrap( void )
1426{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001427 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001428
1429 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001431
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001432 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001433}
1434
1435static void rsa_alt_free_wrap( void *ctx )
1436{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001437 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001439}
1440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1442 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001443 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001444 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001445 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001446 NULL,
1447 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001448#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001449 NULL,
1450 NULL,
1451#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001452 rsa_alt_decrypt_wrap,
1453 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001455 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001456#else
1457 NULL,
1458#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001459 rsa_alt_alloc_wrap,
1460 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001461#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001462 NULL,
1463 NULL,
1464#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001465 NULL,
1466};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001469
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001470#if defined(MBEDTLS_USE_PSA_CRYPTO)
1471
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001472static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001473{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001474 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001475
1476 /* no _init() function to call, an calloc() already zeroized */
1477
1478 return( ctx );
1479}
1480
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001481static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001482{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001483 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001484 mbedtls_free( ctx );
1485}
1486
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001487static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001488{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001489 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001490 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001491 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001492
Gilles Peskined2d45c12019-05-27 14:53:13 +02001493 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001494 return( 0 );
1495
Gilles Peskined2d45c12019-05-27 14:53:13 +02001496 bits = psa_get_key_bits( &attributes );
1497 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001498 return( bits );
1499}
1500
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001501static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001502{
1503 /* For now opaque PSA keys can only wrap ECC keypairs,
1504 * as checked by setup_psa().
1505 * Also, ECKEY_DH does not really make sense with the current API. */
1506 return( type == MBEDTLS_PK_ECKEY ||
1507 type == MBEDTLS_PK_ECDSA );
1508}
1509
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001510static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1511 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001512 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001513 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1514{
John Durkopf35069a2020-08-17 22:05:14 -07001515#if !defined(MBEDTLS_ECDSA_C)
1516 ((void) ctx);
1517 ((void) md_alg);
1518 ((void) hash);
1519 ((void) hash_len);
1520 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001521 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001522 ((void) sig_len);
1523 ((void) f_rng);
1524 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001525 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1526#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001527 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001528 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001529 psa_status_t status;
1530
1531 /* PSA has its own RNG */
1532 (void) f_rng;
1533 (void) p_rng;
1534
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001535 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001536 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001537 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001538 if( status != PSA_SUCCESS )
Jerry Yu848ecce2022-03-22 10:58:48 +08001539 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001540
1541 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001542 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001543#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001544}
1545
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001546const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1547 MBEDTLS_PK_OPAQUE,
1548 "Opaque",
1549 pk_opaque_get_bitlen,
1550 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001551 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001552 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001553#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1554 NULL, /* restartable verify - not relevant */
1555 NULL, /* restartable sign - not relevant */
1556#endif
1557 NULL, /* decrypt - will be done later */
1558 NULL, /* encrypt - will be done later */
1559 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001560 pk_opaque_alloc_wrap,
1561 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001562#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1563 NULL, /* restart alloc - not relevant */
1564 NULL, /* restart free - not relevant */
1565#endif
1566 NULL, /* debug - could be done later, or even left NULL */
1567};
1568
1569#endif /* MBEDTLS_USE_PSA_CRYPTO */
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#endif /* MBEDTLS_PK_C */