blob: 81cfdbfe80585d5ec0bd4e258f97d64e49cb5115 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020055#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020056
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050057#include "mbedtls/platform_util.h"
Andres AG72849872017-01-19 11:24:33 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020067#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020068
Andres AG72849872017-01-19 11:24:33 +000069#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010070#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020071
Gilles Peskinee97dc602018-12-19 00:51:38 +010072/* Parameter validation macros based on platform_util.h */
73#define PK_VALIDATE_RET( cond ) \
74 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
75#define PK_VALIDATE( cond ) \
76 MBEDTLS_INTERNAL_VALIDATE( cond )
77
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020078/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020082{
Gilles Peskinee97dc602018-12-19 00:51:38 +010083 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020084
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020085 ctx->pk_info = NULL;
86 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020087}
88
89/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020093{
irwir2239a862018-06-12 18:25:09 +030094 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020095 return;
96
irwir2239a862018-06-12 18:25:09 +030097 if ( ctx->pk_info != NULL )
98 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020099
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500100 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200101}
102
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200103#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200104/*
105 * Initialize a restart context
106 */
107void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
108{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100109 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200110 ctx->pk_info = NULL;
111 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200112}
113
114/*
115 * Free the components of a restart context
116 */
117void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
118{
Gilles Peskine1f19fa62018-12-19 14:18:39 +0100119 if( ctx == NULL || ctx->pk_info == NULL ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200120 ctx->pk_info->rs_free_func == NULL )
121 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200122 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200123 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200124
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200125 ctx->pk_info->rs_free_func( ctx->rs_ctx );
126
127 ctx->pk_info = NULL;
128 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200129}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200130#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200131
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132/*
133 * Get pk_info structure from type
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200136{
137 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_RSA_C)
139 case MBEDTLS_PK_RSA:
140 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200141#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_ECP_C)
143 case MBEDTLS_PK_ECKEY:
144 return( &mbedtls_eckey_info );
145 case MBEDTLS_PK_ECKEY_DH:
146 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_ECDSA_C)
149 case MBEDTLS_PK_ECDSA:
150 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200153 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200154 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200155 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200156}
157
158/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200159 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200160 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200161int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200162{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100163 PK_VALIDATE_RET( ctx != NULL );
164 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200166
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200167 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200168 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200169
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200170 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200171
172 return( 0 );
173}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100176/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200177 * Initialize an RSA-alt context
178 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200179int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
181 mbedtls_pk_rsa_alt_sign_func sign_func,
182 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_rsa_alt_context *rsa_alt;
185 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200186
Gilles Peskinee97dc602018-12-19 00:51:38 +0100187 PK_VALIDATE_RET( ctx != NULL );
188 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200190
191 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200192 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200193
194 ctx->pk_info = info;
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200197
198 rsa_alt->key = key;
199 rsa_alt->decrypt_func = decrypt_func;
200 rsa_alt->sign_func = sign_func;
201 rsa_alt->key_len_func = key_len_func;
202
203 return( 0 );
204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200206
207/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200208 * Tell if a PK can do the operations of the given type
209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200211{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100212 /* A context with null pk_info is not set up yet and can't do anything.
213 * For backward compatibility, also accept NULL instead of a context
214 * pointer. */
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{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100271 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100272 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
273 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100274 PK_VALIDATE_RET( sig != NULL );
275
276 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200277 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200279
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200280#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200281 /* optimization: use non-restartable version if restart disabled */
282 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200283 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200284 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200285 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200286 int ret;
287
288 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
289 return( ret );
290
291 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
292 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
293
294 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
295 mbedtls_pk_restart_free( rs_ctx );
296
297 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200298 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200299#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200300 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200301#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200302
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200303 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200305
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200306 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200307 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200308}
309
310/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200311 * Verify a signature
312 */
313int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
314 const unsigned char *hash, size_t hash_len,
315 const unsigned char *sig, size_t sig_len )
316{
317 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
318 sig, sig_len, NULL ) );
319}
320
321/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200322 * Verify a signature with options
323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
325 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200326 const unsigned char *hash, size_t hash_len,
327 const unsigned char *sig, size_t sig_len )
328{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100329 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100330 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
331 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100332 PK_VALIDATE_RET( sig != NULL );
333
334 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( ! mbedtls_pk_can_do( ctx, type ) )
338 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200343 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200345
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100346#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000347 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
348 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100349#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000350
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200351 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( sig_len < mbedtls_pk_get_len( ctx ) )
357 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
360 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200361 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200362 pss_opts->mgf1_hash_id,
363 pss_opts->expected_salt_len,
364 sig );
365 if( ret != 0 )
366 return( ret );
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( sig_len > mbedtls_pk_get_len( ctx ) )
369 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200370
371 return( 0 );
372#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000374#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200375 }
376
377 /* General case: no options */
378 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200382}
383
384/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200385 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200386 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200387int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
388 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200389 const unsigned char *hash, size_t hash_len,
390 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200391 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200392 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200393{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100394 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100395 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
396 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100397 PK_VALIDATE_RET( sig != NULL );
398
399 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200400 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200402
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200403#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200404 /* optimization: use non-restartable version if restart disabled */
405 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200406 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200407 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200408 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200409 int ret;
410
411 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
412 return( ret );
413
414 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
415 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
416
417 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
418 mbedtls_pk_restart_free( rs_ctx );
419
420 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200421 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200422#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200423 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200424#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200425
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200426 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200428
429 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
430 sig, sig_len, f_rng, p_rng ) );
431}
432
433/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200434 * Make a signature
435 */
436int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
437 const unsigned char *hash, size_t hash_len,
438 unsigned char *sig, size_t *sig_len,
439 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
440{
441 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
442 sig, sig_len, f_rng, p_rng, NULL ) );
443}
444
445/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200446 * Decrypt message
447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200449 const unsigned char *input, size_t ilen,
450 unsigned char *output, size_t *olen, size_t osize,
451 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
452{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100453 PK_VALIDATE_RET( ctx != NULL );
454 PK_VALIDATE_RET( input != NULL || ilen == 0 );
455 PK_VALIDATE_RET( output != NULL || osize == 0 );
456 PK_VALIDATE_RET( olen != NULL );
457
458 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200460
461 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200463
464 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
465 output, olen, osize, f_rng, p_rng ) );
466}
467
468/*
469 * Encrypt message
470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200472 const unsigned char *input, size_t ilen,
473 unsigned char *output, size_t *olen, size_t osize,
474 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
475{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100476 PK_VALIDATE_RET( ctx != NULL );
477 PK_VALIDATE_RET( input != NULL || ilen == 0 );
478 PK_VALIDATE_RET( output != NULL || osize == 0 );
479 PK_VALIDATE_RET( olen != NULL );
480
481 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200483
484 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200486
487 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
488 output, olen, osize, f_rng, p_rng ) );
489}
490
491/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100492 * Check public-private key pair
493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100495{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100496 PK_VALIDATE_RET( pub != NULL );
497 PK_VALIDATE_RET( prv != NULL );
498
499 if( pub->pk_info == NULL ||
500 prv->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100501 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100504 }
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 if( pub->pk_info->type != MBEDTLS_PK_RSA )
509 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100510 }
511 else
512 {
513 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100515 }
516
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100517 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100518}
519
520/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200521 * Get key size in bits
522 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200523size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200524{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100525 /* For backward compatibility, accept NULL or a context that
526 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200527 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200528 return( 0 );
529
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200530 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200531}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200532
533/*
534 * Export debug information
535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200537{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100538 PK_VALIDATE_RET( ctx != NULL );
539 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200541
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200542 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200544
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200545 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200546 return( 0 );
547}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200548
549/*
550 * Access the PK type name
551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200553{
554 if( ctx == NULL || ctx->pk_info == NULL )
555 return( "invalid PK" );
556
557 return( ctx->pk_info->name );
558}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200559
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200560/*
561 * Access the PK type
562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200564{
565 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200567
568 return( ctx->pk_info->type );
569}
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_PK_C */