blob: 7acaf33ad25ad2e19e91af58a590d4270c40b8c0 [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
229static int rsa_encrypt_wrap( void *ctx,
230 const unsigned char *input, size_t ilen,
231 unsigned char *output, size_t *olen, size_t osize,
232 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
233{
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100234 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
235 *olen = mbedtls_rsa_get_len( rsa );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200236
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100237 if( *olen > osize )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100239
Thomas Daubney21772772021-05-13 17:30:32 +0100240 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng,
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100241 ilen, input, output ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200242}
243
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200244static int rsa_check_pair_wrap( const void *pub, const void *prv,
245 int (*f_rng)(void *, unsigned char *, size_t),
246 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100247{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200248 (void) f_rng;
249 (void) p_rng;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
251 (const mbedtls_rsa_context *) prv ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100252}
253
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200254static void *rsa_alloc_wrap( void )
255{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200256 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200257
258 if( ctx != NULL )
Ronald Cronc1905a12021-06-05 11:11:14 +0200259 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200260
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200261 return( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200262}
263
264static void rsa_free_wrap( void *ctx )
265{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
267 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200268}
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200271{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200272#if defined(MBEDTLS_RSA_ALT)
273 /* Not supported */
274 (void) ctx;
275 (void) items;
276#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200278 items->name = "rsa.N";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200280
281 items++;
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200284 items->name = "rsa.E";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200286#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200287}
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289const mbedtls_pk_info_t mbedtls_rsa_info = {
290 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200291 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200292 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200293 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200294 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200295 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200296#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200297 NULL,
298 NULL,
299#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200300 rsa_decrypt_wrap,
301 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100302 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200303 rsa_alloc_wrap,
304 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200305#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200306 NULL,
307 NULL,
308#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200309 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200310};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200314/*
315 * Generic EC key
316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317static int eckey_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( type == MBEDTLS_PK_ECKEY ||
320 type == MBEDTLS_PK_ECKEY_DH ||
321 type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200322}
323
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200324static size_t eckey_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200325{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200327}
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200330/* Forward declarations */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200332 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200333 const unsigned char *sig, size_t sig_len );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200336 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200337 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200338 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200341 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200342 const unsigned char *sig, size_t sig_len )
343{
Janos Follath24eed8d2019-11-22 13:21:35 +0000344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Manuel Pégourié-Gonnard583b6082013-08-20 16:58:13 +0200350 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200353
354 return( ret );
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200355}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200358 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200359 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200360 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
361{
Janos Follath24eed8d2019-11-22 13:21:35 +0000362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_ecdsa_context ecdsa;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
Gilles Peskinef00f1522021-06-22 00:09:00 +0200368 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len,
369 sig, sig_size, sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200370 f_rng, p_rng );
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200373
374 return( ret );
375}
376
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200377#if defined(MBEDTLS_ECP_RESTARTABLE)
378/* Forward declarations */
379static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
380 const unsigned char *hash, size_t hash_len,
381 const unsigned char *sig, size_t sig_len,
382 void *rs_ctx );
383
384static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
385 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200386 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200387 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
388 void *rs_ctx );
389
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200390/*
391 * Restart context for ECDSA operations with ECKEY context
392 *
393 * We need to store an actual ECDSA context, as we need to pass the same to
394 * the underlying ecdsa function, so we can't create it on the fly every time.
395 */
396typedef struct
397{
398 mbedtls_ecdsa_restart_ctx ecdsa_rs;
399 mbedtls_ecdsa_context ecdsa_ctx;
400} eckey_restart_ctx;
401
402static void *eckey_rs_alloc( void )
403{
404 eckey_restart_ctx *rs_ctx;
405
406 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
407
408 if( ctx != NULL )
409 {
410 rs_ctx = ctx;
411 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
412 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
413 }
414
415 return( ctx );
416}
417
418static void eckey_rs_free( void *ctx )
419{
420 eckey_restart_ctx *rs_ctx;
421
422 if( ctx == NULL)
423 return;
424
425 rs_ctx = ctx;
426 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
427 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
428
429 mbedtls_free( ctx );
430}
431
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200432static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
433 const unsigned char *hash, size_t hash_len,
434 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200435 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200436{
Janos Follath24eed8d2019-11-22 13:21:35 +0000437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200438 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200439
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200440 /* Should never happen */
441 if( rs == NULL )
442 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200443
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200444 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200445 if( rs->ecdsa_ctx.grp.pbits == 0 )
446 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200447
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200448 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
449 md_alg, hash, hash_len,
450 sig, sig_len, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200451
452cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200453 return( ret );
454}
455
456static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
457 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200458 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200459 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200460 void *rs_ctx )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200461{
Janos Follath24eed8d2019-11-22 13:21:35 +0000462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200463 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200464
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200465 /* Should never happen */
466 if( rs == NULL )
467 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200468
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200469 /* set up our own sub-context if needed (that is, on first run) */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200470 if( rs->ecdsa_ctx.grp.pbits == 0 )
471 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200472
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200473 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200474 hash, hash_len, sig, sig_size, sig_len,
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200475 f_rng, p_rng, &rs->ecdsa_rs ) );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200476
477cleanup:
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200478 return( ret );
479}
480#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200482
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200483static int eckey_check_pair( const void *pub, const void *prv,
484 int (*f_rng)(void *, unsigned char *, size_t),
485 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100486{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200488 (const mbedtls_ecp_keypair *) prv,
489 f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100490}
491
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200492static void *eckey_alloc_wrap( void )
493{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200494 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200495
496 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200498
499 return( ctx );
500}
501
502static void eckey_free_wrap( void *ctx )
503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
505 mbedtls_free( ctx );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200506}
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200509{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200511 items->name = "eckey.Q";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200513}
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515const mbedtls_pk_info_t mbedtls_eckey_info = {
516 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200517 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200518 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200519 eckey_can_do,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200521 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200522 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200523#if defined(MBEDTLS_ECP_RESTARTABLE)
524 eckey_verify_rs_wrap,
525 eckey_sign_rs_wrap,
526#endif
527#else /* MBEDTLS_ECDSA_C */
528 NULL,
529 NULL,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200530#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200531 NULL,
532 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100533 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200534 eckey_alloc_wrap,
535 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200536#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200537 eckey_rs_alloc,
538 eckey_rs_free,
539#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200540 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200541};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200542
543/*
Paul Bakker75342a62014-04-08 17:35:40 +0200544 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546static int eckeydh_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( type == MBEDTLS_PK_ECKEY ||
549 type == MBEDTLS_PK_ECKEY_DH );
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200550}
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552const mbedtls_pk_info_t mbedtls_eckeydh_info = {
553 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200554 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200555 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200556 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200557 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200558 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200559#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200560 NULL,
561 NULL,
562#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200563 NULL,
564 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100565 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200566 eckey_alloc_wrap, /* Same underlying key structure */
567 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200568#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200569 NULL,
570 NULL,
571#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200572 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200573};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_ECDSA_C)
577static int ecdsa_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200578{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 return( type == MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200580}
581
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400582#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400583/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500584 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
585 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500586 */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500587static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
588 unsigned char *to, size_t to_len )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500589{
Janos Follath24eed8d2019-11-22 13:21:35 +0000590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500591 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500592
Andrzej Kurek9241d182018-11-20 05:04:35 -0500593 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500594 MBEDTLS_ASN1_INTEGER ) ) != 0 )
595 {
596 return( ret );
597 }
598
Andrzej Kurek9241d182018-11-20 05:04:35 -0500599 while( unpadded_len > 0 && **from == 0x00 )
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500600 {
601 ( *from )++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500602 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500603 }
604
Andrzej Kurek9241d182018-11-20 05:04:35 -0500605 if( unpadded_len > to_len || unpadded_len == 0 )
606 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500607
Andrzej Kurek9241d182018-11-20 05:04:35 -0500608 padding_len = to_len - unpadded_len;
Andrzej Kurek6cb63aa2018-11-20 05:14:46 -0500609 memset( to, 0x00, padding_len );
Andrzej Kurek9241d182018-11-20 05:04:35 -0500610 memcpy( to + padding_len, *from, unpadded_len );
611 ( *from ) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500612
Andrzej Kurek9241d182018-11-20 05:04:35 -0500613 return( 0 );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500614}
615
616/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400617 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500618 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500619 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400620 */
621static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
Andrzej Kurek9241d182018-11-20 05:04:35 -0500622 unsigned char *sig, size_t int_size )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400623{
Janos Follath24eed8d2019-11-22 13:21:35 +0000624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500625 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500626
Andrzej Kurek9241d182018-11-20 05:04:35 -0500627 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500628 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Andrzej Kurek9241d182018-11-20 05:04:35 -0500629 return( ret );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400630
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500631 /* Extract r */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500632 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
633 return( ret );
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500634 /* Extract s */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500635 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
636 return( ret );
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500637
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400638 return( 0 );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400639}
640
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100641static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400642 const unsigned char *hash, size_t hash_len,
643 const unsigned char *sig, size_t sig_len )
644{
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100645 mbedtls_ecdsa_context *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100648 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200649 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400650 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400651 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100652 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000653 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400654 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700655 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100656 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100657 psa_ecc_family_t curve =
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100658 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
659 const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700660 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400661
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500662 if( curve == 0 )
663 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
664
Hanno Beckerccf574e2019-01-29 08:26:15 +0000665 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500666 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400667 key.pk_info = &pk_info;
668 key.pk_ctx = ctx;
Hanno Beckera9851112019-01-25 16:37:10 +0000669 p = buf + sizeof( buf );
670 key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400671 if( key_len <= 0 )
Andrzej Kurekcef91af2018-11-08 04:33:06 -0500672 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400673
Gilles Peskined2d45c12019-05-27 14:53:13 +0200674 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100675 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200676 psa_set_key_algorithm( &attributes, psa_sig_md );
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500677
Gilles Peskined2d45c12019-05-27 14:53:13 +0200678 status = psa_import_key( &attributes,
679 buf + sizeof( buf ) - key_len, key_len,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200680 &key_id );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200681 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400682 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100683 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400684 goto cleanup;
685 }
686
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500687 /* We don't need the exported key anymore and can
688 * reuse its buffer for signature extraction. */
Andrzej Kurek9241d182018-11-20 05:04:35 -0500689 if( 2 * signature_part_size > sizeof( buf ) )
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500690 {
691 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
692 goto cleanup;
693 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500694
Hanno Beckera9851112019-01-25 16:37:10 +0000695 p = (unsigned char*) sig;
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500696 if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500697 signature_part_size ) ) != 0 )
698 {
699 goto cleanup;
700 }
701
Neil Armstrong3f9cef42022-02-21 10:43:18 +0100702 status = psa_verify_hash( key_id, psa_sig_md,
703 hash, hash_len,
704 buf, 2 * signature_part_size );
705 if( status != PSA_SUCCESS )
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400706 {
Neil Armstrong19915c22022-03-01 15:21:02 +0100707 ret = mbedtls_pk_error_from_psa_ecdca( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400708 goto cleanup;
709 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500710
711 if( p != sig + sig_len )
712 {
713 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
714 goto cleanup;
715 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400716 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400717
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500718cleanup:
Neil Armstrongcf5a2152022-02-24 16:58:54 +0100719 status = psa_destroy_key( key_id );
720 if( ret == 0 && status != PSA_SUCCESS )
721 ret = mbedtls_psa_err_translate_pk( status );
722
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400723 return( ret );
724}
725#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200727 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200728 const unsigned char *sig, size_t sig_len )
729{
Janos Follath24eed8d2019-11-22 13:21:35 +0000730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200731 ((void) md_alg);
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200734 hash, hash_len, sig, sig_len );
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
737 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200738
739 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200740}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400741#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200742
Neil Armstronge9606902022-02-09 14:23:00 +0100743#if defined(MBEDTLS_USE_PSA_CRYPTO)
744static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
745 size_t buf_len );
746
747static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end, size_t *key_len)
748{
749 size_t len;
750 int ret;
751
752 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
753 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
754 return( ret );
755
756 /* version */
757 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
758 MBEDTLS_ASN1_INTEGER ) ) != 0 )
759 return( ret );
760
761 *buf += len;
762
763 /* privateKey */
764 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
765 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
766 return( ret );
767
768 *key_len = len;
769
770 return 0;
771}
772
773static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
774 const unsigned char *hash, size_t hash_len,
775 unsigned char *sig, size_t sig_size, size_t *sig_len,
776 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
777{
778 mbedtls_ecdsa_context *ctx = ctx_arg;
779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
781 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
782 psa_status_t status;
783 mbedtls_pk_context key;
784 size_t key_len;
785 /* see ECP_PRV_DER_MAX_BYTES in pkwrite.c */
786 unsigned char buf[29 + 3 * MBEDTLS_ECP_MAX_BYTES];
787 unsigned char *p;
788 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
789 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
790 size_t curve_bits;
791 psa_ecc_family_t curve =
792 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
793 ((void) md_alg);
794
795 /* PSA has its own RNG */
796 ((void) f_rng);
797 ((void) p_rng);
798
799 if( curve == 0 )
800 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
801
802 /* mbedtls_pk_write_key_der() expects a full PK context;
803 * re-construct one to make it happy */
804 key.pk_info = &pk_info;
805 key.pk_ctx = ctx;
806 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
807 if( key_len <= 0 )
808 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
809
810 p = buf + sizeof( buf ) - key_len;
811 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
812 if( ret != 0 )
813 goto cleanup;
814
815 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
816 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
817 psa_set_key_algorithm( &attributes, psa_sig_md );
818
819 status = psa_import_key( &attributes,
820 p, key_len,
821 &key_id );
822 if( status != PSA_SUCCESS )
823 {
824 ret = mbedtls_psa_err_translate_pk( status );
825 goto cleanup;
826 }
827
828 if( psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
829 sig, sig_size, sig_len)
830 != PSA_SUCCESS )
831 {
832 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
833 goto cleanup;
834 }
835
836 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
837
838cleanup:
839 psa_destroy_key( key_id );
840 return( ret );
841}
842#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200844 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200845 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200846 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200849 md_alg, hash, hash_len,
850 sig, sig_size, sig_len,
851 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200852}
Neil Armstronge9606902022-02-09 14:23:00 +0100853#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200854
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200855#if defined(MBEDTLS_ECP_RESTARTABLE)
856static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
857 const unsigned char *hash, size_t hash_len,
858 const unsigned char *sig, size_t sig_len,
859 void *rs_ctx )
860{
Janos Follath24eed8d2019-11-22 13:21:35 +0000861 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200862 ((void) md_alg);
863
864 ret = mbedtls_ecdsa_read_signature_restartable(
865 (mbedtls_ecdsa_context *) ctx,
866 hash, hash_len, sig, sig_len,
867 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
868
869 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
870 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
871
872 return( ret );
873}
874
875static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
876 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +0200877 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200878 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
879 void *rs_ctx )
880{
881 return( mbedtls_ecdsa_write_signature_restartable(
882 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +0200883 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200884 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
885
886}
887#endif /* MBEDTLS_ECP_RESTARTABLE */
888
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200889static void *ecdsa_alloc_wrap( void )
890{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200891 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200892
893 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200895
896 return( ctx );
897}
898
899static void ecdsa_free_wrap( void *ctx )
900{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
902 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200903}
904
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200905#if defined(MBEDTLS_ECP_RESTARTABLE)
906static void *ecdsa_rs_alloc( void )
907{
908 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
909
910 if( ctx != NULL )
911 mbedtls_ecdsa_restart_init( ctx );
912
913 return( ctx );
914}
915
916static void ecdsa_rs_free( void *ctx )
917{
918 mbedtls_ecdsa_restart_free( ctx );
919 mbedtls_free( ctx );
920}
921#endif /* MBEDTLS_ECP_RESTARTABLE */
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923const mbedtls_pk_info_t mbedtls_ecdsa_info = {
924 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200925 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200926 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200927 ecdsa_can_do,
928 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200929 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200930#if defined(MBEDTLS_ECP_RESTARTABLE)
931 ecdsa_verify_rs_wrap,
932 ecdsa_sign_rs_wrap,
933#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200934 NULL,
935 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100936 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200937 ecdsa_alloc_wrap,
938 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200939#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200940 ecdsa_rs_alloc,
941 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200942#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200943 eckey_debug, /* Compatible key structures */
944};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200948/*
949 * Support for alternative RSA-private implementations
950 */
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200955}
956
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200957static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200960
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200961 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200962}
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200965 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200966 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200967 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
968{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200970
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100971#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000972 if( UINT_MAX < hash_len )
973 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100974#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000975
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200976 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +0100977 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
978 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200979 if( *sig_len > sig_size )
980 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200981
Thomas Daubneyfa1581e2021-05-18 12:38:33 +0100982 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200983 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200984}
985
986static int rsa_alt_decrypt_wrap( void *ctx,
987 const unsigned char *input, size_t ilen,
988 unsigned char *output, size_t *olen, size_t osize,
989 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
990{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200992
993 ((void) f_rng);
994 ((void) p_rng);
995
996 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200998
999 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +01001000 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001001}
1002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001004static int rsa_alt_check_pair( const void *pub, const void *prv,
1005 int (*f_rng)(void *, unsigned char *, size_t),
1006 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001009 unsigned char hash[32];
1010 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001012
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001013 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001015
1016 memset( hash, 0x2a, sizeof( hash ) );
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001019 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001020 sig, sizeof( sig ), &sig_len,
1021 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001022 {
1023 return( ret );
1024 }
1025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001027 hash, sizeof( hash ), sig, sig_len ) != 0 )
1028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001030 }
1031
1032 return( 0 );
1033}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001035
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001036static void *rsa_alt_alloc_wrap( void )
1037{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001038 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001039
1040 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001042
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001043 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001044}
1045
1046static void rsa_alt_free_wrap( void *ctx )
1047{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001048 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001050}
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1053 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001054 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001055 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001056 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001057 NULL,
1058 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001059#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001060 NULL,
1061 NULL,
1062#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001063 rsa_alt_decrypt_wrap,
1064 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001066 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001067#else
1068 NULL,
1069#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001070 rsa_alt_alloc_wrap,
1071 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001072#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001073 NULL,
1074 NULL,
1075#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001076 NULL,
1077};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001080
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001081#if defined(MBEDTLS_USE_PSA_CRYPTO)
1082
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001083static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001084{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001085 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001086
1087 /* no _init() function to call, an calloc() already zeroized */
1088
1089 return( ctx );
1090}
1091
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001092static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001093{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001094 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001095 mbedtls_free( ctx );
1096}
1097
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001098static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001099{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001100 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001101 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001102 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001103
Gilles Peskined2d45c12019-05-27 14:53:13 +02001104 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001105 return( 0 );
1106
Gilles Peskined2d45c12019-05-27 14:53:13 +02001107 bits = psa_get_key_bits( &attributes );
1108 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001109 return( bits );
1110}
1111
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001112static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001113{
1114 /* For now opaque PSA keys can only wrap ECC keypairs,
1115 * as checked by setup_psa().
1116 * Also, ECKEY_DH does not really make sense with the current API. */
1117 return( type == MBEDTLS_PK_ECKEY ||
1118 type == MBEDTLS_PK_ECDSA );
1119}
1120
John Durkopf35069a2020-08-17 22:05:14 -07001121#if defined(MBEDTLS_ECDSA_C)
1122
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001123/*
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +01001124 * Simultaneously convert and move raw MPI from the beginning of a buffer
1125 * to an ASN.1 MPI at the end of the buffer.
1126 * See also mbedtls_asn1_write_mpi().
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001127 *
1128 * p: pointer to the end of the output buffer
1129 * 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 +01001130 * n_len: length of the mpi to read from start
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001131 */
1132static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1133 size_t n_len )
1134{
Janos Follath24eed8d2019-11-22 13:21:35 +00001135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001136 size_t len = 0;
1137
1138 if( (size_t)( *p - start ) < n_len )
1139 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1140
1141 len = n_len;
1142 *p -= len;
1143 memmove( *p, start, len );
1144
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001145 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001146 * Neither r nor s should be 0, but as a failsafe measure, still detect
1147 * that rather than overflowing the buffer in case of a PSA error. */
1148 while( len > 0 && **p == 0x00 )
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001149 {
1150 ++(*p);
1151 --len;
1152 }
1153
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001154 /* this is only reached if the signature was invalid */
1155 if( len == 0 )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001156 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001157
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001158 /* if the msb is 1, ASN.1 requires that we prepend a 0.
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001159 * 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 +01001160 if( **p & 0x80 )
1161 {
1162 if( *p - start < 1 )
1163 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1164
1165 *--(*p) = 0x00;
1166 len += 1;
1167 }
1168
1169 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1170 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1171 MBEDTLS_ASN1_INTEGER ) );
1172
1173 return( (int) len );
1174}
1175
1176/* Transcode signature from PSA format to ASN.1 sequence.
1177 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1178 * MPIs, and in-place.
1179 *
1180 * [in/out] sig: the signature pre- and post-transcoding
1181 * [in/out] sig_len: signature length pre- and post-transcoding
1182 * [int] buf_len: the available size the in/out buffer
1183 */
1184static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
1185 size_t buf_len )
1186{
Janos Follath24eed8d2019-11-22 13:21:35 +00001187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001188 size_t len = 0;
1189 const size_t rs_len = *sig_len / 2;
1190 unsigned char *p = sig + buf_len;
1191
1192 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1193 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1194
1195 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1196 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1197 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1198
1199 memmove( sig, p, len );
1200 *sig_len = len;
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +01001201
1202 return( 0 );
1203}
1204
John Durkopd46ede02020-08-24 09:51:00 -07001205#endif /* MBEDTLS_ECDSA_C */
John Durkopf35069a2020-08-17 22:05:14 -07001206
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001207static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1208 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001209 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001210 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1211{
John Durkopf35069a2020-08-17 22:05:14 -07001212#if !defined(MBEDTLS_ECDSA_C)
1213 ((void) ctx);
1214 ((void) md_alg);
1215 ((void) hash);
1216 ((void) hash_len);
1217 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001218 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001219 ((void) sig_len);
1220 ((void) f_rng);
1221 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001222 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1223#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001224 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001225 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001226 psa_status_t status;
1227
1228 /* PSA has its own RNG */
1229 (void) f_rng;
1230 (void) p_rng;
1231
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001232 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001233 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001234 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001235 if( status != PSA_SUCCESS )
Neil Armstrong19915c22022-03-01 15:21:02 +01001236 return( mbedtls_pk_error_from_psa_ecdca( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001237
1238 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001239 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001240#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001241}
1242
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001243const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1244 MBEDTLS_PK_OPAQUE,
1245 "Opaque",
1246 pk_opaque_get_bitlen,
1247 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001248 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001249 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001250#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1251 NULL, /* restartable verify - not relevant */
1252 NULL, /* restartable sign - not relevant */
1253#endif
1254 NULL, /* decrypt - will be done later */
1255 NULL, /* encrypt - will be done later */
1256 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001257 pk_opaque_alloc_wrap,
1258 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001259#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1260 NULL, /* restart alloc - not relevant */
1261 NULL, /* restart free - not relevant */
1262#endif
1263 NULL, /* debug - could be done later, or even left NULL */
1264};
1265
1266#endif /* MBEDTLS_USE_PSA_CRYPTO */
1267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#endif /* MBEDTLS_PK_C */