blob: 3bb91b2849bb629df8701e93277562f54a1a4b2f [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
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400551/* This function compares public keys of two keypairs */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200552static 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{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400579 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
580 volatile int ret_fi = UECC_FAULT_DETECTED;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200581 uint8_t signature[2*NUM_ECC_BYTES];
582 unsigned char *p;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200583 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
584
585 ((void) md_alg);
586 p = (unsigned char*) sig;
587
588 ret = extract_ecdsa_sig( &p, sig + sig_len, signature, NUM_ECC_BYTES );
589 if( ret != 0 )
590 return( ret );
591
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100592 ret_fi = uECC_verify( keypair->public_key, hash,
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100593 (unsigned) hash_len, signature );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200594
Manuel Pégourié-Gonnard4d6186b2019-11-25 10:53:24 +0100595 if( ret_fi == UECC_FAULT_DETECTED )
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100596 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
597
598 if( ret_fi == UECC_SUCCESS )
599 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +0200600 mbedtls_platform_random_delay();
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100601 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{
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400625 size_t len = n_len;
Piotr Nowickice0aab42020-06-08 14:08:49 +0200626 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200627
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400628 if( (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200629 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
630
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200631 *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
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400662 if( ( *p - start ) < 2 )
663 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
664
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200665 /* The ASN.1 length encoding is just a single Byte containing the length,
666 * as we assume that the total buffer length is smaller than 128 Bytes. */
667 *--(*p) = len;
668 *--(*p) = MBEDTLS_ASN1_INTEGER;
669 len += 2;
670
671 return( (int) len );
672}
673
674/* Transcode signature from uECC format to ASN.1 sequence.
675 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
676 * MPIs, and in-place.
677 *
678 * [in/out] sig: the signature pre- and post-transcoding
679 * [in/out] sig_len: signature length pre- and post-transcoding
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400680 * [in] buf_len: the available size the in/out buffer
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200681 *
682 * Warning: buf_len must be smaller than 128 Bytes.
683 */
684static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
685 size_t buf_len )
686{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400687 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200688 size_t len = 0;
689 const size_t rs_len = *sig_len / 2;
690 unsigned char *p = sig + buf_len;
691
692 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
693 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
694
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400695 if( p - sig < 2 )
696 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
697
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200698 /* The ASN.1 length encoding is just a single Byte containing the length,
699 * as we assume that the total buffer length is smaller than 128 Bytes. */
700 *--p = len;
701 *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
702 len += 2;
703
Piotr Nowickice0aab42020-06-08 14:08:49 +0200704 ret = mbedtls_platform_memmove( sig, p, len );
705 if( ret != 0 )
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200706 {
Piotr Nowickice0aab42020-06-08 14:08:49 +0200707 return( ret );
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200708 }
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200709 *sig_len = len;
710
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400711 return( ret );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200712}
713
714static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
715 const unsigned char *hash, size_t hash_len,
716 unsigned char *sig, size_t *sig_len,
717 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
718{
719 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200720 int ret;
721
722 /*
723 * RFC-4492 page 20:
724 *
725 * Ecdsa-Sig-Value ::= SEQUENCE {
726 * r INTEGER,
727 * s INTEGER
728 * }
729 *
730 * Size is at most
731 * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s,
732 * twice that + 1 (tag) + 2 (len) for the sequence
733 *
734 * (The ASN.1 length encodings are all 1-Byte encodings because
735 * the total size is smaller than 128 Bytes).
736 */
737 #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) )
738
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100739 ret = uECC_sign( keypair->private_key, hash, hash_len, sig );
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100740 if( ret == UECC_FAULT_DETECTED )
741 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
742 if( ret != UECC_SUCCESS )
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200743 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
744
745 *sig_len = 2 * NUM_ECC_BYTES;
746
747 /* uECC owns its rng function pointer */
748 (void) f_rng;
749 (void) p_rng;
750 (void) md_alg;
751
752 return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len,
753 MAX_SECP256R1_ECDSA_SIG_LEN ) );
754
755 #undef MAX_SECP256R1_ECDSA_SIG_LEN
756}
757
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200758#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200759static void *uecc_eckey_alloc_wrap( void )
760{
761 return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) );
762}
763
764static void uecc_eckey_free_wrap( void *ctx )
765{
766 if( ctx == NULL )
767 return;
768
769 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) );
770 mbedtls_free( ctx );
771}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200772#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200773
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200774#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200775const mbedtls_pk_info_t mbedtls_uecc_eckey_info =
776 MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY );
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200777#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200778#endif /* MBEDTLS_USE_TINYCRYPT */
779
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200780/*
781 * Internal wrappers around ECDSA functions
782 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200783#if defined(MBEDTLS_ECDSA_C)
784static int ecdsa_can_do( mbedtls_pk_type_t type )
785{
786 return( type == MBEDTLS_PK_ECDSA );
787}
788
789static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
790 const unsigned char *hash, size_t hash_len,
791 const unsigned char *sig, size_t sig_len )
792{
793 int ret;
794 ((void) md_alg);
795
796 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
797 hash, hash_len, sig, sig_len );
798
799 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
800 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
801
802 return( ret );
803}
804
805static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
806 const unsigned char *hash, size_t hash_len,
807 unsigned char *sig, size_t *sig_len,
808 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
809{
810 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
811 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
812}
813
814#if defined(MBEDTLS_ECP_RESTARTABLE)
815static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
816 const unsigned char *hash, size_t hash_len,
817 const unsigned char *sig, size_t sig_len,
818 void *rs_ctx )
819{
820 int ret;
821 ((void) md_alg);
822
823 ret = mbedtls_ecdsa_read_signature_restartable(
824 (mbedtls_ecdsa_context *) ctx,
825 hash, hash_len, sig, sig_len,
826 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
827
828 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
829 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
830
831 return( ret );
832}
833
834static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
835 const unsigned char *hash, size_t hash_len,
836 unsigned char *sig, size_t *sig_len,
837 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
838 void *rs_ctx )
839{
840 return( mbedtls_ecdsa_write_signature_restartable(
841 (mbedtls_ecdsa_context *) ctx,
842 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
843 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
844
845}
846#endif /* MBEDTLS_ECP_RESTARTABLE */
847
848static void *ecdsa_alloc_wrap( void )
849{
850 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
851
852 if( ctx != NULL )
853 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
854
855 return( ctx );
856}
857
858static void ecdsa_free_wrap( void *ctx )
859{
860 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
861 mbedtls_free( ctx );
862}
863
864#if defined(MBEDTLS_ECP_RESTARTABLE)
865static void *ecdsa_rs_alloc( void )
866{
867 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
868
869 if( ctx != NULL )
870 mbedtls_ecdsa_restart_init( ctx );
871
872 return( ctx );
873}
874
875static void ecdsa_rs_free( void *ctx )
876{
877 mbedtls_ecdsa_restart_free( ctx );
878 mbedtls_free( ctx );
879}
880#endif /* MBEDTLS_ECP_RESTARTABLE */
881
882const mbedtls_pk_info_t mbedtls_ecdsa_info = {
883 MBEDTLS_PK_ECDSA,
884 "ECDSA",
885 eckey_get_bitlen, /* Compatible key structures */
886 ecdsa_can_do,
887 ecdsa_verify_wrap,
888 ecdsa_sign_wrap,
889#if defined(MBEDTLS_ECP_RESTARTABLE)
890 ecdsa_verify_rs_wrap,
891 ecdsa_sign_rs_wrap,
892#endif
893 NULL,
894 NULL,
895 eckey_check_pair, /* Compatible key structures */
896 ecdsa_alloc_wrap,
897 ecdsa_free_wrap,
898#if defined(MBEDTLS_ECP_RESTARTABLE)
899 ecdsa_rs_alloc,
900 ecdsa_rs_free,
901#endif
902 eckey_debug, /* Compatible key structures */
903};
904#endif /* MBEDTLS_ECDSA_C */
905
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200906/*
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200907 * Internal wrappers for RSA-alt support
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200908 */
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200909#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200910static int rsa_alt_can_do( mbedtls_pk_type_t type )
911{
912 return( type == MBEDTLS_PK_RSA );
913}
914
915static size_t rsa_alt_get_bitlen( const void *ctx )
916{
917 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
918
919 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
920}
921
922static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
923 const unsigned char *hash, size_t hash_len,
924 unsigned char *sig, size_t *sig_len,
925 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
926{
927 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
928
929#if SIZE_MAX > UINT_MAX
930 if( UINT_MAX < hash_len )
931 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
932#endif /* SIZE_MAX > UINT_MAX */
933
934 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
935
936 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
937 md_alg, (unsigned int) hash_len, hash, sig ) );
938}
939
940static int rsa_alt_decrypt_wrap( void *ctx,
941 const unsigned char *input, size_t ilen,
942 unsigned char *output, size_t *olen, size_t osize,
943 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
944{
945 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
946
947 ((void) f_rng);
948 ((void) p_rng);
949
950 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
951 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
952
953 return( rsa_alt->decrypt_func( rsa_alt->key,
954 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
955}
956
957#if defined(MBEDTLS_RSA_C)
958static int rsa_alt_check_pair( const void *pub, const void *prv )
959{
960 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
961 unsigned char hash[32];
962 size_t sig_len = 0;
963 int ret;
964
965 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
966 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
967
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200968 mbedtls_platform_memset( hash, 0x2a, sizeof( hash ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200969
970 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
971 hash, sizeof( hash ),
972 sig, &sig_len, NULL, NULL ) ) != 0 )
973 {
974 return( ret );
975 }
976
977 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
978 hash, sizeof( hash ), sig, sig_len ) != 0 )
979 {
980 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
981 }
982
983 return( 0 );
984}
985#endif /* MBEDTLS_RSA_C */
986
987static void *rsa_alt_alloc_wrap( void )
988{
989 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
990
991 if( ctx != NULL )
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200992 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200993
994 return( ctx );
995}
996
997static void rsa_alt_free_wrap( void *ctx )
998{
999 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
1000 mbedtls_free( ctx );
1001}
1002
1003const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1004 MBEDTLS_PK_RSA_ALT,
1005 "RSA-alt",
1006 rsa_alt_get_bitlen,
1007 rsa_alt_can_do,
1008 NULL,
1009 rsa_alt_sign_wrap,
1010#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1011 NULL,
1012 NULL,
1013#endif
1014 rsa_alt_decrypt_wrap,
1015 NULL,
1016#if defined(MBEDTLS_RSA_C)
1017 rsa_alt_check_pair,
1018#else
1019 NULL,
1020#endif
1021 rsa_alt_alloc_wrap,
1022 rsa_alt_free_wrap,
1023#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1024 NULL,
1025 NULL,
1026#endif
1027 NULL,
1028};
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +02001029#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
1030
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001031/*
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001032 * Access to members of the pk_info structure. When a single PK type is
1033 * hardcoded, these should have zero runtime cost; otherwise, the usual
1034 * dynamic dispatch based on pk_info is used.
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001035 *
1036 * For function members, don't make a getter, but a function that directly
1037 * calls the method, so that we can entirely get rid of function pointers
1038 * when hardcoding a single PK - some compilers optimize better that way.
1039 *
1040 * Not implemented for members that are only present in builds with
Manuel Pégourié-Gonnard8b5e6bd2019-09-20 08:57:18 +02001041 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for this is builds
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001042 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
1043 */
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001044#if defined(MBEDTLS_PK_SINGLE_TYPE)
1045
1046MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001047 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001048{
1049 (void) info;
1050 return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) );
1051}
1052
1053MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001054 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001055{
1056 (void) info;
1057 return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) );
1058}
1059
1060MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001061 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001062{
1063 (void) info;
1064 return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) );
1065}
1066
1067MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001068 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001069{
1070 (void) info;
1071 return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) );
1072}
1073
1074MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001075 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001076 const unsigned char *hash, size_t hash_len,
1077 const unsigned char *sig, size_t sig_len )
1078{
1079 (void) info;
1080#if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1081 (void) ctx;
1082 (void) md_alg;
1083 (void) hash;
1084 (void) hash_len;
1085 (void) sig;
1086 (void) sig_len;
1087 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1088#else
1089 return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1090 ctx, md_alg, hash, hash_len, sig, sig_len ) );
1091#endif
1092}
1093
1094MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001095 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001096 const unsigned char *hash, size_t hash_len,
1097 unsigned char *sig, size_t *sig_len,
1098 int (*f_rng)(void *, unsigned char *, size_t),
1099 void *p_rng )
1100{
1101 (void) info;
1102#if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1103 (void) ctx;
1104 (void) md_alg;
1105 (void) hash;
1106 (void) hash_len;
1107 (void) sig;
1108 (void) sig_len;
1109 (void) f_rng;
1110 (void) p_rng;
1111 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1112#else
1113 return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1114 ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
1115#endif
1116}
1117
1118MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001119 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001120 const unsigned char *input, size_t ilen,
1121 unsigned char *output, size_t *olen, size_t osize,
1122 int (*f_rng)(void *, unsigned char *, size_t),
1123 void *p_rng )
1124{
1125 (void) info;
1126#if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1127 (void) ctx;
1128 (void) input;
1129 (void) ilen;
1130 (void) output;
1131 (void) olen;
1132 (void) osize;
1133 (void) f_rng;
1134 (void) p_rng;
1135 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1136#else
1137 return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1138 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1139#endif
1140}
1141
1142MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001143 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001144 const unsigned char *input, size_t ilen,
1145 unsigned char *output, size_t *olen, size_t osize,
1146 int (*f_rng)(void *, unsigned char *, size_t),
1147 void *p_rng )
1148{
1149 (void) info;
1150#if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1151 (void) ctx;
1152 (void) input;
1153 (void) ilen;
1154 (void) output;
1155 (void) olen;
1156 (void) osize;
1157 (void) f_rng;
1158 (void) p_rng;
1159 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1160#else
1161 return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1162 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1163#endif
1164}
1165
1166MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001167 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001168{
1169 (void) info;
1170#if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1171 (void) pub;
1172 (void) prv;
1173 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1174#else
1175 return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1176 pub, prv ) );
1177#endif
1178}
1179
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001180MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001181 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001182 const void *ctx, mbedtls_pk_debug_item *items )
1183{
1184 (void) info;
1185#if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1186 (void) ctx;
1187 (void) items;
1188 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1189#else
1190 return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) );
1191#endif
1192}
1193
1194#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001195
1196MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
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->type );
1200}
1201
1202MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001203 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001204{
1205 return( info->name );
1206}
1207
1208MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001209 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001210{
1211 return( info->get_bitlen( ctx ) );
1212}
1213
1214MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001215 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001216{
1217 return( info->can_do( type ) );
1218}
1219
1220MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001221 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001222 const unsigned char *hash, size_t hash_len,
1223 const unsigned char *sig, size_t sig_len )
1224{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001225 if( info->verify_func == NULL )
1226 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1227
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001228 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
1229}
1230
1231MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001232 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001233 const unsigned char *hash, size_t hash_len,
1234 unsigned char *sig, size_t *sig_len,
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->sign_func == NULL )
1239 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1240
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001241 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
1242 f_rng, p_rng ) );
1243}
1244
1245MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_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->decrypt_func == NULL )
1253 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1254
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001255 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
1256 f_rng, p_rng ) );
1257}
1258
1259MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001260 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001261 const unsigned char *input, size_t ilen,
1262 unsigned char *output, size_t *olen, size_t osize,
1263 int (*f_rng)(void *, unsigned char *, size_t),
1264 void *p_rng )
1265{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001266 if( info->encrypt_func == NULL )
1267 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1268
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001269 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
1270 f_rng, p_rng ) );
1271}
1272
1273MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001274 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001275{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001276 if( info->check_pair_func == NULL )
1277 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1278
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001279 return( info->check_pair_func( pub, prv ) );
1280}
1281
1282MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001283 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001284{
1285 return( info->ctx_alloc_func( ) );
1286}
1287
1288MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001289 mbedtls_pk_handle_t info, void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001290{
1291 info->ctx_free_func( ctx );
1292}
1293
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001294MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001295 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001296 const void *ctx, mbedtls_pk_debug_item *items )
1297{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001298 if( info->debug_func == NULL )
1299 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1300
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001301 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001302 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001303}
1304
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001305#endif /* MBEDTLS_PK_SINGLE_TYPE */
1306
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001307/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001311{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001312 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001313
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001314#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001315 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001316 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001317#else
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +02001318 memset( ctx, 0, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001319#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001320}
1321
1322/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001326{
irwir2239a862018-06-12 18:25:09 +03001327 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001328 return;
1329
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001330#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001331 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
1332 pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001333#endif
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +02001334
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001335 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001336}
1337
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001338#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001339/*
1340 * Initialize a restart context
1341 */
1342void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
1343{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001344 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001345 ctx->pk_info = NULL;
1346 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001347}
1348
1349/*
1350 * Free the components of a restart context
1351 */
1352void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
1353{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001354 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001355 ctx->pk_info->rs_free_func == NULL )
1356 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001357 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001358 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001359
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001360 ctx->pk_info->rs_free_func( ctx->rs_ctx );
1361
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001362 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001363 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001364}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001365#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001366
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001367/*
1368 * Get pk_info structure from type
1369 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001370mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001371{
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001372#if defined(MBEDTLS_PK_SINGLE_TYPE)
1373 if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) )
1374 return( MBEDTLS_PK_UNIQUE_VALID_HANDLE );
1375
1376 return( MBEDTLS_PK_INVALID_HANDLE );
1377
1378#else /* MBEDTLS_PK_SINGLE_TYPE */
1379
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001380 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_RSA_C)
1382 case MBEDTLS_PK_RSA:
1383 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 case MBEDTLS_PK_ECKEY_DH:
1387 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001388#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#if defined(MBEDTLS_ECDSA_C)
1390 case MBEDTLS_PK_ECDSA:
1391 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001392#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +01001393#if defined(MBEDTLS_USE_TINYCRYPT)
1394 case MBEDTLS_PK_ECKEY:
1395 return( &mbedtls_uecc_eckey_info );
1396#else /* MBEDTLS_USE_TINYCRYPT */
1397#if defined(MBEDTLS_ECP_C)
1398 case MBEDTLS_PK_ECKEY:
1399 return( &mbedtls_eckey_info );
1400#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +03001401#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001403 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001404 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001405 }
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001406#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001407}
1408
1409/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02001410 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001411 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001412int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001413{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001414 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001415 if( info == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001417
1418#if !defined(MBEDTLS_PK_SINGLE_TYPE)
1419 if( ctx->pk_info != NULL )
1420 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1421
1422 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001423
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001424 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001425 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001426#else
1427 (void) ctx;
1428#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001429
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001430 return( 0 );
1431}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +01001434/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001435 * Initialize an RSA-alt context
1436 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001437int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
1439 mbedtls_pk_rsa_alt_sign_func sign_func,
1440 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001441{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_rsa_alt_context *rsa_alt;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001443 mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001444
Gilles Peskinee97dc602018-12-19 00:51:38 +01001445 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001446 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001448
1449 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001450 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001451
1452 ctx->pk_info = info;
1453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001455
1456 rsa_alt->key = key;
1457 rsa_alt->decrypt_func = decrypt_func;
1458 rsa_alt->sign_func = sign_func;
1459 rsa_alt->key_len_func = key_len_func;
1460
1461 return( 0 );
1462}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001464
1465/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001466 * Tell if a PK can do the operations of the given type
1467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001469{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001470 /* A context with null pk_info is not set up yet and can't do anything.
1471 * For backward compatibility, also accept NULL instead of a context
1472 * pointer. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001473 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001474 return( 0 );
1475
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001476 return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001477}
1478
1479/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001483{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001484 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001485
1486 if( *hash_len != 0 )
1487 return( 0 );
1488
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001489 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
1490 MBEDTLS_MD_INVALID_HANDLE )
1491 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001492 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001493 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001496 return( 0 );
1497}
1498
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001499#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001500/*
1501 * Helper to set up a restart context if needed
1502 */
1503static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001504 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001505{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001506 /* Don't do anything if already set up or invalid */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001507 if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001508 return( 0 );
1509
1510 /* Should never happen when we're called */
1511 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
1512 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1513
1514 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
1515 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1516
1517 ctx->pk_info = info;
1518
1519 return( 0 );
1520}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001521#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001522
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001523/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001524 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001525 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001526int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
1527 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001528 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001529 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001530 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001531{
Jarno Lamsab83a2132019-12-13 14:40:06 +02001532 volatile int verify_ret = MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001533 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001534 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1535 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001536 PK_VALIDATE_RET( sig != NULL );
1537
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001538 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001539 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001541
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001542#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001543 /* optimization: use non-restartable version if restart disabled */
1544 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001545 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001546 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001547 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001548 int ret;
1549
1550 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1551 return( ret );
1552
1553 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
1554 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
1555
1556 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1557 mbedtls_pk_restart_free( rs_ctx );
1558
1559 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001560 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001561#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001562 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001563#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001564
Jarno Lamsab83a2132019-12-13 14:40:06 +02001565 verify_ret = pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ),
1566 ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len );
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001567
Jarno Lamsab83a2132019-12-13 14:40:06 +02001568 if( verify_ret == 0 )
1569 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001570 mbedtls_platform_random_delay();
Jarno Lamsab83a2132019-12-13 14:40:06 +02001571 if( verify_ret == 0 )
1572 {
1573 return( verify_ret );
1574 }
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001575 else
1576 {
Piotr Nowickie048b912020-06-05 17:59:28 +02001577 verify_ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001578 }
Jarno Lamsab83a2132019-12-13 14:40:06 +02001579 }
1580
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001581 return( verify_ret );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001582}
1583
1584/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001585 * Verify a signature
1586 */
1587int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1588 const unsigned char *hash, size_t hash_len,
1589 const unsigned char *sig, size_t sig_len )
1590{
1591 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
1592 sig, sig_len, NULL ) );
1593}
1594
1595/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001596 * Verify a signature with options
1597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
1599 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001600 const unsigned char *hash, size_t hash_len,
1601 const unsigned char *sig, size_t sig_len )
1602{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001603 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001604 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1605 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001606 PK_VALIDATE_RET( sig != NULL );
1607
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001608 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 if( ! mbedtls_pk_can_do( ctx, type ) )
1612 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001617 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001619
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001620#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001621 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
1622 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001623#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001624
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001625 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 if( sig_len < mbedtls_pk_get_len( ctx ) )
1631 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
1634 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001635 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001636 pss_opts->mgf1_hash_id,
1637 pss_opts->expected_salt_len,
1638 sig );
1639 if( ret != 0 )
1640 return( ret );
1641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 if( sig_len > mbedtls_pk_get_len( ctx ) )
1643 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001644
1645 return( 0 );
1646#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +00001648#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001649 }
1650
1651 /* General case: no options */
1652 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001656}
1657
1658/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001659 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001660 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001661int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
1662 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001663 const unsigned char *hash, size_t hash_len,
1664 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001665 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001666 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001667{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001668 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001669 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1670 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001671 PK_VALIDATE_RET( sig != NULL );
1672
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001673 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001674 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001676
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001677#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001678 /* optimization: use non-restartable version if restart disabled */
1679 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001680 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001681 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001682 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001683 int ret;
1684
1685 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1686 return( ret );
1687
1688 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
1689 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
1690
1691 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1692 mbedtls_pk_restart_free( rs_ctx );
1693
1694 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001695 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001696#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001697 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001698#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001699
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001700 return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1701 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001702}
1703
1704/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001705 * Make a signature
1706 */
1707int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1708 const unsigned char *hash, size_t hash_len,
1709 unsigned char *sig, size_t *sig_len,
1710 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1711{
1712 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
1713 sig, sig_len, f_rng, p_rng, NULL ) );
1714}
1715
1716/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001717 * Decrypt message
1718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001720 const unsigned char *input, size_t ilen,
1721 unsigned char *output, size_t *olen, size_t osize,
1722 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1723{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001724 PK_VALIDATE_RET( ctx != NULL );
1725 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1726 PK_VALIDATE_RET( output != NULL || osize == 0 );
1727 PK_VALIDATE_RET( olen != NULL );
1728
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001729 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001731
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001732 return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1733 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001734}
1735
1736/*
1737 * Encrypt message
1738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001740 const unsigned char *input, size_t ilen,
1741 unsigned char *output, size_t *olen, size_t osize,
1742 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1743{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001744 PK_VALIDATE_RET( ctx != NULL );
1745 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1746 PK_VALIDATE_RET( output != NULL || osize == 0 );
1747 PK_VALIDATE_RET( olen != NULL );
1748
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001749 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001751
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001752 return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1753 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001754}
1755
1756/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001757 * Check public-private key pair
1758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001760{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001761 PK_VALIDATE_RET( pub != NULL );
1762 PK_VALIDATE_RET( prv != NULL );
1763
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001764 if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001766
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001767#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001768 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001769 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001770 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001772 }
1773 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001774#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001775 {
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001776 if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001778 }
1779
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001780 return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ),
1781 pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001782}
1783
1784/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001785 * Get key size in bits
1786 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001787size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001788{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001789 /* For backward compatibility, accept NULL or a context that
1790 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001791 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001792 return( 0 );
1793
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001794 return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001795}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001796
1797/*
1798 * Export debug information
1799 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001801{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001802 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001803 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001805
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001806 return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001807}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001808
1809/*
1810 * Access the PK type name
1811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001813{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001814 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001815 return( "invalid PK" );
1816
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001817 return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001818}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001819
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001820/*
1821 * Access the PK type
1822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001824{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001825 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001827
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001828 return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001829}
1830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#endif /* MBEDTLS_PK_C */