blob: 04db6f8a7f766a3465faf1cda1495a6f974de897 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200195 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200196 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200197 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
198{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100199 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
200
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100201#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000202 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
203 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100204#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000205
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100206 *sig_len = mbedtls_rsa_get_len( rsa );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200207 if( sig_size < *sig_len )
208 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200209
Thomas Daubney140184d2021-05-18 16:04:07 +0100210 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng,
211 md_alg, (unsigned int) hash_len,
212 hash, sig ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200213}
214
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200215static int rsa_decrypt_wrap( void *ctx,
216 const unsigned char *input, size_t ilen,
217 unsigned char *output, size_t *olen, size_t osize,
218 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
219{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100220 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
221
222 if( ilen != mbedtls_rsa_get_len( rsa ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200224
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100225 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
Thomas Daubneyc7feaf32021-05-07 14:02:43 +0100226 olen, input, output, osize ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200227}
228
Neil Armstrong96a16a42022-02-10 10:40:11 +0100229#if defined(MBEDTLS_USE_PSA_CRYPTO)
230static int rsa_encrypt_wrap( void *ctx,
231 const unsigned char *input, size_t ilen,
232 unsigned char *output, size_t *olen, size_t osize,
233 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
234{
235 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
237 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
238 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
239 psa_status_t status;
240 mbedtls_pk_context key;
241 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100242 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100243 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100244
245 ((void) f_rng);
246 ((void) p_rng);
247
248#if !defined(MBEDTLS_RSA_ALT)
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100249 if( rsa->padding != MBEDTLS_RSA_PKCS_V15 )
250 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100251#endif
252
253 *olen = mbedtls_rsa_get_len( rsa );
254
255 if( *olen > osize )
256 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
257
Neil Armstrongac014ca2022-02-24 15:27:54 +0100258 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100259 * re-construct one to make it happy */
260 key.pk_info = &pk_info;
261 key.pk_ctx = ctx;
262 key_len = mbedtls_pk_write_pubkey_der( &key, buf, sizeof( buf ) );
263 if( key_len <= 0 )
264 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
265
266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100267 psa_set_key_algorithm( &attributes, PSA_ALG_RSA_PKCS1V15_CRYPT );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100268 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
269
270 status = psa_import_key( &attributes,
271 buf + sizeof( buf ) - key_len, key_len,
272 &key_id );
273 if( status != PSA_SUCCESS )
274 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100275 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100276 goto cleanup;
277 }
278
Neil Armstrong7b1dc852022-03-16 15:35:41 +0100279 status = psa_asymmetric_encrypt( key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
280 input, ilen,
281 NULL, 0,
282 output, osize, olen );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100283 if( status != PSA_SUCCESS )
284 {
Neil Armstrong3770e242022-03-03 16:37:33 +0100285 ret = mbedtls_pk_error_from_psa_rsa( status );
Neil Armstrong96a16a42022-02-10 10:40:11 +0100286 goto cleanup;
287 }
288
289 ret = 0;
290
291cleanup:
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100292 status = psa_destroy_key( key_id );
293 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100294 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100295
Neil Armstrong96a16a42022-02-10 10:40:11 +0100296 return( ret );
297}
298#else
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200299static int rsa_encrypt_wrap( void *ctx,
300 const unsigned char *input, size_t ilen,
301 unsigned char *output, size_t *olen, size_t osize,
302 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
303{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100304 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
305 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200306
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100307 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100309
Thomas Daubney21772772021-05-13 17:30:32 +0100310 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100311 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200312}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100313#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200314
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200315static int rsa_check_pair_wrap( const void *pub, const void *prv,
316 int (*f_rng)(void *, unsigned char *, size_t),
317 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100318{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200319 (void) f_rng;
320 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
322 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100323}
324
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200325static void *rsa_alloc_wrap( void )
326{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200327 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200328
329 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200330 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200331
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200332 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200333}
334
335static void rsa_free_wrap( void *ctx )
336{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
338 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200339}
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200342{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200343#if defined(MBEDTLS_RSA_ALT)
344 /* Not supported */
345 (void) ctx;
346 (void) items;
347#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200349 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200351
352 items++;
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200355 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200357#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200358}
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360const mbedtls_pk_info_t mbedtls_rsa_info = {
361 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200362 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200363 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200364 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200365 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200366 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200367#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200368 NULL,
369 NULL,
370#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200371 rsa_decrypt_wrap,
372 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100373 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200374 rsa_alloc_wrap,
375 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200376#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200377 NULL,
378 NULL,
379#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200380 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200381};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200385/*
386 * Generic EC key
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( type == MBEDTLS_PK_ECKEY ||
391 type == MBEDTLS_PK_ECKEY_DH ||
392 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200393}
394
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200395static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200398}
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200401/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200403 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200404 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200407 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200408 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200409 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200412 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200413 const unsigned char *sig, size_t sig_len )
414{
Janos Follath24eed8d2019-11-22 13:21:35 +0000415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200421 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200424
425 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200426}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200429 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200430 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200431 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
432{
Janos Follath24eed8d2019-11-22 13:21:35 +0000433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200439 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
440 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200441 f_rng, p_rng );
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200444
445 return( ret );
446}
447
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200448#if defined(MBEDTLS_ECP_RESTARTABLE)
449/* Forward declarations */
450static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
451 const unsigned char *hash, size_t hash_len,
452 const unsigned char *sig, size_t sig_len,
453 void *rs_ctx );
454
455static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
456 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200457 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200458 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
459 void *rs_ctx );
460
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200461/*
462 * Restart context for ECDSA operations with ECKEY context
463 *
464 * We need to store an actual ECDSA context, as we need to pass the same to
465 * the underlying ecdsa function, so we can't create it on the fly every time.
466 */
467typedef struct
468{
469 mbedtls_ecdsa_restart_ctx ecdsa_rs;
470 mbedtls_ecdsa_context ecdsa_ctx;
471} eckey_restart_ctx;
472
473static void *eckey_rs_alloc( void )
474{
475 eckey_restart_ctx *rs_ctx;
476
477 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
478
479 if( ctx != NULL )
480 {
481 rs_ctx = ctx;
482 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
483 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
484 }
485
486 return( ctx );
487}
488
489static void eckey_rs_free( void *ctx )
490{
491 eckey_restart_ctx *rs_ctx;
492
493 if( ctx == NULL)
494 return;
495
496 rs_ctx = ctx;
497 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
498 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
499
500 mbedtls_free( ctx );
501}
502
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200503static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
504 const unsigned char *hash, size_t hash_len,
505 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200506 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200507{
Janos Follath24eed8d2019-11-22 13:21:35 +0000508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200509 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200510
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200511 /* Should never happen */
512 if( rs == NULL )
513 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200514
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200515 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200516 if( rs->ecdsa_ctx.grp.pbits == 0 )
517 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200518
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200519 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
520 md_alg, hash, hash_len,
521 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200522
523cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200524 return( ret );
525}
526
527static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
528 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200529 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200530 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200531 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200532{
Janos Follath24eed8d2019-11-22 13:21:35 +0000533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200534 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200535
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200536 /* Should never happen */
537 if( rs == NULL )
538 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200539
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200540 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200541 if( rs->ecdsa_ctx.grp.pbits == 0 )
542 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200543
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200544 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200545 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200546 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200547
548cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200549 return( ret );
550}
551#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200553
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200554static int eckey_check_pair( const void *pub, const void *prv,
555 int (*f_rng)(void *, unsigned char *, size_t),
556 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200559 (const mbedtls_ecp_keypair *) prv,
560 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100561}
562
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200563static void *eckey_alloc_wrap( void )
564{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200565 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200566
567 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200569
570 return( ctx );
571}
572
573static void eckey_free_wrap( void *ctx )
574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
576 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200577}
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200582 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200584}
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586const mbedtls_pk_info_t mbedtls_eckey_info = {
587 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200588 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200589 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200590 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200592 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200593 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200594#if defined(MBEDTLS_ECP_RESTARTABLE)
595 eckey_verify_rs_wrap,
596 eckey_sign_rs_wrap,
597#endif
598#else /* MBEDTLS_ECDSA_C */
599 NULL,
600 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200601#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200602 NULL,
603 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100604 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200605 eckey_alloc_wrap,
606 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200607#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200608 eckey_rs_alloc,
609 eckey_rs_free,
610#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200611 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200612};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200613
614/*
Paul Bakker75342a62014-04-08 17:35:40 +0200615 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 return( type == MBEDTLS_PK_ECKEY ||
620 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200621}
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623const mbedtls_pk_info_t mbedtls_eckeydh_info = {
624 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200625 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200626 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200627 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200628 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200629 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200630#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200631 NULL,
632 NULL,
633#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200634 NULL,
635 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100636 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200637 eckey_alloc_wrap, /* Same underlying key structure */
638 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200639#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200640 NULL,
641 NULL,
642#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200643 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200644};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#if defined(MBEDTLS_ECDSA_C)
648static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200651}
652
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400653#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400654/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500655 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
656 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500657 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500658static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
659 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500660{
Janos Follath24eed8d2019-11-22 13:21:35 +0000661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500662 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500663
Andrzej Kurek9241d182018-11-20 05:04:35 -0500664 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500665 MBEDTLS_ASN1_INTEGER ) ) != 0 )
666 {
667 return( ret );
668 }
669
Andrzej Kurek9241d182018-11-20 05:04:35 -0500670 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500671 {
672 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500673 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500674 }
675
Andrzej Kurek9241d182018-11-20 05:04:35 -0500676 if( unpadded_len > to_len || unpadded_len == 0 )
677 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500678
Andrzej Kurek9241d182018-11-20 05:04:35 -0500679 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500680 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500681 memcpy( to + padding_len, *from, unpadded_len );
682 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500683
Andrzej Kurek9241d182018-11-20 05:04:35 -0500684 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500685}
686
687/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400688 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500689 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500690 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400691 */
692static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500693 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400694{
Janos Follath24eed8d2019-11-22 13:21:35 +0000695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500696 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500697
Andrzej Kurek9241d182018-11-20 05:04:35 -0500698 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500699 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500700 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400701
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500702 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500703 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
704 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500705 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500706 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
707 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500708
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400709 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400710}
711
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100712static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400713 const unsigned char *hash, size_t hash_len,
714 const unsigned char *sig, size_t sig_len )
715{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100716 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100719 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200720 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400721 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400722 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100723 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000724 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400725 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700726 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100727 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100728 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100729 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
730 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700731 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400732
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500733 if( curve == 0 )
734 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
735
Hanno Beckerccf574e2019-01-29 08:26:15 +0000736 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500737 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400738 key.pk_info = &pk_info;
739 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000740 p = buf + sizeof( buf );
741 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400742 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500743 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400744
Gilles Peskined2d45c12019-05-27 14:53:13 +0200745 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100746 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200747 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500748
Gilles Peskined2d45c12019-05-27 14:53:13 +0200749 status = psa_import_key( &attributes,
750 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200751 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200752 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400753 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100754 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400755 goto cleanup;
756 }
757
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500758 /* We don't need the exported key anymore and can
759 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500760 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500761 {
762 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
763 goto cleanup;
764 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500765
Hanno Beckera9851112019-01-25 16:37:10 +0000766 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500767 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500768 signature_part_size ) ) != 0 )
769 {
770 goto cleanup;
771 }
772
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100773 status = psa_verify_hash( key_id, psa_sig_md,
774 hash, hash_len,
775 buf, 2 * signature_part_size );
776 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400777 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100778 ret = mbedtls_pk_error_from_psa_ecdca( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400779 goto cleanup;
780 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500781
782 if( p != sig + sig_len )
783 {
784 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
785 goto cleanup;
786 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400787 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400788
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500789cleanup:
Neil Armstrong9dccd862022-02-24 15:33:13 +0100790 status = psa_destroy_key( key_id );
791 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong3770e242022-03-03 16:37:33 +0100792 ret = mbedtls_pk_error_from_psa( status );
Neil Armstrong9dccd862022-02-24 15:33:13 +0100793
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400794 return( ret );
795}
796#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200798 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200799 const unsigned char *sig, size_t sig_len )
800{
Janos Follath24eed8d2019-11-22 13:21:35 +0000801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200802 ((void) md_alg);
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200805 hash, hash_len, sig, sig_len );
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
808 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200809
810 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200811}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400812#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200815 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200816 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200817 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200820 md_alg, hash, hash_len,
821 sig, sig_size, sig_len,
822 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200823}
824
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200825#if defined(MBEDTLS_ECP_RESTARTABLE)
826static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
827 const unsigned char *hash, size_t hash_len,
828 const unsigned char *sig, size_t sig_len,
829 void *rs_ctx )
830{
Janos Follath24eed8d2019-11-22 13:21:35 +0000831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200832 ((void) md_alg);
833
834 ret = mbedtls_ecdsa_read_signature_restartable(
835 (mbedtls_ecdsa_context *) ctx,
836 hash, hash_len, sig, sig_len,
837 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
838
839 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
840 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
841
842 return( ret );
843}
844
845static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
846 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +0200847 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200848 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
849 void *rs_ctx )
850{
851 return( mbedtls_ecdsa_write_signature_restartable(
852 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +0200853 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200854 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
855
856}
857#endif /* MBEDTLS_ECP_RESTARTABLE */
858
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200859static void *ecdsa_alloc_wrap( void )
860{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200861 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200862
863 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200865
866 return( ctx );
867}
868
869static void ecdsa_free_wrap( void *ctx )
870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
872 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200873}
874
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200875#if defined(MBEDTLS_ECP_RESTARTABLE)
876static void *ecdsa_rs_alloc( void )
877{
878 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
879
880 if( ctx != NULL )
881 mbedtls_ecdsa_restart_init( ctx );
882
883 return( ctx );
884}
885
886static void ecdsa_rs_free( void *ctx )
887{
888 mbedtls_ecdsa_restart_free( ctx );
889 mbedtls_free( ctx );
890}
891#endif /* MBEDTLS_ECP_RESTARTABLE */
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893const mbedtls_pk_info_t mbedtls_ecdsa_info = {
894 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200895 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200896 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200897 ecdsa_can_do,
898 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200899 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200900#if defined(MBEDTLS_ECP_RESTARTABLE)
901 ecdsa_verify_rs_wrap,
902 ecdsa_sign_rs_wrap,
903#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200904 NULL,
905 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100906 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200907 ecdsa_alloc_wrap,
908 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200909#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200910 ecdsa_rs_alloc,
911 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200912#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200913 eckey_debug, /* Compatible key structures */
914};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200918/*
919 * Support for alternative RSA-private implementations
920 */
921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200925}
926
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200927static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200928{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200930
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200931 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200932}
933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200935 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200936 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200937 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
938{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200940
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100941#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000942 if( UINT_MAX < hash_len )
943 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100944#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000945
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200946 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +0100947 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
948 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200949 if( *sig_len > sig_size )
950 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200951
Thomas Daubneyfa1581e2021-05-18 12:38:33 +0100952 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200953 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200954}
955
956static int rsa_alt_decrypt_wrap( void *ctx,
957 const unsigned char *input, size_t ilen,
958 unsigned char *output, size_t *olen, size_t osize,
959 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200962
963 ((void) f_rng);
964 ((void) p_rng);
965
966 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200968
969 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +0100970 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200971}
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200974static int rsa_alt_check_pair( const void *pub, const void *prv,
975 int (*f_rng)(void *, unsigned char *, size_t),
976 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100977{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100979 unsigned char hash[32];
980 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +0000981 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100982
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200983 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100985
986 memset( hash, 0x2a, sizeof( hash ) );
987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100989 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +0200990 sig, sizeof( sig ), &sig_len,
991 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100992 {
993 return( ret );
994 }
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100997 hash, sizeof( hash ), sig, sig_len ) != 0 )
998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001000 }
1001
1002 return( 0 );
1003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001005
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001006static void *rsa_alt_alloc_wrap( void )
1007{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001008 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001009
1010 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001012
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001013 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001014}
1015
1016static void rsa_alt_free_wrap( void *ctx )
1017{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001018 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001020}
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1023 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001024 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001025 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001026 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001027 NULL,
1028 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001029#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001030 NULL,
1031 NULL,
1032#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001033 rsa_alt_decrypt_wrap,
1034 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001036 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001037#else
1038 NULL,
1039#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001040 rsa_alt_alloc_wrap,
1041 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001042#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001043 NULL,
1044 NULL,
1045#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001046 NULL,
1047};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001050
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001051#if defined(MBEDTLS_USE_PSA_CRYPTO)
1052
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001053static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001054{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001055 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001056
1057 /* no _init() function to call, an calloc() already zeroized */
1058
1059 return( ctx );
1060}
1061
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001062static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001063{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001064 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001065 mbedtls_free( ctx );
1066}
1067
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001068static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001069{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001070 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001071 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001073
Gilles Peskined2d45c12019-05-27 14:53:13 +02001074 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001075 return( 0 );
1076
Gilles Peskined2d45c12019-05-27 14:53:13 +02001077 bits = psa_get_key_bits( &attributes );
1078 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001079 return( bits );
1080}
1081
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001082static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001083{
1084 /* For now opaque PSA keys can only wrap ECC keypairs,
1085 * as checked by setup_psa().
1086 * Also, ECKEY_DH does not really make sense with the current API. */
1087 return( type == MBEDTLS_PK_ECKEY ||
1088 type == MBEDTLS_PK_ECDSA );
1089}
1090
John Durkopf35069a2020-08-17 22:05:14 -07001091#if defined(MBEDTLS_ECDSA_C)
1092
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001093/*
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +01001094 * Simultaneously convert and move raw MPI from the beginning of a buffer
1095 * to an ASN.1 MPI at the end of the buffer.
1096 * See also mbedtls_asn1_write_mpi().
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001097 *
1098 * p: pointer to the end of the output buffer
1099 * start: start of the output buffer, and also of the mpi to write at the end
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +01001100 * n_len: length of the mpi to read from start
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001101 */
1102static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1103 size_t n_len )
1104{
Janos Follath24eed8d2019-11-22 13:21:35 +00001105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001106 size_t len = 0;
1107
1108 if( (size_t)( *p - start ) < n_len )
1109 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1110
1111 len = n_len;
1112 *p -= len;
1113 memmove( *p, start, len );
1114
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001115 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001116 * Neither r nor s should be 0, but as a failsafe measure, still detect
1117 * that rather than overflowing the buffer in case of a PSA error. */
1118 while( len > 0 && **p == 0x00 )
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001119 {
1120 ++(*p);
1121 --len;
1122 }
1123
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001124 /* this is only reached if the signature was invalid */
1125 if( len == 0 )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001126 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001127
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001128 /* if the msb is 1, ASN.1 requires that we prepend a 0.
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001129 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001130 if( **p & 0x80 )
1131 {
1132 if( *p - start < 1 )
1133 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1134
1135 *--(*p) = 0x00;
1136 len += 1;
1137 }
1138
1139 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1140 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1141 MBEDTLS_ASN1_INTEGER ) );
1142
1143 return( (int) len );
1144}
1145
1146/* Transcode signature from PSA format to ASN.1 sequence.
1147 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1148 * MPIs, and in-place.
1149 *
1150 * [in/out] sig: the signature pre- and post-transcoding
1151 * [in/out] sig_len: signature length pre- and post-transcoding
1152 * [int] buf_len: the available size the in/out buffer
1153 */
1154static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
1155 size_t buf_len )
1156{
Janos Follath24eed8d2019-11-22 13:21:35 +00001157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001158 size_t len = 0;
1159 const size_t rs_len = *sig_len / 2;
1160 unsigned char *p = sig + buf_len;
1161
1162 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1163 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1164
1165 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1166 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1167 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1168
1169 memmove( sig, p, len );
1170 *sig_len = len;
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +01001171
1172 return( 0 );
1173}
1174
John Durkopd46ede02020-08-24 09:51:00 -07001175#endif /* MBEDTLS_ECDSA_C */
John Durkopf35069a2020-08-17 22:05:14 -07001176
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001177static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1178 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001179 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001180 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1181{
John Durkopf35069a2020-08-17 22:05:14 -07001182#if !defined(MBEDTLS_ECDSA_C)
1183 ((void) ctx);
1184 ((void) md_alg);
1185 ((void) hash);
1186 ((void) hash_len);
1187 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001188 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001189 ((void) sig_len);
1190 ((void) f_rng);
1191 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001192 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1193#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001194 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001195 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001196 psa_status_t status;
1197
1198 /* PSA has its own RNG */
1199 (void) f_rng;
1200 (void) p_rng;
1201
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001202 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001203 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001204 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001205 if( status != PSA_SUCCESS )
Neil Armstrong19915c22022-03-01 15:21:02 +01001206 return( mbedtls_pk_error_from_psa_ecdca( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001207
1208 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001209 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001210#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001211}
1212
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001213const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1214 MBEDTLS_PK_OPAQUE,
1215 "Opaque",
1216 pk_opaque_get_bitlen,
1217 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001218 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001219 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001220#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1221 NULL, /* restartable verify - not relevant */
1222 NULL, /* restartable sign - not relevant */
1223#endif
1224 NULL, /* decrypt - will be done later */
1225 NULL, /* encrypt - will be done later */
1226 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001227 pk_opaque_alloc_wrap,
1228 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001229#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1230 NULL, /* restart alloc - not relevant */
1231 NULL, /* restart free - not relevant */
1232#endif
1233 NULL, /* debug - could be done later, or even left NULL */
1234};
1235
1236#endif /* MBEDTLS_USE_PSA_CRYPTO */
1237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#endif /* MBEDTLS_PK_C */