You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

70 lines
2.4 KiB

  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/bn.h>
  15. #include <openssl/bytestring.h>
  16. #include <openssl/span.h>
  17. #define CHECK(expr) \
  18. do { \
  19. if (!(expr)) { \
  20. printf("%s failed\n", #expr); \
  21. abort(); \
  22. } \
  23. } while (false)
  24. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
  25. CBS cbs, child0, child1;
  26. uint8_t sign0, sign1;
  27. CBS_init(&cbs, buf, len);
  28. if (!CBS_get_u16_length_prefixed(&cbs, &child0) ||
  29. !CBS_get_u8(&child0, &sign0) ||
  30. CBS_len(&child0) == 0 ||
  31. !CBS_get_u16_length_prefixed(&cbs, &child1) ||
  32. !CBS_get_u8(&child1, &sign1) ||
  33. CBS_len(&child1) == 0) {
  34. return 0;
  35. }
  36. bssl::UniquePtr<BIGNUM> numerator(
  37. BN_bin2bn(CBS_data(&child0), CBS_len(&child0), nullptr));
  38. BN_set_negative(numerator.get(), sign0 % 2);
  39. bssl::UniquePtr<BIGNUM> divisor(
  40. BN_bin2bn(CBS_data(&child1), CBS_len(&child1), nullptr));
  41. BN_set_negative(divisor.get(), sign1 % 2);
  42. if (BN_is_zero(divisor.get())) {
  43. return 0;
  44. }
  45. bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
  46. bssl::UniquePtr<BIGNUM> result(BN_new());
  47. bssl::UniquePtr<BIGNUM> remainder(BN_new());
  48. CHECK(ctx);
  49. CHECK(result);
  50. CHECK(remainder);
  51. CHECK(BN_div(result.get(), remainder.get(), numerator.get(), divisor.get(),
  52. ctx.get()));
  53. CHECK(BN_ucmp(remainder.get(), divisor.get()) < 0);
  54. // Check that result*divisor+remainder = numerator.
  55. CHECK(BN_mul(result.get(), result.get(), divisor.get(), ctx.get()));
  56. CHECK(BN_add(result.get(), result.get(), remainder.get()));
  57. CHECK(BN_cmp(result.get(), numerator.get()) == 0);
  58. return 0;
  59. }