blob: a73fa56f5051e3888e5f1dc56dfe243153f41695 [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
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020027#include "hash_info.h"
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +020028
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050029#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000030#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020040#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020041
Jerry Yub02ee182022-03-16 10:30:41 +080042#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010043#include "mbedtls/psa_util.h"
44#endif
45
Andres AG72849872017-01-19 11:24:33 +000046#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010047#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020048
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020049/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020054 ctx->pk_info = NULL;
55 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020056}
57
58/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020064 return;
65
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050066 if ( ctx->pk_info != NULL )
67 ctx->pk_info->ctx_free_func( ctx->pk_ctx );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020068
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050069 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020070}
71
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020072#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020073/*
74 * Initialize a restart context
75 */
76void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
77{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020078 ctx->pk_info = NULL;
79 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020080}
81
82/*
83 * Free the components of a restart context
84 */
85void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
86{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020087 if( ctx == NULL || ctx->pk_info == NULL ||
88 ctx->pk_info->rs_free_func == NULL )
89 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020090 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020091 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020093 ctx->pk_info->rs_free_func( ctx->rs_ctx );
94
95 ctx->pk_info = NULL;
96 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020097}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020098#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200100/*
101 * Get pk_info structure from type
102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200104{
105 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_RSA_C)
107 case MBEDTLS_PK_RSA:
108 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200109#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_ECP_C)
111 case MBEDTLS_PK_ECKEY:
112 return( &mbedtls_eckey_info );
113 case MBEDTLS_PK_ECKEY_DH:
114 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200115#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#if defined(MBEDTLS_ECDSA_C)
117 case MBEDTLS_PK_ECDSA:
118 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200121 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200122 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200123 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200124}
125
126/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200127 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200128 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200129int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200130{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500131 if( info == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200133
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200134 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200135 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200136
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200137 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200138
139 return( 0 );
140}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200141
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200142#if defined(MBEDTLS_USE_PSA_CRYPTO)
143/*
144 * Initialise a PSA-wrapping context
145 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200146int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx,
Andrzej Kurek03e01462022-01-03 12:53:24 +0100147 const mbedtls_svc_key_id_t key )
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200148{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100149 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100151 mbedtls_svc_key_id_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100152 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200153
154 if( ctx == NULL || ctx->pk_info != NULL )
155 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
156
Gilles Peskined2d45c12019-05-27 14:53:13 +0200157 if( PSA_SUCCESS != psa_get_key_attributes( key, &attributes ) )
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100158 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Gilles Peskined2d45c12019-05-27 14:53:13 +0200159 type = psa_get_key_type( &attributes );
160 psa_reset_key_attributes( &attributes );
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100161
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100162 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
163 info = &mbedtls_pk_ecdsa_opaque_info;
Neil Armstrongeccf88f2022-04-08 15:11:50 +0200164 else if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100165 info = &mbedtls_pk_rsa_opaque_info;
166 else
167 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100168
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200169 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
170 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
171
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200172 ctx->pk_info = info;
173
Andrzej Kurek03e01462022-01-03 12:53:24 +0100174 pk_ctx = (mbedtls_svc_key_id_t *) ctx->pk_ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100175 *pk_ctx = key;
176
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200177 return( 0 );
178}
179#endif /* MBEDTLS_USE_PSA_CRYPTO */
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100182/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200183 * Initialize an RSA-alt context
184 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200185int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
187 mbedtls_pk_rsa_alt_sign_func sign_func,
188 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200189{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_rsa_alt_context *rsa_alt;
191 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200192
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500193 if( ctx->pk_info != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200195
196 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200197 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200198
199 ctx->pk_info = info;
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200202
203 rsa_alt->key = key;
204 rsa_alt->decrypt_func = decrypt_func;
205 rsa_alt->sign_func = sign_func;
206 rsa_alt->key_len_func = key_len_func;
207
208 return( 0 );
209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200211
212/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200213 * Tell if a PK can do the operations of the given type
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200216{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 /* A context with null pk_info is not set up yet and can't do anything.
218 * For backward compatibility, also accept NULL instead of a context
219 * pointer. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200220 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200221 return( 0 );
222
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200223 return( ctx->pk_info->can_do( type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200224}
225
Neil Armstronga88b1582022-05-11 14:11:25 +0200226#if defined(MBEDTLS_USE_PSA_CRYPTO)
227/*
228 * Tell if a PK can do the operations of the given PSA algorithm
229 */
Neil Armstrong408f6a62022-05-17 14:23:20 +0200230int mbedtls_pk_can_do_ext( const mbedtls_pk_context *ctx, psa_algorithm_t alg,
231 psa_key_usage_t usage )
Neil Armstronga88b1582022-05-11 14:11:25 +0200232{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200233 psa_key_usage_t key_usage;
234
Neil Armstronga88b1582022-05-11 14:11:25 +0200235 /* A context with null pk_info is not set up yet and can't do anything.
236 * For backward compatibility, also accept NULL instead of a context
237 * pointer. */
238 if( ctx == NULL || ctx->pk_info == NULL )
239 return( 0 );
240
241 /* Filter out non allowed algorithms */
242 if( PSA_ALG_IS_ECDSA( alg ) == 0 &&
243 PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) == 0 &&
244 PSA_ALG_IS_RSA_PSS( alg ) == 0 &&
245 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
246 PSA_ALG_IS_ECDH( alg ) == 0 )
247 return( 0 );
248
Neil Armstrong408f6a62022-05-17 14:23:20 +0200249 /* Filter out non allowed usage flags */
Neil Armstrong81d391f2022-05-20 09:26:16 +0200250 if( usage == 0 ||
251 ( usage & ~( PSA_KEY_USAGE_SIGN_HASH |
Neil Armstrong408f6a62022-05-17 14:23:20 +0200252 PSA_KEY_USAGE_DECRYPT |
253 PSA_KEY_USAGE_DERIVE ) ) != 0 )
254 return( 0 );
255
Neil Armstronga88b1582022-05-11 14:11:25 +0200256 /* Wildcard hash is not allowed */
257 if( PSA_ALG_IS_SIGN_HASH( alg ) &&
258 PSA_ALG_SIGN_GET_HASH( alg ) == PSA_ALG_ANY_HASH )
259 return( 0 );
260
261 if( mbedtls_pk_get_type( ctx ) != MBEDTLS_PK_OPAQUE )
262 {
263 mbedtls_pk_type_t type;
264
265 if( PSA_ALG_IS_ECDSA( alg ) || PSA_ALG_IS_ECDH( alg ) )
266 type = MBEDTLS_PK_ECKEY;
Neil Armstrong084338d2022-05-19 16:22:40 +0200267 else if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
268 alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Neil Armstronga88b1582022-05-11 14:11:25 +0200269 type = MBEDTLS_PK_RSA;
270 else if( PSA_ALG_IS_RSA_PSS( alg ) )
271 type = MBEDTLS_PK_RSASSA_PSS;
272 else
273 return( 0 );
274
Neil Armstrong084338d2022-05-19 16:22:40 +0200275 if( ctx->pk_info->can_do( type ) == 0 )
Neil Armstrong408f6a62022-05-17 14:23:20 +0200276 return( 0 );
277
Neil Armstrong084338d2022-05-19 16:22:40 +0200278 switch( type )
279 {
280 case MBEDTLS_PK_ECKEY:
281 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
282 break;
283 case MBEDTLS_PK_RSA:
284 case MBEDTLS_PK_RSASSA_PSS:
285 key_usage = PSA_KEY_USAGE_SIGN_HASH |
286 PSA_KEY_USAGE_SIGN_MESSAGE |
287 PSA_KEY_USAGE_DECRYPT;
288 break;
289 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200290 /* Should never happen */
Neil Armstrong084338d2022-05-19 16:22:40 +0200291 return( 0 );
292 }
293
294 return( ( key_usage & usage ) == usage );
Neil Armstronga88b1582022-05-11 14:11:25 +0200295 }
296
297 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
299 psa_algorithm_t key_alg, key_alg2;
300 psa_status_t status;
301
302 status = psa_get_key_attributes( *key, &attributes );
303 if( status != PSA_SUCCESS )
304 return( 0 );
305
306 key_alg = psa_get_key_algorithm( &attributes );
307 key_alg2 = psa_get_key_enrollment_algorithm( &attributes );
Neil Armstrong408f6a62022-05-17 14:23:20 +0200308 key_usage = psa_get_key_usage_flags( &attributes );
Neil Armstronga88b1582022-05-11 14:11:25 +0200309 psa_reset_key_attributes( &attributes );
310
Neil Armstrong408f6a62022-05-17 14:23:20 +0200311 if( ( key_usage & usage ) != usage )
312 return( 0 );
313
Neil Armstronga88b1582022-05-11 14:11:25 +0200314 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200315 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200316 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
317 * directly.
318 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
319 * a fixed hash on key_alg/key_alg2.
320 */
321 if( alg == key_alg || alg == key_alg2 )
322 return( 1 );
323
324 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200325 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
326 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200327 * then alg is compliant with this key alg
328 */
329 if( PSA_ALG_IS_SIGN_HASH( alg ) )
330 {
331
332 if( PSA_ALG_IS_SIGN_HASH( key_alg ) &&
333 PSA_ALG_SIGN_GET_HASH( key_alg ) == PSA_ALG_ANY_HASH &&
334 ( alg & ~PSA_ALG_HASH_MASK ) == ( key_alg & ~PSA_ALG_HASH_MASK ) )
335 return( 1 );
336
337 if( PSA_ALG_IS_SIGN_HASH( key_alg2 ) &&
338 PSA_ALG_SIGN_GET_HASH( key_alg2 ) == PSA_ALG_ANY_HASH &&
339 ( alg & ~PSA_ALG_HASH_MASK ) == ( key_alg2 & ~PSA_ALG_HASH_MASK ) )
340 return( 1 );
341 }
342
343 return( 0 );
344}
345#endif /* MBEDTLS_USE_PSA_CRYPTO */
346
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200347/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200351{
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200352 if( *hash_len != 0 )
353 return( 0 );
354
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +0200355 *hash_len = mbedtls_hash_info_get_size( md_alg );
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200356
357 if( *hash_len == 0 )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200358 return( -1 );
359
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200360 return( 0 );
361}
362
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200363#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200364/*
365 * Helper to set up a restart context if needed
366 */
367static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200368 const mbedtls_pk_info_t *info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200369{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200370 /* Don't do anything if already set up or invalid */
371 if( ctx == NULL || ctx->pk_info != NULL )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200372 return( 0 );
373
374 /* Should never happen when we're called */
375 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
376 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
377
378 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
379 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
380
381 ctx->pk_info = info;
382
383 return( 0 );
384}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200385#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200386
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200387/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200388 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200389 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200390int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
391 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200392 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200393 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200394 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200395{
Tuvshinzaya Erdenekhuuc388af62022-08-02 11:54:54 +0100396 if( ( md_alg != MBEDTLS_MD_NONE || hash_len != 0 ) && hash == NULL )
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100397 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500398
399 if( ctx->pk_info == NULL ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200400 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200402
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200403#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200404 /* optimization: use non-restartable version if restart disabled */
405 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200406 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200407 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200408 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200410
411 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
412 return( ret );
413
414 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
415 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
416
417 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
418 mbedtls_pk_restart_free( rs_ctx );
419
420 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200421 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200422#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200423 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200424#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200425
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200426 if( ctx->pk_info->verify_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200428
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200429 return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200430 sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200431}
432
433/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200434 * Verify a signature
435 */
436int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
437 const unsigned char *hash, size_t hash_len,
438 const unsigned char *sig, size_t sig_len )
439{
440 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
441 sig, sig_len, NULL ) );
442}
443
444/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200445 * Verify a signature with options
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
448 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200449 const unsigned char *hash, size_t hash_len,
450 const unsigned char *sig, size_t sig_len )
451{
Tuvshinzaya Erdenekhuuc388af62022-08-02 11:54:54 +0100452 if( ( md_alg != MBEDTLS_MD_NONE || hash_len != 0 ) && hash == NULL )
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100453 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500454
455 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( ! mbedtls_pk_can_do( ctx, type ) )
459 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200460
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500461 if( type != MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200462 {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500463 /* General case: no options */
464 if( options != NULL )
465 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
466
467 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
468 }
469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
472 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200473
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100474#if SIZE_MAX > UINT_MAX
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500475 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
476 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100477#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000478
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500479 if( options == NULL )
480 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200481
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500482 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200483
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard69583552022-06-10 12:46:46 +0200485 if( pss_opts->mgf1_hash_id == md_alg )
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500486 {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200487 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500488 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500489 int key_len;
490 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500491 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
492 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500493
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200494 psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md( md_alg );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500495 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
496 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard69583552022-06-10 12:46:46 +0200497 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT( psa_md_alg );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500498 p = buf + sizeof( buf );
499 key_len = mbedtls_pk_write_pubkey( &p, buf, ctx );
500
501 if( key_len < 0 )
502 return( key_len );
503
504 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY );
505 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500506 psa_set_key_algorithm( &attributes, psa_sig_alg );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500507
508 status = psa_import_key( &attributes,
509 buf + sizeof( buf ) - key_len, key_len,
510 &key_id );
511 if( status != PSA_SUCCESS )
512 {
513 psa_destroy_key( key_id );
Neil Armstrong19915c22022-03-01 15:21:02 +0100514 return( mbedtls_pk_error_from_psa( status ) );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500515 }
516
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500517 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
518 * on a valid signature with trailing data in a buffer, but
519 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
520 * so for this reason the passed sig_len is overwritten. Smaller
521 * signature lengths should not be accepted for verification. */
522 signature_length = sig_len > mbedtls_pk_get_len( ctx ) ?
523 mbedtls_pk_get_len( ctx ) : sig_len;
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500524 status = psa_verify_hash( key_id, psa_sig_alg, hash,
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500525 hash_len, sig, signature_length );
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500526 destruction_status = psa_destroy_key( key_id );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500527
Andrzej Kurek8666df62022-02-15 08:23:02 -0500528 if( status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len( ctx ) )
529 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
530
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500531 if( status == PSA_SUCCESS )
532 status = destruction_status;
533
Neil Armstrong19915c22022-03-01 15:21:02 +0100534 return( mbedtls_pk_error_from_psa_rsa( status ) );
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500535 }
536 else
537#endif
538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( sig_len < mbedtls_pk_get_len( ctx ) )
540 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
Thomas Daubney782a7f52021-05-19 12:27:35 +0100543 md_alg, (unsigned int) hash_len, hash,
544 pss_opts->mgf1_hash_id,
545 pss_opts->expected_salt_len,
546 sig );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200547 if( ret != 0 )
548 return( ret );
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( sig_len > mbedtls_pk_get_len( ctx ) )
551 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500552
553 return( 0 );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200554 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500555#else
556 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
557#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200558}
559
560/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200561 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200562 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200563int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
564 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200565 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200566 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200567 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200568 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200569{
Tuvshinzaya Erdenekhuuc388af62022-08-02 11:54:54 +0100570 if( ( md_alg != MBEDTLS_MD_NONE || hash_len != 0 ) && hash == NULL )
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100571 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500572
Tuvshinzaya Erdenekhuuc388af62022-08-02 11:54:54 +0100573 if( ctx->pk_info == NULL || pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200575
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200576#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200577 /* optimization: use non-restartable version if restart disabled */
578 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200579 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200580 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200581 {
Janos Follath24eed8d2019-11-22 13:21:35 +0000582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200583
584 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
585 return( ret );
586
587 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200588 hash, hash_len,
589 sig, sig_size, sig_len,
590 f_rng, p_rng, rs_ctx->rs_ctx );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200591
592 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
593 mbedtls_pk_restart_free( rs_ctx );
594
595 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200596 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200597#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200598 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200599#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200600
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200601 if( ctx->pk_info->sign_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200603
Gilles Peskinef00f1522021-06-22 00:09:00 +0200604 return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg,
605 hash, hash_len,
606 sig, sig_size, sig_len,
607 f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200608}
609
610/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200611 * Make a signature
612 */
613int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
614 const unsigned char *hash, size_t hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200615 unsigned char *sig, size_t sig_size, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200616 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
617{
618 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
Gilles Peskinef00f1522021-06-22 00:09:00 +0200619 sig, sig_size, sig_len,
620 f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200621}
622
Jerry Yu1d172a32022-03-12 19:12:05 +0800623#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200624/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800625 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800626 */
Jerry Yu718a9b42022-03-12 22:43:01 +0800627int mbedtls_pk_sign_ext( mbedtls_pk_type_t pk_type,
Jerry Yu8beb9e12022-03-12 16:23:53 +0800628 mbedtls_pk_context *ctx,
629 mbedtls_md_type_t md_alg,
630 const unsigned char *hash, size_t hash_len,
631 unsigned char *sig, size_t sig_size, size_t *sig_len,
632 int (*f_rng)(void *, unsigned char *, size_t),
633 void *p_rng )
Jerry Yud69439a2022-02-24 15:52:15 +0800634{
Jerry Yufb0621d2022-03-23 11:42:06 +0800635#if defined(MBEDTLS_RSA_C)
636 psa_algorithm_t psa_md_alg;
637#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800638 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800639
Jerry Yud69439a2022-02-24 15:52:15 +0800640 if( ctx->pk_info == NULL )
641 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
642
Jerry Yu718a9b42022-03-12 22:43:01 +0800643 if( ! mbedtls_pk_can_do( ctx, pk_type ) )
Jerry Yud69439a2022-02-24 15:52:15 +0800644 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
645
Jerry Yu718a9b42022-03-12 22:43:01 +0800646 if( pk_type != MBEDTLS_PK_RSASSA_PSS )
Jerry Yud69439a2022-02-24 15:52:15 +0800647 {
Jerry Yu1d172a32022-03-12 19:12:05 +0800648 return( mbedtls_pk_sign( ctx, md_alg, hash, hash_len,
649 sig, sig_size, sig_len, f_rng, p_rng ) );
Jerry Yud69439a2022-02-24 15:52:15 +0800650 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200651
Jerry Yu89107d12022-03-22 14:20:15 +0800652#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200653 psa_md_alg = mbedtls_hash_info_psa_from_md( md_alg );
Jerry Yufb0621d2022-03-23 11:42:06 +0800654 if( psa_md_alg == 0 )
655 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Neil Armstrong13e76be2022-04-21 12:08:52 +0200656
657 if( mbedtls_pk_get_type( ctx ) == MBEDTLS_PK_OPAQUE )
658 {
659 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
660 psa_status_t status;
661
662 status = psa_sign_hash( *key, PSA_ALG_RSA_PSS( psa_md_alg ),
663 hash, hash_len,
664 sig, sig_size, sig_len );
665 return( mbedtls_pk_error_from_psa_rsa( status ) );
666 }
667
Jerry Yufb0621d2022-03-23 11:42:06 +0800668 return( mbedtls_pk_psa_rsa_sign_ext( PSA_ALG_RSA_PSS( psa_md_alg ),
Jerry Yu89107d12022-03-22 14:20:15 +0800669 ctx->pk_ctx, hash, hash_len,
670 sig, sig_size, sig_len ) );
671#else /* MBEDTLS_RSA_C */
672 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
673#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800674
675}
Jerry Yu1d172a32022-03-12 19:12:05 +0800676#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800677
678/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200679 * Decrypt message
680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200682 const unsigned char *input, size_t ilen,
683 unsigned char *output, size_t *olen, size_t osize,
684 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
685{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500686 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200688
689 if( ctx->pk_info->decrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200691
692 return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
693 output, olen, osize, f_rng, p_rng ) );
694}
695
696/*
697 * Encrypt message
698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200700 const unsigned char *input, size_t ilen,
701 unsigned char *output, size_t *olen, size_t osize,
702 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
703{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500704 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200706
707 if( ctx->pk_info->encrypt_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200709
710 return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
711 output, olen, osize, f_rng, p_rng ) );
712}
713
714/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100715 * Check public-private key pair
716 */
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200717int mbedtls_pk_check_pair( const mbedtls_pk_context *pub,
718 const mbedtls_pk_context *prv,
719 int (*f_rng)(void *, unsigned char *, size_t),
720 void *p_rng )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100721{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500722 if( pub->pk_info == NULL ||
Andrzej Kurekefed3232019-02-05 05:09:05 -0500723 prv->pk_info == NULL )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100726 }
727
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200728 if( f_rng == NULL )
729 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
730
Manuel Pégourié-Gonnardeaeb7b22018-10-24 12:37:44 +0200731 if( prv->pk_info->check_pair_func == NULL )
732 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 if( pub->pk_info->type != MBEDTLS_PK_RSA )
737 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100738 }
739 else
740 {
741 if( pub->pk_info != prv->pk_info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100743 }
744
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200745 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 +0100746}
747
748/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200749 * Get key size in bits
750 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200751size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200752{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500753 /* For backward compatibility, accept NULL or a context that
754 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200755 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200756 return( 0 );
757
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200758 return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200759}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200760
761/*
762 * Export debug information
763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200765{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500766 if( ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200768
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200769 if( ctx->pk_info->debug_func == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200771
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200772 ctx->pk_info->debug_func( ctx->pk_ctx, items );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200773 return( 0 );
774}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200775
776/*
777 * Access the PK type name
778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200780{
781 if( ctx == NULL || ctx->pk_info == NULL )
782 return( "invalid PK" );
783
784 return( ctx->pk_info->name );
785}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200786
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200787/*
788 * Access the PK type
789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200791{
792 if( ctx == NULL || ctx->pk_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200794
795 return( ctx->pk_info->type );
796}
797
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100798#if defined(MBEDTLS_USE_PSA_CRYPTO)
799/*
800 * Load the key to a PSA key slot,
801 * then turn the PK context into a wrapper for that key slot.
802 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200803 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100804 */
805int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
Andrzej Kurek03e01462022-01-03 12:53:24 +0100806 mbedtls_svc_key_id_t *key,
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200807 psa_algorithm_t alg,
808 psa_key_usage_t usage,
809 psa_algorithm_t alg2 )
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100810{
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100811#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700812 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200813 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200814 ((void) alg);
815 ((void) usage);
816 ((void) alg2);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100817#else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100818#if defined(MBEDTLS_ECP_C)
819 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY )
820 {
821 const mbedtls_ecp_keypair *ec;
822 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
823 size_t d_len;
824 psa_ecc_family_t curve_id;
825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
826 psa_key_type_t key_type;
827 size_t bits;
828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100829 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100830
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100831 /* export the private key material in the format PSA wants */
832 ec = mbedtls_pk_ec( *pk );
Neil Armstrong7e1b4a42022-03-22 10:25:14 +0100833 d_len = PSA_BITS_TO_BYTES( ec->grp.nbits );
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100834 if( ( ret = mbedtls_mpi_write_binary( &ec->d, d, d_len ) ) != 0 )
835 return( ret );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100836
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100837 curve_id = mbedtls_ecc_group_to_psa( ec->grp.id, &bits );
838 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( curve_id );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100839
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100840 /* prepare the key attributes */
841 psa_set_key_type( &attributes, key_type );
842 psa_set_key_bits( &attributes, bits );
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200843 psa_set_key_usage_flags( &attributes, usage );
844 psa_set_key_algorithm( &attributes, alg );
845 if( alg2 != PSA_ALG_NONE )
846 psa_set_key_enrollment_algorithm( &attributes, alg2 );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100847
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100848 /* import private key into PSA */
Neil Armstrongc1152e42022-03-22 10:29:06 +0100849 status = psa_import_key( &attributes, d, d_len, key );
850 if( status != PSA_SUCCESS )
851 return( mbedtls_pk_error_from_psa( status ) );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100852
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100853 /* make PK context wrap the key slot */
854 mbedtls_pk_free( pk );
855 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100856
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100857 return( mbedtls_pk_setup_opaque( pk, *key ) );
858 }
859 else
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100860#endif /* MBEDTLS_ECP_C */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100861#if defined(MBEDTLS_RSA_C)
862 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_RSA )
863 {
864 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
866 int key_len;
867 psa_status_t status;
868
869 /* export the private key material in the format PSA wants */
870 key_len = mbedtls_pk_write_key_der( pk, buf, sizeof( buf ) );
871 if( key_len <= 0 )
872 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
873
874 /* prepare the key attributes */
875 psa_set_key_type( &attributes, PSA_KEY_TYPE_RSA_KEY_PAIR );
876 psa_set_key_bits( &attributes, mbedtls_pk_get_bitlen( pk ) );
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200877 psa_set_key_usage_flags( &attributes, usage );
878 psa_set_key_algorithm( &attributes, alg );
879 if( alg2 != PSA_ALG_NONE )
880 psa_set_key_enrollment_algorithm( &attributes, alg2 );
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100881
882 /* import private key into PSA */
883 status = psa_import_key( &attributes,
884 buf + sizeof( buf ) - key_len,
885 key_len, key);
886
887 mbedtls_platform_zeroize( buf, sizeof( buf ) );
888
889 if( status != PSA_SUCCESS )
Neil Armstrongc1152e42022-03-22 10:29:06 +0100890 return( mbedtls_pk_error_from_psa( status ) );
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100891
892 /* make PK context wrap the key slot */
893 mbedtls_pk_free( pk );
894 mbedtls_pk_init( pk );
895
896 return( mbedtls_pk_setup_opaque( pk, *key ) );
897 }
898 else
899#endif /* MBEDTLS_RSA_C */
900#endif /* !MBEDTLS_ECP_C && !MBEDTLS_RSA_C */
901 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100902}
903#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904#endif /* MBEDTLS_PK_C */