blob: 9eddb61ab5ea1836a7d2906d6cb5c43d50118389 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020030#include "mbedtls/pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020031
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +020032#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020040#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020041#if defined(MBEDTLS_USE_TINYCRYPT)
42#include "tinycrypt/ecc.h"
43#include "tinycrypt/ecc_dsa.h"
44#include "mbedtls/asn1.h"
45#include "mbedtls/asn1write.h"
46#endif /* MBEDTLS_USE_TINYCRYPT */
47
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020048#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020049
50#if defined(MBEDTLS_PLATFORM_C)
51#include "mbedtls/platform.h"
52#else
53#include <stdlib.h>
54#define mbedtls_calloc calloc
55#define mbedtls_free free
56#endif
57
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +020058#include <string.h>
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020059#include <limits.h>
60#include <stdint.h>
61
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +020062/* Parameter validation macros based on platform_util.h */
63#define PK_VALIDATE_RET( cond ) \
64 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
65#define PK_VALIDATE( cond ) \
66 MBEDTLS_INTERNAL_VALIDATE( cond )
67
68/*
69 * Internal wrappers around RSA functions
70 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +020071#if defined(MBEDTLS_RSA_C)
72static int rsa_can_do( mbedtls_pk_type_t type )
73{
74 return( type == MBEDTLS_PK_RSA ||
75 type == MBEDTLS_PK_RSASSA_PSS );
76}
77
78static size_t rsa_get_bitlen( const void *ctx )
79{
80 const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
81 return( 8 * mbedtls_rsa_get_len( rsa ) );
82}
83
84static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
85 const unsigned char *hash, size_t hash_len,
86 const unsigned char *sig, size_t sig_len )
87{
88 int ret;
89 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
90 size_t rsa_len = mbedtls_rsa_get_len( rsa );
91
92#if SIZE_MAX > UINT_MAX
93 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
94 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
95#endif /* SIZE_MAX > UINT_MAX */
96
97 if( sig_len < rsa_len )
98 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
99
100 if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
101 MBEDTLS_RSA_PUBLIC, md_alg,
102 (unsigned int) hash_len, hash, sig ) ) != 0 )
103 return( ret );
104
105 /* The buffer contains a valid signature followed by extra data.
106 * We have a special error code for that so that so that callers can
107 * use mbedtls_pk_verify() to check "Does the buffer start with a
108 * valid signature?" and not just "Does the buffer contain a valid
109 * signature?". */
110 if( sig_len > rsa_len )
111 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
112
113 return( 0 );
114}
115
116static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
117 const unsigned char *hash, size_t hash_len,
118 unsigned char *sig, size_t *sig_len,
119 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
120{
121 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
122
123#if SIZE_MAX > UINT_MAX
124 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
125 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
126#endif /* SIZE_MAX > UINT_MAX */
127
128 *sig_len = mbedtls_rsa_get_len( rsa );
129
130 return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
131 md_alg, (unsigned int) hash_len, hash, sig ) );
132}
133
134static int rsa_decrypt_wrap( void *ctx,
135 const unsigned char *input, size_t ilen,
136 unsigned char *output, size_t *olen, size_t osize,
137 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
138{
139 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
140
141 if( ilen != mbedtls_rsa_get_len( rsa ) )
142 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
143
144 return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
145 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
146}
147
148static int rsa_encrypt_wrap( void *ctx,
149 const unsigned char *input, size_t ilen,
150 unsigned char *output, size_t *olen, size_t osize,
151 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
152{
153 mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
154 *olen = mbedtls_rsa_get_len( rsa );
155
156 if( *olen > osize )
157 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
158
159 return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
160 ilen, input, output ) );
161}
162
163static int rsa_check_pair_wrap( const void *pub, const void *prv )
164{
165 return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
166 (const mbedtls_rsa_context *) prv ) );
167}
168
169static void *rsa_alloc_wrap( void )
170{
171 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
172
173 if( ctx != NULL )
174 mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
175
176 return( ctx );
177}
178
179static void rsa_free_wrap( void *ctx )
180{
181 mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
182 mbedtls_free( ctx );
183}
184
185static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
186{
187 items->type = MBEDTLS_PK_DEBUG_MPI;
188 items->name = "rsa.N";
189 items->value = &( ((mbedtls_rsa_context *) ctx)->N );
190
191 items++;
192
193 items->type = MBEDTLS_PK_DEBUG_MPI;
194 items->name = "rsa.E";
195 items->value = &( ((mbedtls_rsa_context *) ctx)->E );
196}
197
198const mbedtls_pk_info_t mbedtls_rsa_info = {
199 MBEDTLS_PK_RSA,
200 "RSA",
201 rsa_get_bitlen,
202 rsa_can_do,
203 rsa_verify_wrap,
204 rsa_sign_wrap,
205#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
206 NULL,
207 NULL,
208#endif
209 rsa_decrypt_wrap,
210 rsa_encrypt_wrap,
211 rsa_check_pair_wrap,
212 rsa_alloc_wrap,
213 rsa_free_wrap,
214#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
215 NULL,
216 NULL,
217#endif
218 rsa_debug,
219};
220#endif /* MBEDTLS_RSA_C */
221
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200222/*
223 * Internal wrappers around ECC functions - based on ECP module
224 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200225#if defined(MBEDTLS_ECP_C)
226/*
227 * Generic EC key
228 */
229static int eckey_can_do( mbedtls_pk_type_t type )
230{
231 return( type == MBEDTLS_PK_ECKEY ||
232 type == MBEDTLS_PK_ECKEY_DH ||
233 type == MBEDTLS_PK_ECDSA );
234}
235
236static size_t eckey_get_bitlen( const void *ctx )
237{
238 return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
239}
240
241#if defined(MBEDTLS_ECDSA_C)
242/* Forward declarations */
243static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
244 const unsigned char *hash, size_t hash_len,
245 const unsigned char *sig, size_t sig_len );
246
247static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
248 const unsigned char *hash, size_t hash_len,
249 unsigned char *sig, size_t *sig_len,
250 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
251
252static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
253 const unsigned char *hash, size_t hash_len,
254 const unsigned char *sig, size_t sig_len )
255{
256 int ret;
257 mbedtls_ecdsa_context ecdsa;
258
259 mbedtls_ecdsa_init( &ecdsa );
260
261 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
262 ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
263
264 mbedtls_ecdsa_free( &ecdsa );
265
266 return( ret );
267}
268
269static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
270 const unsigned char *hash, size_t hash_len,
271 unsigned char *sig, size_t *sig_len,
272 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
273{
274 int ret;
275 mbedtls_ecdsa_context ecdsa;
276
277 mbedtls_ecdsa_init( &ecdsa );
278
279 if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
280 ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
281 f_rng, p_rng );
282
283 mbedtls_ecdsa_free( &ecdsa );
284
285 return( ret );
286}
287
288#if defined(MBEDTLS_ECP_RESTARTABLE)
289/* Forward declarations */
290static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
291 const unsigned char *hash, size_t hash_len,
292 const unsigned char *sig, size_t sig_len,
293 void *rs_ctx );
294
295static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
296 const unsigned char *hash, size_t hash_len,
297 unsigned char *sig, size_t *sig_len,
298 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
299 void *rs_ctx );
300
301/*
302 * Restart context for ECDSA operations with ECKEY context
303 *
304 * We need to store an actual ECDSA context, as we need to pass the same to
305 * the underlying ecdsa function, so we can't create it on the fly every time.
306 */
307typedef struct
308{
309 mbedtls_ecdsa_restart_ctx ecdsa_rs;
310 mbedtls_ecdsa_context ecdsa_ctx;
311} eckey_restart_ctx;
312
313static void *eckey_rs_alloc( void )
314{
315 eckey_restart_ctx *rs_ctx;
316
317 void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
318
319 if( ctx != NULL )
320 {
321 rs_ctx = ctx;
322 mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
323 mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
324 }
325
326 return( ctx );
327}
328
329static void eckey_rs_free( void *ctx )
330{
331 eckey_restart_ctx *rs_ctx;
332
333 if( ctx == NULL)
334 return;
335
336 rs_ctx = ctx;
337 mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
338 mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
339
340 mbedtls_free( ctx );
341}
342
343static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
344 const unsigned char *hash, size_t hash_len,
345 const unsigned char *sig, size_t sig_len,
346 void *rs_ctx )
347{
348 int ret;
349 eckey_restart_ctx *rs = rs_ctx;
350
351 /* Should never happen */
352 if( rs == NULL )
353 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
354
355 /* set up our own sub-context if needed (that is, on first run) */
356 if( rs->ecdsa_ctx.grp.pbits == 0 )
357 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
358
359 MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
360 md_alg, hash, hash_len,
361 sig, sig_len, &rs->ecdsa_rs ) );
362
363cleanup:
364 return( ret );
365}
366
367static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
368 const unsigned char *hash, size_t hash_len,
369 unsigned char *sig, size_t *sig_len,
370 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
371 void *rs_ctx )
372{
373 int ret;
374 eckey_restart_ctx *rs = rs_ctx;
375
376 /* Should never happen */
377 if( rs == NULL )
378 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
379
380 /* set up our own sub-context if needed (that is, on first run) */
381 if( rs->ecdsa_ctx.grp.pbits == 0 )
382 MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
383
384 MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
385 hash, hash_len, sig, sig_len,
386 f_rng, p_rng, &rs->ecdsa_rs ) );
387
388cleanup:
389 return( ret );
390}
391#endif /* MBEDTLS_ECP_RESTARTABLE */
392#endif /* MBEDTLS_ECDSA_C */
393
394static int eckey_check_pair( const void *pub, const void *prv )
395{
396 return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
397 (const mbedtls_ecp_keypair *) prv ) );
398}
399
400static void *eckey_alloc_wrap( void )
401{
402 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
403
404 if( ctx != NULL )
405 mbedtls_ecp_keypair_init( ctx );
406
407 return( ctx );
408}
409
410static void eckey_free_wrap( void *ctx )
411{
412 mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
413 mbedtls_free( ctx );
414}
415
416static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
417{
418 items->type = MBEDTLS_PK_DEBUG_ECP;
419 items->name = "eckey.Q";
420 items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
421}
422
423const mbedtls_pk_info_t mbedtls_eckey_info = {
424 MBEDTLS_PK_ECKEY,
425 "EC",
426 eckey_get_bitlen,
427 eckey_can_do,
428#if defined(MBEDTLS_ECDSA_C)
429 eckey_verify_wrap,
430 eckey_sign_wrap,
431#if defined(MBEDTLS_ECP_RESTARTABLE)
432 eckey_verify_rs_wrap,
433 eckey_sign_rs_wrap,
434#endif
435#else /* MBEDTLS_ECDSA_C */
436 NULL,
437 NULL,
438#endif /* MBEDTLS_ECDSA_C */
439 NULL,
440 NULL,
441 eckey_check_pair,
442 eckey_alloc_wrap,
443 eckey_free_wrap,
444#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
445 eckey_rs_alloc,
446 eckey_rs_free,
447#endif
448 eckey_debug,
449};
450
451/*
452 * EC key restricted to ECDH
453 */
454static int eckeydh_can_do( mbedtls_pk_type_t type )
455{
456 return( type == MBEDTLS_PK_ECKEY ||
457 type == MBEDTLS_PK_ECKEY_DH );
458}
459
460const mbedtls_pk_info_t mbedtls_eckeydh_info = {
461 MBEDTLS_PK_ECKEY_DH,
462 "EC_DH",
463 eckey_get_bitlen, /* Same underlying key structure */
464 eckeydh_can_do,
465 NULL,
466 NULL,
467#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
468 NULL,
469 NULL,
470#endif
471 NULL,
472 NULL,
473 eckey_check_pair,
474 eckey_alloc_wrap, /* Same underlying key structure */
475 eckey_free_wrap, /* Same underlying key structure */
476#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
477 NULL,
478 NULL,
479#endif
480 eckey_debug, /* Same underlying key structure */
481};
482#endif /* MBEDTLS_ECP_C */
483
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200484/*
485 * Internal wrappers around ECC functions - based on TinyCrypt
486 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200487#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200488/*
489 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
490 * those integers and convert it to the fixed-length encoding.
491 */
492static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
493 unsigned char *to, size_t to_len )
494{
495 int ret;
496 size_t unpadded_len, padding_len;
497
498 if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
499 MBEDTLS_ASN1_INTEGER ) ) != 0 )
500 {
501 return( ret );
502 }
503
504 while( unpadded_len > 0 && **from == 0x00 )
505 {
506 ( *from )++;
507 unpadded_len--;
508 }
509
510 if( unpadded_len > to_len || unpadded_len == 0 )
511 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
512
513 padding_len = to_len - unpadded_len;
514 memset( to, 0x00, padding_len );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300515 mbedtls_platform_memcpy( to + padding_len, *from, unpadded_len );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200516 ( *from ) += unpadded_len;
517
518 return( 0 );
519}
520
521/*
522 * Convert a signature from an ASN.1 sequence of two integers
523 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
524 * twice as big as int_size.
525 */
526static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
527 unsigned char *sig, size_t int_size )
528{
529 int ret;
530 size_t tmp_size;
531
532 if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
533 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
534 return( ret );
535
536 /* Extract r */
537 if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
538 return( ret );
539 /* Extract s */
540 if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
541 return( ret );
542
543 return( 0 );
544}
545
546static size_t uecc_eckey_get_bitlen( const void *ctx )
547{
548 (void) ctx;
549 return( (size_t) ( NUM_ECC_BYTES * 8 ) );
550}
551
552static int uecc_eckey_check_pair( const void *pub, const void *prv )
553{
554 const mbedtls_uecc_keypair *uecc_pub =
555 (const mbedtls_uecc_keypair *) pub;
556 const mbedtls_uecc_keypair *uecc_prv =
557 (const mbedtls_uecc_keypair *) prv;
558
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300559 if( mbedtls_platform_memcmp( uecc_pub->public_key,
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200560 uecc_prv->public_key,
561 2 * NUM_ECC_BYTES ) == 0 )
562 {
563 return( 0 );
564 }
565
566 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
567}
568
569static int uecc_eckey_can_do( mbedtls_pk_type_t type )
570{
571 return( type == MBEDTLS_PK_ECDSA ||
572 type == MBEDTLS_PK_ECKEY );
573}
574
575static int uecc_eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
576 const unsigned char *hash, size_t hash_len,
577 const unsigned char *sig, size_t sig_len )
578{
579 int ret;
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100580 volatile int ret_fi;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200581 uint8_t signature[2*NUM_ECC_BYTES];
582 unsigned char *p;
583 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
584 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
585
586 ((void) md_alg);
587 p = (unsigned char*) sig;
588
589 ret = extract_ecdsa_sig( &p, sig + sig_len, signature, NUM_ECC_BYTES );
590 if( ret != 0 )
591 return( ret );
592
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100593 ret_fi = uECC_verify( keypair->public_key, hash,
594 (unsigned) hash_len, signature, uecc_curve );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200595
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100596 if( ret_fi == UECC_ATTACK_DETECTED )
597 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
598
599 if( ret_fi == UECC_SUCCESS )
600 {
Manuel Pégourié-Gonnard72a8c9e2019-11-08 10:21:00 +0100601 mbedtls_platform_enforce_volatile_reads();
Manuel Pégourié-Gonnardca7b5ab2019-11-06 11:56:25 +0100602 if( ret_fi == UECC_SUCCESS )
603 return( 0 );
604 else
605 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
606 }
607
608 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200609}
610
611/*
612 * Simultaneously convert and move raw MPI from the beginning of a buffer
613 * to an ASN.1 MPI at the end of the buffer.
614 * See also mbedtls_asn1_write_mpi().
615 *
616 * p: pointer to the end of the output buffer
617 * start: start of the output buffer, and also of the mpi to write at the end
618 * n_len: length of the mpi to read from start
619 *
620 * Warning:
621 * The total length of the output buffer must be smaller than 128 Bytes.
622 */
623static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
624 size_t n_len )
625{
626 size_t len = 0;
627
628 if( (size_t)( *p - start ) < n_len )
629 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
630
631 len = n_len;
632 *p -= len;
633 memmove( *p, start, len );
634
635 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
636 * Neither r nor s should be 0, but as a failsafe measure, still detect
637 * that rather than overflowing the buffer in case of an error. */
638 while( len > 0 && **p == 0x00 )
639 {
640 ++(*p);
641 --len;
642 }
643
644 /* this is only reached if the signature was invalid */
645 if( len == 0 )
646 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
647
648 /* if the msb is 1, ASN.1 requires that we prepend a 0.
649 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
650 if( **p & 0x80 )
651 {
652 if( *p - start < 1 )
653 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
654
655 *--(*p) = 0x00;
656 len += 1;
657 }
658
659 /* The ASN.1 length encoding is just a single Byte containing the length,
660 * as we assume that the total buffer length is smaller than 128 Bytes. */
661 *--(*p) = len;
662 *--(*p) = MBEDTLS_ASN1_INTEGER;
663 len += 2;
664
665 return( (int) len );
666}
667
668/* Transcode signature from uECC format to ASN.1 sequence.
669 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
670 * MPIs, and in-place.
671 *
672 * [in/out] sig: the signature pre- and post-transcoding
673 * [in/out] sig_len: signature length pre- and post-transcoding
674 * [int] buf_len: the available size the in/out buffer
675 *
676 * Warning: buf_len must be smaller than 128 Bytes.
677 */
678static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
679 size_t buf_len )
680{
681 int ret;
682 size_t len = 0;
683 const size_t rs_len = *sig_len / 2;
684 unsigned char *p = sig + buf_len;
685
686 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
687 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
688
689 /* The ASN.1 length encoding is just a single Byte containing the length,
690 * as we assume that the total buffer length is smaller than 128 Bytes. */
691 *--p = len;
692 *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
693 len += 2;
694
695 memmove( sig, p, len );
696 *sig_len = len;
697
698 return( 0 );
699}
700
701static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
702 const unsigned char *hash, size_t hash_len,
703 unsigned char *sig, size_t *sig_len,
704 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
705{
706 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
707 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
708 int ret;
709
710 /*
711 * RFC-4492 page 20:
712 *
713 * Ecdsa-Sig-Value ::= SEQUENCE {
714 * r INTEGER,
715 * s INTEGER
716 * }
717 *
718 * Size is at most
719 * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s,
720 * twice that + 1 (tag) + 2 (len) for the sequence
721 *
722 * (The ASN.1 length encodings are all 1-Byte encodings because
723 * the total size is smaller than 128 Bytes).
724 */
725 #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) )
726
727 ret = uECC_sign( keypair->private_key, hash, hash_len, sig, uecc_curve );
728 /* TinyCrypt uses 0 to signal errors. */
729 if( ret == 0 )
730 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
731
732 *sig_len = 2 * NUM_ECC_BYTES;
733
734 /* uECC owns its rng function pointer */
735 (void) f_rng;
736 (void) p_rng;
737 (void) md_alg;
738
739 return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len,
740 MAX_SECP256R1_ECDSA_SIG_LEN ) );
741
742 #undef MAX_SECP256R1_ECDSA_SIG_LEN
743}
744
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200745#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200746static void *uecc_eckey_alloc_wrap( void )
747{
748 return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) );
749}
750
751static void uecc_eckey_free_wrap( void *ctx )
752{
753 if( ctx == NULL )
754 return;
755
756 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) );
757 mbedtls_free( ctx );
758}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200759#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200760
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200761#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200762const mbedtls_pk_info_t mbedtls_uecc_eckey_info =
763 MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY );
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200764#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200765#endif /* MBEDTLS_USE_TINYCRYPT */
766
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200767/*
768 * Internal wrappers around ECDSA functions
769 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200770#if defined(MBEDTLS_ECDSA_C)
771static int ecdsa_can_do( mbedtls_pk_type_t type )
772{
773 return( type == MBEDTLS_PK_ECDSA );
774}
775
776static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
777 const unsigned char *hash, size_t hash_len,
778 const unsigned char *sig, size_t sig_len )
779{
780 int ret;
781 ((void) md_alg);
782
783 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
784 hash, hash_len, sig, sig_len );
785
786 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
787 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
788
789 return( ret );
790}
791
792static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
793 const unsigned char *hash, size_t hash_len,
794 unsigned char *sig, size_t *sig_len,
795 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
796{
797 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
798 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
799}
800
801#if defined(MBEDTLS_ECP_RESTARTABLE)
802static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
803 const unsigned char *hash, size_t hash_len,
804 const unsigned char *sig, size_t sig_len,
805 void *rs_ctx )
806{
807 int ret;
808 ((void) md_alg);
809
810 ret = mbedtls_ecdsa_read_signature_restartable(
811 (mbedtls_ecdsa_context *) ctx,
812 hash, hash_len, sig, sig_len,
813 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
814
815 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
816 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
817
818 return( ret );
819}
820
821static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
822 const unsigned char *hash, size_t hash_len,
823 unsigned char *sig, size_t *sig_len,
824 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
825 void *rs_ctx )
826{
827 return( mbedtls_ecdsa_write_signature_restartable(
828 (mbedtls_ecdsa_context *) ctx,
829 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
830 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
831
832}
833#endif /* MBEDTLS_ECP_RESTARTABLE */
834
835static void *ecdsa_alloc_wrap( void )
836{
837 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
838
839 if( ctx != NULL )
840 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
841
842 return( ctx );
843}
844
845static void ecdsa_free_wrap( void *ctx )
846{
847 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
848 mbedtls_free( ctx );
849}
850
851#if defined(MBEDTLS_ECP_RESTARTABLE)
852static void *ecdsa_rs_alloc( void )
853{
854 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
855
856 if( ctx != NULL )
857 mbedtls_ecdsa_restart_init( ctx );
858
859 return( ctx );
860}
861
862static void ecdsa_rs_free( void *ctx )
863{
864 mbedtls_ecdsa_restart_free( ctx );
865 mbedtls_free( ctx );
866}
867#endif /* MBEDTLS_ECP_RESTARTABLE */
868
869const mbedtls_pk_info_t mbedtls_ecdsa_info = {
870 MBEDTLS_PK_ECDSA,
871 "ECDSA",
872 eckey_get_bitlen, /* Compatible key structures */
873 ecdsa_can_do,
874 ecdsa_verify_wrap,
875 ecdsa_sign_wrap,
876#if defined(MBEDTLS_ECP_RESTARTABLE)
877 ecdsa_verify_rs_wrap,
878 ecdsa_sign_rs_wrap,
879#endif
880 NULL,
881 NULL,
882 eckey_check_pair, /* Compatible key structures */
883 ecdsa_alloc_wrap,
884 ecdsa_free_wrap,
885#if defined(MBEDTLS_ECP_RESTARTABLE)
886 ecdsa_rs_alloc,
887 ecdsa_rs_free,
888#endif
889 eckey_debug, /* Compatible key structures */
890};
891#endif /* MBEDTLS_ECDSA_C */
892
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200893/*
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200894 * Internal wrappers for RSA-alt support
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200895 */
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200896#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200897static int rsa_alt_can_do( mbedtls_pk_type_t type )
898{
899 return( type == MBEDTLS_PK_RSA );
900}
901
902static size_t rsa_alt_get_bitlen( const void *ctx )
903{
904 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
905
906 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
907}
908
909static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
910 const unsigned char *hash, size_t hash_len,
911 unsigned char *sig, size_t *sig_len,
912 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
913{
914 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
915
916#if SIZE_MAX > UINT_MAX
917 if( UINT_MAX < hash_len )
918 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
919#endif /* SIZE_MAX > UINT_MAX */
920
921 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
922
923 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
924 md_alg, (unsigned int) hash_len, hash, sig ) );
925}
926
927static int rsa_alt_decrypt_wrap( void *ctx,
928 const unsigned char *input, size_t ilen,
929 unsigned char *output, size_t *olen, size_t osize,
930 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
931{
932 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
933
934 ((void) f_rng);
935 ((void) p_rng);
936
937 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
938 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
939
940 return( rsa_alt->decrypt_func( rsa_alt->key,
941 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
942}
943
944#if defined(MBEDTLS_RSA_C)
945static int rsa_alt_check_pair( const void *pub, const void *prv )
946{
947 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
948 unsigned char hash[32];
949 size_t sig_len = 0;
950 int ret;
951
952 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
953 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
954
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200955 mbedtls_platform_memset( hash, 0x2a, sizeof( hash ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200956
957 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
958 hash, sizeof( hash ),
959 sig, &sig_len, NULL, NULL ) ) != 0 )
960 {
961 return( ret );
962 }
963
964 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
965 hash, sizeof( hash ), sig, sig_len ) != 0 )
966 {
967 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
968 }
969
970 return( 0 );
971}
972#endif /* MBEDTLS_RSA_C */
973
974static void *rsa_alt_alloc_wrap( void )
975{
976 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
977
978 if( ctx != NULL )
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200979 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200980
981 return( ctx );
982}
983
984static void rsa_alt_free_wrap( void *ctx )
985{
986 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
987 mbedtls_free( ctx );
988}
989
990const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
991 MBEDTLS_PK_RSA_ALT,
992 "RSA-alt",
993 rsa_alt_get_bitlen,
994 rsa_alt_can_do,
995 NULL,
996 rsa_alt_sign_wrap,
997#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
998 NULL,
999 NULL,
1000#endif
1001 rsa_alt_decrypt_wrap,
1002 NULL,
1003#if defined(MBEDTLS_RSA_C)
1004 rsa_alt_check_pair,
1005#else
1006 NULL,
1007#endif
1008 rsa_alt_alloc_wrap,
1009 rsa_alt_free_wrap,
1010#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1011 NULL,
1012 NULL,
1013#endif
1014 NULL,
1015};
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +02001016#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
1017
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001018/*
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001019 * Access to members of the pk_info structure. When a single PK type is
1020 * hardcoded, these should have zero runtime cost; otherwise, the usual
1021 * dynamic dispatch based on pk_info is used.
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001022 *
1023 * For function members, don't make a getter, but a function that directly
1024 * calls the method, so that we can entirely get rid of function pointers
1025 * when hardcoding a single PK - some compilers optimize better that way.
1026 *
1027 * Not implemented for members that are only present in builds with
Manuel Pégourié-Gonnard8b5e6bd2019-09-20 08:57:18 +02001028 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for this is builds
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001029 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
1030 */
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001031#if defined(MBEDTLS_PK_SINGLE_TYPE)
1032
1033MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001034 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001035{
1036 (void) info;
1037 return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) );
1038}
1039
1040MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001041 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001042{
1043 (void) info;
1044 return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) );
1045}
1046
1047MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001048 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001049{
1050 (void) info;
1051 return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) );
1052}
1053
1054MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001055 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001056{
1057 (void) info;
1058 return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) );
1059}
1060
1061MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001062 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001063 const unsigned char *hash, size_t hash_len,
1064 const unsigned char *sig, size_t sig_len )
1065{
1066 (void) info;
1067#if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1068 (void) ctx;
1069 (void) md_alg;
1070 (void) hash;
1071 (void) hash_len;
1072 (void) sig;
1073 (void) sig_len;
1074 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1075#else
1076 return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1077 ctx, md_alg, hash, hash_len, sig, sig_len ) );
1078#endif
1079}
1080
1081MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001082 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001083 const unsigned char *hash, size_t hash_len,
1084 unsigned char *sig, size_t *sig_len,
1085 int (*f_rng)(void *, unsigned char *, size_t),
1086 void *p_rng )
1087{
1088 (void) info;
1089#if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1090 (void) ctx;
1091 (void) md_alg;
1092 (void) hash;
1093 (void) hash_len;
1094 (void) sig;
1095 (void) sig_len;
1096 (void) f_rng;
1097 (void) p_rng;
1098 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1099#else
1100 return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1101 ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
1102#endif
1103}
1104
1105MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001106 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001107 const unsigned char *input, size_t ilen,
1108 unsigned char *output, size_t *olen, size_t osize,
1109 int (*f_rng)(void *, unsigned char *, size_t),
1110 void *p_rng )
1111{
1112 (void) info;
1113#if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1114 (void) ctx;
1115 (void) input;
1116 (void) ilen;
1117 (void) output;
1118 (void) olen;
1119 (void) osize;
1120 (void) f_rng;
1121 (void) p_rng;
1122 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1123#else
1124 return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1125 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1126#endif
1127}
1128
1129MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001130 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001131 const unsigned char *input, size_t ilen,
1132 unsigned char *output, size_t *olen, size_t osize,
1133 int (*f_rng)(void *, unsigned char *, size_t),
1134 void *p_rng )
1135{
1136 (void) info;
1137#if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1138 (void) ctx;
1139 (void) input;
1140 (void) ilen;
1141 (void) output;
1142 (void) olen;
1143 (void) osize;
1144 (void) f_rng;
1145 (void) p_rng;
1146 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1147#else
1148 return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1149 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1150#endif
1151}
1152
1153MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001154 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001155{
1156 (void) info;
1157#if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1158 (void) pub;
1159 (void) prv;
1160 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1161#else
1162 return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1163 pub, prv ) );
1164#endif
1165}
1166
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001167MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001168 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001169 const void *ctx, mbedtls_pk_debug_item *items )
1170{
1171 (void) info;
1172#if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1173 (void) ctx;
1174 (void) items;
1175 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1176#else
1177 return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) );
1178#endif
1179}
1180
1181#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001182
1183MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001184 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001185{
1186 return( info->type );
1187}
1188
1189MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001190 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001191{
1192 return( info->name );
1193}
1194
1195MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001196 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001197{
1198 return( info->get_bitlen( ctx ) );
1199}
1200
1201MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001202 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001203{
1204 return( info->can_do( type ) );
1205}
1206
1207MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001208 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001209 const unsigned char *hash, size_t hash_len,
1210 const unsigned char *sig, size_t sig_len )
1211{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001212 if( info->verify_func == NULL )
1213 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1214
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001215 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
1216}
1217
1218MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001219 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001220 const unsigned char *hash, size_t hash_len,
1221 unsigned char *sig, size_t *sig_len,
1222 int (*f_rng)(void *, unsigned char *, size_t),
1223 void *p_rng )
1224{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001225 if( info->sign_func == NULL )
1226 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1227
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001228 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
1229 f_rng, p_rng ) );
1230}
1231
1232MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001233 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001234 const unsigned char *input, size_t ilen,
1235 unsigned char *output, size_t *olen, size_t osize,
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->decrypt_func == NULL )
1240 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1241
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001242 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
1243 f_rng, p_rng ) );
1244}
1245
1246MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_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->encrypt_func == NULL )
1254 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1255
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001256 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
1257 f_rng, p_rng ) );
1258}
1259
1260MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001261 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001262{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001263 if( info->check_pair_func == NULL )
1264 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1265
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001266 return( info->check_pair_func( pub, prv ) );
1267}
1268
1269MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001270 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001271{
1272 return( info->ctx_alloc_func( ) );
1273}
1274
1275MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001276 mbedtls_pk_handle_t info, void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001277{
1278 info->ctx_free_func( ctx );
1279}
1280
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001281MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001282 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001283 const void *ctx, mbedtls_pk_debug_item *items )
1284{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001285 if( info->debug_func == NULL )
1286 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1287
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001288 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001289 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001290}
1291
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001292#endif /* MBEDTLS_PK_SINGLE_TYPE */
1293
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001294/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001298{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001299 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001300
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001301#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001302 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001303 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001304#else
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +02001305 memset( ctx, 0, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001306#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001307}
1308
1309/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001313{
irwir2239a862018-06-12 18:25:09 +03001314 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001315 return;
1316
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001317#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001318 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
1319 pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001320#endif
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +02001321
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001322 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001323}
1324
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001325#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001326/*
1327 * Initialize a restart context
1328 */
1329void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
1330{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001331 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001332 ctx->pk_info = NULL;
1333 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001334}
1335
1336/*
1337 * Free the components of a restart context
1338 */
1339void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
1340{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001341 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001342 ctx->pk_info->rs_free_func == NULL )
1343 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001344 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001345 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001346
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001347 ctx->pk_info->rs_free_func( ctx->rs_ctx );
1348
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001349 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001350 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001351}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001352#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001353
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001354/*
1355 * Get pk_info structure from type
1356 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001357mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001358{
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001359#if defined(MBEDTLS_PK_SINGLE_TYPE)
1360 if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) )
1361 return( MBEDTLS_PK_UNIQUE_VALID_HANDLE );
1362
1363 return( MBEDTLS_PK_INVALID_HANDLE );
1364
1365#else /* MBEDTLS_PK_SINGLE_TYPE */
1366
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001367 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_RSA_C)
1369 case MBEDTLS_PK_RSA:
1370 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 case MBEDTLS_PK_ECKEY_DH:
1374 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_ECDSA_C)
1377 case MBEDTLS_PK_ECDSA:
1378 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001379#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +01001380#if defined(MBEDTLS_USE_TINYCRYPT)
1381 case MBEDTLS_PK_ECKEY:
1382 return( &mbedtls_uecc_eckey_info );
1383#else /* MBEDTLS_USE_TINYCRYPT */
1384#if defined(MBEDTLS_ECP_C)
1385 case MBEDTLS_PK_ECKEY:
1386 return( &mbedtls_eckey_info );
1387#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +03001388#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001390 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001391 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001392 }
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001393#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001394}
1395
1396/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02001397 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001398 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001399int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001400{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001401 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001402 if( info == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001404
1405#if !defined(MBEDTLS_PK_SINGLE_TYPE)
1406 if( ctx->pk_info != NULL )
1407 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1408
1409 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001410
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001411 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001412 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001413#else
1414 (void) ctx;
1415#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001416
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001417 return( 0 );
1418}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +01001421/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001422 * Initialize an RSA-alt context
1423 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001424int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
1426 mbedtls_pk_rsa_alt_sign_func sign_func,
1427 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001428{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_rsa_alt_context *rsa_alt;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001430 mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001431
Gilles Peskinee97dc602018-12-19 00:51:38 +01001432 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001433 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001435
1436 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001437 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001438
1439 ctx->pk_info = info;
1440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001442
1443 rsa_alt->key = key;
1444 rsa_alt->decrypt_func = decrypt_func;
1445 rsa_alt->sign_func = sign_func;
1446 rsa_alt->key_len_func = key_len_func;
1447
1448 return( 0 );
1449}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001451
1452/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001453 * Tell if a PK can do the operations of the given type
1454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001456{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001457 /* A context with null pk_info is not set up yet and can't do anything.
1458 * For backward compatibility, also accept NULL instead of a context
1459 * pointer. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001460 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001461 return( 0 );
1462
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001463 return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001464}
1465
1466/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001470{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001471 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001472
1473 if( *hash_len != 0 )
1474 return( 0 );
1475
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001476 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
1477 MBEDTLS_MD_INVALID_HANDLE )
1478 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001479 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001480 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001483 return( 0 );
1484}
1485
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001486#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001487/*
1488 * Helper to set up a restart context if needed
1489 */
1490static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001491 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001492{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001493 /* Don't do anything if already set up or invalid */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001494 if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001495 return( 0 );
1496
1497 /* Should never happen when we're called */
1498 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
1499 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1500
1501 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
1502 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1503
1504 ctx->pk_info = info;
1505
1506 return( 0 );
1507}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001508#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001509
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001510/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001511 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001512 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001513int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
1514 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001515 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001516 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001517 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001518{
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
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001551 return( pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ),
1552 ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001553}
1554
1555/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001556 * Verify a signature
1557 */
1558int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1559 const unsigned char *hash, size_t hash_len,
1560 const unsigned char *sig, size_t sig_len )
1561{
1562 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
1563 sig, sig_len, NULL ) );
1564}
1565
1566/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001567 * Verify a signature with options
1568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
1570 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001571 const unsigned char *hash, size_t hash_len,
1572 const unsigned char *sig, size_t sig_len )
1573{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001574 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001575 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1576 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001577 PK_VALIDATE_RET( sig != NULL );
1578
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001579 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 if( ! mbedtls_pk_can_do( ctx, type ) )
1583 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001588 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001590
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001591#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001592 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
1593 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001594#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001595
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001596 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 if( sig_len < mbedtls_pk_get_len( ctx ) )
1602 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
1605 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001606 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001607 pss_opts->mgf1_hash_id,
1608 pss_opts->expected_salt_len,
1609 sig );
1610 if( ret != 0 )
1611 return( ret );
1612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 if( sig_len > mbedtls_pk_get_len( ctx ) )
1614 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001615
1616 return( 0 );
1617#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +00001619#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001620 }
1621
1622 /* General case: no options */
1623 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001627}
1628
1629/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001630 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001631 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001632int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
1633 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001634 const unsigned char *hash, size_t hash_len,
1635 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001636 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001637 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001638{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001639 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001640 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1641 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001642 PK_VALIDATE_RET( sig != NULL );
1643
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001644 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001645 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001647
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001648#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001649 /* optimization: use non-restartable version if restart disabled */
1650 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001651 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001652 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001653 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001654 int ret;
1655
1656 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1657 return( ret );
1658
1659 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
1660 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
1661
1662 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1663 mbedtls_pk_restart_free( rs_ctx );
1664
1665 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001666 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001667#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001668 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001669#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001670
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001671 return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1672 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001673}
1674
1675/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001676 * Make a signature
1677 */
1678int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1679 const unsigned char *hash, size_t hash_len,
1680 unsigned char *sig, size_t *sig_len,
1681 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1682{
1683 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
1684 sig, sig_len, f_rng, p_rng, NULL ) );
1685}
1686
1687/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001688 * Decrypt message
1689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001691 const unsigned char *input, size_t ilen,
1692 unsigned char *output, size_t *olen, size_t osize,
1693 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1694{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001695 PK_VALIDATE_RET( ctx != NULL );
1696 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1697 PK_VALIDATE_RET( output != NULL || osize == 0 );
1698 PK_VALIDATE_RET( olen != NULL );
1699
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001700 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001702
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001703 return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1704 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001705}
1706
1707/*
1708 * Encrypt message
1709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001711 const unsigned char *input, size_t ilen,
1712 unsigned char *output, size_t *olen, size_t osize,
1713 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1714{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001715 PK_VALIDATE_RET( ctx != NULL );
1716 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1717 PK_VALIDATE_RET( output != NULL || osize == 0 );
1718 PK_VALIDATE_RET( olen != NULL );
1719
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001720 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001722
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001723 return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1724 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001725}
1726
1727/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001728 * Check public-private key pair
1729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001731{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001732 PK_VALIDATE_RET( pub != NULL );
1733 PK_VALIDATE_RET( prv != NULL );
1734
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001735 if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001737
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001738#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001739 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001740 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001741 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001743 }
1744 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001745#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001746 {
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001747 if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001749 }
1750
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001751 return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ),
1752 pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001753}
1754
1755/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001756 * Get key size in bits
1757 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001758size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001759{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001760 /* For backward compatibility, accept NULL or a context that
1761 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001762 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001763 return( 0 );
1764
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001765 return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001766}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001767
1768/*
1769 * Export debug information
1770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001771int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001772{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001773 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001774 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001776
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001777 return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001778}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001779
1780/*
1781 * Access the PK type name
1782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001784{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001785 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001786 return( "invalid PK" );
1787
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001788 return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001789}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001790
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001791/*
1792 * Access the PK type
1793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001795{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001796 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001798
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001799 return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001800}
1801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#endif /* MBEDTLS_PK_C */