blob: 82778b5368373f15067f3557ff618de0191156dc [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;
625
626 if( (size_t)( *p - start ) < n_len )
627 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
628
629 len = n_len;
630 *p -= len;
631 memmove( *p, start, len );
632
633 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
634 * Neither r nor s should be 0, but as a failsafe measure, still detect
635 * that rather than overflowing the buffer in case of an error. */
636 while( len > 0 && **p == 0x00 )
637 {
638 ++(*p);
639 --len;
640 }
641
642 /* this is only reached if the signature was invalid */
643 if( len == 0 )
644 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
645
646 /* if the msb is 1, ASN.1 requires that we prepend a 0.
647 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
648 if( **p & 0x80 )
649 {
650 if( *p - start < 1 )
651 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
652
653 *--(*p) = 0x00;
654 len += 1;
655 }
656
657 /* The ASN.1 length encoding is just a single Byte containing the length,
658 * as we assume that the total buffer length is smaller than 128 Bytes. */
659 *--(*p) = len;
660 *--(*p) = MBEDTLS_ASN1_INTEGER;
661 len += 2;
662
663 return( (int) len );
664}
665
666/* Transcode signature from uECC format to ASN.1 sequence.
667 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
668 * MPIs, and in-place.
669 *
670 * [in/out] sig: the signature pre- and post-transcoding
671 * [in/out] sig_len: signature length pre- and post-transcoding
672 * [int] buf_len: the available size the in/out buffer
673 *
674 * Warning: buf_len must be smaller than 128 Bytes.
675 */
676static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
677 size_t buf_len )
678{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400679 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200680 size_t len = 0;
681 const size_t rs_len = *sig_len / 2;
682 unsigned char *p = sig + buf_len;
683
684 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
685 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
686
687 /* The ASN.1 length encoding is just a single Byte containing the length,
688 * as we assume that the total buffer length is smaller than 128 Bytes. */
689 *--p = len;
690 *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
691 len += 2;
692
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400693 ret = 0;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200694 memmove( sig, p, len );
695 *sig_len = len;
696
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400697 return( ret );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200698}
699
700static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
701 const unsigned char *hash, size_t hash_len,
702 unsigned char *sig, size_t *sig_len,
703 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
704{
705 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200706 int ret;
707
708 /*
709 * RFC-4492 page 20:
710 *
711 * Ecdsa-Sig-Value ::= SEQUENCE {
712 * r INTEGER,
713 * s INTEGER
714 * }
715 *
716 * Size is at most
717 * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s,
718 * twice that + 1 (tag) + 2 (len) for the sequence
719 *
720 * (The ASN.1 length encodings are all 1-Byte encodings because
721 * the total size is smaller than 128 Bytes).
722 */
723 #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) )
724
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100725 ret = uECC_sign( keypair->private_key, hash, hash_len, sig );
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100726 if( ret == UECC_FAULT_DETECTED )
727 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
728 if( ret != UECC_SUCCESS )
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200729 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
730
731 *sig_len = 2 * NUM_ECC_BYTES;
732
733 /* uECC owns its rng function pointer */
734 (void) f_rng;
735 (void) p_rng;
736 (void) md_alg;
737
738 return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len,
739 MAX_SECP256R1_ECDSA_SIG_LEN ) );
740
741 #undef MAX_SECP256R1_ECDSA_SIG_LEN
742}
743
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200744#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200745static void *uecc_eckey_alloc_wrap( void )
746{
747 return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) );
748}
749
750static void uecc_eckey_free_wrap( void *ctx )
751{
752 if( ctx == NULL )
753 return;
754
755 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) );
756 mbedtls_free( ctx );
757}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200758#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200759
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200760#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200761const mbedtls_pk_info_t mbedtls_uecc_eckey_info =
762 MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY );
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200763#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200764#endif /* MBEDTLS_USE_TINYCRYPT */
765
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200766/*
767 * Internal wrappers around ECDSA functions
768 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200769#if defined(MBEDTLS_ECDSA_C)
770static int ecdsa_can_do( mbedtls_pk_type_t type )
771{
772 return( type == MBEDTLS_PK_ECDSA );
773}
774
775static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
776 const unsigned char *hash, size_t hash_len,
777 const unsigned char *sig, size_t sig_len )
778{
779 int ret;
780 ((void) md_alg);
781
782 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
783 hash, hash_len, sig, sig_len );
784
785 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
786 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
787
788 return( ret );
789}
790
791static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
792 const unsigned char *hash, size_t hash_len,
793 unsigned char *sig, size_t *sig_len,
794 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
795{
796 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
797 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
798}
799
800#if defined(MBEDTLS_ECP_RESTARTABLE)
801static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
802 const unsigned char *hash, size_t hash_len,
803 const unsigned char *sig, size_t sig_len,
804 void *rs_ctx )
805{
806 int ret;
807 ((void) md_alg);
808
809 ret = mbedtls_ecdsa_read_signature_restartable(
810 (mbedtls_ecdsa_context *) ctx,
811 hash, hash_len, sig, sig_len,
812 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
813
814 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
815 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
816
817 return( ret );
818}
819
820static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
821 const unsigned char *hash, size_t hash_len,
822 unsigned char *sig, size_t *sig_len,
823 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
824 void *rs_ctx )
825{
826 return( mbedtls_ecdsa_write_signature_restartable(
827 (mbedtls_ecdsa_context *) ctx,
828 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
829 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
830
831}
832#endif /* MBEDTLS_ECP_RESTARTABLE */
833
834static void *ecdsa_alloc_wrap( void )
835{
836 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
837
838 if( ctx != NULL )
839 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
840
841 return( ctx );
842}
843
844static void ecdsa_free_wrap( void *ctx )
845{
846 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
847 mbedtls_free( ctx );
848}
849
850#if defined(MBEDTLS_ECP_RESTARTABLE)
851static void *ecdsa_rs_alloc( void )
852{
853 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
854
855 if( ctx != NULL )
856 mbedtls_ecdsa_restart_init( ctx );
857
858 return( ctx );
859}
860
861static void ecdsa_rs_free( void *ctx )
862{
863 mbedtls_ecdsa_restart_free( ctx );
864 mbedtls_free( ctx );
865}
866#endif /* MBEDTLS_ECP_RESTARTABLE */
867
868const mbedtls_pk_info_t mbedtls_ecdsa_info = {
869 MBEDTLS_PK_ECDSA,
870 "ECDSA",
871 eckey_get_bitlen, /* Compatible key structures */
872 ecdsa_can_do,
873 ecdsa_verify_wrap,
874 ecdsa_sign_wrap,
875#if defined(MBEDTLS_ECP_RESTARTABLE)
876 ecdsa_verify_rs_wrap,
877 ecdsa_sign_rs_wrap,
878#endif
879 NULL,
880 NULL,
881 eckey_check_pair, /* Compatible key structures */
882 ecdsa_alloc_wrap,
883 ecdsa_free_wrap,
884#if defined(MBEDTLS_ECP_RESTARTABLE)
885 ecdsa_rs_alloc,
886 ecdsa_rs_free,
887#endif
888 eckey_debug, /* Compatible key structures */
889};
890#endif /* MBEDTLS_ECDSA_C */
891
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200892/*
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200893 * Internal wrappers for RSA-alt support
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200894 */
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200895#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200896static int rsa_alt_can_do( mbedtls_pk_type_t type )
897{
898 return( type == MBEDTLS_PK_RSA );
899}
900
901static size_t rsa_alt_get_bitlen( const void *ctx )
902{
903 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
904
905 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
906}
907
908static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
909 const unsigned char *hash, size_t hash_len,
910 unsigned char *sig, size_t *sig_len,
911 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
912{
913 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
914
915#if SIZE_MAX > UINT_MAX
916 if( UINT_MAX < hash_len )
917 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
918#endif /* SIZE_MAX > UINT_MAX */
919
920 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
921
922 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
923 md_alg, (unsigned int) hash_len, hash, sig ) );
924}
925
926static int rsa_alt_decrypt_wrap( void *ctx,
927 const unsigned char *input, size_t ilen,
928 unsigned char *output, size_t *olen, size_t osize,
929 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
930{
931 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
932
933 ((void) f_rng);
934 ((void) p_rng);
935
936 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
937 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
938
939 return( rsa_alt->decrypt_func( rsa_alt->key,
940 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
941}
942
943#if defined(MBEDTLS_RSA_C)
944static int rsa_alt_check_pair( const void *pub, const void *prv )
945{
946 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
947 unsigned char hash[32];
948 size_t sig_len = 0;
949 int ret;
950
951 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
952 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
953
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200954 mbedtls_platform_memset( hash, 0x2a, sizeof( hash ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200955
956 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
957 hash, sizeof( hash ),
958 sig, &sig_len, NULL, NULL ) ) != 0 )
959 {
960 return( ret );
961 }
962
963 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
964 hash, sizeof( hash ), sig, sig_len ) != 0 )
965 {
966 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
967 }
968
969 return( 0 );
970}
971#endif /* MBEDTLS_RSA_C */
972
973static void *rsa_alt_alloc_wrap( void )
974{
975 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
976
977 if( ctx != NULL )
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200978 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200979
980 return( ctx );
981}
982
983static void rsa_alt_free_wrap( void *ctx )
984{
985 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
986 mbedtls_free( ctx );
987}
988
989const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
990 MBEDTLS_PK_RSA_ALT,
991 "RSA-alt",
992 rsa_alt_get_bitlen,
993 rsa_alt_can_do,
994 NULL,
995 rsa_alt_sign_wrap,
996#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
997 NULL,
998 NULL,
999#endif
1000 rsa_alt_decrypt_wrap,
1001 NULL,
1002#if defined(MBEDTLS_RSA_C)
1003 rsa_alt_check_pair,
1004#else
1005 NULL,
1006#endif
1007 rsa_alt_alloc_wrap,
1008 rsa_alt_free_wrap,
1009#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1010 NULL,
1011 NULL,
1012#endif
1013 NULL,
1014};
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +02001015#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
1016
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001017/*
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001018 * Access to members of the pk_info structure. When a single PK type is
1019 * hardcoded, these should have zero runtime cost; otherwise, the usual
1020 * dynamic dispatch based on pk_info is used.
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001021 *
1022 * For function members, don't make a getter, but a function that directly
1023 * calls the method, so that we can entirely get rid of function pointers
1024 * when hardcoding a single PK - some compilers optimize better that way.
1025 *
1026 * Not implemented for members that are only present in builds with
Manuel Pégourié-Gonnard8b5e6bd2019-09-20 08:57:18 +02001027 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for this is builds
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001028 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
1029 */
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001030#if defined(MBEDTLS_PK_SINGLE_TYPE)
1031
1032MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001033 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001034{
1035 (void) info;
1036 return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) );
1037}
1038
1039MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001040 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001041{
1042 (void) info;
1043 return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) );
1044}
1045
1046MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001047 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001048{
1049 (void) info;
1050 return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) );
1051}
1052
1053MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001054 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001055{
1056 (void) info;
1057 return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) );
1058}
1059
1060MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001061 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001062 const unsigned char *hash, size_t hash_len,
1063 const unsigned char *sig, size_t sig_len )
1064{
1065 (void) info;
1066#if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1067 (void) ctx;
1068 (void) md_alg;
1069 (void) hash;
1070 (void) hash_len;
1071 (void) sig;
1072 (void) sig_len;
1073 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1074#else
1075 return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1076 ctx, md_alg, hash, hash_len, sig, sig_len ) );
1077#endif
1078}
1079
1080MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001081 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001082 const unsigned char *hash, size_t hash_len,
1083 unsigned char *sig, size_t *sig_len,
1084 int (*f_rng)(void *, unsigned char *, size_t),
1085 void *p_rng )
1086{
1087 (void) info;
1088#if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1089 (void) ctx;
1090 (void) md_alg;
1091 (void) hash;
1092 (void) hash_len;
1093 (void) sig;
1094 (void) sig_len;
1095 (void) f_rng;
1096 (void) p_rng;
1097 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1098#else
1099 return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1100 ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
1101#endif
1102}
1103
1104MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001105 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001106 const unsigned char *input, size_t ilen,
1107 unsigned char *output, size_t *olen, size_t osize,
1108 int (*f_rng)(void *, unsigned char *, size_t),
1109 void *p_rng )
1110{
1111 (void) info;
1112#if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1113 (void) ctx;
1114 (void) input;
1115 (void) ilen;
1116 (void) output;
1117 (void) olen;
1118 (void) osize;
1119 (void) f_rng;
1120 (void) p_rng;
1121 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1122#else
1123 return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1124 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1125#endif
1126}
1127
1128MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001129 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001130 const unsigned char *input, size_t ilen,
1131 unsigned char *output, size_t *olen, size_t osize,
1132 int (*f_rng)(void *, unsigned char *, size_t),
1133 void *p_rng )
1134{
1135 (void) info;
1136#if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1137 (void) ctx;
1138 (void) input;
1139 (void) ilen;
1140 (void) output;
1141 (void) olen;
1142 (void) osize;
1143 (void) f_rng;
1144 (void) p_rng;
1145 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1146#else
1147 return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1148 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1149#endif
1150}
1151
1152MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001153 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001154{
1155 (void) info;
1156#if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1157 (void) pub;
1158 (void) prv;
1159 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1160#else
1161 return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1162 pub, prv ) );
1163#endif
1164}
1165
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001166MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001167 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001168 const void *ctx, mbedtls_pk_debug_item *items )
1169{
1170 (void) info;
1171#if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1172 (void) ctx;
1173 (void) items;
1174 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1175#else
1176 return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) );
1177#endif
1178}
1179
1180#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001181
1182MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001183 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001184{
1185 return( info->type );
1186}
1187
1188MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001189 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001190{
1191 return( info->name );
1192}
1193
1194MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001195 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001196{
1197 return( info->get_bitlen( ctx ) );
1198}
1199
1200MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001201 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001202{
1203 return( info->can_do( type ) );
1204}
1205
1206MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001207 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001208 const unsigned char *hash, size_t hash_len,
1209 const unsigned char *sig, size_t sig_len )
1210{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001211 if( info->verify_func == NULL )
1212 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1213
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001214 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
1215}
1216
1217MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001218 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001219 const unsigned char *hash, size_t hash_len,
1220 unsigned char *sig, size_t *sig_len,
1221 int (*f_rng)(void *, unsigned char *, size_t),
1222 void *p_rng )
1223{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001224 if( info->sign_func == NULL )
1225 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1226
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001227 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
1228 f_rng, p_rng ) );
1229}
1230
1231MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001232 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001233 const unsigned char *input, size_t ilen,
1234 unsigned char *output, size_t *olen, size_t osize,
1235 int (*f_rng)(void *, unsigned char *, size_t),
1236 void *p_rng )
1237{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001238 if( info->decrypt_func == NULL )
1239 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1240
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001241 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
1242 f_rng, p_rng ) );
1243}
1244
1245MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001246 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001247 const unsigned char *input, size_t ilen,
1248 unsigned char *output, size_t *olen, size_t osize,
1249 int (*f_rng)(void *, unsigned char *, size_t),
1250 void *p_rng )
1251{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001252 if( info->encrypt_func == NULL )
1253 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1254
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001255 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
1256 f_rng, p_rng ) );
1257}
1258
1259MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001260 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001261{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001262 if( info->check_pair_func == NULL )
1263 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1264
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001265 return( info->check_pair_func( pub, prv ) );
1266}
1267
1268MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001269 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001270{
1271 return( info->ctx_alloc_func( ) );
1272}
1273
1274MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001275 mbedtls_pk_handle_t info, void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001276{
1277 info->ctx_free_func( ctx );
1278}
1279
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001280MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001281 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001282 const void *ctx, mbedtls_pk_debug_item *items )
1283{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001284 if( info->debug_func == NULL )
1285 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1286
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001287 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001288 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001289}
1290
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001291#endif /* MBEDTLS_PK_SINGLE_TYPE */
1292
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001293/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001297{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001298 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001299
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001300#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001301 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001302 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001303#else
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +02001304 memset( ctx, 0, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001305#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001306}
1307
1308/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001312{
irwir2239a862018-06-12 18:25:09 +03001313 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001314 return;
1315
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001316#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001317 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
1318 pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001319#endif
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +02001320
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001321 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001322}
1323
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001324#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001325/*
1326 * Initialize a restart context
1327 */
1328void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
1329{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001330 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001331 ctx->pk_info = NULL;
1332 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001333}
1334
1335/*
1336 * Free the components of a restart context
1337 */
1338void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
1339{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001340 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001341 ctx->pk_info->rs_free_func == NULL )
1342 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001343 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001344 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001345
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001346 ctx->pk_info->rs_free_func( ctx->rs_ctx );
1347
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001348 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001349 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001350}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001351#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001352
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001353/*
1354 * Get pk_info structure from type
1355 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001356mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001357{
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001358#if defined(MBEDTLS_PK_SINGLE_TYPE)
1359 if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) )
1360 return( MBEDTLS_PK_UNIQUE_VALID_HANDLE );
1361
1362 return( MBEDTLS_PK_INVALID_HANDLE );
1363
1364#else /* MBEDTLS_PK_SINGLE_TYPE */
1365
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001366 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_RSA_C)
1368 case MBEDTLS_PK_RSA:
1369 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001370#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 case MBEDTLS_PK_ECKEY_DH:
1373 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_ECDSA_C)
1376 case MBEDTLS_PK_ECDSA:
1377 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001378#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +01001379#if defined(MBEDTLS_USE_TINYCRYPT)
1380 case MBEDTLS_PK_ECKEY:
1381 return( &mbedtls_uecc_eckey_info );
1382#else /* MBEDTLS_USE_TINYCRYPT */
1383#if defined(MBEDTLS_ECP_C)
1384 case MBEDTLS_PK_ECKEY:
1385 return( &mbedtls_eckey_info );
1386#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +03001387#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001389 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001390 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001391 }
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001392#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001393}
1394
1395/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02001396 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001397 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001398int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001399{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001400 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001401 if( info == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001403
1404#if !defined(MBEDTLS_PK_SINGLE_TYPE)
1405 if( ctx->pk_info != NULL )
1406 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1407
1408 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001409
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001410 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001411 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001412#else
1413 (void) ctx;
1414#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001415
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001416 return( 0 );
1417}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +01001420/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001421 * Initialize an RSA-alt context
1422 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001423int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
1425 mbedtls_pk_rsa_alt_sign_func sign_func,
1426 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001427{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 mbedtls_rsa_alt_context *rsa_alt;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001429 mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001430
Gilles Peskinee97dc602018-12-19 00:51:38 +01001431 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001432 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001434
1435 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001436 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001437
1438 ctx->pk_info = info;
1439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001441
1442 rsa_alt->key = key;
1443 rsa_alt->decrypt_func = decrypt_func;
1444 rsa_alt->sign_func = sign_func;
1445 rsa_alt->key_len_func = key_len_func;
1446
1447 return( 0 );
1448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001450
1451/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001452 * Tell if a PK can do the operations of the given type
1453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001455{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001456 /* A context with null pk_info is not set up yet and can't do anything.
1457 * For backward compatibility, also accept NULL instead of a context
1458 * pointer. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001459 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001460 return( 0 );
1461
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001462 return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001463}
1464
1465/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001467 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001469{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001470 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001471
1472 if( *hash_len != 0 )
1473 return( 0 );
1474
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001475 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
1476 MBEDTLS_MD_INVALID_HANDLE )
1477 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001478 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001479 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001482 return( 0 );
1483}
1484
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001485#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001486/*
1487 * Helper to set up a restart context if needed
1488 */
1489static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001490 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001491{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001492 /* Don't do anything if already set up or invalid */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001493 if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001494 return( 0 );
1495
1496 /* Should never happen when we're called */
1497 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
1498 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1499
1500 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
1501 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1502
1503 ctx->pk_info = info;
1504
1505 return( 0 );
1506}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001507#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001508
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001509/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001510 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001511 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001512int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
1513 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001514 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001515 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001516 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001517{
Jarno Lamsab83a2132019-12-13 14:40:06 +02001518 volatile int verify_ret = MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001519 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001520 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1521 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001522 PK_VALIDATE_RET( sig != NULL );
1523
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001524 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001525 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001527
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001528#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001529 /* optimization: use non-restartable version if restart disabled */
1530 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001531 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001532 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001533 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001534 int ret;
1535
1536 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1537 return( ret );
1538
1539 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
1540 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
1541
1542 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1543 mbedtls_pk_restart_free( rs_ctx );
1544
1545 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001546 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001547#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001548 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001549#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001550
Jarno Lamsab83a2132019-12-13 14:40:06 +02001551 verify_ret = pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ),
1552 ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len );
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001553
Jarno Lamsab83a2132019-12-13 14:40:06 +02001554 if( verify_ret == 0 )
1555 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001556 mbedtls_platform_random_delay();
Jarno Lamsab83a2132019-12-13 14:40:06 +02001557 if( verify_ret == 0 )
1558 {
1559 return( verify_ret );
1560 }
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001561 else
1562 {
Piotr Nowickie048b912020-06-05 17:59:28 +02001563 verify_ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001564 }
Jarno Lamsab83a2132019-12-13 14:40:06 +02001565 }
1566
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001567 return( verify_ret );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001568}
1569
1570/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001571 * Verify a signature
1572 */
1573int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1574 const unsigned char *hash, size_t hash_len,
1575 const unsigned char *sig, size_t sig_len )
1576{
1577 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
1578 sig, sig_len, NULL ) );
1579}
1580
1581/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001582 * Verify a signature with options
1583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
1585 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001586 const unsigned char *hash, size_t hash_len,
1587 const unsigned char *sig, size_t sig_len )
1588{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001589 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001590 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1591 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001592 PK_VALIDATE_RET( sig != NULL );
1593
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001594 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( ! mbedtls_pk_can_do( ctx, type ) )
1598 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001603 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001605
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001606#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001607 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
1608 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001609#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001610
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001611 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 if( sig_len < mbedtls_pk_get_len( ctx ) )
1617 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
1620 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001621 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001622 pss_opts->mgf1_hash_id,
1623 pss_opts->expected_salt_len,
1624 sig );
1625 if( ret != 0 )
1626 return( ret );
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( sig_len > mbedtls_pk_get_len( ctx ) )
1629 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001630
1631 return( 0 );
1632#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +00001634#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001635 }
1636
1637 /* General case: no options */
1638 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001642}
1643
1644/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001645 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001646 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001647int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
1648 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001649 const unsigned char *hash, size_t hash_len,
1650 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001651 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001652 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001653{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001654 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001655 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1656 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001657 PK_VALIDATE_RET( sig != NULL );
1658
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001659 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001660 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001662
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001663#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001664 /* optimization: use non-restartable version if restart disabled */
1665 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001666 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001667 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001668 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001669 int ret;
1670
1671 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1672 return( ret );
1673
1674 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
1675 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
1676
1677 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1678 mbedtls_pk_restart_free( rs_ctx );
1679
1680 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001681 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001682#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001683 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001684#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001685
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001686 return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1687 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001688}
1689
1690/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001691 * Make a signature
1692 */
1693int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1694 const unsigned char *hash, size_t hash_len,
1695 unsigned char *sig, size_t *sig_len,
1696 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1697{
1698 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
1699 sig, sig_len, f_rng, p_rng, NULL ) );
1700}
1701
1702/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001703 * Decrypt message
1704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001706 const unsigned char *input, size_t ilen,
1707 unsigned char *output, size_t *olen, size_t osize,
1708 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1709{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001710 PK_VALIDATE_RET( ctx != NULL );
1711 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1712 PK_VALIDATE_RET( output != NULL || osize == 0 );
1713 PK_VALIDATE_RET( olen != NULL );
1714
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001715 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001717
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001718 return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1719 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001720}
1721
1722/*
1723 * Encrypt message
1724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001726 const unsigned char *input, size_t ilen,
1727 unsigned char *output, size_t *olen, size_t osize,
1728 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1729{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001730 PK_VALIDATE_RET( ctx != NULL );
1731 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1732 PK_VALIDATE_RET( output != NULL || osize == 0 );
1733 PK_VALIDATE_RET( olen != NULL );
1734
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001735 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001737
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001738 return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1739 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001740}
1741
1742/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001743 * Check public-private key pair
1744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001746{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001747 PK_VALIDATE_RET( pub != NULL );
1748 PK_VALIDATE_RET( prv != NULL );
1749
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001750 if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001752
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001753#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001754 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001755 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001756 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001758 }
1759 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001760#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001761 {
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001762 if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001764 }
1765
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001766 return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ),
1767 pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001768}
1769
1770/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001771 * Get key size in bits
1772 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001773size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001774{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001775 /* For backward compatibility, accept NULL or a context that
1776 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001777 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001778 return( 0 );
1779
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001780 return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001781}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001782
1783/*
1784 * Export debug information
1785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001787{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001788 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001789 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001791
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001792 return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001793}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001794
1795/*
1796 * Access the PK type name
1797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001799{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001800 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001801 return( "invalid PK" );
1802
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001803 return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001804}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001805
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001806/*
1807 * Access the PK type
1808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001810{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001811 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001813
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001814 return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001815}
1816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#endif /* MBEDTLS_PK_C */