bndiv.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL licenses, (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. /*
  11. * Confirm that if (d, r) = a / b, then b * d + r == a, and that sign(d) ==
  12. * sign(a), and 0 <= r <= b
  13. */
  14. #include <stdio.h>
  15. #include <openssl/bn.h>
  16. #include <openssl/err.h>
  17. #include "fuzzer.h"
  18. static BN_CTX *ctx;
  19. static BIGNUM *b1;
  20. static BIGNUM *b2;
  21. static BIGNUM *b3;
  22. static BIGNUM *b4;
  23. static BIGNUM *b5;
  24. int FuzzerInitialize(int *argc, char ***argv)
  25. {
  26. b1 = BN_new();
  27. b2 = BN_new();
  28. b3 = BN_new();
  29. b4 = BN_new();
  30. b5 = BN_new();
  31. ctx = BN_CTX_new();
  32. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  33. ERR_get_state();
  34. return 1;
  35. }
  36. int FuzzerTestOneInput(const uint8_t *buf, size_t len)
  37. {
  38. int success = 0;
  39. size_t l1 = 0, l2 = 0;
  40. /* s1 and s2 will be the signs for b1 and b2. */
  41. int s1 = 0, s2 = 0;
  42. /* We are going to split the buffer in two, sizes l1 and l2, giving b1 and
  43. * b2.
  44. */
  45. if (len > 0) {
  46. --len;
  47. /* Use first byte to divide the remaining buffer into 3Fths. I admit
  48. * this disallows some number sizes. If it matters, better ideas are
  49. * welcome (Ben).
  50. */
  51. l1 = ((buf[0] & 0x3f) * len) / 0x3f;
  52. s1 = buf[0] & 0x40;
  53. s2 = buf[0] & 0x80;
  54. ++buf;
  55. l2 = len - l1;
  56. }
  57. OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1);
  58. BN_set_negative(b1, s1);
  59. OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2);
  60. BN_set_negative(b2, s2);
  61. /* divide by 0 is an error */
  62. if (BN_is_zero(b2)) {
  63. success = 1;
  64. goto done;
  65. }
  66. OPENSSL_assert(BN_div(b3, b4, b1, b2, ctx));
  67. if (BN_is_zero(b1))
  68. success = BN_is_zero(b3) && BN_is_zero(b4);
  69. else if (BN_is_negative(b1))
  70. success = (BN_is_negative(b3) != BN_is_negative(b2) || BN_is_zero(b3))
  71. && (BN_is_negative(b4) || BN_is_zero(b4));
  72. else
  73. success = (BN_is_negative(b3) == BN_is_negative(b2) || BN_is_zero(b3))
  74. && (!BN_is_negative(b4) || BN_is_zero(b4));
  75. OPENSSL_assert(BN_mul(b5, b3, b2, ctx));
  76. OPENSSL_assert(BN_add(b5, b5, b4));
  77. success = success && BN_cmp(b5, b1) == 0;
  78. if (!success) {
  79. BN_print_fp(stdout, b1);
  80. putchar('\n');
  81. BN_print_fp(stdout, b2);
  82. putchar('\n');
  83. BN_print_fp(stdout, b3);
  84. putchar('\n');
  85. BN_print_fp(stdout, b4);
  86. putchar('\n');
  87. BN_print_fp(stdout, b5);
  88. putchar('\n');
  89. printf("%d %d %d %d %d %d %d\n", BN_is_negative(b1),
  90. BN_is_negative(b2),
  91. BN_is_negative(b3), BN_is_negative(b4), BN_is_zero(b4),
  92. BN_is_negative(b3) != BN_is_negative(b2)
  93. && (BN_is_negative(b4) || BN_is_zero(b4)),
  94. BN_cmp(b5, b1));
  95. puts("----\n");
  96. }
  97. done:
  98. OPENSSL_assert(success);
  99. ERR_clear_error();
  100. return 0;
  101. }
  102. void FuzzerCleanup(void)
  103. {
  104. BN_free(b1);
  105. BN_free(b2);
  106. BN_free(b3);
  107. BN_free(b4);
  108. BN_free(b5);
  109. BN_CTX_free(ctx);
  110. }