blob: c34ab7e02b191d7fea0c0996bf64d9c6b577829e [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
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020047/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020051{
52 if( ctx == NULL )
53 return;
54
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020055 ctx->pk_info = NULL;
56 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020057}
58
59/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020063{
Paul Bakker66d5d072014-06-17 16:39:18 +020064 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020065 return;
66
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020067 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020068
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050069 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020070}
71
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020072#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020073/*
74 * Initialize a restart context
75 */
76void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
77{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020078 ctx->pk_info = NULL;
79 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020080}
81
82/*
83 * Free the components of a restart context
84 */
85void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
86{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020087 if( ctx == NULL || ctx->pk_info == NULL ||
88 ctx->pk_info->rs_free_func == NULL )
89 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020090 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020091 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020093 ctx->pk_info->rs_free_func( ctx->rs_ctx );
94
95 ctx->pk_info = NULL;
96 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020097}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020098#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200100/*
101 * Get pk_info structure from type
102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200104{
105 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_RSA_C)
107 case MBEDTLS_PK_RSA:
108 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_ECP_C)
111 case MBEDTLS_PK_ECKEY:
112 return( &mbedtls_eckey_info );
113 case MBEDTLS_PK_ECKEY_DH:
114 return( &mbedtls_eckeydh_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_ECDSA_C)
117 case MBEDTLS_PK_ECDSA:
118 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200121 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200122 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200123 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200124}
125
126/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200127 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200128 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200129int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200130{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200131 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200133
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200134 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200135 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200136
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200137 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200138
139 return( 0 );
140}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200141
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200142#if defined(MBEDTLS_USE_PSA_CRYPTO)
143/*
144 * Initialise a PSA-wrapping context
145 */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100146int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, const psa_key_slot_t key )
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200147{
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +0100148 const mbedtls_pk_info_t * const info = &mbedtls_pk_opaque_info;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100149 psa_key_slot_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100150 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200151
152 if( ctx == NULL || ctx->pk_info != NULL )
153 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
154
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100155 if( PSA_SUCCESS != psa_get_key_information( key, &type, NULL ) )
156 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
157
158 /* Current implementation of can_do() relies on this. */
159 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
160 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) ;
161
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200162 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
163 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
164
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200165 ctx->pk_info = info;
166
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100167 pk_ctx = (psa_key_slot_t *) ctx->pk_ctx;
168 *pk_ctx = key;
169
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200170 return( 0 );
171}
172#endif /* MBEDTLS_USE_PSA_CRYPTO */
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100175/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200176 * Initialize an RSA-alt context
177 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200178int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
180 mbedtls_pk_rsa_alt_sign_func sign_func,
181 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200182{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_rsa_alt_context *rsa_alt;
184 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200185
186 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200188
189 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200190 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200191
192 ctx->pk_info = info;
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200195
196 rsa_alt->key = key;
197 rsa_alt->decrypt_func = decrypt_func;
198 rsa_alt->sign_func = sign_func;
199 rsa_alt->key_len_func = key_len_func;
200
201 return( 0 );
202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200204
205/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200206 * Tell if a PK can do the operations of the given type
207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200209{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200210 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200211 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200212 return( 0 );
213
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200214 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200215}
216
217/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200223
224 if( *hash_len != 0 )
225 return( 0 );
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200228 return( -1 );
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200231 return( 0 );
232}
233
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200234#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200235/*
236 * Helper to set up a restart context if needed
237 */
238static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200239 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200240{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200241 /* Don't do anything if already set up or invalid */
242 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200243 return( 0 );
244
245 /* Should never happen when we're called */
246 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
247 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
248
249 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
250 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
251
252 ctx->pk_info = info;
253
254 return( 0 );
255}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200256#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200257
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200258/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200259 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200260 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200261int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
262 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200263 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200264 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200265 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200266{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200267 if( ctx == NULL || ctx->pk_info == NULL ||
268 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200270
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200271#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200272 /* optimization: use non-restartable version if restart disabled */
273 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200274 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200275 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200276 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200277 int ret;
278
279 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
280 return( ret );
281
282 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
283 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
284
285 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
286 mbedtls_pk_restart_free( rs_ctx );
287
288 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200289 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200290#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200291 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200292#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200293
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200294 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200296
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200297 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200298 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200299}
300
301/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200302 * Verify a signature
303 */
304int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
305 const unsigned char *hash, size_t hash_len,
306 const unsigned char *sig, size_t sig_len )
307{
308 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
309 sig, sig_len, NULL ) );
310}
311
312/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200313 * Verify a signature with options
314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
316 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200317 const unsigned char *hash, size_t hash_len,
318 const unsigned char *sig, size_t sig_len )
319{
320 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ! mbedtls_pk_can_do( ctx, type ) )
324 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200329 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200331
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100332#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000333 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
334 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100335#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000336
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200337 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( sig_len < mbedtls_pk_get_len( ctx ) )
343 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
346 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200347 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200348 pss_opts->mgf1_hash_id,
349 pss_opts->expected_salt_len,
350 sig );
351 if( ret != 0 )
352 return( ret );
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( sig_len > mbedtls_pk_get_len( ctx ) )
355 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200356
357 return( 0 );
358#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000360#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200361 }
362
363 /* General case: no options */
364 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200368}
369
370/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200371 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200372 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200373int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
374 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200375 const unsigned char *hash, size_t hash_len,
376 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200377 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200378 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200379{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200380 if( ctx == NULL || ctx->pk_info == NULL ||
381 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200383
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200384#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200385 /* optimization: use non-restartable version if restart disabled */
386 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200387 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200388 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200389 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200390 int ret;
391
392 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
393 return( ret );
394
395 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
396 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
397
398 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
399 mbedtls_pk_restart_free( rs_ctx );
400
401 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200402 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200403#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200404 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200405#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200406
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200407 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200409
410 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
411 sig, sig_len, f_rng, p_rng ) );
412}
413
414/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200415 * Make a signature
416 */
417int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
418 const unsigned char *hash, size_t hash_len,
419 unsigned char *sig, size_t *sig_len,
420 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
421{
422 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
423 sig, sig_len, f_rng, p_rng, NULL ) );
424}
425
426/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200427 * Decrypt message
428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200430 const unsigned char *input, size_t ilen,
431 unsigned char *output, size_t *olen, size_t osize,
432 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
433{
434 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200436
437 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200439
440 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
441 output, olen, osize, f_rng, p_rng ) );
442}
443
444/*
445 * Encrypt message
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200448 const unsigned char *input, size_t ilen,
449 unsigned char *output, size_t *olen, size_t osize,
450 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
451{
452 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200454
455 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200457
458 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
459 output, olen, osize, f_rng, p_rng ) );
460}
461
462/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100463 * Check public-private key pair
464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100466{
467 if( pub == NULL || pub->pk_info == NULL ||
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200468 prv == NULL || prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100471 }
472
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200473 if( prv->pk_info->check_pair_func == NULL )
474 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 if( pub->pk_info->type != MBEDTLS_PK_RSA )
479 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100480 }
481 else
482 {
483 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100485 }
486
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100487 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100488}
489
490/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200491 * Get key size in bits
492 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200493size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200494{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200495 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200496 return( 0 );
497
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200498 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200499}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200500
501/*
502 * Export debug information
503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200505{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200506 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200508
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200509 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200511
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200512 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200513 return( 0 );
514}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200515
516/*
517 * Access the PK type name
518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200520{
521 if( ctx == NULL || ctx->pk_info == NULL )
522 return( "invalid PK" );
523
524 return( ctx->pk_info->name );
525}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200526
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200527/*
528 * Access the PK type
529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200531{
532 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200534
535 return( ctx->pk_info->type );
536}
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#endif /* MBEDTLS_PK_C */