blob: e8e1d442eabedd26a19735ed10fc9c1bf7ca2996 [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>
Paul Elliott51a76792022-02-11 19:10:14 +00005#include <string.h>
Philippe Antoine72333522018-05-03 16:40:24 +02006#include "mbedtls/pk.h"
Paul Elliott51a76792022-02-11 19:10:14 +00007#include "mbedtls/entropy.h"
8#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard7f93da12021-06-16 10:20:30 +02009#include "common.h"
Philippe Antoine72333522018-05-03 16:40:24 +020010
11//4 Kb should be enough for every bug ;-)
12#define MAX_LEN 0x1000
13
Paul Elliott51a76792022-02-11 19:10:14 +000014#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_CTR_DRBG_C)
15const char *pers = "fuzz_privkey";
16#endif // MBEDTLS_PK_PARSE_C && MBEDTLS_CTR_DRBG_C
Philippe Antoine72333522018-05-03 16:40:24 +020017
18int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Paul Elliott51a76792022-02-11 19:10:14 +000019#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_CTR_DRBG_C)
Philippe Antoine72333522018-05-03 16:40:24 +020020 int ret;
21 mbedtls_pk_context pk;
Paul Elliott51a76792022-02-11 19:10:14 +000022 mbedtls_ctr_drbg_context ctr_drbg;
23 mbedtls_entropy_context entropy;
Philippe Antoine72333522018-05-03 16:40:24 +020024
25 if (Size > MAX_LEN) {
26 //only work on small inputs
27 Size = MAX_LEN;
28 }
29
Paul Elliott51a76792022-02-11 19:10:14 +000030 mbedtls_ctr_drbg_init( &ctr_drbg );
31 mbedtls_entropy_init( &entropy );
32
33 if( mbedtls_ctr_drbg_seed( &ctr_drbg, dummy_entropy, &entropy,
34 ( const unsigned char * ) pers, strlen( pers ) ) != 0 )
35 return 1;
36
Philippe Antoine72333522018-05-03 16:40:24 +020037 mbedtls_pk_init( &pk );
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +020038 ret = mbedtls_pk_parse_key( &pk, Data, Size, NULL, 0,
Paul Elliott51a76792022-02-11 19:10:14 +000039 dummy_random, &ctr_drbg );
Philippe Antoine72333522018-05-03 16:40:24 +020040 if (ret == 0) {
41#if defined(MBEDTLS_RSA_C)
42 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
43 {
44 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
45 mbedtls_rsa_context *rsa;
46
47 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
48 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
49 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
50
51 rsa = mbedtls_pk_rsa( pk );
Philippe Antoine66070bc2020-01-22 13:54:56 +010052 if ( mbedtls_rsa_export( rsa, &N, &P, &Q, &D, &E ) != 0 ) {
53 abort();
54 }
Philippe Antoine7d4bd6f2020-01-22 14:13:08 +010055 if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != 0 ) {
56 abort();
57 }
Philippe Antoine72333522018-05-03 16:40:24 +020058
59 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
60 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
61 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
62 }
63 else
64#endif
65#if defined(MBEDTLS_ECP_C)
Gilles Peskinee60b3652020-02-25 19:54:07 +010066 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
67 mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
Philippe Antoine72333522018-05-03 16:40:24 +020068 {
Gilles Peskinef02b9842020-02-25 19:52:44 +010069 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
70 mbedtls_ecp_group_id grp_id = ecp->grp.id;
71 const mbedtls_ecp_curve_info *curve_info =
72 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Philippe Antoine72333522018-05-03 16:40:24 +020073
Gilles Peskinef02b9842020-02-25 19:52:44 +010074 /* If the curve is not supported, the key should not have been
75 * accepted. */
76 if( curve_info == NULL )
77 abort( );
Philippe Antoine72333522018-05-03 16:40:24 +020078 }
79 else
80#endif
Gilles Peskined7fb66f2020-02-25 19:54:27 +010081 {
82 /* The key is valid but is not of a supported type.
83 * This should not happen. */
84 abort( );
85 }
Philippe Antoine72333522018-05-03 16:40:24 +020086 }
87 mbedtls_pk_free( &pk );
88#else
89 (void) Data;
90 (void) Size;
Paul Elliott51a76792022-02-11 19:10:14 +000091#endif // MBEDTLS_PK_PARSE_C && MBEDTLS_CTR_DRBG_C
Philippe Antoine72333522018-05-03 16:40:24 +020092
93 return 0;
94}