blob: 655d5d615a5d8a01eac31e28c26cd2ccc4bfb850 [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"
6
7int 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 );
Gilles Peskine8d366962020-02-25 19:51:07 +010026 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 ) {
Philippe Antoine66070bc2020-01-22 13:54:56 +010030 abort();
31 }
Philippe Antoine7d4bd6f2020-01-22 14:13:08 +010032 if ( mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) != MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) {
33 abort();
34 }
Philippe Antoine72333522018-05-03 16:40:24 +020035
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)
Gilles Peskinee60b3652020-02-25 19:54:07 +010044 if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ||
45 mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY_DH )
Philippe Antoine72333522018-05-03 16:40:24 +020046 {
Gilles Peskinef02b9842020-02-25 19:52:44 +010047 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 );
Philippe Antoine72333522018-05-03 16:40:24 +020051
Gilles Peskinef02b9842020-02-25 19:52:44 +010052 /* 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( );
Philippe Antoine72333522018-05-03 16:40:24 +020061 }
62 else
63#endif
64 {
Gilles Peskined7fb66f2020-02-25 19:54:27 +010065 /* The key is valid but is not of a supported type.
66 * This should not happen. */
67 abort( );
Philippe Antoine72333522018-05-03 16:40:24 +020068 }
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}