blob: 637fd49dd00426ec44ee699eed6813bbcc27c017 [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
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020057#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020067#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020068
Andres AG72849872017-01-19 11:24:33 +000069#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010070#include <stdint.h>
Andres AG72849872017-01-19 11:24:33 +000071
Paul Bakker34617722014-06-13 17:20:13 +020072/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020074 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
75}
76
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020077/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020081{
82 if( ctx == NULL )
83 return;
84
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020085 ctx->pk_info = NULL;
86 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020087}
88
89/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020093{
Paul Bakker66d5d072014-06-17 16:39:18 +020094 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020095 return;
96
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020097 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200100}
101
102/*
103 * Get pk_info structure from type
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200106{
107 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_RSA_C)
109 case MBEDTLS_PK_RSA:
110 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_ECP_C)
113 case MBEDTLS_PK_ECKEY:
114 return( &mbedtls_eckey_info );
115 case MBEDTLS_PK_ECKEY_DH:
116 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200117#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_ECDSA_C)
119 case MBEDTLS_PK_ECDSA:
120 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200121#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200123 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200124 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200125 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200126}
127
128/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200129 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200130 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200131int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200132{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200133 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200136 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200137 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200138
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200139 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200140
141 return( 0 );
142}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100145/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200146 * Initialize an RSA-alt context
147 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200148int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
150 mbedtls_pk_rsa_alt_sign_func sign_func,
151 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_rsa_alt_context *rsa_alt;
154 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200155
156 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200158
159 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200160 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200161
162 ctx->pk_info = info;
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200165
166 rsa_alt->key = key;
167 rsa_alt->decrypt_func = decrypt_func;
168 rsa_alt->sign_func = sign_func;
169 rsa_alt->key_len_func = key_len_func;
170
171 return( 0 );
172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200174
175/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200176 * Tell if a PK can do the operations of the given type
177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200179{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200180 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200181 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200182 return( 0 );
183
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200184 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200185}
186
187/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200191{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200193
194 if( *hash_len != 0 )
195 return( 0 );
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200198 return( -1 );
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200201 return( 0 );
202}
203
204/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200205 * Verify a signature
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200208 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200209 const unsigned char *sig, size_t sig_len )
210{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200211 if( ctx == NULL || ctx->pk_info == NULL ||
212 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200214
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200215 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200217
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200218 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200219 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200220}
221
222/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200223 * Verify a signature with options
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
226 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200227 const unsigned char *hash, size_t hash_len,
228 const unsigned char *sig, size_t sig_len )
229{
230 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ! mbedtls_pk_can_do( ctx, type ) )
234 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200239 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200241
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100242#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000243 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
244 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100245#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000246
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200247 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( sig_len < mbedtls_pk_get_len( ctx ) )
253 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
256 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200257 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200258 pss_opts->mgf1_hash_id,
259 pss_opts->expected_salt_len,
260 sig );
261 if( ret != 0 )
262 return( ret );
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( sig_len > mbedtls_pk_get_len( ctx ) )
265 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200266
267 return( 0 );
268#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000270#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200271 }
272
273 /* General case: no options */
274 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200278}
279
280/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200281 * Make a signature
282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200284 const unsigned char *hash, size_t hash_len,
285 unsigned char *sig, size_t *sig_len,
286 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
287{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200288 if( ctx == NULL || ctx->pk_info == NULL ||
289 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200291
292 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200294
295 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
296 sig, sig_len, f_rng, p_rng ) );
297}
298
299/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200300 * Decrypt message
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200303 const unsigned char *input, size_t ilen,
304 unsigned char *output, size_t *olen, size_t osize,
305 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
306{
307 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200309
310 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200312
313 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
314 output, olen, osize, f_rng, p_rng ) );
315}
316
317/*
318 * Encrypt message
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200321 const unsigned char *input, size_t ilen,
322 unsigned char *output, size_t *olen, size_t osize,
323 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
324{
325 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200327
328 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200330
331 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
332 output, olen, osize, f_rng, p_rng ) );
333}
334
335/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100336 * Check public-private key pair
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100339{
340 if( pub == NULL || pub->pk_info == NULL ||
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100341 prv == NULL || prv->pk_info == NULL ||
342 prv->pk_info->check_pair_func == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100345 }
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 if( pub->pk_info->type != MBEDTLS_PK_RSA )
350 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100351 }
352 else
353 {
354 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100356 }
357
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100358 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100359}
360
361/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200362 * Get key size in bits
363 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200364size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200365{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200366 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200367 return( 0 );
368
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200369 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200370}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200371
372/*
373 * Export debug information
374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200376{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200377 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200379
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200380 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200382
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200383 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200384 return( 0 );
385}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200386
387/*
388 * Access the PK type name
389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200391{
392 if( ctx == NULL || ctx->pk_info == NULL )
393 return( "invalid PK" );
394
395 return( ctx->pk_info->name );
396}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200397
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200398/*
399 * Access the PK type
400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200402{
403 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200405
406 return( ctx->pk_info->type );
407}
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_PK_C */