blob: e9e56c029b4553843402c183653ae933108dd696 [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
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020057#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050059#include "mbedtls/platform_util.h"
Andres AG72849872017-01-19 11:24:33 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020069#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020070
Andres AG72849872017-01-19 11:24:33 +000071#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010072#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020073
Gilles Peskinee97dc602018-12-19 00:51:38 +010074/* Parameter validation macros based on platform_util.h */
75#define PK_VALIDATE_RET( cond ) \
76 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
77#define PK_VALIDATE( cond ) \
78 MBEDTLS_INTERNAL_VALIDATE( cond )
79
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020080/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020084{
Gilles Peskinee97dc602018-12-19 00:51:38 +010085 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020086
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020087 ctx->pk_info = NULL;
88 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020089}
90
91/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020095{
irwir2239a862018-06-12 18:25:09 +030096 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020097 return;
98
irwir2239a862018-06-12 18:25:09 +030099 if ( ctx->pk_info != NULL )
100 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +0200101
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500102 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200103}
104
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200105#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200106/*
107 * Initialize a restart context
108 */
109void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
110{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100111 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200112 ctx->pk_info = NULL;
113 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200114}
115
116/*
117 * Free the components of a restart context
118 */
119void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
120{
Gilles Peskine1f19fa62018-12-19 14:18:39 +0100121 if( ctx == NULL || ctx->pk_info == NULL ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200122 ctx->pk_info->rs_free_func == NULL )
123 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200124 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200125 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200126
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200127 ctx->pk_info->rs_free_func( ctx->rs_ctx );
128
129 ctx->pk_info = NULL;
130 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200131}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200132#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200133
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200134/*
135 * Get pk_info structure from type
136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200138{
139 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_RSA_C)
141 case MBEDTLS_PK_RSA:
142 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200143#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_ECP_C)
145 case MBEDTLS_PK_ECKEY:
146 return( &mbedtls_eckey_info );
147 case MBEDTLS_PK_ECKEY_DH:
148 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_ECDSA_C)
151 case MBEDTLS_PK_ECDSA:
152 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200155 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200156 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200157 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200158}
159
160/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200161 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200162 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200163int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200164{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100165 PK_VALIDATE_RET( ctx != NULL );
166 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200168
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200169 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200170 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200171
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200172 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200173
174 return( 0 );
175}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100178/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200179 * Initialize an RSA-alt context
180 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200181int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
183 mbedtls_pk_rsa_alt_sign_func sign_func,
184 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_rsa_alt_context *rsa_alt;
187 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200188
Gilles Peskinee97dc602018-12-19 00:51:38 +0100189 PK_VALIDATE_RET( ctx != NULL );
190 if( 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{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100214 /* A context with null pk_info is not set up yet and can't do anything.
215 * For backward compatibility, also accept NULL instead of a context
216 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200217 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200218 return( 0 );
219
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200220 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200221}
222
223/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200229
230 if( *hash_len != 0 )
231 return( 0 );
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200234 return( -1 );
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200237 return( 0 );
238}
239
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200240#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200241/*
242 * Helper to set up a restart context if needed
243 */
244static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200245 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200246{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200247 /* Don't do anything if already set up or invalid */
248 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200249 return( 0 );
250
251 /* Should never happen when we're called */
252 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
253 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
254
255 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
256 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
257
258 ctx->pk_info = info;
259
260 return( 0 );
261}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200262#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200263
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200264/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200265 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200266 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200267int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
268 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200269 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200270 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200271 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200272{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100273 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100274 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
275 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100276 PK_VALIDATE_RET( sig != NULL );
277
278 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200279 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200281
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200282#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200283 /* optimization: use non-restartable version if restart disabled */
284 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200285 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200286 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200287 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200288 int ret;
289
290 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
291 return( ret );
292
293 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
294 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
295
296 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
297 mbedtls_pk_restart_free( rs_ctx );
298
299 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200300 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200301#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200302 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200303#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200304
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200305 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200307
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200308 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200309 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200310}
311
312/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200313 * Verify a signature
314 */
315int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
316 const unsigned char *hash, size_t hash_len,
317 const unsigned char *sig, size_t sig_len )
318{
319 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
320 sig, sig_len, NULL ) );
321}
322
323/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200324 * Verify a signature with options
325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
327 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200328 const unsigned char *hash, size_t hash_len,
329 const unsigned char *sig, size_t sig_len )
330{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100331 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100332 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
333 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100334 PK_VALIDATE_RET( sig != NULL );
335
336 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ! mbedtls_pk_can_do( ctx, type ) )
340 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200345 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200347
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100348#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000349 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
350 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100351#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000352
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200353 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 if( sig_len < mbedtls_pk_get_len( ctx ) )
359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
362 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200363 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200364 pss_opts->mgf1_hash_id,
365 pss_opts->expected_salt_len,
366 sig );
367 if( ret != 0 )
368 return( ret );
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( sig_len > mbedtls_pk_get_len( ctx ) )
371 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200372
373 return( 0 );
374#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000376#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200377 }
378
379 /* General case: no options */
380 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200384}
385
386/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200387 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200388 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200389int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
390 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200391 const unsigned char *hash, size_t hash_len,
392 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200393 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200394 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200395{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100396 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100397 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
398 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100399 PK_VALIDATE_RET( sig != NULL );
400
401 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200402 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200404
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200405#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200406 /* optimization: use non-restartable version if restart disabled */
407 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200408 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200409 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200410 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200411 int ret;
412
413 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
414 return( ret );
415
416 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
417 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
418
419 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
420 mbedtls_pk_restart_free( rs_ctx );
421
422 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200423 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200424#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200425 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200426#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200427
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200428 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200430
431 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
432 sig, sig_len, f_rng, p_rng ) );
433}
434
435/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200436 * Make a signature
437 */
438int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
439 const unsigned char *hash, size_t hash_len,
440 unsigned char *sig, size_t *sig_len,
441 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
442{
443 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
444 sig, sig_len, f_rng, p_rng, NULL ) );
445}
446
447/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200448 * Decrypt message
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200451 const unsigned char *input, size_t ilen,
452 unsigned char *output, size_t *olen, size_t osize,
453 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
454{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100455 PK_VALIDATE_RET( ctx != NULL );
456 PK_VALIDATE_RET( input != NULL || ilen == 0 );
457 PK_VALIDATE_RET( output != NULL || osize == 0 );
458 PK_VALIDATE_RET( olen != NULL );
459
460 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200462
463 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200465
466 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
467 output, olen, osize, f_rng, p_rng ) );
468}
469
470/*
471 * Encrypt message
472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200474 const unsigned char *input, size_t ilen,
475 unsigned char *output, size_t *olen, size_t osize,
476 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
477{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100478 PK_VALIDATE_RET( ctx != NULL );
479 PK_VALIDATE_RET( input != NULL || ilen == 0 );
480 PK_VALIDATE_RET( output != NULL || osize == 0 );
481 PK_VALIDATE_RET( olen != NULL );
482
483 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200485
486 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200488
489 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
490 output, olen, osize, f_rng, p_rng ) );
491}
492
493/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100494 * Check public-private key pair
495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100497{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100498 PK_VALIDATE_RET( pub != NULL );
499 PK_VALIDATE_RET( prv != NULL );
500
501 if( pub->pk_info == NULL ||
502 prv->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100503 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100506 }
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( pub->pk_info->type != MBEDTLS_PK_RSA )
511 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100512 }
513 else
514 {
515 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100517 }
518
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100519 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100520}
521
522/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200523 * Get key size in bits
524 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200525size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200526{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100527 /* For backward compatibility, accept NULL or a context that
528 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200529 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200530 return( 0 );
531
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200532 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200533}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200534
535/*
536 * Export debug information
537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200539{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100540 PK_VALIDATE_RET( ctx != NULL );
541 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200543
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200544 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200546
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200547 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200548 return( 0 );
549}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200550
551/*
552 * Access the PK type name
553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200555{
556 if( ctx == NULL || ctx->pk_info == NULL )
557 return( "invalid PK" );
558
559 return( ctx->pk_info->name );
560}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200561
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200562/*
563 * Access the PK type
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200566{
567 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200569
570 return( ctx->pk_info->type );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#endif /* MBEDTLS_PK_C */