blob: d6ea912ff8f81432365893d5c27ac45abe00d7fe [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000024#include "pk_wrap.h"
Neil Armstrongca5b55f2022-03-15 15:00:55 +010025#include "pkwrite.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020026
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/ecp.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_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020038#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020039
Jerry Yub02ee182022-03-16 10:30:41 +080040#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010041#include "mbedtls/psa_util.h"
42#endif
43
Andres AG72849872017-01-19 11:24:33 +000044#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010045#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020046
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050047/* Parameter validation macros based on platform_util.h */
48#define PK_VALIDATE_RET( cond ) \
49 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
50#define PK_VALIDATE( cond ) \
51 MBEDTLS_INTERNAL_VALIDATE( cond )
52
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020057{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050058 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020059
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020060 ctx->pk_info = NULL;
61 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062}
63
64/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020068{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020070 return;
71
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072 if ( ctx->pk_info != NULL )
73 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050075 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020076}
77
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020078#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020079/*
80 * Initialize a restart context
81 */
82void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
83{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020085 ctx->pk_info = NULL;
86 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020087}
88
89/*
90 * Free the components of a restart context
91 */
92void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
93{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020094 if( ctx == NULL || ctx->pk_info == NULL ||
95 ctx->pk_info->rs_free_func == NULL )
96 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020097 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020098 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200100 ctx->pk_info->rs_free_func( ctx->rs_ctx );
101
102 ctx->pk_info = NULL;
103 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200104}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200105#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200106
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200107/*
108 * Get pk_info structure from type
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111{
112 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_RSA_C)
114 case MBEDTLS_PK_RSA:
115 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_ECP_C)
118 case MBEDTLS_PK_ECKEY:
119 return( &mbedtls_eckey_info );
120 case MBEDTLS_PK_ECKEY_DH:
121 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_ECDSA_C)
124 case MBEDTLS_PK_ECDSA:
125 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200128 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200129 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200130 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200131}
132
133/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200134 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200136int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200137{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500138 PK_VALIDATE_RET( ctx != NULL );
139 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200142 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200143 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200144
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200145 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200146
147 return( 0 );
148}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200149
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200150#if defined(MBEDTLS_USE_PSA_CRYPTO)
151/*
152 * Initialise a PSA-wrapping context
153 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200154int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx,
Andrzej Kurek03e01462022-01-03 12:53:24 +0100155 const mbedtls_svc_key_id_t key )
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200156{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100157 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100159 mbedtls_svc_key_id_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100160 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200161
162 if( ctx == NULL || ctx->pk_info != NULL )
163 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
164
Gilles Peskined2d45c12019-05-27 14:53:13 +0200165 if( PSA_SUCCESS != psa_get_key_attributes( key, &attributes ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100166 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200167 type = psa_get_key_type( &attributes );
168 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100169
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100170 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
171 info = &mbedtls_pk_ecdsa_opaque_info;
172 else if( PSA_KEY_TYPE_IS_RSA( type ) )
173 info = &mbedtls_pk_rsa_opaque_info;
174 else
175 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100176
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200177 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
178 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
179
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200180 ctx->pk_info = info;
181
Andrzej Kurek03e01462022-01-03 12:53:24 +0100182 pk_ctx = (mbedtls_svc_key_id_t *) ctx->pk_ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100183 *pk_ctx = key;
184
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200185 return( 0 );
186}
187#endif /* MBEDTLS_USE_PSA_CRYPTO */
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100190/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200191 * Initialize an RSA-alt context
192 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200193int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
195 mbedtls_pk_rsa_alt_sign_func sign_func,
196 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_rsa_alt_context *rsa_alt;
199 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200200
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 PK_VALIDATE_RET( ctx != NULL );
202 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200204
205 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200206 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200207
208 ctx->pk_info = info;
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200211
212 rsa_alt->key = key;
213 rsa_alt->decrypt_func = decrypt_func;
214 rsa_alt->sign_func = sign_func;
215 rsa_alt->key_len_func = key_len_func;
216
217 return( 0 );
218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200220
221/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200222 * Tell if a PK can do the operations of the given type
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200225{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226 /* A context with null pk_info is not set up yet and can't do anything.
227 * For backward compatibility, also accept NULL instead of a context
228 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200229 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200230 return( 0 );
231
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200232 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200233}
234
235/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200241
242 if( *hash_len != 0 )
243 return( 0 );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200246 return( -1 );
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200249 return( 0 );
250}
251
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200252#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200253/*
254 * Helper to set up a restart context if needed
255 */
256static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200257 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200258{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200259 /* Don't do anything if already set up or invalid */
260 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200261 return( 0 );
262
263 /* Should never happen when we're called */
264 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
265 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
266
267 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
268 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
269
270 ctx->pk_info = info;
271
272 return( 0 );
273}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200274#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200275
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200276/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200277 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200278 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200279int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
280 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200281 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200282 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200283 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200284{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285 PK_VALIDATE_RET( ctx != NULL );
286 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
287 hash != NULL );
288 PK_VALIDATE_RET( sig != NULL );
289
290 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200291 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200293
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200294#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200295 /* optimization: use non-restartable version if restart disabled */
296 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200297 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200298 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200299 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200301
302 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
303 return( ret );
304
305 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
306 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
307
308 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
309 mbedtls_pk_restart_free( rs_ctx );
310
311 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200312 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200313#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200314 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200315#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200316
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200317 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200319
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200320 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200321 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200322}
323
324/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200325 * Verify a signature
326 */
327int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
328 const unsigned char *hash, size_t hash_len,
329 const unsigned char *sig, size_t sig_len )
330{
331 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
332 sig, sig_len, NULL ) );
333}
334
335/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200336 * Verify a signature with options
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
339 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200340 const unsigned char *hash, size_t hash_len,
341 const unsigned char *sig, size_t sig_len )
342{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500343 PK_VALIDATE_RET( ctx != NULL );
344 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
345 hash != NULL );
346 PK_VALIDATE_RET( sig != NULL );
347
348 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ! mbedtls_pk_can_do( ctx, type ) )
352 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200353
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500354 if( type != MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200355 {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500356 /* General case: no options */
357 if( options != NULL )
358 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
359
360 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
361 }
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
365 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200366
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100367#if SIZE_MAX > UINT_MAX
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500368 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
369 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100370#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000371
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500372 if( options == NULL )
373 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200374
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500375 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200376
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500377#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500378 if( pss_opts->mgf1_hash_id == md_alg &&
379 ( (size_t) pss_opts->expected_salt_len == hash_len ||
380 pss_opts->expected_salt_len == MBEDTLS_RSA_SALT_LEN_ANY ) )
381 {
382 /* see RSA_PUB_DER_MAX_BYTES in pkwrite.c */
383 unsigned char buf[ 38 + 2 * MBEDTLS_MPI_MAX_SIZE ];
384 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500385 int key_len;
386 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500387 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
388 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500389
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500390 psa_algorithm_t psa_md_alg = mbedtls_psa_translate_md( md_alg );
391 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500393 psa_algorithm_t psa_sig_alg =
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500394 ( pss_opts->expected_salt_len == MBEDTLS_RSA_SALT_LEN_ANY ?
395 PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg) :
396 PSA_ALG_RSA_PSS(psa_md_alg) );
397 p = buf + sizeof( buf );
398 key_len = mbedtls_pk_write_pubkey( &p, buf, ctx );
399
400 if( key_len < 0 )
401 return( key_len );
402
403 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
404 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500405 psa_set_key_algorithm( &attributes, psa_sig_alg );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500406
407 status = psa_import_key( &attributes,
408 buf + sizeof( buf ) - key_len, key_len,
409 &key_id );
410 if( status != PSA_SUCCESS )
411 {
412 psa_destroy_key( key_id );
Neil Armstrong19915c22022-03-01 15:21:02 +0100413 return( mbedtls_pk_error_from_psa( status ) );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500414 }
415
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500416 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
417 * on a valid signature with trailing data in a buffer, but
418 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
419 * so for this reason the passed sig_len is overwritten. Smaller
420 * signature lengths should not be accepted for verification. */
421 signature_length = sig_len > mbedtls_pk_get_len( ctx ) ?
422 mbedtls_pk_get_len( ctx ) : sig_len;
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500423 status = psa_verify_hash( key_id, psa_sig_alg, hash,
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500424 hash_len, sig, signature_length );
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500425 destruction_status = psa_destroy_key( key_id );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500426
Andrzej Kurek8666df62022-02-15 08:23:02 -0500427 if( status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len( ctx ) )
428 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
429
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500430 if( status == PSA_SUCCESS )
431 status = destruction_status;
432
Neil Armstrong19915c22022-03-01 15:21:02 +0100433 return( mbedtls_pk_error_from_psa_rsa( status ) );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500434 }
435 else
436#endif
437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 if( sig_len < mbedtls_pk_get_len( ctx ) )
439 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
Thomas Daubney782a7f52021-05-19 12:27:35 +0100442 md_alg, (unsigned int) hash_len, hash,
443 pss_opts->mgf1_hash_id,
444 pss_opts->expected_salt_len,
445 sig );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200446 if( ret != 0 )
447 return( ret );
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 if( sig_len > mbedtls_pk_get_len( ctx ) )
450 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500451
452 return( 0 );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200453 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500454#else
455 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
456#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200457}
458
459/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200460 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200461 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200462int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
463 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200464 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200465 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200466 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200467 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200468{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500469 PK_VALIDATE_RET( ctx != NULL );
470 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
471 hash != NULL );
472 PK_VALIDATE_RET( sig != NULL );
473
474 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200475 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200477
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200478#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200479 /* optimization: use non-restartable version if restart disabled */
480 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200481 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200482 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200483 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200485
486 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
487 return( ret );
488
489 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200490 hash, hash_len,
491 sig, sig_size, sig_len,
492 f_rng, p_rng, rs_ctx->rs_ctx );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200493
494 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
495 mbedtls_pk_restart_free( rs_ctx );
496
497 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200498 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200499#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200500 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200501#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200502
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200503 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200505
Gilles Peskinef00f1522021-06-22 00:09:00 +0200506 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg,
507 hash, hash_len,
508 sig, sig_size, sig_len,
509 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200510}
511
512/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200513 * Make a signature
514 */
515int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
516 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200517 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200518 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
519{
520 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200521 sig, sig_size, sig_len,
522 f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200523}
524
Jerry Yu1d172a32022-03-12 19:12:05 +0800525#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200526/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800527 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800528 */
Jerry Yu718a9b42022-03-12 22:43:01 +0800529int mbedtls_pk_sign_ext( mbedtls_pk_type_t pk_type,
Jerry Yu8beb9e12022-03-12 16:23:53 +0800530 mbedtls_pk_context *ctx,
531 mbedtls_md_type_t md_alg,
532 const unsigned char *hash, size_t hash_len,
533 unsigned char *sig, size_t sig_size, size_t *sig_len,
534 int (*f_rng)(void *, unsigned char *, size_t),
535 void *p_rng )
Jerry Yud69439a2022-02-24 15:52:15 +0800536{
Jerry Yufb0621d2022-03-23 11:42:06 +0800537#if defined(MBEDTLS_RSA_C)
538 psa_algorithm_t psa_md_alg;
539#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800540 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800541
Jerry Yud69439a2022-02-24 15:52:15 +0800542 if( ctx->pk_info == NULL )
543 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
544
Jerry Yu718a9b42022-03-12 22:43:01 +0800545 if( ! mbedtls_pk_can_do( ctx, pk_type ) )
Jerry Yud69439a2022-02-24 15:52:15 +0800546 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
547
Jerry Yu718a9b42022-03-12 22:43:01 +0800548 if( pk_type != MBEDTLS_PK_RSASSA_PSS )
Jerry Yud69439a2022-02-24 15:52:15 +0800549 {
Jerry Yu1d172a32022-03-12 19:12:05 +0800550 return( mbedtls_pk_sign( ctx, md_alg, hash, hash_len,
551 sig, sig_size, sig_len, f_rng, p_rng ) );
Jerry Yud69439a2022-02-24 15:52:15 +0800552 }
Jerry Yu89107d12022-03-22 14:20:15 +0800553#if defined(MBEDTLS_RSA_C)
Jerry Yufb0621d2022-03-23 11:42:06 +0800554 psa_md_alg = mbedtls_psa_translate_md( md_alg );
555 if( psa_md_alg == 0 )
556 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
557 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PSS( psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800558 ctx->pk_ctx, hash, hash_len,
559 sig, sig_size, sig_len ) );
560#else /* MBEDTLS_RSA_C */
561 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
562#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800563
564}
Jerry Yu1d172a32022-03-12 19:12:05 +0800565#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800566
567/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200568 * Decrypt message
569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200571 const unsigned char *input, size_t ilen,
572 unsigned char *output, size_t *olen, size_t osize,
573 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
574{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500575 PK_VALIDATE_RET( ctx != NULL );
576 PK_VALIDATE_RET( input != NULL || ilen == 0 );
577 PK_VALIDATE_RET( output != NULL || osize == 0 );
578 PK_VALIDATE_RET( olen != NULL );
579
580 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200582
583 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200585
586 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
587 output, olen, osize, f_rng, p_rng ) );
588}
589
590/*
591 * Encrypt message
592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200594 const unsigned char *input, size_t ilen,
595 unsigned char *output, size_t *olen, size_t osize,
596 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
597{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500598 PK_VALIDATE_RET( ctx != NULL );
599 PK_VALIDATE_RET( input != NULL || ilen == 0 );
600 PK_VALIDATE_RET( output != NULL || osize == 0 );
601 PK_VALIDATE_RET( olen != NULL );
602
603 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200605
606 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200608
609 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
610 output, olen, osize, f_rng, p_rng ) );
611}
612
613/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100614 * Check public-private key pair
615 */
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200616int mbedtls_pk_check_pair( const mbedtls_pk_context *pub,
617 const mbedtls_pk_context *prv,
618 int (*f_rng)(void *, unsigned char *, size_t),
619 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100620{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500621 PK_VALIDATE_RET( pub != NULL );
622 PK_VALIDATE_RET( prv != NULL );
623
624 if( pub->pk_info == NULL ||
Andrzej Kurekefed3232019-02-05 05:09:05 -0500625 prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100628 }
629
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200630 if( f_rng == NULL )
631 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
632
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200633 if( prv->pk_info->check_pair_func == NULL )
634 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 if( pub->pk_info->type != MBEDTLS_PK_RSA )
639 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100640 }
641 else
642 {
643 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100645 }
646
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200647 return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx, f_rng, p_rng ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100648}
649
650/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200651 * Get key size in bits
652 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200653size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200654{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500655 /* For backward compatibility, accept NULL or a context that
656 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200657 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200658 return( 0 );
659
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200660 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200661}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200662
663/*
664 * Export debug information
665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200667{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500668 PK_VALIDATE_RET( ctx != NULL );
669 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200671
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200672 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200674
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200675 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200676 return( 0 );
677}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200678
679/*
680 * Access the PK type name
681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200683{
684 if( ctx == NULL || ctx->pk_info == NULL )
685 return( "invalid PK" );
686
687 return( ctx->pk_info->name );
688}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200689
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200690/*
691 * Access the PK type
692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200694{
695 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200697
698 return( ctx->pk_info->type );
699}
700
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100701#if defined(MBEDTLS_USE_PSA_CRYPTO)
702/*
703 * Load the key to a PSA key slot,
704 * then turn the PK context into a wrapper for that key slot.
705 *
706 * Currently only works for EC private keys.
707 */
708int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
Andrzej Kurek03e01462022-01-03 12:53:24 +0100709 mbedtls_svc_key_id_t *key,
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100710 psa_algorithm_t hash_alg )
711{
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100712#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700713 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200714 ((void) key);
John Durkopf35069a2020-08-17 22:05:14 -0700715 ((void) hash_alg);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100716#else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100717#if defined(MBEDTLS_ECP_C)
718 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY )
719 {
720 const mbedtls_ecp_keypair *ec;
721 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
722 size_t d_len;
723 psa_ecc_family_t curve_id;
724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
725 psa_key_type_t key_type;
726 size_t bits;
727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100728
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100729 /* export the private key material in the format PSA wants */
730 ec = mbedtls_pk_ec( *pk );
731 d_len = ( ec->grp.nbits + 7 ) / 8;
732 if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
733 return( ret );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100734
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100735 curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
736 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100737
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100738 /* prepare the key attributes */
739 psa_set_key_type( &attributes, key_type );
740 psa_set_key_bits( &attributes, bits );
741 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH |
742 PSA_KEY_USAGE_DERIVE);
743 psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA( hash_alg ) );
744 psa_set_key_enrollment_algorithm( &attributes, PSA_ALG_ECDH );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100745
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100746 /* import private key into PSA */
747 if( PSA_SUCCESS != psa_import_key( &attributes, d, d_len, key ) )
748 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100749
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100750 /* make PK context wrap the key slot */
751 mbedtls_pk_free( pk );
752 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100753
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100754 return( mbedtls_pk_setup_opaque( pk, *key ) );
755 }
756 else
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100757#endif /* MBEDTLS_ECP_C */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100758#if defined(MBEDTLS_RSA_C)
759 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_RSA )
760 {
761 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
763 int key_len;
764 psa_status_t status;
765
766 /* export the private key material in the format PSA wants */
767 key_len = mbedtls_pk_write_key_der( pk, buf, sizeof( buf ) );
768 if( key_len <= 0 )
769 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
770
771 /* prepare the key attributes */
772 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
773 psa_set_key_bits( &attributes, mbedtls_pk_get_bitlen( pk ) );
774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
775 psa_set_key_algorithm( &attributes,
776 PSA_ALG_RSA_PKCS1V15_SIGN( hash_alg ) );
777
778 /* import private key into PSA */
779 status = psa_import_key( &attributes,
780 buf + sizeof( buf ) - key_len,
781 key_len, key);
782
783 mbedtls_platform_zeroize( buf, sizeof( buf ) );
784
785 if( status != PSA_SUCCESS )
786 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
787
788 /* make PK context wrap the key slot */
789 mbedtls_pk_free( pk );
790 mbedtls_pk_init( pk );
791
792 return( mbedtls_pk_setup_opaque( pk, *key ) );
793 }
794 else
795#endif /* MBEDTLS_RSA_C */
796#endif /* !MBEDTLS_ECP_C && !MBEDTLS_RSA_C */
797 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100798}
799#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#endif /* MBEDTLS_PK_C */