blob: 52bcb866973d30868f0053cdbdd57255a2e7a21f [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"
Gilles Peskine5cc7bc52017-11-03 11:58:25 +010031#include "mbedtls/pk_info.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020041#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020042
Andres AG72849872017-01-19 11:24:33 +000043#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010044#include <stdint.h>
Andres AG72849872017-01-19 11:24:33 +000045
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
76/*
77 * Get pk_info structure from type
78 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020080{
81 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_RSA_C)
83 case MBEDTLS_PK_RSA:
84 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020085#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_ECP_C)
87 case MBEDTLS_PK_ECKEY:
88 return( &mbedtls_eckey_info );
89 case MBEDTLS_PK_ECKEY_DH:
90 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020091#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_ECDSA_C)
93 case MBEDTLS_PK_ECDSA:
94 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Gilles Peskine02768b42017-11-03 19:20:27 +010097 /* MBEDTLS_PK_OPAQUE omitted on purpose: they can't be built by parsing */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020098 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +020099 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200100 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200101}
102
103/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200104 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200105 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200106int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200107{
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200108 if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200110
Gilles Peskine02768b42017-11-03 19:20:27 +0100111 if( info->ctx_alloc_func != NULL )
112 {
113 if( ( ctx->pk_ctx = info->ctx_alloc_func( ) ) == NULL )
114 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
115 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200116
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200117 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200118
119 return( 0 );
120}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100123/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200124 * Initialize an RSA-alt context
125 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200126int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
128 mbedtls_pk_rsa_alt_sign_func sign_func,
129 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_rsa_alt_context *rsa_alt;
132 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200133
134 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200136
137 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200138 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200139
140 ctx->pk_info = info;
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200143
144 rsa_alt->key = key;
145 rsa_alt->decrypt_func = decrypt_func;
146 rsa_alt->sign_func = sign_func;
147 rsa_alt->key_len_func = key_len_func;
148
149 return( 0 );
150}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200152
153/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200154 * Tell if a PK can do the operations of the given type
155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200157{
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200158 /* null or NONE context can't do anything */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200159 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200160 return( 0 );
161
Gilles Peskine373deea2017-10-26 12:03:35 +0200162 return( ctx->pk_info->can_do( ctx->pk_ctx, type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200163}
164
165/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200171
172 if( *hash_len != 0 )
173 return( 0 );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200176 return( -1 );
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200179 return( 0 );
180}
181
182/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200183 * Verify a signature
184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200186 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200187 const unsigned char *sig, size_t sig_len )
188{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200189 if( ctx == NULL || ctx->pk_info == NULL ||
190 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200192
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200193 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200195
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200196 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200197 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200198}
199
200/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200201 * Verify a signature with options
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
204 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200205 const unsigned char *hash, size_t hash_len,
206 const unsigned char *sig, size_t sig_len )
207{
208 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ! mbedtls_pk_can_do( ctx, type ) )
212 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200217 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200219
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100220#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +0000221 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
222 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100223#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000224
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200225 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( sig_len < mbedtls_pk_get_len( ctx ) )
231 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
234 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200235 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200236 pss_opts->mgf1_hash_id,
237 pss_opts->expected_salt_len,
238 sig );
239 if( ret != 0 )
240 return( ret );
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( sig_len > mbedtls_pk_get_len( ctx ) )
243 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200244
245 return( 0 );
246#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +0000248#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200249 }
250
251 /* General case: no options */
252 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200256}
257
258/*
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200259 * Make a signature
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200262 const unsigned char *hash, size_t hash_len,
263 unsigned char *sig, size_t *sig_len,
264 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
265{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200266 if( ctx == NULL || ctx->pk_info == NULL ||
267 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200269
270 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200272
273 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
274 sig, sig_len, f_rng, p_rng ) );
275}
276
277/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200278 * Decrypt message
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200281 const unsigned char *input, size_t ilen,
282 unsigned char *output, size_t *olen, size_t osize,
283 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
284{
285 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200287
288 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200290
291 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
292 output, olen, osize, f_rng, p_rng ) );
293}
294
295/*
296 * Encrypt message
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200299 const unsigned char *input, size_t ilen,
300 unsigned char *output, size_t *olen, size_t osize,
301 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
302{
303 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200305
306 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200308
309 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
310 output, olen, osize, f_rng, p_rng ) );
311}
312
313/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100314 * Check public-private key pair
315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100317{
318 if( pub == NULL || pub->pk_info == NULL ||
Gilles Peskine02768b42017-11-03 19:20:27 +0100319 prv == NULL || prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100322 }
323
Gilles Peskine02768b42017-11-03 19:20:27 +0100324 if( pub->pk_info == prv->pk_info && pub->pk_ctx == prv->pk_ctx )
325 return( 0 );
326
327 if( prv->pk_info->check_pair_func == NULL )
328 {
329 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
330 }
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 if( pub->pk_info->type != MBEDTLS_PK_RSA )
335 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100336 }
Gilles Peskine02768b42017-11-03 19:20:27 +0100337 else if( prv->pk_info->type != MBEDTLS_PK_OPAQUE )
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100338 {
339 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100341 }
342
Gilles Peskine02768b42017-11-03 19:20:27 +0100343 return( prv->pk_info->check_pair_func( pub, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100344}
345
346/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200347 * Get key size in bits
348 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200349size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200350{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200351 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200352 return( 0 );
353
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200354 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200355}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200356
357/*
Gilles Peskinecd062d82017-11-02 17:16:43 +0100358 * Maximum signature size
359 */
360size_t mbedtls_pk_signature_size( const mbedtls_pk_context *ctx )
361{
362 if( ctx == NULL || ctx->pk_info == NULL )
363 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
364
365 if( ctx->pk_info->signature_size_func == NULL )
Andrzej Kurek8b6aaca2018-01-22 07:04:46 -0500366 return( 0 );
367
368 return( ctx->pk_info->signature_size_func( ctx->pk_ctx ) );
Gilles Peskinecd062d82017-11-02 17:16:43 +0100369}
370
371/*
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200372 * Export debug information
373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200375{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200376 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200378
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200379 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200381
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200382 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200383 return( 0 );
384}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200385
386/*
387 * Access the PK type name
388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200390{
391 if( ctx == NULL || ctx->pk_info == NULL )
392 return( "invalid PK" );
393
394 return( ctx->pk_info->name );
395}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200396
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200397/*
Gilles Peskine02768b42017-11-03 19:20:27 +0100398 * Access the PK type.
399 * For an opaque key pair object, this does not give any information on the
400 * underlying cryptographic material.
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200403{
404 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200406
407 return( ctx->pk_info->type );
408}
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#endif /* MBEDTLS_PK_C */