blob: b83ba8e71d443066c818b1dc8c1842e84dc08882 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020030#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020031
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050032#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020043#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020044
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#endif
48
Andres AG72849872017-01-19 11:24:33 +000049#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010050#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020051
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050052/* Parameter validation macros based on platform_util.h */
53#define PK_VALIDATE_RET( cond ) \
54 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
55#define PK_VALIDATE( cond ) \
56 MBEDTLS_INTERNAL_VALIDATE( cond )
57
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020064
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020065 ctx->pk_info = NULL;
66 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020067}
68
69/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020073{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050074 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020075 return;
76
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077 if ( ctx->pk_info != NULL )
78 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020079
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050080 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020081}
82
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020083#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020084/*
85 * Initialize a restart context
86 */
87void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
88{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050089 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020090 ctx->pk_info = NULL;
91 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092}
93
94/*
95 * Free the components of a restart context
96 */
97void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
98{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020099 if( ctx == NULL || ctx->pk_info == NULL ||
100 ctx->pk_info->rs_free_func == NULL )
101 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200102 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200103 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200104
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200105 ctx->pk_info->rs_free_func( ctx->rs_ctx );
106
107 ctx->pk_info = NULL;
108 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200109}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200110#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200111
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200112/*
113 * Get pk_info structure from type
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200116{
117 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_RSA_C)
119 case MBEDTLS_PK_RSA:
120 return( &mbedtls_rsa_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_ECP_C)
123 case MBEDTLS_PK_ECKEY:
124 return( &mbedtls_eckey_info );
125 case MBEDTLS_PK_ECKEY_DH:
126 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_ECDSA_C)
129 case MBEDTLS_PK_ECDSA:
130 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200133 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200134 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200135 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200136}
137
138/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200139 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200140 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200141int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200142{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500143 PK_VALIDATE_RET( ctx != NULL );
144 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200146
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200147 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200148 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200149
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200150 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200151
152 return( 0 );
153}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200154
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200155#if defined(MBEDTLS_USE_PSA_CRYPTO)
156/*
157 * Initialise a PSA-wrapping context
158 */
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500159int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, const psa_key_handle_t key )
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200160{
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100161 const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500163 psa_key_handle_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100164 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200165
166 if( ctx == NULL || ctx->pk_info != NULL )
167 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
168
Gilles Peskined2d45c12019-05-27 14:53:13 +0200169 if( PSA_SUCCESS != psa_get_key_attributes( key, &attributes ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100170 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200171 type = psa_get_key_type( &attributes );
172 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100173
174 /* Current implementation of can_do() relies on this. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200175 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100176 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) ;
177
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200178 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
179 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
180
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200181 ctx->pk_info = info;
182
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500183 pk_ctx = (psa_key_handle_t *) ctx->pk_ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100184 *pk_ctx = key;
185
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200186 return( 0 );
187}
188#endif /* MBEDTLS_USE_PSA_CRYPTO */
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100191/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200192 * Initialize an RSA-alt context
193 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200194int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
196 mbedtls_pk_rsa_alt_sign_func sign_func,
197 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_rsa_alt_context *rsa_alt;
200 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200201
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 PK_VALIDATE_RET( ctx != NULL );
203 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200205
206 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200207 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200208
209 ctx->pk_info = info;
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200212
213 rsa_alt->key = key;
214 rsa_alt->decrypt_func = decrypt_func;
215 rsa_alt->sign_func = sign_func;
216 rsa_alt->key_len_func = key_len_func;
217
218 return( 0 );
219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200221
222/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200223 * Tell if a PK can do the operations of the given type
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200226{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 /* A context with null pk_info is not set up yet and can't do anything.
228 * For backward compatibility, also accept NULL instead of a context
229 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200230 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200231 return( 0 );
232
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200233 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200234}
235
236/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200242
243 if( *hash_len != 0 )
244 return( 0 );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200247 return( -1 );
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200250 return( 0 );
251}
252
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200253#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200254/*
255 * Helper to set up a restart context if needed
256 */
257static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200258 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200259{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200260 /* Don't do anything if already set up or invalid */
261 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200262 return( 0 );
263
264 /* Should never happen when we're called */
265 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
266 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
267
268 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
269 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
270
271 ctx->pk_info = info;
272
273 return( 0 );
274}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200275#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200276
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200277/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200278 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200279 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200280int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
281 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200282 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200283 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200284 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200285{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 PK_VALIDATE_RET( ctx != NULL );
287 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
288 hash != NULL );
289 PK_VALIDATE_RET( sig != NULL );
290
291 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200292 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200294
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200295#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200296 /* optimization: use non-restartable version if restart disabled */
297 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200298 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200299 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200300 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200302
303 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
304 return( ret );
305
306 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
307 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
308
309 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
310 mbedtls_pk_restart_free( rs_ctx );
311
312 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200313 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200314#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200315 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200316#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200317
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200318 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200320
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200321 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200322 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200323}
324
325/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200326 * Verify a signature
327 */
328int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
329 const unsigned char *hash, size_t hash_len,
330 const unsigned char *sig, size_t sig_len )
331{
332 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
333 sig, sig_len, NULL ) );
334}
335
336/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200337 * Verify a signature with options
338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
340 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200341 const unsigned char *hash, size_t hash_len,
342 const unsigned char *sig, size_t sig_len )
343{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500344 PK_VALIDATE_RET( ctx != NULL );
345 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
346 hash != NULL );
347 PK_VALIDATE_RET( sig != NULL );
348
349 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 if( ! mbedtls_pk_can_do( ctx, type ) )
353 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Janos Follath24eed8d2019-11-22 13:21:35 +0000358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200360
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100361#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000362 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
363 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100364#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000365
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200366 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 if( sig_len < mbedtls_pk_get_len( ctx ) )
372 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
375 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200376 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200377 pss_opts->mgf1_hash_id,
378 pss_opts->expected_salt_len,
379 sig );
380 if( ret != 0 )
381 return( ret );
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( sig_len > mbedtls_pk_get_len( ctx ) )
384 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200385
386 return( 0 );
387#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000389#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200390 }
391
392 /* General case: no options */
393 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200397}
398
399/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200400 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200401 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200402int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
403 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200404 const unsigned char *hash, size_t hash_len,
405 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200406 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200407 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200408{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500409 PK_VALIDATE_RET( ctx != NULL );
410 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
411 hash != NULL );
412 PK_VALIDATE_RET( sig != NULL );
413
414 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200415 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200417
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200418#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200419 /* optimization: use non-restartable version if restart disabled */
420 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200421 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200422 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200423 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200425
426 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
427 return( ret );
428
429 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
430 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
431
432 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
433 mbedtls_pk_restart_free( rs_ctx );
434
435 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200436 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200437#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200438 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200439#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200440
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200441 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200443
444 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
445 sig, sig_len, f_rng, p_rng ) );
446}
447
448/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200449 * Make a signature
450 */
451int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
452 const unsigned char *hash, size_t hash_len,
453 unsigned char *sig, size_t *sig_len,
454 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
455{
456 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
457 sig, sig_len, f_rng, p_rng, NULL ) );
458}
459
460/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200461 * Decrypt message
462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200464 const unsigned char *input, size_t ilen,
465 unsigned char *output, size_t *olen, size_t osize,
466 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
467{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500468 PK_VALIDATE_RET( ctx != NULL );
469 PK_VALIDATE_RET( input != NULL || ilen == 0 );
470 PK_VALIDATE_RET( output != NULL || osize == 0 );
471 PK_VALIDATE_RET( olen != NULL );
472
473 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200475
476 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200478
479 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
480 output, olen, osize, f_rng, p_rng ) );
481}
482
483/*
484 * Encrypt message
485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200487 const unsigned char *input, size_t ilen,
488 unsigned char *output, size_t *olen, size_t osize,
489 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
490{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500491 PK_VALIDATE_RET( ctx != NULL );
492 PK_VALIDATE_RET( input != NULL || ilen == 0 );
493 PK_VALIDATE_RET( output != NULL || osize == 0 );
494 PK_VALIDATE_RET( olen != NULL );
495
496 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200498
499 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200501
502 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
503 output, olen, osize, f_rng, p_rng ) );
504}
505
506/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100507 * Check public-private key pair
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100510{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500511 PK_VALIDATE_RET( pub != NULL );
512 PK_VALIDATE_RET( prv != NULL );
513
514 if( pub->pk_info == NULL ||
Andrzej Kurekefed3232019-02-05 05:09:05 -0500515 prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100518 }
519
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200520 if( prv->pk_info->check_pair_func == NULL )
521 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( pub->pk_info->type != MBEDTLS_PK_RSA )
526 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100527 }
528 else
529 {
530 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100532 }
533
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100534 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100535}
536
537/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200538 * Get key size in bits
539 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200540size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200541{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500542 /* For backward compatibility, accept NULL or a context that
543 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200544 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200545 return( 0 );
546
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200547 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200548}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200549
550/*
551 * Export debug information
552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200554{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500555 PK_VALIDATE_RET( ctx != NULL );
556 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200558
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200559 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200561
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200562 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200563 return( 0 );
564}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200565
566/*
567 * Access the PK type name
568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200570{
571 if( ctx == NULL || ctx->pk_info == NULL )
572 return( "invalid PK" );
573
574 return( ctx->pk_info->name );
575}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200576
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200577/*
578 * Access the PK type
579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200581{
582 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200584
585 return( ctx->pk_info->type );
586}
587
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100588#if defined(MBEDTLS_USE_PSA_CRYPTO)
589/*
590 * Load the key to a PSA key slot,
591 * then turn the PK context into a wrapper for that key slot.
592 *
593 * Currently only works for EC private keys.
594 */
595int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
Gilles Peskined2d45c12019-05-27 14:53:13 +0200596 psa_key_handle_t *handle,
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100597 psa_algorithm_t hash_alg )
598{
599#if !defined(MBEDTLS_ECP_C)
600 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
601#else
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100602 const mbedtls_ecp_keypair *ec;
603 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
604 size_t d_len;
605 psa_ecc_curve_t curve_id;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200606 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100607 psa_key_type_t key_type;
Gilles Peskine4080c912019-12-18 20:43:03 +0100608 size_t bits;
Janos Follath24eed8d2019-11-22 13:21:35 +0000609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100610
611 /* export the private key material in the format PSA wants */
612 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
613 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
614
615 ec = mbedtls_pk_ec( *pk );
616 d_len = ( ec->grp.nbits + 7 ) / 8;
617 if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
618 return( ret );
619
Gilles Peskine4080c912019-12-18 20:43:03 +0100620 curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
621 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100622
Gilles Peskined2d45c12019-05-27 14:53:13 +0200623 /* prepare the key attributes */
624 psa_set_key_type( &attributes, key_type );
Gilles Peskine4080c912019-12-18 20:43:03 +0100625 psa_set_key_bits( &attributes, bits );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100626 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200627 psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(hash_alg) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100628
Gilles Peskined2d45c12019-05-27 14:53:13 +0200629 /* import private key into PSA */
630 if( PSA_SUCCESS != psa_import_key( &attributes, d, d_len, handle ) )
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100631 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
632
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100633 /* make PK context wrap the key slot */
634 mbedtls_pk_free( pk );
635 mbedtls_pk_init( pk );
636
Gilles Peskined2d45c12019-05-27 14:53:13 +0200637 return( mbedtls_pk_setup_opaque( pk, *handle ) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100638#endif /* MBEDTLS_ECP_C */
639}
640#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_PK_C */