2018-05-03 16:40:24 +02:00
|
|
|
#include <stdint.h>
|
2020-01-22 16:22:36 +01:00
|
|
|
#include <stdlib.h>
|
2022-02-11 20:10:14 +01:00
|
|
|
#include <string.h>
|
2018-05-03 16:40:24 +02:00
|
|
|
#include "mbedtls/pk.h"
|
2022-02-11 20:10:14 +01:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
2021-06-16 10:20:30 +02:00
|
|
|
#include "common.h"
|
2018-05-03 16:40:24 +02:00
|
|
|
|
|
|
|
//4 Kb should be enough for every bug ;-)
|
|
|
|
#define MAX_LEN 0x1000
|
|
|
|
|
2022-07-20 16:25:06 +02:00
|
|
|
#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C)
|
2022-02-11 20:10:14 +01:00
|
|
|
const char *pers = "fuzz_privkey";
|
2022-07-20 16:25:06 +02:00
|
|
|
#endif // MBEDTLS_PK_PARSE_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
|
|
|
|
{
|
2022-07-20 16:25:06 +02:00
|
|
|
#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C)
|
2018-05-03 16:40:24 +02:00
|
|
|
int ret;
|
|
|
|
mbedtls_pk_context pk;
|
2022-02-11 20:10:14 +01:00
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_entropy_context entropy;
|
2018-05-03 16:40:24 +02:00
|
|
|
|
|
|
|
if (Size > MAX_LEN) {
|
|
|
|
//only work on small inputs
|
|
|
|
Size = MAX_LEN;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
|
mbedtls_entropy_init(&entropy);
|
2023-04-19 11:47:01 +02:00
|
|
|
mbedtls_pk_init(&pk);
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status = psa_crypto_init();
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-11 20:10:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy,
|
|
|
|
(const unsigned char *) pers, strlen(pers)) != 0) {
|
2023-04-19 11:47:01 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-11 20:10:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_parse_key(&pk, Data, Size, NULL, 0,
|
|
|
|
dummy_random, &ctr_drbg);
|
2018-05-03 16:40:24 +02:00
|
|
|
if (ret == 0) {
|
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) {
|
2018-05-03 16:40:24 +02:00
|
|
|
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
|
|
|
mbedtls_rsa_context *rsa;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
|
|
|
|
mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
|
|
|
|
mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
rsa = mbedtls_pk_rsa(pk);
|
|
|
|
if (mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E) != 0) {
|
2020-01-22 13:54:56 +01:00
|
|
|
abort();
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP) != 0) {
|
2020-01-22 14:13:08 +01:00
|
|
|
abort();
|
|
|
|
}
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
|
|
|
|
mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
|
|
|
|
mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
|
|
|
|
} else
|
2018-05-03 16:40:24 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY ||
|
|
|
|
mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY_DH) {
|
|
|
|
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk);
|
2024-02-22 12:05:35 +01:00
|
|
|
mbedtls_ecp_group_id grp_id = mbedtls_ecp_keypair_get_group_id(ecp);
|
2020-02-25 19:52:44 +01:00
|
|
|
const mbedtls_ecp_curve_info *curve_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_curve_info_from_grp_id(grp_id);
|
2018-05-03 16:40:24 +02:00
|
|
|
|
2020-02-25 19:52:44 +01:00
|
|
|
/* If the curve is not supported, the key should not have been
|
|
|
|
* accepted. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (curve_info == NULL) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} else
|
2018-05-03 16:40:24 +02:00
|
|
|
#endif
|
2020-02-25 19:54:27 +01:00
|
|
|
{
|
|
|
|
/* The key is valid but is not of a supported type.
|
|
|
|
* This should not happen. */
|
2023-01-11 14:50:10 +01:00
|
|
|
abort();
|
2020-02-25 19:54:27 +01:00
|
|
|
}
|
2018-05-03 16:40:24 +02:00
|
|
|
}
|
2023-04-19 11:47:01 +02:00
|
|
|
exit:
|
|
|
|
mbedtls_entropy_free(&entropy);
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&pk);
|
2023-04-19 13:47:43 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-04-19 11:47:01 +02:00
|
|
|
mbedtls_psa_crypto_free();
|
2023-04-19 13:47:43 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2018-05-03 16:40:24 +02:00
|
|
|
#else
|
|
|
|
(void) Data;
|
|
|
|
(void) Size;
|
2022-07-20 16:25:06 +02:00
|
|
|
#endif // MBEDTLS_PK_PARSE_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C
|
2018-05-03 16:40:24 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|