blob: 266829011a456dcf530c326f79ba30a703dd0034 [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 Armstrong82cf8042022-03-03 12:30:59 +0100180 psa_algorithm_t psa_alg_md =
181 PSA_ALG_RSA_PKCS1V15_SIGN( mbedtls_psa_translate_md( md_alg ) );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100182 size_t rsa_len = mbedtls_rsa_get_len( rsa );
183
184#if SIZE_MAX > UINT_MAX
185 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
186 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
187#endif /* SIZE_MAX > UINT_MAX */
188
189 if( sig_len < rsa_len )
190 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
191
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100192 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100193 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100194 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100195 key.pk_ctx = ctx;
196 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
197 if( key_len <= 0 )
198 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
199
200 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
201 psa_set_key_algorithm( &attributes, psa_alg_md );
202 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
203
204 status = psa_import_key( &attributes,
205 buf + sizeof( buf ) - key_len, key_len,
206 &key_id );
207 if( status != PSA_SUCCESS )
208 {
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100209 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100210 goto cleanup;
211 }
212
213 status = psa_verify_hash( key_id, psa_alg_md, hash, hash_len,
214 sig, sig_len );
215 if( status != PSA_SUCCESS )
216 {
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100217 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong52f41f82022-02-22 15:30:24 +0100218 goto cleanup;
219 }
220 ret = 0;
221
222cleanup:
Neil Armstronga33280a2022-02-24 15:17:47 +0100223 status = psa_destroy_key( key_id );
224 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong19e6bc42022-03-03 16:50:11 +0100225 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronga33280a2022-02-24 15:17:47 +0100226
Neil Armstrong52f41f82022-02-22 15:30:24 +0100227 return( ret );
228}
229#else
230static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
231 const unsigned char *hash, size_t hash_len,
232 const unsigned char *sig, size_t sig_len )
233{
Janos Follath24eed8d2019-11-22 13:21:35 +0000234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100235 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
236 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200237
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100238#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000239 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
240 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100241#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000242
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100243 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200245
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100246 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
247 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100248 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200249 return( ret );
250
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200251 /* The buffer contains a valid signature followed by extra data.
252 * We have a special error code for that so that so that callers can
253 * use mbedtls_pk_verify() to check "Does the buffer start with a
254 * valid signature?" and not just "Does the buffer contain a valid
255 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100256 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200258
259 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200260}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100261#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200262
Jerry Yub02ee182022-03-16 10:30:41 +0800263#if defined(MBEDTLS_PSA_CRYPTO_C)
Jerry Yue010de42022-03-23 11:45:55 +0800264int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t alg,
265 mbedtls_rsa_context *rsa_ctx,
Jerry Yu89107d12022-03-22 14:20:15 +0800266 const unsigned char *hash, size_t hash_len,
267 unsigned char *sig, size_t sig_size,
268 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100269{
Neil Armstrong98545682022-02-22 16:12:51 +0100270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
272 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
273 psa_status_t status;
274 mbedtls_pk_context key;
275 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100276 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100277 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100278
Jerry Yue010de42022-03-23 11:45:55 +0800279 *sig_len = mbedtls_rsa_get_len( rsa_ctx );
Neil Armstrong98545682022-02-22 16:12:51 +0100280 if( sig_size < *sig_len )
281 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
282
Neil Armstronge4f28682022-02-24 15:41:39 +0100283 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100284 * re-construct one to make it happy */
285 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800286 key.pk_ctx = rsa_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100287 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
288 if( key_len <= 0 )
289 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100290 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Jerry Yubf455e72022-03-22 21:39:41 +0800291 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong98545682022-02-22 16:12:51 +0100292 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
293
294 status = psa_import_key( &attributes,
295 buf + sizeof( buf ) - key_len, key_len,
296 &key_id );
297 if( status != PSA_SUCCESS )
298 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100299 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100300 goto cleanup;
301 }
Jerry Yubf455e72022-03-22 21:39:41 +0800302 status = psa_sign_hash( key_id, alg, hash, hash_len,
Neil Armstrong98545682022-02-22 16:12:51 +0100303 sig, sig_size, sig_len );
304 if( status != PSA_SUCCESS )
305 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100306 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100307 goto cleanup;
308 }
309
310 ret = 0;
311
312cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100313 status = psa_destroy_key( key_id );
314 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100315 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100316 return( ret );
317}
Jerry Yu406cf272022-03-22 11:33:42 +0800318#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800319
320#if defined(MBEDTLS_USE_PSA_CRYPTO)
321static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
322 const unsigned char *hash, size_t hash_len,
323 unsigned char *sig, size_t sig_size, size_t *sig_len,
324 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
325{
Jerry Yu1d172a32022-03-12 19:12:05 +0800326 ((void) f_rng);
327 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800328
Jerry Yubd1b3272022-03-24 13:05:20 +0800329 psa_algorithm_t psa_md_alg;
330 psa_md_alg = mbedtls_psa_translate_md( md_alg );
331 if( psa_md_alg == 0 )
Jerry Yu1d172a32022-03-12 19:12:05 +0800332 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Jerry Yu1d172a32022-03-12 19:12:05 +0800333
Jerry Yubd1b3272022-03-24 13:05:20 +0800334 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
335 psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800336 ctx, hash, hash_len,
337 sig, sig_size, sig_len ) );
Jerry Yu1d172a32022-03-12 19:12:05 +0800338}
Neil Armstrong98545682022-02-22 16:12:51 +0100339#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200341 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200342 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200343 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
344{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100345 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
346
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100347#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000348 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
349 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100350#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000351
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100352 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200353 if( sig_size < *sig_len )
354 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200355
Thomas Daubney140184d2021-05-18 16:04:07 +0100356 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
357 md_alg, (unsigned int) hash_len,
358 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200359}
Neil Armstrong98545682022-02-22 16:12:51 +0100360#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200361
Neil Armstrong18f43c72022-02-09 15:32:45 +0100362#if defined(MBEDTLS_USE_PSA_CRYPTO)
363static int rsa_decrypt_wrap( void *ctx,
364 const unsigned char *input, size_t ilen,
365 unsigned char *output, size_t *olen, size_t osize,
366 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
367{
368 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
371 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
372 psa_status_t status;
373 mbedtls_pk_context key;
374 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100375 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100376
377 ((void) f_rng);
378 ((void) p_rng);
379
380#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100381 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
382 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
383#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100384
385 if( ilen != mbedtls_rsa_get_len( rsa ) )
386 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
387
388 /* mbedtls_pk_write_key_der() expects a full PK context;
389 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100390 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100391 key.pk_ctx = ctx;
392 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
393 if( key_len <= 0 )
394 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
395
396 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
397 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100398 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100399
400 status = psa_import_key( &attributes,
401 buf + sizeof( buf ) - key_len, key_len,
402 &key_id );
403 if( status != PSA_SUCCESS )
404 {
Neil Armstronge8780492022-03-03 16:54:16 +0100405 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100406 goto cleanup;
407 }
408
Neil Armstrong8e805042022-03-16 15:30:31 +0100409 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
410 input, ilen,
411 NULL, 0,
412 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100413 if( status != PSA_SUCCESS )
414 {
Neil Armstronge8780492022-03-03 16:54:16 +0100415 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100416 goto cleanup;
417 }
418
419 ret = 0;
420
421cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100422 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100423 status = psa_destroy_key( key_id );
424 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100425 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100426
Neil Armstrong18f43c72022-02-09 15:32:45 +0100427 return( ret );
428}
429#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200430static int rsa_decrypt_wrap( void *ctx,
431 const unsigned char *input, size_t ilen,
432 unsigned char *output, size_t *olen, size_t osize,
433 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
434{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100435 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
436
437 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200439
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100440 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100441 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200442}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100443#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200444
Neil Armstrong96a16a42022-02-10 10:40:11 +0100445#if defined(MBEDTLS_USE_PSA_CRYPTO)
446static int rsa_encrypt_wrap( void *ctx,
447 const unsigned char *input, size_t ilen,
448 unsigned char *output, size_t *olen, size_t osize,
449 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
450{
451 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
454 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
455 psa_status_t status;
456 mbedtls_pk_context key;
457 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100458 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100459
460 ((void) f_rng);
461 ((void) p_rng);
462
463#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100464 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
465 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100466#endif
467
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100468 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100469 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
470
Neil Armstrongac014ca2022-02-24 15:27:54 +0100471 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100472 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100473 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100474 key.pk_ctx = ctx;
475 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
476 if( key_len <= 0 )
477 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
478
479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100480 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100481 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
482
483 status = psa_import_key( &attributes,
484 buf + sizeof( buf ) - key_len, key_len,
485 &key_id );
486 if( status != PSA_SUCCESS )
487 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100488 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100489 goto cleanup;
490 }
491
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100492 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
493 input, ilen,
494 NULL, 0,
495 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100496 if( status != PSA_SUCCESS )
497 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100498 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100499 goto cleanup;
500 }
501
502 ret = 0;
503
504cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100505 status = psa_destroy_key( key_id );
506 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100507 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100508
Neil Armstrong96a16a42022-02-10 10:40:11 +0100509 return( ret );
510}
511#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200512static int rsa_encrypt_wrap( void *ctx,
513 const unsigned char *input, size_t ilen,
514 unsigned char *output, size_t *olen, size_t osize,
515 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
516{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100517 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
518 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200519
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100520 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100522
Thomas Daubney21772772021-05-13 17:30:32 +0100523 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100524 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200525}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100526#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200527
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200528static int rsa_check_pair_wrap( const void *pub, const void *prv,
529 int (*f_rng)(void *, unsigned char *, size_t),
530 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100531{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200532 (void) f_rng;
533 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
535 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100536}
537
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200538static void *rsa_alloc_wrap( void )
539{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200540 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200541
542 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200543 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200544
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200545 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200546}
547
548static void rsa_free_wrap( void *ctx )
549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
551 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200552}
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200555{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200556#if defined(MBEDTLS_RSA_ALT)
557 /* Not supported */
558 (void) ctx;
559 (void) items;
560#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200562 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200564
565 items++;
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200568 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200570#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573const mbedtls_pk_info_t mbedtls_rsa_info = {
574 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200575 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200576 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200577 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200578 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200579 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200580#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200581 NULL,
582 NULL,
583#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200584 rsa_decrypt_wrap,
585 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100586 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200587 rsa_alloc_wrap,
588 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200589#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200590 NULL,
591 NULL,
592#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200593 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200594};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200598/*
599 * Generic EC key
600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( type == MBEDTLS_PK_ECKEY ||
604 type == MBEDTLS_PK_ECKEY_DH ||
605 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200606}
607
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200608static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200611}
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200614/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200616 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200617 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200620 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200621 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200622 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200625 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200626 const unsigned char *sig, size_t sig_len )
627{
Janos Follath24eed8d2019-11-22 13:21:35 +0000628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200634 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200637
638 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200639}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200642 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200643 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200644 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
645{
Janos Follath24eed8d2019-11-22 13:21:35 +0000646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200652 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
653 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200654 f_rng, p_rng );
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200657
658 return( ret );
659}
660
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200661#if defined(MBEDTLS_ECP_RESTARTABLE)
662/* Forward declarations */
663static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
664 const unsigned char *hash, size_t hash_len,
665 const unsigned char *sig, size_t sig_len,
666 void *rs_ctx );
667
668static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
669 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200670 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200671 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
672 void *rs_ctx );
673
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200674/*
675 * Restart context for ECDSA operations with ECKEY context
676 *
677 * We need to store an actual ECDSA context, as we need to pass the same to
678 * the underlying ecdsa function, so we can't create it on the fly every time.
679 */
680typedef struct
681{
682 mbedtls_ecdsa_restart_ctx ecdsa_rs;
683 mbedtls_ecdsa_context ecdsa_ctx;
684} eckey_restart_ctx;
685
686static void *eckey_rs_alloc( void )
687{
688 eckey_restart_ctx *rs_ctx;
689
690 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
691
692 if( ctx != NULL )
693 {
694 rs_ctx = ctx;
695 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
696 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
697 }
698
699 return( ctx );
700}
701
702static void eckey_rs_free( void *ctx )
703{
704 eckey_restart_ctx *rs_ctx;
705
706 if( ctx == NULL)
707 return;
708
709 rs_ctx = ctx;
710 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
711 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
712
713 mbedtls_free( ctx );
714}
715
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200716static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
717 const unsigned char *hash, size_t hash_len,
718 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200719 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200720{
Janos Follath24eed8d2019-11-22 13:21:35 +0000721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200722 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200723
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200724 /* Should never happen */
725 if( rs == NULL )
726 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200727
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200728 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200729 if( rs->ecdsa_ctx.grp.pbits == 0 )
730 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200731
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200732 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
733 md_alg, hash, hash_len,
734 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200735
736cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200737 return( ret );
738}
739
740static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
741 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200742 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200743 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200744 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200745{
Janos Follath24eed8d2019-11-22 13:21:35 +0000746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200747 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200748
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200749 /* Should never happen */
750 if( rs == NULL )
751 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200752
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200753 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200754 if( rs->ecdsa_ctx.grp.pbits == 0 )
755 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200756
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200757 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200758 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200759 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200760
761cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200762 return( ret );
763}
764#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200766
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200767static int eckey_check_pair( const void *pub, const void *prv,
768 int (*f_rng)(void *, unsigned char *, size_t),
769 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200772 (const mbedtls_ecp_keypair *) prv,
773 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100774}
775
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200776static void *eckey_alloc_wrap( void )
777{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200778 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200779
780 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200782
783 return( ctx );
784}
785
786static void eckey_free_wrap( void *ctx )
787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
789 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200790}
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200793{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200795 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200797}
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799const mbedtls_pk_info_t mbedtls_eckey_info = {
800 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200801 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200802 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200803 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200805 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200806 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200807#if defined(MBEDTLS_ECP_RESTARTABLE)
808 eckey_verify_rs_wrap,
809 eckey_sign_rs_wrap,
810#endif
811#else /* MBEDTLS_ECDSA_C */
812 NULL,
813 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200814#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200815 NULL,
816 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100817 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200818 eckey_alloc_wrap,
819 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200820#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200821 eckey_rs_alloc,
822 eckey_rs_free,
823#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200824 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200825};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200826
827/*
Paul Bakker75342a62014-04-08 17:35:40 +0200828 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200831{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 return( type == MBEDTLS_PK_ECKEY ||
833 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200834}
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836const mbedtls_pk_info_t mbedtls_eckeydh_info = {
837 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200838 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200839 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200840 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200841 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200842 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200843#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200844 NULL,
845 NULL,
846#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200847 NULL,
848 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100849 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200850 eckey_alloc_wrap, /* Same underlying key structure */
851 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200852#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200853 NULL,
854 NULL,
855#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200856 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200857};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_ECDSA_C)
861static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200864}
865
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400866#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400867/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500868 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
869 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500870 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500871static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
872 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500873{
Janos Follath24eed8d2019-11-22 13:21:35 +0000874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500875 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500876
Andrzej Kurek9241d182018-11-20 05:04:35 -0500877 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500878 MBEDTLS_ASN1_INTEGER ) ) != 0 )
879 {
880 return( ret );
881 }
882
Andrzej Kurek9241d182018-11-20 05:04:35 -0500883 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500884 {
885 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500886 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500887 }
888
Andrzej Kurek9241d182018-11-20 05:04:35 -0500889 if( unpadded_len > to_len || unpadded_len == 0 )
890 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500891
Andrzej Kurek9241d182018-11-20 05:04:35 -0500892 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500893 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500894 memcpy( to + padding_len, *from, unpadded_len );
895 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500896
Andrzej Kurek9241d182018-11-20 05:04:35 -0500897 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500898}
899
900/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400901 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500902 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500903 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400904 */
905static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500906 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400907{
Janos Follath24eed8d2019-11-22 13:21:35 +0000908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500909 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500910
Andrzej Kurek9241d182018-11-20 05:04:35 -0500911 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500912 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500913 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400914
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500915 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500916 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
917 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500918 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500919 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
920 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500921
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400922 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400923}
924
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100925static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400926 const unsigned char *hash, size_t hash_len,
927 const unsigned char *sig, size_t sig_len )
928{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100929 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100932 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200933 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400934 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400935 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100936 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000937 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400938 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700939 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100940 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100941 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100942 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
943 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700944 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400945
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500946 if( curve == 0 )
947 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
948
Hanno Beckerccf574e2019-01-29 08:26:15 +0000949 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500950 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400951 key.pk_info = &pk_info;
952 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000953 p = buf + sizeof( buf );
954 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400955 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500956 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400957
Gilles Peskined2d45c12019-05-27 14:53:13 +0200958 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200960 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500961
Gilles Peskined2d45c12019-05-27 14:53:13 +0200962 status = psa_import_key( &attributes,
963 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200964 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200965 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400966 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100967 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400968 goto cleanup;
969 }
970
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500971 /* We don't need the exported key anymore and can
972 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500973 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500974 {
975 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
976 goto cleanup;
977 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500978
Hanno Beckera9851112019-01-25 16:37:10 +0000979 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500980 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500981 signature_part_size ) ) != 0 )
982 {
983 goto cleanup;
984 }
985
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100986 status = psa_verify_hash( key_id, psa_sig_md,
987 hash, hash_len,
988 buf, 2 * signature_part_size );
989 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400990 {
Jerry Yu848ecce2022-03-22 10:58:48 +0800991 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400992 goto cleanup;
993 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500994
995 if( p != sig + sig_len )
996 {
997 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
998 goto cleanup;
999 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001000 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001001
Andrzej Kurekb7b04782018-11-19 17:01:16 -05001002cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +01001003 status = psa_destroy_key( key_id );
1004 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +01001005 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +01001006
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001007 return( ret );
1008}
1009#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001011 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001012 const unsigned char *sig, size_t sig_len )
1013{
Janos Follath24eed8d2019-11-22 13:21:35 +00001014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001015 ((void) md_alg);
1016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001018 hash, hash_len, sig, sig_len );
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1021 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001022
1023 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001024}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001025#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001026
Neil Armstronge9606902022-02-09 14:23:00 +01001027#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001028/*
1029 * Simultaneously convert and move raw MPI from the beginning of a buffer
1030 * to an ASN.1 MPI at the end of the buffer.
1031 * See also mbedtls_asn1_write_mpi().
1032 *
1033 * p: pointer to the end of the output buffer
1034 * start: start of the output buffer, and also of the mpi to write at the end
1035 * n_len: length of the mpi to read from start
1036 */
1037static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1038 size_t n_len )
1039{
1040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1041 size_t len = 0;
1042
1043 if( (size_t)( *p - start ) < n_len )
1044 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1045
1046 len = n_len;
1047 *p -= len;
1048 memmove( *p, start, len );
1049
1050 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1051 * Neither r nor s should be 0, but as a failsafe measure, still detect
1052 * that rather than overflowing the buffer in case of a PSA error. */
1053 while( len > 0 && **p == 0x00 )
1054 {
1055 ++(*p);
1056 --len;
1057 }
1058
1059 /* this is only reached if the signature was invalid */
1060 if( len == 0 )
1061 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
1062
1063 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1064 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
1065 if( **p & 0x80 )
1066 {
1067 if( *p - start < 1 )
1068 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1069
1070 *--(*p) = 0x00;
1071 len += 1;
1072 }
1073
1074 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1075 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1076 MBEDTLS_ASN1_INTEGER ) );
1077
1078 return( (int) len );
1079}
1080
1081/* Transcode signature from PSA format to ASN.1 sequence.
1082 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1083 * MPIs, and in-place.
1084 *
1085 * [in/out] sig: the signature pre- and post-transcoding
1086 * [in/out] sig_len: signature length pre- and post-transcoding
1087 * [int] buf_len: the available size the in/out buffer
1088 */
Neil Armstronge9606902022-02-09 14:23:00 +01001089static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001090 size_t buf_len )
1091{
1092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1093 size_t len = 0;
1094 const size_t rs_len = *sig_len / 2;
1095 unsigned char *p = sig + buf_len;
1096
1097 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1098 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1099
1100 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1101 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1102 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1103
1104 memmove( sig, p, len );
1105 *sig_len = len;
1106
1107 return( 0 );
1108}
Neil Armstronge9606902022-02-09 14:23:00 +01001109
Neil Armstrong17a06552022-03-18 15:27:38 +01001110/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1111 *
1112 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1113 * [in] end: ASN.1 buffer end
1114 * [out] key_len: the ECDSA privateKey length in bytes
1115 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001116static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1117 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001118{
1119 size_t len;
1120 int ret;
1121
Neil Armstrong17a06552022-03-18 15:27:38 +01001122 /*
1123 * RFC 5915, or SEC1 Appendix C.4
1124 *
1125 * ECPrivateKey ::= SEQUENCE {
1126 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1127 * privateKey OCTET STRING,
1128 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1129 * publicKey [1] BIT STRING OPTIONAL
1130 * }
1131 */
1132
Neil Armstronge9606902022-02-09 14:23:00 +01001133 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001134 MBEDTLS_ASN1_CONSTRUCTED |
1135 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001136 return( ret );
1137
1138 /* version */
1139 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1140 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1141 return( ret );
1142
1143 *buf += len;
1144
1145 /* privateKey */
1146 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1147 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1148 return( ret );
1149
1150 *key_len = len;
1151
1152 return 0;
1153}
1154
1155static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1156 const unsigned char *hash, size_t hash_len,
1157 unsigned char *sig, size_t sig_size, size_t *sig_len,
1158 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1159{
1160 mbedtls_ecdsa_context *ctx = ctx_arg;
1161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1163 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1164 psa_status_t status;
1165 mbedtls_pk_context key;
1166 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001167 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001168 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001169 psa_algorithm_t psa_sig_md =
1170 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001171 size_t curve_bits;
1172 psa_ecc_family_t curve =
1173 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001174
1175 /* PSA has its own RNG */
1176 ((void) f_rng);
1177 ((void) p_rng);
1178
1179 if( curve == 0 )
1180 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1181
1182 /* mbedtls_pk_write_key_der() expects a full PK context;
1183 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001184 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001185 key.pk_ctx = ctx;
1186 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1187 if( key_len <= 0 )
1188 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1189
1190 p = buf + sizeof( buf ) - key_len;
1191 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1192 if( ret != 0 )
1193 goto cleanup;
1194
1195 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1196 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1197 psa_set_key_algorithm( &attributes, psa_sig_md );
1198
1199 status = psa_import_key( &attributes,
1200 p, key_len,
1201 &key_id );
1202 if( status != PSA_SUCCESS )
1203 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001204 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001205 goto cleanup;
1206 }
1207
Neil Armstronge4edcf72022-03-03 16:46:41 +01001208 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1209 sig, sig_size, sig_len );
1210 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001211 {
Jerry Yu848ecce2022-03-22 10:58:48 +08001212 ret = mbedtls_pk_error_from_psa_ecdsa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001213 goto cleanup;
1214 }
1215
1216 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1217
1218cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001219 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001220 status = psa_destroy_key( key_id );
1221 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001222 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001223
Neil Armstronge9606902022-02-09 14:23:00 +01001224 return( ret );
1225}
1226#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001228 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001229 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001230 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001233 md_alg, hash, hash_len,
1234 sig, sig_size, sig_len,
1235 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001236}
Neil Armstronge9606902022-02-09 14:23:00 +01001237#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001238
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001239#if defined(MBEDTLS_ECP_RESTARTABLE)
1240static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1241 const unsigned char *hash, size_t hash_len,
1242 const unsigned char *sig, size_t sig_len,
1243 void *rs_ctx )
1244{
Janos Follath24eed8d2019-11-22 13:21:35 +00001245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001246 ((void) md_alg);
1247
1248 ret = mbedtls_ecdsa_read_signature_restartable(
1249 (mbedtls_ecdsa_context *) ctx,
1250 hash, hash_len, sig, sig_len,
1251 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1252
1253 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1254 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1255
1256 return( ret );
1257}
1258
1259static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1260 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001261 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001262 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1263 void *rs_ctx )
1264{
1265 return( mbedtls_ecdsa_write_signature_restartable(
1266 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001267 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001268 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1269
1270}
1271#endif /* MBEDTLS_ECP_RESTARTABLE */
1272
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001273static void *ecdsa_alloc_wrap( void )
1274{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001275 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001276
1277 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001279
1280 return( ctx );
1281}
1282
1283static void ecdsa_free_wrap( void *ctx )
1284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1286 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001287}
1288
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001289#if defined(MBEDTLS_ECP_RESTARTABLE)
1290static void *ecdsa_rs_alloc( void )
1291{
1292 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1293
1294 if( ctx != NULL )
1295 mbedtls_ecdsa_restart_init( ctx );
1296
1297 return( ctx );
1298}
1299
1300static void ecdsa_rs_free( void *ctx )
1301{
1302 mbedtls_ecdsa_restart_free( ctx );
1303 mbedtls_free( ctx );
1304}
1305#endif /* MBEDTLS_ECP_RESTARTABLE */
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1308 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001309 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001310 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001311 ecdsa_can_do,
1312 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001313 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001314#if defined(MBEDTLS_ECP_RESTARTABLE)
1315 ecdsa_verify_rs_wrap,
1316 ecdsa_sign_rs_wrap,
1317#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001318 NULL,
1319 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001320 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001321 ecdsa_alloc_wrap,
1322 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001323#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001324 ecdsa_rs_alloc,
1325 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001326#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001327 eckey_debug, /* Compatible key structures */
1328};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001332/*
1333 * Support for alternative RSA-private implementations
1334 */
1335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001339}
1340
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001341static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001344
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001345 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001346}
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001350 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001351 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1352{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001354
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001355#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001356 if( UINT_MAX < hash_len )
1357 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001358#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001359
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001360 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001361 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1362 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001363 if( *sig_len > sig_size )
1364 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001365
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001366 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001367 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001368}
1369
1370static int rsa_alt_decrypt_wrap( void *ctx,
1371 const unsigned char *input, size_t ilen,
1372 unsigned char *output, size_t *olen, size_t osize,
1373 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1374{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001376
1377 ((void) f_rng);
1378 ((void) p_rng);
1379
1380 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001382
1383 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001384 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001385}
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001388static int rsa_alt_check_pair( const void *pub, const void *prv,
1389 int (*f_rng)(void *, unsigned char *, size_t),
1390 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001393 unsigned char hash[32];
1394 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001396
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001397 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001399
1400 memset( hash, 0x2a, sizeof( hash ) );
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001403 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001404 sig, sizeof( sig ), &sig_len,
1405 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001406 {
1407 return( ret );
1408 }
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001411 hash, sizeof( hash ), sig, sig_len ) != 0 )
1412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001414 }
1415
1416 return( 0 );
1417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001419
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001420static void *rsa_alt_alloc_wrap( void )
1421{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001422 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001423
1424 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001426
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001427 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001428}
1429
1430static void rsa_alt_free_wrap( void *ctx )
1431{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001432 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001434}
1435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1437 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001438 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001439 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001440 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001441 NULL,
1442 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001443#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001444 NULL,
1445 NULL,
1446#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001447 rsa_alt_decrypt_wrap,
1448 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001450 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001451#else
1452 NULL,
1453#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001454 rsa_alt_alloc_wrap,
1455 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001456#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001457 NULL,
1458 NULL,
1459#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001460 NULL,
1461};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001464
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001465#if defined(MBEDTLS_USE_PSA_CRYPTO)
1466
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001467static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001468{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001469 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001470
1471 /* no _init() function to call, an calloc() already zeroized */
1472
1473 return( ctx );
1474}
1475
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001476static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001477{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001478 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001479 mbedtls_free( ctx );
1480}
1481
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001482static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001483{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001484 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001485 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001487
Gilles Peskined2d45c12019-05-27 14:53:13 +02001488 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001489 return( 0 );
1490
Gilles Peskined2d45c12019-05-27 14:53:13 +02001491 bits = psa_get_key_bits( &attributes );
1492 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001493 return( bits );
1494}
1495
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001496static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001497{
1498 /* For now opaque PSA keys can only wrap ECC keypairs,
1499 * as checked by setup_psa().
1500 * Also, ECKEY_DH does not really make sense with the current API. */
1501 return( type == MBEDTLS_PK_ECKEY ||
1502 type == MBEDTLS_PK_ECDSA );
1503}
1504
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001505static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1506 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001507 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001508 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1509{
John Durkopf35069a2020-08-17 22:05:14 -07001510#if !defined(MBEDTLS_ECDSA_C)
1511 ((void) ctx);
1512 ((void) md_alg);
1513 ((void) hash);
1514 ((void) hash_len);
1515 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001516 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001517 ((void) sig_len);
1518 ((void) f_rng);
1519 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001520 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1521#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001522 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001523 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001524 psa_status_t status;
1525
1526 /* PSA has its own RNG */
1527 (void) f_rng;
1528 (void) p_rng;
1529
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001530 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001531 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001532 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001533 if( status != PSA_SUCCESS )
Jerry Yu848ecce2022-03-22 10:58:48 +08001534 return( mbedtls_pk_error_from_psa_ecdsa( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001535
1536 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001537 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001538#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001539}
1540
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001541const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1542 MBEDTLS_PK_OPAQUE,
1543 "Opaque",
1544 pk_opaque_get_bitlen,
1545 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001546 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001547 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001548#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1549 NULL, /* restartable verify - not relevant */
1550 NULL, /* restartable sign - not relevant */
1551#endif
1552 NULL, /* decrypt - will be done later */
1553 NULL, /* encrypt - will be done later */
1554 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001555 pk_opaque_alloc_wrap,
1556 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001557#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1558 NULL, /* restart alloc - not relevant */
1559 NULL, /* restart free - not relevant */
1560#endif
1561 NULL, /* debug - could be done later, or even left NULL */
1562};
1563
1564#endif /* MBEDTLS_USE_PSA_CRYPTO */
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#endif /* MBEDTLS_PK_C */