blob: 9a3bcb0dc611aefdcdfa6719c7d03769ea4b4576 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
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é-Gonnard12e0ed92013-07-04 13:31:32 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020024#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020025
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050026#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000027#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020037#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020038
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010039#if defined(MBEDTLS_USE_PSA_CRYPTO)
40#include "mbedtls/psa_util.h"
41#endif
42
Andres AG72849872017-01-19 11:24:33 +000043#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010044#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020045
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050046/* Parameter validation macros based on platform_util.h */
47#define PK_VALIDATE_RET( cond ) \
48 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
49#define PK_VALIDATE( cond ) \
50 MBEDTLS_INTERNAL_VALIDATE( cond )
51
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020052/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020056{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050057 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020059 ctx->pk_info = NULL;
60 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061}
62
63/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020067{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050068 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069 return;
70
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050071 if ( ctx->pk_info != NULL )
72 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020073
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050074 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020075}
76
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020077#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020078/*
79 * Initialize a restart context
80 */
81void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
82{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020084 ctx->pk_info = NULL;
85 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020086}
87
88/*
89 * Free the components of a restart context
90 */
91void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
92{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020093 if( ctx == NULL || ctx->pk_info == NULL ||
94 ctx->pk_info->rs_free_func == NULL )
95 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020096 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020097 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020099 ctx->pk_info->rs_free_func( ctx->rs_ctx );
100
101 ctx->pk_info = NULL;
102 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200104#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200105
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200106/*
107 * Get pk_info structure from type
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200110{
111 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_RSA_C)
113 case MBEDTLS_PK_RSA:
114 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_ECP_C)
117 case MBEDTLS_PK_ECKEY:
118 return( &mbedtls_eckey_info );
119 case MBEDTLS_PK_ECKEY_DH:
120 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_ECDSA_C)
123 case MBEDTLS_PK_ECDSA:
124 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200127 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200128 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200129 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200130}
131
132/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200133 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200134 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200135int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200136{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 PK_VALIDATE_RET( ctx != NULL );
138 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200140
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200141 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200142 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200143
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200144 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200145
146 return( 0 );
147}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200148
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200149#if defined(MBEDTLS_USE_PSA_CRYPTO)
150/*
151 * Initialise a PSA-wrapping context
152 */
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500153int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, const psa_key_handle_t key )
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200154{
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100155 const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500157 psa_key_handle_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100158 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200159
160 if( ctx == NULL || ctx->pk_info != NULL )
161 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
162
Gilles Peskined2d45c12019-05-27 14:53:13 +0200163 if( PSA_SUCCESS != psa_get_key_attributes( key, &attributes ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100164 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200165 type = psa_get_key_type( &attributes );
166 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100167
168 /* Current implementation of can_do() relies on this. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200169 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100170 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) ;
171
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200172 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
173 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
174
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200175 ctx->pk_info = info;
176
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500177 pk_ctx = (psa_key_handle_t *) ctx->pk_ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100178 *pk_ctx = key;
179
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200180 return( 0 );
181}
182#endif /* MBEDTLS_USE_PSA_CRYPTO */
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100185/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200186 * Initialize an RSA-alt context
187 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200188int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
190 mbedtls_pk_rsa_alt_sign_func sign_func,
191 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_rsa_alt_context *rsa_alt;
194 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200195
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 PK_VALIDATE_RET( ctx != NULL );
197 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200199
200 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200201 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200202
203 ctx->pk_info = info;
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200206
207 rsa_alt->key = key;
208 rsa_alt->decrypt_func = decrypt_func;
209 rsa_alt->sign_func = sign_func;
210 rsa_alt->key_len_func = key_len_func;
211
212 return( 0 );
213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200215
216/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200217 * Tell if a PK can do the operations of the given type
218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200220{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221 /* A context with null pk_info is not set up yet and can't do anything.
222 * For backward compatibility, also accept NULL instead of a context
223 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200224 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200225 return( 0 );
226
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200227 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200228}
229
230/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200236
237 if( *hash_len != 0 )
238 return( 0 );
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200241 return( -1 );
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200244 return( 0 );
245}
246
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200247#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200248/*
249 * Helper to set up a restart context if needed
250 */
251static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200252 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200253{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200254 /* Don't do anything if already set up or invalid */
255 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200256 return( 0 );
257
258 /* Should never happen when we're called */
259 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
260 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
261
262 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
263 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
264
265 ctx->pk_info = info;
266
267 return( 0 );
268}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200269#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200270
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200271/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200272 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200273 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200274int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
275 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200276 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200277 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200278 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200279{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 PK_VALIDATE_RET( ctx != NULL );
281 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
282 hash != NULL );
283 PK_VALIDATE_RET( sig != NULL );
284
285 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200286 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200288
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200289#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200290 /* optimization: use non-restartable version if restart disabled */
291 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200292 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200293 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200294 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200296
297 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
298 return( ret );
299
300 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
301 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
302
303 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
304 mbedtls_pk_restart_free( rs_ctx );
305
306 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200307 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200308#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200309 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200310#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200311
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200312 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200314
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200315 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200316 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200317}
318
319/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200320 * Verify a signature
321 */
322int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
323 const unsigned char *hash, size_t hash_len,
324 const unsigned char *sig, size_t sig_len )
325{
326 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
327 sig, sig_len, NULL ) );
328}
329
330/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200331 * Verify a signature with options
332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
334 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200335 const unsigned char *hash, size_t hash_len,
336 const unsigned char *sig, size_t sig_len )
337{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500338 PK_VALIDATE_RET( ctx != NULL );
339 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
340 hash != NULL );
341 PK_VALIDATE_RET( sig != NULL );
342
343 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ! mbedtls_pk_can_do( ctx, type ) )
347 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Janos Follath24eed8d2019-11-22 13:21:35 +0000352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200354
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100355#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000356 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
357 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100358#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000359
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200360 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( sig_len < mbedtls_pk_get_len( ctx ) )
366 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
369 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200370 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200371 pss_opts->mgf1_hash_id,
372 pss_opts->expected_salt_len,
373 sig );
374 if( ret != 0 )
375 return( ret );
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 if( sig_len > mbedtls_pk_get_len( ctx ) )
378 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200379
380 return( 0 );
381#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000383#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200384 }
385
386 /* General case: no options */
387 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200391}
392
393/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200394 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200395 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200396int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
397 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200398 const unsigned char *hash, size_t hash_len,
399 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200400 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200401 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200402{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500403 PK_VALIDATE_RET( ctx != NULL );
404 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
405 hash != NULL );
406 PK_VALIDATE_RET( sig != NULL );
407
408 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200409 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200411
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200412#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200413 /* optimization: use non-restartable version if restart disabled */
414 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200415 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200416 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200417 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200419
420 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
421 return( ret );
422
423 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
424 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
425
426 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
427 mbedtls_pk_restart_free( rs_ctx );
428
429 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200430 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200431#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200432 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200433#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200434
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200435 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200437
438 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
439 sig, sig_len, f_rng, p_rng ) );
440}
441
442/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200443 * Make a signature
444 */
445int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
446 const unsigned char *hash, size_t hash_len,
447 unsigned char *sig, size_t *sig_len,
448 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
449{
450 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
451 sig, sig_len, f_rng, p_rng, NULL ) );
452}
453
454/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200455 * Decrypt message
456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200458 const unsigned char *input, size_t ilen,
459 unsigned char *output, size_t *olen, size_t osize,
460 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
461{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500462 PK_VALIDATE_RET( ctx != NULL );
463 PK_VALIDATE_RET( input != NULL || ilen == 0 );
464 PK_VALIDATE_RET( output != NULL || osize == 0 );
465 PK_VALIDATE_RET( olen != NULL );
466
467 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200469
470 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200472
473 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
474 output, olen, osize, f_rng, p_rng ) );
475}
476
477/*
478 * Encrypt message
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200481 const unsigned char *input, size_t ilen,
482 unsigned char *output, size_t *olen, size_t osize,
483 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
484{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500485 PK_VALIDATE_RET( ctx != NULL );
486 PK_VALIDATE_RET( input != NULL || ilen == 0 );
487 PK_VALIDATE_RET( output != NULL || osize == 0 );
488 PK_VALIDATE_RET( olen != NULL );
489
490 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200492
493 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200495
496 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
497 output, olen, osize, f_rng, p_rng ) );
498}
499
500/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100501 * Check public-private key pair
502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100504{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500505 PK_VALIDATE_RET( pub != NULL );
506 PK_VALIDATE_RET( prv != NULL );
507
508 if( pub->pk_info == NULL ||
Andrzej Kurekefed3232019-02-05 05:09:05 -0500509 prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100512 }
513
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200514 if( prv->pk_info->check_pair_func == NULL )
515 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( pub->pk_info->type != MBEDTLS_PK_RSA )
520 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100521 }
522 else
523 {
524 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100526 }
527
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100528 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100529}
530
531/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200532 * Get key size in bits
533 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200534size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200535{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500536 /* For backward compatibility, accept NULL or a context that
537 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200538 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200539 return( 0 );
540
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200541 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200542}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200543
544/*
545 * Export debug information
546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200548{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500549 PK_VALIDATE_RET( ctx != NULL );
550 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200552
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200553 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200555
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200556 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200557 return( 0 );
558}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200559
560/*
561 * Access the PK type name
562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200564{
565 if( ctx == NULL || ctx->pk_info == NULL )
566 return( "invalid PK" );
567
568 return( ctx->pk_info->name );
569}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200570
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200571/*
572 * Access the PK type
573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200575{
576 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200578
579 return( ctx->pk_info->type );
580}
581
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100582#if defined(MBEDTLS_USE_PSA_CRYPTO)
583/*
584 * Load the key to a PSA key slot,
585 * then turn the PK context into a wrapper for that key slot.
586 *
587 * Currently only works for EC private keys.
588 */
589int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
Gilles Peskined2d45c12019-05-27 14:53:13 +0200590 psa_key_handle_t *handle,
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100591 psa_algorithm_t hash_alg )
592{
593#if !defined(MBEDTLS_ECP_C)
John Durkopf35069a2020-08-17 22:05:14 -0700594 ((void) pk);
595 ((void) handle);
596 ((void) hash_alg);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100597 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
598#else
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100599 const mbedtls_ecp_keypair *ec;
600 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
601 size_t d_len;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100602 psa_ecc_family_t curve_id;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100604 psa_key_type_t key_type;
Gilles Peskine4080c912019-12-18 20:43:03 +0100605 size_t bits;
Janos Follath24eed8d2019-11-22 13:21:35 +0000606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100607
608 /* export the private key material in the format PSA wants */
609 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
610 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
611
612 ec = mbedtls_pk_ec( *pk );
613 d_len = ( ec->grp.nbits + 7 ) / 8;
614 if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
615 return( ret );
616
Gilles Peskine4080c912019-12-18 20:43:03 +0100617 curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
618 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100619
Gilles Peskined2d45c12019-05-27 14:53:13 +0200620 /* prepare the key attributes */
621 psa_set_key_type( &attributes, key_type );
Gilles Peskine4080c912019-12-18 20:43:03 +0100622 psa_set_key_bits( &attributes, bits );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100623 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200624 psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100625
Gilles Peskined2d45c12019-05-27 14:53:13 +0200626 /* import private key into PSA */
627 if( PSA_SUCCESS != psa_import_key( &attributes, d, d_len, handle ) )
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100628 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
629
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100630 /* make PK context wrap the key slot */
631 mbedtls_pk_free( pk );
632 mbedtls_pk_init( pk );
633
Gilles Peskined2d45c12019-05-27 14:53:13 +0200634 return( mbedtls_pk_setup_opaque( pk, *handle ) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100635#endif /* MBEDTLS_ECP_C */
636}
637#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_PK_C */