blob: 93c57642da92fd7e6680fa28964286eb8466da4b [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
Andres AG72849872017-01-19 11:24:33 +000044#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010045#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020046
Gilles Peskinee97dc602018-12-19 00:51:38 +010047/* Parameter validation macros based on platform_util.h */
48#define PK_VALIDATE_RET( cond ) \
49 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
50#define PK_VALIDATE( cond ) \
51 MBEDTLS_INTERNAL_VALIDATE( cond )
52
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020057{
Gilles Peskinee97dc602018-12-19 00:51:38 +010058 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020059
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020060 ctx->pk_info = NULL;
61 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062}
63
64/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020068{
irwir2239a862018-06-12 18:25:09 +030069 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020070 return;
71
irwir2239a862018-06-12 18:25:09 +030072 if ( ctx->pk_info != NULL )
73 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020076}
77
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020078#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020079/*
80 * Initialize a restart context
81 */
82void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
83{
Gilles Peskinee97dc602018-12-19 00:51:38 +010084 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020085 ctx->pk_info = NULL;
86 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020087}
88
89/*
90 * Free the components of a restart context
91 */
92void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
93{
Gilles Peskine1f19fa62018-12-19 14:18:39 +010094 if( ctx == NULL || ctx->pk_info == NULL ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020095 ctx->pk_info->rs_free_func == NULL )
96 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020097 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020098 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200100 ctx->pk_info->rs_free_func( ctx->rs_ctx );
101
102 ctx->pk_info = NULL;
103 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200104}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200105#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200106
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200107/*
108 * Get pk_info structure from type
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111{
112 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_RSA_C)
114 case MBEDTLS_PK_RSA:
115 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 case MBEDTLS_PK_ECKEY_DH:
119 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#if defined(MBEDTLS_ECDSA_C)
122 case MBEDTLS_PK_ECDSA:
123 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200124#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +0100125#if defined(MBEDTLS_USE_TINYCRYPT)
126 case MBEDTLS_PK_ECKEY:
127 return( &mbedtls_uecc_eckey_info );
128#else /* MBEDTLS_USE_TINYCRYPT */
129#if defined(MBEDTLS_ECP_C)
130 case MBEDTLS_PK_ECKEY:
131 return( &mbedtls_eckey_info );
132#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300133#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200135 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200136 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200137 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200138}
139
140/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200141 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200142 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200143int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200144{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100145 PK_VALIDATE_RET( ctx != NULL );
146 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200149 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200150 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200151
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200152 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200153
154 return( 0 );
155}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100158/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200159 * Initialize an RSA-alt context
160 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200161int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
163 mbedtls_pk_rsa_alt_sign_func sign_func,
164 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_rsa_alt_context *rsa_alt;
167 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200168
Gilles Peskinee97dc602018-12-19 00:51:38 +0100169 PK_VALIDATE_RET( ctx != NULL );
170 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200172
173 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200174 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200175
176 ctx->pk_info = info;
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200179
180 rsa_alt->key = key;
181 rsa_alt->decrypt_func = decrypt_func;
182 rsa_alt->sign_func = sign_func;
183 rsa_alt->key_len_func = key_len_func;
184
185 return( 0 );
186}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200188
189/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200190 * Tell if a PK can do the operations of the given type
191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200193{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100194 /* A context with null pk_info is not set up yet and can't do anything.
195 * For backward compatibility, also accept NULL instead of a context
196 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200197 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200198 return( 0 );
199
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200200 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200201}
202
203/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200207{
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100208 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200209
210 if( *hash_len != 0 )
211 return( 0 );
212
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100213 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
214 MBEDTLS_MD_INVALID_HANDLE )
215 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200216 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100217 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200220 return( 0 );
221}
222
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200223#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200224/*
225 * Helper to set up a restart context if needed
226 */
227static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200228 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200229{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200230 /* Don't do anything if already set up or invalid */
231 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200232 return( 0 );
233
234 /* Should never happen when we're called */
235 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
236 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
237
238 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
239 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
240
241 ctx->pk_info = info;
242
243 return( 0 );
244}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200245#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200246
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200247/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200248 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200249 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200250int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
251 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200252 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200253 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200254 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200255{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100256 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100257 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
258 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100259 PK_VALIDATE_RET( sig != NULL );
260
261 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200262 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200264
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200265#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200266 /* optimization: use non-restartable version if restart disabled */
267 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200268 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200269 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200270 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200271 int ret;
272
273 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
274 return( ret );
275
276 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
277 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
278
279 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
280 mbedtls_pk_restart_free( rs_ctx );
281
282 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200283 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200284#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200285 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200286#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200287
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200288 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200290
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200291 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200292 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200293}
294
295/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200296 * Verify a signature
297 */
298int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
299 const unsigned char *hash, size_t hash_len,
300 const unsigned char *sig, size_t sig_len )
301{
302 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
303 sig, sig_len, NULL ) );
304}
305
306/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200307 * Verify a signature with options
308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
310 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200311 const unsigned char *hash, size_t hash_len,
312 const unsigned char *sig, size_t sig_len )
313{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100314 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100315 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
316 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100317 PK_VALIDATE_RET( sig != NULL );
318
319 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 if( ! mbedtls_pk_can_do( ctx, type ) )
323 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200328 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200330
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100331#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000332 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
333 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100334#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000335
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200336 if( options == 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 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( sig_len < mbedtls_pk_get_len( ctx ) )
342 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
345 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200346 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200347 pss_opts->mgf1_hash_id,
348 pss_opts->expected_salt_len,
349 sig );
350 if( ret != 0 )
351 return( ret );
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( sig_len > mbedtls_pk_get_len( ctx ) )
354 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200355
356 return( 0 );
357#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000359#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200360 }
361
362 /* General case: no options */
363 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200367}
368
369/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200370 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200371 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200372int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
373 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200374 const unsigned char *hash, size_t hash_len,
375 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200376 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200377 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200378{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100379 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100380 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
381 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100382 PK_VALIDATE_RET( sig != NULL );
383
384 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200385 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{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100438 PK_VALIDATE_RET( ctx != NULL );
439 PK_VALIDATE_RET( input != NULL || ilen == 0 );
440 PK_VALIDATE_RET( output != NULL || osize == 0 );
441 PK_VALIDATE_RET( olen != NULL );
442
443 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200445
446 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200448
449 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
450 output, olen, osize, f_rng, p_rng ) );
451}
452
453/*
454 * Encrypt message
455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200457 const unsigned char *input, size_t ilen,
458 unsigned char *output, size_t *olen, size_t osize,
459 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
460{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100461 PK_VALIDATE_RET( ctx != NULL );
462 PK_VALIDATE_RET( input != NULL || ilen == 0 );
463 PK_VALIDATE_RET( output != NULL || osize == 0 );
464 PK_VALIDATE_RET( olen != NULL );
465
466 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200468
469 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200471
472 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
473 output, olen, osize, f_rng, p_rng ) );
474}
475
476/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100477 * Check public-private key pair
478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100480{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100481 PK_VALIDATE_RET( pub != NULL );
482 PK_VALIDATE_RET( prv != NULL );
483
484 if( pub->pk_info == NULL ||
485 prv->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100486 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100489 }
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( pub->pk_info->type != MBEDTLS_PK_RSA )
494 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100495 }
496 else
497 {
498 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100500 }
501
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100502 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100503}
504
505/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200506 * Get key size in bits
507 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200508size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200509{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100510 /* For backward compatibility, accept NULL or a context that
511 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200512 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200513 return( 0 );
514
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200515 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200516}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200517
518/*
519 * Export debug information
520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200522{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100523 PK_VALIDATE_RET( ctx != NULL );
524 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200526
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200527 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200529
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200530 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200531 return( 0 );
532}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200533
534/*
535 * Access the PK type name
536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200538{
539 if( ctx == NULL || ctx->pk_info == NULL )
540 return( "invalid PK" );
541
542 return( ctx->pk_info->name );
543}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200544
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200545/*
546 * Access the PK type
547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200549{
550 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200552
553 return( ctx->pk_info->type );
554}
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#endif /* MBEDTLS_PK_C */