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.
 
 
 
 
 
 

126 line
4.1 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/mem.h>
  17. #define CHECK(expr) \
  18. do { \
  19. if (!(expr)) { \
  20. printf("%s failed\n", #expr); \
  21. abort(); \
  22. } \
  23. } while (false)
  24. // Basic implementation of mod_exp using square and multiple method.
  25. int mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
  26. BN_CTX *ctx) {
  27. if (BN_is_one(m)) {
  28. BN_zero(r);
  29. return 1;
  30. }
  31. bssl::UniquePtr<BIGNUM> exp(BN_dup(p));
  32. bssl::UniquePtr<BIGNUM> base(BN_new());
  33. if (!exp || !base) {
  34. return 0;
  35. }
  36. if (!BN_one(r) || !BN_nnmod(base.get(), a, m, ctx)) {
  37. return 0;
  38. }
  39. while (!BN_is_zero(exp.get())) {
  40. if (BN_is_odd(exp.get())) {
  41. if (!BN_mul(r, r, base.get(), ctx) || !BN_nnmod(r, r, m, ctx)) {
  42. return 0;
  43. }
  44. }
  45. if (!BN_rshift1(exp.get(), exp.get()) ||
  46. !BN_mul(base.get(), base.get(), base.get(), ctx) ||
  47. !BN_nnmod(base.get(), base.get(), m, ctx)) {
  48. return 0;
  49. }
  50. }
  51. return 1;
  52. }
  53. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
  54. CBS cbs, child0, child1, child2;
  55. uint8_t sign;
  56. CBS_init(&cbs, buf, len);
  57. if (!CBS_get_u16_length_prefixed(&cbs, &child0) ||
  58. !CBS_get_u8(&child0, &sign) ||
  59. CBS_len(&child0) == 0 ||
  60. !CBS_get_u16_length_prefixed(&cbs, &child1) ||
  61. CBS_len(&child1) == 0 ||
  62. !CBS_get_u16_length_prefixed(&cbs, &child2) ||
  63. CBS_len(&child2) == 0) {
  64. return 0;
  65. }
  66. // Don't fuzz inputs larger than 512 bytes (4096 bits). This isn't ideal, but
  67. // the naive |mod_exp| above is somewhat slow, so this otherwise causes the
  68. // fuzzers to spend a lot of time exploring timeouts.
  69. if (CBS_len(&child0) > 512 ||
  70. CBS_len(&child1) > 512 ||
  71. CBS_len(&child2) > 512) {
  72. return 0;
  73. }
  74. bssl::UniquePtr<BIGNUM> base(
  75. BN_bin2bn(CBS_data(&child0), CBS_len(&child0), nullptr));
  76. BN_set_negative(base.get(), sign % 2);
  77. bssl::UniquePtr<BIGNUM> power(
  78. BN_bin2bn(CBS_data(&child1), CBS_len(&child1), nullptr));
  79. bssl::UniquePtr<BIGNUM> modulus(
  80. BN_bin2bn(CBS_data(&child2), CBS_len(&child2), nullptr));
  81. if (BN_is_zero(modulus.get())) {
  82. return 0;
  83. }
  84. bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
  85. bssl::UniquePtr<BIGNUM> result(BN_new());
  86. bssl::UniquePtr<BIGNUM> expected(BN_new());
  87. CHECK(ctx);
  88. CHECK(result);
  89. CHECK(expected);
  90. CHECK(mod_exp(expected.get(), base.get(), power.get(), modulus.get(),
  91. ctx.get()));
  92. CHECK(BN_mod_exp(result.get(), base.get(), power.get(), modulus.get(),
  93. ctx.get()));
  94. CHECK(BN_cmp(result.get(), expected.get()) == 0);
  95. if (BN_is_odd(modulus.get())) {
  96. bssl::UniquePtr<BN_MONT_CTX> mont(
  97. BN_MONT_CTX_new_for_modulus(modulus.get(), ctx.get()));
  98. CHECK(mont);
  99. CHECK(BN_mod_exp_mont(result.get(), base.get(), power.get(), modulus.get(),
  100. ctx.get(), mont.get()));
  101. CHECK(BN_cmp(result.get(), expected.get()) == 0);
  102. CHECK(BN_mod_exp_mont_consttime(result.get(), base.get(), power.get(),
  103. modulus.get(), ctx.get(), mont.get()));
  104. CHECK(BN_cmp(result.get(), expected.get()) == 0);
  105. }
  106. uint8_t *data = (uint8_t *)OPENSSL_malloc(BN_num_bytes(result.get()));
  107. BN_bn2bin(result.get(), data);
  108. OPENSSL_free(data);
  109. return 0;
  110. }