blob: 2658627c4793ad1956dcccc551f75463f8e5ea01 [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{
Paul Bakker66d5d072014-06-17 16:39:18 +020069 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020070 return;
71
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020072 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020073
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050074 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020075}
76
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020077#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020078/*
79 * Initialize a restart context
80 */
81void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
82{
Gilles Peskinee97dc602018-12-19 00:51:38 +010083 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020084 ctx->pk_info = NULL;
85 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020086}
87
88/*
89 * Free the components of a restart context
90 */
91void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
92{
Gilles Peskine1f19fa62018-12-19 14:18:39 +010093 if( ctx == NULL || ctx->pk_info == NULL ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020094 ctx->pk_info->rs_free_func == NULL )
95 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020096 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020097 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020099 ctx->pk_info->rs_free_func( ctx->rs_ctx );
100
101 ctx->pk_info = NULL;
102 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200104#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200105
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200106/*
107 * Get pk_info structure from type
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200110{
111 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_RSA_C)
113 case MBEDTLS_PK_RSA:
114 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_ECP_C)
117 case MBEDTLS_PK_ECKEY:
118 return( &mbedtls_eckey_info );
119 case MBEDTLS_PK_ECKEY_DH:
120 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_ECDSA_C)
123 case MBEDTLS_PK_ECDSA:
124 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200127 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200128 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200129 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200130}
131
132/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200133 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200134 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200135int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200136{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100137 PK_VALIDATE_RET( ctx != NULL );
138 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200140
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200141 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200142 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200143
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200144 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200145
146 return( 0 );
147}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100150/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200151 * Initialize an RSA-alt context
152 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200153int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
155 mbedtls_pk_rsa_alt_sign_func sign_func,
156 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_rsa_alt_context *rsa_alt;
159 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200160
Gilles Peskinee97dc602018-12-19 00:51:38 +0100161 PK_VALIDATE_RET( ctx != NULL );
162 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200164
165 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200166 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200167
168 ctx->pk_info = info;
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200171
172 rsa_alt->key = key;
173 rsa_alt->decrypt_func = decrypt_func;
174 rsa_alt->sign_func = sign_func;
175 rsa_alt->key_len_func = key_len_func;
176
177 return( 0 );
178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200180
181/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200182 * Tell if a PK can do the operations of the given type
183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200185{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100186 /* A context with null pk_info is not set up yet and can't do anything.
187 * For backward compatibility, also accept NULL instead of a context
188 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200189 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200190 return( 0 );
191
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200192 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200193}
194
195/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200201
202 if( *hash_len != 0 )
203 return( 0 );
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200206 return( -1 );
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200209 return( 0 );
210}
211
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200212#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200213/*
214 * Helper to set up a restart context if needed
215 */
216static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200217 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200218{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200219 /* Don't do anything if already set up or invalid */
220 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200221 return( 0 );
222
223 /* Should never happen when we're called */
224 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
225 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
226
227 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
228 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
229
230 ctx->pk_info = info;
231
232 return( 0 );
233}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200234#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200235
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200236/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200237 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200238 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200239int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
240 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200241 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200242 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200243 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200244{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100245 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100246 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
247 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100248 PK_VALIDATE_RET( sig != NULL );
249
250 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200251 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200253
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200254#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200255 /* optimization: use non-restartable version if restart disabled */
256 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200257 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200258 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200259 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200260 int ret;
261
262 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
263 return( ret );
264
265 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
266 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
267
268 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
269 mbedtls_pk_restart_free( rs_ctx );
270
271 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200272 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200273#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200274 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200275#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200276
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200277 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200279
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200280 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200281 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200282}
283
284/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200285 * Verify a signature
286 */
287int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
288 const unsigned char *hash, size_t hash_len,
289 const unsigned char *sig, size_t sig_len )
290{
291 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
292 sig, sig_len, NULL ) );
293}
294
295/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200296 * Verify a signature with options
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
299 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200300 const unsigned char *hash, size_t hash_len,
301 const unsigned char *sig, size_t sig_len )
302{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100303 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100304 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
305 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100306 PK_VALIDATE_RET( sig != NULL );
307
308 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( ! mbedtls_pk_can_do( ctx, type ) )
312 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200317 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200319
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100320#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000321 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
322 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100323#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000324
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200325 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( sig_len < mbedtls_pk_get_len( ctx ) )
331 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
334 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200335 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200336 pss_opts->mgf1_hash_id,
337 pss_opts->expected_salt_len,
338 sig );
339 if( ret != 0 )
340 return( ret );
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( sig_len > mbedtls_pk_get_len( ctx ) )
343 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200344
345 return( 0 );
346#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000348#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200349 }
350
351 /* General case: no options */
352 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200356}
357
358/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200359 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200360 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200361int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
362 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200363 const unsigned char *hash, size_t hash_len,
364 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200365 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200366 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200367{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100368 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +0100369 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
370 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100371 PK_VALIDATE_RET( sig != NULL );
372
373 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200374 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200376
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200377#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200378 /* optimization: use non-restartable version if restart disabled */
379 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200380 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200381 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200382 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200383 int ret;
384
385 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
386 return( ret );
387
388 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
389 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
390
391 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
392 mbedtls_pk_restart_free( rs_ctx );
393
394 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200395 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200396#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200397 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200398#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200399
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200400 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200402
403 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
404 sig, sig_len, f_rng, p_rng ) );
405}
406
407/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200408 * Make a signature
409 */
410int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
411 const unsigned char *hash, size_t hash_len,
412 unsigned char *sig, size_t *sig_len,
413 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
414{
415 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
416 sig, sig_len, f_rng, p_rng, NULL ) );
417}
418
419/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200420 * Decrypt message
421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200423 const unsigned char *input, size_t ilen,
424 unsigned char *output, size_t *olen, size_t osize,
425 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
426{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100427 PK_VALIDATE_RET( ctx != NULL );
428 PK_VALIDATE_RET( input != NULL || ilen == 0 );
429 PK_VALIDATE_RET( output != NULL || osize == 0 );
430 PK_VALIDATE_RET( olen != NULL );
431
432 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200434
435 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200437
438 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
439 output, olen, osize, f_rng, p_rng ) );
440}
441
442/*
443 * Encrypt message
444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200446 const unsigned char *input, size_t ilen,
447 unsigned char *output, size_t *olen, size_t osize,
448 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
449{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100450 PK_VALIDATE_RET( ctx != NULL );
451 PK_VALIDATE_RET( input != NULL || ilen == 0 );
452 PK_VALIDATE_RET( output != NULL || osize == 0 );
453 PK_VALIDATE_RET( olen != NULL );
454
455 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200457
458 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200460
461 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
462 output, olen, osize, f_rng, p_rng ) );
463}
464
465/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100466 * Check public-private key pair
467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100469{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100470 PK_VALIDATE_RET( pub != NULL );
471 PK_VALIDATE_RET( prv != NULL );
472
473 if( pub->pk_info == NULL ||
474 prv->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100475 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100478 }
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( pub->pk_info->type != MBEDTLS_PK_RSA )
483 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100484 }
485 else
486 {
487 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100489 }
490
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100491 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100492}
493
494/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200495 * Get key size in bits
496 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200497size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200498{
Gilles Peskine6af45ec2018-12-19 17:52:05 +0100499 /* For backward compatibility, accept NULL or a context that
500 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200501 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200502 return( 0 );
503
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200504 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200505}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200506
507/*
508 * Export debug information
509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200511{
Gilles Peskinee97dc602018-12-19 00:51:38 +0100512 PK_VALIDATE_RET( ctx != NULL );
513 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200515
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200516 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200518
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200519 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200520 return( 0 );
521}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200522
523/*
524 * Access the PK type name
525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200527{
528 if( ctx == NULL || ctx->pk_info == NULL )
529 return( "invalid PK" );
530
531 return( ctx->pk_info->name );
532}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200533
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200534/*
535 * Access the PK type
536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200538{
539 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200541
542 return( ctx->pk_info->type );
543}
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_PK_C */