• 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 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)7 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
8 #ifdef MBEDTLS_PK_PARSE_C
9     int ret;
10     mbedtls_pk_context pk;
11 
12     mbedtls_pk_init( &pk );
13     ret = mbedtls_pk_parse_public_key( &pk, Data, Size );
14     if (ret == 0) {
15 #if defined(MBEDTLS_RSA_C)
16         if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
17         {
18             mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
19             mbedtls_rsa_context *rsa;
20 
21             mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
22             mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
23             mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
24 
25             rsa = mbedtls_pk_rsa( pk );
26             if ( mbedtls_rsa_export( rsa, &N, NULL, NULL, NULL, &E ) != 0 ) {
27                 abort();
28             }
29             if ( mbedtls_rsa_export( rsa, &N, &P, &Q, &D, &E ) != MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) {
30                 abort();
31             }
32             if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) {
33                 abort();
34             }
35 
36             mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
37             mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
38             mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
39 
40         }
41         else
42 #endif
43 #if defined(MBEDTLS_ECP_C)
44         if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
45             mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
46         {
47             mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
48             mbedtls_ecp_group_id grp_id = ecp->grp.id;
49             const mbedtls_ecp_curve_info *curve_info =
50                 mbedtls_ecp_curve_info_from_grp_id( grp_id );
51 
52             /* If the curve is not supported, the key should not have been
53              * accepted. */
54             if( curve_info == NULL )
55                 abort( );
56 
57             /* It's a public key, so the private value should not have
58              * been changed from its initialization to 0. */
59             if( mbedtls_mpi_cmp_int( &ecp->d, 0 ) != 0 )
60                 abort( );
61         }
62         else
63 #endif
64         {
65             /* The key is valid but is not of a supported type.
66              * This should not happen. */
67             abort( );
68         }
69     }
70     mbedtls_pk_free( &pk );
71 #else
72     (void) Data;
73     (void) Size;
74 #endif //MBEDTLS_PK_PARSE_C
75 
76     return 0;
77 }
78