blob: 9a6e86233b89c8de30393152748cf4ded0f290c5 [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 AG72849872017-01-19 11:24:33 +000032#include "mbedtls/bignum.h"
33
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>
45
Paul Bakker34617722014-06-13 17:20:13 +020046/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020048 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020051/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020055{
56 if( ctx == NULL )
57 return;
58
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020059 ctx->pk_info = NULL;
60 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061}
62
63/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020067{
Paul Bakker66d5d072014-06-17 16:39:18 +020068 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069 return;
70
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020071 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020074}
75
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020076#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020077/*
78 * Initialize a restart context
79 */
80void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
81{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020082 ctx->pk_info = NULL;
83 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020084}
85
86/*
87 * Free the components of a restart context
88 */
89void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
90{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020091 if( ctx == NULL || ctx->pk_info == NULL ||
92 ctx->pk_info->rs_free_func == NULL )
93 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020094 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020095 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020096
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020097 ctx->pk_info->rs_free_func( ctx->rs_ctx );
98
99 ctx->pk_info = NULL;
100 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200102#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200104/*
105 * Get pk_info structure from type
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200108{
109 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_RSA_C)
111 case MBEDTLS_PK_RSA:
112 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_ECP_C)
115 case MBEDTLS_PK_ECKEY:
116 return( &mbedtls_eckey_info );
117 case MBEDTLS_PK_ECKEY_DH:
118 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_ECDSA_C)
121 case MBEDTLS_PK_ECDSA:
122 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200125 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200126 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200127 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200128}
129
130/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200131 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200132 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200133int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200134{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200135 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200137
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200138 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200139 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200140
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200141 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200142
143 return( 0 );
144}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100147/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200148 * Initialize an RSA-alt context
149 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200150int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
152 mbedtls_pk_rsa_alt_sign_func sign_func,
153 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_rsa_alt_context *rsa_alt;
156 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200157
158 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200160
161 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200162 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200163
164 ctx->pk_info = info;
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200167
168 rsa_alt->key = key;
169 rsa_alt->decrypt_func = decrypt_func;
170 rsa_alt->sign_func = sign_func;
171 rsa_alt->key_len_func = key_len_func;
172
173 return( 0 );
174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200176
177/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200178 * Tell if a PK can do the operations of the given type
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200181{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200182 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200183 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200184 return( 0 );
185
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200186 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200187}
188
189/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200195
196 if( *hash_len != 0 )
197 return( 0 );
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200200 return( -1 );
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200203 return( 0 );
204}
205
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200206#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200207/*
208 * Helper to set up a restart context if needed
209 */
210static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
211 const mbedtls_pk_info_t *info )
212{
213 /* Don't do anything it already set up */
214 if( ctx->pk_info != NULL )
215 return( 0 );
216
217 /* Should never happen when we're called */
218 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
219 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
220
221 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
222 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
223
224 ctx->pk_info = info;
225
226 return( 0 );
227}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200228#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200229
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200230/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200231 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200232 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200233int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
234 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200235 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200236 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200237 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200238{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200239 if( ctx == NULL || ctx->pk_info == NULL ||
240 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200242
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200243#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200244 /* optimization: use non-restartable version if restart disabled */
245 if( rs_ctx != NULL &&
246 mbedtls_ecp_restart_enabled() &&
247 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200248 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200249 int ret;
250
251 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
252 return( ret );
253
254 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
255 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
256
257 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
258 mbedtls_pk_restart_free( rs_ctx );
259
260 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200261 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200262#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200263 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200264#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200265
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200266 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200268
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200269 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200270 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200271}
272
273/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200274 * Verify a signature
275 */
276int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
277 const unsigned char *hash, size_t hash_len,
278 const unsigned char *sig, size_t sig_len )
279{
280 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
281 sig, sig_len, NULL ) );
282}
283
284/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200285 * Verify a signature with options
286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
288 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200289 const unsigned char *hash, size_t hash_len,
290 const unsigned char *sig, size_t sig_len )
291{
292 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ! mbedtls_pk_can_do( ctx, type ) )
296 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200301 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200303
Andres AG72849872017-01-19 11:24:33 +0000304#if defined(MBEDTLS_HAVE_INT64)
305 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
306 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
307#endif /* MBEDTLS_HAVE_INT64 */
308
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200309 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( sig_len < mbedtls_pk_get_len( ctx ) )
315 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
318 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200319 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200320 pss_opts->mgf1_hash_id,
321 pss_opts->expected_salt_len,
322 sig );
323 if( ret != 0 )
324 return( ret );
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( sig_len > mbedtls_pk_get_len( ctx ) )
327 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200328
329 return( 0 );
330#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000332#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200333 }
334
335 /* General case: no options */
336 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 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200340}
341
342/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200343 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200344 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200345int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
346 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200347 const unsigned char *hash, size_t hash_len,
348 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200349 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200350 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200351{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200352 if( ctx == NULL || ctx->pk_info == NULL ||
353 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200355
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200356#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200357 /* optimization: use non-restartable version if restart disabled */
358 if( rs_ctx != NULL &&
359 mbedtls_ecp_restart_enabled() &&
360 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200361 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200362 int ret;
363
364 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
365 return( ret );
366
367 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
368 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
369
370 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
371 mbedtls_pk_restart_free( rs_ctx );
372
373 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200374 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200375#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200376 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200377#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200378
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200379 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200381
382 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
383 sig, sig_len, f_rng, p_rng ) );
384}
385
386/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200387 * Make a signature
388 */
389int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
390 const unsigned char *hash, size_t hash_len,
391 unsigned char *sig, size_t *sig_len,
392 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
393{
394 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
395 sig, sig_len, f_rng, p_rng, NULL ) );
396}
397
398/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200399 * Decrypt message
400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200402 const unsigned char *input, size_t ilen,
403 unsigned char *output, size_t *olen, size_t osize,
404 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
405{
406 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200408
409 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200411
412 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
413 output, olen, osize, f_rng, p_rng ) );
414}
415
416/*
417 * Encrypt message
418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200420 const unsigned char *input, size_t ilen,
421 unsigned char *output, size_t *olen, size_t osize,
422 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
423{
424 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200426
427 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200429
430 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
431 output, olen, osize, f_rng, p_rng ) );
432}
433
434/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100435 * Check public-private key pair
436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100438{
439 if( pub == NULL || pub->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100440 prv == NULL || prv->pk_info == NULL ||
441 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100444 }
445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 if( pub->pk_info->type != MBEDTLS_PK_RSA )
449 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100450 }
451 else
452 {
453 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100455 }
456
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100457 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100458}
459
460/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200461 * Get key size in bits
462 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200463size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200464{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200465 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200466 return( 0 );
467
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200468 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200469}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200470
471/*
472 * Export debug information
473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200475{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200476 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200478
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200479 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200481
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200482 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200483 return( 0 );
484}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200485
486/*
487 * Access the PK type name
488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200490{
491 if( ctx == NULL || ctx->pk_info == NULL )
492 return( "invalid PK" );
493
494 return( ctx->pk_info->name );
495}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200496
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200497/*
498 * Access the PK type
499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200501{
502 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200504
505 return( ctx->pk_info->type );
506}
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#endif /* MBEDTLS_PK_C */