blob: ecf002d452c8b5aa532d64668d71b03cf64f9667 [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 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200153int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx,
154 const psa_key_id_t key )
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200155{
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100156 const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200158 psa_key_id_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100159 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200160
161 if( ctx == NULL || ctx->pk_info != NULL )
162 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
163
Gilles Peskined2d45c12019-05-27 14:53:13 +0200164 if( PSA_SUCCESS != psa_get_key_attributes( key, &attributes ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100165 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200166 type = psa_get_key_type( &attributes );
167 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100168
169 /* Current implementation of can_do() relies on this. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200170 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100171 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) ;
172
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200173 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
174 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
175
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200176 ctx->pk_info = info;
177
Ronald Croncf56a0a2020-08-04 09:51:30 +0200178 pk_ctx = (psa_key_id_t *) ctx->pk_ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100179 *pk_ctx = key;
180
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200181 return( 0 );
182}
183#endif /* MBEDTLS_USE_PSA_CRYPTO */
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100186/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200187 * Initialize an RSA-alt context
188 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200189int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
191 mbedtls_pk_rsa_alt_sign_func sign_func,
192 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_rsa_alt_context *rsa_alt;
195 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200196
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 PK_VALIDATE_RET( ctx != NULL );
198 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200200
201 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200202 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200203
204 ctx->pk_info = info;
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200207
208 rsa_alt->key = key;
209 rsa_alt->decrypt_func = decrypt_func;
210 rsa_alt->sign_func = sign_func;
211 rsa_alt->key_len_func = key_len_func;
212
213 return( 0 );
214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200216
217/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200218 * Tell if a PK can do the operations of the given type
219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200221{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500222 /* A context with null pk_info is not set up yet and can't do anything.
223 * For backward compatibility, also accept NULL instead of a context
224 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200225 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200226 return( 0 );
227
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200228 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200229}
230
231/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200237
238 if( *hash_len != 0 )
239 return( 0 );
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200242 return( -1 );
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200245 return( 0 );
246}
247
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200248#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200249/*
250 * Helper to set up a restart context if needed
251 */
252static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200253 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200254{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200255 /* Don't do anything if already set up or invalid */
256 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200257 return( 0 );
258
259 /* Should never happen when we're called */
260 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
261 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
262
263 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
264 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
265
266 ctx->pk_info = info;
267
268 return( 0 );
269}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200270#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200271
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200272/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200273 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200274 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200275int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
276 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200277 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200278 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200279 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200280{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500281 PK_VALIDATE_RET( ctx != NULL );
282 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
283 hash != NULL );
284 PK_VALIDATE_RET( sig != NULL );
285
286 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200287 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200289
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200290#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200291 /* optimization: use non-restartable version if restart disabled */
292 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200293 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200294 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200295 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000296 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200297
298 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
299 return( ret );
300
301 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
302 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
303
304 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
305 mbedtls_pk_restart_free( rs_ctx );
306
307 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200308 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200309#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200310 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200311#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200312
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200313 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200315
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200316 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200317 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200318}
319
320/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200321 * Verify a signature
322 */
323int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
324 const unsigned char *hash, size_t hash_len,
325 const unsigned char *sig, size_t sig_len )
326{
327 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
328 sig, sig_len, NULL ) );
329}
330
331/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200332 * Verify a signature with options
333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
335 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200336 const unsigned char *hash, size_t hash_len,
337 const unsigned char *sig, size_t sig_len )
338{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500339 PK_VALIDATE_RET( ctx != NULL );
340 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
341 hash != NULL );
342 PK_VALIDATE_RET( sig != NULL );
343
344 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( ! mbedtls_pk_can_do( ctx, type ) )
348 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Janos Follath24eed8d2019-11-22 13:21:35 +0000353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200355
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100356#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000357 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
358 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100359#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000360
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200361 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 if( sig_len < mbedtls_pk_get_len( ctx ) )
367 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
370 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200371 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200372 pss_opts->mgf1_hash_id,
373 pss_opts->expected_salt_len,
374 sig );
375 if( ret != 0 )
376 return( ret );
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( sig_len > mbedtls_pk_get_len( ctx ) )
379 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200380
381 return( 0 );
382#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000384#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200385 }
386
387 /* General case: no options */
388 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200392}
393
394/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200395 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200396 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200397int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
398 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200399 const unsigned char *hash, size_t hash_len,
400 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200401 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200402 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200403{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500404 PK_VALIDATE_RET( ctx != NULL );
405 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
406 hash != NULL );
407 PK_VALIDATE_RET( sig != NULL );
408
409 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200410 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200412
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200413#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200414 /* optimization: use non-restartable version if restart disabled */
415 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200416 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200417 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200418 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200420
421 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
422 return( ret );
423
424 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
425 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
426
427 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
428 mbedtls_pk_restart_free( rs_ctx );
429
430 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200431 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200432#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200433 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200434#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200435
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200436 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200438
439 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
440 sig, sig_len, f_rng, p_rng ) );
441}
442
443/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200444 * Make a signature
445 */
446int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
447 const unsigned char *hash, size_t hash_len,
448 unsigned char *sig, size_t *sig_len,
449 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
450{
451 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
452 sig, sig_len, f_rng, p_rng, NULL ) );
453}
454
455/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200456 * Decrypt message
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200459 const unsigned char *input, size_t ilen,
460 unsigned char *output, size_t *olen, size_t osize,
461 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
462{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500463 PK_VALIDATE_RET( ctx != NULL );
464 PK_VALIDATE_RET( input != NULL || ilen == 0 );
465 PK_VALIDATE_RET( output != NULL || osize == 0 );
466 PK_VALIDATE_RET( olen != NULL );
467
468 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200470
471 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200473
474 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
475 output, olen, osize, f_rng, p_rng ) );
476}
477
478/*
479 * Encrypt message
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200482 const unsigned char *input, size_t ilen,
483 unsigned char *output, size_t *olen, size_t osize,
484 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
485{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500486 PK_VALIDATE_RET( ctx != NULL );
487 PK_VALIDATE_RET( input != NULL || ilen == 0 );
488 PK_VALIDATE_RET( output != NULL || osize == 0 );
489 PK_VALIDATE_RET( olen != NULL );
490
491 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200493
494 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200496
497 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
498 output, olen, osize, f_rng, p_rng ) );
499}
500
501/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100502 * Check public-private key pair
503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100505{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500506 PK_VALIDATE_RET( pub != NULL );
507 PK_VALIDATE_RET( prv != NULL );
508
509 if( pub->pk_info == NULL ||
Andrzej Kurekefed3232019-02-05 05:09:05 -0500510 prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100513 }
514
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200515 if( prv->pk_info->check_pair_func == NULL )
516 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 if( pub->pk_info->type != MBEDTLS_PK_RSA )
521 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100522 }
523 else
524 {
525 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100527 }
528
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100529 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100530}
531
532/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200533 * Get key size in bits
534 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200535size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200536{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500537 /* For backward compatibility, accept NULL or a context that
538 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200539 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200540 return( 0 );
541
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200542 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200543}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200544
545/*
546 * Export debug information
547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200549{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500550 PK_VALIDATE_RET( ctx != NULL );
551 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200553
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200554 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200556
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200557 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200558 return( 0 );
559}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200560
561/*
562 * Access the PK type name
563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200565{
566 if( ctx == NULL || ctx->pk_info == NULL )
567 return( "invalid PK" );
568
569 return( ctx->pk_info->name );
570}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200571
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200572/*
573 * Access the PK type
574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200576{
577 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200579
580 return( ctx->pk_info->type );
581}
582
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100583#if defined(MBEDTLS_USE_PSA_CRYPTO)
584/*
585 * Load the key to a PSA key slot,
586 * then turn the PK context into a wrapper for that key slot.
587 *
588 * Currently only works for EC private keys.
589 */
590int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200591 psa_key_id_t *key,
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100592 psa_algorithm_t hash_alg )
593{
594#if !defined(MBEDTLS_ECP_C)
John Durkopf35069a2020-08-17 22:05:14 -0700595 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200596 ((void) key);
John Durkopf35069a2020-08-17 22:05:14 -0700597 ((void) hash_alg);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100598 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
599#else
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100600 const mbedtls_ecp_keypair *ec;
601 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
602 size_t d_len;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100603 psa_ecc_family_t curve_id;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100605 psa_key_type_t key_type;
Gilles Peskine4080c912019-12-18 20:43:03 +0100606 size_t bits;
Janos Follath24eed8d2019-11-22 13:21:35 +0000607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100608
609 /* export the private key material in the format PSA wants */
610 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
611 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
612
613 ec = mbedtls_pk_ec( *pk );
614 d_len = ( ec->grp.nbits + 7 ) / 8;
615 if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
616 return( ret );
617
Gilles Peskine4080c912019-12-18 20:43:03 +0100618 curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
619 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100620
Gilles Peskined2d45c12019-05-27 14:53:13 +0200621 /* prepare the key attributes */
622 psa_set_key_type( &attributes, key_type );
Gilles Peskine4080c912019-12-18 20:43:03 +0100623 psa_set_key_bits( &attributes, bits );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100624 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200625 psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100626
Gilles Peskined2d45c12019-05-27 14:53:13 +0200627 /* import private key into PSA */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200628 if( PSA_SUCCESS != psa_import_key( &attributes, d, d_len, key ) )
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100629 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
630
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100631 /* make PK context wrap the key slot */
632 mbedtls_pk_free( pk );
633 mbedtls_pk_init( pk );
634
Ronald Croncf56a0a2020-08-04 09:51:30 +0200635 return( mbedtls_pk_setup_opaque( pk, *key ) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100636#endif /* MBEDTLS_ECP_C */
637}
638#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_PK_C */