blob: 9e8035045e5f355cc6ec0cf32e18b42a118cc748 [file] [log] [blame]
Philippe Antoine72333522018-05-03 16:40:24 +02001#include <stdint.h>
Philippe Antoine8b1ed1c2020-01-22 16:22:36 +01002#include <stdlib.h>
Philippe Antoine72333522018-05-03 16:40:24 +02003#include "mbedtls/pk.h"
4
5int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
6#ifdef MBEDTLS_PK_PARSE_C
7 int ret;
8 mbedtls_pk_context pk;
9
10 mbedtls_pk_init( &pk );
11 ret = mbedtls_pk_parse_public_key( &pk, Data, Size );
12 if (ret == 0) {
13#if defined(MBEDTLS_RSA_C)
14 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
15 {
16 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
17 mbedtls_rsa_context *rsa;
18
19 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
20 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
21 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
22
23 rsa = mbedtls_pk_rsa( pk );
Gilles Peskine8d366962020-02-25 19:51:07 +010024 if ( mbedtls_rsa_export( rsa, &N, NULL, NULL, NULL, &E ) != 0 ) {
25 abort();
26 }
27 if ( mbedtls_rsa_export( rsa, &N, &P, &Q, &D, &E ) != MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) {
Philippe Antoine66070bc2020-01-22 13:54:56 +010028 abort();
29 }
Philippe Antoine7d4bd6f2020-01-22 14:13:08 +010030 if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) {
31 abort();
32 }
Philippe Antoine72333522018-05-03 16:40:24 +020033
34 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
35 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
36 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
37
38 }
39 else
40#endif
41#if defined(MBEDTLS_ECP_C)
Gilles Peskinee60b3652020-02-25 19:54:07 +010042 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
43 mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
Philippe Antoine72333522018-05-03 16:40:24 +020044 {
Gilles Peskinef02b9842020-02-25 19:52:44 +010045 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
46 mbedtls_ecp_group_id grp_id = ecp->grp.id;
47 const mbedtls_ecp_curve_info *curve_info =
48 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Philippe Antoine72333522018-05-03 16:40:24 +020049
Gilles Peskinef02b9842020-02-25 19:52:44 +010050 /* If the curve is not supported, the key should not have been
51 * accepted. */
52 if( curve_info == NULL )
53 abort( );
54
55 /* It's a public key, so the private value should not have
56 * been changed from its initialization to 0. */
57 if( mbedtls_mpi_cmp_int( &ecp->d, 0 ) != 0 )
58 abort( );
Philippe Antoine72333522018-05-03 16:40:24 +020059 }
60 else
61#endif
62 {
Gilles Peskined7fb66f2020-02-25 19:54:27 +010063 /* The key is valid but is not of a supported type.
64 * This should not happen. */
65 abort( );
Philippe Antoine72333522018-05-03 16:40:24 +020066 }
67 }
68 mbedtls_pk_free( &pk );
69#else
70 (void) Data;
71 (void) Size;
72#endif //MBEDTLS_PK_PARSE_C
73
74 return 0;
75}