blob: eaaa371b7f69e6e58185f320b2141f131b52dd8b [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020030#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020031
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +020032#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
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é-Gonnard4ed179f2019-09-19 10:45:14 +020041#if defined(MBEDTLS_USE_TINYCRYPT)
42#include "tinycrypt/ecc.h"
43#include "tinycrypt/ecc_dsa.h"
44#include "mbedtls/asn1.h"
45#include "mbedtls/asn1write.h"
46#endif /* MBEDTLS_USE_TINYCRYPT */
47
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020048#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020049
50#if defined(MBEDTLS_PLATFORM_C)
51#include "mbedtls/platform.h"
52#else
53#include <stdlib.h>
54#define mbedtls_calloc calloc
55#define mbedtls_free free
56#endif
57
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +020058#include <string.h>
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020059#include <limits.h>
60#include <stdint.h>
61
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +020062/* Parameter validation macros based on platform_util.h */
63#define PK_VALIDATE_RET( cond ) \
64 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
65#define PK_VALIDATE( cond ) \
66 MBEDTLS_INTERNAL_VALIDATE( cond )
67
68/*
69 * Internal wrappers around RSA functions
70 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020071#if defined(MBEDTLS_RSA_C)
72static int rsa_can_do( mbedtls_pk_type_t type )
73{
74 return( type == MBEDTLS_PK_RSA ||
75 type == MBEDTLS_PK_RSASSA_PSS );
76}
77
78static size_t rsa_get_bitlen( const void *ctx )
79{
80 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
81 return( 8 * mbedtls_rsa_get_len( rsa ) );
82}
83
84static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
85 const unsigned char *hash, size_t hash_len,
86 const unsigned char *sig, size_t sig_len )
87{
88 int ret;
89 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
90 size_t rsa_len = mbedtls_rsa_get_len( rsa );
91
92#if SIZE_MAX > UINT_MAX
93 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
94 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
95#endif /* SIZE_MAX > UINT_MAX */
96
97 if( sig_len < rsa_len )
98 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
99
100 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
101 MBEDTLS_RSA_PUBLIC, md_alg,
102 (unsigned int) hash_len, hash, sig ) ) != 0 )
103 return( ret );
104
105 /* The buffer contains a valid signature followed by extra data.
106 * We have a special error code for that so that so that callers can
107 * use mbedtls_pk_verify() to check "Does the buffer start with a
108 * valid signature?" and not just "Does the buffer contain a valid
109 * signature?". */
110 if( sig_len > rsa_len )
111 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
112
113 return( 0 );
114}
115
116static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
117 const unsigned char *hash, size_t hash_len,
118 unsigned char *sig, size_t *sig_len,
119 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
120{
121 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
122
123#if SIZE_MAX > UINT_MAX
124 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
125 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
126#endif /* SIZE_MAX > UINT_MAX */
127
128 *sig_len = mbedtls_rsa_get_len( rsa );
129
130 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
131 md_alg, (unsigned int) hash_len, hash, sig ) );
132}
133
134static int rsa_decrypt_wrap( void *ctx,
135 const unsigned char *input, size_t ilen,
136 unsigned char *output, size_t *olen, size_t osize,
137 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
138{
139 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
140
141 if( ilen != mbedtls_rsa_get_len( rsa ) )
142 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
143
144 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
145 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
146}
147
148static int rsa_encrypt_wrap( void *ctx,
149 const unsigned char *input, size_t ilen,
150 unsigned char *output, size_t *olen, size_t osize,
151 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
152{
153 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
154 *olen = mbedtls_rsa_get_len( rsa );
155
156 if( *olen > osize )
157 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
158
159 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
160 ilen, input, output ) );
161}
162
163static int rsa_check_pair_wrap( const void *pub, const void *prv )
164{
165 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
166 (const mbedtls_rsa_context *) prv ) );
167}
168
169static void *rsa_alloc_wrap( void )
170{
171 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
172
173 if( ctx != NULL )
174 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
175
176 return( ctx );
177}
178
179static void rsa_free_wrap( void *ctx )
180{
181 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
182 mbedtls_free( ctx );
183}
184
185static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
186{
187 items->type = MBEDTLS_PK_DEBUG_MPI;
188 items->name = "rsa.N";
189 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
190
191 items++;
192
193 items->type = MBEDTLS_PK_DEBUG_MPI;
194 items->name = "rsa.E";
195 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
196}
197
198const mbedtls_pk_info_t mbedtls_rsa_info = {
199 MBEDTLS_PK_RSA,
200 "RSA",
201 rsa_get_bitlen,
202 rsa_can_do,
203 rsa_verify_wrap,
204 rsa_sign_wrap,
205#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
206 NULL,
207 NULL,
208#endif
209 rsa_decrypt_wrap,
210 rsa_encrypt_wrap,
211 rsa_check_pair_wrap,
212 rsa_alloc_wrap,
213 rsa_free_wrap,
214#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
215 NULL,
216 NULL,
217#endif
218 rsa_debug,
219};
220#endif /* MBEDTLS_RSA_C */
221
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200222/*
223 * Internal wrappers around ECC functions - based on ECP module
224 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200225#if defined(MBEDTLS_ECP_C)
226/*
227 * Generic EC key
228 */
229static int eckey_can_do( mbedtls_pk_type_t type )
230{
231 return( type == MBEDTLS_PK_ECKEY ||
232 type == MBEDTLS_PK_ECKEY_DH ||
233 type == MBEDTLS_PK_ECDSA );
234}
235
236static size_t eckey_get_bitlen( const void *ctx )
237{
238 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
239}
240
241#if defined(MBEDTLS_ECDSA_C)
242/* Forward declarations */
243static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
244 const unsigned char *hash, size_t hash_len,
245 const unsigned char *sig, size_t sig_len );
246
247static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
248 const unsigned char *hash, size_t hash_len,
249 unsigned char *sig, size_t *sig_len,
250 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
251
252static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
253 const unsigned char *hash, size_t hash_len,
254 const unsigned char *sig, size_t sig_len )
255{
256 int ret;
257 mbedtls_ecdsa_context ecdsa;
258
259 mbedtls_ecdsa_init( &ecdsa );
260
261 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
262 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
263
264 mbedtls_ecdsa_free( &ecdsa );
265
266 return( ret );
267}
268
269static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
270 const unsigned char *hash, size_t hash_len,
271 unsigned char *sig, size_t *sig_len,
272 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
273{
274 int ret;
275 mbedtls_ecdsa_context ecdsa;
276
277 mbedtls_ecdsa_init( &ecdsa );
278
279 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
280 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
281 f_rng, p_rng );
282
283 mbedtls_ecdsa_free( &ecdsa );
284
285 return( ret );
286}
287
288#if defined(MBEDTLS_ECP_RESTARTABLE)
289/* Forward declarations */
290static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
291 const unsigned char *hash, size_t hash_len,
292 const unsigned char *sig, size_t sig_len,
293 void *rs_ctx );
294
295static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
296 const unsigned char *hash, size_t hash_len,
297 unsigned char *sig, size_t *sig_len,
298 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
299 void *rs_ctx );
300
301/*
302 * Restart context for ECDSA operations with ECKEY context
303 *
304 * We need to store an actual ECDSA context, as we need to pass the same to
305 * the underlying ecdsa function, so we can't create it on the fly every time.
306 */
307typedef struct
308{
309 mbedtls_ecdsa_restart_ctx ecdsa_rs;
310 mbedtls_ecdsa_context ecdsa_ctx;
311} eckey_restart_ctx;
312
313static void *eckey_rs_alloc( void )
314{
315 eckey_restart_ctx *rs_ctx;
316
317 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
318
319 if( ctx != NULL )
320 {
321 rs_ctx = ctx;
322 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
323 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
324 }
325
326 return( ctx );
327}
328
329static void eckey_rs_free( void *ctx )
330{
331 eckey_restart_ctx *rs_ctx;
332
333 if( ctx == NULL)
334 return;
335
336 rs_ctx = ctx;
337 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
338 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
339
340 mbedtls_free( ctx );
341}
342
343static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
344 const unsigned char *hash, size_t hash_len,
345 const unsigned char *sig, size_t sig_len,
346 void *rs_ctx )
347{
348 int ret;
349 eckey_restart_ctx *rs = rs_ctx;
350
351 /* Should never happen */
352 if( rs == NULL )
353 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
354
355 /* set up our own sub-context if needed (that is, on first run) */
356 if( rs->ecdsa_ctx.grp.pbits == 0 )
357 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
358
359 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
360 md_alg, hash, hash_len,
361 sig, sig_len, &rs->ecdsa_rs ) );
362
363cleanup:
364 return( ret );
365}
366
367static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
368 const unsigned char *hash, size_t hash_len,
369 unsigned char *sig, size_t *sig_len,
370 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
371 void *rs_ctx )
372{
373 int ret;
374 eckey_restart_ctx *rs = rs_ctx;
375
376 /* Should never happen */
377 if( rs == NULL )
378 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
379
380 /* set up our own sub-context if needed (that is, on first run) */
381 if( rs->ecdsa_ctx.grp.pbits == 0 )
382 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
383
384 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
385 hash, hash_len, sig, sig_len,
386 f_rng, p_rng, &rs->ecdsa_rs ) );
387
388cleanup:
389 return( ret );
390}
391#endif /* MBEDTLS_ECP_RESTARTABLE */
392#endif /* MBEDTLS_ECDSA_C */
393
394static int eckey_check_pair( const void *pub, const void *prv )
395{
396 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
397 (const mbedtls_ecp_keypair *) prv ) );
398}
399
400static void *eckey_alloc_wrap( void )
401{
402 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
403
404 if( ctx != NULL )
405 mbedtls_ecp_keypair_init( ctx );
406
407 return( ctx );
408}
409
410static void eckey_free_wrap( void *ctx )
411{
412 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
413 mbedtls_free( ctx );
414}
415
416static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
417{
418 items->type = MBEDTLS_PK_DEBUG_ECP;
419 items->name = "eckey.Q";
420 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
421}
422
423const mbedtls_pk_info_t mbedtls_eckey_info = {
424 MBEDTLS_PK_ECKEY,
425 "EC",
426 eckey_get_bitlen,
427 eckey_can_do,
428#if defined(MBEDTLS_ECDSA_C)
429 eckey_verify_wrap,
430 eckey_sign_wrap,
431#if defined(MBEDTLS_ECP_RESTARTABLE)
432 eckey_verify_rs_wrap,
433 eckey_sign_rs_wrap,
434#endif
435#else /* MBEDTLS_ECDSA_C */
436 NULL,
437 NULL,
438#endif /* MBEDTLS_ECDSA_C */
439 NULL,
440 NULL,
441 eckey_check_pair,
442 eckey_alloc_wrap,
443 eckey_free_wrap,
444#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
445 eckey_rs_alloc,
446 eckey_rs_free,
447#endif
448 eckey_debug,
449};
450
451/*
452 * EC key restricted to ECDH
453 */
454static int eckeydh_can_do( mbedtls_pk_type_t type )
455{
456 return( type == MBEDTLS_PK_ECKEY ||
457 type == MBEDTLS_PK_ECKEY_DH );
458}
459
460const mbedtls_pk_info_t mbedtls_eckeydh_info = {
461 MBEDTLS_PK_ECKEY_DH,
462 "EC_DH",
463 eckey_get_bitlen, /* Same underlying key structure */
464 eckeydh_can_do,
465 NULL,
466 NULL,
467#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
468 NULL,
469 NULL,
470#endif
471 NULL,
472 NULL,
473 eckey_check_pair,
474 eckey_alloc_wrap, /* Same underlying key structure */
475 eckey_free_wrap, /* Same underlying key structure */
476#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
477 NULL,
478 NULL,
479#endif
480 eckey_debug, /* Same underlying key structure */
481};
482#endif /* MBEDTLS_ECP_C */
483
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200484/*
485 * Internal wrappers around ECC functions - based on TinyCrypt
486 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200487#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200488/*
489 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
490 * those integers and convert it to the fixed-length encoding.
491 */
492static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
493 unsigned char *to, size_t to_len )
494{
495 int ret;
496 size_t unpadded_len, padding_len;
497
498 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
499 MBEDTLS_ASN1_INTEGER ) ) != 0 )
500 {
501 return( ret );
502 }
503
504 while( unpadded_len > 0 && **from == 0x00 )
505 {
506 ( *from )++;
507 unpadded_len--;
508 }
509
510 if( unpadded_len > to_len || unpadded_len == 0 )
511 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
512
513 padding_len = to_len - unpadded_len;
514 memset( to, 0x00, padding_len );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300515 mbedtls_platform_memcpy( to + padding_len, *from, unpadded_len );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200516 ( *from ) += unpadded_len;
517
518 return( 0 );
519}
520
521/*
522 * Convert a signature from an ASN.1 sequence of two integers
523 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
524 * twice as big as int_size.
525 */
526static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
527 unsigned char *sig, size_t int_size )
528{
529 int ret;
530 size_t tmp_size;
531
532 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
533 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
534 return( ret );
535
536 /* Extract r */
537 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
538 return( ret );
539 /* Extract s */
540 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
541 return( ret );
542
543 return( 0 );
544}
545
546static size_t uecc_eckey_get_bitlen( const void *ctx )
547{
548 (void) ctx;
549 return( (size_t) ( NUM_ECC_BYTES * 8 ) );
550}
551
552static int uecc_eckey_check_pair( const void *pub, const void *prv )
553{
554 const mbedtls_uecc_keypair *uecc_pub =
555 (const mbedtls_uecc_keypair *) pub;
556 const mbedtls_uecc_keypair *uecc_prv =
557 (const mbedtls_uecc_keypair *) prv;
558
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300559 if( mbedtls_platform_memcmp( uecc_pub->public_key,
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200560 uecc_prv->public_key,
561 2 * NUM_ECC_BYTES ) == 0 )
562 {
563 return( 0 );
564 }
565
566 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
567}
568
569static int uecc_eckey_can_do( mbedtls_pk_type_t type )
570{
571 return( type == MBEDTLS_PK_ECDSA ||
572 type == MBEDTLS_PK_ECKEY );
573}
574
575static int uecc_eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
576 const unsigned char *hash, size_t hash_len,
577 const unsigned char *sig, size_t sig_len )
578{
579 int ret;
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100580 volatile int ret_fi;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200581 uint8_t signature[2*NUM_ECC_BYTES];
582 unsigned char *p;
583 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
584 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
585
586 ((void) md_alg);
587 p = (unsigned char*) sig;
588
589 ret = extract_ecdsa_sig( &p, sig + sig_len, signature, NUM_ECC_BYTES );
590 if( ret != 0 )
591 return( ret );
592
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100593 ret_fi = uECC_verify( keypair->public_key, hash,
594 (unsigned) hash_len, signature, uecc_curve );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200595
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100596 if( ret_fi == UECC_ATTACK_DETECTED )
597 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
598
599 if( ret_fi == UECC_SUCCESS )
600 {
601 if( ret_fi == UECC_SUCCESS )
602 return( 0 );
603 else
604 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
605 }
606
607 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200608}
609
610/*
611 * Simultaneously convert and move raw MPI from the beginning of a buffer
612 * to an ASN.1 MPI at the end of the buffer.
613 * See also mbedtls_asn1_write_mpi().
614 *
615 * p: pointer to the end of the output buffer
616 * start: start of the output buffer, and also of the mpi to write at the end
617 * n_len: length of the mpi to read from start
618 *
619 * Warning:
620 * The total length of the output buffer must be smaller than 128 Bytes.
621 */
622static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
623 size_t n_len )
624{
625 size_t len = 0;
626
627 if( (size_t)( *p - start ) < n_len )
628 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
629
630 len = n_len;
631 *p -= len;
632 memmove( *p, start, len );
633
634 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
635 * Neither r nor s should be 0, but as a failsafe measure, still detect
636 * that rather than overflowing the buffer in case of an error. */
637 while( len > 0 && **p == 0x00 )
638 {
639 ++(*p);
640 --len;
641 }
642
643 /* this is only reached if the signature was invalid */
644 if( len == 0 )
645 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
646
647 /* if the msb is 1, ASN.1 requires that we prepend a 0.
648 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
649 if( **p & 0x80 )
650 {
651 if( *p - start < 1 )
652 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
653
654 *--(*p) = 0x00;
655 len += 1;
656 }
657
658 /* The ASN.1 length encoding is just a single Byte containing the length,
659 * as we assume that the total buffer length is smaller than 128 Bytes. */
660 *--(*p) = len;
661 *--(*p) = MBEDTLS_ASN1_INTEGER;
662 len += 2;
663
664 return( (int) len );
665}
666
667/* Transcode signature from uECC format to ASN.1 sequence.
668 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
669 * MPIs, and in-place.
670 *
671 * [in/out] sig: the signature pre- and post-transcoding
672 * [in/out] sig_len: signature length pre- and post-transcoding
673 * [int] buf_len: the available size the in/out buffer
674 *
675 * Warning: buf_len must be smaller than 128 Bytes.
676 */
677static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
678 size_t buf_len )
679{
680 int ret;
681 size_t len = 0;
682 const size_t rs_len = *sig_len / 2;
683 unsigned char *p = sig + buf_len;
684
685 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
686 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
687
688 /* The ASN.1 length encoding is just a single Byte containing the length,
689 * as we assume that the total buffer length is smaller than 128 Bytes. */
690 *--p = len;
691 *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
692 len += 2;
693
694 memmove( sig, p, len );
695 *sig_len = len;
696
697 return( 0 );
698}
699
700static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
701 const unsigned char *hash, size_t hash_len,
702 unsigned char *sig, size_t *sig_len,
703 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
704{
705 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
706 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
707 int ret;
708
709 /*
710 * RFC-4492 page 20:
711 *
712 * Ecdsa-Sig-Value ::= SEQUENCE {
713 * r INTEGER,
714 * s INTEGER
715 * }
716 *
717 * Size is at most
718 * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s,
719 * twice that + 1 (tag) + 2 (len) for the sequence
720 *
721 * (The ASN.1 length encodings are all 1-Byte encodings because
722 * the total size is smaller than 128 Bytes).
723 */
724 #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) )
725
726 ret = uECC_sign( keypair->private_key, hash, hash_len, sig, uecc_curve );
727 /* TinyCrypt uses 0 to signal errors. */
728 if( ret == 0 )
729 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
730
731 *sig_len = 2 * NUM_ECC_BYTES;
732
733 /* uECC owns its rng function pointer */
734 (void) f_rng;
735 (void) p_rng;
736 (void) md_alg;
737
738 return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len,
739 MAX_SECP256R1_ECDSA_SIG_LEN ) );
740
741 #undef MAX_SECP256R1_ECDSA_SIG_LEN
742}
743
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200744#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200745static void *uecc_eckey_alloc_wrap( void )
746{
747 return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) );
748}
749
750static void uecc_eckey_free_wrap( void *ctx )
751{
752 if( ctx == NULL )
753 return;
754
755 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) );
756 mbedtls_free( ctx );
757}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200758#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200759
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200760#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200761const mbedtls_pk_info_t mbedtls_uecc_eckey_info =
762 MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY );
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200763#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200764#endif /* MBEDTLS_USE_TINYCRYPT */
765
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200766/*
767 * Internal wrappers around ECDSA functions
768 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200769#if defined(MBEDTLS_ECDSA_C)
770static int ecdsa_can_do( mbedtls_pk_type_t type )
771{
772 return( type == MBEDTLS_PK_ECDSA );
773}
774
775static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
776 const unsigned char *hash, size_t hash_len,
777 const unsigned char *sig, size_t sig_len )
778{
779 int ret;
780 ((void) md_alg);
781
782 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
783 hash, hash_len, sig, sig_len );
784
785 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
786 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
787
788 return( ret );
789}
790
791static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
792 const unsigned char *hash, size_t hash_len,
793 unsigned char *sig, size_t *sig_len,
794 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
795{
796 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
797 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
798}
799
800#if defined(MBEDTLS_ECP_RESTARTABLE)
801static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
802 const unsigned char *hash, size_t hash_len,
803 const unsigned char *sig, size_t sig_len,
804 void *rs_ctx )
805{
806 int ret;
807 ((void) md_alg);
808
809 ret = mbedtls_ecdsa_read_signature_restartable(
810 (mbedtls_ecdsa_context *) ctx,
811 hash, hash_len, sig, sig_len,
812 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
813
814 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
815 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
816
817 return( ret );
818}
819
820static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
821 const unsigned char *hash, size_t hash_len,
822 unsigned char *sig, size_t *sig_len,
823 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
824 void *rs_ctx )
825{
826 return( mbedtls_ecdsa_write_signature_restartable(
827 (mbedtls_ecdsa_context *) ctx,
828 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
829 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
830
831}
832#endif /* MBEDTLS_ECP_RESTARTABLE */
833
834static void *ecdsa_alloc_wrap( void )
835{
836 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
837
838 if( ctx != NULL )
839 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
840
841 return( ctx );
842}
843
844static void ecdsa_free_wrap( void *ctx )
845{
846 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
847 mbedtls_free( ctx );
848}
849
850#if defined(MBEDTLS_ECP_RESTARTABLE)
851static void *ecdsa_rs_alloc( void )
852{
853 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
854
855 if( ctx != NULL )
856 mbedtls_ecdsa_restart_init( ctx );
857
858 return( ctx );
859}
860
861static void ecdsa_rs_free( void *ctx )
862{
863 mbedtls_ecdsa_restart_free( ctx );
864 mbedtls_free( ctx );
865}
866#endif /* MBEDTLS_ECP_RESTARTABLE */
867
868const mbedtls_pk_info_t mbedtls_ecdsa_info = {
869 MBEDTLS_PK_ECDSA,
870 "ECDSA",
871 eckey_get_bitlen, /* Compatible key structures */
872 ecdsa_can_do,
873 ecdsa_verify_wrap,
874 ecdsa_sign_wrap,
875#if defined(MBEDTLS_ECP_RESTARTABLE)
876 ecdsa_verify_rs_wrap,
877 ecdsa_sign_rs_wrap,
878#endif
879 NULL,
880 NULL,
881 eckey_check_pair, /* Compatible key structures */
882 ecdsa_alloc_wrap,
883 ecdsa_free_wrap,
884#if defined(MBEDTLS_ECP_RESTARTABLE)
885 ecdsa_rs_alloc,
886 ecdsa_rs_free,
887#endif
888 eckey_debug, /* Compatible key structures */
889};
890#endif /* MBEDTLS_ECDSA_C */
891
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200892/*
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200893 * Internal wrappers for RSA-alt support
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200894 */
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200895#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200896static int rsa_alt_can_do( mbedtls_pk_type_t type )
897{
898 return( type == MBEDTLS_PK_RSA );
899}
900
901static size_t rsa_alt_get_bitlen( const void *ctx )
902{
903 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
904
905 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
906}
907
908static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
909 const unsigned char *hash, size_t hash_len,
910 unsigned char *sig, size_t *sig_len,
911 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
912{
913 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
914
915#if SIZE_MAX > UINT_MAX
916 if( UINT_MAX < hash_len )
917 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
918#endif /* SIZE_MAX > UINT_MAX */
919
920 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
921
922 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
923 md_alg, (unsigned int) hash_len, hash, sig ) );
924}
925
926static int rsa_alt_decrypt_wrap( void *ctx,
927 const unsigned char *input, size_t ilen,
928 unsigned char *output, size_t *olen, size_t osize,
929 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
930{
931 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
932
933 ((void) f_rng);
934 ((void) p_rng);
935
936 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
937 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
938
939 return( rsa_alt->decrypt_func( rsa_alt->key,
940 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
941}
942
943#if defined(MBEDTLS_RSA_C)
944static int rsa_alt_check_pair( const void *pub, const void *prv )
945{
946 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
947 unsigned char hash[32];
948 size_t sig_len = 0;
949 int ret;
950
951 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
952 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
953
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200954 mbedtls_platform_memset( hash, 0x2a, sizeof( hash ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200955
956 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
957 hash, sizeof( hash ),
958 sig, &sig_len, NULL, NULL ) ) != 0 )
959 {
960 return( ret );
961 }
962
963 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
964 hash, sizeof( hash ), sig, sig_len ) != 0 )
965 {
966 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
967 }
968
969 return( 0 );
970}
971#endif /* MBEDTLS_RSA_C */
972
973static void *rsa_alt_alloc_wrap( void )
974{
975 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
976
977 if( ctx != NULL )
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200978 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200979
980 return( ctx );
981}
982
983static void rsa_alt_free_wrap( void *ctx )
984{
985 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
986 mbedtls_free( ctx );
987}
988
989const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
990 MBEDTLS_PK_RSA_ALT,
991 "RSA-alt",
992 rsa_alt_get_bitlen,
993 rsa_alt_can_do,
994 NULL,
995 rsa_alt_sign_wrap,
996#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
997 NULL,
998 NULL,
999#endif
1000 rsa_alt_decrypt_wrap,
1001 NULL,
1002#if defined(MBEDTLS_RSA_C)
1003 rsa_alt_check_pair,
1004#else
1005 NULL,
1006#endif
1007 rsa_alt_alloc_wrap,
1008 rsa_alt_free_wrap,
1009#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1010 NULL,
1011 NULL,
1012#endif
1013 NULL,
1014};
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +02001015#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
1016
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001017/*
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001018 * Access to members of the pk_info structure. When a single PK type is
1019 * hardcoded, these should have zero runtime cost; otherwise, the usual
1020 * dynamic dispatch based on pk_info is used.
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001021 *
1022 * For function members, don't make a getter, but a function that directly
1023 * calls the method, so that we can entirely get rid of function pointers
1024 * when hardcoding a single PK - some compilers optimize better that way.
1025 *
1026 * Not implemented for members that are only present in builds with
Manuel Pégourié-Gonnard8b5e6bd2019-09-20 08:57:18 +02001027 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for this is builds
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001028 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
1029 */
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001030#if defined(MBEDTLS_PK_SINGLE_TYPE)
1031
1032MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001033 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001034{
1035 (void) info;
1036 return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) );
1037}
1038
1039MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001040 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001041{
1042 (void) info;
1043 return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) );
1044}
1045
1046MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001047 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001048{
1049 (void) info;
1050 return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) );
1051}
1052
1053MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001054 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001055{
1056 (void) info;
1057 return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) );
1058}
1059
1060MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001061 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001062 const unsigned char *hash, size_t hash_len,
1063 const unsigned char *sig, size_t sig_len )
1064{
1065 (void) info;
1066#if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1067 (void) ctx;
1068 (void) md_alg;
1069 (void) hash;
1070 (void) hash_len;
1071 (void) sig;
1072 (void) sig_len;
1073 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1074#else
1075 return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1076 ctx, md_alg, hash, hash_len, sig, sig_len ) );
1077#endif
1078}
1079
1080MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001081 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001082 const unsigned char *hash, size_t hash_len,
1083 unsigned char *sig, size_t *sig_len,
1084 int (*f_rng)(void *, unsigned char *, size_t),
1085 void *p_rng )
1086{
1087 (void) info;
1088#if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1089 (void) ctx;
1090 (void) md_alg;
1091 (void) hash;
1092 (void) hash_len;
1093 (void) sig;
1094 (void) sig_len;
1095 (void) f_rng;
1096 (void) p_rng;
1097 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1098#else
1099 return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1100 ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
1101#endif
1102}
1103
1104MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001105 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001106 const unsigned char *input, size_t ilen,
1107 unsigned char *output, size_t *olen, size_t osize,
1108 int (*f_rng)(void *, unsigned char *, size_t),
1109 void *p_rng )
1110{
1111 (void) info;
1112#if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1113 (void) ctx;
1114 (void) input;
1115 (void) ilen;
1116 (void) output;
1117 (void) olen;
1118 (void) osize;
1119 (void) f_rng;
1120 (void) p_rng;
1121 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1122#else
1123 return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1124 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1125#endif
1126}
1127
1128MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001129 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001130 const unsigned char *input, size_t ilen,
1131 unsigned char *output, size_t *olen, size_t osize,
1132 int (*f_rng)(void *, unsigned char *, size_t),
1133 void *p_rng )
1134{
1135 (void) info;
1136#if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1137 (void) ctx;
1138 (void) input;
1139 (void) ilen;
1140 (void) output;
1141 (void) olen;
1142 (void) osize;
1143 (void) f_rng;
1144 (void) p_rng;
1145 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1146#else
1147 return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1148 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1149#endif
1150}
1151
1152MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001153 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001154{
1155 (void) info;
1156#if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1157 (void) pub;
1158 (void) prv;
1159 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1160#else
1161 return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1162 pub, prv ) );
1163#endif
1164}
1165
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001166MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001167 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001168 const void *ctx, mbedtls_pk_debug_item *items )
1169{
1170 (void) info;
1171#if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1172 (void) ctx;
1173 (void) items;
1174 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1175#else
1176 return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) );
1177#endif
1178}
1179
1180#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001181
1182MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001183 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001184{
1185 return( info->type );
1186}
1187
1188MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001189 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001190{
1191 return( info->name );
1192}
1193
1194MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001195 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001196{
1197 return( info->get_bitlen( ctx ) );
1198}
1199
1200MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001201 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001202{
1203 return( info->can_do( type ) );
1204}
1205
1206MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001207 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001208 const unsigned char *hash, size_t hash_len,
1209 const unsigned char *sig, size_t sig_len )
1210{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001211 if( info->verify_func == NULL )
1212 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1213
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001214 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
1215}
1216
1217MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001218 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001219 const unsigned char *hash, size_t hash_len,
1220 unsigned char *sig, size_t *sig_len,
1221 int (*f_rng)(void *, unsigned char *, size_t),
1222 void *p_rng )
1223{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001224 if( info->sign_func == NULL )
1225 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1226
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001227 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
1228 f_rng, p_rng ) );
1229}
1230
1231MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001232 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001233 const unsigned char *input, size_t ilen,
1234 unsigned char *output, size_t *olen, size_t osize,
1235 int (*f_rng)(void *, unsigned char *, size_t),
1236 void *p_rng )
1237{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001238 if( info->decrypt_func == NULL )
1239 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1240
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001241 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
1242 f_rng, p_rng ) );
1243}
1244
1245MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001246 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001247 const unsigned char *input, size_t ilen,
1248 unsigned char *output, size_t *olen, size_t osize,
1249 int (*f_rng)(void *, unsigned char *, size_t),
1250 void *p_rng )
1251{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001252 if( info->encrypt_func == NULL )
1253 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1254
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001255 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
1256 f_rng, p_rng ) );
1257}
1258
1259MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001260 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001261{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001262 if( info->check_pair_func == NULL )
1263 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1264
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001265 return( info->check_pair_func( pub, prv ) );
1266}
1267
1268MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001269 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001270{
1271 return( info->ctx_alloc_func( ) );
1272}
1273
1274MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001275 mbedtls_pk_handle_t info, void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001276{
1277 info->ctx_free_func( ctx );
1278}
1279
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001280MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001281 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001282 const void *ctx, mbedtls_pk_debug_item *items )
1283{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001284 if( info->debug_func == NULL )
1285 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1286
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001287 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001288 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001289}
1290
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001291#endif /* MBEDTLS_PK_SINGLE_TYPE */
1292
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001293/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001297{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001298 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001299
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001300#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001301 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001302 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001303#else
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +02001304 memset( ctx, 0, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001305#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001306}
1307
1308/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001312{
irwir2239a862018-06-12 18:25:09 +03001313 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001314 return;
1315
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001316#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001317 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
1318 pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001319#endif
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +02001320
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001321 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001322}
1323
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001324#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001325/*
1326 * Initialize a restart context
1327 */
1328void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
1329{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001330 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001331 ctx->pk_info = NULL;
1332 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001333}
1334
1335/*
1336 * Free the components of a restart context
1337 */
1338void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
1339{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001340 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001341 ctx->pk_info->rs_free_func == NULL )
1342 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001343 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001344 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001345
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001346 ctx->pk_info->rs_free_func( ctx->rs_ctx );
1347
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001348 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001349 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001350}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001351#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001352
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001353/*
1354 * Get pk_info structure from type
1355 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001356mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001357{
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001358#if defined(MBEDTLS_PK_SINGLE_TYPE)
1359 if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) )
1360 return( MBEDTLS_PK_UNIQUE_VALID_HANDLE );
1361
1362 return( MBEDTLS_PK_INVALID_HANDLE );
1363
1364#else /* MBEDTLS_PK_SINGLE_TYPE */
1365
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001366 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_RSA_C)
1368 case MBEDTLS_PK_RSA:
1369 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001370#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 case MBEDTLS_PK_ECKEY_DH:
1373 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_ECDSA_C)
1376 case MBEDTLS_PK_ECDSA:
1377 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001378#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +01001379#if defined(MBEDTLS_USE_TINYCRYPT)
1380 case MBEDTLS_PK_ECKEY:
1381 return( &mbedtls_uecc_eckey_info );
1382#else /* MBEDTLS_USE_TINYCRYPT */
1383#if defined(MBEDTLS_ECP_C)
1384 case MBEDTLS_PK_ECKEY:
1385 return( &mbedtls_eckey_info );
1386#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +03001387#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001389 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001390 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001391 }
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001392#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001393}
1394
1395/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02001396 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001397 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001398int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001399{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001400 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001401 if( info == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001403
1404#if !defined(MBEDTLS_PK_SINGLE_TYPE)
1405 if( ctx->pk_info != NULL )
1406 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1407
1408 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001409
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001410 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001411 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001412#else
1413 (void) ctx;
1414#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001415
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001416 return( 0 );
1417}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +01001420/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001421 * Initialize an RSA-alt context
1422 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001423int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
1425 mbedtls_pk_rsa_alt_sign_func sign_func,
1426 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_rsa_alt_context *rsa_alt;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001429 mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001430
Gilles Peskinee97dc602018-12-19 00:51:38 +01001431 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001432 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001434
1435 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001436 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001437
1438 ctx->pk_info = info;
1439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001441
1442 rsa_alt->key = key;
1443 rsa_alt->decrypt_func = decrypt_func;
1444 rsa_alt->sign_func = sign_func;
1445 rsa_alt->key_len_func = key_len_func;
1446
1447 return( 0 );
1448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001450
1451/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001452 * Tell if a PK can do the operations of the given type
1453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001455{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001456 /* A context with null pk_info is not set up yet and can't do anything.
1457 * For backward compatibility, also accept NULL instead of a context
1458 * pointer. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001459 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001460 return( 0 );
1461
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001462 return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001463}
1464
1465/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001469{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001470 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001471
1472 if( *hash_len != 0 )
1473 return( 0 );
1474
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001475 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
1476 MBEDTLS_MD_INVALID_HANDLE )
1477 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001478 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001479 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001482 return( 0 );
1483}
1484
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001485#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001486/*
1487 * Helper to set up a restart context if needed
1488 */
1489static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001490 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001491{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001492 /* Don't do anything if already set up or invalid */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001493 if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001494 return( 0 );
1495
1496 /* Should never happen when we're called */
1497 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
1498 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1499
1500 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
1501 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1502
1503 ctx->pk_info = info;
1504
1505 return( 0 );
1506}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001507#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001508
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001509/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001510 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001511 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001512int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
1513 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001514 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001515 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001516 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001517{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001518 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001519 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1520 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001521 PK_VALIDATE_RET( sig != NULL );
1522
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001523 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001524 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001526
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001527#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001528 /* optimization: use non-restartable version if restart disabled */
1529 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001530 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001531 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001532 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001533 int ret;
1534
1535 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1536 return( ret );
1537
1538 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
1539 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
1540
1541 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1542 mbedtls_pk_restart_free( rs_ctx );
1543
1544 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001545 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001546#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001547 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001548#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001549
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001550 return( pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ),
1551 ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001552}
1553
1554/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001555 * Verify a signature
1556 */
1557int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1558 const unsigned char *hash, size_t hash_len,
1559 const unsigned char *sig, size_t sig_len )
1560{
1561 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
1562 sig, sig_len, NULL ) );
1563}
1564
1565/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001566 * Verify a signature with options
1567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
1569 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001570 const unsigned char *hash, size_t hash_len,
1571 const unsigned char *sig, size_t sig_len )
1572{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001573 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001574 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1575 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001576 PK_VALIDATE_RET( sig != NULL );
1577
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001578 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 if( ! mbedtls_pk_can_do( ctx, type ) )
1582 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001587 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001589
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001590#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001591 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
1592 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001593#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001594
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001595 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( sig_len < mbedtls_pk_get_len( ctx ) )
1601 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
1604 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001605 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001606 pss_opts->mgf1_hash_id,
1607 pss_opts->expected_salt_len,
1608 sig );
1609 if( ret != 0 )
1610 return( ret );
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 if( sig_len > mbedtls_pk_get_len( ctx ) )
1613 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001614
1615 return( 0 );
1616#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +00001618#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001619 }
1620
1621 /* General case: no options */
1622 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001626}
1627
1628/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001629 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001630 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001631int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
1632 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001633 const unsigned char *hash, size_t hash_len,
1634 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001635 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001636 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001637{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001638 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001639 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1640 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001641 PK_VALIDATE_RET( sig != NULL );
1642
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001643 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001644 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001646
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001647#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001648 /* optimization: use non-restartable version if restart disabled */
1649 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001650 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001651 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001652 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001653 int ret;
1654
1655 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1656 return( ret );
1657
1658 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
1659 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
1660
1661 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1662 mbedtls_pk_restart_free( rs_ctx );
1663
1664 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001665 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001666#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001667 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001668#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001669
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001670 return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1671 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001672}
1673
1674/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001675 * Make a signature
1676 */
1677int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1678 const unsigned char *hash, size_t hash_len,
1679 unsigned char *sig, size_t *sig_len,
1680 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1681{
1682 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
1683 sig, sig_len, f_rng, p_rng, NULL ) );
1684}
1685
1686/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001687 * Decrypt message
1688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001690 const unsigned char *input, size_t ilen,
1691 unsigned char *output, size_t *olen, size_t osize,
1692 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1693{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001694 PK_VALIDATE_RET( ctx != NULL );
1695 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1696 PK_VALIDATE_RET( output != NULL || osize == 0 );
1697 PK_VALIDATE_RET( olen != NULL );
1698
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001699 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001701
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001702 return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1703 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001704}
1705
1706/*
1707 * Encrypt message
1708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001710 const unsigned char *input, size_t ilen,
1711 unsigned char *output, size_t *olen, size_t osize,
1712 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1713{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001714 PK_VALIDATE_RET( ctx != NULL );
1715 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1716 PK_VALIDATE_RET( output != NULL || osize == 0 );
1717 PK_VALIDATE_RET( olen != NULL );
1718
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001719 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001721
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001722 return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1723 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001724}
1725
1726/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001727 * Check public-private key pair
1728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001730{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001731 PK_VALIDATE_RET( pub != NULL );
1732 PK_VALIDATE_RET( prv != NULL );
1733
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001734 if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001736
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001737#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001738 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001739 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001740 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001742 }
1743 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001744#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001745 {
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001746 if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001748 }
1749
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001750 return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ),
1751 pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001752}
1753
1754/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001755 * Get key size in bits
1756 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001757size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001758{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001759 /* For backward compatibility, accept NULL or a context that
1760 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001761 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001762 return( 0 );
1763
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001764 return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001765}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001766
1767/*
1768 * Export debug information
1769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001771{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001772 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001773 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001775
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001776 return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001777}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001778
1779/*
1780 * Access the PK type name
1781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001783{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001784 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001785 return( "invalid PK" );
1786
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001787 return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001788}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001789
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001790/*
1791 * Access the PK type
1792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001794{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001795 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001797
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001798 return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001799}
1800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#endif /* MBEDTLS_PK_C */