blob: b92eb14fef7c99fd4ba32fa439ed99fa8465ded6 [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#include "mbedtls/platform.h"
Piotr Nowickie048b912020-06-05 17:59:28 +020050
51#if !defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020052#include <stdlib.h>
53#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +020057#include <string.h>
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020058#include <limits.h>
59#include <stdint.h>
60
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +020061/* Parameter validation macros based on platform_util.h */
62#define PK_VALIDATE_RET( cond ) \
63 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
64#define PK_VALIDATE( cond ) \
65 MBEDTLS_INTERNAL_VALIDATE( cond )
66
67/*
68 * Internal wrappers around RSA functions
69 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020070#if defined(MBEDTLS_RSA_C)
71static int rsa_can_do( mbedtls_pk_type_t type )
72{
73 return( type == MBEDTLS_PK_RSA ||
74 type == MBEDTLS_PK_RSASSA_PSS );
75}
76
77static size_t rsa_get_bitlen( const void *ctx )
78{
79 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
80 return( 8 * mbedtls_rsa_get_len( rsa ) );
81}
82
83static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
84 const unsigned char *hash, size_t hash_len,
85 const unsigned char *sig, size_t sig_len )
86{
87 int ret;
88 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
89 size_t rsa_len = mbedtls_rsa_get_len( rsa );
90
91#if SIZE_MAX > UINT_MAX
92 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
93 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
94#endif /* SIZE_MAX > UINT_MAX */
95
96 if( sig_len < rsa_len )
97 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
98
99 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
100 MBEDTLS_RSA_PUBLIC, md_alg,
101 (unsigned int) hash_len, hash, sig ) ) != 0 )
102 return( ret );
103
104 /* The buffer contains a valid signature followed by extra data.
105 * We have a special error code for that so that so that callers can
106 * use mbedtls_pk_verify() to check "Does the buffer start with a
107 * valid signature?" and not just "Does the buffer contain a valid
108 * signature?". */
109 if( sig_len > rsa_len )
110 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
111
112 return( 0 );
113}
114
115static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
116 const unsigned char *hash, size_t hash_len,
117 unsigned char *sig, size_t *sig_len,
118 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
119{
120 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
121
122#if SIZE_MAX > UINT_MAX
123 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
124 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
125#endif /* SIZE_MAX > UINT_MAX */
126
127 *sig_len = mbedtls_rsa_get_len( rsa );
128
129 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
130 md_alg, (unsigned int) hash_len, hash, sig ) );
131}
132
133static int rsa_decrypt_wrap( void *ctx,
134 const unsigned char *input, size_t ilen,
135 unsigned char *output, size_t *olen, size_t osize,
136 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
137{
138 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
139
140 if( ilen != mbedtls_rsa_get_len( rsa ) )
141 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
142
143 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
144 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
145}
146
147static int rsa_encrypt_wrap( void *ctx,
148 const unsigned char *input, size_t ilen,
149 unsigned char *output, size_t *olen, size_t osize,
150 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
151{
152 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
153 *olen = mbedtls_rsa_get_len( rsa );
154
155 if( *olen > osize )
156 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
157
158 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
159 ilen, input, output ) );
160}
161
162static int rsa_check_pair_wrap( const void *pub, const void *prv )
163{
164 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
165 (const mbedtls_rsa_context *) prv ) );
166}
167
168static void *rsa_alloc_wrap( void )
169{
170 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
171
172 if( ctx != NULL )
173 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
174
175 return( ctx );
176}
177
178static void rsa_free_wrap( void *ctx )
179{
180 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
181 mbedtls_free( ctx );
182}
183
184static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
185{
186 items->type = MBEDTLS_PK_DEBUG_MPI;
187 items->name = "rsa.N";
188 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
189
190 items++;
191
192 items->type = MBEDTLS_PK_DEBUG_MPI;
193 items->name = "rsa.E";
194 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
195}
196
197const mbedtls_pk_info_t mbedtls_rsa_info = {
198 MBEDTLS_PK_RSA,
199 "RSA",
200 rsa_get_bitlen,
201 rsa_can_do,
202 rsa_verify_wrap,
203 rsa_sign_wrap,
204#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
205 NULL,
206 NULL,
207#endif
208 rsa_decrypt_wrap,
209 rsa_encrypt_wrap,
210 rsa_check_pair_wrap,
211 rsa_alloc_wrap,
212 rsa_free_wrap,
213#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
214 NULL,
215 NULL,
216#endif
217 rsa_debug,
218};
219#endif /* MBEDTLS_RSA_C */
220
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200221/*
222 * Internal wrappers around ECC functions - based on ECP module
223 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200224#if defined(MBEDTLS_ECP_C)
225/*
226 * Generic EC key
227 */
228static int eckey_can_do( mbedtls_pk_type_t type )
229{
230 return( type == MBEDTLS_PK_ECKEY ||
231 type == MBEDTLS_PK_ECKEY_DH ||
232 type == MBEDTLS_PK_ECDSA );
233}
234
235static size_t eckey_get_bitlen( const void *ctx )
236{
237 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
238}
239
240#if defined(MBEDTLS_ECDSA_C)
241/* Forward declarations */
242static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
243 const unsigned char *hash, size_t hash_len,
244 const unsigned char *sig, size_t sig_len );
245
246static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
247 const unsigned char *hash, size_t hash_len,
248 unsigned char *sig, size_t *sig_len,
249 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
250
251static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
252 const unsigned char *hash, size_t hash_len,
253 const unsigned char *sig, size_t sig_len )
254{
255 int ret;
256 mbedtls_ecdsa_context ecdsa;
257
258 mbedtls_ecdsa_init( &ecdsa );
259
260 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
261 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
262
263 mbedtls_ecdsa_free( &ecdsa );
264
265 return( ret );
266}
267
268static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
269 const unsigned char *hash, size_t hash_len,
270 unsigned char *sig, size_t *sig_len,
271 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
272{
273 int ret;
274 mbedtls_ecdsa_context ecdsa;
275
276 mbedtls_ecdsa_init( &ecdsa );
277
278 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
279 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
280 f_rng, p_rng );
281
282 mbedtls_ecdsa_free( &ecdsa );
283
284 return( ret );
285}
286
287#if defined(MBEDTLS_ECP_RESTARTABLE)
288/* Forward declarations */
289static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
290 const unsigned char *hash, size_t hash_len,
291 const unsigned char *sig, size_t sig_len,
292 void *rs_ctx );
293
294static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
295 const unsigned char *hash, size_t hash_len,
296 unsigned char *sig, size_t *sig_len,
297 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
298 void *rs_ctx );
299
300/*
301 * Restart context for ECDSA operations with ECKEY context
302 *
303 * We need to store an actual ECDSA context, as we need to pass the same to
304 * the underlying ecdsa function, so we can't create it on the fly every time.
305 */
306typedef struct
307{
308 mbedtls_ecdsa_restart_ctx ecdsa_rs;
309 mbedtls_ecdsa_context ecdsa_ctx;
310} eckey_restart_ctx;
311
312static void *eckey_rs_alloc( void )
313{
314 eckey_restart_ctx *rs_ctx;
315
316 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
317
318 if( ctx != NULL )
319 {
320 rs_ctx = ctx;
321 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
322 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
323 }
324
325 return( ctx );
326}
327
328static void eckey_rs_free( void *ctx )
329{
330 eckey_restart_ctx *rs_ctx;
331
332 if( ctx == NULL)
333 return;
334
335 rs_ctx = ctx;
336 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
337 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
338
339 mbedtls_free( ctx );
340}
341
342static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
343 const unsigned char *hash, size_t hash_len,
344 const unsigned char *sig, size_t sig_len,
345 void *rs_ctx )
346{
347 int ret;
348 eckey_restart_ctx *rs = rs_ctx;
349
350 /* Should never happen */
351 if( rs == NULL )
352 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
353
354 /* set up our own sub-context if needed (that is, on first run) */
355 if( rs->ecdsa_ctx.grp.pbits == 0 )
356 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
357
358 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
359 md_alg, hash, hash_len,
360 sig, sig_len, &rs->ecdsa_rs ) );
361
362cleanup:
363 return( ret );
364}
365
366static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
367 const unsigned char *hash, size_t hash_len,
368 unsigned char *sig, size_t *sig_len,
369 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
370 void *rs_ctx )
371{
372 int ret;
373 eckey_restart_ctx *rs = rs_ctx;
374
375 /* Should never happen */
376 if( rs == NULL )
377 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
378
379 /* set up our own sub-context if needed (that is, on first run) */
380 if( rs->ecdsa_ctx.grp.pbits == 0 )
381 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
382
383 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
384 hash, hash_len, sig, sig_len,
385 f_rng, p_rng, &rs->ecdsa_rs ) );
386
387cleanup:
388 return( ret );
389}
390#endif /* MBEDTLS_ECP_RESTARTABLE */
391#endif /* MBEDTLS_ECDSA_C */
392
393static int eckey_check_pair( const void *pub, const void *prv )
394{
395 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
396 (const mbedtls_ecp_keypair *) prv ) );
397}
398
399static void *eckey_alloc_wrap( void )
400{
401 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
402
403 if( ctx != NULL )
404 mbedtls_ecp_keypair_init( ctx );
405
406 return( ctx );
407}
408
409static void eckey_free_wrap( void *ctx )
410{
411 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
412 mbedtls_free( ctx );
413}
414
415static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
416{
417 items->type = MBEDTLS_PK_DEBUG_ECP;
418 items->name = "eckey.Q";
419 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
420}
421
422const mbedtls_pk_info_t mbedtls_eckey_info = {
423 MBEDTLS_PK_ECKEY,
424 "EC",
425 eckey_get_bitlen,
426 eckey_can_do,
427#if defined(MBEDTLS_ECDSA_C)
428 eckey_verify_wrap,
429 eckey_sign_wrap,
430#if defined(MBEDTLS_ECP_RESTARTABLE)
431 eckey_verify_rs_wrap,
432 eckey_sign_rs_wrap,
433#endif
434#else /* MBEDTLS_ECDSA_C */
435 NULL,
436 NULL,
437#endif /* MBEDTLS_ECDSA_C */
438 NULL,
439 NULL,
440 eckey_check_pair,
441 eckey_alloc_wrap,
442 eckey_free_wrap,
443#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
444 eckey_rs_alloc,
445 eckey_rs_free,
446#endif
447 eckey_debug,
448};
449
450/*
451 * EC key restricted to ECDH
452 */
453static int eckeydh_can_do( mbedtls_pk_type_t type )
454{
455 return( type == MBEDTLS_PK_ECKEY ||
456 type == MBEDTLS_PK_ECKEY_DH );
457}
458
459const mbedtls_pk_info_t mbedtls_eckeydh_info = {
460 MBEDTLS_PK_ECKEY_DH,
461 "EC_DH",
462 eckey_get_bitlen, /* Same underlying key structure */
463 eckeydh_can_do,
464 NULL,
465 NULL,
466#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
467 NULL,
468 NULL,
469#endif
470 NULL,
471 NULL,
472 eckey_check_pair,
473 eckey_alloc_wrap, /* Same underlying key structure */
474 eckey_free_wrap, /* Same underlying key structure */
475#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
476 NULL,
477 NULL,
478#endif
479 eckey_debug, /* Same underlying key structure */
480};
481#endif /* MBEDTLS_ECP_C */
482
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200483/*
484 * Internal wrappers around ECC functions - based on TinyCrypt
485 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200486#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200487/*
488 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
489 * those integers and convert it to the fixed-length encoding.
490 */
491static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
492 unsigned char *to, size_t to_len )
493{
494 int ret;
495 size_t unpadded_len, padding_len;
496
497 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
498 MBEDTLS_ASN1_INTEGER ) ) != 0 )
499 {
500 return( ret );
501 }
502
503 while( unpadded_len > 0 && **from == 0x00 )
504 {
505 ( *from )++;
506 unpadded_len--;
507 }
508
509 if( unpadded_len > to_len || unpadded_len == 0 )
510 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
511
512 padding_len = to_len - unpadded_len;
513 memset( to, 0x00, padding_len );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300514 mbedtls_platform_memcpy( to + padding_len, *from, unpadded_len );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200515 ( *from ) += unpadded_len;
516
517 return( 0 );
518}
519
520/*
521 * Convert a signature from an ASN.1 sequence of two integers
522 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
523 * twice as big as int_size.
524 */
525static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
526 unsigned char *sig, size_t int_size )
527{
528 int ret;
529 size_t tmp_size;
530
531 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
532 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
533 return( ret );
534
535 /* Extract r */
536 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
537 return( ret );
538 /* Extract s */
539 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
540 return( ret );
541
542 return( 0 );
543}
544
545static size_t uecc_eckey_get_bitlen( const void *ctx )
546{
547 (void) ctx;
548 return( (size_t) ( NUM_ECC_BYTES * 8 ) );
549}
550
551static int uecc_eckey_check_pair( const void *pub, const void *prv )
552{
553 const mbedtls_uecc_keypair *uecc_pub =
554 (const mbedtls_uecc_keypair *) pub;
555 const mbedtls_uecc_keypair *uecc_prv =
556 (const mbedtls_uecc_keypair *) prv;
557
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300558 if( mbedtls_platform_memcmp( uecc_pub->public_key,
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200559 uecc_prv->public_key,
560 2 * NUM_ECC_BYTES ) == 0 )
561 {
562 return( 0 );
563 }
564
565 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
566}
567
568static int uecc_eckey_can_do( mbedtls_pk_type_t type )
569{
570 return( type == MBEDTLS_PK_ECDSA ||
571 type == MBEDTLS_PK_ECKEY );
572}
573
574static int uecc_eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
575 const unsigned char *hash, size_t hash_len,
576 const unsigned char *sig, size_t sig_len )
577{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400578 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
579 volatile int ret_fi = UECC_FAULT_DETECTED;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200580 uint8_t signature[2*NUM_ECC_BYTES];
581 unsigned char *p;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200582 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
583
584 ((void) md_alg);
585 p = (unsigned char*) sig;
586
587 ret = extract_ecdsa_sig( &p, sig + sig_len, signature, NUM_ECC_BYTES );
588 if( ret != 0 )
589 return( ret );
590
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100591 ret_fi = uECC_verify( keypair->public_key, hash,
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100592 (unsigned) hash_len, signature );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200593
Manuel Pégourié-Gonnard4d6186b2019-11-25 10:53:24 +0100594 if( ret_fi == UECC_FAULT_DETECTED )
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100595 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
596
597 if( ret_fi == UECC_SUCCESS )
598 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +0200599 mbedtls_platform_random_delay();
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100600 if( ret_fi == UECC_SUCCESS )
601 return( 0 );
602 else
603 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
604 }
605
606 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200607}
608
609/*
610 * Simultaneously convert and move raw MPI from the beginning of a buffer
611 * to an ASN.1 MPI at the end of the buffer.
612 * See also mbedtls_asn1_write_mpi().
613 *
614 * p: pointer to the end of the output buffer
615 * start: start of the output buffer, and also of the mpi to write at the end
616 * n_len: length of the mpi to read from start
617 *
618 * Warning:
619 * The total length of the output buffer must be smaller than 128 Bytes.
620 */
621static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
622 size_t n_len )
623{
624 size_t len = 0;
Piotr Nowickice0aab42020-06-08 14:08:49 +0200625 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200626
627 if( (size_t)( *p - start ) < n_len )
628 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
629
630 len = n_len;
631 *p -= len;
Piotr Nowickice0aab42020-06-08 14:08:49 +0200632 ret = mbedtls_platform_memmove( *p, start, len );
633 if( ret != 0 )
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200634 {
Piotr Nowickice0aab42020-06-08 14:08:49 +0200635 return( ret );
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200636 }
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200637
638 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
639 * Neither r nor s should be 0, but as a failsafe measure, still detect
640 * that rather than overflowing the buffer in case of an error. */
641 while( len > 0 && **p == 0x00 )
642 {
643 ++(*p);
644 --len;
645 }
646
647 /* this is only reached if the signature was invalid */
648 if( len == 0 )
649 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
650
651 /* if the msb is 1, ASN.1 requires that we prepend a 0.
652 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
653 if( **p & 0x80 )
654 {
655 if( *p - start < 1 )
656 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
657
658 *--(*p) = 0x00;
659 len += 1;
660 }
661
662 /* The ASN.1 length encoding is just a single Byte containing the length,
663 * as we assume that the total buffer length is smaller than 128 Bytes. */
664 *--(*p) = len;
665 *--(*p) = MBEDTLS_ASN1_INTEGER;
666 len += 2;
667
668 return( (int) len );
669}
670
671/* Transcode signature from uECC format to ASN.1 sequence.
672 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
673 * MPIs, and in-place.
674 *
675 * [in/out] sig: the signature pre- and post-transcoding
676 * [in/out] sig_len: signature length pre- and post-transcoding
677 * [int] buf_len: the available size the in/out buffer
678 *
679 * Warning: buf_len must be smaller than 128 Bytes.
680 */
681static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
682 size_t buf_len )
683{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400684 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200685 size_t len = 0;
686 const size_t rs_len = *sig_len / 2;
687 unsigned char *p = sig + buf_len;
688
689 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
690 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
691
692 /* The ASN.1 length encoding is just a single Byte containing the length,
693 * as we assume that the total buffer length is smaller than 128 Bytes. */
694 *--p = len;
695 *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
696 len += 2;
697
Piotr Nowickice0aab42020-06-08 14:08:49 +0200698 ret = mbedtls_platform_memmove( sig, p, len );
699 if( ret != 0 )
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200700 {
Piotr Nowickice0aab42020-06-08 14:08:49 +0200701 return( ret );
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200702 }
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200703 *sig_len = len;
704
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400705 return( ret );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200706}
707
708static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
709 const unsigned char *hash, size_t hash_len,
710 unsigned char *sig, size_t *sig_len,
711 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
712{
713 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200714 int ret;
715
716 /*
717 * RFC-4492 page 20:
718 *
719 * Ecdsa-Sig-Value ::= SEQUENCE {
720 * r INTEGER,
721 * s INTEGER
722 * }
723 *
724 * Size is at most
725 * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s,
726 * twice that + 1 (tag) + 2 (len) for the sequence
727 *
728 * (The ASN.1 length encodings are all 1-Byte encodings because
729 * the total size is smaller than 128 Bytes).
730 */
731 #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) )
732
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100733 ret = uECC_sign( keypair->private_key, hash, hash_len, sig );
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100734 if( ret == UECC_FAULT_DETECTED )
735 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
736 if( ret != UECC_SUCCESS )
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200737 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
738
739 *sig_len = 2 * NUM_ECC_BYTES;
740
741 /* uECC owns its rng function pointer */
742 (void) f_rng;
743 (void) p_rng;
744 (void) md_alg;
745
746 return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len,
747 MAX_SECP256R1_ECDSA_SIG_LEN ) );
748
749 #undef MAX_SECP256R1_ECDSA_SIG_LEN
750}
751
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200752#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200753static void *uecc_eckey_alloc_wrap( void )
754{
755 return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) );
756}
757
758static void uecc_eckey_free_wrap( void *ctx )
759{
760 if( ctx == NULL )
761 return;
762
763 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) );
764 mbedtls_free( ctx );
765}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200766#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200767
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200768#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200769const mbedtls_pk_info_t mbedtls_uecc_eckey_info =
770 MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY );
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200771#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200772#endif /* MBEDTLS_USE_TINYCRYPT */
773
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200774/*
775 * Internal wrappers around ECDSA functions
776 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200777#if defined(MBEDTLS_ECDSA_C)
778static int ecdsa_can_do( mbedtls_pk_type_t type )
779{
780 return( type == MBEDTLS_PK_ECDSA );
781}
782
783static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
784 const unsigned char *hash, size_t hash_len,
785 const unsigned char *sig, size_t sig_len )
786{
787 int ret;
788 ((void) md_alg);
789
790 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
791 hash, hash_len, sig, sig_len );
792
793 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
794 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
795
796 return( ret );
797}
798
799static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
800 const unsigned char *hash, size_t hash_len,
801 unsigned char *sig, size_t *sig_len,
802 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
803{
804 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
805 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
806}
807
808#if defined(MBEDTLS_ECP_RESTARTABLE)
809static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
810 const unsigned char *hash, size_t hash_len,
811 const unsigned char *sig, size_t sig_len,
812 void *rs_ctx )
813{
814 int ret;
815 ((void) md_alg);
816
817 ret = mbedtls_ecdsa_read_signature_restartable(
818 (mbedtls_ecdsa_context *) ctx,
819 hash, hash_len, sig, sig_len,
820 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
821
822 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
823 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
824
825 return( ret );
826}
827
828static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
829 const unsigned char *hash, size_t hash_len,
830 unsigned char *sig, size_t *sig_len,
831 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
832 void *rs_ctx )
833{
834 return( mbedtls_ecdsa_write_signature_restartable(
835 (mbedtls_ecdsa_context *) ctx,
836 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
837 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
838
839}
840#endif /* MBEDTLS_ECP_RESTARTABLE */
841
842static void *ecdsa_alloc_wrap( void )
843{
844 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
845
846 if( ctx != NULL )
847 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
848
849 return( ctx );
850}
851
852static void ecdsa_free_wrap( void *ctx )
853{
854 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
855 mbedtls_free( ctx );
856}
857
858#if defined(MBEDTLS_ECP_RESTARTABLE)
859static void *ecdsa_rs_alloc( void )
860{
861 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
862
863 if( ctx != NULL )
864 mbedtls_ecdsa_restart_init( ctx );
865
866 return( ctx );
867}
868
869static void ecdsa_rs_free( void *ctx )
870{
871 mbedtls_ecdsa_restart_free( ctx );
872 mbedtls_free( ctx );
873}
874#endif /* MBEDTLS_ECP_RESTARTABLE */
875
876const mbedtls_pk_info_t mbedtls_ecdsa_info = {
877 MBEDTLS_PK_ECDSA,
878 "ECDSA",
879 eckey_get_bitlen, /* Compatible key structures */
880 ecdsa_can_do,
881 ecdsa_verify_wrap,
882 ecdsa_sign_wrap,
883#if defined(MBEDTLS_ECP_RESTARTABLE)
884 ecdsa_verify_rs_wrap,
885 ecdsa_sign_rs_wrap,
886#endif
887 NULL,
888 NULL,
889 eckey_check_pair, /* Compatible key structures */
890 ecdsa_alloc_wrap,
891 ecdsa_free_wrap,
892#if defined(MBEDTLS_ECP_RESTARTABLE)
893 ecdsa_rs_alloc,
894 ecdsa_rs_free,
895#endif
896 eckey_debug, /* Compatible key structures */
897};
898#endif /* MBEDTLS_ECDSA_C */
899
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200900/*
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200901 * Internal wrappers for RSA-alt support
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200902 */
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200903#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200904static int rsa_alt_can_do( mbedtls_pk_type_t type )
905{
906 return( type == MBEDTLS_PK_RSA );
907}
908
909static size_t rsa_alt_get_bitlen( const void *ctx )
910{
911 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
912
913 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
914}
915
916static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
917 const unsigned char *hash, size_t hash_len,
918 unsigned char *sig, size_t *sig_len,
919 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
920{
921 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
922
923#if SIZE_MAX > UINT_MAX
924 if( UINT_MAX < hash_len )
925 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
926#endif /* SIZE_MAX > UINT_MAX */
927
928 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
929
930 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
931 md_alg, (unsigned int) hash_len, hash, sig ) );
932}
933
934static int rsa_alt_decrypt_wrap( void *ctx,
935 const unsigned char *input, size_t ilen,
936 unsigned char *output, size_t *olen, size_t osize,
937 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
938{
939 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
940
941 ((void) f_rng);
942 ((void) p_rng);
943
944 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
945 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
946
947 return( rsa_alt->decrypt_func( rsa_alt->key,
948 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
949}
950
951#if defined(MBEDTLS_RSA_C)
952static int rsa_alt_check_pair( const void *pub, const void *prv )
953{
954 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
955 unsigned char hash[32];
956 size_t sig_len = 0;
957 int ret;
958
959 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
960 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
961
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200962 mbedtls_platform_memset( hash, 0x2a, sizeof( hash ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200963
964 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
965 hash, sizeof( hash ),
966 sig, &sig_len, NULL, NULL ) ) != 0 )
967 {
968 return( ret );
969 }
970
971 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
972 hash, sizeof( hash ), sig, sig_len ) != 0 )
973 {
974 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
975 }
976
977 return( 0 );
978}
979#endif /* MBEDTLS_RSA_C */
980
981static void *rsa_alt_alloc_wrap( void )
982{
983 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
984
985 if( ctx != NULL )
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200986 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200987
988 return( ctx );
989}
990
991static void rsa_alt_free_wrap( void *ctx )
992{
993 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
994 mbedtls_free( ctx );
995}
996
997const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
998 MBEDTLS_PK_RSA_ALT,
999 "RSA-alt",
1000 rsa_alt_get_bitlen,
1001 rsa_alt_can_do,
1002 NULL,
1003 rsa_alt_sign_wrap,
1004#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1005 NULL,
1006 NULL,
1007#endif
1008 rsa_alt_decrypt_wrap,
1009 NULL,
1010#if defined(MBEDTLS_RSA_C)
1011 rsa_alt_check_pair,
1012#else
1013 NULL,
1014#endif
1015 rsa_alt_alloc_wrap,
1016 rsa_alt_free_wrap,
1017#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1018 NULL,
1019 NULL,
1020#endif
1021 NULL,
1022};
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +02001023#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
1024
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001025/*
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001026 * Access to members of the pk_info structure. When a single PK type is
1027 * hardcoded, these should have zero runtime cost; otherwise, the usual
1028 * dynamic dispatch based on pk_info is used.
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001029 *
1030 * For function members, don't make a getter, but a function that directly
1031 * calls the method, so that we can entirely get rid of function pointers
1032 * when hardcoding a single PK - some compilers optimize better that way.
1033 *
1034 * Not implemented for members that are only present in builds with
Manuel Pégourié-Gonnard8b5e6bd2019-09-20 08:57:18 +02001035 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for this is builds
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001036 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
1037 */
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001038#if defined(MBEDTLS_PK_SINGLE_TYPE)
1039
1040MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001041 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001042{
1043 (void) info;
1044 return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) );
1045}
1046
1047MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001048 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001049{
1050 (void) info;
1051 return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) );
1052}
1053
1054MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001055 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001056{
1057 (void) info;
1058 return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) );
1059}
1060
1061MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001062 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001063{
1064 (void) info;
1065 return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) );
1066}
1067
1068MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001069 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001070 const unsigned char *hash, size_t hash_len,
1071 const unsigned char *sig, size_t sig_len )
1072{
1073 (void) info;
1074#if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1075 (void) ctx;
1076 (void) md_alg;
1077 (void) hash;
1078 (void) hash_len;
1079 (void) sig;
1080 (void) sig_len;
1081 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1082#else
1083 return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1084 ctx, md_alg, hash, hash_len, sig, sig_len ) );
1085#endif
1086}
1087
1088MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001089 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001090 const unsigned char *hash, size_t hash_len,
1091 unsigned char *sig, size_t *sig_len,
1092 int (*f_rng)(void *, unsigned char *, size_t),
1093 void *p_rng )
1094{
1095 (void) info;
1096#if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1097 (void) ctx;
1098 (void) md_alg;
1099 (void) hash;
1100 (void) hash_len;
1101 (void) sig;
1102 (void) sig_len;
1103 (void) f_rng;
1104 (void) p_rng;
1105 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1106#else
1107 return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1108 ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
1109#endif
1110}
1111
1112MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001113 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001114 const unsigned char *input, size_t ilen,
1115 unsigned char *output, size_t *olen, size_t osize,
1116 int (*f_rng)(void *, unsigned char *, size_t),
1117 void *p_rng )
1118{
1119 (void) info;
1120#if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1121 (void) ctx;
1122 (void) input;
1123 (void) ilen;
1124 (void) output;
1125 (void) olen;
1126 (void) osize;
1127 (void) f_rng;
1128 (void) p_rng;
1129 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1130#else
1131 return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1132 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1133#endif
1134}
1135
1136MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001137 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001138 const unsigned char *input, size_t ilen,
1139 unsigned char *output, size_t *olen, size_t osize,
1140 int (*f_rng)(void *, unsigned char *, size_t),
1141 void *p_rng )
1142{
1143 (void) info;
1144#if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1145 (void) ctx;
1146 (void) input;
1147 (void) ilen;
1148 (void) output;
1149 (void) olen;
1150 (void) osize;
1151 (void) f_rng;
1152 (void) p_rng;
1153 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1154#else
1155 return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1156 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1157#endif
1158}
1159
1160MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001161 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001162{
1163 (void) info;
1164#if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1165 (void) pub;
1166 (void) prv;
1167 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1168#else
1169 return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1170 pub, prv ) );
1171#endif
1172}
1173
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001174MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001175 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001176 const void *ctx, mbedtls_pk_debug_item *items )
1177{
1178 (void) info;
1179#if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1180 (void) ctx;
1181 (void) items;
1182 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1183#else
1184 return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) );
1185#endif
1186}
1187
1188#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001189
1190MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001191 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001192{
1193 return( info->type );
1194}
1195
1196MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001197 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001198{
1199 return( info->name );
1200}
1201
1202MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001203 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001204{
1205 return( info->get_bitlen( ctx ) );
1206}
1207
1208MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001209 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001210{
1211 return( info->can_do( type ) );
1212}
1213
1214MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001215 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001216 const unsigned char *hash, size_t hash_len,
1217 const unsigned char *sig, size_t sig_len )
1218{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001219 if( info->verify_func == NULL )
1220 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1221
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001222 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
1223}
1224
1225MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001226 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001227 const unsigned char *hash, size_t hash_len,
1228 unsigned char *sig, size_t *sig_len,
1229 int (*f_rng)(void *, unsigned char *, size_t),
1230 void *p_rng )
1231{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001232 if( info->sign_func == NULL )
1233 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1234
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001235 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
1236 f_rng, p_rng ) );
1237}
1238
1239MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001240 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001241 const unsigned char *input, size_t ilen,
1242 unsigned char *output, size_t *olen, size_t osize,
1243 int (*f_rng)(void *, unsigned char *, size_t),
1244 void *p_rng )
1245{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001246 if( info->decrypt_func == NULL )
1247 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1248
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001249 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
1250 f_rng, p_rng ) );
1251}
1252
1253MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001254 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001255 const unsigned char *input, size_t ilen,
1256 unsigned char *output, size_t *olen, size_t osize,
1257 int (*f_rng)(void *, unsigned char *, size_t),
1258 void *p_rng )
1259{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001260 if( info->encrypt_func == NULL )
1261 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1262
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001263 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
1264 f_rng, p_rng ) );
1265}
1266
1267MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001268 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001269{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001270 if( info->check_pair_func == NULL )
1271 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1272
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001273 return( info->check_pair_func( pub, prv ) );
1274}
1275
1276MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001277 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001278{
1279 return( info->ctx_alloc_func( ) );
1280}
1281
1282MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001283 mbedtls_pk_handle_t info, void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001284{
1285 info->ctx_free_func( ctx );
1286}
1287
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001288MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001289 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001290 const void *ctx, mbedtls_pk_debug_item *items )
1291{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001292 if( info->debug_func == NULL )
1293 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1294
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001295 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001296 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001297}
1298
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001299#endif /* MBEDTLS_PK_SINGLE_TYPE */
1300
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001301/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001305{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001306 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001307
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001308#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001309 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001310 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001311#else
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +02001312 memset( ctx, 0, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001313#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001314}
1315
1316/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001320{
irwir2239a862018-06-12 18:25:09 +03001321 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001322 return;
1323
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001324#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001325 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
1326 pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001327#endif
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +02001328
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001329 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001330}
1331
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001332#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001333/*
1334 * Initialize a restart context
1335 */
1336void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
1337{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001338 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001339 ctx->pk_info = NULL;
1340 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001341}
1342
1343/*
1344 * Free the components of a restart context
1345 */
1346void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
1347{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001348 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001349 ctx->pk_info->rs_free_func == NULL )
1350 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001351 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001352 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001353
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001354 ctx->pk_info->rs_free_func( ctx->rs_ctx );
1355
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001356 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001357 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001358}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001359#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001360
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001361/*
1362 * Get pk_info structure from type
1363 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001364mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001365{
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001366#if defined(MBEDTLS_PK_SINGLE_TYPE)
1367 if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) )
1368 return( MBEDTLS_PK_UNIQUE_VALID_HANDLE );
1369
1370 return( MBEDTLS_PK_INVALID_HANDLE );
1371
1372#else /* MBEDTLS_PK_SINGLE_TYPE */
1373
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001374 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_RSA_C)
1376 case MBEDTLS_PK_RSA:
1377 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 case MBEDTLS_PK_ECKEY_DH:
1381 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001382#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_ECDSA_C)
1384 case MBEDTLS_PK_ECDSA:
1385 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001386#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +01001387#if defined(MBEDTLS_USE_TINYCRYPT)
1388 case MBEDTLS_PK_ECKEY:
1389 return( &mbedtls_uecc_eckey_info );
1390#else /* MBEDTLS_USE_TINYCRYPT */
1391#if defined(MBEDTLS_ECP_C)
1392 case MBEDTLS_PK_ECKEY:
1393 return( &mbedtls_eckey_info );
1394#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +03001395#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001397 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001398 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001399 }
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001400#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001401}
1402
1403/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02001404 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001405 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001406int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001407{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001408 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001409 if( info == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001411
1412#if !defined(MBEDTLS_PK_SINGLE_TYPE)
1413 if( ctx->pk_info != NULL )
1414 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1415
1416 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001417
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001418 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001419 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001420#else
1421 (void) ctx;
1422#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001423
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001424 return( 0 );
1425}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +01001428/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001429 * Initialize an RSA-alt context
1430 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001431int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
1433 mbedtls_pk_rsa_alt_sign_func sign_func,
1434 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_rsa_alt_context *rsa_alt;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001437 mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001438
Gilles Peskinee97dc602018-12-19 00:51:38 +01001439 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001440 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001442
1443 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001444 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001445
1446 ctx->pk_info = info;
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001449
1450 rsa_alt->key = key;
1451 rsa_alt->decrypt_func = decrypt_func;
1452 rsa_alt->sign_func = sign_func;
1453 rsa_alt->key_len_func = key_len_func;
1454
1455 return( 0 );
1456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001458
1459/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001460 * Tell if a PK can do the operations of the given type
1461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001463{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001464 /* A context with null pk_info is not set up yet and can't do anything.
1465 * For backward compatibility, also accept NULL instead of a context
1466 * pointer. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001467 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001468 return( 0 );
1469
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001470 return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001471}
1472
1473/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001477{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001478 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001479
1480 if( *hash_len != 0 )
1481 return( 0 );
1482
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001483 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
1484 MBEDTLS_MD_INVALID_HANDLE )
1485 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001486 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001487 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001490 return( 0 );
1491}
1492
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001493#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001494/*
1495 * Helper to set up a restart context if needed
1496 */
1497static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001498 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001499{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001500 /* Don't do anything if already set up or invalid */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001501 if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001502 return( 0 );
1503
1504 /* Should never happen when we're called */
1505 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
1506 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1507
1508 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
1509 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1510
1511 ctx->pk_info = info;
1512
1513 return( 0 );
1514}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001515#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001516
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001517/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001518 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001519 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001520int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
1521 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001522 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001523 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001524 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001525{
Jarno Lamsab83a2132019-12-13 14:40:06 +02001526 volatile int verify_ret = MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001527 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001528 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1529 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001530 PK_VALIDATE_RET( sig != NULL );
1531
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001532 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001533 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001535
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001536#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001537 /* optimization: use non-restartable version if restart disabled */
1538 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001539 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001540 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001541 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001542 int ret;
1543
1544 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1545 return( ret );
1546
1547 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
1548 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
1549
1550 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1551 mbedtls_pk_restart_free( rs_ctx );
1552
1553 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001554 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001555#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001556 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001557#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001558
Jarno Lamsab83a2132019-12-13 14:40:06 +02001559 verify_ret = pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ),
1560 ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len );
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001561
Jarno Lamsab83a2132019-12-13 14:40:06 +02001562 if( verify_ret == 0 )
1563 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001564 mbedtls_platform_random_delay();
Jarno Lamsab83a2132019-12-13 14:40:06 +02001565 if( verify_ret == 0 )
1566 {
1567 return( verify_ret );
1568 }
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001569 else
1570 {
Piotr Nowickie048b912020-06-05 17:59:28 +02001571 verify_ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001572 }
Jarno Lamsab83a2132019-12-13 14:40:06 +02001573 }
1574
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001575 return( verify_ret );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001576}
1577
1578/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001579 * Verify a signature
1580 */
1581int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1582 const unsigned char *hash, size_t hash_len,
1583 const unsigned char *sig, size_t sig_len )
1584{
1585 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
1586 sig, sig_len, NULL ) );
1587}
1588
1589/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001590 * Verify a signature with options
1591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
1593 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001594 const unsigned char *hash, size_t hash_len,
1595 const unsigned char *sig, size_t sig_len )
1596{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001597 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001598 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1599 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001600 PK_VALIDATE_RET( sig != NULL );
1601
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001602 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 if( ! mbedtls_pk_can_do( ctx, type ) )
1606 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001611 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001613
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001614#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001615 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
1616 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001617#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001618
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001619 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 if( sig_len < mbedtls_pk_get_len( ctx ) )
1625 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
1628 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001629 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001630 pss_opts->mgf1_hash_id,
1631 pss_opts->expected_salt_len,
1632 sig );
1633 if( ret != 0 )
1634 return( ret );
1635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 if( sig_len > mbedtls_pk_get_len( ctx ) )
1637 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001638
1639 return( 0 );
1640#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +00001642#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001643 }
1644
1645 /* General case: no options */
1646 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001650}
1651
1652/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001653 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001654 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001655int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
1656 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001657 const unsigned char *hash, size_t hash_len,
1658 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001659 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001660 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001661{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001662 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001663 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1664 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001665 PK_VALIDATE_RET( sig != NULL );
1666
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001667 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001668 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001670
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001671#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001672 /* optimization: use non-restartable version if restart disabled */
1673 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001674 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001675 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001676 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001677 int ret;
1678
1679 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1680 return( ret );
1681
1682 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
1683 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
1684
1685 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1686 mbedtls_pk_restart_free( rs_ctx );
1687
1688 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001689 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001690#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001691 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001692#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001693
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001694 return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1695 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001696}
1697
1698/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001699 * Make a signature
1700 */
1701int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1702 const unsigned char *hash, size_t hash_len,
1703 unsigned char *sig, size_t *sig_len,
1704 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1705{
1706 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
1707 sig, sig_len, f_rng, p_rng, NULL ) );
1708}
1709
1710/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001711 * Decrypt message
1712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001714 const unsigned char *input, size_t ilen,
1715 unsigned char *output, size_t *olen, size_t osize,
1716 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1717{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001718 PK_VALIDATE_RET( ctx != NULL );
1719 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1720 PK_VALIDATE_RET( output != NULL || osize == 0 );
1721 PK_VALIDATE_RET( olen != NULL );
1722
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001723 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001725
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001726 return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1727 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001728}
1729
1730/*
1731 * Encrypt message
1732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001734 const unsigned char *input, size_t ilen,
1735 unsigned char *output, size_t *olen, size_t osize,
1736 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1737{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001738 PK_VALIDATE_RET( ctx != NULL );
1739 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1740 PK_VALIDATE_RET( output != NULL || osize == 0 );
1741 PK_VALIDATE_RET( olen != NULL );
1742
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001743 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001745
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001746 return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1747 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001748}
1749
1750/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001751 * Check public-private key pair
1752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001754{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001755 PK_VALIDATE_RET( pub != NULL );
1756 PK_VALIDATE_RET( prv != NULL );
1757
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001758 if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001760
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001761#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001762 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001763 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001764 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001766 }
1767 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001768#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001769 {
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001770 if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001772 }
1773
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001774 return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ),
1775 pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001776}
1777
1778/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001779 * Get key size in bits
1780 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001781size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001782{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001783 /* For backward compatibility, accept NULL or a context that
1784 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001785 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001786 return( 0 );
1787
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001788 return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001789}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001790
1791/*
1792 * Export debug information
1793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001795{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001796 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001797 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001799
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001800 return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001801}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001802
1803/*
1804 * Access the PK type name
1805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001807{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001808 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001809 return( "invalid PK" );
1810
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001811 return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001812}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001813
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001814/*
1815 * Access the PK type
1816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001818{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001819 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001821
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001822 return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001823}
1824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#endif /* MBEDTLS_PK_C */