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 {
9 #ifdef MBEDTLS_PK_PARSE_C
10 int ret;
11 mbedtls_pk_context pk;
12
13 mbedtls_pk_init(&pk);
14 ret = mbedtls_pk_parse_public_key(&pk, Data, Size);
15 if (ret == 0) {
16 #if defined(MBEDTLS_RSA_C)
17 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
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 } else
41 #endif
42 #if defined(MBEDTLS_ECP_C)
43 if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY ||
44 mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY_DH) {
45 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
46 mbedtls_ecp_group_id grp_id = ecp->grp.id;
47 const mbedtls_ecp_curve_info *curve_info =
48 mbedtls_ecp_curve_info_from_grp_id(grp_id);
49
50 /* If the curve is not supported, the key should not have been
51 * accepted. */
52 if (curve_info == NULL) {
53 abort();
54 }
55
56 /* It's a public key, so the private value should not have
57 * been changed from its initialization to 0. */
58 if (mbedtls_mpi_cmp_int(&ecp->d, 0) != 0) {
59 abort();
60 }
61 } else
62 #endif
63 {
64 /* The key is valid but is not of a supported type.
65 * This should not happen. */
66 abort();
67 }
68 }
69 mbedtls_pk_free(&pk);
70 #else
71 (void) Data;
72 (void) Size;
73 #endif //MBEDTLS_PK_PARSE_C
74
75 return 0;
76 }
77