1
#pragma clang diagnostic ignored "-Wunknown-warning-option"
2
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
3
#pragma clang diagnostic ignored "-Wsign-conversion"
5
#pragma GCC diagnostic ignored "-Wconversion"
7
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
8
#pragma clang diagnostic ignored "-Wsign-conversion"
10
#pragma GCC diagnostic ignored "-Wconversion"
12
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
13
#pragma clang diagnostic ignored "-Wsign-conversion"
15
* Public Key abstraction layer
17
* Copyright The Mbed TLS Contributors
18
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20
* This file is provided under the Apache License 2.0, or the
21
* GNU General Public License v2.0 or later.
26
* Licensed under the Apache License, Version 2.0 (the "License"); you may
27
* not use this file except in compliance with the License.
28
* You may obtain a copy of the License at
30
* http://www.apache.org/licenses/LICENSE-2.0
32
* Unless required by applicable law or agreed to in writing, software
33
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35
* See the License for the specific language governing permissions and
36
* limitations under the License.
41
* GNU General Public License v2.0 or later:
43
* This program is free software; you can redistribute it and/or modify
44
* it under the terms of the GNU General Public License as published by
45
* the Free Software Foundation; either version 2 of the License, or
46
* (at your option) any later version.
48
* This program is distributed in the hope that it will be useful,
49
* but WITHOUT ANY WARRANTY; without even the implied warranty of
50
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51
* GNU General Public License for more details.
53
* You should have received a copy of the GNU General Public License along
54
* with this program; if not, write to the Free Software Foundation, Inc.,
55
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
60
#if !defined(MBEDTLS_CONFIG_FILE)
61
#include "mbedtls/config.h"
63
#include MBEDTLS_CONFIG_FILE
66
#if defined(MBEDTLS_PK_C)
67
#include "mbedtls/pk.h"
68
#include "mbedtls/pk_internal.h"
70
#include "mbedtls/platform_util.h"
72
#if defined(MBEDTLS_RSA_C)
73
#include "mbedtls/rsa.h"
75
#if defined(MBEDTLS_ECP_C)
76
#include "mbedtls/ecp.h"
78
#if defined(MBEDTLS_ECDSA_C)
79
#include "mbedtls/ecdsa.h"
85
/* Parameter validation macros based on platform_util.h */
86
#define PK_VALIDATE_RET( cond ) \
87
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
88
#define PK_VALIDATE( cond ) \
89
MBEDTLS_INTERNAL_VALIDATE( cond )
92
* Initialise a mbedtls_pk_context
94
void mbedtls_pk_init( mbedtls_pk_context *ctx )
96
PK_VALIDATE( ctx != NULL );
103
* Free (the components of) a mbedtls_pk_context
105
void mbedtls_pk_free( mbedtls_pk_context *ctx )
110
if ( ctx->pk_info != NULL )
111
ctx->pk_info->ctx_free_func( ctx->pk_ctx );
113
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
116
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
118
* Initialize a restart context
120
void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
122
PK_VALIDATE( ctx != NULL );
128
* Free the components of a restart context
130
void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
132
if( ctx == NULL || ctx->pk_info == NULL ||
133
ctx->pk_info->rs_free_func == NULL )
138
ctx->pk_info->rs_free_func( ctx->rs_ctx );
143
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
146
* Get pk_info structure from type
148
const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
151
#if defined(MBEDTLS_RSA_C)
153
return( &mbedtls_rsa_info );
155
#if defined(MBEDTLS_ECP_C)
156
case MBEDTLS_PK_ECKEY:
157
return( &mbedtls_eckey_info );
158
case MBEDTLS_PK_ECKEY_DH:
159
return( &mbedtls_eckeydh_info );
161
#if defined(MBEDTLS_ECDSA_C)
162
case MBEDTLS_PK_ECDSA:
163
return( &mbedtls_ecdsa_info );
165
/* MBEDTLS_PK_RSA_ALT omitted on purpose */
174
int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
176
PK_VALIDATE_RET( ctx != NULL );
177
if( info == NULL || ctx->pk_info != NULL )
178
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
180
if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
181
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
188
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
190
* Initialize an RSA-alt context
192
int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
193
mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
194
mbedtls_pk_rsa_alt_sign_func sign_func,
195
mbedtls_pk_rsa_alt_key_len_func key_len_func )
197
mbedtls_rsa_alt_context *rsa_alt;
198
const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
200
PK_VALIDATE_RET( ctx != NULL );
201
if( ctx->pk_info != NULL )
202
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
204
if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
205
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
209
rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
212
rsa_alt->decrypt_func = decrypt_func;
213
rsa_alt->sign_func = sign_func;
214
rsa_alt->key_len_func = key_len_func;
218
#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
221
* Tell if a PK can do the operations of the given type
223
int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
225
/* A context with null pk_info is not set up yet and can't do anything.
226
* For backward compatibility, also accept NULL instead of a context
228
if( ctx == NULL || ctx->pk_info == NULL )
231
return( ctx->pk_info->can_do( type ) );
235
* Helper for mbedtls_pk_sign and mbedtls_pk_verify
237
static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
239
const mbedtls_md_info_t *md_info;
241
if( *hash_len != 0 && md_alg == MBEDTLS_MD_NONE )
244
if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
247
if ( *hash_len != 0 && *hash_len < mbedtls_md_get_size( md_info ) )
250
*hash_len = mbedtls_md_get_size( md_info );
254
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
256
* Helper to set up a restart context if needed
258
static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
259
const mbedtls_pk_info_t *info )
261
/* Don't do anything if already set up or invalid */
262
if( ctx == NULL || ctx->pk_info != NULL )
265
/* Should never happen when we're called */
266
if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
267
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
269
if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
270
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
276
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
279
* Verify a signature (restartable)
281
int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
282
mbedtls_md_type_t md_alg,
283
const unsigned char *hash, size_t hash_len,
284
const unsigned char *sig, size_t sig_len,
285
mbedtls_pk_restart_ctx *rs_ctx )
287
PK_VALIDATE_RET( ctx != NULL );
288
PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
290
PK_VALIDATE_RET( sig != NULL );
292
if( ctx->pk_info == NULL ||
293
pk_hashlen_helper( md_alg, &hash_len ) != 0 )
294
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
296
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
297
/* optimization: use non-restartable version if restart disabled */
298
if( rs_ctx != NULL &&
299
mbedtls_ecp_restart_is_enabled() &&
300
ctx->pk_info->verify_rs_func != NULL )
304
if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
307
ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
308
md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
310
if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
311
mbedtls_pk_restart_free( rs_ctx );
315
#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
317
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
319
if( ctx->pk_info->verify_func == NULL )
320
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
322
return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
329
int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
330
const unsigned char *hash, size_t hash_len,
331
const unsigned char *sig, size_t sig_len )
333
return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
334
sig, sig_len, NULL ) );
338
* Verify a signature with options
340
int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
341
mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
342
const unsigned char *hash, size_t hash_len,
343
const unsigned char *sig, size_t sig_len )
345
PK_VALIDATE_RET( ctx != NULL );
346
PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
348
PK_VALIDATE_RET( sig != NULL );
350
if( ctx->pk_info == NULL )
351
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
353
if( ! mbedtls_pk_can_do( ctx, type ) )
354
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
356
if( type == MBEDTLS_PK_RSASSA_PSS )
358
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
360
const mbedtls_pk_rsassa_pss_options *pss_opts;
362
#if SIZE_MAX > UINT_MAX
363
if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
364
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
365
#endif /* SIZE_MAX > UINT_MAX */
367
if( options == NULL )
368
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
370
pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
372
if( sig_len < mbedtls_pk_get_len( ctx ) )
373
return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
375
ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
376
NULL, NULL, MBEDTLS_RSA_PUBLIC,
377
md_alg, (unsigned int) hash_len, hash,
378
pss_opts->mgf1_hash_id,
379
pss_opts->expected_salt_len,
384
if( sig_len > mbedtls_pk_get_len( ctx ) )
385
return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
389
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
390
#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
393
/* General case: no options */
394
if( options != NULL )
395
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
397
return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
401
* Make a signature (restartable)
403
int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
404
mbedtls_md_type_t md_alg,
405
const unsigned char *hash, size_t hash_len,
406
unsigned char *sig, size_t *sig_len,
407
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
408
mbedtls_pk_restart_ctx *rs_ctx )
410
PK_VALIDATE_RET( ctx != NULL );
411
PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
413
PK_VALIDATE_RET( sig != NULL );
415
if( ctx->pk_info == NULL ||
416
pk_hashlen_helper( md_alg, &hash_len ) != 0 )
417
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
419
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
420
/* optimization: use non-restartable version if restart disabled */
421
if( rs_ctx != NULL &&
422
mbedtls_ecp_restart_is_enabled() &&
423
ctx->pk_info->sign_rs_func != NULL )
427
if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
430
ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
431
hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
433
if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
434
mbedtls_pk_restart_free( rs_ctx );
438
#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
440
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
442
if( ctx->pk_info->sign_func == NULL )
443
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
445
return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
446
sig, sig_len, f_rng, p_rng ) );
452
int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
453
const unsigned char *hash, size_t hash_len,
454
unsigned char *sig, size_t *sig_len,
455
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
457
return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
458
sig, sig_len, f_rng, p_rng, NULL ) );
464
int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
465
const unsigned char *input, size_t ilen,
466
unsigned char *output, size_t *olen, size_t osize,
467
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
469
PK_VALIDATE_RET( ctx != NULL );
470
PK_VALIDATE_RET( input != NULL || ilen == 0 );
471
PK_VALIDATE_RET( output != NULL || osize == 0 );
472
PK_VALIDATE_RET( olen != NULL );
474
if( ctx->pk_info == NULL )
475
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
477
if( ctx->pk_info->decrypt_func == NULL )
478
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
480
return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
481
output, olen, osize, f_rng, p_rng ) );
487
int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
488
const unsigned char *input, size_t ilen,
489
unsigned char *output, size_t *olen, size_t osize,
490
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
492
PK_VALIDATE_RET( ctx != NULL );
493
PK_VALIDATE_RET( input != NULL || ilen == 0 );
494
PK_VALIDATE_RET( output != NULL || osize == 0 );
495
PK_VALIDATE_RET( olen != NULL );
497
if( ctx->pk_info == NULL )
498
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
500
if( ctx->pk_info->encrypt_func == NULL )
501
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
503
return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
504
output, olen, osize, f_rng, p_rng ) );
508
* Check public-private key pair
510
int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
512
PK_VALIDATE_RET( pub != NULL );
513
PK_VALIDATE_RET( prv != NULL );
515
if( pub->pk_info == NULL ||
516
prv->pk_info == NULL ||
517
prv->pk_info->check_pair_func == NULL )
519
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
522
if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
524
if( pub->pk_info->type != MBEDTLS_PK_RSA )
525
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
529
if( pub->pk_info != prv->pk_info )
530
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
533
return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
537
* Get key size in bits
539
size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
541
/* For backward compatibility, accept NULL or a context that
542
* isn't set up yet, and return a fake value that should be safe. */
543
if( ctx == NULL || ctx->pk_info == NULL )
546
return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
550
* Export debug information
552
int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
554
PK_VALIDATE_RET( ctx != NULL );
555
if( ctx->pk_info == NULL )
556
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
558
if( ctx->pk_info->debug_func == NULL )
559
return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
561
ctx->pk_info->debug_func( ctx->pk_ctx, items );
566
* Access the PK type name
568
const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
570
if( ctx == NULL || ctx->pk_info == NULL )
571
return( "invalid PK" );
573
return( ctx->pk_info->name );
579
mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
581
if( ctx == NULL || ctx->pk_info == NULL )
582
return( MBEDTLS_PK_NONE );
584
return( ctx->pk_info->type );
587
#endif /* MBEDTLS_PK_C */