blob: 2d4a1e28655ec5ca62dff9709cea7523a6e07c65 [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:
Ronald Croncf56a0a2020-08-04 09:51:30 +0200719 psa_destroy_key( key_id );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400720 return( ret );
721}
722#else /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200724 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200725 const unsigned char *sig, size_t sig_len )
726{
Janos Follath24eed8d2019-11-22 13:21:35 +0000727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200728 ((void) md_alg);
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200731 hash, hash_len, sig, sig_len );
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
734 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200735
736 return( ret );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200737}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400738#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200739
Neil Armstronge9606902022-02-09 14:23:00 +0100740#if defined(MBEDTLS_USE_PSA_CRYPTO)
741static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
742 size_t buf_len );
743
744static int find_ecdsa_private_key( unsigned char **buf, unsigned char *end, size_t *key_len)
745{
746 size_t len;
747 int ret;
748
749 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
750 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
751 return( ret );
752
753 /* version */
754 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
755 MBEDTLS_ASN1_INTEGER ) ) != 0 )
756 return( ret );
757
758 *buf += len;
759
760 /* privateKey */
761 if( ( ret = mbedtls_asn1_get_tag( buf, end, &len,
762 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
763 return( ret );
764
765 *key_len = len;
766
767 return 0;
768}
769
770static int ecdsa_sign_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
771 const unsigned char *hash, size_t hash_len,
772 unsigned char *sig, size_t sig_size, size_t *sig_len,
773 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
774{
775 mbedtls_ecdsa_context *ctx = ctx_arg;
776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
778 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
779 psa_status_t status;
780 mbedtls_pk_context key;
781 size_t key_len;
782 /* see ECP_PRV_DER_MAX_BYTES in pkwrite.c */
783 unsigned char buf[29 + 3 * MBEDTLS_ECP_MAX_BYTES];
784 unsigned char *p;
785 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
786 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
787 size_t curve_bits;
788 psa_ecc_family_t curve =
789 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
790 ((void) md_alg);
791
792 /* PSA has its own RNG */
793 ((void) f_rng);
794 ((void) p_rng);
795
796 if( curve == 0 )
797 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
798
799 /* mbedtls_pk_write_key_der() expects a full PK context;
800 * re-construct one to make it happy */
801 key.pk_info = &pk_info;
802 key.pk_ctx = ctx;
803 key_len = mbedtls_pk_write_key_der( &key, buf, sizeof( buf ) );
804 if( key_len <= 0 )
805 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
806
807 p = buf + sizeof( buf ) - key_len;
808 ret = find_ecdsa_private_key( &p, buf + sizeof( buf ), &key_len );
809 if( ret != 0 )
810 goto cleanup;
811
812 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_KEY_PAIR( curve ) );
813 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
814 psa_set_key_algorithm( &attributes, psa_sig_md );
815
816 status = psa_import_key( &attributes,
817 p, key_len,
818 &key_id );
819 if( status != PSA_SUCCESS )
820 {
821 ret = mbedtls_psa_err_translate_pk( status );
822 goto cleanup;
823 }
824
825 if( psa_sign_hash( key_id, psa_sig_md, hash, hash_len,
826 sig, sig_size, sig_len)
827 != PSA_SUCCESS )
828 {
829 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
830 goto cleanup;
831 }
832
833 ret = pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size );
834
835cleanup:
836 psa_destroy_key( key_id );
837 return( ret );
838}
839#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200841 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200842 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200843 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200846 md_alg, hash, hash_len,
847 sig, sig_size, sig_len,
848 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200849}
Neil Armstronge9606902022-02-09 14:23:00 +0100850#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200851
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200852#if defined(MBEDTLS_ECP_RESTARTABLE)
853static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
854 const unsigned char *hash, size_t hash_len,
855 const unsigned char *sig, size_t sig_len,
856 void *rs_ctx )
857{
Janos Follath24eed8d2019-11-22 13:21:35 +0000858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200859 ((void) md_alg);
860
861 ret = mbedtls_ecdsa_read_signature_restartable(
862 (mbedtls_ecdsa_context *) ctx,
863 hash, hash_len, sig, sig_len,
864 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
865
866 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
867 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
868
869 return( ret );
870}
871
872static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
873 const unsigned char *hash, size_t hash_len,
Gilles Peskine908982b2021-06-22 11:06:08 +0200874 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200875 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
876 void *rs_ctx )
877{
878 return( mbedtls_ecdsa_write_signature_restartable(
879 (mbedtls_ecdsa_context *) ctx,
Gilles Peskine908982b2021-06-22 11:06:08 +0200880 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200881 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
882
883}
884#endif /* MBEDTLS_ECP_RESTARTABLE */
885
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200886static void *ecdsa_alloc_wrap( void )
887{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200888 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200889
890 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200892
893 return( ctx );
894}
895
896static void ecdsa_free_wrap( void *ctx )
897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
899 mbedtls_free( ctx );
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200900}
901
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200902#if defined(MBEDTLS_ECP_RESTARTABLE)
903static void *ecdsa_rs_alloc( void )
904{
905 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
906
907 if( ctx != NULL )
908 mbedtls_ecdsa_restart_init( ctx );
909
910 return( ctx );
911}
912
913static void ecdsa_rs_free( void *ctx )
914{
915 mbedtls_ecdsa_restart_free( ctx );
916 mbedtls_free( ctx );
917}
918#endif /* MBEDTLS_ECP_RESTARTABLE */
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920const mbedtls_pk_info_t mbedtls_ecdsa_info = {
921 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200922 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200923 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200924 ecdsa_can_do,
925 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200926 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200927#if defined(MBEDTLS_ECP_RESTARTABLE)
928 ecdsa_verify_rs_wrap,
929 ecdsa_sign_rs_wrap,
930#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200931 NULL,
932 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100933 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200934 ecdsa_alloc_wrap,
935 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200936#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200937 ecdsa_rs_alloc,
938 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200939#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200940 eckey_debug, /* Compatible key structures */
941};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200945/*
946 * Support for alternative RSA-private implementations
947 */
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949static int rsa_alt_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 return( type == MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200952}
953
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200954static size_t rsa_alt_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200957
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200958 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200959}
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200962 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200963 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200964 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200967
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100968#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000969 if( UINT_MAX < hash_len )
970 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100971#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000972
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200973 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
Gilles Peskinef48d6f22019-11-05 17:31:36 +0100974 if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
975 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskinef00f1522021-06-22 00:09:00 +0200976 if( *sig_len > sig_size )
977 return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200978
Thomas Daubneyfa1581e2021-05-18 12:38:33 +0100979 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200980 md_alg, (unsigned int) hash_len, hash, sig ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200981}
982
983static int rsa_alt_decrypt_wrap( void *ctx,
984 const unsigned char *input, size_t ilen,
985 unsigned char *output, size_t *olen, size_t osize,
986 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
987{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200989
990 ((void) f_rng);
991 ((void) p_rng);
992
993 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200995
996 return( rsa_alt->decrypt_func( rsa_alt->key,
Thomas Daubney99914142021-05-06 15:17:03 +0100997 olen, input, output, osize ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200998}
999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +02001001static int rsa_alt_check_pair( const void *pub, const void *prv,
1002 int (*f_rng)(void *, unsigned char *, size_t),
1003 void *p_rng )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001004{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001006 unsigned char hash[32];
1007 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001009
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001010 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001012
1013 memset( hash, 0x2a, sizeof( hash ) );
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001016 hash, sizeof( hash ),
Gilles Peskinef00f1522021-06-22 00:09:00 +02001017 sig, sizeof( sig ), &sig_len,
1018 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001019 {
1020 return( ret );
1021 }
1022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001024 hash, sizeof( hash ), sig, sig_len ) != 0 )
1025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001027 }
1028
1029 return( 0 );
1030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001032
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001033static void *rsa_alt_alloc_wrap( void )
1034{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001035 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001036
1037 if( ctx != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001039
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001040 return( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001041}
1042
1043static void rsa_alt_free_wrap( void *ctx )
1044{
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001045 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_free( ctx );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001047}
1048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1050 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001051 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001052 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001053 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001054 NULL,
1055 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001056#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001057 NULL,
1058 NULL,
1059#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001060 rsa_alt_decrypt_wrap,
1061 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001063 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001064#else
1065 NULL,
1066#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001067 rsa_alt_alloc_wrap,
1068 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001069#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001070 NULL,
1071 NULL,
1072#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001073 NULL,
1074};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001077
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001078#if defined(MBEDTLS_USE_PSA_CRYPTO)
1079
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001080static void *pk_opaque_alloc_wrap( void )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001081{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001082 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001083
1084 /* no _init() function to call, an calloc() already zeroized */
1085
1086 return( ctx );
1087}
1088
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001089static void pk_opaque_free_wrap( void *ctx )
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001090{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001091 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_svc_key_id_t ) );
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001092 mbedtls_free( ctx );
1093}
1094
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001095static size_t pk_opaque_get_bitlen( const void *ctx )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001096{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001097 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001098 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001100
Gilles Peskined2d45c12019-05-27 14:53:13 +02001101 if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001102 return( 0 );
1103
Gilles Peskined2d45c12019-05-27 14:53:13 +02001104 bits = psa_get_key_bits( &attributes );
1105 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001106 return( bits );
1107}
1108
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001109static int pk_opaque_can_do( mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001110{
1111 /* For now opaque PSA keys can only wrap ECC keypairs,
1112 * as checked by setup_psa().
1113 * Also, ECKEY_DH does not really make sense with the current API. */
1114 return( type == MBEDTLS_PK_ECKEY ||
1115 type == MBEDTLS_PK_ECDSA );
1116}
1117
John Durkopf35069a2020-08-17 22:05:14 -07001118#if defined(MBEDTLS_ECDSA_C)
1119
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001120/*
Manuel Pégourié-Gonnard509aff12018-11-15 12:17:38 +01001121 * Simultaneously convert and move raw MPI from the beginning of a buffer
1122 * to an ASN.1 MPI at the end of the buffer.
1123 * See also mbedtls_asn1_write_mpi().
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001124 *
1125 * p: pointer to the end of the output buffer
1126 * 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 +01001127 * n_len: length of the mpi to read from start
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001128 */
1129static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
1130 size_t n_len )
1131{
Janos Follath24eed8d2019-11-22 13:21:35 +00001132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001133 size_t len = 0;
1134
1135 if( (size_t)( *p - start ) < n_len )
1136 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1137
1138 len = n_len;
1139 *p -= len;
1140 memmove( *p, start, len );
1141
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001142 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001143 * Neither r nor s should be 0, but as a failsafe measure, still detect
1144 * that rather than overflowing the buffer in case of a PSA error. */
1145 while( len > 0 && **p == 0x00 )
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001146 {
1147 ++(*p);
1148 --len;
1149 }
1150
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001151 /* this is only reached if the signature was invalid */
1152 if( len == 0 )
TRodziewiczb579ccd2021-04-13 14:28:28 +02001153 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard59eecb02018-11-16 10:54:54 +01001154
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001155 /* if the msb is 1, ASN.1 requires that we prepend a 0.
Manuel Pégourié-Gonnard45013a12018-11-16 10:09:11 +01001156 * 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 +01001157 if( **p & 0x80 )
1158 {
1159 if( *p - start < 1 )
1160 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
1161
1162 *--(*p) = 0x00;
1163 len += 1;
1164 }
1165
1166 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
1167 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
1168 MBEDTLS_ASN1_INTEGER ) );
1169
1170 return( (int) len );
1171}
1172
1173/* Transcode signature from PSA format to ASN.1 sequence.
1174 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1175 * MPIs, and in-place.
1176 *
1177 * [in/out] sig: the signature pre- and post-transcoding
1178 * [in/out] sig_len: signature length pre- and post-transcoding
1179 * [int] buf_len: the available size the in/out buffer
1180 */
1181static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
1182 size_t buf_len )
1183{
Janos Follath24eed8d2019-11-22 13:21:35 +00001184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001185 size_t len = 0;
1186 const size_t rs_len = *sig_len / 2;
1187 unsigned char *p = sig + buf_len;
1188
1189 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
1190 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
1191
1192 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
1193 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
1194 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
1195
1196 memmove( sig, p, len );
1197 *sig_len = len;
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +01001198
1199 return( 0 );
1200}
1201
John Durkopd46ede02020-08-24 09:51:00 -07001202#endif /* MBEDTLS_ECDSA_C */
John Durkopf35069a2020-08-17 22:05:14 -07001203
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001204static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
1205 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001206 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001207 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1208{
John Durkopf35069a2020-08-17 22:05:14 -07001209#if !defined(MBEDTLS_ECDSA_C)
1210 ((void) ctx);
1211 ((void) md_alg);
1212 ((void) hash);
1213 ((void) hash_len);
1214 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001215 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001216 ((void) sig_len);
1217 ((void) f_rng);
1218 ((void) p_rng);
John Durkopaf5363c2020-08-24 08:29:39 -07001219 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1220#else /* !MBEDTLS_ECDSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001221 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001222 psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001223 psa_status_t status;
1224
1225 /* PSA has its own RNG */
1226 (void) f_rng;
1227 (void) p_rng;
1228
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001229 /* make the signature */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001230 status = psa_sign_hash( *key, alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +02001231 sig, sig_size, sig_len );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001232 if( status != PSA_SUCCESS )
Neil Armstrong19915c22022-03-01 15:21:02 +01001233 return( mbedtls_pk_error_from_psa_ecdca( status ) );
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001234
1235 /* transcode it to ASN.1 sequence */
Gilles Peskinef00f1522021-06-22 00:09:00 +02001236 return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, sig_size ) );
John Durkopaf5363c2020-08-24 08:29:39 -07001237#endif /* !MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001238}
1239
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001240const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
1241 MBEDTLS_PK_OPAQUE,
1242 "Opaque",
1243 pk_opaque_get_bitlen,
1244 pk_opaque_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001245 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001246 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001247#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1248 NULL, /* restartable verify - not relevant */
1249 NULL, /* restartable sign - not relevant */
1250#endif
1251 NULL, /* decrypt - will be done later */
1252 NULL, /* encrypt - will be done later */
1253 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001254 pk_opaque_alloc_wrap,
1255 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001256#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1257 NULL, /* restart alloc - not relevant */
1258 NULL, /* restart free - not relevant */
1259#endif
1260 NULL, /* debug - could be done later, or even left NULL */
1261};
1262
1263#endif /* MBEDTLS_USE_PSA_CRYPTO */
1264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#endif /* MBEDTLS_PK_C */