bn_internal_test.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <openssl/bn.h>
  15. #include <openssl/crypto.h>
  16. #include <openssl/err.h>
  17. #include <openssl/rand.h>
  18. #include "internal/nelem.h"
  19. #include "internal/numbers.h"
  20. #include "testutil.h"
  21. #include "bn_prime.h"
  22. #include "internal/bn_int.h"
  23. static BN_CTX *ctx;
  24. static int test_is_prime_enhanced(void)
  25. {
  26. int ret;
  27. int status = 0;
  28. BIGNUM *bn = NULL;
  29. ret = TEST_ptr(bn = BN_new())
  30. /* test passing a prime returns the correct status */
  31. && TEST_true(BN_set_word(bn, 11))
  32. /* return extra parameters related to composite */
  33. && TEST_true(bn_miller_rabin_is_prime(bn, 10, ctx, NULL, 1, &status))
  34. && TEST_int_eq(status, BN_PRIMETEST_PROBABLY_PRIME);
  35. BN_free(bn);
  36. return ret;
  37. }
  38. static int composites[] = {
  39. 9, 21, 77, 81, 265
  40. };
  41. static int test_is_composite_enhanced(int id)
  42. {
  43. int ret;
  44. int status = 0;
  45. BIGNUM *bn = NULL;
  46. ret = TEST_ptr(bn = BN_new())
  47. /* negative tests for different composite numbers */
  48. && TEST_true(BN_set_word(bn, composites[id]))
  49. && TEST_true(bn_miller_rabin_is_prime(bn, 10, ctx, NULL, 1, &status))
  50. && TEST_int_ne(status, BN_PRIMETEST_PROBABLY_PRIME);
  51. BN_free(bn);
  52. return ret;
  53. }
  54. /* Test that multiplying all the small primes from 3 to 751 equals a constant.
  55. * This test is mainly used to test that both 32 and 64 bit are correct.
  56. */
  57. static int test_bn_small_factors(void)
  58. {
  59. int ret = 0, i;
  60. BIGNUM *b = NULL;
  61. if (!(TEST_ptr(b = BN_new()) && TEST_true(BN_set_word(b, 3))))
  62. goto err;
  63. for (i = 1; i < NUMPRIMES; i++) {
  64. prime_t p = primes[i];
  65. if (p > 3 && p <= 751 && !BN_mul_word(b, p))
  66. goto err;
  67. if (p > 751)
  68. break;
  69. }
  70. ret = TEST_BN_eq(bn_get0_small_factors(), b);
  71. err:
  72. BN_free(b);
  73. return ret;
  74. }
  75. int setup_tests(void)
  76. {
  77. if (!TEST_ptr(ctx = BN_CTX_new()))
  78. return 0;
  79. ADD_TEST(test_is_prime_enhanced);
  80. ADD_ALL_TESTS(test_is_composite_enhanced, (int)OSSL_NELEM(composites));
  81. ADD_TEST(test_bn_small_factors);
  82. return 1;
  83. }
  84. void cleanup_tests(void)
  85. {
  86. BN_CTX_free(ctx);
  87. }