fuzz_privkey.c 2.9 KB

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