• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
2 
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include "mbedtls/pk.h"
6 #include "common.h"
7 
8 //4 Kb should be enough for every bug ;-)
9 #define MAX_LEN 0x1000
10 
11 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)12 int 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 );
23     ret = mbedtls_pk_parse_key( &pk, Data, Size, NULL, 0,
24                                 dummy_random, NULL );
25     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 );
37             if ( mbedtls_rsa_export( rsa, &N, &P, &Q, &D, &E ) != 0 ) {
38                 abort();
39             }
40             if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != 0 ) {
41                 abort();
42             }
43 
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)
51         if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
52             mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
53         {
54             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 );
58 
59             /* If the curve is not supported, the key should not have been
60              * accepted. */
61             if( curve_info == NULL )
62                 abort( );
63         }
64         else
65 #endif
66         {
67             /* The key is valid but is not of a supported type.
68              * This should not happen. */
69             abort( );
70         }
71     }
72     mbedtls_pk_free( &pk );
73 #else
74     (void) Data;
75     (void) Size;
76 #endif //MBEDTLS_PK_PARSE_C
77 
78     return 0;
79 }
80