blob: 989ed095b2ecb0efbfc62e54cf5c4ad44ccac8c4 [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
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020051/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020055{
56 if( ctx == NULL )
57 return;
58
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{
Paul Bakker66d5d072014-06-17 16:39:18 +020068 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069 return;
70
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020071 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020072
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050073 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020074}
75
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020076#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020077/*
78 * Initialize a restart context
79 */
80void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
81{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020082 ctx->pk_info = NULL;
83 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020084}
85
86/*
87 * Free the components of a restart context
88 */
89void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
90{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020091 if( ctx == NULL || ctx->pk_info == NULL ||
92 ctx->pk_info->rs_free_func == NULL )
93 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020094 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020095 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020096
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020097 ctx->pk_info->rs_free_func( ctx->rs_ctx );
98
99 ctx->pk_info = NULL;
100 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200102#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200104/*
105 * Get pk_info structure from type
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200108{
109 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_RSA_C)
111 case MBEDTLS_PK_RSA:
112 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_ECP_C)
115 case MBEDTLS_PK_ECKEY:
116 return( &mbedtls_eckey_info );
117 case MBEDTLS_PK_ECKEY_DH:
118 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_ECDSA_C)
121 case MBEDTLS_PK_ECDSA:
122 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200125 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200126 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200127 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200128}
129
130/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200131 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200132 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200133int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200134{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200135 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200137
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200138 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200139 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200140
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200141 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200142
143 return( 0 );
144}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200145
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200146#if defined(MBEDTLS_USE_PSA_CRYPTO)
147/*
148 * Initialise a PSA-wrapping context
149 */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100150int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, const psa_key_slot_t key )
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200151{
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100152 const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100153 psa_key_slot_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100154 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200155
156 if( ctx == NULL || ctx->pk_info != NULL )
157 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
158
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100159 if( PSA_SUCCESS != psa_get_key_information( key, &type, NULL ) )
160 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
161
162 /* Current implementation of can_do() relies on this. */
163 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
164 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) ;
165
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200166 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
167 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
168
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200169 ctx->pk_info = info;
170
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100171 pk_ctx = (psa_key_slot_t *) ctx->pk_ctx;
172 *pk_ctx = key;
173
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200174 return( 0 );
175}
176#endif /* MBEDTLS_USE_PSA_CRYPTO */
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100179/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200180 * Initialize an RSA-alt context
181 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200182int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
184 mbedtls_pk_rsa_alt_sign_func sign_func,
185 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_rsa_alt_context *rsa_alt;
188 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200189
190 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200192
193 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200194 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200195
196 ctx->pk_info = info;
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200199
200 rsa_alt->key = key;
201 rsa_alt->decrypt_func = decrypt_func;
202 rsa_alt->sign_func = sign_func;
203 rsa_alt->key_len_func = key_len_func;
204
205 return( 0 );
206}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200208
209/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200210 * Tell if a PK can do the operations of the given type
211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200213{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200214 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200215 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200216 return( 0 );
217
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200218 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200219}
220
221/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200227
228 if( *hash_len != 0 )
229 return( 0 );
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200232 return( -1 );
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200235 return( 0 );
236}
237
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200238#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200239/*
240 * Helper to set up a restart context if needed
241 */
242static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200243 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200244{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200245 /* Don't do anything if already set up or invalid */
246 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200247 return( 0 );
248
249 /* Should never happen when we're called */
250 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
251 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
252
253 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
254 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
255
256 ctx->pk_info = info;
257
258 return( 0 );
259}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200260#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200261
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200262/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200263 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200264 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200265int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
266 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200267 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200268 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200269 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200270{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200271 if( ctx == NULL || ctx->pk_info == NULL ||
272 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200274
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200275#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200276 /* optimization: use non-restartable version if restart disabled */
277 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200278 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200279 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200280 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200281 int ret;
282
283 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
284 return( ret );
285
286 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
287 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
288
289 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
290 mbedtls_pk_restart_free( rs_ctx );
291
292 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200293 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200294#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200295 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200296#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200297
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200298 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200300
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200301 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200302 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200303}
304
305/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200306 * Verify a signature
307 */
308int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
309 const unsigned char *hash, size_t hash_len,
310 const unsigned char *sig, size_t sig_len )
311{
312 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
313 sig, sig_len, NULL ) );
314}
315
316/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200317 * Verify a signature with options
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
320 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200321 const unsigned char *hash, size_t hash_len,
322 const unsigned char *sig, size_t sig_len )
323{
324 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( ! mbedtls_pk_can_do( ctx, type ) )
328 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200333 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200335
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100336#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000337 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
338 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100339#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000340
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200341 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( sig_len < mbedtls_pk_get_len( ctx ) )
347 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
350 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200351 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200352 pss_opts->mgf1_hash_id,
353 pss_opts->expected_salt_len,
354 sig );
355 if( ret != 0 )
356 return( ret );
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 if( sig_len > mbedtls_pk_get_len( ctx ) )
359 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200360
361 return( 0 );
362#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000364#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200365 }
366
367 /* General case: no options */
368 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200372}
373
374/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200375 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200376 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200377int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
378 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200379 const unsigned char *hash, size_t hash_len,
380 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200381 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200382 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200383{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200384 if( ctx == NULL || ctx->pk_info == NULL ||
385 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200387
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200388#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200389 /* optimization: use non-restartable version if restart disabled */
390 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200391 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200392 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200393 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200394 int ret;
395
396 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
397 return( ret );
398
399 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
400 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
401
402 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
403 mbedtls_pk_restart_free( rs_ctx );
404
405 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200406 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200407#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200408 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200409#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200410
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200411 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200413
414 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
415 sig, sig_len, f_rng, p_rng ) );
416}
417
418/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200419 * Make a signature
420 */
421int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
422 const unsigned char *hash, size_t hash_len,
423 unsigned char *sig, size_t *sig_len,
424 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
425{
426 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
427 sig, sig_len, f_rng, p_rng, NULL ) );
428}
429
430/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200431 * Decrypt message
432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200434 const unsigned char *input, size_t ilen,
435 unsigned char *output, size_t *olen, size_t osize,
436 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
437{
438 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200440
441 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200443
444 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
445 output, olen, osize, f_rng, p_rng ) );
446}
447
448/*
449 * Encrypt message
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200452 const unsigned char *input, size_t ilen,
453 unsigned char *output, size_t *olen, size_t osize,
454 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
455{
456 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200458
459 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200461
462 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
463 output, olen, osize, f_rng, p_rng ) );
464}
465
466/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100467 * Check public-private key pair
468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100470{
471 if( pub == NULL || pub->pk_info == NULL ||
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200472 prv == NULL || prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100475 }
476
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200477 if( prv->pk_info->check_pair_func == NULL )
478 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( pub->pk_info->type != MBEDTLS_PK_RSA )
483 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100484 }
485 else
486 {
487 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100489 }
490
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100491 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100492}
493
494/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200495 * Get key size in bits
496 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200497size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200498{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200499 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200500 return( 0 );
501
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200502 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200503}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200504
505/*
506 * Export debug information
507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200509{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200510 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200512
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200513 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200515
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200516 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200517 return( 0 );
518}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200519
520/*
521 * Access the PK type name
522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200524{
525 if( ctx == NULL || ctx->pk_info == NULL )
526 return( "invalid PK" );
527
528 return( ctx->pk_info->name );
529}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200530
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200531/*
532 * Access the PK type
533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200535{
536 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200538
539 return( ctx->pk_info->type );
540}
541
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100542#if defined(MBEDTLS_USE_PSA_CRYPTO)
543/*
544 * Load the key to a PSA key slot,
545 * then turn the PK context into a wrapper for that key slot.
546 *
547 * Currently only works for EC private keys.
548 */
549int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
550 psa_key_slot_t *slot,
551 psa_algorithm_t hash_alg )
552{
553#if !defined(MBEDTLS_ECP_C)
554 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
555#else
556 psa_key_slot_t key;
557 const mbedtls_ecp_keypair *ec;
558 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
559 size_t d_len;
560 psa_ecc_curve_t curve_id;
561 psa_key_type_t key_type;
562 psa_key_policy_t policy;
563 int ret;
564
565 /* export the private key material in the format PSA wants */
566 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
567 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
568
569 ec = mbedtls_pk_ec( *pk );
570 d_len = ( ec->grp.nbits + 7 ) / 8;
571 if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
572 return( ret );
573
574 curve_id = mbedtls_ecp_curve_info_from_grp_id( ec->grp.id )->tls_id;
575
576 /* find a free key slot */
577 if( PSA_SUCCESS != mbedtls_psa_get_free_key_slot( &key ) )
578 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
579
580 /* set policy */
581 psa_key_policy_init( &policy );
582 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN,
583 PSA_ALG_ECDSA(hash_alg) );
584 if( PSA_SUCCESS != psa_set_key_policy( key, &policy ) )
585 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
586
587 /* import private key in slot */
588 key_type = PSA_KEY_TYPE_ECC_KEYPAIR(curve_id);
589 if( PSA_SUCCESS != psa_import_key( key, key_type, d, d_len ) )
590 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
591
592 /* remember slot number to be destroyed later by caller */
593 *slot = key;
594
595 /* make PK context wrap the key slot */
596 mbedtls_pk_free( pk );
597 mbedtls_pk_init( pk );
598
599 return( mbedtls_pk_setup_opaque( pk, key ) );
600#endif /* MBEDTLS_ECP_C */
601}
602#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_PK_C */