blob: b9a160e1e13cdffa6d0bfe7c8641ae450ddfe2d5 [file] [log] [blame]
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +02001#define MBEDTLS_ALLOW_PRIVATE_ACCESS
2
Philippe Antoine72333522018-05-03 16:40:24 +02003#include <stdint.h>
Philippe Antoine8b1ed1c2020-01-22 16:22:36 +01004#include <stdlib.h>
Philippe Antoine72333522018-05-03 16:40:24 +02005#include "mbedtls/pk.h"
Manuel Pégourié-Gonnard7f93da12021-06-16 10:20:30 +02006#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +02007
8//4 Kb should be enough for every bug ;-)
9#define MAX_LEN 0x1000
10
11
12int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
13#ifdef MBEDTLS_PK_PARSE_C
14 int ret;
15 mbedtls_pk_context pk;
16
17 if (Size > MAX_LEN) {
18 //only work on small inputs
19 Size = MAX_LEN;
20 }
21
22 mbedtls_pk_init( &pk );
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020023 ret = mbedtls_pk_parse_key( &pk, Data, Size, NULL, 0,
Manuel Pégourié-Gonnard7f93da12021-06-16 10:20:30 +020024 dummy_random, NULL );
Philippe Antoine72333522018-05-03 16:40:24 +020025 if (ret == 0) {
26#if defined(MBEDTLS_RSA_C)
27 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
28 {
29 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
30 mbedtls_rsa_context *rsa;
31
32 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
33 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
34 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
35
36 rsa = mbedtls_pk_rsa( pk );
Philippe Antoine66070bc2020-01-22 13:54:56 +010037 if ( mbedtls_rsa_export( rsa, &N, &P, &Q, &D, &E ) != 0 ) {
38 abort();
39 }
Philippe Antoine7d4bd6f2020-01-22 14:13:08 +010040 if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != 0 ) {
41 abort();
42 }
Philippe Antoine72333522018-05-03 16:40:24 +020043
44 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
45 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
46 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
47 }
48 else
49#endif
50#if defined(MBEDTLS_ECP_C)
Gilles Peskinee60b3652020-02-25 19:54:07 +010051 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
52 mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
Philippe Antoine72333522018-05-03 16:40:24 +020053 {
Gilles Peskinef02b9842020-02-25 19:52:44 +010054 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
55 mbedtls_ecp_group_id grp_id = ecp->grp.id;
56 const mbedtls_ecp_curve_info *curve_info =
57 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Philippe Antoine72333522018-05-03 16:40:24 +020058
Gilles Peskinef02b9842020-02-25 19:52:44 +010059 /* If the curve is not supported, the key should not have been
60 * accepted. */
61 if( curve_info == NULL )
62 abort( );
Philippe Antoine72333522018-05-03 16:40:24 +020063 }
64 else
65#endif
Gilles Peskined7fb66f2020-02-25 19:54:27 +010066 {
67 /* The key is valid but is not of a supported type.
68 * This should not happen. */
69 abort( );
70 }
Philippe Antoine72333522018-05-03 16:40:24 +020071 }
72 mbedtls_pk_free( &pk );
73#else
74 (void) Data;
75 (void) Size;
76#endif //MBEDTLS_PK_PARSE_C
77
78 return 0;
79}