blob: e93ccfdab9a367c4df25f78a0025587c0bdf40ec [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"
Andres AG72849872017-01-19 11:24:33 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020042#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020043
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010044#if defined(MBEDTLS_USE_PSA_CRYPTO)
45#include "mbedtls/psa_util.h"
46#endif
47
Andres AG72849872017-01-19 11:24:33 +000048#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010049#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020050
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050051/* Parameter validation macros based on platform_util.h */
52#define PK_VALIDATE_RET( cond ) \
53 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
54#define PK_VALIDATE( cond ) \
55 MBEDTLS_INTERNAL_VALIDATE( cond )
56
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020057/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050062 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020063
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020064 ctx->pk_info = NULL;
65 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066}
67
68/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020072{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020074 return;
75
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076 if ( ctx->pk_info != NULL )
77 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020078
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050079 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020080}
81
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020082#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020083/*
84 * Initialize a restart context
85 */
86void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
87{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020089 ctx->pk_info = NULL;
90 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020091}
92
93/*
94 * Free the components of a restart context
95 */
96void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
97{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020098 if( ctx == NULL || ctx->pk_info == NULL ||
99 ctx->pk_info->rs_free_func == NULL )
100 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200102 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200104 ctx->pk_info->rs_free_func( ctx->rs_ctx );
105
106 ctx->pk_info = NULL;
107 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200108}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200109#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200110
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111/*
112 * Get pk_info structure from type
113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200115{
116 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_RSA_C)
118 case MBEDTLS_PK_RSA:
119 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_ECP_C)
122 case MBEDTLS_PK_ECKEY:
123 return( &mbedtls_eckey_info );
124 case MBEDTLS_PK_ECKEY_DH:
125 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if defined(MBEDTLS_ECDSA_C)
128 case MBEDTLS_PK_ECDSA:
129 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200133 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200134 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135}
136
137/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200138 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200139 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200140int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 PK_VALIDATE_RET( ctx != NULL );
143 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200145
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200146 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200147 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200149 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150
151 return( 0 );
152}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200153
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200154#if defined(MBEDTLS_USE_PSA_CRYPTO)
155/*
156 * Initialise a PSA-wrapping context
157 */
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500158int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, const psa_key_handle_t key )
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200159{
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100160 const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500162 psa_key_handle_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100163 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200164
165 if( ctx == NULL || ctx->pk_info != NULL )
166 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
167
Gilles Peskined2d45c12019-05-27 14:53:13 +0200168 if( PSA_SUCCESS != psa_get_key_attributes( key, &attributes ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100169 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200170 type = psa_get_key_type( &attributes );
171 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100172
173 /* Current implementation of can_do() relies on this. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200174 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100175 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) ;
176
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200177 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
178 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
179
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200180 ctx->pk_info = info;
181
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500182 pk_ctx = (psa_key_handle_t *) ctx->pk_ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100183 *pk_ctx = key;
184
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200185 return( 0 );
186}
187#endif /* MBEDTLS_USE_PSA_CRYPTO */
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100190/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200191 * Initialize an RSA-alt context
192 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200193int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
195 mbedtls_pk_rsa_alt_sign_func sign_func,
196 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_rsa_alt_context *rsa_alt;
199 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200200
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 PK_VALIDATE_RET( ctx != NULL );
202 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200204
205 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200206 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200207
208 ctx->pk_info = info;
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200211
212 rsa_alt->key = key;
213 rsa_alt->decrypt_func = decrypt_func;
214 rsa_alt->sign_func = sign_func;
215 rsa_alt->key_len_func = key_len_func;
216
217 return( 0 );
218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200220
221/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200222 * Tell if a PK can do the operations of the given type
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200225{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226 /* A context with null pk_info is not set up yet and can't do anything.
227 * For backward compatibility, also accept NULL instead of a context
228 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200229 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200230 return( 0 );
231
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200232 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200233}
234
235/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200241
242 if( *hash_len != 0 )
243 return( 0 );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200246 return( -1 );
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200249 return( 0 );
250}
251
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200252#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200253/*
254 * Helper to set up a restart context if needed
255 */
256static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200257 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200258{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200259 /* Don't do anything if already set up or invalid */
260 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200261 return( 0 );
262
263 /* Should never happen when we're called */
264 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
265 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
266
267 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
268 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
269
270 ctx->pk_info = info;
271
272 return( 0 );
273}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200274#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200275
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200276/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200277 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200278 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200279int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
280 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200281 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200282 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200283 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200284{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285 PK_VALIDATE_RET( ctx != NULL );
286 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
287 hash != NULL );
288 PK_VALIDATE_RET( sig != NULL );
289
290 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200291 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200293
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200294#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200295 /* optimization: use non-restartable version if restart disabled */
296 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200297 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200298 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200299 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200300 int ret;
301
302 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
303 return( ret );
304
305 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
306 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
307
308 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
309 mbedtls_pk_restart_free( rs_ctx );
310
311 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200312 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200313#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200314 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200315#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200316
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200317 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200319
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200320 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200321 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200322}
323
324/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200325 * Verify a signature
326 */
327int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
328 const unsigned char *hash, size_t hash_len,
329 const unsigned char *sig, size_t sig_len )
330{
331 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
332 sig, sig_len, NULL ) );
333}
334
335/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200336 * Verify a signature with options
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
339 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200340 const unsigned char *hash, size_t hash_len,
341 const unsigned char *sig, size_t sig_len )
342{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500343 PK_VALIDATE_RET( ctx != NULL );
344 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
345 hash != NULL );
346 PK_VALIDATE_RET( sig != NULL );
347
348 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ! mbedtls_pk_can_do( ctx, type ) )
352 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200357 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200359
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100360#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000361 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
362 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100363#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000364
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200365 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( sig_len < mbedtls_pk_get_len( ctx ) )
371 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
374 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200375 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200376 pss_opts->mgf1_hash_id,
377 pss_opts->expected_salt_len,
378 sig );
379 if( ret != 0 )
380 return( ret );
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( sig_len > mbedtls_pk_get_len( ctx ) )
383 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200384
385 return( 0 );
386#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000388#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200389 }
390
391 /* General case: no options */
392 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200396}
397
398/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200399 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200400 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200401int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
402 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200403 const unsigned char *hash, size_t hash_len,
404 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200405 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200406 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200407{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500408 PK_VALIDATE_RET( ctx != NULL );
409 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
410 hash != NULL );
411 PK_VALIDATE_RET( sig != NULL );
412
413 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200414 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200416
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200417#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200418 /* optimization: use non-restartable version if restart disabled */
419 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200420 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200421 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200422 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200423 int ret;
424
425 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
426 return( ret );
427
428 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
429 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
430
431 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
432 mbedtls_pk_restart_free( rs_ctx );
433
434 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200435 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200436#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200437 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200438#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200439
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200440 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200442
443 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
444 sig, sig_len, f_rng, p_rng ) );
445}
446
447/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200448 * Make a signature
449 */
450int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
451 const unsigned char *hash, size_t hash_len,
452 unsigned char *sig, size_t *sig_len,
453 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
454{
455 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
456 sig, sig_len, f_rng, p_rng, NULL ) );
457}
458
459/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200460 * Decrypt message
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200463 const unsigned char *input, size_t ilen,
464 unsigned char *output, size_t *olen, size_t osize,
465 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
466{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500467 PK_VALIDATE_RET( ctx != NULL );
468 PK_VALIDATE_RET( input != NULL || ilen == 0 );
469 PK_VALIDATE_RET( output != NULL || osize == 0 );
470 PK_VALIDATE_RET( olen != NULL );
471
472 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200474
475 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200477
478 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
479 output, olen, osize, f_rng, p_rng ) );
480}
481
482/*
483 * Encrypt message
484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200486 const unsigned char *input, size_t ilen,
487 unsigned char *output, size_t *olen, size_t osize,
488 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
489{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500490 PK_VALIDATE_RET( ctx != NULL );
491 PK_VALIDATE_RET( input != NULL || ilen == 0 );
492 PK_VALIDATE_RET( output != NULL || osize == 0 );
493 PK_VALIDATE_RET( olen != NULL );
494
495 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200497
498 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200500
501 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
502 output, olen, osize, f_rng, p_rng ) );
503}
504
505/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100506 * Check public-private key pair
507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100509{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500510 PK_VALIDATE_RET( pub != NULL );
511 PK_VALIDATE_RET( prv != NULL );
512
513 if( pub->pk_info == NULL ||
Andrzej Kurekefed3232019-02-05 05:09:05 -0500514 prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100517 }
518
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200519 if( prv->pk_info->check_pair_func == NULL )
520 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( pub->pk_info->type != MBEDTLS_PK_RSA )
525 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100526 }
527 else
528 {
529 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100531 }
532
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100533 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100534}
535
536/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200537 * Get key size in bits
538 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200539size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200540{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500541 /* For backward compatibility, accept NULL or a context that
542 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200543 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200544 return( 0 );
545
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200546 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200547}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200548
549/*
550 * Export debug information
551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200553{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500554 PK_VALIDATE_RET( ctx != NULL );
555 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200557
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200558 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200560
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200561 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200562 return( 0 );
563}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200564
565/*
566 * Access the PK type name
567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200569{
570 if( ctx == NULL || ctx->pk_info == NULL )
571 return( "invalid PK" );
572
573 return( ctx->pk_info->name );
574}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200575
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200576/*
577 * Access the PK type
578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200580{
581 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200583
584 return( ctx->pk_info->type );
585}
586
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100587#if defined(MBEDTLS_USE_PSA_CRYPTO)
588/*
589 * Load the key to a PSA key slot,
590 * then turn the PK context into a wrapper for that key slot.
591 *
592 * Currently only works for EC private keys.
593 */
594int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
Gilles Peskined2d45c12019-05-27 14:53:13 +0200595 psa_key_handle_t *handle,
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100596 psa_algorithm_t hash_alg )
597{
598#if !defined(MBEDTLS_ECP_C)
599 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
600#else
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100601 const mbedtls_ecp_keypair *ec;
602 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
603 size_t d_len;
604 psa_ecc_curve_t curve_id;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100606 psa_key_type_t key_type;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100607 int ret;
608
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
618 curve_id = mbedtls_ecp_curve_info_from_grp_id( ec->grp.id )->tls_id;
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200619 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Andrzej Kurek62594a82019-01-14 05:14:18 -0500620 mbedtls_psa_parse_tls_ecc_group ( curve_id ) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100621
Gilles Peskined2d45c12019-05-27 14:53:13 +0200622 /* prepare the key attributes */
623 psa_set_key_type( &attributes, key_type );
624 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
625 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 */
628 if( PSA_SUCCESS != psa_import_key( &attributes, d, d_len, handle ) )
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
Gilles Peskined2d45c12019-05-27 14:53:13 +0200635 return( mbedtls_pk_setup_opaque( pk, *handle ) );
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 */