2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
14 # include <strings.h> /* strcasecmp */
18 #include <openssl/bn.h>
19 #include <openssl/crypto.h>
20 #include <openssl/err.h>
21 #include <openssl/rand.h>
22 #include "internal/nelem.h"
23 #include "internal/numbers.h"
26 #ifdef OPENSSL_SYS_WINDOWS
27 # define strcasecmp _stricmp
31 * Things in boring, not in openssl.
33 #define HAVE_BN_PADDED 0
34 #define HAVE_BN_SQRT 0
36 typedef struct filetest_st {
38 int (*func)(STANZA *s);
41 typedef struct mpitest_st {
47 static const int NUM0 = 100; /* number of tests */
48 static const int NUM1 = 50; /* additional tests for some functions */
52 * Polynomial coefficients used in GFM tests.
54 #ifndef OPENSSL_NO_EC2M
55 static int p0[] = { 163, 7, 6, 3, 0, -1 };
56 static int p1[] = { 193, 15, 0, -1 };
60 * Look for |key| in the stanza and return it or NULL if not found.
62 static const char *findattr(STANZA *s, const char *key)
67 for ( ; --i >= 0; pp++)
68 if (strcasecmp(pp->key, key) == 0)
74 * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
76 static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
78 char *bigstring = glue_strings(bn_strings, NULL);
79 int ret = BN_hex2bn(out, bigstring);
81 OPENSSL_free(bigstring);
86 * Parse BIGNUM, return number of bytes parsed.
88 static int parseBN(BIGNUM **out, const char *in)
91 return BN_hex2bn(out, in);
94 static int parsedecBN(BIGNUM **out, const char *in)
97 return BN_dec2bn(out, in);
100 static BIGNUM *getBN(STANZA *s, const char *attribute)
105 if ((hex = findattr(s, attribute)) == NULL) {
106 TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
110 if (parseBN(&ret, hex) != (int)strlen(hex)) {
111 TEST_error("Could not decode '%s'", hex);
117 static int getint(STANZA *s, int *out, const char *attribute)
123 if (!TEST_ptr(ret = getBN(s, attribute))
124 || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
134 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
136 if (BN_cmp(expected, actual) == 0)
139 TEST_error("unexpected %s value", op);
140 TEST_BN_eq(expected, actual);
145 * Return a "random" flag for if a BN should be negated.
147 static int rand_neg(void)
149 static unsigned int neg = 0;
150 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
152 return sign[(neg++) % 8];
155 static int test_swap(void)
157 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
158 int top, cond, st = 0;
160 if (!TEST_ptr(a = BN_new())
161 || !TEST_ptr(b = BN_new())
162 || !TEST_ptr(c = BN_new())
163 || !TEST_ptr(d = BN_new()))
166 if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))
167 && TEST_true(BN_bntest_rand(b, 1024, 1, 0))
168 && TEST_ptr(BN_copy(c, a))
169 && TEST_ptr(BN_copy(d, b))))
171 top = BN_num_bits(a) / BN_BITS2;
175 if (!equalBN("swap", a, d)
176 || !equalBN("swap", b, c))
179 /* conditional swap: true */
181 BN_consttime_swap(cond, a, b, top);
182 if (!equalBN("cswap true", a, c)
183 || !equalBN("cswap true", b, d))
186 /* conditional swap: false */
188 BN_consttime_swap(cond, a, b, top);
189 if (!equalBN("cswap false", a, c)
190 || !equalBN("cswap false", b, d))
193 /* same tests but checking flag swap */
194 BN_set_flags(a, BN_FLG_CONSTTIME);
197 if (!equalBN("swap, flags", a, d)
198 || !equalBN("swap, flags", b, c)
199 || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
200 || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
204 BN_consttime_swap(cond, a, b, top);
205 if (!equalBN("cswap true, flags", a, c)
206 || !equalBN("cswap true, flags", b, d)
207 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
208 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
212 BN_consttime_swap(cond, a, b, top);
213 if (!equalBN("cswap false, flags", a, c)
214 || !equalBN("cswap false, flags", b, d)
215 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
216 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
228 static int test_sub(void)
230 BIGNUM *a = NULL, *b = NULL, *c = NULL;
233 if (!TEST_ptr(a = BN_new())
234 || !TEST_ptr(b = BN_new())
235 || !TEST_ptr(c = BN_new()))
238 for (i = 0; i < NUM0 + NUM1; i++) {
240 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0)))
241 && TEST_ptr(BN_copy(b, a))
242 && TEST_int_ne(BN_set_bit(a, i), 0)
243 && TEST_true(BN_add_word(b, i)))
246 if (!TEST_true(BN_bntest_rand(b, 400 + i - NUM1, 0, 0)))
248 BN_set_negative(a, rand_neg());
249 BN_set_negative(b, rand_neg());
251 if (!(TEST_true(BN_sub(c, a, b))
252 && TEST_true(BN_add(c, c, b))
253 && TEST_true(BN_sub(c, c, a))
254 && TEST_BN_eq_zero(c)))
265 static int test_div_recip(void)
267 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
268 BN_RECP_CTX *recp = NULL;
271 if (!TEST_ptr(a = BN_new())
272 || !TEST_ptr(b = BN_new())
273 || !TEST_ptr(c = BN_new())
274 || !TEST_ptr(d = BN_new())
275 || !TEST_ptr(e = BN_new())
276 || !TEST_ptr(recp = BN_RECP_CTX_new()))
279 for (i = 0; i < NUM0 + NUM1; i++) {
281 if (!(TEST_true(BN_bntest_rand(a, 400, 0, 0))
282 && TEST_ptr(BN_copy(b, a))
283 && TEST_true(BN_lshift(a, a, i))
284 && TEST_true(BN_add_word(a, i))))
287 if (!(TEST_true(BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0))))
290 BN_set_negative(a, rand_neg());
291 BN_set_negative(b, rand_neg());
292 if (!(TEST_true(BN_RECP_CTX_set(recp, b, ctx))
293 && TEST_true(BN_div_recp(d, c, a, recp, ctx))
294 && TEST_true(BN_mul(e, d, b, ctx))
295 && TEST_true(BN_add(d, e, c))
296 && TEST_true(BN_sub(d, d, a))
297 && TEST_BN_eq_zero(d)))
307 BN_RECP_CTX_free(recp);
312 int n, divisor, result, remainder;
313 } signed_mod_tests[] = {
320 static BIGNUM *set_signed_bn(int value)
322 BIGNUM *bn = BN_new();
326 if (!BN_set_word(bn, value < 0 ? -value : value)) {
330 BN_set_negative(bn, value < 0);
334 static int test_signed_mod_replace_ab(int n)
336 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
339 if (!TEST_ptr(a = set_signed_bn(signed_mod_tests[n].n))
340 || !TEST_ptr(b = set_signed_bn(signed_mod_tests[n].divisor))
341 || !TEST_ptr(c = set_signed_bn(signed_mod_tests[n].result))
342 || !TEST_ptr(d = set_signed_bn(signed_mod_tests[n].remainder)))
345 if (TEST_true(BN_div(a, b, a, b, ctx))
357 static int test_signed_mod_replace_ba(int n)
359 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
362 if (!TEST_ptr(a = set_signed_bn(signed_mod_tests[n].n))
363 || !TEST_ptr(b = set_signed_bn(signed_mod_tests[n].divisor))
364 || !TEST_ptr(c = set_signed_bn(signed_mod_tests[n].result))
365 || !TEST_ptr(d = set_signed_bn(signed_mod_tests[n].remainder)))
368 if (TEST_true(BN_div(b, a, a, b, ctx))
380 static int test_mod(void)
382 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
385 if (!TEST_ptr(a = BN_new())
386 || !TEST_ptr(b = BN_new())
387 || !TEST_ptr(c = BN_new())
388 || !TEST_ptr(d = BN_new())
389 || !TEST_ptr(e = BN_new()))
392 if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
394 for (i = 0; i < NUM0; i++) {
395 if (!(TEST_true(BN_bntest_rand(b, 450 + i * 10, 0, 0))))
397 BN_set_negative(a, rand_neg());
398 BN_set_negative(b, rand_neg());
399 if (!(TEST_true(BN_mod(c, a, b, ctx))
400 && TEST_true(BN_div(d, e, a, b, ctx))
402 && TEST_true(BN_mul(c, d, b, ctx))
403 && TEST_true(BN_add(d, c, e))
404 && TEST_BN_eq(d, a)))
417 static const char *bn1strings[] = {
418 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
419 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
420 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
421 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
422 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
423 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
424 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
425 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
426 "0000000000000000000000000000000000000000000000000000000000000000",
427 "0000000000000000000000000000000000000000000000000000000000000000",
428 "0000000000000000000000000000000000000000000000000000000000000000",
429 "0000000000000000000000000000000000000000000000000000000000000000",
430 "0000000000000000000000000000000000000000000000000000000000000000",
431 "0000000000000000000000000000000000000000000000000000000000000000",
432 "0000000000000000000000000000000000000000000000000000000000000000",
433 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
437 static const char *bn2strings[] = {
438 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
439 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
440 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
441 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
442 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
443 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
444 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
445 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
446 "0000000000000000000000000000000000000000000000000000000000000000",
447 "0000000000000000000000000000000000000000000000000000000000000000",
448 "0000000000000000000000000000000000000000000000000000000000000000",
449 "0000000000000000000000000000000000000000000000000000000000000000",
450 "0000000000000000000000000000000000000000000000000000000000000000",
451 "0000000000000000000000000000000000000000000000000000000000000000",
452 "0000000000000000000000000000000000000000000000000000000000000000",
453 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
458 * Test constant-time modular exponentiation with 1024-bit inputs, which on
459 * x86_64 cause a different code branch to be taken.
461 static int test_modexp_mont5(void)
463 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
464 BIGNUM *b = NULL, *n = NULL, *c = NULL;
465 BN_MONT_CTX *mont = NULL;
468 if (!TEST_ptr(a = BN_new())
469 || !TEST_ptr(p = BN_new())
470 || !TEST_ptr(m = BN_new())
471 || !TEST_ptr(d = BN_new())
472 || !TEST_ptr(e = BN_new())
473 || !TEST_ptr(b = BN_new())
474 || !TEST_ptr(n = BN_new())
475 || !TEST_ptr(c = BN_new())
476 || !TEST_ptr(mont = BN_MONT_CTX_new()))
479 /* must be odd for montgomery */
480 if (!(TEST_true(BN_bntest_rand(m, 1024, 0, 1))
482 && TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
486 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
488 if (!TEST_BN_eq_one(d))
491 /* Regression test for carry bug in mulx4x_mont */
492 if (!(TEST_true(BN_hex2bn(&a,
493 "7878787878787878787878787878787878787878787878787878787878787878"
494 "7878787878787878787878787878787878787878787878787878787878787878"
495 "7878787878787878787878787878787878787878787878787878787878787878"
496 "7878787878787878787878787878787878787878787878787878787878787878"))
497 && TEST_true(BN_hex2bn(&b,
498 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
499 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
500 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
501 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81"))
502 && TEST_true(BN_hex2bn(&n,
503 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
504 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
505 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
506 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF"))))
509 if (!(TEST_true(BN_MONT_CTX_set(mont, n, ctx))
510 && TEST_true(BN_mod_mul_montgomery(c, a, b, mont, ctx))
511 && TEST_true(BN_mod_mul_montgomery(d, b, a, mont, ctx))
512 && TEST_BN_eq(c, d)))
515 /* Regression test for carry bug in sqr[x]8x_mont */
516 if (!(TEST_true(parse_bigBN(&n, bn1strings))
517 && TEST_true(parse_bigBN(&a, bn2strings))))
520 if (!(TEST_ptr(b = BN_dup(a))
521 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
522 && TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
523 && TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
524 && TEST_BN_eq(c, d)))
527 /* Regression test for carry bug in bn_sqrx8x_internal */
529 static const char *ahex[] = {
530 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
531 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
532 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
533 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
534 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
535 "9544D954000000006C0000000000000000000000000000000000000000000000",
536 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
537 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
538 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
539 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
540 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
541 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
544 static const char *nhex[] = {
545 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
546 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
547 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
548 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
549 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
550 "00000010000000006C0000000000000000000000000000000000000000000000",
551 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
552 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
553 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
554 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
555 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
556 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
560 if (!(TEST_true(parse_bigBN(&a, ahex))
561 && TEST_true(parse_bigBN(&n, nhex))))
565 if (!(TEST_ptr(b = BN_dup(a))
566 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))))
569 if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
570 || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
571 || !TEST_BN_eq(c, d))
574 /* Regression test for bug in BN_from_montgomery_word */
575 if (!(TEST_true(BN_hex2bn(&a,
576 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
577 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
578 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
579 && TEST_true(BN_hex2bn(&n,
580 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
581 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
582 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
583 && TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx))))
586 /* Regression test for bug in rsaz_1024_mul_avx2 */
587 if (!(TEST_true(BN_hex2bn(&a,
588 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
589 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
590 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
591 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
592 && TEST_true(BN_hex2bn(&b,
593 "2020202020202020202020202020202020202020202020202020202020202020"
594 "2020202020202020202020202020202020202020202020202020202020202020"
595 "20202020202020FF202020202020202020202020202020202020202020202020"
596 "2020202020202020202020202020202020202020202020202020202020202020"))
597 && TEST_true(BN_hex2bn(&n,
598 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
599 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
600 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
601 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF"))
602 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
603 && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))
604 && TEST_true(BN_mod_exp_mont(d, a, b, n, ctx, mont))
605 && TEST_BN_eq(c, d)))
609 * rsaz_1024_mul_avx2 expects fully-reduced inputs.
610 * BN_mod_exp_mont_consttime should reduce the input first.
612 if (!(TEST_true(BN_hex2bn(&a,
613 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
614 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
615 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
616 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
617 && TEST_true(BN_hex2bn(&b,
618 "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
619 "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
620 "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
621 "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E"))
622 && TEST_true(BN_hex2bn(&n,
623 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
624 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
625 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
626 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
627 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
628 && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))))
631 if (!TEST_BN_eq(c, d))
635 if (!TEST_true(BN_bntest_rand(p, 1024, 0, 0)))
638 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
639 || !TEST_BN_eq_zero(d))
643 * Craft an input whose Montgomery representation is 1, i.e., shorter
644 * than the modulus m, in order to test the const time precomputation
645 * scattering/gathering.
647 if (!(TEST_true(BN_one(a))
648 && TEST_true(BN_MONT_CTX_set(mont, m, ctx))))
650 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
651 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
652 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
653 || !TEST_BN_eq(a, d))
656 /* Finally, some regular test vectors. */
657 if (!(TEST_true(BN_bntest_rand(e, 1024, 0, 0))
658 && TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
659 && TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
660 && TEST_BN_eq(a, d)))
666 BN_MONT_CTX_free(mont);
678 #ifndef OPENSSL_NO_EC2M
679 static int test_gf2m_add(void)
681 BIGNUM *a = NULL, *b = NULL, *c = NULL;
684 if (!TEST_ptr(a = BN_new())
685 || !TEST_ptr(b = BN_new())
686 || !TEST_ptr(c = BN_new()))
689 for (i = 0; i < NUM0; i++) {
690 if (!(TEST_true(BN_rand(a, 512, 0, 0))
691 && TEST_ptr(BN_copy(b, BN_value_one()))))
693 BN_set_negative(a, rand_neg());
694 BN_set_negative(b, rand_neg());
695 if (!(TEST_true(BN_GF2m_add(c, a, b))
696 /* Test that two added values have the correct parity. */
697 && TEST_false((BN_is_odd(a) && BN_is_odd(c))
698 || (!BN_is_odd(a) && !BN_is_odd(c)))))
700 if (!(TEST_true(BN_GF2m_add(c, c, c))
701 /* Test that c + c = 0. */
702 && TEST_BN_eq_zero(c)))
713 static int test_gf2m_mod(void)
715 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL, *e = NULL;
718 if (!TEST_ptr(a = BN_new())
719 || !TEST_ptr(b[0] = BN_new())
720 || !TEST_ptr(b[1] = BN_new())
721 || !TEST_ptr(c = BN_new())
722 || !TEST_ptr(d = BN_new())
723 || !TEST_ptr(e = BN_new()))
726 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
727 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
730 for (i = 0; i < NUM0; i++) {
731 if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
733 for (j = 0; j < 2; j++) {
734 if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
735 && TEST_true(BN_GF2m_add(d, a, c))
736 && TEST_true(BN_GF2m_mod(e, d, b[j]))
737 /* Test that a + (a mod p) mod p == 0. */
738 && TEST_BN_eq_zero(e)))
753 static int test_gf2m_mul(void)
755 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
756 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
759 if (!TEST_ptr(a = BN_new())
760 || !TEST_ptr(b[0] = BN_new())
761 || !TEST_ptr(b[1] = BN_new())
762 || !TEST_ptr(c = BN_new())
763 || !TEST_ptr(d = BN_new())
764 || !TEST_ptr(e = BN_new())
765 || !TEST_ptr(f = BN_new())
766 || !TEST_ptr(g = BN_new())
767 || !TEST_ptr(h = BN_new()))
770 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
771 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
774 for (i = 0; i < NUM0; i++) {
775 if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))
776 && TEST_true(BN_bntest_rand(c, 1024, 0, 0))
777 && TEST_true(BN_bntest_rand(d, 1024, 0, 0))))
779 for (j = 0; j < 2; j++) {
780 if (!(TEST_true(BN_GF2m_mod_mul(e, a, c, b[j], ctx))
781 && TEST_true(BN_GF2m_add(f, a, d))
782 && TEST_true(BN_GF2m_mod_mul(g, f, c, b[j], ctx))
783 && TEST_true(BN_GF2m_mod_mul(h, d, c, b[j], ctx))
784 && TEST_true(BN_GF2m_add(f, e, g))
785 && TEST_true(BN_GF2m_add(f, f, h))
786 /* Test that (a+d)*c = a*c + d*c. */
787 && TEST_BN_eq_zero(f)))
806 static int test_gf2m_sqr(void)
808 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
811 if (!TEST_ptr(a = BN_new())
812 || !TEST_ptr(b[0] = BN_new())
813 || !TEST_ptr(b[1] = BN_new())
814 || !TEST_ptr(c = BN_new())
815 || !TEST_ptr(d = BN_new()))
818 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
819 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
822 for (i = 0; i < NUM0; i++) {
823 if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
825 for (j = 0; j < 2; j++) {
826 if (!(TEST_true(BN_GF2m_mod_sqr(c, a, b[j], ctx))
827 && TEST_true(BN_copy(d, a))
828 && TEST_true(BN_GF2m_mod_mul(d, a, d, b[j], ctx))
829 && TEST_true(BN_GF2m_add(d, c, d))
830 /* Test that a*a = a^2. */
831 && TEST_BN_eq_zero(d)))
845 static int test_gf2m_modinv(void)
847 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
850 if (!TEST_ptr(a = BN_new())
851 || !TEST_ptr(b[0] = BN_new())
852 || !TEST_ptr(b[1] = BN_new())
853 || !TEST_ptr(c = BN_new())
854 || !TEST_ptr(d = BN_new()))
857 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
858 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
861 for (i = 0; i < NUM0; i++) {
862 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
864 for (j = 0; j < 2; j++) {
865 if (!(TEST_true(BN_GF2m_mod_inv(c, a, b[j], ctx))
866 && TEST_true(BN_GF2m_mod_mul(d, a, c, b[j], ctx))
867 /* Test that ((1/a)*a) = 1. */
868 && TEST_BN_eq_one(d)))
882 static int test_gf2m_moddiv(void)
884 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
885 BIGNUM *e = NULL, *f = NULL;
888 if (!TEST_ptr(a = BN_new())
889 || !TEST_ptr(b[0] = BN_new())
890 || !TEST_ptr(b[1] = BN_new())
891 || !TEST_ptr(c = BN_new())
892 || !TEST_ptr(d = BN_new())
893 || !TEST_ptr(e = BN_new())
894 || !TEST_ptr(f = BN_new()))
897 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
898 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
901 for (i = 0; i < NUM0; i++) {
902 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
903 && TEST_true(BN_bntest_rand(c, 512, 0, 0))))
905 for (j = 0; j < 2; j++) {
906 if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))
907 && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))
908 && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))
909 /* Test that ((a/c)*c)/a = 1. */
910 && TEST_BN_eq_one(f)))
926 static int test_gf2m_modexp(void)
928 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
929 BIGNUM *e = NULL, *f = NULL;
932 if (!TEST_ptr(a = BN_new())
933 || !TEST_ptr(b[0] = BN_new())
934 || !TEST_ptr(b[1] = BN_new())
935 || !TEST_ptr(c = BN_new())
936 || !TEST_ptr(d = BN_new())
937 || !TEST_ptr(e = BN_new())
938 || !TEST_ptr(f = BN_new()))
941 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
942 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
945 for (i = 0; i < NUM0; i++) {
946 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
947 && TEST_true(BN_bntest_rand(c, 512, 0, 0))
948 && TEST_true(BN_bntest_rand(d, 512, 0, 0))))
950 for (j = 0; j < 2; j++) {
951 if (!(TEST_true(BN_GF2m_mod_exp(e, a, c, b[j], ctx))
952 && TEST_true(BN_GF2m_mod_exp(f, a, d, b[j], ctx))
953 && TEST_true(BN_GF2m_mod_mul(e, e, f, b[j], ctx))
954 && TEST_true(BN_add(f, c, d))
955 && TEST_true(BN_GF2m_mod_exp(f, a, f, b[j], ctx))
956 && TEST_true(BN_GF2m_add(f, e, f))
957 /* Test that a^(c+d)=a^c*a^d. */
958 && TEST_BN_eq_zero(f)))
974 static int test_gf2m_modsqrt(void)
976 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
977 BIGNUM *e = NULL, *f = NULL;
980 if (!TEST_ptr(a = BN_new())
981 || !TEST_ptr(b[0] = BN_new())
982 || !TEST_ptr(b[1] = BN_new())
983 || !TEST_ptr(c = BN_new())
984 || !TEST_ptr(d = BN_new())
985 || !TEST_ptr(e = BN_new())
986 || !TEST_ptr(f = BN_new()))
989 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
990 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
993 for (i = 0; i < NUM0; i++) {
994 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
997 for (j = 0; j < 2; j++) {
998 if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
999 && TEST_true(BN_GF2m_mod_sqrt(d, a, b[j], ctx))
1000 && TEST_true(BN_GF2m_mod_sqr(e, d, b[j], ctx))
1001 && TEST_true(BN_GF2m_add(f, c, e))
1002 /* Test that d^2 = a, where d = sqrt(a). */
1003 && TEST_BN_eq_zero(f)))
1019 static int test_gf2m_modsolvequad(void)
1021 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
1023 int i, j, s = 0, t, st = 0;
1025 if (!TEST_ptr(a = BN_new())
1026 || !TEST_ptr(b[0] = BN_new())
1027 || !TEST_ptr(b[1] = BN_new())
1028 || !TEST_ptr(c = BN_new())
1029 || !TEST_ptr(d = BN_new())
1030 || !TEST_ptr(e = BN_new()))
1033 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
1034 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
1037 for (i = 0; i < NUM0; i++) {
1038 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1040 for (j = 0; j < 2; j++) {
1041 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1044 if (!(TEST_true(BN_GF2m_mod_sqr(d, c, b[j], ctx))
1045 && TEST_true(BN_GF2m_add(d, c, d))
1046 && TEST_true(BN_GF2m_mod(e, a, b[j]))
1047 && TEST_true(BN_GF2m_add(e, e, d))
1049 * Test that solution of quadratic c
1050 * satisfies c^2 + c = a.
1052 && TEST_BN_eq_zero(e)))
1057 if (!TEST_int_ge(s, 0)) {
1058 TEST_info("%d tests found no roots; probably an error", NUM0);
1073 static int test_kronecker(void)
1075 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
1076 int i, legendre, kronecker, st = 0;
1078 if (!TEST_ptr(a = BN_new())
1079 || !TEST_ptr(b = BN_new())
1080 || !TEST_ptr(r = BN_new())
1081 || !TEST_ptr(t = BN_new()))
1085 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1086 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1087 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1088 * generate a random prime b and compare these values for a number of
1089 * random a's. (That is, we run the Solovay-Strassen primality test to
1090 * confirm that b is prime, except that we don't want to test whether b
1091 * is prime but whether BN_kronecker works.)
1094 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
1096 BN_set_negative(b, rand_neg());
1098 for (i = 0; i < NUM0; i++) {
1099 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1101 BN_set_negative(a, rand_neg());
1103 /* t := (|b|-1)/2 (note that b is odd) */
1104 if (!TEST_true(BN_copy(t, b)))
1106 BN_set_negative(t, 0);
1107 if (!TEST_true(BN_sub_word(t, 1)))
1109 if (!TEST_true(BN_rshift1(t, t)))
1111 /* r := a^t mod b */
1112 BN_set_negative(b, 0);
1114 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
1116 BN_set_negative(b, 1);
1118 if (BN_is_word(r, 1))
1120 else if (BN_is_zero(r))
1123 if (!TEST_true(BN_add_word(r, 1)))
1125 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
1126 TEST_info("Legendre symbol computation failed");
1132 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
1134 /* we actually need BN_kronecker(a, |b|) */
1135 if (BN_is_negative(a) && BN_is_negative(b))
1136 kronecker = -kronecker;
1138 if (!TEST_int_eq(legendre, kronecker))
1151 static int file_sum(STANZA *s)
1153 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
1157 if (!TEST_ptr(a = getBN(s, "A"))
1158 || !TEST_ptr(b = getBN(s, "B"))
1159 || !TEST_ptr(sum = getBN(s, "Sum"))
1160 || !TEST_ptr(ret = BN_new()))
1163 if (!TEST_true(BN_add(ret, a, b))
1164 || !equalBN("A + B", sum, ret)
1165 || !TEST_true(BN_sub(ret, sum, a))
1166 || !equalBN("Sum - A", b, ret)
1167 || !TEST_true(BN_sub(ret, sum, b))
1168 || !equalBN("Sum - B", a, ret))
1172 * Test that the functions work when |r| and |a| point to the same BIGNUM,
1173 * or when |r| and |b| point to the same BIGNUM.
1174 * There is no test for all of |r|, |a|, and |b| pointint to the same BIGNUM.
1176 if (!TEST_true(BN_copy(ret, a))
1177 || !TEST_true(BN_add(ret, ret, b))
1178 || !equalBN("A + B (r is a)", sum, ret)
1179 || !TEST_true(BN_copy(ret, b))
1180 || !TEST_true(BN_add(ret, a, ret))
1181 || !equalBN("A + B (r is b)", sum, ret)
1182 || !TEST_true(BN_copy(ret, sum))
1183 || !TEST_true(BN_sub(ret, ret, a))
1184 || !equalBN("Sum - A (r is a)", b, ret)
1185 || !TEST_true(BN_copy(ret, a))
1186 || !TEST_true(BN_sub(ret, sum, ret))
1187 || !equalBN("Sum - A (r is b)", b, ret)
1188 || !TEST_true(BN_copy(ret, sum))
1189 || !TEST_true(BN_sub(ret, ret, b))
1190 || !equalBN("Sum - B (r is a)", a, ret)
1191 || !TEST_true(BN_copy(ret, b))
1192 || !TEST_true(BN_sub(ret, sum, ret))
1193 || !equalBN("Sum - B (r is b)", a, ret))
1197 * Test BN_uadd() and BN_usub() with the prerequisites they are
1198 * documented as having. Note that these functions are frequently used
1199 * when the prerequisites don't hold. In those cases, they are supposed
1200 * to work as if the prerequisite hold, but we don't test that yet.
1202 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
1203 if (!TEST_true(BN_uadd(ret, a, b))
1204 || !equalBN("A +u B", sum, ret)
1205 || !TEST_true(BN_usub(ret, sum, a))
1206 || !equalBN("Sum -u A", b, ret)
1207 || !TEST_true(BN_usub(ret, sum, b))
1208 || !equalBN("Sum -u B", a, ret))
1211 * Test that the functions work when |r| and |a| point to the same
1212 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1213 * There is no test for all of |r|, |a|, and |b| pointint to the same
1216 if (!TEST_true(BN_copy(ret, a))
1217 || !TEST_true(BN_uadd(ret, ret, b))
1218 || !equalBN("A +u B (r is a)", sum, ret)
1219 || !TEST_true(BN_copy(ret, b))
1220 || !TEST_true(BN_uadd(ret, a, ret))
1221 || !equalBN("A +u B (r is b)", sum, ret)
1222 || !TEST_true(BN_copy(ret, sum))
1223 || !TEST_true(BN_usub(ret, ret, a))
1224 || !equalBN("Sum -u A (r is a)", b, ret)
1225 || !TEST_true(BN_copy(ret, a))
1226 || !TEST_true(BN_usub(ret, sum, ret))
1227 || !equalBN("Sum -u A (r is b)", b, ret)
1228 || !TEST_true(BN_copy(ret, sum))
1229 || !TEST_true(BN_usub(ret, ret, b))
1230 || !equalBN("Sum -u B (r is a)", a, ret)
1231 || !TEST_true(BN_copy(ret, b))
1232 || !TEST_true(BN_usub(ret, sum, ret))
1233 || !equalBN("Sum -u B (r is b)", a, ret))
1238 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1240 b_word = BN_get_word(b);
1241 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1242 if (!TEST_true(BN_copy(ret, a))
1243 || !TEST_true(BN_add_word(ret, b_word))
1244 || !equalBN("A + B (word)", sum, ret)
1245 || !TEST_true(BN_copy(ret, sum))
1246 || !TEST_true(BN_sub_word(ret, b_word))
1247 || !equalBN("Sum - B (word)", a, ret))
1260 static int file_lshift1(STANZA *s)
1262 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1263 BIGNUM *two = NULL, *remainder = NULL;
1266 if (!TEST_ptr(a = getBN(s, "A"))
1267 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1268 || !TEST_ptr(zero = BN_new())
1269 || !TEST_ptr(ret = BN_new())
1270 || !TEST_ptr(two = BN_new())
1271 || !TEST_ptr(remainder = BN_new()))
1276 if (!TEST_true(BN_set_word(two, 2))
1277 || !TEST_true(BN_add(ret, a, a))
1278 || !equalBN("A + A", lshift1, ret)
1279 || !TEST_true(BN_mul(ret, a, two, ctx))
1280 || !equalBN("A * 2", lshift1, ret)
1281 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1282 || !equalBN("LShift1 / 2", a, ret)
1283 || !equalBN("LShift1 % 2", zero, remainder)
1284 || !TEST_true(BN_lshift1(ret, a))
1285 || !equalBN("A << 1", lshift1, ret)
1286 || !TEST_true(BN_rshift1(ret, lshift1))
1287 || !equalBN("LShift >> 1", a, ret)
1288 || !TEST_true(BN_rshift1(ret, lshift1))
1289 || !equalBN("LShift >> 1", a, ret))
1292 /* Set the LSB to 1 and test rshift1 again. */
1293 if (!TEST_true(BN_set_bit(lshift1, 0))
1294 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1295 || !equalBN("(LShift1 | 1) / 2", a, ret)
1296 || !TEST_true(BN_rshift1(ret, lshift1))
1297 || !equalBN("(LShift | 1) >> 1", a, ret))
1312 static int file_lshift(STANZA *s)
1314 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1317 if (!TEST_ptr(a = getBN(s, "A"))
1318 || !TEST_ptr(lshift = getBN(s, "LShift"))
1319 || !TEST_ptr(ret = BN_new())
1320 || !getint(s, &n, "N"))
1323 if (!TEST_true(BN_lshift(ret, a, n))
1324 || !equalBN("A << N", lshift, ret)
1325 || !TEST_true(BN_rshift(ret, lshift, n))
1326 || !equalBN("A >> N", a, ret))
1337 static int file_rshift(STANZA *s)
1339 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1342 if (!TEST_ptr(a = getBN(s, "A"))
1343 || !TEST_ptr(rshift = getBN(s, "RShift"))
1344 || !TEST_ptr(ret = BN_new())
1345 || !getint(s, &n, "N"))
1348 if (!TEST_true(BN_rshift(ret, a, n))
1349 || !equalBN("A >> N", rshift, ret))
1352 /* If N == 1, try with rshift1 as well */
1354 if (!TEST_true(BN_rshift1(ret, a))
1355 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1367 static int file_square(STANZA *s)
1369 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1370 BIGNUM *remainder = NULL, *tmp = NULL;
1373 if (!TEST_ptr(a = getBN(s, "A"))
1374 || !TEST_ptr(square = getBN(s, "Square"))
1375 || !TEST_ptr(zero = BN_new())
1376 || !TEST_ptr(ret = BN_new())
1377 || !TEST_ptr(remainder = BN_new()))
1381 if (!TEST_true(BN_sqr(ret, a, ctx))
1382 || !equalBN("A^2", square, ret)
1383 || !TEST_true(BN_mul(ret, a, a, ctx))
1384 || !equalBN("A * A", square, ret)
1385 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1386 || !equalBN("Square / A", a, ret)
1387 || !equalBN("Square % A", zero, remainder))
1391 BN_set_negative(a, 0);
1392 if (!TEST_true(BN_sqrt(ret, square, ctx))
1393 || !equalBN("sqrt(Square)", a, ret))
1396 /* BN_sqrt should fail on non-squares and negative numbers. */
1397 if (!TEST_BN_eq_zero(square)) {
1398 if (!TEST_ptr(tmp = BN_new())
1399 || !TEST_true(BN_copy(tmp, square)))
1401 BN_set_negative(tmp, 1);
1403 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1407 BN_set_negative(tmp, 0);
1408 if (BN_add(tmp, tmp, BN_value_one()))
1410 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1427 static int file_product(STANZA *s)
1429 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1430 BIGNUM *remainder = NULL, *zero = NULL;
1433 if (!TEST_ptr(a = getBN(s, "A"))
1434 || !TEST_ptr(b = getBN(s, "B"))
1435 || !TEST_ptr(product = getBN(s, "Product"))
1436 || !TEST_ptr(ret = BN_new())
1437 || !TEST_ptr(remainder = BN_new())
1438 || !TEST_ptr(zero = BN_new()))
1443 if (!TEST_true(BN_mul(ret, a, b, ctx))
1444 || !equalBN("A * B", product, ret)
1445 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1446 || !equalBN("Product / A", b, ret)
1447 || !equalBN("Product % A", zero, remainder)
1448 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1449 || !equalBN("Product / B", a, ret)
1450 || !equalBN("Product % B", zero, remainder))
1464 static int file_quotient(STANZA *s)
1466 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1467 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1468 BN_ULONG b_word, ret_word;
1471 if (!TEST_ptr(a = getBN(s, "A"))
1472 || !TEST_ptr(b = getBN(s, "B"))
1473 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1474 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1475 || !TEST_ptr(ret = BN_new())
1476 || !TEST_ptr(ret2 = BN_new())
1477 || !TEST_ptr(nnmod = BN_new()))
1480 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1481 || !equalBN("A / B", quotient, ret)
1482 || !equalBN("A % B", remainder, ret2)
1483 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1484 || !TEST_true(BN_add(ret, ret, remainder))
1485 || !equalBN("Quotient * B + Remainder", a, ret))
1489 * Test with BN_mod_word() and BN_div_word() if the divisor is
1492 b_word = BN_get_word(b);
1493 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1494 BN_ULONG remainder_word = BN_get_word(remainder);
1496 assert(remainder_word != (BN_ULONG)-1);
1497 if (!TEST_ptr(BN_copy(ret, a)))
1499 ret_word = BN_div_word(ret, b_word);
1500 if (ret_word != remainder_word) {
1503 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1504 ret_word, remainder_word);
1506 TEST_error("Got A %% B (word) mismatch");
1510 if (!equalBN ("A / B (word)", quotient, ret))
1513 ret_word = BN_mod_word(a, b_word);
1514 if (ret_word != remainder_word) {
1517 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1518 ret_word, remainder_word);
1520 TEST_error("Got A %% B (word) mismatch");
1526 /* Test BN_nnmod. */
1527 if (!BN_is_negative(b)) {
1528 if (!TEST_true(BN_copy(nnmod, remainder))
1529 || (BN_is_negative(nnmod)
1530 && !TEST_true(BN_add(nnmod, nnmod, b)))
1531 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1532 || !equalBN("A % B (non-negative)", nnmod, ret))
1548 static int file_modmul(STANZA *s)
1550 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1553 if (!TEST_ptr(a = getBN(s, "A"))
1554 || !TEST_ptr(b = getBN(s, "B"))
1555 || !TEST_ptr(m = getBN(s, "M"))
1556 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1557 || !TEST_ptr(ret = BN_new()))
1560 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1561 || !equalBN("A * B (mod M)", mod_mul, ret))
1565 /* Reduce |a| and |b| and test the Montgomery version. */
1566 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1567 BIGNUM *a_tmp = BN_new();
1568 BIGNUM *b_tmp = BN_new();
1570 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1571 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1572 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1573 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1574 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1575 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1576 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1578 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1579 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1583 BN_MONT_CTX_free(mont);
1600 static int file_modexp(STANZA *s)
1602 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1603 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1606 if (!TEST_ptr(a = getBN(s, "A"))
1607 || !TEST_ptr(e = getBN(s, "E"))
1608 || !TEST_ptr(m = getBN(s, "M"))
1609 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1610 || !TEST_ptr(ret = BN_new())
1611 || !TEST_ptr(d = BN_new()))
1614 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1615 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1619 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1620 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1621 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1623 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1627 /* Regression test for carry propagation bug in sqr8x_reduction */
1628 BN_hex2bn(&a, "050505050505");
1629 BN_hex2bn(&b, "02");
1631 "4141414141414141414141274141414141414141414141414141414141414141"
1632 "4141414141414141414141414141414141414141414141414141414141414141"
1633 "4141414141414141414141800000000000000000000000000000000000000000"
1634 "0000000000000000000000000000000000000000000000000000000000000000"
1635 "0000000000000000000000000000000000000000000000000000000000000000"
1636 "0000000000000000000000000000000000000000000000000000000001");
1637 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1638 || !TEST_true(BN_mul(e, a, a, ctx))
1639 || !TEST_BN_eq(d, e))
1655 static int file_exp(STANZA *s)
1657 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1660 if (!TEST_ptr(a = getBN(s, "A"))
1661 || !TEST_ptr(e = getBN(s, "E"))
1662 || !TEST_ptr(exp = getBN(s, "Exp"))
1663 || !TEST_ptr(ret = BN_new()))
1666 if (!TEST_true(BN_exp(ret, a, e, ctx))
1667 || !equalBN("A ^ E", exp, ret))
1679 static int file_modsqrt(STANZA *s)
1681 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1684 if (!TEST_ptr(a = getBN(s, "A"))
1685 || !TEST_ptr(p = getBN(s, "P"))
1686 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1687 || !TEST_ptr(ret = BN_new())
1688 || !TEST_ptr(ret2 = BN_new()))
1691 /* There are two possible answers. */
1692 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1693 || !TEST_true(BN_sub(ret2, p, ret)))
1696 /* The first condition should NOT be a test. */
1697 if (BN_cmp(ret2, mod_sqrt) != 0
1698 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1711 static int file_gcd(STANZA *s)
1713 BIGNUM *a = NULL, *b = NULL, *gcd = NULL, *ret = NULL;
1716 if (!TEST_ptr(a = getBN(s, "A"))
1717 || !TEST_ptr(b = getBN(s, "B"))
1718 || !TEST_ptr(gcd = getBN(s, "GCD"))
1719 || !TEST_ptr(ret = BN_new()))
1722 if (!TEST_true(BN_gcd(ret, a, b, ctx))
1723 || !equalBN("gcd(A,B)", gcd, ret))
1735 static int test_bn2padded(void)
1738 uint8_t zeros[256], out[256], reference[128];
1739 BIGNUM *n = BN_new();
1742 /* Test edge case at 0. */
1745 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1747 memset(out, -1, sizeof(out));
1748 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1750 memset(zeros, 0, sizeof(zeros));
1751 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1754 /* Test a random numbers at various byte lengths. */
1755 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1756 # define TOP_BIT_ON 0
1757 # define BOTTOM_BIT_NOTOUCH 0
1758 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1760 if (!TEST_int_eq(BN_num_bytes(n), A) bytes
1761 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1763 /* Empty buffer should fail. */
1764 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1766 /* One byte short should fail. */
1767 if (BN_bn2bin_padded(out, bytes - 1, n))
1769 /* Exactly right size should encode. */
1770 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1771 || TEST_mem_eq(out, bytes, reference, bytes))
1773 /* Pad up one byte extra. */
1774 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1775 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1776 || !TEST_mem_eq(out, 1, zeros, 1))
1778 /* Pad up to 256. */
1779 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1780 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1782 || !TEST_mem_eq(out, sizseof(out) - bytes,
1783 zeros, sizeof(out) - bytes))
1796 static int test_dec2bn(void)
1801 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1802 || !TEST_BN_eq_word(bn, 0)
1803 || !TEST_BN_eq_zero(bn)
1804 || !TEST_BN_le_zero(bn)
1805 || !TEST_BN_ge_zero(bn)
1806 || !TEST_BN_even(bn))
1811 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1812 || !TEST_BN_eq_word(bn, 256)
1813 || !TEST_BN_ge_zero(bn)
1814 || !TEST_BN_gt_zero(bn)
1815 || !TEST_BN_ne_zero(bn)
1816 || !TEST_BN_even(bn))
1821 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1822 || !TEST_BN_abs_eq_word(bn, 42)
1823 || !TEST_BN_lt_zero(bn)
1824 || !TEST_BN_le_zero(bn)
1825 || !TEST_BN_ne_zero(bn)
1826 || !TEST_BN_even(bn))
1831 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1832 || !TEST_BN_eq_word(bn, 1)
1833 || !TEST_BN_ne_zero(bn)
1834 || !TEST_BN_gt_zero(bn)
1835 || !TEST_BN_ge_zero(bn)
1836 || !TEST_BN_eq_one(bn)
1837 || !TEST_BN_odd(bn))
1842 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1843 || !TEST_BN_eq_zero(bn)
1844 || !TEST_BN_ge_zero(bn)
1845 || !TEST_BN_le_zero(bn)
1846 || !TEST_BN_even(bn))
1851 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1852 || !TEST_BN_abs_eq_word(bn, 42)
1853 || !TEST_BN_ge_zero(bn)
1854 || !TEST_BN_gt_zero(bn)
1855 || !TEST_BN_ne_zero(bn)
1856 || !TEST_BN_even(bn))
1865 static int test_hex2bn(void)
1870 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1871 || !TEST_BN_eq_zero(bn)
1872 || !TEST_BN_ge_zero(bn)
1873 || !TEST_BN_even(bn))
1878 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1879 || !TEST_BN_eq_word(bn, 0x256)
1880 || !TEST_BN_ge_zero(bn)
1881 || !TEST_BN_gt_zero(bn)
1882 || !TEST_BN_ne_zero(bn)
1883 || !TEST_BN_even(bn))
1888 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1889 || !TEST_BN_abs_eq_word(bn, 0x42)
1890 || !TEST_BN_lt_zero(bn)
1891 || !TEST_BN_le_zero(bn)
1892 || !TEST_BN_ne_zero(bn)
1893 || !TEST_BN_even(bn))
1898 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1899 || !TEST_BN_eq_word(bn, 0xCB)
1900 || !TEST_BN_ge_zero(bn)
1901 || !TEST_BN_gt_zero(bn)
1902 || !TEST_BN_ne_zero(bn)
1903 || !TEST_BN_odd(bn))
1908 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1909 || !TEST_BN_eq_zero(bn)
1910 || !TEST_BN_ge_zero(bn)
1911 || !TEST_BN_le_zero(bn)
1912 || !TEST_BN_even(bn))
1917 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1918 || !TEST_BN_eq_word(bn, 0xabc)
1919 || !TEST_BN_ge_zero(bn)
1920 || !TEST_BN_gt_zero(bn)
1921 || !TEST_BN_ne_zero(bn)
1922 || !TEST_BN_even(bn))
1931 static int test_asc2bn(void)
1936 if (!TEST_ptr(bn = BN_new()))
1939 if (!TEST_true(BN_asc2bn(&bn, "0"))
1940 || !TEST_BN_eq_zero(bn)
1941 || !TEST_BN_ge_zero(bn))
1944 if (!TEST_true(BN_asc2bn(&bn, "256"))
1945 || !TEST_BN_eq_word(bn, 256)
1946 || !TEST_BN_ge_zero(bn))
1949 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1950 || !TEST_BN_abs_eq_word(bn, 42)
1951 || !TEST_BN_lt_zero(bn))
1954 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1955 || !TEST_BN_eq_word(bn, 0x1234)
1956 || !TEST_BN_ge_zero(bn))
1959 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1960 || !TEST_BN_eq_word(bn, 0x1234)
1961 || !TEST_BN_ge_zero(bn))
1964 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1965 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1966 || !TEST_BN_lt_zero(bn))
1969 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1970 || !TEST_BN_eq_zero(bn)
1971 || !TEST_BN_ge_zero(bn))
1974 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1975 || !TEST_BN_eq_word(bn, 123)
1976 || !TEST_BN_ge_zero(bn))
1985 static const MPITEST kMPITests[] = {
1986 {"0", "\x00\x00\x00\x00", 4},
1987 {"1", "\x00\x00\x00\x01\x01", 5},
1988 {"-1", "\x00\x00\x00\x01\x81", 5},
1989 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1990 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1991 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1994 static int test_mpi(int i)
1997 const MPITEST *test = &kMPITests[i];
1998 size_t mpi_len, mpi_len2;
2003 if (!TEST_ptr(bn = BN_new())
2004 || !TEST_true(BN_asc2bn(&bn, test->base10)))
2006 mpi_len = BN_bn2mpi(bn, NULL);
2007 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
2010 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
2011 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
2014 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
2017 if (!TEST_BN_eq(bn, bn2)) {
2029 static int test_rand(void)
2034 if (!TEST_ptr(bn = BN_new()))
2037 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
2038 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
2039 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
2040 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
2041 || !TEST_BN_eq_one(bn)
2042 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
2043 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
2044 || !TEST_BN_eq_one(bn)
2045 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
2046 || !TEST_BN_eq_word(bn, 3))
2056 * Run some statistical tests to provide a degree confidence that the
2057 * BN_rand_range() function works as expected. The test cases and
2058 * critical values are generated by the bn_rand_range script.
2060 * Each individual test is a Chi^2 goodness of fit for a specified number
2061 * of samples and range. The samples are assumed to be independent and
2062 * that they are from a discrete uniform distribution.
2064 * Some of these individual tests are expected to fail, the success/failure
2065 * of each is an independent Bernoulli trial. The number of such successes
2066 * will form a binomial distribution. The count of the successes is compared
2067 * against a precomputed critical value to determine the overall outcome.
2069 struct rand_range_case {
2071 unsigned int iterations;
2075 #include "bn_rand_range.h"
2077 static int test_rand_range_single(size_t n)
2079 const unsigned int range = rand_range_cases[n].range;
2080 const unsigned int iterations = rand_range_cases[n].iterations;
2081 const double critical = rand_range_cases[n].critical;
2082 const double expected = iterations / (double)range;
2084 BIGNUM *rng = NULL, *val = NULL;
2089 if (!TEST_ptr(counts = OPENSSL_zalloc(sizeof(*counts) * range))
2090 || !TEST_ptr(rng = BN_new())
2091 || !TEST_ptr(val = BN_new())
2092 || !TEST_true(BN_set_word(rng, range)))
2094 for (i = 0; i < iterations; i++) {
2095 if (!TEST_true(BN_rand_range(val, rng))
2096 || !TEST_uint_lt(v = (unsigned int)BN_get_word(val), range))
2101 for (i = 0; i < range; i++) {
2102 const double delta = counts[i] - expected;
2103 sum += delta * delta;
2107 if (sum > critical) {
2108 TEST_info("Chi^2 test negative %.4f > %4.f", sum, critical);
2109 TEST_note("test case %zu range %u iterations %u", n + 1, range,
2118 OPENSSL_free(counts);
2122 static int test_rand_range(void)
2127 for (i = 0; i < OSSL_NELEM(rand_range_cases); i++)
2128 n_success += test_rand_range_single(i);
2129 if (TEST_int_ge(n_success, binomial_critical))
2131 TEST_note("This test is expected to fail by chance 0.01%% of the time.");
2135 static int test_negzero(void)
2137 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
2138 BIGNUM *numerator = NULL, *denominator = NULL;
2139 int consttime, st = 0;
2141 if (!TEST_ptr(a = BN_new())
2142 || !TEST_ptr(b = BN_new())
2143 || !TEST_ptr(c = BN_new())
2144 || !TEST_ptr(d = BN_new()))
2147 /* Test that BN_mul never gives negative zero. */
2148 if (!TEST_true(BN_set_word(a, 1)))
2150 BN_set_negative(a, 1);
2152 if (!TEST_true(BN_mul(c, a, b, ctx)))
2154 if (!TEST_BN_eq_zero(c)
2155 || !TEST_BN_ge_zero(c))
2158 for (consttime = 0; consttime < 2; consttime++) {
2159 if (!TEST_ptr(numerator = BN_new())
2160 || !TEST_ptr(denominator = BN_new()))
2163 BN_set_flags(numerator, BN_FLG_CONSTTIME);
2164 BN_set_flags(denominator, BN_FLG_CONSTTIME);
2166 /* Test that BN_div never gives negative zero in the quotient. */
2167 if (!TEST_true(BN_set_word(numerator, 1))
2168 || !TEST_true(BN_set_word(denominator, 2)))
2170 BN_set_negative(numerator, 1);
2171 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
2172 || !TEST_BN_eq_zero(a)
2173 || !TEST_BN_ge_zero(a))
2176 /* Test that BN_div never gives negative zero in the remainder. */
2177 if (!TEST_true(BN_set_word(denominator, 1))
2178 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
2179 || !TEST_BN_eq_zero(b)
2180 || !TEST_BN_ge_zero(b))
2183 BN_free(denominator);
2184 numerator = denominator = NULL;
2187 /* Test that BN_set_negative will not produce a negative zero. */
2189 BN_set_negative(a, 1);
2190 if (BN_is_negative(a))
2200 BN_free(denominator);
2204 static int test_badmod(void)
2206 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
2207 BN_MONT_CTX *mont = NULL;
2210 if (!TEST_ptr(a = BN_new())
2211 || !TEST_ptr(b = BN_new())
2212 || !TEST_ptr(zero = BN_new())
2213 || !TEST_ptr(mont = BN_MONT_CTX_new()))
2217 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
2221 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
2225 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
2229 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2234 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2239 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
2243 /* Some operations also may not be used with an even modulus. */
2244 if (!TEST_true(BN_set_word(b, 16)))
2247 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
2251 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2256 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2266 BN_MONT_CTX_free(mont);
2270 static int test_expmodzero(void)
2272 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
2275 if (!TEST_ptr(zero = BN_new())
2276 || !TEST_ptr(a = BN_new())
2277 || !TEST_ptr(r = BN_new()))
2281 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
2282 || !TEST_BN_eq_zero(r)
2283 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
2285 || !TEST_BN_eq_zero(r)
2286 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
2289 || !TEST_BN_eq_zero(r)
2290 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
2291 BN_value_one(), NULL, NULL))
2292 || !TEST_BN_eq_zero(r))
2303 static int test_expmodone(void)
2306 BIGNUM *r = BN_new();
2307 BIGNUM *a = BN_new();
2308 BIGNUM *p = BN_new();
2309 BIGNUM *m = BN_new();
2316 || !TEST_true(BN_set_word(a, 1))
2317 || !TEST_true(BN_set_word(p, 0))
2318 || !TEST_true(BN_set_word(m, 1)))
2321 /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
2322 for (i = 0; i < 2; i++) {
2323 if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
2324 || !TEST_BN_eq_zero(r)
2325 || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
2326 || !TEST_BN_eq_zero(r)
2327 || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
2328 || !TEST_BN_eq_zero(r)
2329 || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
2330 || !TEST_BN_eq_zero(r)
2331 || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
2332 || !TEST_BN_eq_zero(r)
2333 || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
2334 || !TEST_BN_eq_zero(r))
2336 /* Repeat for r = 1 ^ 0 mod -1 */
2338 BN_set_negative(m, 1);
2350 static int test_smallprime(int kBits)
2355 if (!TEST_ptr(r = BN_new()))
2359 if (!TEST_false(BN_generate_prime_ex(r, kBits, 0,
2363 if (!TEST_true(BN_generate_prime_ex(r, kBits, 0,
2365 || !TEST_int_eq(BN_num_bits(r), kBits))
2375 static int test_smallsafeprime(int kBits)
2380 if (!TEST_ptr(r = BN_new()))
2383 if (kBits <= 5 && kBits != 3) {
2384 if (!TEST_false(BN_generate_prime_ex(r, kBits, 1,
2388 if (!TEST_true(BN_generate_prime_ex(r, kBits, 1,
2390 || !TEST_int_eq(BN_num_bits(r), kBits))
2400 static int primes[] = { 2, 3, 5, 7, 17863 };
2402 static int test_is_prime(int i)
2408 if (!TEST_ptr(r = BN_new()))
2411 for (trial = 0; trial <= 1; ++trial) {
2412 if (!TEST_true(BN_set_word(r, primes[i]))
2413 || !TEST_int_eq(BN_check_prime(r, ctx, NULL),
2424 static int not_primes[] = { -1, 0, 1, 4 };
2426 static int test_not_prime(int i)
2432 if (!TEST_ptr(r = BN_new()))
2435 for (trial = 0; trial <= 1; ++trial) {
2436 if (!TEST_true(BN_set_word(r, not_primes[i]))
2437 || !TEST_false(BN_check_prime(r, ctx, NULL)))
2447 static int test_ctx_set_ct_flag(BN_CTX *c)
2454 for (i = 0; i < OSSL_NELEM(b); i++) {
2455 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2458 BN_set_flags(b[i], BN_FLG_CONSTTIME);
2467 static int test_ctx_check_ct_flag(BN_CTX *c)
2474 for (i = 0; i < OSSL_NELEM(b); i++) {
2475 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2477 if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
2487 static int test_ctx_consttime_flag(void)
2490 * The constant-time flag should not "leak" among BN_CTX frames:
2492 * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
2493 * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
2494 * from the frame before ending it.
2495 * - test_ctx_check_ct_flag() then starts a new frame and gets a
2496 * number of BIGNUMs from it. In absence of leaks, none of the
2497 * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
2499 * In actual BN_CTX usage inside libcrypto the leak could happen at
2500 * any depth level in the BN_CTX stack, with varying results
2501 * depending on the patterns of sibling trees of nested function
2502 * calls sharing the same BN_CTX object, and the effect of
2503 * unintended BN_FLG_CONSTTIME on the called BN_* functions.
2505 * This simple unit test abstracts away this complexity and verifies
2506 * that the leak does not happen between two sibling functions
2507 * sharing the same BN_CTX object at the same level of nesting.
2510 BN_CTX *nctx = NULL;
2511 BN_CTX *sctx = NULL;
2515 if (!TEST_ptr(nctx = BN_CTX_new())
2516 || !TEST_ptr(sctx = BN_CTX_secure_new()))
2519 for (i = 0; i < 2; i++) {
2520 BN_CTX *c = i == 0 ? nctx : sctx;
2521 if (!TEST_true(test_ctx_set_ct_flag(c))
2522 || !TEST_true(test_ctx_check_ct_flag(c)))
2533 static int test_gcd_prime(void)
2535 BIGNUM *a = NULL, *b = NULL, *gcd = NULL;
2538 if (!TEST_ptr(a = BN_new())
2539 || !TEST_ptr(b = BN_new())
2540 || !TEST_ptr(gcd = BN_new()))
2543 if (!TEST_true(BN_generate_prime_ex(a, 1024, 0, NULL, NULL, NULL)))
2545 for (i = 0; i < NUM0; i++) {
2546 if (!TEST_true(BN_generate_prime_ex(b, 1024, 0,
2548 || !TEST_true(BN_gcd(gcd, a, b, ctx))
2549 || !TEST_true(BN_is_one(gcd)))
2561 typedef struct mod_exp_test_st
2569 static const MOD_EXP_TEST ModExpTests[] = {
2570 /* original test vectors for rsaz_512_sqr bug, by OSS-Fuzz */
2572 "1166180238001879113042182292626169621106255558914000595999312084"
2573 "4627946820899490684928760491249738643524880720584249698100907201"
2574 "002086675047927600340800371",
2575 "8000000000000000000000000000000000000000000000000000000000000000"
2576 "0000000000000000000000000000000000000000000000000000000000000000"
2578 "1340780792684523720980737645613191762604395855615117867483316354"
2579 "3294276330515137663421134775482798690129946803802212663956180562"
2580 "088664022929883876655300863",
2581 "8243904058268085430037326628480645845409758077568738532059032482"
2582 "8294114415890603594730158120426756266457928475330450251339773498"
2583 "26758407619521544102068438"
2586 "4974270041410803822078866696159586946995877618987010219312844726"
2587 "0284386121835740784990869050050504348861513337232530490826340663"
2588 "197278031692737429054",
2589 "4974270041410803822078866696159586946995877428188754995041148539"
2590 "1663243362592271353668158565195557417149981094324650322556843202"
2591 "946445882670777892608",
2592 "1340780716511420227215592830971452482815377482627251725537099028"
2593 "4429769497230131760206012644403029349547320953206103351725462999"
2594 "947509743623340557059752191",
2595 "5296244594780707015616522701706118082963369547253192207884519362"
2596 "1767869984947542695665420219028522815539559194793619684334900442"
2597 "49304558011362360473525933"
2599 /* test vectors for rsaz_512_srq bug, with rcx/rbx=1 */
2600 { /* between first and second iteration */
2601 "5148719036160389201525610950887605325980251964889646556085286545"
2602 "3931548809178823413169359635978762036512397113080988070677858033"
2603 "36463909753993540214027190",
2604 "6703903964971298549787012499102923063739682910296196688861780721"
2605 "8608820150367734884009371490834517138450159290932430254268769414"
2606 "05973284973216824503042158",
2607 "6703903964971298549787012499102923063739682910296196688861780721"
2608 "8608820150367734884009371490834517138450159290932430254268769414"
2609 "05973284973216824503042159",
2612 { /* between second and third iteration */
2613 "8908340854353752577419678771330460827942371434853054158622636544"
2614 "8151360109722890949471912566649465436296659601091730745087014189"
2615 "2672764191218875181826063",
2616 "6703903964971298549787012499102923063739682910296196688861780721"
2617 "8608820150367734884009371490834517138450159290932430254268769414"
2618 "05973284973216824503042158",
2619 "6703903964971298549787012499102923063739682910296196688861780721"
2620 "8608820150367734884009371490834517138450159290932430254268769414"
2621 "05973284973216824503042159",
2624 { /* between third and fourth iteration */
2625 "3427446396505596330634350984901719674479522569002785244080234738"
2626 "4288743635435746136297299366444548736533053717416735379073185344"
2627 "26985272974404612945608761",
2628 "6703903964971298549787012499102923063739682910296196688861780721"
2629 "8608820150367734884009371490834517138450159290932430254268769414"
2630 "05973284973216824503042158",
2631 "6703903964971298549787012499102923063739682910296196688861780721"
2632 "8608820150367734884009371490834517138450159290932430254268769414"
2633 "05973284973216824503042159",
2636 { /* between fourth and fifth iteration */
2637 "3472743044917564564078857826111874560045331237315597383869652985"
2638 "6919870028890895988478351133601517365908445058405433832718206902"
2639 "4088133164805266956353542",
2640 "6703903964971298549787012499102923063739682910296196688861780721"
2641 "8608820150367734884009371490834517138450159290932430254268769414"
2642 "05973284973216824503042158",
2643 "6703903964971298549787012499102923063739682910296196688861780721"
2644 "8608820150367734884009371490834517138450159290932430254268769414"
2645 "05973284973216824503042159",
2648 { /* between fifth and sixth iteration */
2649 "3608632990153469264412378349742339216742409743898601587274768025"
2650 "0110772032985643555192767717344946174122842255204082586753499651"
2651 "14483434992887431333675068",
2652 "6703903964971298549787012499102923063739682910296196688861780721"
2653 "8608820150367734884009371490834517138450159290932430254268769414"
2654 "05973284973216824503042158",
2655 "6703903964971298549787012499102923063739682910296196688861780721"
2656 "8608820150367734884009371490834517138450159290932430254268769414"
2657 "05973284973216824503042159",
2660 { /* between sixth and seventh iteration */
2661 "8455374370234070242910508226941981520235709767260723212165264877"
2662 "8689064388017521524568434328264431772644802567028663962962025746"
2663 "9283458217850119569539086",
2664 "6703903964971298549787012499102923063739682910296196688861780721"
2665 "8608820150367734884009371490834517138450159290932430254268769414"
2666 "05973284973216824503042158",
2667 "6703903964971298549787012499102923063739682910296196688861780721"
2668 "8608820150367734884009371490834517138450159290932430254268769414"
2669 "05973284973216824503042159",
2672 { /* between seventh and eighth iteration */
2673 "5155371529688532178421209781159131443543419764974688878527112131"
2674 "7446518205609427412336183157918981038066636807317733319323257603"
2675 "04416292040754017461076359",
2676 "1005585594745694782468051874865438459560952436544429503329267108"
2677 "2791323022555160232601405723625177570767523893639864538140315412"
2678 "108959927459825236754563832",
2679 "1005585594745694782468051874865438459560952436544429503329267108"
2680 "2791323022555160232601405723625177570767523893639864538140315412"
2681 "108959927459825236754563833",
2684 /* test vectors for rsaz_512_srq bug, with rcx/rbx=2 */
2685 { /* between first and second iteration */
2686 "3155666506033786929967309937640790361084670559125912405342594979"
2687 "4345142818528956285490897841406338022378565972533508820577760065"
2688 "58494345853302083699912572",
2689 "6703903964971298549787012499102923063739682910296196688861780721"
2690 "8608820150367734884009371490834517138450159290932430254268769414"
2691 "05973284973216824503042158",
2692 "6703903964971298549787012499102923063739682910296196688861780721"
2693 "8608820150367734884009371490834517138450159290932430254268769414"
2694 "05973284973216824503042159",
2697 { /* between second and third iteration */
2698 "3789819583801342198190405714582958759005991915505282362397087750"
2699 "4213544724644823098843135685133927198668818185338794377239590049"
2700 "41019388529192775771488319",
2701 "6703903964971298549787012499102923063739682910296196688861780721"
2702 "8608820150367734884009371490834517138450159290932430254268769414"
2703 "05973284973216824503042158",
2704 "6703903964971298549787012499102923063739682910296196688861780721"
2705 "8608820150367734884009371490834517138450159290932430254268769414"
2706 "05973284973216824503042159",
2709 { /* between third and forth iteration */
2710 "4695752552040706867080542538786056470322165281761525158189220280"
2711 "4025547447667484759200742764246905647644662050122968912279199065"
2712 "48065034299166336940507214",
2713 "6703903964971298549787012499102923063739682910296196688861780721"
2714 "8608820150367734884009371490834517138450159290932430254268769414"
2715 "05973284973216824503042158",
2716 "6703903964971298549787012499102923063739682910296196688861780721"
2717 "8608820150367734884009371490834517138450159290932430254268769414"
2718 "05973284973216824503042159",
2721 { /* between forth and fifth iteration */
2722 "2159140240970485794188159431017382878636879856244045329971239574"
2723 "8919691133560661162828034323196457386059819832804593989740268964"
2724 "74502911811812651475927076",
2725 "6703903964971298549787012499102923063739682910296196688861780721"
2726 "8608820150367734884009371490834517138450159290932430254268769414"
2727 "05973284973216824503042158",
2728 "6703903964971298549787012499102923063739682910296196688861780721"
2729 "8608820150367734884009371490834517138450159290932430254268769414"
2730 "05973284973216824503042159",
2733 { /* between fifth and sixth iteration */
2734 "5239312332984325668414624633307915097111691815000872662334695514"
2735 "5436533521392362443557163429336808208137221322444780490437871903"
2736 "99972784701334569424519255",
2737 "6703903964971298549787012499102923063739682910296196688861780721"
2738 "8608820150367734884009371490834517138450159290932430254268769414"
2739 "05973284973216824503042158",
2740 "6703903964971298549787012499102923063739682910296196688861780721"
2741 "8608820150367734884009371490834517138450159290932430254268769414"
2742 "05973284973216824503042159",
2745 { /* between sixth and seventh iteration */
2746 "1977953647322612860406858017869125467496941904523063466791308891"
2747 "1172796739058531929470539758361774569875505293428856181093904091"
2748 "33788264851714311303725089",
2749 "6703903964971298549787012499102923063739682910296196688861780721"
2750 "8608820150367734884009371490834517138450159290932430254268769414"
2751 "05973284973216824503042158",
2752 "6703903964971298549787012499102923063739682910296196688861780721"
2753 "8608820150367734884009371490834517138450159290932430254268769414"
2754 "05973284973216824503042159",
2757 { /* between seventh and eighth iteration */
2758 "6456987954117763835533395796948878140715006860263624787492985786"
2759 "8514630216966738305923915688821526449499763719943997120302368211"
2760 "04813318117996225041943964",
2761 "1340780792994259709957402499820584612747936582059239337772356144"
2762 "3721764030073546976801874298166903427690031858186486050853753882"
2763 "811946551499689575296532556",
2764 "1340780792994259709957402499820584612747936582059239337772356144"
2765 "3721764030073546976801874298166903427690031858186486050853753882"
2766 "811946551499689575296532557",
2771 static int test_mod_exp(int i)
2773 const MOD_EXP_TEST *test = &ModExpTests[i];
2775 BIGNUM* result = NULL;
2776 BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
2779 if (!TEST_ptr(result = BN_new())
2780 || !TEST_true(BN_dec2bn(&base, test->base))
2781 || !TEST_true(BN_dec2bn(&exponent, test->exp))
2782 || !TEST_true(BN_dec2bn(&modulo, test->mod)))
2785 if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
2788 if (!TEST_ptr(s = BN_bn2dec(result)))
2791 if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
2805 static int test_mod_exp_consttime(int i)
2807 const MOD_EXP_TEST *test = &ModExpTests[i];
2809 BIGNUM* result = NULL;
2810 BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
2813 if (!TEST_ptr(result = BN_new())
2814 || !TEST_true(BN_dec2bn(&base, test->base))
2815 || !TEST_true(BN_dec2bn(&exponent, test->exp))
2816 || !TEST_true(BN_dec2bn(&modulo, test->mod)))
2819 BN_set_flags(base, BN_FLG_CONSTTIME);
2820 BN_set_flags(exponent, BN_FLG_CONSTTIME);
2821 BN_set_flags(modulo, BN_FLG_CONSTTIME);
2823 if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
2826 if (!TEST_ptr(s = BN_bn2dec(result)))
2829 if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
2843 static int file_test_run(STANZA *s)
2845 static const FILETEST filetests[] = {
2847 {"LShift1", file_lshift1},
2848 {"LShift", file_lshift},
2849 {"RShift", file_rshift},
2850 {"Square", file_square},
2851 {"Product", file_product},
2852 {"Quotient", file_quotient},
2853 {"ModMul", file_modmul},
2854 {"ModExp", file_modexp},
2856 {"ModSqrt", file_modsqrt},
2859 int numtests = OSSL_NELEM(filetests);
2860 const FILETEST *tp = filetests;
2862 for ( ; --numtests >= 0; tp++) {
2863 if (findattr(s, tp->name) != NULL) {
2865 TEST_info("%s:%d: Failed %s test",
2866 s->test_file, s->start, tp->name);
2872 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2876 static int run_file_tests(int i)
2879 char *testfile = test_get_argument(i);
2882 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2884 if (!test_start_file(s, testfile)) {
2889 /* Read test file. */
2890 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2891 if (s->numpairs == 0)
2893 if (!file_test_run(s))
2896 test_clearstanza(s);
2905 typedef enum OPTION_choice {
2908 OPT_STOCHASTIC_TESTS,
2912 const OPTIONS *test_get_options(void)
2914 static const OPTIONS test_options[] = {
2915 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
2916 { "stochastic", OPT_STOCHASTIC_TESTS, '-', "Run stochastic tests" },
2917 { OPT_HELP_STR, 1, '-',
2918 "file\tFile to run tests on. Normal tests are not run\n" },
2921 return test_options;
2924 int setup_tests(void)
2927 int n, stochastic = 0;
2929 while ((o = opt_next()) != OPT_EOF) {
2931 case OPT_STOCHASTIC_TESTS:
2934 case OPT_TEST_CASES:
2941 n = test_get_argument_count();
2943 if (!TEST_ptr(ctx = BN_CTX_new()))
2948 ADD_TEST(test_div_recip);
2949 ADD_ALL_TESTS(test_signed_mod_replace_ab, OSSL_NELEM(signed_mod_tests));
2950 ADD_ALL_TESTS(test_signed_mod_replace_ba, OSSL_NELEM(signed_mod_tests));
2952 ADD_TEST(test_modexp_mont5);
2953 ADD_TEST(test_kronecker);
2954 ADD_TEST(test_rand);
2955 ADD_TEST(test_bn2padded);
2956 ADD_TEST(test_dec2bn);
2957 ADD_TEST(test_hex2bn);
2958 ADD_TEST(test_asc2bn);
2959 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2960 ADD_TEST(test_negzero);
2961 ADD_TEST(test_badmod);
2962 ADD_TEST(test_expmodzero);
2963 ADD_TEST(test_expmodone);
2964 ADD_ALL_TESTS(test_smallprime, 16);
2965 ADD_ALL_TESTS(test_smallsafeprime, 16);
2966 ADD_TEST(test_swap);
2967 ADD_TEST(test_ctx_consttime_flag);
2968 #ifndef OPENSSL_NO_EC2M
2969 ADD_TEST(test_gf2m_add);
2970 ADD_TEST(test_gf2m_mod);
2971 ADD_TEST(test_gf2m_mul);
2972 ADD_TEST(test_gf2m_sqr);
2973 ADD_TEST(test_gf2m_modinv);
2974 ADD_TEST(test_gf2m_moddiv);
2975 ADD_TEST(test_gf2m_modexp);
2976 ADD_TEST(test_gf2m_modsqrt);
2977 ADD_TEST(test_gf2m_modsolvequad);
2979 ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
2980 ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
2981 ADD_TEST(test_gcd_prime);
2982 ADD_ALL_TESTS(test_mod_exp, (int)OSSL_NELEM(ModExpTests));
2983 ADD_ALL_TESTS(test_mod_exp_consttime, (int)OSSL_NELEM(ModExpTests));
2985 ADD_TEST(test_rand_range);
2987 ADD_ALL_TESTS(run_file_tests, n);
2992 void cleanup_tests(void)