blob: fbe711dc2de7eb54348aa314fc7a89a38b67852c [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;
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 {
Manuel Pégourié-Gonnard72a8c9e2019-11-08 10:21:00 +0100600 mbedtls_platform_enforce_volatile_reads();
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{
625 size_t len = 0;
626
627 if( (size_t)( *p - start ) < n_len )
628 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
629
630 len = n_len;
631 *p -= len;
632 memmove( *p, start, len );
633
634 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
635 * Neither r nor s should be 0, but as a failsafe measure, still detect
636 * that rather than overflowing the buffer in case of an error. */
637 while( len > 0 && **p == 0x00 )
638 {
639 ++(*p);
640 --len;
641 }
642
643 /* this is only reached if the signature was invalid */
644 if( len == 0 )
645 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
646
647 /* if the msb is 1, ASN.1 requires that we prepend a 0.
648 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
649 if( **p & 0x80 )
650 {
651 if( *p - start < 1 )
652 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
653
654 *--(*p) = 0x00;
655 len += 1;
656 }
657
658 /* The ASN.1 length encoding is just a single Byte containing the length,
659 * as we assume that the total buffer length is smaller than 128 Bytes. */
660 *--(*p) = len;
661 *--(*p) = MBEDTLS_ASN1_INTEGER;
662 len += 2;
663
664 return( (int) len );
665}
666
667/* Transcode signature from uECC format to ASN.1 sequence.
668 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
669 * MPIs, and in-place.
670 *
671 * [in/out] sig: the signature pre- and post-transcoding
672 * [in/out] sig_len: signature length pre- and post-transcoding
673 * [int] buf_len: the available size the in/out buffer
674 *
675 * Warning: buf_len must be smaller than 128 Bytes.
676 */
677static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
678 size_t buf_len )
679{
680 int ret;
681 size_t len = 0;
682 const size_t rs_len = *sig_len / 2;
683 unsigned char *p = sig + buf_len;
684
685 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
686 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
687
688 /* The ASN.1 length encoding is just a single Byte containing the length,
689 * as we assume that the total buffer length is smaller than 128 Bytes. */
690 *--p = len;
691 *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
692 len += 2;
693
694 memmove( sig, p, len );
695 *sig_len = len;
696
697 return( 0 );
698}
699
700static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
701 const unsigned char *hash, size_t hash_len,
702 unsigned char *sig, size_t *sig_len,
703 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
704{
705 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200706 int ret;
707
708 /*
709 * RFC-4492 page 20:
710 *
711 * Ecdsa-Sig-Value ::= SEQUENCE {
712 * r INTEGER,
713 * s INTEGER
714 * }
715 *
716 * Size is at most
717 * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s,
718 * twice that + 1 (tag) + 2 (len) for the sequence
719 *
720 * (The ASN.1 length encodings are all 1-Byte encodings because
721 * the total size is smaller than 128 Bytes).
722 */
723 #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) )
724
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100725 ret = uECC_sign( keypair->private_key, hash, hash_len, sig );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200726 /* TinyCrypt uses 0 to signal errors. */
727 if( ret == 0 )
728 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
729
730 *sig_len = 2 * NUM_ECC_BYTES;
731
732 /* uECC owns its rng function pointer */
733 (void) f_rng;
734 (void) p_rng;
735 (void) md_alg;
736
737 return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len,
738 MAX_SECP256R1_ECDSA_SIG_LEN ) );
739
740 #undef MAX_SECP256R1_ECDSA_SIG_LEN
741}
742
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200743#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200744static void *uecc_eckey_alloc_wrap( void )
745{
746 return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) );
747}
748
749static void uecc_eckey_free_wrap( void *ctx )
750{
751 if( ctx == NULL )
752 return;
753
754 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) );
755 mbedtls_free( ctx );
756}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200757#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200758
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200759#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200760const mbedtls_pk_info_t mbedtls_uecc_eckey_info =
761 MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY );
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200762#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200763#endif /* MBEDTLS_USE_TINYCRYPT */
764
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200765/*
766 * Internal wrappers around ECDSA functions
767 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200768#if defined(MBEDTLS_ECDSA_C)
769static int ecdsa_can_do( mbedtls_pk_type_t type )
770{
771 return( type == MBEDTLS_PK_ECDSA );
772}
773
774static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
775 const unsigned char *hash, size_t hash_len,
776 const unsigned char *sig, size_t sig_len )
777{
778 int ret;
779 ((void) md_alg);
780
781 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
782 hash, hash_len, sig, sig_len );
783
784 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
785 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
786
787 return( ret );
788}
789
790static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
791 const unsigned char *hash, size_t hash_len,
792 unsigned char *sig, size_t *sig_len,
793 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
794{
795 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
796 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
797}
798
799#if defined(MBEDTLS_ECP_RESTARTABLE)
800static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
801 const unsigned char *hash, size_t hash_len,
802 const unsigned char *sig, size_t sig_len,
803 void *rs_ctx )
804{
805 int ret;
806 ((void) md_alg);
807
808 ret = mbedtls_ecdsa_read_signature_restartable(
809 (mbedtls_ecdsa_context *) ctx,
810 hash, hash_len, sig, sig_len,
811 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
812
813 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
814 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
815
816 return( ret );
817}
818
819static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
820 const unsigned char *hash, size_t hash_len,
821 unsigned char *sig, size_t *sig_len,
822 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
823 void *rs_ctx )
824{
825 return( mbedtls_ecdsa_write_signature_restartable(
826 (mbedtls_ecdsa_context *) ctx,
827 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
828 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
829
830}
831#endif /* MBEDTLS_ECP_RESTARTABLE */
832
833static void *ecdsa_alloc_wrap( void )
834{
835 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
836
837 if( ctx != NULL )
838 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
839
840 return( ctx );
841}
842
843static void ecdsa_free_wrap( void *ctx )
844{
845 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
846 mbedtls_free( ctx );
847}
848
849#if defined(MBEDTLS_ECP_RESTARTABLE)
850static void *ecdsa_rs_alloc( void )
851{
852 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
853
854 if( ctx != NULL )
855 mbedtls_ecdsa_restart_init( ctx );
856
857 return( ctx );
858}
859
860static void ecdsa_rs_free( void *ctx )
861{
862 mbedtls_ecdsa_restart_free( ctx );
863 mbedtls_free( ctx );
864}
865#endif /* MBEDTLS_ECP_RESTARTABLE */
866
867const mbedtls_pk_info_t mbedtls_ecdsa_info = {
868 MBEDTLS_PK_ECDSA,
869 "ECDSA",
870 eckey_get_bitlen, /* Compatible key structures */
871 ecdsa_can_do,
872 ecdsa_verify_wrap,
873 ecdsa_sign_wrap,
874#if defined(MBEDTLS_ECP_RESTARTABLE)
875 ecdsa_verify_rs_wrap,
876 ecdsa_sign_rs_wrap,
877#endif
878 NULL,
879 NULL,
880 eckey_check_pair, /* Compatible key structures */
881 ecdsa_alloc_wrap,
882 ecdsa_free_wrap,
883#if defined(MBEDTLS_ECP_RESTARTABLE)
884 ecdsa_rs_alloc,
885 ecdsa_rs_free,
886#endif
887 eckey_debug, /* Compatible key structures */
888};
889#endif /* MBEDTLS_ECDSA_C */
890
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200891/*
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200892 * Internal wrappers for RSA-alt support
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200893 */
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200894#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200895static int rsa_alt_can_do( mbedtls_pk_type_t type )
896{
897 return( type == MBEDTLS_PK_RSA );
898}
899
900static size_t rsa_alt_get_bitlen( const void *ctx )
901{
902 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
903
904 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
905}
906
907static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
908 const unsigned char *hash, size_t hash_len,
909 unsigned char *sig, size_t *sig_len,
910 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
911{
912 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
913
914#if SIZE_MAX > UINT_MAX
915 if( UINT_MAX < hash_len )
916 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
917#endif /* SIZE_MAX > UINT_MAX */
918
919 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
920
921 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
922 md_alg, (unsigned int) hash_len, hash, sig ) );
923}
924
925static int rsa_alt_decrypt_wrap( void *ctx,
926 const unsigned char *input, size_t ilen,
927 unsigned char *output, size_t *olen, size_t osize,
928 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
929{
930 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
931
932 ((void) f_rng);
933 ((void) p_rng);
934
935 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
936 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
937
938 return( rsa_alt->decrypt_func( rsa_alt->key,
939 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
940}
941
942#if defined(MBEDTLS_RSA_C)
943static int rsa_alt_check_pair( const void *pub, const void *prv )
944{
945 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
946 unsigned char hash[32];
947 size_t sig_len = 0;
948 int ret;
949
950 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
951 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
952
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200953 mbedtls_platform_memset( hash, 0x2a, sizeof( hash ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200954
955 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
956 hash, sizeof( hash ),
957 sig, &sig_len, NULL, NULL ) ) != 0 )
958 {
959 return( ret );
960 }
961
962 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
963 hash, sizeof( hash ), sig, sig_len ) != 0 )
964 {
965 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
966 }
967
968 return( 0 );
969}
970#endif /* MBEDTLS_RSA_C */
971
972static void *rsa_alt_alloc_wrap( void )
973{
974 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
975
976 if( ctx != NULL )
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200977 mbedtls_platform_memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200978
979 return( ctx );
980}
981
982static void rsa_alt_free_wrap( void *ctx )
983{
984 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
985 mbedtls_free( ctx );
986}
987
988const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
989 MBEDTLS_PK_RSA_ALT,
990 "RSA-alt",
991 rsa_alt_get_bitlen,
992 rsa_alt_can_do,
993 NULL,
994 rsa_alt_sign_wrap,
995#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
996 NULL,
997 NULL,
998#endif
999 rsa_alt_decrypt_wrap,
1000 NULL,
1001#if defined(MBEDTLS_RSA_C)
1002 rsa_alt_check_pair,
1003#else
1004 NULL,
1005#endif
1006 rsa_alt_alloc_wrap,
1007 rsa_alt_free_wrap,
1008#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1009 NULL,
1010 NULL,
1011#endif
1012 NULL,
1013};
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +02001014#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
1015
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001016/*
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001017 * Access to members of the pk_info structure. When a single PK type is
1018 * hardcoded, these should have zero runtime cost; otherwise, the usual
1019 * dynamic dispatch based on pk_info is used.
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001020 *
1021 * For function members, don't make a getter, but a function that directly
1022 * calls the method, so that we can entirely get rid of function pointers
1023 * when hardcoding a single PK - some compilers optimize better that way.
1024 *
1025 * Not implemented for members that are only present in builds with
Manuel Pégourié-Gonnard8b5e6bd2019-09-20 08:57:18 +02001026 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for this is builds
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001027 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
1028 */
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001029#if defined(MBEDTLS_PK_SINGLE_TYPE)
1030
1031MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001032 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001033{
1034 (void) info;
1035 return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) );
1036}
1037
1038MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001039 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001040{
1041 (void) info;
1042 return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) );
1043}
1044
1045MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001046 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001047{
1048 (void) info;
1049 return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) );
1050}
1051
1052MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001053 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001054{
1055 (void) info;
1056 return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) );
1057}
1058
1059MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001060 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001061 const unsigned char *hash, size_t hash_len,
1062 const unsigned char *sig, size_t sig_len )
1063{
1064 (void) info;
1065#if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1066 (void) ctx;
1067 (void) md_alg;
1068 (void) hash;
1069 (void) hash_len;
1070 (void) sig;
1071 (void) sig_len;
1072 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1073#else
1074 return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1075 ctx, md_alg, hash, hash_len, sig, sig_len ) );
1076#endif
1077}
1078
1079MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001080 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001081 const unsigned char *hash, size_t hash_len,
1082 unsigned char *sig, size_t *sig_len,
1083 int (*f_rng)(void *, unsigned char *, size_t),
1084 void *p_rng )
1085{
1086 (void) info;
1087#if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1088 (void) ctx;
1089 (void) md_alg;
1090 (void) hash;
1091 (void) hash_len;
1092 (void) sig;
1093 (void) sig_len;
1094 (void) f_rng;
1095 (void) p_rng;
1096 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1097#else
1098 return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1099 ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
1100#endif
1101}
1102
1103MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001104 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001105 const unsigned char *input, size_t ilen,
1106 unsigned char *output, size_t *olen, size_t osize,
1107 int (*f_rng)(void *, unsigned char *, size_t),
1108 void *p_rng )
1109{
1110 (void) info;
1111#if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1112 (void) ctx;
1113 (void) input;
1114 (void) ilen;
1115 (void) output;
1116 (void) olen;
1117 (void) osize;
1118 (void) f_rng;
1119 (void) p_rng;
1120 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1121#else
1122 return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1123 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1124#endif
1125}
1126
1127MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001128 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001129 const unsigned char *input, size_t ilen,
1130 unsigned char *output, size_t *olen, size_t osize,
1131 int (*f_rng)(void *, unsigned char *, size_t),
1132 void *p_rng )
1133{
1134 (void) info;
1135#if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1136 (void) ctx;
1137 (void) input;
1138 (void) ilen;
1139 (void) output;
1140 (void) olen;
1141 (void) osize;
1142 (void) f_rng;
1143 (void) p_rng;
1144 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1145#else
1146 return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1147 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1148#endif
1149}
1150
1151MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001152 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001153{
1154 (void) info;
1155#if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1156 (void) pub;
1157 (void) prv;
1158 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1159#else
1160 return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1161 pub, prv ) );
1162#endif
1163}
1164
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001165MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001166 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001167 const void *ctx, mbedtls_pk_debug_item *items )
1168{
1169 (void) info;
1170#if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1171 (void) ctx;
1172 (void) items;
1173 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1174#else
1175 return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) );
1176#endif
1177}
1178
1179#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001180
1181MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001182 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001183{
1184 return( info->type );
1185}
1186
1187MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001188 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001189{
1190 return( info->name );
1191}
1192
1193MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001194 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001195{
1196 return( info->get_bitlen( ctx ) );
1197}
1198
1199MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001200 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001201{
1202 return( info->can_do( type ) );
1203}
1204
1205MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001206 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001207 const unsigned char *hash, size_t hash_len,
1208 const unsigned char *sig, size_t sig_len )
1209{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001210 if( info->verify_func == NULL )
1211 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1212
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001213 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
1214}
1215
1216MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001217 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001218 const unsigned char *hash, size_t hash_len,
1219 unsigned char *sig, size_t *sig_len,
1220 int (*f_rng)(void *, unsigned char *, size_t),
1221 void *p_rng )
1222{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001223 if( info->sign_func == NULL )
1224 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1225
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001226 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
1227 f_rng, p_rng ) );
1228}
1229
1230MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001231 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001232 const unsigned char *input, size_t ilen,
1233 unsigned char *output, size_t *olen, size_t osize,
1234 int (*f_rng)(void *, unsigned char *, size_t),
1235 void *p_rng )
1236{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001237 if( info->decrypt_func == NULL )
1238 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1239
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001240 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
1241 f_rng, p_rng ) );
1242}
1243
1244MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001245 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001246 const unsigned char *input, size_t ilen,
1247 unsigned char *output, size_t *olen, size_t osize,
1248 int (*f_rng)(void *, unsigned char *, size_t),
1249 void *p_rng )
1250{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001251 if( info->encrypt_func == NULL )
1252 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1253
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001254 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
1255 f_rng, p_rng ) );
1256}
1257
1258MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001259 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001260{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001261 if( info->check_pair_func == NULL )
1262 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1263
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001264 return( info->check_pair_func( pub, prv ) );
1265}
1266
1267MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001268 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001269{
1270 return( info->ctx_alloc_func( ) );
1271}
1272
1273MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001274 mbedtls_pk_handle_t info, void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001275{
1276 info->ctx_free_func( ctx );
1277}
1278
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001279MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001280 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001281 const void *ctx, mbedtls_pk_debug_item *items )
1282{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001283 if( info->debug_func == NULL )
1284 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1285
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001286 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001287 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001288}
1289
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001290#endif /* MBEDTLS_PK_SINGLE_TYPE */
1291
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001292/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001296{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001297 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001298
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001299#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001300 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001301 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001302#else
Manuel Pégourié-Gonnard99419332019-10-03 10:40:57 +02001303 memset( ctx, 0, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001304#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001305}
1306
1307/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001311{
irwir2239a862018-06-12 18:25:09 +03001312 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001313 return;
1314
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001315#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001316 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
1317 pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001318#endif
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +02001319
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001320 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001321}
1322
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001323#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001324/*
1325 * Initialize a restart context
1326 */
1327void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
1328{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001329 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001330 ctx->pk_info = NULL;
1331 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001332}
1333
1334/*
1335 * Free the components of a restart context
1336 */
1337void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
1338{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001339 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001340 ctx->pk_info->rs_free_func == NULL )
1341 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001342 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001343 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001344
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001345 ctx->pk_info->rs_free_func( ctx->rs_ctx );
1346
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001347 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001348 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001349}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001350#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001351
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001352/*
1353 * Get pk_info structure from type
1354 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001355mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001356{
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001357#if defined(MBEDTLS_PK_SINGLE_TYPE)
1358 if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) )
1359 return( MBEDTLS_PK_UNIQUE_VALID_HANDLE );
1360
1361 return( MBEDTLS_PK_INVALID_HANDLE );
1362
1363#else /* MBEDTLS_PK_SINGLE_TYPE */
1364
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001365 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_RSA_C)
1367 case MBEDTLS_PK_RSA:
1368 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001369#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 case MBEDTLS_PK_ECKEY_DH:
1372 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_ECDSA_C)
1375 case MBEDTLS_PK_ECDSA:
1376 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001377#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +01001378#if defined(MBEDTLS_USE_TINYCRYPT)
1379 case MBEDTLS_PK_ECKEY:
1380 return( &mbedtls_uecc_eckey_info );
1381#else /* MBEDTLS_USE_TINYCRYPT */
1382#if defined(MBEDTLS_ECP_C)
1383 case MBEDTLS_PK_ECKEY:
1384 return( &mbedtls_eckey_info );
1385#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +03001386#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001388 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001389 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001390 }
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001391#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001392}
1393
1394/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02001395 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001396 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001397int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001398{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001399 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001400 if( info == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001402
1403#if !defined(MBEDTLS_PK_SINGLE_TYPE)
1404 if( ctx->pk_info != NULL )
1405 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1406
1407 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001408
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001409 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001410 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001411#else
1412 (void) ctx;
1413#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001414
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001415 return( 0 );
1416}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +01001419/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001420 * Initialize an RSA-alt context
1421 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001422int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
1424 mbedtls_pk_rsa_alt_sign_func sign_func,
1425 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_rsa_alt_context *rsa_alt;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001428 mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001429
Gilles Peskinee97dc602018-12-19 00:51:38 +01001430 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001431 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001433
1434 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001435 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001436
1437 ctx->pk_info = info;
1438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001440
1441 rsa_alt->key = key;
1442 rsa_alt->decrypt_func = decrypt_func;
1443 rsa_alt->sign_func = sign_func;
1444 rsa_alt->key_len_func = key_len_func;
1445
1446 return( 0 );
1447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001449
1450/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001451 * Tell if a PK can do the operations of the given type
1452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001454{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001455 /* A context with null pk_info is not set up yet and can't do anything.
1456 * For backward compatibility, also accept NULL instead of a context
1457 * pointer. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001458 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001459 return( 0 );
1460
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001461 return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001462}
1463
1464/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001468{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001469 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001470
1471 if( *hash_len != 0 )
1472 return( 0 );
1473
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001474 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
1475 MBEDTLS_MD_INVALID_HANDLE )
1476 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001477 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001478 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001481 return( 0 );
1482}
1483
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001484#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001485/*
1486 * Helper to set up a restart context if needed
1487 */
1488static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001489 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001490{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001491 /* Don't do anything if already set up or invalid */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001492 if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001493 return( 0 );
1494
1495 /* Should never happen when we're called */
1496 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
1497 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1498
1499 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
1500 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1501
1502 ctx->pk_info = info;
1503
1504 return( 0 );
1505}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001506#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001507
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001508/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001509 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001510 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001511int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
1512 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001513 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001514 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001515 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001516{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001517 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001518 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1519 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001520 PK_VALIDATE_RET( sig != NULL );
1521
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001522 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001523 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001525
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001526#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001527 /* optimization: use non-restartable version if restart disabled */
1528 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001529 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001530 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001531 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001532 int ret;
1533
1534 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1535 return( ret );
1536
1537 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
1538 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
1539
1540 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1541 mbedtls_pk_restart_free( rs_ctx );
1542
1543 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001544 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001545#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001546 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001547#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001548
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001549 return( pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ),
1550 ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001551}
1552
1553/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001554 * Verify a signature
1555 */
1556int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1557 const unsigned char *hash, size_t hash_len,
1558 const unsigned char *sig, size_t sig_len )
1559{
1560 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
1561 sig, sig_len, NULL ) );
1562}
1563
1564/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001565 * Verify a signature with options
1566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
1568 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001569 const unsigned char *hash, size_t hash_len,
1570 const unsigned char *sig, size_t sig_len )
1571{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001572 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001573 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1574 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001575 PK_VALIDATE_RET( sig != NULL );
1576
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001577 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 if( ! mbedtls_pk_can_do( ctx, type ) )
1581 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001586 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001588
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001589#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001590 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
1591 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001592#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001593
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001594 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( sig_len < mbedtls_pk_get_len( ctx ) )
1600 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
1603 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001604 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001605 pss_opts->mgf1_hash_id,
1606 pss_opts->expected_salt_len,
1607 sig );
1608 if( ret != 0 )
1609 return( ret );
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 if( sig_len > mbedtls_pk_get_len( ctx ) )
1612 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001613
1614 return( 0 );
1615#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +00001617#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001618 }
1619
1620 /* General case: no options */
1621 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001625}
1626
1627/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001628 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001629 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001630int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
1631 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001632 const unsigned char *hash, size_t hash_len,
1633 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001634 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001635 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001636{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001637 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001638 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1639 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001640 PK_VALIDATE_RET( sig != NULL );
1641
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001642 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001643 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001645
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001646#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001647 /* optimization: use non-restartable version if restart disabled */
1648 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001649 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001650 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001651 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001652 int ret;
1653
1654 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1655 return( ret );
1656
1657 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
1658 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
1659
1660 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1661 mbedtls_pk_restart_free( rs_ctx );
1662
1663 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001664 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001665#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001666 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001667#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001668
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001669 return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1670 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001671}
1672
1673/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001674 * Make a signature
1675 */
1676int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1677 const unsigned char *hash, size_t hash_len,
1678 unsigned char *sig, size_t *sig_len,
1679 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1680{
1681 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
1682 sig, sig_len, f_rng, p_rng, NULL ) );
1683}
1684
1685/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001686 * Decrypt message
1687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001689 const unsigned char *input, size_t ilen,
1690 unsigned char *output, size_t *olen, size_t osize,
1691 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1692{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001693 PK_VALIDATE_RET( ctx != NULL );
1694 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1695 PK_VALIDATE_RET( output != NULL || osize == 0 );
1696 PK_VALIDATE_RET( olen != NULL );
1697
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001698 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001700
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001701 return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1702 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001703}
1704
1705/*
1706 * Encrypt message
1707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001709 const unsigned char *input, size_t ilen,
1710 unsigned char *output, size_t *olen, size_t osize,
1711 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1712{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001713 PK_VALIDATE_RET( ctx != NULL );
1714 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1715 PK_VALIDATE_RET( output != NULL || osize == 0 );
1716 PK_VALIDATE_RET( olen != NULL );
1717
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001718 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001720
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001721 return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1722 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001723}
1724
1725/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001726 * Check public-private key pair
1727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001729{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001730 PK_VALIDATE_RET( pub != NULL );
1731 PK_VALIDATE_RET( prv != NULL );
1732
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001733 if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001735
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001736#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001737 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001738 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001739 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001741 }
1742 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001743#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001744 {
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001745 if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001747 }
1748
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001749 return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ),
1750 pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001751}
1752
1753/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001754 * Get key size in bits
1755 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001756size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001757{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001758 /* For backward compatibility, accept NULL or a context that
1759 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001760 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001761 return( 0 );
1762
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001763 return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001764}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001765
1766/*
1767 * Export debug information
1768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001770{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001771 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001772 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001774
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001775 return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001776}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001777
1778/*
1779 * Access the PK type name
1780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001782{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001783 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001784 return( "invalid PK" );
1785
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001786 return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001787}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001788
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001789/*
1790 * Access the PK type
1791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001793{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001794 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001796
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001797 return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001798}
1799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800#endif /* MBEDTLS_PK_C */