blob: 1eab65a27633f715a3a50ae24de3229613e44f32 [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;
Neil Armstrongeccf88f2022-04-08 15:11:50 +0200172 else if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100173 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
Neil Armstronga88b1582022-05-11 14:11:25 +0200235#if defined(MBEDTLS_USE_PSA_CRYPTO)
236/*
237 * Tell if a PK can do the operations of the given PSA algorithm
238 */
Neil Armstrong408f6a62022-05-17 14:23:20 +0200239int mbedtls_pk_can_do_ext( const mbedtls_pk_context *ctx, psa_algorithm_t alg,
240 psa_key_usage_t usage )
Neil Armstronga88b1582022-05-11 14:11:25 +0200241{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200242 psa_key_usage_t key_usage;
243
Neil Armstronga88b1582022-05-11 14:11:25 +0200244 /* A context with null pk_info is not set up yet and can't do anything.
245 * For backward compatibility, also accept NULL instead of a context
246 * pointer. */
247 if( ctx == NULL || ctx->pk_info == NULL )
248 return( 0 );
249
250 /* Filter out non allowed algorithms */
251 if( PSA_ALG_IS_ECDSA( alg ) == 0 &&
252 PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) == 0 &&
253 PSA_ALG_IS_RSA_PSS( alg ) == 0 &&
254 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
255 PSA_ALG_IS_ECDH( alg ) == 0 )
256 return( 0 );
257
Neil Armstrong408f6a62022-05-17 14:23:20 +0200258 /* Filter out non allowed usage flags */
Neil Armstrong81d391f2022-05-20 09:26:16 +0200259 if( usage == 0 ||
260 ( usage & ~( PSA_KEY_USAGE_SIGN_HASH |
Neil Armstrong408f6a62022-05-17 14:23:20 +0200261 PSA_KEY_USAGE_DECRYPT |
262 PSA_KEY_USAGE_DERIVE ) ) != 0 )
263 return( 0 );
264
Neil Armstronga88b1582022-05-11 14:11:25 +0200265 /* Wildcard hash is not allowed */
266 if( PSA_ALG_IS_SIGN_HASH( alg ) &&
267 PSA_ALG_SIGN_GET_HASH( alg ) == PSA_ALG_ANY_HASH )
268 return( 0 );
269
270 if( mbedtls_pk_get_type( ctx ) != MBEDTLS_PK_OPAQUE )
271 {
272 mbedtls_pk_type_t type;
273
274 if( PSA_ALG_IS_ECDSA( alg ) || PSA_ALG_IS_ECDH( alg ) )
275 type = MBEDTLS_PK_ECKEY;
Neil Armstrong084338d2022-05-19 16:22:40 +0200276 else if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
277 alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Neil Armstronga88b1582022-05-11 14:11:25 +0200278 type = MBEDTLS_PK_RSA;
279 else if( PSA_ALG_IS_RSA_PSS( alg ) )
280 type = MBEDTLS_PK_RSASSA_PSS;
281 else
282 return( 0 );
283
Neil Armstrong084338d2022-05-19 16:22:40 +0200284 if( ctx->pk_info->can_do( type ) == 0 )
Neil Armstrong408f6a62022-05-17 14:23:20 +0200285 return( 0 );
286
Neil Armstrong084338d2022-05-19 16:22:40 +0200287 switch( type )
288 {
289 case MBEDTLS_PK_ECKEY:
290 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
291 break;
292 case MBEDTLS_PK_RSA:
293 case MBEDTLS_PK_RSASSA_PSS:
294 key_usage = PSA_KEY_USAGE_SIGN_HASH |
295 PSA_KEY_USAGE_SIGN_MESSAGE |
296 PSA_KEY_USAGE_DECRYPT;
297 break;
298 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200299 /* Should never happen */
Neil Armstrong084338d2022-05-19 16:22:40 +0200300 return( 0 );
301 }
302
303 return( ( key_usage & usage ) == usage );
Neil Armstronga88b1582022-05-11 14:11:25 +0200304 }
305
306 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
308 psa_algorithm_t key_alg, key_alg2;
309 psa_status_t status;
310
311 status = psa_get_key_attributes( *key, &attributes );
312 if( status != PSA_SUCCESS )
313 return( 0 );
314
315 key_alg = psa_get_key_algorithm( &attributes );
316 key_alg2 = psa_get_key_enrollment_algorithm( &attributes );
Neil Armstrong408f6a62022-05-17 14:23:20 +0200317 key_usage = psa_get_key_usage_flags( &attributes );
Neil Armstronga88b1582022-05-11 14:11:25 +0200318 psa_reset_key_attributes( &attributes );
319
Neil Armstrong408f6a62022-05-17 14:23:20 +0200320 if( ( key_usage & usage ) != usage )
321 return( 0 );
322
Neil Armstronga88b1582022-05-11 14:11:25 +0200323 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200324 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200325 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
326 * directly.
327 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
328 * a fixed hash on key_alg/key_alg2.
329 */
330 if( alg == key_alg || alg == key_alg2 )
331 return( 1 );
332
333 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200334 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
335 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200336 * then alg is compliant with this key alg
337 */
338 if( PSA_ALG_IS_SIGN_HASH( alg ) )
339 {
340
341 if( PSA_ALG_IS_SIGN_HASH( key_alg ) &&
342 PSA_ALG_SIGN_GET_HASH( key_alg ) == PSA_ALG_ANY_HASH &&
343 ( alg & ~PSA_ALG_HASH_MASK ) == ( key_alg & ~PSA_ALG_HASH_MASK ) )
344 return( 1 );
345
346 if( PSA_ALG_IS_SIGN_HASH( key_alg2 ) &&
347 PSA_ALG_SIGN_GET_HASH( key_alg2 ) == PSA_ALG_ANY_HASH &&
348 ( alg & ~PSA_ALG_HASH_MASK ) == ( key_alg2 & ~PSA_ALG_HASH_MASK ) )
349 return( 1 );
350 }
351
352 return( 0 );
353}
354#endif /* MBEDTLS_USE_PSA_CRYPTO */
355
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200356/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200362
363 if( *hash_len != 0 )
364 return( 0 );
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200367 return( -1 );
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200370 return( 0 );
371}
372
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200373#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200374/*
375 * Helper to set up a restart context if needed
376 */
377static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200378 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200379{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200380 /* Don't do anything if already set up or invalid */
381 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200382 return( 0 );
383
384 /* Should never happen when we're called */
385 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
386 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
387
388 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
389 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
390
391 ctx->pk_info = info;
392
393 return( 0 );
394}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200395#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200396
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200397/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200398 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200399 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200400int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
401 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200402 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200403 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200404 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200405{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500406 PK_VALIDATE_RET( ctx != NULL );
407 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
408 hash != NULL );
409 PK_VALIDATE_RET( sig != NULL );
410
411 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200412 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200414
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200415#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200416 /* optimization: use non-restartable version if restart disabled */
417 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200418 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200419 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200420 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200422
423 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
424 return( ret );
425
426 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
427 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
428
429 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
430 mbedtls_pk_restart_free( rs_ctx );
431
432 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200433 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200434#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200435 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200436#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200437
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200438 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200440
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200441 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200442 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200443}
444
445/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200446 * Verify a signature
447 */
448int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
449 const unsigned char *hash, size_t hash_len,
450 const unsigned char *sig, size_t sig_len )
451{
452 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
453 sig, sig_len, NULL ) );
454}
455
456/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200457 * Verify a signature with options
458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
460 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200461 const unsigned char *hash, size_t hash_len,
462 const unsigned char *sig, size_t sig_len )
463{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500464 PK_VALIDATE_RET( ctx != NULL );
465 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
466 hash != NULL );
467 PK_VALIDATE_RET( sig != NULL );
468
469 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 if( ! mbedtls_pk_can_do( ctx, type ) )
473 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200474
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500475 if( type != MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200476 {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500477 /* General case: no options */
478 if( options != NULL )
479 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
480
481 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
482 }
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
486 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200487
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100488#if SIZE_MAX > UINT_MAX
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500489 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
490 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100491#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000492
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500493 if( options == NULL )
494 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200495
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500496 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200497
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500498#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500499 if( pss_opts->mgf1_hash_id == md_alg &&
500 ( (size_t) pss_opts->expected_salt_len == hash_len ||
501 pss_opts->expected_salt_len == MBEDTLS_RSA_SALT_LEN_ANY ) )
502 {
503 /* see RSA_PUB_DER_MAX_BYTES in pkwrite.c */
504 unsigned char buf[ 38 + 2 * MBEDTLS_MPI_MAX_SIZE ];
505 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500506 int key_len;
507 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500508 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
509 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500510
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500511 psa_algorithm_t psa_md_alg = mbedtls_psa_translate_md( md_alg );
512 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500514 psa_algorithm_t psa_sig_alg =
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500515 ( pss_opts->expected_salt_len == MBEDTLS_RSA_SALT_LEN_ANY ?
516 PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg) :
517 PSA_ALG_RSA_PSS(psa_md_alg) );
518 p = buf + sizeof( buf );
519 key_len = mbedtls_pk_write_pubkey( &p, buf, ctx );
520
521 if( key_len < 0 )
522 return( key_len );
523
524 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
525 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500526 psa_set_key_algorithm( &attributes, psa_sig_alg );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500527
528 status = psa_import_key( &attributes,
529 buf + sizeof( buf ) - key_len, key_len,
530 &key_id );
531 if( status != PSA_SUCCESS )
532 {
533 psa_destroy_key( key_id );
Neil Armstrong19915c22022-03-01 15:21:02 +0100534 return( mbedtls_pk_error_from_psa( status ) );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500535 }
536
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500537 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
538 * on a valid signature with trailing data in a buffer, but
539 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
540 * so for this reason the passed sig_len is overwritten. Smaller
541 * signature lengths should not be accepted for verification. */
542 signature_length = sig_len > mbedtls_pk_get_len( ctx ) ?
543 mbedtls_pk_get_len( ctx ) : sig_len;
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500544 status = psa_verify_hash( key_id, psa_sig_alg, hash,
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500545 hash_len, sig, signature_length );
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500546 destruction_status = psa_destroy_key( key_id );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500547
Andrzej Kurek8666df62022-02-15 08:23:02 -0500548 if( status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len( ctx ) )
549 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
550
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500551 if( status == PSA_SUCCESS )
552 status = destruction_status;
553
Neil Armstrong19915c22022-03-01 15:21:02 +0100554 return( mbedtls_pk_error_from_psa_rsa( status ) );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500555 }
556 else
557#endif
558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( sig_len < mbedtls_pk_get_len( ctx ) )
560 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
Thomas Daubney782a7f52021-05-19 12:27:35 +0100563 md_alg, (unsigned int) hash_len, hash,
564 pss_opts->mgf1_hash_id,
565 pss_opts->expected_salt_len,
566 sig );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200567 if( ret != 0 )
568 return( ret );
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 if( sig_len > mbedtls_pk_get_len( ctx ) )
571 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500572
573 return( 0 );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200574 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500575#else
576 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
577#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200578}
579
580/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200581 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200582 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200583int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
584 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200585 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200586 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200587 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200588 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200589{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500590 PK_VALIDATE_RET( ctx != NULL );
591 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
592 hash != NULL );
593 PK_VALIDATE_RET( sig != NULL );
594
595 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200596 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200598
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200599#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200600 /* optimization: use non-restartable version if restart disabled */
601 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200602 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200603 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200604 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200606
607 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
608 return( ret );
609
610 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200611 hash, hash_len,
612 sig, sig_size, sig_len,
613 f_rng, p_rng, rs_ctx->rs_ctx );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200614
615 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
616 mbedtls_pk_restart_free( rs_ctx );
617
618 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200619 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200620#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200621 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200622#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200623
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200624 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200626
Gilles Peskinef00f1522021-06-22 00:09:00 +0200627 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg,
628 hash, hash_len,
629 sig, sig_size, sig_len,
630 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200631}
632
633/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200634 * Make a signature
635 */
636int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
637 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200638 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200639 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
640{
641 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200642 sig, sig_size, sig_len,
643 f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200644}
645
Jerry Yu1d172a32022-03-12 19:12:05 +0800646#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200647/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800648 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800649 */
Jerry Yu718a9b42022-03-12 22:43:01 +0800650int mbedtls_pk_sign_ext( mbedtls_pk_type_t pk_type,
Jerry Yu8beb9e12022-03-12 16:23:53 +0800651 mbedtls_pk_context *ctx,
652 mbedtls_md_type_t md_alg,
653 const unsigned char *hash, size_t hash_len,
654 unsigned char *sig, size_t sig_size, size_t *sig_len,
655 int (*f_rng)(void *, unsigned char *, size_t),
656 void *p_rng )
Jerry Yud69439a2022-02-24 15:52:15 +0800657{
Jerry Yufb0621d2022-03-23 11:42:06 +0800658#if defined(MBEDTLS_RSA_C)
659 psa_algorithm_t psa_md_alg;
660#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800661 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800662
Jerry Yud69439a2022-02-24 15:52:15 +0800663 if( ctx->pk_info == NULL )
664 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
665
Jerry Yu718a9b42022-03-12 22:43:01 +0800666 if( ! mbedtls_pk_can_do( ctx, pk_type ) )
Jerry Yud69439a2022-02-24 15:52:15 +0800667 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
668
Jerry Yu718a9b42022-03-12 22:43:01 +0800669 if( pk_type != MBEDTLS_PK_RSASSA_PSS )
Jerry Yud69439a2022-02-24 15:52:15 +0800670 {
Jerry Yu1d172a32022-03-12 19:12:05 +0800671 return( mbedtls_pk_sign( ctx, md_alg, hash, hash_len,
672 sig, sig_size, sig_len, f_rng, p_rng ) );
Jerry Yud69439a2022-02-24 15:52:15 +0800673 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200674
Jerry Yu89107d12022-03-22 14:20:15 +0800675#if defined(MBEDTLS_RSA_C)
Jerry Yufb0621d2022-03-23 11:42:06 +0800676 psa_md_alg = mbedtls_psa_translate_md( md_alg );
677 if( psa_md_alg == 0 )
678 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong13e76be2022-04-21 12:08:52 +0200679
680 if( mbedtls_pk_get_type( ctx ) == MBEDTLS_PK_OPAQUE )
681 {
682 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
683 psa_status_t status;
684
685 status = psa_sign_hash( *key, PSA_ALG_RSA_PSS( psa_md_alg ),
686 hash, hash_len,
687 sig, sig_size, sig_len );
688 return( mbedtls_pk_error_from_psa_rsa( status ) );
689 }
690
Jerry Yufb0621d2022-03-23 11:42:06 +0800691 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PSS( psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800692 ctx->pk_ctx, hash, hash_len,
693 sig, sig_size, sig_len ) );
694#else /* MBEDTLS_RSA_C */
695 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
696#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800697
698}
Jerry Yu1d172a32022-03-12 19:12:05 +0800699#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800700
701/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200702 * Decrypt message
703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200705 const unsigned char *input, size_t ilen,
706 unsigned char *output, size_t *olen, size_t osize,
707 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
708{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500709 PK_VALIDATE_RET( ctx != NULL );
710 PK_VALIDATE_RET( input != NULL || ilen == 0 );
711 PK_VALIDATE_RET( output != NULL || osize == 0 );
712 PK_VALIDATE_RET( olen != NULL );
713
714 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200716
717 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200719
720 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
721 output, olen, osize, f_rng, p_rng ) );
722}
723
724/*
725 * Encrypt message
726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200728 const unsigned char *input, size_t ilen,
729 unsigned char *output, size_t *olen, size_t osize,
730 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
731{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500732 PK_VALIDATE_RET( ctx != NULL );
733 PK_VALIDATE_RET( input != NULL || ilen == 0 );
734 PK_VALIDATE_RET( output != NULL || osize == 0 );
735 PK_VALIDATE_RET( olen != NULL );
736
737 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200739
740 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200742
743 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
744 output, olen, osize, f_rng, p_rng ) );
745}
746
747/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100748 * Check public-private key pair
749 */
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200750int mbedtls_pk_check_pair( const mbedtls_pk_context *pub,
751 const mbedtls_pk_context *prv,
752 int (*f_rng)(void *, unsigned char *, size_t),
753 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100754{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500755 PK_VALIDATE_RET( pub != NULL );
756 PK_VALIDATE_RET( prv != NULL );
757
758 if( pub->pk_info == NULL ||
Andrzej Kurekefed3232019-02-05 05:09:05 -0500759 prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100762 }
763
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200764 if( f_rng == NULL )
765 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
766
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200767 if( prv->pk_info->check_pair_func == NULL )
768 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( pub->pk_info->type != MBEDTLS_PK_RSA )
773 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100774 }
775 else
776 {
777 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100779 }
780
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200781 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 +0100782}
783
784/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200785 * Get key size in bits
786 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200787size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200788{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500789 /* For backward compatibility, accept NULL or a context that
790 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200791 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200792 return( 0 );
793
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200794 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200795}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200796
797/*
798 * Export debug information
799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200801{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500802 PK_VALIDATE_RET( ctx != NULL );
803 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200805
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200806 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200808
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200809 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200810 return( 0 );
811}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200812
813/*
814 * Access the PK type name
815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200817{
818 if( ctx == NULL || ctx->pk_info == NULL )
819 return( "invalid PK" );
820
821 return( ctx->pk_info->name );
822}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200823
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200824/*
825 * Access the PK type
826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200828{
829 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200831
832 return( ctx->pk_info->type );
833}
834
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100835#if defined(MBEDTLS_USE_PSA_CRYPTO)
836/*
837 * Load the key to a PSA key slot,
838 * then turn the PK context into a wrapper for that key slot.
839 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200840 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100841 */
842int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
Andrzej Kurek03e01462022-01-03 12:53:24 +0100843 mbedtls_svc_key_id_t *key,
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200844 psa_algorithm_t alg,
845 psa_key_usage_t usage,
846 psa_algorithm_t alg2 )
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100847{
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100848#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700849 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200850 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200851 ((void) alg);
852 ((void) usage);
853 ((void) alg2);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100854#else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100855#if defined(MBEDTLS_ECP_C)
856 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY )
857 {
858 const mbedtls_ecp_keypair *ec;
859 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
860 size_t d_len;
861 psa_ecc_family_t curve_id;
862 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
863 psa_key_type_t key_type;
864 size_t bits;
865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100866 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100867
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100868 /* export the private key material in the format PSA wants */
869 ec = mbedtls_pk_ec( *pk );
Neil Armstrong7e1b4a42022-03-22 10:25:14 +0100870 d_len = PSA_BITS_TO_BYTES( ec->grp.nbits );
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100871 if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
872 return( ret );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100873
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100874 curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
875 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100876
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100877 /* prepare the key attributes */
878 psa_set_key_type( &attributes, key_type );
879 psa_set_key_bits( &attributes, bits );
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200880 psa_set_key_usage_flags( &attributes, usage );
881 psa_set_key_algorithm( &attributes, alg );
882 if( alg2 != PSA_ALG_NONE )
883 psa_set_key_enrollment_algorithm( &attributes, alg2 );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100884
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100885 /* import private key into PSA */
Neil Armstrongc1152e42022-03-22 10:29:06 +0100886 status = psa_import_key( &attributes, d, d_len, key );
887 if( status != PSA_SUCCESS )
888 return( mbedtls_pk_error_from_psa( status ) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100889
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100890 /* make PK context wrap the key slot */
891 mbedtls_pk_free( pk );
892 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100893
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100894 return( mbedtls_pk_setup_opaque( pk, *key ) );
895 }
896 else
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100897#endif /* MBEDTLS_ECP_C */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100898#if defined(MBEDTLS_RSA_C)
899 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_RSA )
900 {
901 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
902 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
903 int key_len;
904 psa_status_t status;
905
906 /* export the private key material in the format PSA wants */
907 key_len = mbedtls_pk_write_key_der( pk, buf, sizeof( buf ) );
908 if( key_len <= 0 )
909 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
910
911 /* prepare the key attributes */
912 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
913 psa_set_key_bits( &attributes, mbedtls_pk_get_bitlen( pk ) );
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200914 psa_set_key_usage_flags( &attributes, usage );
915 psa_set_key_algorithm( &attributes, alg );
916 if( alg2 != PSA_ALG_NONE )
917 psa_set_key_enrollment_algorithm( &attributes, alg2 );
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100918
919 /* import private key into PSA */
920 status = psa_import_key( &attributes,
921 buf + sizeof( buf ) - key_len,
922 key_len, key);
923
924 mbedtls_platform_zeroize( buf, sizeof( buf ) );
925
926 if( status != PSA_SUCCESS )
Neil Armstrongc1152e42022-03-22 10:29:06 +0100927 return( mbedtls_pk_error_from_psa( status ) );
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100928
929 /* make PK context wrap the key slot */
930 mbedtls_pk_free( pk );
931 mbedtls_pk_init( pk );
932
933 return( mbedtls_pk_setup_opaque( pk, *key ) );
934 }
935 else
936#endif /* MBEDTLS_RSA_C */
937#endif /* !MBEDTLS_ECP_C && !MBEDTLS_RSA_C */
938 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100939}
940#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#endif /* MBEDTLS_PK_C */