blob: b910242441dcf7fe909e8cf0b426461f2d5bd9a2 [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
Neil Armstrong19915c22022-03-01 15:21:02 +010068#if defined(MBEDTLS_USE_PSA_CRYPTO)
69int 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
103#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
104int mbedtls_pk_error_from_psa_ecdca( psa_status_t status )
105{
106 switch( status )
107 {
108 case PSA_ERROR_NOT_PERMITTED:
109 case PSA_ERROR_INVALID_ARGUMENT:
110 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
111 case PSA_ERROR_INVALID_HANDLE:
112 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
113 case PSA_ERROR_BUFFER_TOO_SMALL:
114 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
115 case PSA_ERROR_INSUFFICIENT_ENTROPY:
116 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
117 case PSA_ERROR_INVALID_SIGNATURE:
118 return( MBEDTLS_ERR_ECP_VERIFY_FAILED );
119 default:
120 return( mbedtls_pk_error_from_psa( status ) );
121 }
122}
123#endif
124
125#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
126int mbedtls_pk_error_from_psa_rsa( psa_status_t status )
127{
128 switch( status )
129 {
130 case PSA_ERROR_NOT_PERMITTED:
131 case PSA_ERROR_INVALID_ARGUMENT:
132 case PSA_ERROR_INVALID_HANDLE:
133 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
134 case PSA_ERROR_BUFFER_TOO_SMALL:
135 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
136 case PSA_ERROR_INSUFFICIENT_ENTROPY:
137 return( MBEDTLS_ERR_RSA_RNG_FAILED );
138 case PSA_ERROR_INVALID_SIGNATURE:
139 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
140 case PSA_ERROR_INVALID_PADDING:
141 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
142 default:
143 return( mbedtls_pk_error_from_psa( status ) );
144 }
145}
146#endif
147#endif
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_RSA_C)
150static int rsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 return( type == MBEDTLS_PK_RSA ||
153 type == MBEDTLS_PK_RSASSA_PSS );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200154}
155
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200156static size_t rsa_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200157{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100158 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
159 return( 8 * mbedtls_rsa_get_len( rsa ) );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200160}
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200163 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200164 const unsigned char *sig, size_t sig_len )
165{
Janos Follath24eed8d2019-11-22 13:21:35 +0000166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100167 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
168 size_t rsa_len = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200169
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100170#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000171 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
172 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100173#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000174
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100175 if( sig_len < rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200177
Thomas Daubney68d9cbc2021-05-18 18:45:09 +0100178 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, md_alg,
179 (unsigned int) hash_len,
Thomas Daubney613d1a42021-05-18 19:34:03 +0100180 hash, sig ) ) != 0 )
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200181 return( ret );
182
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200183 /* The buffer contains a valid signature followed by extra data.
184 * We have a special error code for that so that so that callers can
185 * use mbedtls_pk_verify() to check "Does the buffer start with a
186 * valid signature?" and not just "Does the buffer contain a valid
187 * signature?". */
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100188 if( sig_len > rsa_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200190
191 return( 0 );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200192}
193
Neil Armstrong98545682022-02-22 16:12:51 +0100194#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu1d172a32022-03-12 19:12:05 +0800195int mbedtls_pk_psa_sign_ext( psa_algorithm_t psa_alg_md, void *pk_ctx,
196 const unsigned char *hash, size_t hash_len,
197 unsigned char *sig, size_t sig_size,
198 size_t *sig_len )
Neil Armstrong98545682022-02-22 16:12:51 +0100199{
Jerry Yu1d172a32022-03-12 19:12:05 +0800200 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) pk_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
203 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
204 psa_status_t status;
205 mbedtls_pk_context key;
206 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100207 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100208 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100209
210 *sig_len = mbedtls_rsa_get_len( rsa );
211 if( sig_size < *sig_len )
212 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
213
Neil Armstronge4f28682022-02-24 15:41:39 +0100214 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100215 * re-construct one to make it happy */
216 key.pk_info = &pk_info;
Jerry Yu1d172a32022-03-12 19:12:05 +0800217 key.pk_ctx = pk_ctx;
Neil Armstrong98545682022-02-22 16:12:51 +0100218 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
219 if( key_len <= 0 )
220 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong98545682022-02-22 16:12:51 +0100221 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
222 psa_set_key_algorithm( &attributes, psa_alg_md );
223 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
224
225 status = psa_import_key( &attributes,
226 buf + sizeof( buf ) - key_len, key_len,
227 &key_id );
228 if( status != PSA_SUCCESS )
229 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100230 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100231 goto cleanup;
232 }
Neil Armstrong98545682022-02-22 16:12:51 +0100233 status = psa_sign_hash( key_id, psa_alg_md, hash, hash_len,
234 sig, sig_size, sig_len );
235 if( status != PSA_SUCCESS )
236 {
Neil Armstrongdb69c522022-03-03 16:41:23 +0100237 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100238 goto cleanup;
239 }
240
241 ret = 0;
242
243cleanup:
Neil Armstrong48a98332022-02-24 16:56:46 +0100244 status = psa_destroy_key( key_id );
245 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrongdb69c522022-03-03 16:41:23 +0100246 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong98545682022-02-22 16:12:51 +0100247 return( ret );
248}
Jerry Yu1d172a32022-03-12 19:12:05 +0800249#endif /* MBEDTLS_USE_PSA_CRYPTO */
250
251#if defined(MBEDTLS_USE_PSA_CRYPTO)
252static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
253 const unsigned char *hash, size_t hash_len,
254 unsigned char *sig, size_t sig_size, size_t *sig_len,
255 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
256{
257
258 ((void) f_rng);
259 ((void) p_rng);
260 ((void) md_alg);
261
262#if SIZE_MAX > UINT_MAX
263 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
264 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
265#endif /* SIZE_MAX > UINT_MAX */
266
267 return( mbedtls_pk_psa_sign_ext( PSA_ALG_RSA_PKCS1V15_SIGN(
268 mbedtls_psa_translate_md( md_alg ) ),
269 ctx, hash, hash_len,
270 sig, sig_size, sig_len ) );
271}
Neil Armstrong98545682022-02-22 16:12:51 +0100272#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200274 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200275 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200276 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
277{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100278 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
279
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100280#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000281 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
282 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100283#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000284
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100285 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200286 if( sig_size < *sig_len )
287 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200288
Thomas Daubney140184d2021-05-18 16:04:07 +0100289 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
290 md_alg, (unsigned int) hash_len,
291 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200292}
Neil Armstrong98545682022-02-22 16:12:51 +0100293#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200294
Neil Armstrong18f43c72022-02-09 15:32:45 +0100295#if defined(MBEDTLS_USE_PSA_CRYPTO)
296static int rsa_decrypt_wrap( void *ctx,
297 const unsigned char *input, size_t ilen,
298 unsigned char *output, size_t *olen, size_t osize,
299 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
300{
301 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
303 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
304 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
305 psa_status_t status;
306 mbedtls_pk_context key;
307 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100308 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100309
310 ((void) f_rng);
311 ((void) p_rng);
312
313#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong8e805042022-03-16 15:30:31 +0100314 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
315 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
316#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100317
318 if( ilen != mbedtls_rsa_get_len( rsa ) )
319 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
320
321 /* mbedtls_pk_write_key_der() expects a full PK context;
322 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100323 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100324 key.pk_ctx = ctx;
325 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
326 if( key_len <= 0 )
327 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
328
329 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
330 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
Neil Armstrong8e805042022-03-16 15:30:31 +0100331 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100332
333 status = psa_import_key( &attributes,
334 buf + sizeof( buf ) - key_len, key_len,
335 &key_id );
336 if( status != PSA_SUCCESS )
337 {
Neil Armstronge8780492022-03-03 16:54:16 +0100338 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100339 goto cleanup;
340 }
341
Neil Armstrong8e805042022-03-16 15:30:31 +0100342 status = psa_asymmetric_decrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
343 input, ilen,
344 NULL, 0,
345 output, osize, olen );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100346 if( status != PSA_SUCCESS )
347 {
Neil Armstronge8780492022-03-03 16:54:16 +0100348 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong18f43c72022-02-09 15:32:45 +0100349 goto cleanup;
350 }
351
352 ret = 0;
353
354cleanup:
Neil Armstrong169e61a2022-03-14 14:26:49 +0100355 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100356 status = psa_destroy_key( key_id );
357 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge8780492022-03-03 16:54:16 +0100358 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100359
Neil Armstrong18f43c72022-02-09 15:32:45 +0100360 return( ret );
361}
362#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200363static 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{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100368 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
369
370 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200372
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100373 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100374 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200375}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100376#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200377
Neil Armstrong96a16a42022-02-10 10:40:11 +0100378#if defined(MBEDTLS_USE_PSA_CRYPTO)
379static int rsa_encrypt_wrap( void *ctx,
380 const unsigned char *input, size_t ilen,
381 unsigned char *output, size_t *olen, size_t osize,
382 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
383{
384 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
386 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
387 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
388 psa_status_t status;
389 mbedtls_pk_context key;
390 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100391 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100392
393 ((void) f_rng);
394 ((void) p_rng);
395
396#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100397 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
398 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100399#endif
400
Neil Armstrong62e6ea22022-03-18 15:39:44 +0100401 if( mbedtls_rsa_get_len( rsa ) > osize )
Neil Armstrong96a16a42022-02-10 10:40:11 +0100402 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
403
Neil Armstrongac014ca2022-02-24 15:27:54 +0100404 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100405 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100406 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100407 key.pk_ctx = ctx;
408 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
409 if( key_len <= 0 )
410 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
411
412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100413 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100414 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
415
416 status = psa_import_key( &attributes,
417 buf + sizeof( buf ) - key_len, key_len,
418 &key_id );
419 if( status != PSA_SUCCESS )
420 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100421 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100422 goto cleanup;
423 }
424
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100425 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
426 input, ilen,
427 NULL, 0,
428 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100429 if( status != PSA_SUCCESS )
430 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100431 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100432 goto cleanup;
433 }
434
435 ret = 0;
436
437cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100438 status = psa_destroy_key( key_id );
439 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100440 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100441
Neil Armstrong96a16a42022-02-10 10:40:11 +0100442 return( ret );
443}
444#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200445static int rsa_encrypt_wrap( void *ctx,
446 const unsigned char *input, size_t ilen,
447 unsigned char *output, size_t *olen, size_t osize,
448 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
449{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100450 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
451 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200452
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100453 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100455
Thomas Daubney21772772021-05-13 17:30:32 +0100456 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100457 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200458}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100459#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200460
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200461static int rsa_check_pair_wrap( const void *pub, const void *prv,
462 int (*f_rng)(void *, unsigned char *, size_t),
463 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100464{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200465 (void) f_rng;
466 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
468 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100469}
470
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200471static void *rsa_alloc_wrap( void )
472{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200473 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200474
475 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200476 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200477
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200478 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200479}
480
481static void rsa_free_wrap( void *ctx )
482{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
484 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200485}
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200488{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200489#if defined(MBEDTLS_RSA_ALT)
490 /* Not supported */
491 (void) ctx;
492 (void) items;
493#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200495 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200497
498 items++;
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200501 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200503#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200504}
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506const mbedtls_pk_info_t mbedtls_rsa_info = {
507 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200508 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200509 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200510 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200511 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200512 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200513#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200514 NULL,
515 NULL,
516#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200517 rsa_decrypt_wrap,
518 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100519 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200520 rsa_alloc_wrap,
521 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200522#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200523 NULL,
524 NULL,
525#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200526 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200527};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200531/*
532 * Generic EC key
533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200535{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 return( type == MBEDTLS_PK_ECKEY ||
537 type == MBEDTLS_PK_ECKEY_DH ||
538 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200539}
540
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200541static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200542{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200544}
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200547/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200549 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200550 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200553 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200554 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200555 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200558 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200559 const unsigned char *sig, size_t sig_len )
560{
Janos Follath24eed8d2019-11-22 13:21:35 +0000561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200567 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200570
571 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200572}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200575 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200576 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200577 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
578{
Janos Follath24eed8d2019-11-22 13:21:35 +0000579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200585 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
586 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200587 f_rng, p_rng );
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200590
591 return( ret );
592}
593
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200594#if defined(MBEDTLS_ECP_RESTARTABLE)
595/* Forward declarations */
596static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
597 const unsigned char *hash, size_t hash_len,
598 const unsigned char *sig, size_t sig_len,
599 void *rs_ctx );
600
601static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
602 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200603 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200604 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
605 void *rs_ctx );
606
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200607/*
608 * Restart context for ECDSA operations with ECKEY context
609 *
610 * We need to store an actual ECDSA context, as we need to pass the same to
611 * the underlying ecdsa function, so we can't create it on the fly every time.
612 */
613typedef struct
614{
615 mbedtls_ecdsa_restart_ctx ecdsa_rs;
616 mbedtls_ecdsa_context ecdsa_ctx;
617} eckey_restart_ctx;
618
619static void *eckey_rs_alloc( void )
620{
621 eckey_restart_ctx *rs_ctx;
622
623 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
624
625 if( ctx != NULL )
626 {
627 rs_ctx = ctx;
628 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
629 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
630 }
631
632 return( ctx );
633}
634
635static void eckey_rs_free( void *ctx )
636{
637 eckey_restart_ctx *rs_ctx;
638
639 if( ctx == NULL)
640 return;
641
642 rs_ctx = ctx;
643 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
644 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
645
646 mbedtls_free( ctx );
647}
648
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200649static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
650 const unsigned char *hash, size_t hash_len,
651 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200652 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200653{
Janos Follath24eed8d2019-11-22 13:21:35 +0000654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200655 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200656
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200657 /* Should never happen */
658 if( rs == NULL )
659 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200660
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200661 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200662 if( rs->ecdsa_ctx.grp.pbits == 0 )
663 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200664
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200665 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
666 md_alg, hash, hash_len,
667 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200668
669cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200670 return( ret );
671}
672
673static int eckey_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,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200677 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200678{
Janos Follath24eed8d2019-11-22 13:21:35 +0000679 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200680 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200681
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200682 /* Should never happen */
683 if( rs == NULL )
684 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200685
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200686 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200687 if( rs->ecdsa_ctx.grp.pbits == 0 )
688 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200689
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200690 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200691 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200692 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200693
694cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200695 return( ret );
696}
697#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200699
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200700static int eckey_check_pair( const void *pub, const void *prv,
701 int (*f_rng)(void *, unsigned char *, size_t),
702 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200705 (const mbedtls_ecp_keypair *) prv,
706 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100707}
708
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200709static void *eckey_alloc_wrap( void )
710{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200711 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200712
713 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200715
716 return( ctx );
717}
718
719static void eckey_free_wrap( void *ctx )
720{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
722 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200723}
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200728 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200730}
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732const mbedtls_pk_info_t mbedtls_eckey_info = {
733 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200734 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200735 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200736 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200738 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200739 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200740#if defined(MBEDTLS_ECP_RESTARTABLE)
741 eckey_verify_rs_wrap,
742 eckey_sign_rs_wrap,
743#endif
744#else /* MBEDTLS_ECDSA_C */
745 NULL,
746 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200747#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200748 NULL,
749 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100750 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200751 eckey_alloc_wrap,
752 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200753#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200754 eckey_rs_alloc,
755 eckey_rs_free,
756#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200757 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200758};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200759
760/*
Paul Bakker75342a62014-04-08 17:35:40 +0200761 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( type == MBEDTLS_PK_ECKEY ||
766 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200767}
768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769const mbedtls_pk_info_t mbedtls_eckeydh_info = {
770 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200771 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200772 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200773 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200774 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200775 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200776#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200777 NULL,
778 NULL,
779#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200780 NULL,
781 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100782 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200783 eckey_alloc_wrap, /* Same underlying key structure */
784 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200785#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200786 NULL,
787 NULL,
788#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200789 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200790};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_ECDSA_C)
794static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200795{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200797}
798
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400799#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400800/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500801 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
802 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500803 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500804static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
805 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500806{
Janos Follath24eed8d2019-11-22 13:21:35 +0000807 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500808 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500809
Andrzej Kurek9241d182018-11-20 05:04:35 -0500810 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500811 MBEDTLS_ASN1_INTEGER ) ) != 0 )
812 {
813 return( ret );
814 }
815
Andrzej Kurek9241d182018-11-20 05:04:35 -0500816 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500817 {
818 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500819 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500820 }
821
Andrzej Kurek9241d182018-11-20 05:04:35 -0500822 if( unpadded_len > to_len || unpadded_len == 0 )
823 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500824
Andrzej Kurek9241d182018-11-20 05:04:35 -0500825 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500826 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500827 memcpy( to + padding_len, *from, unpadded_len );
828 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500829
Andrzej Kurek9241d182018-11-20 05:04:35 -0500830 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500831}
832
833/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400834 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500835 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500836 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400837 */
838static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500839 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400840{
Janos Follath24eed8d2019-11-22 13:21:35 +0000841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500842 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500843
Andrzej Kurek9241d182018-11-20 05:04:35 -0500844 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500845 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500846 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400847
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500848 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500849 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
850 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500851 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500852 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
853 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500854
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400855 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400856}
857
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100858static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400859 const unsigned char *hash, size_t hash_len,
860 const unsigned char *sig, size_t sig_len )
861{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100862 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100865 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200866 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400867 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400868 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100869 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000870 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400871 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700872 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100873 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100874 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100875 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
876 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700877 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400878
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500879 if( curve == 0 )
880 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
881
Hanno Beckerccf574e2019-01-29 08:26:15 +0000882 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500883 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400884 key.pk_info = &pk_info;
885 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000886 p = buf + sizeof( buf );
887 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400888 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500889 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400890
Gilles Peskined2d45c12019-05-27 14:53:13 +0200891 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100892 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200893 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500894
Gilles Peskined2d45c12019-05-27 14:53:13 +0200895 status = psa_import_key( &attributes,
896 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200897 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200898 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400899 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100900 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400901 goto cleanup;
902 }
903
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500904 /* We don't need the exported key anymore and can
905 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500906 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500907 {
908 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
909 goto cleanup;
910 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500911
Hanno Beckera9851112019-01-25 16:37:10 +0000912 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500913 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500914 signature_part_size ) ) != 0 )
915 {
916 goto cleanup;
917 }
918
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100919 status = psa_verify_hash( key_id, psa_sig_md,
920 hash, hash_len,
921 buf, 2 * signature_part_size );
922 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400923 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100924 ret = mbedtls_pk_error_from_psa_ecdca( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400925 goto cleanup;
926 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500927
928 if( p != sig + sig_len )
929 {
930 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
931 goto cleanup;
932 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400933 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400934
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500935cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +0100936 status = psa_destroy_key( key_id );
937 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100938 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +0100939
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400940 return( ret );
941}
942#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200944 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200945 const unsigned char *sig, size_t sig_len )
946{
Janos Follath24eed8d2019-11-22 13:21:35 +0000947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200948 ((void) md_alg);
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200951 hash, hash_len, sig, sig_len );
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
954 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200955
956 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200957}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400958#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200959
Neil Armstronge9606902022-02-09 14:23:00 +0100960#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100961/*
962 * Simultaneously convert and move raw MPI from the beginning of a buffer
963 * to an ASN.1 MPI at the end of the buffer.
964 * See also mbedtls_asn1_write_mpi().
965 *
966 * p: pointer to the end of the output buffer
967 * start: start of the output buffer, and also of the mpi to write at the end
968 * n_len: length of the mpi to read from start
969 */
970static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
971 size_t n_len )
972{
973 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
974 size_t len = 0;
975
976 if( (size_t)( *p - start ) < n_len )
977 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
978
979 len = n_len;
980 *p -= len;
981 memmove( *p, start, len );
982
983 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
984 * Neither r nor s should be 0, but as a failsafe measure, still detect
985 * that rather than overflowing the buffer in case of a PSA error. */
986 while( len > 0 && **p == 0x00 )
987 {
988 ++(*p);
989 --len;
990 }
991
992 /* this is only reached if the signature was invalid */
993 if( len == 0 )
994 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
995
996 /* if the msb is 1, ASN.1 requires that we prepend a 0.
997 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
998 if( **p & 0x80 )
999 {
1000 if( *p - start < 1 )
1001 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1002
1003 *--(*p) = 0x00;
1004 len += 1;
1005 }
1006
1007 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1008 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1009 MBEDTLS_ASN1_INTEGER ) );
1010
1011 return( (int) len );
1012}
1013
1014/* Transcode signature from PSA format to ASN.1 sequence.
1015 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1016 * MPIs, and in-place.
1017 *
1018 * [in/out] sig: the signature pre- and post-transcoding
1019 * [in/out] sig_len: signature length pre- and post-transcoding
1020 * [int] buf_len: the available size the in/out buffer
1021 */
Neil Armstronge9606902022-02-09 14:23:00 +01001022static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
Neil Armstrong15021652022-03-01 10:14:17 +01001023 size_t buf_len )
1024{
1025 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1026 size_t len = 0;
1027 const size_t rs_len = *sig_len / 2;
1028 unsigned char *p = sig + buf_len;
1029
1030 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1031 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1032
1033 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1034 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1035 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1036
1037 memmove( sig, p, len );
1038 *sig_len = len;
1039
1040 return( 0 );
1041}
Neil Armstronge9606902022-02-09 14:23:00 +01001042
Neil Armstrong17a06552022-03-18 15:27:38 +01001043/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1044 *
1045 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1046 * [in] end: ASN.1 buffer end
1047 * [out] key_len: the ECDSA privateKey length in bytes
1048 */
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001049static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end,
1050 size_t *key_len )
Neil Armstronge9606902022-02-09 14:23:00 +01001051{
1052 size_t len;
1053 int ret;
1054
Neil Armstrong17a06552022-03-18 15:27:38 +01001055 /*
1056 * RFC 5915, or SEC1 Appendix C.4
1057 *
1058 * ECPrivateKey ::= SEQUENCE {
1059 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1060 * privateKey OCTET STRING,
1061 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1062 * publicKey [1] BIT STRING OPTIONAL
1063 * }
1064 */
1065
Neil Armstronge9606902022-02-09 14:23:00 +01001066 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001067 MBEDTLS_ASN1_CONSTRUCTED |
1068 MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Neil Armstronge9606902022-02-09 14:23:00 +01001069 return( ret );
1070
1071 /* version */
1072 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1073 MBEDTLS_ASN1_INTEGER ) ) != 0 )
1074 return( ret );
1075
1076 *buf += len;
1077
1078 /* privateKey */
1079 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
1080 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1081 return( ret );
1082
1083 *key_len = len;
1084
1085 return 0;
1086}
1087
1088static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
1089 const unsigned char *hash, size_t hash_len,
1090 unsigned char *sig, size_t sig_size, size_t *sig_len,
1091 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1092{
1093 mbedtls_ecdsa_context *ctx = ctx_arg;
1094 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1096 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1097 psa_status_t status;
1098 mbedtls_pk_context key;
1099 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001100 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001101 unsigned char *p;
Neil Armstrongedcc73c2022-03-03 12:34:14 +01001102 psa_algorithm_t psa_sig_md =
1103 PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +01001104 size_t curve_bits;
1105 psa_ecc_family_t curve =
1106 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
Neil Armstronge9606902022-02-09 14:23:00 +01001107
1108 /* PSA has its own RNG */
1109 ((void) f_rng);
1110 ((void) p_rng);
1111
1112 if( curve == 0 )
1113 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1114
1115 /* mbedtls_pk_write_key_der() expects a full PK context;
1116 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001117 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001118 key.pk_ctx = ctx;
1119 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
1120 if( key_len <= 0 )
1121 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1122
1123 p = buf + sizeof( buf ) - key_len;
1124 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
1125 if( ret != 0 )
1126 goto cleanup;
1127
1128 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
1129 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
1130 psa_set_key_algorithm( &attributes, psa_sig_md );
1131
1132 status = psa_import_key( &attributes,
1133 p, key_len,
1134 &key_id );
1135 if( status != PSA_SUCCESS )
1136 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001137 ret = mbedtls_pk_error_from_psa( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001138 goto cleanup;
1139 }
1140
Neil Armstronge4edcf72022-03-03 16:46:41 +01001141 status = psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
1142 sig, sig_size, sig_len );
1143 if( status != PSA_SUCCESS )
Neil Armstronge9606902022-02-09 14:23:00 +01001144 {
Neil Armstronge4edcf72022-03-03 16:46:41 +01001145 ret = mbedtls_pk_error_from_psa_ecdca( status );
Neil Armstronge9606902022-02-09 14:23:00 +01001146 goto cleanup;
1147 }
1148
1149 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
1150
1151cleanup:
Neil Armstrong3aca61f2022-03-14 14:24:48 +01001152 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001153 status = psa_destroy_key( key_id );
1154 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstronge4edcf72022-03-03 16:46:41 +01001155 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001156
Neil Armstronge9606902022-02-09 14:23:00 +01001157 return( ret );
1158}
1159#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001161 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001162 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001163 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1164{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001166 md_alg, hash, hash_len,
1167 sig, sig_size, sig_len,
1168 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001169}
Neil Armstronge9606902022-02-09 14:23:00 +01001170#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001171
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001172#if defined(MBEDTLS_ECP_RESTARTABLE)
1173static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1174 const unsigned char *hash, size_t hash_len,
1175 const unsigned char *sig, size_t sig_len,
1176 void *rs_ctx )
1177{
Janos Follath24eed8d2019-11-22 13:21:35 +00001178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001179 ((void) md_alg);
1180
1181 ret = mbedtls_ecdsa_read_signature_restartable(
1182 (mbedtls_ecdsa_context *) ctx,
1183 hash, hash_len, sig, sig_len,
1184 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
1185
1186 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
1187 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
1188
1189 return( ret );
1190}
1191
1192static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
1193 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +02001194 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001195 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1196 void *rs_ctx )
1197{
1198 return( mbedtls_ecdsa_write_signature_restartable(
1199 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +02001200 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001201 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
1202
1203}
1204#endif /* MBEDTLS_ECP_RESTARTABLE */
1205
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001206static void *ecdsa_alloc_wrap( void )
1207{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001208 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001209
1210 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001212
1213 return( ctx );
1214}
1215
1216static void ecdsa_free_wrap( void *ctx )
1217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
1219 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001220}
1221
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001222#if defined(MBEDTLS_ECP_RESTARTABLE)
1223static void *ecdsa_rs_alloc( void )
1224{
1225 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
1226
1227 if( ctx != NULL )
1228 mbedtls_ecdsa_restart_init( ctx );
1229
1230 return( ctx );
1231}
1232
1233static void ecdsa_rs_free( void *ctx )
1234{
1235 mbedtls_ecdsa_restart_free( ctx );
1236 mbedtls_free( ctx );
1237}
1238#endif /* MBEDTLS_ECP_RESTARTABLE */
1239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1241 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001242 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001243 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001244 ecdsa_can_do,
1245 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001246 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001247#if defined(MBEDTLS_ECP_RESTARTABLE)
1248 ecdsa_verify_rs_wrap,
1249 ecdsa_sign_rs_wrap,
1250#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001251 NULL,
1252 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001253 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001254 ecdsa_alloc_wrap,
1255 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001256#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001257 ecdsa_rs_alloc,
1258 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001259#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001260 eckey_debug, /* Compatible key structures */
1261};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001265/*
1266 * Support for alternative RSA-private implementations
1267 */
1268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001270{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001272}
1273
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001274static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001277
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001278 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001279}
1280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001282 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001283 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001284 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1285{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001287
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001288#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001289 if( UINT_MAX < hash_len )
1290 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001291#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001292
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001293 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +01001294 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
1295 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +02001296 if( *sig_len > sig_size )
1297 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001298
Thomas Daubneyfa1581e2021-05-18 12:38:33 +01001299 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001300 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001301}
1302
1303static int rsa_alt_decrypt_wrap( void *ctx,
1304 const unsigned char *input, size_t ilen,
1305 unsigned char *output, size_t *olen, size_t osize,
1306 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001309
1310 ((void) f_rng);
1311 ((void) p_rng);
1312
1313 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001315
1316 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001317 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001318}
1319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001321static int rsa_alt_check_pair( const void *pub, const void *prv,
1322 int (*f_rng)(void *, unsigned char *, size_t),
1323 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001326 unsigned char hash[32];
1327 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001329
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001330 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001332
1333 memset( hash, 0x2a, sizeof( hash ) );
1334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001336 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001337 sig, sizeof( sig ), &sig_len,
1338 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001339 {
1340 return( ret );
1341 }
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001344 hash, sizeof( hash ), sig, sig_len ) != 0 )
1345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001347 }
1348
1349 return( 0 );
1350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001352
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001353static void *rsa_alt_alloc_wrap( void )
1354{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001355 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356
1357 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001359
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001360 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001361}
1362
1363static void rsa_alt_free_wrap( void *ctx )
1364{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001365 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001367}
1368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1370 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001371 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001372 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001373 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001374 NULL,
1375 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001376#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001377 NULL,
1378 NULL,
1379#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001380 rsa_alt_decrypt_wrap,
1381 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001383 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001384#else
1385 NULL,
1386#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001387 rsa_alt_alloc_wrap,
1388 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001389#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001390 NULL,
1391 NULL,
1392#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001393 NULL,
1394};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001397
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001398#if defined(MBEDTLS_USE_PSA_CRYPTO)
1399
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001400static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001401{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001402 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001403
1404 /* no _init() function to call, an calloc() already zeroized */
1405
1406 return( ctx );
1407}
1408
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001409static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001410{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001411 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001412 mbedtls_free( ctx );
1413}
1414
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001415static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001416{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001417 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001418 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001420
Gilles Peskined2d45c12019-05-27 14:53:13 +02001421 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001422 return( 0 );
1423
Gilles Peskined2d45c12019-05-27 14:53:13 +02001424 bits = psa_get_key_bits( &attributes );
1425 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001426 return( bits );
1427}
1428
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001429static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001430{
1431 /* For now opaque PSA keys can only wrap ECC keypairs,
1432 * as checked by setup_psa().
1433 * Also, ECKEY_DH does not really make sense with the current API. */
1434 return( type == MBEDTLS_PK_ECKEY ||
1435 type == MBEDTLS_PK_ECDSA );
1436}
1437
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001438static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1439 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001440 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001441 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1442{
John Durkopf35069a2020-08-17 22:05:14 -07001443#if !defined(MBEDTLS_ECDSA_C)
1444 ((void) ctx);
1445 ((void) md_alg);
1446 ((void) hash);
1447 ((void) hash_len);
1448 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001449 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001450 ((void) sig_len);
1451 ((void) f_rng);
1452 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001453 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1454#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001455 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001456 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001457 psa_status_t status;
1458
1459 /* PSA has its own RNG */
1460 (void) f_rng;
1461 (void) p_rng;
1462
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001463 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001464 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001465 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001466 if( status != PSA_SUCCESS )
Neil Armstrong19915c22022-03-01 15:21:02 +01001467 return( mbedtls_pk_error_from_psa_ecdca( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001468
1469 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001470 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001471#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001472}
1473
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001474const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1475 MBEDTLS_PK_OPAQUE,
1476 "Opaque",
1477 pk_opaque_get_bitlen,
1478 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001479 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001480 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001481#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1482 NULL, /* restartable verify - not relevant */
1483 NULL, /* restartable sign - not relevant */
1484#endif
1485 NULL, /* decrypt - will be done later */
1486 NULL, /* encrypt - will be done later */
1487 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001488 pk_opaque_alloc_wrap,
1489 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001490#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1491 NULL, /* restart alloc - not relevant */
1492 NULL, /* restart free - not relevant */
1493#endif
1494 NULL, /* debug - could be done later, or even left NULL */
1495};
1496
1497#endif /* MBEDTLS_USE_PSA_CRYPTO */
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_PK_C */