1a8e1175bSopenharmony_ci#include <stdint.h> 2a8e1175bSopenharmony_ci#include <stdlib.h> 3a8e1175bSopenharmony_ci#include <string.h> 4a8e1175bSopenharmony_ci#include "mbedtls/pk.h" 5a8e1175bSopenharmony_ci#include "mbedtls/entropy.h" 6a8e1175bSopenharmony_ci#include "mbedtls/ctr_drbg.h" 7a8e1175bSopenharmony_ci#include "common.h" 8a8e1175bSopenharmony_ci 9a8e1175bSopenharmony_ci//4 Kb should be enough for every bug ;-) 10a8e1175bSopenharmony_ci#define MAX_LEN 0x1000 11a8e1175bSopenharmony_ci 12a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) 13a8e1175bSopenharmony_ciconst char *pers = "fuzz_privkey"; 14a8e1175bSopenharmony_ci#endif // MBEDTLS_PK_PARSE_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C 15a8e1175bSopenharmony_ci 16a8e1175bSopenharmony_ciint LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) 17a8e1175bSopenharmony_ci{ 18a8e1175bSopenharmony_ci#if defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) 19a8e1175bSopenharmony_ci int ret; 20a8e1175bSopenharmony_ci mbedtls_pk_context pk; 21a8e1175bSopenharmony_ci mbedtls_ctr_drbg_context ctr_drbg; 22a8e1175bSopenharmony_ci mbedtls_entropy_context entropy; 23a8e1175bSopenharmony_ci 24a8e1175bSopenharmony_ci if (Size > MAX_LEN) { 25a8e1175bSopenharmony_ci //only work on small inputs 26a8e1175bSopenharmony_ci Size = MAX_LEN; 27a8e1175bSopenharmony_ci } 28a8e1175bSopenharmony_ci 29a8e1175bSopenharmony_ci mbedtls_ctr_drbg_init(&ctr_drbg); 30a8e1175bSopenharmony_ci mbedtls_entropy_init(&entropy); 31a8e1175bSopenharmony_ci mbedtls_pk_init(&pk); 32a8e1175bSopenharmony_ci 33a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 34a8e1175bSopenharmony_ci psa_status_t status = psa_crypto_init(); 35a8e1175bSopenharmony_ci if (status != PSA_SUCCESS) { 36a8e1175bSopenharmony_ci goto exit; 37a8e1175bSopenharmony_ci } 38a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 39a8e1175bSopenharmony_ci 40a8e1175bSopenharmony_ci if (mbedtls_ctr_drbg_seed(&ctr_drbg, dummy_entropy, &entropy, 41a8e1175bSopenharmony_ci (const unsigned char *) pers, strlen(pers)) != 0) { 42a8e1175bSopenharmony_ci goto exit; 43a8e1175bSopenharmony_ci } 44a8e1175bSopenharmony_ci 45a8e1175bSopenharmony_ci ret = mbedtls_pk_parse_key(&pk, Data, Size, NULL, 0, 46a8e1175bSopenharmony_ci dummy_random, &ctr_drbg); 47a8e1175bSopenharmony_ci if (ret == 0) { 48a8e1175bSopenharmony_ci#if defined(MBEDTLS_RSA_C) 49a8e1175bSopenharmony_ci if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) { 50a8e1175bSopenharmony_ci mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; 51a8e1175bSopenharmony_ci mbedtls_rsa_context *rsa; 52a8e1175bSopenharmony_ci 53a8e1175bSopenharmony_ci mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q); 54a8e1175bSopenharmony_ci mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP); 55a8e1175bSopenharmony_ci mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP); 56a8e1175bSopenharmony_ci 57a8e1175bSopenharmony_ci rsa = mbedtls_pk_rsa(pk); 58a8e1175bSopenharmony_ci if (mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E) != 0) { 59a8e1175bSopenharmony_ci abort(); 60a8e1175bSopenharmony_ci } 61a8e1175bSopenharmony_ci if (mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP) != 0) { 62a8e1175bSopenharmony_ci abort(); 63a8e1175bSopenharmony_ci } 64a8e1175bSopenharmony_ci 65a8e1175bSopenharmony_ci mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q); 66a8e1175bSopenharmony_ci mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP); 67a8e1175bSopenharmony_ci mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP); 68a8e1175bSopenharmony_ci } else 69a8e1175bSopenharmony_ci#endif 70a8e1175bSopenharmony_ci#if defined(MBEDTLS_ECP_C) 71a8e1175bSopenharmony_ci if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY || 72a8e1175bSopenharmony_ci mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY_DH) { 73a8e1175bSopenharmony_ci mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk); 74a8e1175bSopenharmony_ci mbedtls_ecp_group_id grp_id = mbedtls_ecp_keypair_get_group_id(ecp); 75a8e1175bSopenharmony_ci const mbedtls_ecp_curve_info *curve_info = 76a8e1175bSopenharmony_ci mbedtls_ecp_curve_info_from_grp_id(grp_id); 77a8e1175bSopenharmony_ci 78a8e1175bSopenharmony_ci /* If the curve is not supported, the key should not have been 79a8e1175bSopenharmony_ci * accepted. */ 80a8e1175bSopenharmony_ci if (curve_info == NULL) { 81a8e1175bSopenharmony_ci abort(); 82a8e1175bSopenharmony_ci } 83a8e1175bSopenharmony_ci } else 84a8e1175bSopenharmony_ci#endif 85a8e1175bSopenharmony_ci { 86a8e1175bSopenharmony_ci /* The key is valid but is not of a supported type. 87a8e1175bSopenharmony_ci * This should not happen. */ 88a8e1175bSopenharmony_ci abort(); 89a8e1175bSopenharmony_ci } 90a8e1175bSopenharmony_ci } 91a8e1175bSopenharmony_ciexit: 92a8e1175bSopenharmony_ci mbedtls_entropy_free(&entropy); 93a8e1175bSopenharmony_ci mbedtls_ctr_drbg_free(&ctr_drbg); 94a8e1175bSopenharmony_ci mbedtls_pk_free(&pk); 95a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 96a8e1175bSopenharmony_ci mbedtls_psa_crypto_free(); 97a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 98a8e1175bSopenharmony_ci#else 99a8e1175bSopenharmony_ci (void) Data; 100a8e1175bSopenharmony_ci (void) Size; 101a8e1175bSopenharmony_ci#endif // MBEDTLS_PK_PARSE_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C 102a8e1175bSopenharmony_ci 103a8e1175bSopenharmony_ci return 0; 104a8e1175bSopenharmony_ci} 105