blob: bf96debba133518e803494d3c3d2ba86290a7f39 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020055#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020065#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020066
Andres AG72849872017-01-19 11:24:33 +000067#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010068#include <stdint.h>
Andres AG72849872017-01-19 11:24:33 +000069
Paul Bakker34617722014-06-13 17:20:13 +020070/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020072 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
73}
74
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020075/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020079{
80 if( ctx == NULL )
81 return;
82
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020083 ctx->pk_info = NULL;
84 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020085}
86
87/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020091{
Paul Bakker66d5d072014-06-17 16:39:18 +020092 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020093 return;
94
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020095 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020098}
99
100/*
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100143/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200144 * Initialize an RSA-alt context
145 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200146int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
148 mbedtls_pk_rsa_alt_sign_func sign_func,
149 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_rsa_alt_context *rsa_alt;
152 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200153
154 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200156
157 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200158 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200159
160 ctx->pk_info = info;
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200163
164 rsa_alt->key = key;
165 rsa_alt->decrypt_func = decrypt_func;
166 rsa_alt->sign_func = sign_func;
167 rsa_alt->key_len_func = key_len_func;
168
169 return( 0 );
170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200172
173/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200174 * Tell if a PK can do the operations of the given type
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200177{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200178 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200179 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200180 return( 0 );
181
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200182 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200183}
184
185/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200191
192 if( *hash_len != 0 )
193 return( 0 );
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200196 return( -1 );
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200199 return( 0 );
200}
201
202/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200203 * Verify a signature
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200206 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200207 const unsigned char *sig, size_t sig_len )
208{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200209 if( ctx == NULL || ctx->pk_info == NULL ||
210 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200212
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200213 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200215
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200216 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200217 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200218}
219
220/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200221 * Verify a signature with options
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
224 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200225 const unsigned char *hash, size_t hash_len,
226 const unsigned char *sig, size_t sig_len )
227{
228 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( ! mbedtls_pk_can_do( ctx, type ) )
232 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200237 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200239
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100240#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000241 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
242 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100243#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000244
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200245 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( sig_len < mbedtls_pk_get_len( ctx ) )
251 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
254 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200255 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200256 pss_opts->mgf1_hash_id,
257 pss_opts->expected_salt_len,
258 sig );
259 if( ret != 0 )
260 return( ret );
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( sig_len > mbedtls_pk_get_len( ctx ) )
263 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200264
265 return( 0 );
266#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000268#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200269 }
270
271 /* General case: no options */
272 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200276}
277
278/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200279 * Make a signature
280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200282 const unsigned char *hash, size_t hash_len,
283 unsigned char *sig, size_t *sig_len,
284 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
285{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200286 if( ctx == NULL || ctx->pk_info == NULL ||
287 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200289
290 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200292
293 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
294 sig, sig_len, f_rng, p_rng ) );
295}
296
297/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200298 * Decrypt message
299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200301 const unsigned char *input, size_t ilen,
302 unsigned char *output, size_t *olen, size_t osize,
303 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
304{
305 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200307
308 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200310
311 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
312 output, olen, osize, f_rng, p_rng ) );
313}
314
315/*
316 * Encrypt message
317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200319 const unsigned char *input, size_t ilen,
320 unsigned char *output, size_t *olen, size_t osize,
321 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
322{
323 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200325
326 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200328
329 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
330 output, olen, osize, f_rng, p_rng ) );
331}
332
333/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100334 * Check public-private key pair
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100337{
338 if( pub == NULL || pub->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100339 prv == NULL || prv->pk_info == NULL ||
340 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100343 }
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( pub->pk_info->type != MBEDTLS_PK_RSA )
348 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100349 }
350 else
351 {
352 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100354 }
355
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100356 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100357}
358
359/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200360 * Get key size in bits
361 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200362size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200363{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200364 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200365 return( 0 );
366
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200367 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200368}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200369
370/*
371 * Export debug information
372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200374{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200375 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200377
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200378 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200380
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200381 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200382 return( 0 );
383}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200384
385/*
386 * Access the PK type name
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200389{
390 if( ctx == NULL || ctx->pk_info == NULL )
391 return( "invalid PK" );
392
393 return( ctx->pk_info->name );
394}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200395
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200396/*
397 * Access the PK type
398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200400{
401 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200403
404 return( ctx->pk_info->type );
405}
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#endif /* MBEDTLS_PK_C */