blob: df159e0125d5b904bb2b2e7474dab044829d2f2e [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 );
515 memcpy( to + padding_len, *from, unpadded_len );
516 ( *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
559 if( memcmp( uecc_pub->public_key,
560 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;
580 uint8_t signature[2*NUM_ECC_BYTES];
581 unsigned char *p;
582 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
583 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
592 ret = uECC_verify( keypair->public_key, hash,
593 (unsigned) hash_len, signature, uecc_curve );
594 if( ret == 0 )
595 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
596
597 return( 0 );
598}
599
600/*
601 * Simultaneously convert and move raw MPI from the beginning of a buffer
602 * to an ASN.1 MPI at the end of the buffer.
603 * See also mbedtls_asn1_write_mpi().
604 *
605 * p: pointer to the end of the output buffer
606 * start: start of the output buffer, and also of the mpi to write at the end
607 * n_len: length of the mpi to read from start
608 *
609 * Warning:
610 * The total length of the output buffer must be smaller than 128 Bytes.
611 */
612static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
613 size_t n_len )
614{
615 size_t len = 0;
616
617 if( (size_t)( *p - start ) < n_len )
618 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
619
620 len = n_len;
621 *p -= len;
622 memmove( *p, start, len );
623
624 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
625 * Neither r nor s should be 0, but as a failsafe measure, still detect
626 * that rather than overflowing the buffer in case of an error. */
627 while( len > 0 && **p == 0x00 )
628 {
629 ++(*p);
630 --len;
631 }
632
633 /* this is only reached if the signature was invalid */
634 if( len == 0 )
635 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
636
637 /* if the msb is 1, ASN.1 requires that we prepend a 0.
638 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
639 if( **p & 0x80 )
640 {
641 if( *p - start < 1 )
642 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
643
644 *--(*p) = 0x00;
645 len += 1;
646 }
647
648 /* The ASN.1 length encoding is just a single Byte containing the length,
649 * as we assume that the total buffer length is smaller than 128 Bytes. */
650 *--(*p) = len;
651 *--(*p) = MBEDTLS_ASN1_INTEGER;
652 len += 2;
653
654 return( (int) len );
655}
656
657/* Transcode signature from uECC format to ASN.1 sequence.
658 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
659 * MPIs, and in-place.
660 *
661 * [in/out] sig: the signature pre- and post-transcoding
662 * [in/out] sig_len: signature length pre- and post-transcoding
663 * [int] buf_len: the available size the in/out buffer
664 *
665 * Warning: buf_len must be smaller than 128 Bytes.
666 */
667static int pk_ecdsa_sig_asn1_from_uecc( unsigned char *sig, size_t *sig_len,
668 size_t buf_len )
669{
670 int ret;
671 size_t len = 0;
672 const size_t rs_len = *sig_len / 2;
673 unsigned char *p = sig + buf_len;
674
675 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
676 MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
677
678 /* The ASN.1 length encoding is just a single Byte containing the length,
679 * as we assume that the total buffer length is smaller than 128 Bytes. */
680 *--p = len;
681 *--p = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
682 len += 2;
683
684 memmove( sig, p, len );
685 *sig_len = len;
686
687 return( 0 );
688}
689
690static int uecc_eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
691 const unsigned char *hash, size_t hash_len,
692 unsigned char *sig, size_t *sig_len,
693 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
694{
695 const mbedtls_uecc_keypair *keypair = (const mbedtls_uecc_keypair *) ctx;
696 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
697 int ret;
698
699 /*
700 * RFC-4492 page 20:
701 *
702 * Ecdsa-Sig-Value ::= SEQUENCE {
703 * r INTEGER,
704 * s INTEGER
705 * }
706 *
707 * Size is at most
708 * 1 (tag) + 1 (len) + 1 (initial 0) + NUM_ECC_BYTES for each of r and s,
709 * twice that + 1 (tag) + 2 (len) for the sequence
710 *
711 * (The ASN.1 length encodings are all 1-Byte encodings because
712 * the total size is smaller than 128 Bytes).
713 */
714 #define MAX_SECP256R1_ECDSA_SIG_LEN ( 3 + 2 * ( 3 + NUM_ECC_BYTES ) )
715
716 ret = uECC_sign( keypair->private_key, hash, hash_len, sig, uecc_curve );
717 /* TinyCrypt uses 0 to signal errors. */
718 if( ret == 0 )
719 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
720
721 *sig_len = 2 * NUM_ECC_BYTES;
722
723 /* uECC owns its rng function pointer */
724 (void) f_rng;
725 (void) p_rng;
726 (void) md_alg;
727
728 return( pk_ecdsa_sig_asn1_from_uecc( sig, sig_len,
729 MAX_SECP256R1_ECDSA_SIG_LEN ) );
730
731 #undef MAX_SECP256R1_ECDSA_SIG_LEN
732}
733
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200734#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200735static void *uecc_eckey_alloc_wrap( void )
736{
737 return( mbedtls_calloc( 1, sizeof( mbedtls_uecc_keypair ) ) );
738}
739
740static void uecc_eckey_free_wrap( void *ctx )
741{
742 if( ctx == NULL )
743 return;
744
745 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_uecc_keypair ) );
746 mbedtls_free( ctx );
747}
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +0200748#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200749
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200750#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200751const mbedtls_pk_info_t mbedtls_uecc_eckey_info =
752 MBEDTLS_PK_INFO( MBEDTLS_PK_INFO_ECKEY );
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +0200753#endif
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200754#endif /* MBEDTLS_USE_TINYCRYPT */
755
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200756/*
757 * Internal wrappers around ECDSA functions
758 */
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200759#if defined(MBEDTLS_ECDSA_C)
760static int ecdsa_can_do( mbedtls_pk_type_t type )
761{
762 return( type == MBEDTLS_PK_ECDSA );
763}
764
765static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
766 const unsigned char *hash, size_t hash_len,
767 const unsigned char *sig, size_t sig_len )
768{
769 int ret;
770 ((void) md_alg);
771
772 ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
773 hash, hash_len, sig, sig_len );
774
775 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
776 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
777
778 return( ret );
779}
780
781static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
782 const unsigned char *hash, size_t hash_len,
783 unsigned char *sig, size_t *sig_len,
784 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
785{
786 return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
787 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
788}
789
790#if defined(MBEDTLS_ECP_RESTARTABLE)
791static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
792 const unsigned char *hash, size_t hash_len,
793 const unsigned char *sig, size_t sig_len,
794 void *rs_ctx )
795{
796 int ret;
797 ((void) md_alg);
798
799 ret = mbedtls_ecdsa_read_signature_restartable(
800 (mbedtls_ecdsa_context *) ctx,
801 hash, hash_len, sig, sig_len,
802 (mbedtls_ecdsa_restart_ctx *) rs_ctx );
803
804 if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
805 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
806
807 return( ret );
808}
809
810static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
811 const unsigned char *hash, size_t hash_len,
812 unsigned char *sig, size_t *sig_len,
813 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
814 void *rs_ctx )
815{
816 return( mbedtls_ecdsa_write_signature_restartable(
817 (mbedtls_ecdsa_context *) ctx,
818 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
819 (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
820
821}
822#endif /* MBEDTLS_ECP_RESTARTABLE */
823
824static void *ecdsa_alloc_wrap( void )
825{
826 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
827
828 if( ctx != NULL )
829 mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
830
831 return( ctx );
832}
833
834static void ecdsa_free_wrap( void *ctx )
835{
836 mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
837 mbedtls_free( ctx );
838}
839
840#if defined(MBEDTLS_ECP_RESTARTABLE)
841static void *ecdsa_rs_alloc( void )
842{
843 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
844
845 if( ctx != NULL )
846 mbedtls_ecdsa_restart_init( ctx );
847
848 return( ctx );
849}
850
851static void ecdsa_rs_free( void *ctx )
852{
853 mbedtls_ecdsa_restart_free( ctx );
854 mbedtls_free( ctx );
855}
856#endif /* MBEDTLS_ECP_RESTARTABLE */
857
858const mbedtls_pk_info_t mbedtls_ecdsa_info = {
859 MBEDTLS_PK_ECDSA,
860 "ECDSA",
861 eckey_get_bitlen, /* Compatible key structures */
862 ecdsa_can_do,
863 ecdsa_verify_wrap,
864 ecdsa_sign_wrap,
865#if defined(MBEDTLS_ECP_RESTARTABLE)
866 ecdsa_verify_rs_wrap,
867 ecdsa_sign_rs_wrap,
868#endif
869 NULL,
870 NULL,
871 eckey_check_pair, /* Compatible key structures */
872 ecdsa_alloc_wrap,
873 ecdsa_free_wrap,
874#if defined(MBEDTLS_ECP_RESTARTABLE)
875 ecdsa_rs_alloc,
876 ecdsa_rs_free,
877#endif
878 eckey_debug, /* Compatible key structures */
879};
880#endif /* MBEDTLS_ECDSA_C */
881
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200882/*
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200883 * Internal wrappers for RSA-alt support
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200884 */
Manuel Pégourié-Gonnard8cd28892019-09-19 10:45:14 +0200885#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +0200886static int rsa_alt_can_do( mbedtls_pk_type_t type )
887{
888 return( type == MBEDTLS_PK_RSA );
889}
890
891static size_t rsa_alt_get_bitlen( const void *ctx )
892{
893 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
894
895 return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
896}
897
898static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
899 const unsigned char *hash, size_t hash_len,
900 unsigned char *sig, size_t *sig_len,
901 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
902{
903 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
904
905#if SIZE_MAX > UINT_MAX
906 if( UINT_MAX < hash_len )
907 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
908#endif /* SIZE_MAX > UINT_MAX */
909
910 *sig_len = rsa_alt->key_len_func( rsa_alt->key );
911
912 return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
913 md_alg, (unsigned int) hash_len, hash, sig ) );
914}
915
916static int rsa_alt_decrypt_wrap( void *ctx,
917 const unsigned char *input, size_t ilen,
918 unsigned char *output, size_t *olen, size_t osize,
919 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
920{
921 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
922
923 ((void) f_rng);
924 ((void) p_rng);
925
926 if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
927 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
928
929 return( rsa_alt->decrypt_func( rsa_alt->key,
930 MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
931}
932
933#if defined(MBEDTLS_RSA_C)
934static int rsa_alt_check_pair( const void *pub, const void *prv )
935{
936 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
937 unsigned char hash[32];
938 size_t sig_len = 0;
939 int ret;
940
941 if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
942 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
943
944 memset( hash, 0x2a, sizeof( hash ) );
945
946 if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
947 hash, sizeof( hash ),
948 sig, &sig_len, NULL, NULL ) ) != 0 )
949 {
950 return( ret );
951 }
952
953 if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
954 hash, sizeof( hash ), sig, sig_len ) != 0 )
955 {
956 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
957 }
958
959 return( 0 );
960}
961#endif /* MBEDTLS_RSA_C */
962
963static void *rsa_alt_alloc_wrap( void )
964{
965 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
966
967 if( ctx != NULL )
968 memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
969
970 return( ctx );
971}
972
973static void rsa_alt_free_wrap( void *ctx )
974{
975 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
976 mbedtls_free( ctx );
977}
978
979const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
980 MBEDTLS_PK_RSA_ALT,
981 "RSA-alt",
982 rsa_alt_get_bitlen,
983 rsa_alt_can_do,
984 NULL,
985 rsa_alt_sign_wrap,
986#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
987 NULL,
988 NULL,
989#endif
990 rsa_alt_decrypt_wrap,
991 NULL,
992#if defined(MBEDTLS_RSA_C)
993 rsa_alt_check_pair,
994#else
995 NULL,
996#endif
997 rsa_alt_alloc_wrap,
998 rsa_alt_free_wrap,
999#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1000 NULL,
1001 NULL,
1002#endif
1003 NULL,
1004};
Manuel Pégourié-Gonnard4ed179f2019-09-19 10:45:14 +02001005#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
1006
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001007/*
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001008 * Access to members of the pk_info structure. When a single PK type is
1009 * hardcoded, these should have zero runtime cost; otherwise, the usual
1010 * dynamic dispatch based on pk_info is used.
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001011 *
1012 * For function members, don't make a getter, but a function that directly
1013 * calls the method, so that we can entirely get rid of function pointers
1014 * when hardcoding a single PK - some compilers optimize better that way.
1015 *
1016 * Not implemented for members that are only present in builds with
1017 * MBEDTLS_ECP_RESTARTABLE for now, as the main target for hardcoded is builds
1018 * with MBEDTLS_USE_TINYCRYPT, which don't have MBEDTLS_ECP_RESTARTABLE.
1019 */
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001020#if defined(MBEDTLS_PK_SINGLE_TYPE)
1021
1022MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001023 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001024{
1025 (void) info;
1026 return( MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) );
1027}
1028
1029MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001030 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001031{
1032 (void) info;
1033 return( MBEDTLS_PK_INFO_NAME( MBEDTLS_PK_SINGLE_TYPE ) );
1034}
1035
1036MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001037 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001038{
1039 (void) info;
1040 return( MBEDTLS_PK_INFO_GET_BITLEN( MBEDTLS_PK_SINGLE_TYPE )( ctx ) );
1041}
1042
1043MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001044 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001045{
1046 (void) info;
1047 return( MBEDTLS_PK_INFO_CAN_DO( MBEDTLS_PK_SINGLE_TYPE )( type ) );
1048}
1049
1050MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001051 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001052 const unsigned char *hash, size_t hash_len,
1053 const unsigned char *sig, size_t sig_len )
1054{
1055 (void) info;
1056#if MBEDTLS_PK_INFO_VERIFY_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1057 (void) ctx;
1058 (void) md_alg;
1059 (void) hash;
1060 (void) hash_len;
1061 (void) sig;
1062 (void) sig_len;
1063 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1064#else
1065 return( MBEDTLS_PK_INFO_VERIFY_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1066 ctx, md_alg, hash, hash_len, sig, sig_len ) );
1067#endif
1068}
1069
1070MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001071 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001072 const unsigned char *hash, size_t hash_len,
1073 unsigned char *sig, size_t *sig_len,
1074 int (*f_rng)(void *, unsigned char *, size_t),
1075 void *p_rng )
1076{
1077 (void) info;
1078#if MBEDTLS_PK_INFO_SIGN_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1079 (void) ctx;
1080 (void) md_alg;
1081 (void) hash;
1082 (void) hash_len;
1083 (void) sig;
1084 (void) sig_len;
1085 (void) f_rng;
1086 (void) p_rng;
1087 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1088#else
1089 return( MBEDTLS_PK_INFO_SIGN_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1090 ctx, md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
1091#endif
1092}
1093
1094MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001095 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001096 const unsigned char *input, size_t ilen,
1097 unsigned char *output, size_t *olen, size_t osize,
1098 int (*f_rng)(void *, unsigned char *, size_t),
1099 void *p_rng )
1100{
1101 (void) info;
1102#if MBEDTLS_PK_INFO_DECRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1103 (void) ctx;
1104 (void) input;
1105 (void) ilen;
1106 (void) output;
1107 (void) olen;
1108 (void) osize;
1109 (void) f_rng;
1110 (void) p_rng;
1111 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1112#else
1113 return( MBEDTLS_PK_INFO_DECRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1114 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1115#endif
1116}
1117
1118MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001119 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001120 const unsigned char *input, size_t ilen,
1121 unsigned char *output, size_t *olen, size_t osize,
1122 int (*f_rng)(void *, unsigned char *, size_t),
1123 void *p_rng )
1124{
1125 (void) info;
1126#if MBEDTLS_PK_INFO_ENCRYPT_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1127 (void) ctx;
1128 (void) input;
1129 (void) ilen;
1130 (void) output;
1131 (void) olen;
1132 (void) osize;
1133 (void) f_rng;
1134 (void) p_rng;
1135 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1136#else
1137 return( MBEDTLS_PK_INFO_ENCRYPT_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1138 ctx, input, ilen, output, olen, osize, f_rng, p_rng ) );
1139#endif
1140}
1141
1142MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001143 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001144{
1145 (void) info;
1146#if MBEDTLS_PK_INFO_CHECK_PAIR_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1147 (void) pub;
1148 (void) prv;
1149 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1150#else
1151 return( MBEDTLS_PK_INFO_CHECK_PAIR_FUNC( MBEDTLS_PK_SINGLE_TYPE )(
1152 pub, prv ) );
1153#endif
1154}
1155
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001156MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001157 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001158 const void *ctx, mbedtls_pk_debug_item *items )
1159{
1160 (void) info;
1161#if MBEDTLS_PK_INFO_DEBUG_OMIT( MBEDTLS_PK_SINGLE_TYPE )
1162 (void) ctx;
1163 (void) items;
1164 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1165#else
1166 return( MBEDTLS_PK_INFO_DEBUG_FUNC( MBEDTLS_PK_SINGLE_TYPE )( ctx, items ) );
1167#endif
1168}
1169
1170#else /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001171
1172MBEDTLS_ALWAYS_INLINE static inline mbedtls_pk_type_t pk_info_type(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001173 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001174{
1175 return( info->type );
1176}
1177
1178MBEDTLS_ALWAYS_INLINE static inline const char * pk_info_name(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001179 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001180{
1181 return( info->name );
1182}
1183
1184MBEDTLS_ALWAYS_INLINE static inline size_t pk_info_get_bitlen(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001185 mbedtls_pk_handle_t info, const void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001186{
1187 return( info->get_bitlen( ctx ) );
1188}
1189
1190MBEDTLS_ALWAYS_INLINE static inline int pk_info_can_do(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001191 mbedtls_pk_handle_t info, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001192{
1193 return( info->can_do( type ) );
1194}
1195
1196MBEDTLS_ALWAYS_INLINE static inline int pk_info_verify_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001197 mbedtls_pk_handle_t info, void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001198 const unsigned char *hash, size_t hash_len,
1199 const unsigned char *sig, size_t sig_len )
1200{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001201 if( info->verify_func == NULL )
1202 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1203
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001204 return( info->verify_func( ctx, md_alg, hash, hash_len, sig, sig_len ) );
1205}
1206
1207MBEDTLS_ALWAYS_INLINE static inline int pk_info_sign_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 unsigned char *sig, size_t *sig_len,
1211 int (*f_rng)(void *, unsigned char *, size_t),
1212 void *p_rng )
1213{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001214 if( info->sign_func == NULL )
1215 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1216
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001217 return( info->sign_func( ctx, md_alg, hash, hash_len, sig, sig_len,
1218 f_rng, p_rng ) );
1219}
1220
1221MBEDTLS_ALWAYS_INLINE static inline int pk_info_decrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001222 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001223 const unsigned char *input, size_t ilen,
1224 unsigned char *output, size_t *olen, size_t osize,
1225 int (*f_rng)(void *, unsigned char *, size_t),
1226 void *p_rng )
1227{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001228 if( info->decrypt_func == NULL )
1229 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1230
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001231 return( info->decrypt_func( ctx, input, ilen, output, olen, osize,
1232 f_rng, p_rng ) );
1233}
1234
1235MBEDTLS_ALWAYS_INLINE static inline int pk_info_encrypt_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001236 mbedtls_pk_handle_t info, void *ctx,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001237 const unsigned char *input, size_t ilen,
1238 unsigned char *output, size_t *olen, size_t osize,
1239 int (*f_rng)(void *, unsigned char *, size_t),
1240 void *p_rng )
1241{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001242 if( info->encrypt_func == NULL )
1243 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1244
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001245 return( info->encrypt_func( ctx, input, ilen, output, olen, osize,
1246 f_rng, p_rng ) );
1247}
1248
1249MBEDTLS_ALWAYS_INLINE static inline int pk_info_check_pair_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001250 mbedtls_pk_handle_t info, const void *pub, const void *prv )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001251{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001252 if( info->check_pair_func == NULL )
1253 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1254
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001255 return( info->check_pair_func( pub, prv ) );
1256}
1257
1258MBEDTLS_ALWAYS_INLINE static inline void *pk_info_ctx_alloc_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001259 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001260{
1261 return( info->ctx_alloc_func( ) );
1262}
1263
1264MBEDTLS_ALWAYS_INLINE static inline void pk_info_ctx_free_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001265 mbedtls_pk_handle_t info, void *ctx )
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001266{
1267 info->ctx_free_func( ctx );
1268}
1269
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001270MBEDTLS_ALWAYS_INLINE static inline int pk_info_debug_func(
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001271 mbedtls_pk_handle_t info,
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001272 const void *ctx, mbedtls_pk_debug_item *items )
1273{
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001274 if( info->debug_func == NULL )
1275 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
1276
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001277 info->debug_func( ctx, items );
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +02001278 return( 0 );
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001279}
1280
Manuel Pégourié-Gonnard08620cb2019-09-19 10:45:14 +02001281#endif /* MBEDTLS_PK_SINGLE_TYPE */
1282
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001283/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286void mbedtls_pk_init( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001287{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001288 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001289
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001290#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001291 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001292 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001293#else
1294 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
1295#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001296}
1297
1298/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301void mbedtls_pk_free( mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001302{
irwir2239a862018-06-12 18:25:09 +03001303 if( ctx == NULL )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001304 return;
1305
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001306#if !defined(MBEDTLS_PK_SINGLE_TYPE)
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001307 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
1308 pk_info_ctx_free_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001309#endif
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +02001310
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001311 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001312}
1313
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001314#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001315/*
1316 * Initialize a restart context
1317 */
1318void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx )
1319{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001320 PK_VALIDATE( ctx != NULL );
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001321 ctx->pk_info = NULL;
1322 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001323}
1324
1325/*
1326 * Free the components of a restart context
1327 */
1328void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx )
1329{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001330 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001331 ctx->pk_info->rs_free_func == NULL )
1332 {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001333 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001334 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001335
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001336 ctx->pk_info->rs_free_func( ctx->rs_ctx );
1337
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001338 ctx->pk_info = MBEDTLS_PK_INVALID_HANDLE;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001339 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001340}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001341#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001342
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001343/*
1344 * Get pk_info structure from type
1345 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001346mbedtls_pk_handle_t mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001347{
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001348#if defined(MBEDTLS_PK_SINGLE_TYPE)
1349 if( pk_type == MBEDTLS_PK_INFO_TYPE( MBEDTLS_PK_SINGLE_TYPE ) )
1350 return( MBEDTLS_PK_UNIQUE_VALID_HANDLE );
1351
1352 return( MBEDTLS_PK_INVALID_HANDLE );
1353
1354#else /* MBEDTLS_PK_SINGLE_TYPE */
1355
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001356 switch( pk_type ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_RSA_C)
1358 case MBEDTLS_PK_RSA:
1359 return( &mbedtls_rsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001360#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 case MBEDTLS_PK_ECKEY_DH:
1363 return( &mbedtls_eckeydh_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_ECDSA_C)
1366 case MBEDTLS_PK_ECDSA:
1367 return( &mbedtls_ecdsa_info );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001368#endif
Hanno Beckeradf11e12019-08-21 13:03:44 +01001369#if defined(MBEDTLS_USE_TINYCRYPT)
1370 case MBEDTLS_PK_ECKEY:
1371 return( &mbedtls_uecc_eckey_info );
1372#else /* MBEDTLS_USE_TINYCRYPT */
1373#if defined(MBEDTLS_ECP_C)
1374 case MBEDTLS_PK_ECKEY:
1375 return( &mbedtls_eckey_info );
1376#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +03001377#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001379 default:
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001380 return( NULL );
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +02001381 }
Manuel Pégourié-Gonnardf8b7c7f2019-09-19 10:45:14 +02001382#endif /* MBEDTLS_PK_SINGLE_TYPE */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001383}
1384
1385/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +02001386 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001387 */
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001388int mbedtls_pk_setup( mbedtls_pk_context *ctx, mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001389{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001390 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001391 if( info == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard073c1e12019-09-19 10:45:14 +02001393
1394#if !defined(MBEDTLS_PK_SINGLE_TYPE)
1395 if( ctx->pk_info != NULL )
1396 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1397
1398 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001399
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001400 if( ( ctx->pk_ctx = pk_info_ctx_alloc_func( info ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001401 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnarda77e9b52019-09-19 10:45:14 +02001402#else
1403 (void) ctx;
1404#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001405
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001406 return( 0 );
1407}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +01001410/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001411 * Initialize an RSA-alt context
1412 */
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001413int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
1415 mbedtls_pk_rsa_alt_sign_func sign_func,
1416 mbedtls_pk_rsa_alt_key_len_func key_len_func )
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_rsa_alt_context *rsa_alt;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001419 mbedtls_pk_handle_t info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001420
Gilles Peskinee97dc602018-12-19 00:51:38 +01001421 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001422 if( MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001424
1425 if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001426 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001427
1428 ctx->pk_info = info;
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001431
1432 rsa_alt->key = key;
1433 rsa_alt->decrypt_func = decrypt_func;
1434 rsa_alt->sign_func = sign_func;
1435 rsa_alt->key_len_func = key_len_func;
1436
1437 return( 0 );
1438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001440
1441/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001442 * Tell if a PK can do the operations of the given type
1443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001445{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001446 /* A context with null pk_info is not set up yet and can't do anything.
1447 * For backward compatibility, also accept NULL instead of a context
1448 * pointer. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001449 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001450 return( 0 );
1451
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001452 return( pk_info_can_do( MBEDTLS_PK_CTX_INFO( ctx ), type ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001453}
1454
1455/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001459{
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001460 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001461
1462 if( *hash_len != 0 )
1463 return( 0 );
1464
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001465 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) ==
1466 MBEDTLS_MD_INVALID_HANDLE )
1467 {
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001468 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001469 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 *hash_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001472 return( 0 );
1473}
1474
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001475#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001476/*
1477 * Helper to set up a restart context if needed
1478 */
1479static int pk_restart_setup( mbedtls_pk_restart_ctx *ctx,
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001480 mbedtls_pk_handle_t info )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001481{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001482 /* Don't do anything if already set up or invalid */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001483 if( ctx == NULL || MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001484 return( 0 );
1485
1486 /* Should never happen when we're called */
1487 if( info->rs_alloc_func == NULL || info->rs_free_func == NULL )
1488 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
1489
1490 if( ( ctx->rs_ctx = info->rs_alloc_func() ) == NULL )
1491 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1492
1493 ctx->pk_info = info;
1494
1495 return( 0 );
1496}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001497#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001498
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001499/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001500 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001501 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001502int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
1503 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001504 const unsigned char *hash, size_t hash_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001505 const unsigned char *sig, size_t sig_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001506 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001507{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001508 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001509 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1510 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001511 PK_VALIDATE_RET( sig != NULL );
1512
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001513 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001514 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001516
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001517#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001518 /* optimization: use non-restartable version if restart disabled */
1519 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001520 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001521 ctx->pk_info->verify_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001522 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001523 int ret;
1524
1525 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1526 return( ret );
1527
1528 ret = ctx->pk_info->verify_rs_func( ctx->pk_ctx,
1529 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx );
1530
1531 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1532 mbedtls_pk_restart_free( rs_ctx );
1533
1534 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001535 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001536#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001537 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001538#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001539
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001540 return( pk_info_verify_func( MBEDTLS_PK_CTX_INFO( ctx ),
1541 ctx->pk_ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001542}
1543
1544/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001545 * Verify a signature
1546 */
1547int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1548 const unsigned char *hash, size_t hash_len,
1549 const unsigned char *sig, size_t sig_len )
1550{
1551 return( mbedtls_pk_verify_restartable( ctx, md_alg, hash, hash_len,
1552 sig, sig_len, NULL ) );
1553}
1554
1555/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001556 * Verify a signature with options
1557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
1559 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001560 const unsigned char *hash, size_t hash_len,
1561 const unsigned char *sig, size_t sig_len )
1562{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001563 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001564 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1565 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001566 PK_VALIDATE_RET( sig != NULL );
1567
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001568 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( ! mbedtls_pk_can_do( ctx, type ) )
1572 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( type == MBEDTLS_PK_RSASSA_PSS )
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001577 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001579
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001580#if SIZE_MAX > UINT_MAX
Andres AG72849872017-01-19 11:24:33 +00001581 if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
1582 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001583#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001584
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001585 if( options == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( sig_len < mbedtls_pk_get_len( ctx ) )
1591 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
1594 NULL, NULL, MBEDTLS_RSA_PUBLIC,
Sander Niemeijeref5087d2014-08-16 12:45:52 +02001595 md_alg, (unsigned int) hash_len, hash,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001596 pss_opts->mgf1_hash_id,
1597 pss_opts->expected_salt_len,
1598 sig );
1599 if( ret != 0 )
1600 return( ret );
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( sig_len > mbedtls_pk_get_len( ctx ) )
1603 return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001604
1605 return( 0 );
1606#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Andres AG72849872017-01-19 11:24:33 +00001608#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001609 }
1610
1611 /* General case: no options */
1612 if( options != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001616}
1617
1618/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001619 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001620 */
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001621int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
1622 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001623 const unsigned char *hash, size_t hash_len,
1624 unsigned char *sig, size_t *sig_len,
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001625 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02001626 mbedtls_pk_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001627{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001628 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskineee3cfec2018-12-19 17:10:02 +01001629 PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) ||
1630 hash != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001631 PK_VALIDATE_RET( sig != NULL );
1632
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001633 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) ||
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001634 pk_hashlen_helper( md_alg, &hash_len ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001636
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001637#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001638 /* optimization: use non-restartable version if restart disabled */
1639 if( rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001640 mbedtls_ecp_restart_is_enabled() &&
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001641 ctx->pk_info->sign_rs_func != NULL )
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001642 {
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001643 int ret;
1644
1645 if( ( ret = pk_restart_setup( rs_ctx, ctx->pk_info ) ) != 0 )
1646 return( ret );
1647
1648 ret = ctx->pk_info->sign_rs_func( ctx->pk_ctx, md_alg,
1649 hash, hash_len, sig, sig_len, f_rng, p_rng, rs_ctx->rs_ctx );
1650
1651 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1652 mbedtls_pk_restart_free( rs_ctx );
1653
1654 return( ret );
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001655 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001656#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001657 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001658#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001659
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001660 return( pk_info_sign_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1661 md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001662}
1663
1664/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001665 * Make a signature
1666 */
1667int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1668 const unsigned char *hash, size_t hash_len,
1669 unsigned char *sig, size_t *sig_len,
1670 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1671{
1672 return( mbedtls_pk_sign_restartable( ctx, md_alg, hash, hash_len,
1673 sig, sig_len, f_rng, p_rng, NULL ) );
1674}
1675
1676/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001677 * Decrypt message
1678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001680 const unsigned char *input, size_t ilen,
1681 unsigned char *output, size_t *olen, size_t osize,
1682 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1683{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001684 PK_VALIDATE_RET( ctx != NULL );
1685 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1686 PK_VALIDATE_RET( output != NULL || osize == 0 );
1687 PK_VALIDATE_RET( olen != NULL );
1688
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001689 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001691
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001692 return( pk_info_decrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1693 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001694}
1695
1696/*
1697 * Encrypt message
1698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001700 const unsigned char *input, size_t ilen,
1701 unsigned char *output, size_t *olen, size_t osize,
1702 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1703{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001704 PK_VALIDATE_RET( ctx != NULL );
1705 PK_VALIDATE_RET( input != NULL || ilen == 0 );
1706 PK_VALIDATE_RET( output != NULL || osize == 0 );
1707 PK_VALIDATE_RET( olen != NULL );
1708
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001709 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001711
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001712 return( pk_info_encrypt_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx,
1713 input, ilen, output, olen, osize, f_rng, p_rng ) );
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001714}
1715
1716/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001717 * Check public-private key pair
1718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001720{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001721 PK_VALIDATE_RET( pub != NULL );
1722 PK_VALIDATE_RET( prv != NULL );
1723
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001724 if( !MBEDTLS_PK_CTX_IS_VALID( pub ) || !MBEDTLS_PK_CTX_IS_VALID( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001726
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001727#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001728 if( pk_info_type( prv->pk_info ) == MBEDTLS_PK_RSA_ALT )
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001729 {
Manuel Pégourié-Gonnardc10f0922019-09-19 10:45:14 +02001730 if( pk_info_type( pub->pk_info ) != MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001732 }
1733 else
Manuel Pégourié-Gonnard2d9466f2019-09-19 10:45:14 +02001734#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001735 {
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001736 if( MBEDTLS_PK_CTX_INFO( pub ) != MBEDTLS_PK_CTX_INFO( prv ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001738 }
1739
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001740 return( pk_info_check_pair_func( MBEDTLS_PK_CTX_INFO( prv ),
1741 pub->pk_ctx, prv->pk_ctx ) );
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001742}
1743
1744/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001745 * Get key size in bits
1746 */
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001747size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001748{
Gilles Peskine6af45ec2018-12-19 17:52:05 +01001749 /* For backward compatibility, accept NULL or a context that
1750 * isn't set up yet, and return a fake value that should be safe. */
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001751 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001752 return( 0 );
1753
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001754 return( pk_info_get_bitlen( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx ) );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001755}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001756
1757/*
1758 * Export debug information
1759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001761{
Gilles Peskinee97dc602018-12-19 00:51:38 +01001762 PK_VALIDATE_RET( ctx != NULL );
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001763 if( !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001765
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001766 return( pk_info_debug_func( MBEDTLS_PK_CTX_INFO( ctx ), ctx->pk_ctx, items ) );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001767}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001768
1769/*
1770 * Access the PK type name
1771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001773{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001774 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001775 return( "invalid PK" );
1776
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001777 return( pk_info_name( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001778}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001779
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001780/*
1781 * Access the PK type
1782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001784{
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001785 if( ctx == NULL || !MBEDTLS_PK_CTX_IS_VALID( ctx ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001787
Manuel Pégourié-Gonnard4223ce42019-09-19 10:45:14 +02001788 return( pk_info_type( MBEDTLS_PK_CTX_INFO( ctx ) ) );
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001789}
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#endif /* MBEDTLS_PK_C */