blob: 4976f1f732261dc00d973dfc5f63670db08f1cf5 [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
Piotr Nowickie3c4ee52020-06-23 12:59:56 +0200559 if( mbedtls_platform_memequal( 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 Kurek34039692020-07-17 05:49:47 -0400662 /* Ensure that there is still space for len and ASN1_INTEGER */
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400663 if( ( *p - start ) < 2 )
664 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
665
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200666 /* The ASN.1 length encoding is just a single Byte containing the length,
667 * as we assume that the total buffer length is smaller than 128 Bytes. */
668 *--(*p) = len;
669 *--(*p) = MBEDTLS_ASN1_INTEGER;
670 len += 2;
671
672 return( (int) len );
673}
674
675/* Transcode signature from uECC format to ASN.1 sequence.
676 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
677 * MPIs, and in-place.
678 *
679 * [in/out] sig: the signature pre- and post-transcoding
680 * [in/out] sig_len: signature length pre- and post-transcoding
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400681 * [in] buf_len: the available size the in/out buffer
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200682 *
683 * Warning: buf_len must be smaller than 128 Bytes.
684 */
685static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
686 size_t buf_len )
687{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400688 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200689 size_t len = 0;
690 const size_t rs_len = *sig_len / 2;
691 unsigned char *p = sig + buf_len;
692
693 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
694 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
695
Andrzej Kureka798e5d2020-07-16 09:57:17 -0400696 if( p - sig < 2 )
697 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
698
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200699 /* The ASN.1 length encoding is just a single Byte containing the length,
700 * as we assume that the total buffer length is smaller than 128 Bytes. */
701 *--p = len;
702 *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
703 len += 2;
704
Piotr Nowickice0aab42020-06-08 14:08:49 +0200705 ret = mbedtls_platform_memmove( sig, p, len );
706 if( ret != 0 )
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200707 {
Piotr Nowickice0aab42020-06-08 14:08:49 +0200708 return( ret );
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200709 }
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200710 *sig_len = len;
711
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400712 return( ret );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200713}
714
715static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
716 const unsigned char *hash, size_t hash_len,
717 unsigned char *sig, size_t *sig_len,
718 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
719{
720 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200721 int ret;
722
723 /*
724 * RFC-4492 page 20:
725 *
726 * Ecdsa-Sig-Value ::= SEQUENCE {
727 * r INTEGER,
728 * s INTEGER
729 * }
730 *
731 * Size is at most
732 * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s,
733 * twice that + 1 (tag) + 2 (len) for the sequence
734 *
735 * (The ASN.1 length encodings are all 1-Byte encodings because
736 * the total size is smaller than 128 Bytes).
737 */
738 #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) )
739
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100740 ret = uECC_sign( keypair->private_key, hash, hash_len, sig );
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100741 if( ret == UECC_FAULT_DETECTED )
742 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
743 if( ret != UECC_SUCCESS )
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200744 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
745
746 *sig_len = 2 * NUM_ECC_BYTES;
747
748 /* uECC owns its rng function pointer */
749 (void) f_rng;
750 (void) p_rng;
751 (void) md_alg;
752
753 return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len,
754 MAX_SECP256R1_ECDSA_SIG_LEN ) );
755
756 #undef MAX_SECP256R1_ECDSA_SIG_LEN
757}
758
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200759#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200760static void *uecc_eckey_alloc_wrap( void )
761{
762 return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) );
763}
764
765static void uecc_eckey_free_wrap( void *ctx )
766{
767 if( ctx == NULL )
768 return;
769
770 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) );
771 mbedtls_free( ctx );
772}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200773#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200774
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200775#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200776const mbedtls_pk_info_t mbedtls_uecc_eckey_info =
777 MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY );
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200778#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200779#endif /* MBEDTLS_USE_TINYCRYPT */
780
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200781/*
782 * Internal wrappers around ECDSA functions
783 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200784#if defined(MBEDTLS_ECDSA_C)
785static int ecdsa_can_do( mbedtls_pk_type_t type )
786{
787 return( type == MBEDTLS_PK_ECDSA );
788}
789
790static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
791 const unsigned char *hash, size_t hash_len,
792 const unsigned char *sig, size_t sig_len )
793{
794 int ret;
795 ((void) md_alg);
796
797 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
798 hash, hash_len, sig, sig_len );
799
800 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
801 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
802
803 return( ret );
804}
805
806static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
807 const unsigned char *hash, size_t hash_len,
808 unsigned char *sig, size_t *sig_len,
809 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
810{
811 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
812 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
813}
814
815#if defined(MBEDTLS_ECP_RESTARTABLE)
816static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
817 const unsigned char *hash, size_t hash_len,
818 const unsigned char *sig, size_t sig_len,
819 void *rs_ctx )
820{
821 int ret;
822 ((void) md_alg);
823
824 ret = mbedtls_ecdsa_read_signature_restartable(
825 (mbedtls_ecdsa_context *) ctx,
826 hash, hash_len, sig, sig_len,
827 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
828
829 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
830 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
831
832 return( ret );
833}
834
835static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
836 const unsigned char *hash, size_t hash_len,
837 unsigned char *sig, size_t *sig_len,
838 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
839 void *rs_ctx )
840{
841 return( mbedtls_ecdsa_write_signature_restartable(
842 (mbedtls_ecdsa_context *) ctx,
843 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
844 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
845
846}
847#endif /* MBEDTLS_ECP_RESTARTABLE */
848
849static void *ecdsa_alloc_wrap( void )
850{
851 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
852
853 if( ctx != NULL )
854 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
855
856 return( ctx );
857}
858
859static void ecdsa_free_wrap( void *ctx )
860{
861 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
862 mbedtls_free( ctx );
863}
864
865#if defined(MBEDTLS_ECP_RESTARTABLE)
866static void *ecdsa_rs_alloc( void )
867{
868 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
869
870 if( ctx != NULL )
871 mbedtls_ecdsa_restart_init( ctx );
872
873 return( ctx );
874}
875
876static void ecdsa_rs_free( void *ctx )
877{
878 mbedtls_ecdsa_restart_free( ctx );
879 mbedtls_free( ctx );
880}
881#endif /* MBEDTLS_ECP_RESTARTABLE */
882
883const mbedtls_pk_info_t mbedtls_ecdsa_info = {
884 MBEDTLS_PK_ECDSA,
885 "ECDSA",
886 eckey_get_bitlen, /* Compatible key structures */
887 ecdsa_can_do,
888 ecdsa_verify_wrap,
889 ecdsa_sign_wrap,
890#if defined(MBEDTLS_ECP_RESTARTABLE)
891 ecdsa_verify_rs_wrap,
892 ecdsa_sign_rs_wrap,
893#endif
894 NULL,
895 NULL,
896 eckey_check_pair, /* Compatible key structures */
897 ecdsa_alloc_wrap,
898 ecdsa_free_wrap,
899#if defined(MBEDTLS_ECP_RESTARTABLE)
900 ecdsa_rs_alloc,
901 ecdsa_rs_free,
902#endif
903 eckey_debug, /* Compatible key structures */
904};
905#endif /* MBEDTLS_ECDSA_C */
906
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200907/*
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200908 * Internal wrappers for RSA-alt support
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200909 */
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200910#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200911static int rsa_alt_can_do( mbedtls_pk_type_t type )
912{
913 return( type == MBEDTLS_PK_RSA );
914}
915
916static size_t rsa_alt_get_bitlen( const void *ctx )
917{
918 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
919
920 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
921}
922
923static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
924 const unsigned char *hash, size_t hash_len,
925 unsigned char *sig, size_t *sig_len,
926 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
927{
928 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
929
930#if SIZE_MAX > UINT_MAX
931 if( UINT_MAX < hash_len )
932 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
933#endif /* SIZE_MAX > UINT_MAX */
934
935 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
936
937 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
938 md_alg, (unsigned int) hash_len, hash, sig ) );
939}
940
941static int rsa_alt_decrypt_wrap( void *ctx,
942 const unsigned char *input, size_t ilen,
943 unsigned char *output, size_t *olen, size_t osize,
944 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
945{
946 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
947
948 ((void) f_rng);
949 ((void) p_rng);
950
951 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
952 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
953
954 return( rsa_alt->decrypt_func( rsa_alt->key,
955 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
956}
957
958#if defined(MBEDTLS_RSA_C)
959static int rsa_alt_check_pair( const void *pub, const void *prv )
960{
961 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
962 unsigned char hash[32];
963 size_t sig_len = 0;
964 int ret;
965
966 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
967 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
968
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200969 mbedtls_platform_memset( hash, 0x2a, sizeof( hash ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200970
971 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
972 hash, sizeof( hash ),
973 sig, &sig_len, NULL, NULL ) ) != 0 )
974 {
975 return( ret );
976 }
977
978 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
979 hash, sizeof( hash ), sig, sig_len ) != 0 )
980 {
981 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
982 }
983
984 return( 0 );
985}
986#endif /* MBEDTLS_RSA_C */
987
988static void *rsa_alt_alloc_wrap( void )
989{
990 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
991
992 if( ctx != NULL )
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200993 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200994
995 return( ctx );
996}
997
998static void rsa_alt_free_wrap( void *ctx )
999{
1000 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
1001 mbedtls_free( ctx );
1002}
1003
1004const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1005 MBEDTLS_PK_RSA_ALT,
1006 "RSA-alt",
1007 rsa_alt_get_bitlen,
1008 rsa_alt_can_do,
1009 NULL,
1010 rsa_alt_sign_wrap,
1011#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1012 NULL,
1013 NULL,
1014#endif
1015 rsa_alt_decrypt_wrap,
1016 NULL,
1017#if defined(MBEDTLS_RSA_C)
1018 rsa_alt_check_pair,
1019#else
1020 NULL,
1021#endif
1022 rsa_alt_alloc_wrap,
1023 rsa_alt_free_wrap,
1024#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1025 NULL,
1026 NULL,
1027#endif
1028 NULL,
1029};
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +02001030#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
1031
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001032/*
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001033 * Access to members of the pk_info structure. When a single PK type is
1034 * hardcoded, these should have zero runtime cost; otherwise, the usual
1035 * dynamic dispatch based on pk_info is used.
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001036 *
1037 * For function members, don't make a getter, but a function that directly
1038 * calls the method, so that we can entirely get rid of function pointers
1039 * when hardcoding a single PK - some compilers optimize better that way.
1040 *
1041 * Not implemented for members that are only present in builds with
Manuel Pégourié-Gonnard8b5e6bd2019-09-20 08:57:18 +02001042 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for this is builds
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001043 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
1044 */
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001045#if defined(MBEDTLS_PK_SINGLE_TYPE)
1046
1047MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001048 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001049{
1050 (void) info;
1051 return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) );
1052}
1053
1054MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001055 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001056{
1057 (void) info;
1058 return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) );
1059}
1060
1061MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001062 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001063{
1064 (void) info;
1065 return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) );
1066}
1067
1068MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001069 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001070{
1071 (void) info;
1072 return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) );
1073}
1074
1075MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001076 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001077 const unsigned char *hash, size_t hash_len,
1078 const unsigned char *sig, size_t sig_len )
1079{
1080 (void) info;
1081#if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1082 (void) ctx;
1083 (void) md_alg;
1084 (void) hash;
1085 (void) hash_len;
1086 (void) sig;
1087 (void) sig_len;
1088 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1089#else
1090 return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1091 ctx, md_alg, hash, hash_len, sig, sig_len ) );
1092#endif
1093}
1094
1095MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001096 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001097 const unsigned char *hash, size_t hash_len,
1098 unsigned char *sig, size_t *sig_len,
1099 int (*f_rng)(void *, unsigned char *, size_t),
1100 void *p_rng )
1101{
1102 (void) info;
1103#if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1104 (void) ctx;
1105 (void) md_alg;
1106 (void) hash;
1107 (void) hash_len;
1108 (void) sig;
1109 (void) sig_len;
1110 (void) f_rng;
1111 (void) p_rng;
1112 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1113#else
1114 return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1115 ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
1116#endif
1117}
1118
1119MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001120 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001121 const unsigned char *input, size_t ilen,
1122 unsigned char *output, size_t *olen, size_t osize,
1123 int (*f_rng)(void *, unsigned char *, size_t),
1124 void *p_rng )
1125{
1126 (void) info;
1127#if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1128 (void) ctx;
1129 (void) input;
1130 (void) ilen;
1131 (void) output;
1132 (void) olen;
1133 (void) osize;
1134 (void) f_rng;
1135 (void) p_rng;
1136 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1137#else
1138 return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1139 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1140#endif
1141}
1142
1143MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001144 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001145 const unsigned char *input, size_t ilen,
1146 unsigned char *output, size_t *olen, size_t osize,
1147 int (*f_rng)(void *, unsigned char *, size_t),
1148 void *p_rng )
1149{
1150 (void) info;
1151#if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1152 (void) ctx;
1153 (void) input;
1154 (void) ilen;
1155 (void) output;
1156 (void) olen;
1157 (void) osize;
1158 (void) f_rng;
1159 (void) p_rng;
1160 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1161#else
1162 return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1163 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1164#endif
1165}
1166
1167MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001168 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001169{
1170 (void) info;
1171#if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1172 (void) pub;
1173 (void) prv;
1174 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1175#else
1176 return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1177 pub, prv ) );
1178#endif
1179}
1180
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001181MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001182 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001183 const void *ctx, mbedtls_pk_debug_item *items )
1184{
1185 (void) info;
1186#if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1187 (void) ctx;
1188 (void) items;
1189 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1190#else
1191 return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) );
1192#endif
1193}
1194
1195#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001196
1197MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001198 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001199{
1200 return( info->type );
1201}
1202
1203MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001204 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001205{
1206 return( info->name );
1207}
1208
1209MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001210 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001211{
1212 return( info->get_bitlen( ctx ) );
1213}
1214
1215MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001216 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001217{
1218 return( info->can_do( type ) );
1219}
1220
1221MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001222 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001223 const unsigned char *hash, size_t hash_len,
1224 const unsigned char *sig, size_t sig_len )
1225{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001226 if( info->verify_func == NULL )
1227 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1228
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001229 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
1230}
1231
1232MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001233 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001234 const unsigned char *hash, size_t hash_len,
1235 unsigned char *sig, size_t *sig_len,
1236 int (*f_rng)(void *, unsigned char *, size_t),
1237 void *p_rng )
1238{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001239 if( info->sign_func == NULL )
1240 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1241
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001242 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
1243 f_rng, p_rng ) );
1244}
1245
1246MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001247 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001248 const unsigned char *input, size_t ilen,
1249 unsigned char *output, size_t *olen, size_t osize,
1250 int (*f_rng)(void *, unsigned char *, size_t),
1251 void *p_rng )
1252{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001253 if( info->decrypt_func == NULL )
1254 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1255
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001256 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
1257 f_rng, p_rng ) );
1258}
1259
1260MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001261 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001262 const unsigned char *input, size_t ilen,
1263 unsigned char *output, size_t *olen, size_t osize,
1264 int (*f_rng)(void *, unsigned char *, size_t),
1265 void *p_rng )
1266{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001267 if( info->encrypt_func == NULL )
1268 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1269
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001270 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
1271 f_rng, p_rng ) );
1272}
1273
1274MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001275 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001276{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001277 if( info->check_pair_func == NULL )
1278 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1279
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001280 return( info->check_pair_func( pub, prv ) );
1281}
1282
1283MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001284 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001285{
1286 return( info->ctx_alloc_func( ) );
1287}
1288
1289MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001290 mbedtls_pk_handle_t info, void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001291{
1292 info->ctx_free_func( ctx );
1293}
1294
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001295MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001296 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001297 const void *ctx, mbedtls_pk_debug_item *items )
1298{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001299 if( info->debug_func == NULL )
1300 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1301
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001302 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001303 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001304}
1305
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001306#endif /* MBEDTLS_PK_SINGLE_TYPE */
1307
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001308/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 * Initialise 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_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001312{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001313 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001314
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001315#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001316 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001317 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001318#else
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +02001319 memset( ctx, 0, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001320#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001321}
1322
1323/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001327{
irwir2239a862018-06-12 18:25:09 +03001328 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001329 return;
1330
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001331#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001332 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
1333 pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001334#endif
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +02001335
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001336 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001337}
1338
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001339#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001340/*
1341 * Initialize a restart context
1342 */
1343void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
1344{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001345 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001346 ctx->pk_info = NULL;
1347 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001348}
1349
1350/*
1351 * Free the components of a restart context
1352 */
1353void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
1354{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001355 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001356 ctx->pk_info->rs_free_func == NULL )
1357 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001358 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001359 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001360
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001361 ctx->pk_info->rs_free_func( ctx->rs_ctx );
1362
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001363 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001364 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001365}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001366#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001367
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001368/*
1369 * Get pk_info structure from type
1370 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001371mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001372{
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001373#if defined(MBEDTLS_PK_SINGLE_TYPE)
1374 if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) )
1375 return( MBEDTLS_PK_UNIQUE_VALID_HANDLE );
1376
1377 return( MBEDTLS_PK_INVALID_HANDLE );
1378
1379#else /* MBEDTLS_PK_SINGLE_TYPE */
1380
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001381 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_RSA_C)
1383 case MBEDTLS_PK_RSA:
1384 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 case MBEDTLS_PK_ECKEY_DH:
1388 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_ECDSA_C)
1391 case MBEDTLS_PK_ECDSA:
1392 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001393#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +01001394#if defined(MBEDTLS_USE_TINYCRYPT)
1395 case MBEDTLS_PK_ECKEY:
1396 return( &mbedtls_uecc_eckey_info );
1397#else /* MBEDTLS_USE_TINYCRYPT */
1398#if defined(MBEDTLS_ECP_C)
1399 case MBEDTLS_PK_ECKEY:
1400 return( &mbedtls_eckey_info );
1401#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +03001402#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001404 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001405 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001406 }
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001407#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001408}
1409
1410/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02001411 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001412 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001413int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001414{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001415 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001416 if( info == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001418
1419#if !defined(MBEDTLS_PK_SINGLE_TYPE)
1420 if( ctx->pk_info != NULL )
1421 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1422
1423 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001424
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001425 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001426 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001427#else
1428 (void) ctx;
1429#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001430
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001431 return( 0 );
1432}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +01001435/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001436 * Initialize an RSA-alt context
1437 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001438int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
1440 mbedtls_pk_rsa_alt_sign_func sign_func,
1441 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mbedtls_rsa_alt_context *rsa_alt;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001444 mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001445
Gilles Peskinee97dc602018-12-19 00:51:38 +01001446 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001447 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001449
1450 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001451 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001452
1453 ctx->pk_info = info;
1454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001456
1457 rsa_alt->key = key;
1458 rsa_alt->decrypt_func = decrypt_func;
1459 rsa_alt->sign_func = sign_func;
1460 rsa_alt->key_len_func = key_len_func;
1461
1462 return( 0 );
1463}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001465
1466/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001467 * Tell if a PK can do the operations of the given type
1468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001470{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001471 /* A context with null pk_info is not set up yet and can't do anything.
1472 * For backward compatibility, also accept NULL instead of a context
1473 * pointer. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001474 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001475 return( 0 );
1476
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001477 return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001478}
1479
1480/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001484{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001485 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001486
1487 if( *hash_len != 0 )
1488 return( 0 );
1489
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001490 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
1491 MBEDTLS_MD_INVALID_HANDLE )
1492 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001493 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001494 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001497 return( 0 );
1498}
1499
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001500#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001501/*
1502 * Helper to set up a restart context if needed
1503 */
1504static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001505 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001506{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001507 /* Don't do anything if already set up or invalid */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001508 if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001509 return( 0 );
1510
1511 /* Should never happen when we're called */
1512 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
1513 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1514
1515 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
1516 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1517
1518 ctx->pk_info = info;
1519
1520 return( 0 );
1521}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001522#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001523
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001524/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001525 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001526 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001527int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
1528 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001529 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001530 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001531 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001532{
Jarno Lamsab83a2132019-12-13 14:40:06 +02001533 volatile int verify_ret = MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001534 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001535 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1536 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001537 PK_VALIDATE_RET( sig != NULL );
1538
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001539 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001540 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001542
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001543#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001544 /* optimization: use non-restartable version if restart disabled */
1545 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001546 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001547 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001548 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001549 int ret;
1550
1551 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1552 return( ret );
1553
1554 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
1555 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
1556
1557 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1558 mbedtls_pk_restart_free( rs_ctx );
1559
1560 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001561 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001562#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001563 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001564#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001565
Jarno Lamsab83a2132019-12-13 14:40:06 +02001566 verify_ret = pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ),
1567 ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len );
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001568
Jarno Lamsab83a2132019-12-13 14:40:06 +02001569 if( verify_ret == 0 )
1570 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001571 mbedtls_platform_random_delay();
Jarno Lamsab83a2132019-12-13 14:40:06 +02001572 if( verify_ret == 0 )
1573 {
1574 return( verify_ret );
1575 }
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001576 else
1577 {
Piotr Nowickie048b912020-06-05 17:59:28 +02001578 verify_ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001579 }
Jarno Lamsab83a2132019-12-13 14:40:06 +02001580 }
1581
Jarno Lamsa91dbb792019-12-16 12:20:27 +02001582 return( verify_ret );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001583}
1584
1585/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001586 * Verify a signature
1587 */
1588int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1589 const unsigned char *hash, size_t hash_len,
1590 const unsigned char *sig, size_t sig_len )
1591{
1592 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
1593 sig, sig_len, NULL ) );
1594}
1595
1596/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001597 * Verify a signature with options
1598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
1600 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001601 const unsigned char *hash, size_t hash_len,
1602 const unsigned char *sig, size_t sig_len )
1603{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001604 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001605 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1606 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001607 PK_VALIDATE_RET( sig != NULL );
1608
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001609 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 if( ! mbedtls_pk_can_do( ctx, type ) )
1613 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001618 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001620
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001621#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001622 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
1623 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001624#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001625
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001626 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 if( sig_len < mbedtls_pk_get_len( ctx ) )
1632 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
1635 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001636 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001637 pss_opts->mgf1_hash_id,
1638 pss_opts->expected_salt_len,
1639 sig );
1640 if( ret != 0 )
1641 return( ret );
1642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( sig_len > mbedtls_pk_get_len( ctx ) )
1644 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001645
1646 return( 0 );
1647#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +00001649#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001650 }
1651
1652 /* General case: no options */
1653 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001657}
1658
1659/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001660 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001661 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001662int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
1663 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001664 const unsigned char *hash, size_t hash_len,
1665 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001666 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001667 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001668{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001669 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001670 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1671 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001672 PK_VALIDATE_RET( sig != NULL );
1673
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001674 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001675 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001677
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001678#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001679 /* optimization: use non-restartable version if restart disabled */
1680 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001681 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001682 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001683 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001684 int ret;
1685
1686 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1687 return( ret );
1688
1689 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
1690 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
1691
1692 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1693 mbedtls_pk_restart_free( rs_ctx );
1694
1695 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001696 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001697#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001698 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001699#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001700
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001701 return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1702 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001703}
1704
1705/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001706 * Make a signature
1707 */
1708int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1709 const unsigned char *hash, size_t hash_len,
1710 unsigned char *sig, size_t *sig_len,
1711 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1712{
1713 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
1714 sig, sig_len, f_rng, p_rng, NULL ) );
1715}
1716
1717/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001718 * Decrypt message
1719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001721 const unsigned char *input, size_t ilen,
1722 unsigned char *output, size_t *olen, size_t osize,
1723 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1724{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001725 PK_VALIDATE_RET( ctx != NULL );
1726 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1727 PK_VALIDATE_RET( output != NULL || osize == 0 );
1728 PK_VALIDATE_RET( olen != NULL );
1729
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001730 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001732
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001733 return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1734 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001735}
1736
1737/*
1738 * Encrypt message
1739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001741 const unsigned char *input, size_t ilen,
1742 unsigned char *output, size_t *olen, size_t osize,
1743 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1744{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001745 PK_VALIDATE_RET( ctx != NULL );
1746 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1747 PK_VALIDATE_RET( output != NULL || osize == 0 );
1748 PK_VALIDATE_RET( olen != NULL );
1749
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001750 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001752
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001753 return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1754 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001755}
1756
1757/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001758 * Check public-private key pair
1759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001761{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001762 PK_VALIDATE_RET( pub != NULL );
1763 PK_VALIDATE_RET( prv != NULL );
1764
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001765 if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001767
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001768#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001769 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001770 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001771 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001773 }
1774 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001775#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001776 {
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001777 if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001779 }
1780
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001781 return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ),
1782 pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001783}
1784
1785/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001786 * Get key size in bits
1787 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001788size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001789{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001790 /* For backward compatibility, accept NULL or a context that
1791 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001792 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001793 return( 0 );
1794
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001795 return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001796}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001797
1798/*
1799 * Export debug information
1800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001802{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001803 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001804 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001806
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001807 return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001808}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001809
1810/*
1811 * Access the PK type name
1812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001814{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001815 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001816 return( "invalid PK" );
1817
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001818 return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001819}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001820
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001821/*
1822 * Access the PK type
1823 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001825{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001826 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001828
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001829 return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001830}
1831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#endif /* MBEDTLS_PK_C */