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