blob: 6c968fd54cb09fa25b49bcf3cb24eb9917cfd065 [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
5//4 Kb should be enough for every bug ;-)
6#define MAX_LEN 0x1000
7
8
9int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
10#ifdef MBEDTLS_PK_PARSE_C
11 int ret;
12 mbedtls_pk_context pk;
13
14 if (Size > MAX_LEN) {
15 //only work on small inputs
16 Size = MAX_LEN;
17 }
18
19 mbedtls_pk_init( &pk );
20 ret = mbedtls_pk_parse_key( &pk, Data, Size, NULL, 0 );
21 if (ret == 0) {
22#if defined(MBEDTLS_RSA_C)
23 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
24 {
25 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
26 mbedtls_rsa_context *rsa;
27
28 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
29 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
30 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
31
32 rsa = mbedtls_pk_rsa( pk );
Philippe Antoine66070bc2020-01-22 13:54:56 +010033 if ( mbedtls_rsa_export( rsa, &N, &P, &Q, &D, &E ) != 0 ) {
34 abort();
35 }
Philippe Antoine7d4bd6f2020-01-22 14:13:08 +010036 if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != 0 ) {
37 abort();
38 }
Philippe Antoine72333522018-05-03 16:40:24 +020039
40 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
41 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
42 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
43 }
44 else
45#endif
46#if defined(MBEDTLS_ECP_C)
Gilles Peskinee60b3652020-02-25 19:54:07 +010047 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
48 mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
Philippe Antoine72333522018-05-03 16:40:24 +020049 {
Gilles Peskinef02b9842020-02-25 19:52:44 +010050 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
51 mbedtls_ecp_group_id grp_id = ecp->grp.id;
52 const mbedtls_ecp_curve_info *curve_info =
53 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Philippe Antoine72333522018-05-03 16:40:24 +020054
Gilles Peskinef02b9842020-02-25 19:52:44 +010055 /* If the curve is not supported, the key should not have been
56 * accepted. */
57 if( curve_info == NULL )
58 abort( );
Philippe Antoine72333522018-05-03 16:40:24 +020059 }
60 else
61#endif
Gilles Peskined7fb66f2020-02-25 19:54:27 +010062 {
63 /* The key is valid but is not of a supported type.
64 * This should not happen. */
65 abort( );
66 }
Philippe Antoine72333522018-05-03 16:40:24 +020067 }
68 mbedtls_pk_free( &pk );
69#else
70 (void) Data;
71 (void) Size;
72#endif //MBEDTLS_PK_PARSE_C
73
74 return 0;
75}