fuzzer-kexcurve25519.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "fuzz.h"
  2. #include "session.h"
  3. #include "fuzz-wrapfd.h"
  4. #include "debug.h"
  5. #include "runopts.h"
  6. #include "algo.h"
  7. #include "bignum.h"
  8. static struct key_context* keep_newkeys = NULL;
  9. /* An arbitrary limit */
  10. #define NUM_PARAMS 80
  11. static struct kex_curve25519_param *curve25519_params[NUM_PARAMS];
  12. static void setup() __attribute__((constructor));
  13. // Perform initial setup here to avoid hitting timeouts on first run
  14. static void setup() {
  15. fuzz_common_setup();
  16. fuzz_svr_setup();
  17. keep_newkeys = (struct key_context*)m_malloc(sizeof(struct key_context));
  18. keep_newkeys->algo_kex = fuzz_get_algo(sshkex, "curve25519-sha256");
  19. keep_newkeys->algo_hostkey = DROPBEAR_SIGNKEY_ED25519;
  20. ses.newkeys = keep_newkeys;
  21. /* Pre-generate parameters */
  22. int i;
  23. for (i = 0; i < NUM_PARAMS; i++) {
  24. curve25519_params[i] = gen_kexcurve25519_param();
  25. }
  26. }
  27. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  28. if (fuzz_set_input(Data, Size) == DROPBEAR_FAILURE) {
  29. return 0;
  30. }
  31. m_malloc_set_epoch(1);
  32. if (setjmp(fuzz.jmp) == 0) {
  33. /* Based on recv_msg_kexdh_init()/send_msg_kexdh_reply()
  34. with DROPBEAR_KEX_CURVE25519 */
  35. ses.newkeys = keep_newkeys;
  36. /* Choose from the collection of curve25519 params */
  37. unsigned int e = buf_getint(fuzz.input);
  38. struct kex_curve25519_param *curve25519_param = curve25519_params[e % NUM_PARAMS];
  39. buffer * ecdh_qs = buf_getstringbuf(fuzz.input);
  40. ses.kexhashbuf = buf_new(KEXHASHBUF_MAX_INTS);
  41. kexcurve25519_comb_key(curve25519_param, ecdh_qs, svr_opts.hostkey);
  42. mp_clear(ses.dh_K);
  43. m_free(ses.dh_K);
  44. buf_free(ecdh_qs);
  45. buf_free(ses.hash);
  46. buf_free(ses.session_id);
  47. /* kexhashbuf is freed in kexdh_comb_key */
  48. m_malloc_free_epoch(1, 0);
  49. } else {
  50. m_malloc_free_epoch(1, 1);
  51. TRACE(("dropbear_exit longjmped"))
  52. /* dropbear_exit jumped here */
  53. }
  54. return 0;
  55. }