2
0

bn_internal_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 "crypto/bn.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(ossl_bn_miller_rabin_is_prime(bn, 10, ctx, NULL, 1,
  34. &status))
  35. && TEST_int_eq(status, BN_PRIMETEST_PROBABLY_PRIME);
  36. BN_free(bn);
  37. return ret;
  38. }
  39. static int composites[] = {
  40. 9, 21, 77, 81, 265
  41. };
  42. static int test_is_composite_enhanced(int id)
  43. {
  44. int ret;
  45. int status = 0;
  46. BIGNUM *bn = NULL;
  47. ret = TEST_ptr(bn = BN_new())
  48. /* negative tests for different composite numbers */
  49. && TEST_true(BN_set_word(bn, composites[id]))
  50. && TEST_true(ossl_bn_miller_rabin_is_prime(bn, 10, ctx, NULL, 1,
  51. &status))
  52. && TEST_int_ne(status, BN_PRIMETEST_PROBABLY_PRIME);
  53. BN_free(bn);
  54. return ret;
  55. }
  56. /* Test that multiplying all the small primes from 3 to 751 equals a constant.
  57. * This test is mainly used to test that both 32 and 64 bit are correct.
  58. */
  59. static int test_bn_small_factors(void)
  60. {
  61. int ret = 0, i;
  62. BIGNUM *b = NULL;
  63. if (!(TEST_ptr(b = BN_new()) && TEST_true(BN_set_word(b, 3))))
  64. goto err;
  65. for (i = 1; i < NUMPRIMES; i++) {
  66. prime_t p = primes[i];
  67. if (p > 3 && p <= 751 && !BN_mul_word(b, p))
  68. goto err;
  69. if (p > 751)
  70. break;
  71. }
  72. ret = TEST_BN_eq(ossl_bn_get0_small_factors(), b);
  73. err:
  74. BN_free(b);
  75. return ret;
  76. }
  77. int setup_tests(void)
  78. {
  79. if (!TEST_ptr(ctx = BN_CTX_new()))
  80. return 0;
  81. ADD_TEST(test_is_prime_enhanced);
  82. ADD_ALL_TESTS(test_is_composite_enhanced, (int)OSSL_NELEM(composites));
  83. ADD_TEST(test_bn_small_factors);
  84. return 1;
  85. }
  86. void cleanup_tests(void)
  87. {
  88. BN_CTX_free(ctx);
  89. }