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: wrapper functions
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_internal.h"
69
/* Even if RSA not activated, for the sake of RSA-alt */
70
#include "mbedtls/rsa.h"
74
#if defined(MBEDTLS_ECP_C)
75
#include "mbedtls/ecp.h"
78
#if defined(MBEDTLS_ECDSA_C)
79
#include "mbedtls/ecdsa.h"
82
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
83
#include "mbedtls/platform_util.h"
86
#if defined(MBEDTLS_PLATFORM_C)
87
#include "mbedtls/platform.h"
90
#define mbedtls_calloc calloc
91
#define mbedtls_free free
97
#if defined(MBEDTLS_RSA_C)
98
static int rsa_can_do( mbedtls_pk_type_t type )
100
return( type == MBEDTLS_PK_RSA ||
101
type == MBEDTLS_PK_RSASSA_PSS );
104
static size_t rsa_get_bitlen( const void *ctx )
106
const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
107
return( 8 * mbedtls_rsa_get_len( rsa ) );
110
static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
111
const unsigned char *hash, size_t hash_len,
112
const unsigned char *sig, size_t sig_len )
115
mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
116
size_t rsa_len = mbedtls_rsa_get_len( rsa );
118
#if SIZE_MAX > UINT_MAX
119
if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
120
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
121
#endif /* SIZE_MAX > UINT_MAX */
123
if( sig_len < rsa_len )
124
return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
126
if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
127
MBEDTLS_RSA_PUBLIC, md_alg,
128
(unsigned int) hash_len, hash, sig ) ) != 0 )
131
/* The buffer contains a valid signature followed by extra data.
132
* We have a special error code for that so that so that callers can
133
* use mbedtls_pk_verify() to check "Does the buffer start with a
134
* valid signature?" and not just "Does the buffer contain a valid
136
if( sig_len > rsa_len )
137
return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
142
static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
143
const unsigned char *hash, size_t hash_len,
144
unsigned char *sig, size_t *sig_len,
145
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
147
mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
149
#if SIZE_MAX > UINT_MAX
150
if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
151
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
152
#endif /* SIZE_MAX > UINT_MAX */
154
*sig_len = mbedtls_rsa_get_len( rsa );
156
return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
157
md_alg, (unsigned int) hash_len, hash, sig ) );
160
static int rsa_decrypt_wrap( void *ctx,
161
const unsigned char *input, size_t ilen,
162
unsigned char *output, size_t *olen, size_t osize,
163
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
165
mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
167
if( ilen != mbedtls_rsa_get_len( rsa ) )
168
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
170
return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
171
MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
174
static int rsa_encrypt_wrap( void *ctx,
175
const unsigned char *input, size_t ilen,
176
unsigned char *output, size_t *olen, size_t osize,
177
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
179
mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
180
*olen = mbedtls_rsa_get_len( rsa );
183
return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
185
return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
186
ilen, input, output ) );
189
static int rsa_check_pair_wrap( const void *pub, const void *prv )
191
return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
192
(const mbedtls_rsa_context *) prv ) );
195
static void *rsa_alloc_wrap( void )
197
void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
200
mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
205
static void rsa_free_wrap( void *ctx )
207
mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
211
static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
213
items->type = MBEDTLS_PK_DEBUG_MPI;
214
items->name = "rsa.N";
215
items->value = &( ((mbedtls_rsa_context *) ctx)->N );
219
items->type = MBEDTLS_PK_DEBUG_MPI;
220
items->name = "rsa.E";
221
items->value = &( ((mbedtls_rsa_context *) ctx)->E );
224
const mbedtls_pk_info_t mbedtls_rsa_info = {
231
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
240
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
246
#endif /* MBEDTLS_RSA_C */
248
#if defined(MBEDTLS_ECP_C)
252
static int eckey_can_do( mbedtls_pk_type_t type )
254
return( type == MBEDTLS_PK_ECKEY ||
255
type == MBEDTLS_PK_ECKEY_DH ||
256
type == MBEDTLS_PK_ECDSA );
259
static size_t eckey_get_bitlen( const void *ctx )
261
return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
264
#if defined(MBEDTLS_ECDSA_C)
265
/* Forward declarations */
266
static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
267
const unsigned char *hash, size_t hash_len,
268
const unsigned char *sig, size_t sig_len );
270
static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
271
const unsigned char *hash, size_t hash_len,
272
unsigned char *sig, size_t *sig_len,
273
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
275
static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
276
const unsigned char *hash, size_t hash_len,
277
const unsigned char *sig, size_t sig_len )
280
mbedtls_ecdsa_context ecdsa;
282
mbedtls_ecdsa_init( &ecdsa );
284
if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
285
ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
287
mbedtls_ecdsa_free( &ecdsa );
292
static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
293
const unsigned char *hash, size_t hash_len,
294
unsigned char *sig, size_t *sig_len,
295
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
298
mbedtls_ecdsa_context ecdsa;
300
mbedtls_ecdsa_init( &ecdsa );
302
if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
303
ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
306
mbedtls_ecdsa_free( &ecdsa );
311
#if defined(MBEDTLS_ECP_RESTARTABLE)
312
/* Forward declarations */
313
static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
314
const unsigned char *hash, size_t hash_len,
315
const unsigned char *sig, size_t sig_len,
318
static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
319
const unsigned char *hash, size_t hash_len,
320
unsigned char *sig, size_t *sig_len,
321
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
325
* Restart context for ECDSA operations with ECKEY context
327
* We need to store an actual ECDSA context, as we need to pass the same to
328
* the underlying ecdsa function, so we can't create it on the fly every time.
332
mbedtls_ecdsa_restart_ctx ecdsa_rs;
333
mbedtls_ecdsa_context ecdsa_ctx;
336
static void *eckey_rs_alloc( void )
338
eckey_restart_ctx *rs_ctx;
340
void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
345
mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
346
mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
352
static void eckey_rs_free( void *ctx )
354
eckey_restart_ctx *rs_ctx;
360
mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
361
mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
366
static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
367
const unsigned char *hash, size_t hash_len,
368
const unsigned char *sig, size_t sig_len,
372
eckey_restart_ctx *rs = rs_ctx;
374
/* Should never happen */
376
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
378
/* set up our own sub-context if needed (that is, on first run) */
379
if( rs->ecdsa_ctx.grp.pbits == 0 )
380
MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
382
MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
383
md_alg, hash, hash_len,
384
sig, sig_len, &rs->ecdsa_rs ) );
390
static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
391
const unsigned char *hash, size_t hash_len,
392
unsigned char *sig, size_t *sig_len,
393
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
397
eckey_restart_ctx *rs = rs_ctx;
399
/* Should never happen */
401
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
403
/* set up our own sub-context if needed (that is, on first run) */
404
if( rs->ecdsa_ctx.grp.pbits == 0 )
405
MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
407
MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
408
hash, hash_len, sig, sig_len,
409
f_rng, p_rng, &rs->ecdsa_rs ) );
414
#endif /* MBEDTLS_ECP_RESTARTABLE */
415
#endif /* MBEDTLS_ECDSA_C */
417
static int eckey_check_pair( const void *pub, const void *prv )
419
return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
420
(const mbedtls_ecp_keypair *) prv ) );
423
static void *eckey_alloc_wrap( void )
425
void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
428
mbedtls_ecp_keypair_init( ctx );
433
static void eckey_free_wrap( void *ctx )
435
mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
439
static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
441
items->type = MBEDTLS_PK_DEBUG_ECP;
442
items->name = "eckey.Q";
443
items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
446
const mbedtls_pk_info_t mbedtls_eckey_info = {
451
#if defined(MBEDTLS_ECDSA_C)
454
#if defined(MBEDTLS_ECP_RESTARTABLE)
455
eckey_verify_rs_wrap,
458
#else /* MBEDTLS_ECDSA_C */
461
#endif /* MBEDTLS_ECDSA_C */
467
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
475
* EC key restricted to ECDH
477
static int eckeydh_can_do( mbedtls_pk_type_t type )
479
return( type == MBEDTLS_PK_ECKEY ||
480
type == MBEDTLS_PK_ECKEY_DH );
483
const mbedtls_pk_info_t mbedtls_eckeydh_info = {
486
eckey_get_bitlen, /* Same underlying key structure */
490
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
497
eckey_alloc_wrap, /* Same underlying key structure */
498
eckey_free_wrap, /* Same underlying key structure */
499
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
503
eckey_debug, /* Same underlying key structure */
505
#endif /* MBEDTLS_ECP_C */
507
#if defined(MBEDTLS_ECDSA_C)
508
static int ecdsa_can_do( mbedtls_pk_type_t type )
510
return( type == MBEDTLS_PK_ECDSA );
513
static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
514
const unsigned char *hash, size_t hash_len,
515
const unsigned char *sig, size_t sig_len )
520
ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
521
hash, hash_len, sig, sig_len );
523
if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
524
return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
529
static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
530
const unsigned char *hash, size_t hash_len,
531
unsigned char *sig, size_t *sig_len,
532
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
534
return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
535
md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
538
#if defined(MBEDTLS_ECP_RESTARTABLE)
539
static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
540
const unsigned char *hash, size_t hash_len,
541
const unsigned char *sig, size_t sig_len,
547
ret = mbedtls_ecdsa_read_signature_restartable(
548
(mbedtls_ecdsa_context *) ctx,
549
hash, hash_len, sig, sig_len,
550
(mbedtls_ecdsa_restart_ctx *) rs_ctx );
552
if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
553
return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
558
static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
559
const unsigned char *hash, size_t hash_len,
560
unsigned char *sig, size_t *sig_len,
561
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
564
return( mbedtls_ecdsa_write_signature_restartable(
565
(mbedtls_ecdsa_context *) ctx,
566
md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
567
(mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
570
#endif /* MBEDTLS_ECP_RESTARTABLE */
572
static void *ecdsa_alloc_wrap( void )
574
void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
577
mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
582
static void ecdsa_free_wrap( void *ctx )
584
mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
588
#if defined(MBEDTLS_ECP_RESTARTABLE)
589
static void *ecdsa_rs_alloc( void )
591
void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
594
mbedtls_ecdsa_restart_init( ctx );
599
static void ecdsa_rs_free( void *ctx )
601
mbedtls_ecdsa_restart_free( ctx );
604
#endif /* MBEDTLS_ECP_RESTARTABLE */
606
const mbedtls_pk_info_t mbedtls_ecdsa_info = {
609
eckey_get_bitlen, /* Compatible key structures */
613
#if defined(MBEDTLS_ECP_RESTARTABLE)
614
ecdsa_verify_rs_wrap,
619
eckey_check_pair, /* Compatible key structures */
622
#if defined(MBEDTLS_ECP_RESTARTABLE)
626
eckey_debug, /* Compatible key structures */
628
#endif /* MBEDTLS_ECDSA_C */
630
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
632
* Support for alternative RSA-private implementations
635
static int rsa_alt_can_do( mbedtls_pk_type_t type )
637
return( type == MBEDTLS_PK_RSA );
640
static size_t rsa_alt_get_bitlen( const void *ctx )
642
const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
644
return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
647
static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
648
const unsigned char *hash, size_t hash_len,
649
unsigned char *sig, size_t *sig_len,
650
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
652
mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
654
#if SIZE_MAX > UINT_MAX
655
if( UINT_MAX < hash_len )
656
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
657
#endif /* SIZE_MAX > UINT_MAX */
659
*sig_len = rsa_alt->key_len_func( rsa_alt->key );
661
return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
662
md_alg, (unsigned int) hash_len, hash, sig ) );
665
static int rsa_alt_decrypt_wrap( void *ctx,
666
const unsigned char *input, size_t ilen,
667
unsigned char *output, size_t *olen, size_t osize,
668
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
670
mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
675
if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
676
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
678
return( rsa_alt->decrypt_func( rsa_alt->key,
679
MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
682
#if defined(MBEDTLS_RSA_C)
683
static int rsa_alt_check_pair( const void *pub, const void *prv )
685
unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
686
unsigned char hash[32];
690
if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
691
return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
693
memset( hash, 0x2a, sizeof( hash ) );
695
if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
696
hash, sizeof( hash ),
697
sig, &sig_len, NULL, NULL ) ) != 0 )
702
if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
703
hash, sizeof( hash ), sig, sig_len ) != 0 )
705
return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
710
#endif /* MBEDTLS_RSA_C */
712
static void *rsa_alt_alloc_wrap( void )
714
void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
717
memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
722
static void rsa_alt_free_wrap( void *ctx )
724
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
728
const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
735
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
739
rsa_alt_decrypt_wrap,
741
#if defined(MBEDTLS_RSA_C)
748
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
755
#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
757
#endif /* MBEDTLS_PK_C */