2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
15 #include <openssl/bn.h>
16 #include <openssl/crypto.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
19 #include "internal/nelem.h"
20 #include "internal/numbers.h"
23 #ifdef OPENSSL_SYS_WINDOWS
24 # define strcasecmp _stricmp
28 * Things in boring, not in openssl. TODO we should add them.
30 #define HAVE_BN_SQRT 0
32 typedef struct filetest_st {
34 int (*func)(STANZA *s);
37 typedef struct mpitest_st {
43 static const int NUM0 = 100; /* number of tests */
44 static const int NUM1 = 50; /* additional tests for some functions */
48 * Polynomial coefficients used in GFM tests.
50 #ifndef OPENSSL_NO_EC2M
51 static int p0[] = { 163, 7, 6, 3, 0, -1 };
52 static int p1[] = { 193, 15, 0, -1 };
56 * Look for |key| in the stanza and return it or NULL if not found.
58 static const char *findattr(STANZA *s, const char *key)
63 for ( ; --i >= 0; pp++)
64 if (strcasecmp(pp->key, key) == 0)
70 * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
72 static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
74 char *bigstring = glue_strings(bn_strings, NULL);
75 int ret = BN_hex2bn(out, bigstring);
77 OPENSSL_free(bigstring);
82 * Parse BIGNUM, return number of bytes parsed.
84 static int parseBN(BIGNUM **out, const char *in)
87 return BN_hex2bn(out, in);
90 static int parsedecBN(BIGNUM **out, const char *in)
93 return BN_dec2bn(out, in);
96 static BIGNUM *getBN(STANZA *s, const char *attribute)
101 if ((hex = findattr(s, attribute)) == NULL) {
102 TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
106 if (parseBN(&ret, hex) != (int)strlen(hex)) {
107 TEST_error("Could not decode '%s'", hex);
113 static int getint(STANZA *s, int *out, const char *attribute)
119 if (!TEST_ptr(ret = getBN(s, attribute))
120 || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
130 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
132 if (BN_cmp(expected, actual) == 0)
135 TEST_error("unexpected %s value", op);
136 TEST_BN_eq(expected, actual);
141 * Return a "random" flag for if a BN should be negated.
143 static int rand_neg(void)
145 static unsigned int neg = 0;
146 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
148 return sign[(neg++) % 8];
151 static int test_swap(void)
153 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
154 int top, cond, st = 0;
156 if (!TEST_ptr(a = BN_new())
157 || !TEST_ptr(b = BN_new())
158 || !TEST_ptr(c = BN_new())
159 || !TEST_ptr(d = BN_new()))
162 if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))
163 && TEST_true(BN_bntest_rand(b, 1024, 1, 0))
164 && TEST_ptr(BN_copy(c, a))
165 && TEST_ptr(BN_copy(d, b))))
167 top = BN_num_bits(a) / BN_BITS2;
171 if (!equalBN("swap", a, d)
172 || !equalBN("swap", b, c))
175 /* conditional swap: true */
177 BN_consttime_swap(cond, a, b, top);
178 if (!equalBN("cswap true", a, c)
179 || !equalBN("cswap true", b, d))
182 /* conditional swap: false */
184 BN_consttime_swap(cond, a, b, top);
185 if (!equalBN("cswap false", a, c)
186 || !equalBN("cswap false", b, d))
189 /* same tests but checking flag swap */
190 BN_set_flags(a, BN_FLG_CONSTTIME);
193 if (!equalBN("swap, flags", a, d)
194 || !equalBN("swap, flags", b, c)
195 || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
196 || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
200 BN_consttime_swap(cond, a, b, top);
201 if (!equalBN("cswap true, flags", a, c)
202 || !equalBN("cswap true, flags", b, d)
203 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
204 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
208 BN_consttime_swap(cond, a, b, top);
209 if (!equalBN("cswap false, flags", a, c)
210 || !equalBN("cswap false, flags", b, d)
211 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
212 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
224 static int test_sub(void)
226 BIGNUM *a = NULL, *b = NULL, *c = NULL;
229 if (!TEST_ptr(a = BN_new())
230 || !TEST_ptr(b = BN_new())
231 || !TEST_ptr(c = BN_new()))
234 for (i = 0; i < NUM0 + NUM1; i++) {
236 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0)))
237 && TEST_ptr(BN_copy(b, a))
238 && TEST_int_ne(BN_set_bit(a, i), 0)
239 && TEST_true(BN_add_word(b, i)))
242 if (!TEST_true(BN_bntest_rand(b, 400 + i - NUM1, 0, 0)))
244 BN_set_negative(a, rand_neg());
245 BN_set_negative(b, rand_neg());
247 if (!(TEST_true(BN_sub(c, a, b))
248 && TEST_true(BN_add(c, c, b))
249 && TEST_true(BN_sub(c, c, a))
250 && TEST_BN_eq_zero(c)))
261 static int test_div_recip(void)
263 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
264 BN_RECP_CTX *recp = NULL;
267 if (!TEST_ptr(a = BN_new())
268 || !TEST_ptr(b = BN_new())
269 || !TEST_ptr(c = BN_new())
270 || !TEST_ptr(d = BN_new())
271 || !TEST_ptr(e = BN_new())
272 || !TEST_ptr(recp = BN_RECP_CTX_new()))
275 for (i = 0; i < NUM0 + NUM1; i++) {
277 if (!(TEST_true(BN_bntest_rand(a, 400, 0, 0))
278 && TEST_ptr(BN_copy(b, a))
279 && TEST_true(BN_lshift(a, a, i))
280 && TEST_true(BN_add_word(a, i))))
283 if (!(TEST_true(BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0))))
286 BN_set_negative(a, rand_neg());
287 BN_set_negative(b, rand_neg());
288 if (!(TEST_true(BN_RECP_CTX_set(recp, b, ctx))
289 && TEST_true(BN_div_recp(d, c, a, recp, ctx))
290 && TEST_true(BN_mul(e, d, b, ctx))
291 && TEST_true(BN_add(d, e, c))
292 && TEST_true(BN_sub(d, d, a))
293 && TEST_BN_eq_zero(d)))
303 BN_RECP_CTX_free(recp);
308 int n, divisor, result, remainder;
309 } signed_mod_tests[] = {
316 static BIGNUM *set_signed_bn(int value)
318 BIGNUM *bn = BN_new();
322 if (!BN_set_word(bn, value < 0 ? -value : value)) {
326 BN_set_negative(bn, value < 0);
330 static int test_signed_mod_replace_ab(int n)
332 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
335 if (!TEST_ptr(a = set_signed_bn(signed_mod_tests[n].n))
336 || !TEST_ptr(b = set_signed_bn(signed_mod_tests[n].divisor))
337 || !TEST_ptr(c = set_signed_bn(signed_mod_tests[n].result))
338 || !TEST_ptr(d = set_signed_bn(signed_mod_tests[n].remainder)))
341 if (TEST_true(BN_div(a, b, a, b, ctx))
353 static int test_signed_mod_replace_ba(int n)
355 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
358 if (!TEST_ptr(a = set_signed_bn(signed_mod_tests[n].n))
359 || !TEST_ptr(b = set_signed_bn(signed_mod_tests[n].divisor))
360 || !TEST_ptr(c = set_signed_bn(signed_mod_tests[n].result))
361 || !TEST_ptr(d = set_signed_bn(signed_mod_tests[n].remainder)))
364 if (TEST_true(BN_div(b, a, a, b, ctx))
376 static int test_mod(void)
378 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
381 if (!TEST_ptr(a = BN_new())
382 || !TEST_ptr(b = BN_new())
383 || !TEST_ptr(c = BN_new())
384 || !TEST_ptr(d = BN_new())
385 || !TEST_ptr(e = BN_new()))
388 if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
390 for (i = 0; i < NUM0; i++) {
391 if (!(TEST_true(BN_bntest_rand(b, 450 + i * 10, 0, 0))))
393 BN_set_negative(a, rand_neg());
394 BN_set_negative(b, rand_neg());
395 if (!(TEST_true(BN_mod(c, a, b, ctx))
396 && TEST_true(BN_div(d, e, a, b, ctx))
398 && TEST_true(BN_mul(c, d, b, ctx))
399 && TEST_true(BN_add(d, c, e))
400 && TEST_BN_eq(d, a)))
413 static const char *bn1strings[] = {
414 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
415 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
416 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
417 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
418 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
419 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
420 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
421 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
422 "0000000000000000000000000000000000000000000000000000000000000000",
423 "0000000000000000000000000000000000000000000000000000000000000000",
424 "0000000000000000000000000000000000000000000000000000000000000000",
425 "0000000000000000000000000000000000000000000000000000000000000000",
426 "0000000000000000000000000000000000000000000000000000000000000000",
427 "0000000000000000000000000000000000000000000000000000000000000000",
428 "0000000000000000000000000000000000000000000000000000000000000000",
429 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
433 static const char *bn2strings[] = {
434 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
435 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
436 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
437 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
438 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
439 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
440 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
441 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
442 "0000000000000000000000000000000000000000000000000000000000000000",
443 "0000000000000000000000000000000000000000000000000000000000000000",
444 "0000000000000000000000000000000000000000000000000000000000000000",
445 "0000000000000000000000000000000000000000000000000000000000000000",
446 "0000000000000000000000000000000000000000000000000000000000000000",
447 "0000000000000000000000000000000000000000000000000000000000000000",
448 "0000000000000000000000000000000000000000000000000000000000000000",
449 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
454 * Test constant-time modular exponentiation with 1024-bit inputs, which on
455 * x86_64 cause a different code branch to be taken.
457 static int test_modexp_mont5(void)
459 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
460 BIGNUM *b = NULL, *n = NULL, *c = NULL;
461 BN_MONT_CTX *mont = NULL;
464 if (!TEST_ptr(a = BN_new())
465 || !TEST_ptr(p = BN_new())
466 || !TEST_ptr(m = BN_new())
467 || !TEST_ptr(d = BN_new())
468 || !TEST_ptr(e = BN_new())
469 || !TEST_ptr(b = BN_new())
470 || !TEST_ptr(n = BN_new())
471 || !TEST_ptr(c = BN_new())
472 || !TEST_ptr(mont = BN_MONT_CTX_new()))
475 /* must be odd for montgomery */
476 if (!(TEST_true(BN_bntest_rand(m, 1024, 0, 1))
478 && TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
482 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
484 if (!TEST_BN_eq_one(d))
487 /* Regression test for carry bug in mulx4x_mont */
488 if (!(TEST_true(BN_hex2bn(&a,
489 "7878787878787878787878787878787878787878787878787878787878787878"
490 "7878787878787878787878787878787878787878787878787878787878787878"
491 "7878787878787878787878787878787878787878787878787878787878787878"
492 "7878787878787878787878787878787878787878787878787878787878787878"))
493 && TEST_true(BN_hex2bn(&b,
494 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
495 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
496 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
497 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81"))
498 && TEST_true(BN_hex2bn(&n,
499 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
500 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
501 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
502 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF"))))
505 if (!(TEST_true(BN_MONT_CTX_set(mont, n, ctx))
506 && TEST_true(BN_mod_mul_montgomery(c, a, b, mont, ctx))
507 && TEST_true(BN_mod_mul_montgomery(d, b, a, mont, ctx))
508 && TEST_BN_eq(c, d)))
511 /* Regression test for carry bug in sqr[x]8x_mont */
512 if (!(TEST_true(parse_bigBN(&n, bn1strings))
513 && TEST_true(parse_bigBN(&a, bn2strings))))
516 if (!(TEST_ptr(b = BN_dup(a))
517 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
518 && TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
519 && TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
520 && TEST_BN_eq(c, d)))
523 /* Regression test for carry bug in bn_sqrx8x_internal */
525 static const char *ahex[] = {
526 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
527 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
528 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
529 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
530 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
531 "9544D954000000006C0000000000000000000000000000000000000000000000",
532 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
533 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
534 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
535 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
536 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
537 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
540 static const char *nhex[] = {
541 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
542 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
543 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
544 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
545 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
546 "00000010000000006C0000000000000000000000000000000000000000000000",
547 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
548 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
549 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
550 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
551 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
552 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
556 if (!(TEST_true(parse_bigBN(&a, ahex))
557 && TEST_true(parse_bigBN(&n, nhex))))
561 if (!(TEST_ptr(b = BN_dup(a))
562 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))))
565 if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
566 || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
567 || !TEST_BN_eq(c, d))
570 /* Regression test for bug in BN_from_montgomery_word */
571 if (!(TEST_true(BN_hex2bn(&a,
572 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
573 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
574 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
575 && TEST_true(BN_hex2bn(&n,
576 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
577 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
578 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
579 && TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx))))
582 /* Regression test for bug in rsaz_1024_mul_avx2 */
583 if (!(TEST_true(BN_hex2bn(&a,
584 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
585 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
586 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
587 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
588 && TEST_true(BN_hex2bn(&b,
589 "2020202020202020202020202020202020202020202020202020202020202020"
590 "2020202020202020202020202020202020202020202020202020202020202020"
591 "20202020202020FF202020202020202020202020202020202020202020202020"
592 "2020202020202020202020202020202020202020202020202020202020202020"))
593 && TEST_true(BN_hex2bn(&n,
594 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
595 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
596 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
597 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF"))
598 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
599 && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))
600 && TEST_true(BN_mod_exp_mont(d, a, b, n, ctx, mont))
601 && TEST_BN_eq(c, d)))
605 * rsaz_1024_mul_avx2 expects fully-reduced inputs.
606 * BN_mod_exp_mont_consttime should reduce the input first.
608 if (!(TEST_true(BN_hex2bn(&a,
609 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
610 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
611 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
612 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
613 && TEST_true(BN_hex2bn(&b,
614 "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
615 "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
616 "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
617 "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E"))
618 && TEST_true(BN_hex2bn(&n,
619 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
620 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
621 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
622 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
623 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
624 && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))))
627 if (!TEST_BN_eq(c, d))
631 * Regression test for overflow bug in bn_sqr_comba4/8 for
632 * mips-linux-gnu and mipsel-linux-gnu 32bit targets.
635 static const char *ehex[] = {
636 "95564994a96c45954227b845a1e99cb939d5a1da99ee91acc962396ae999a9ee",
637 "38603790448f2f7694c242a875f0cad0aae658eba085f312d2febbbd128dd2b5",
638 "8f7d1149f03724215d704344d0d62c587ae3c5939cba4b9b5f3dc5e8e911ef9a",
639 "5ce1a5a749a4989d0d8368f6e1f8cdf3a362a6c97fb02047ff152b480a4ad985",
640 "2d45efdf0770542992afca6a0590d52930434bba96017afbc9f99e112950a8b1",
641 "a359473ec376f329bdae6a19f503be6d4be7393c4e43468831234e27e3838680",
642 "b949390d2e416a3f9759e5349ab4c253f6f29f819a6fe4cbfd27ada34903300e",
643 "da021f62839f5878a36f1bc3085375b00fd5fa3e68d316c0fdace87a97558465",
645 static const char *phex[] = {
646 "f95dc0f980fbd22e90caa5a387cc4a369f3f830d50dd321c40db8c09a7e1a241",
647 "a536e096622d3280c0c1ba849c1f4a79bf490f60006d081e8cf69960189f0d31",
648 "2cd9e17073a3fba7881b21474a13b334116cb2f5dbf3189a6de3515d0840f053",
649 "c776d3982d391b6d04d642dda5cc6d1640174c09875addb70595658f89efb439",
650 "dc6fbd55f903aadd307982d3f659207f265e1ec6271b274521b7a5e28e8fd7a5",
651 "5df089292820477802a43cf5b6b94e999e8c9944ddebb0d0e95a60f88cb7e813",
652 "ba110d20e1024774107dd02949031864923b3cb8c3f7250d6d1287b0a40db6a4",
653 "7bd5a469518eb65aa207ddc47d8c6e5fc8e0c105be8fc1d4b57b2e27540471d5",
655 static const char *mhex[] = {
656 "fef15d5ce4625f1bccfbba49fc8439c72bf8202af039a2259678941b60bb4a8f",
657 "2987e965d58fd8cf86a856674d519763d0e1211cc9f8596971050d56d9b35db3",
658 "785866cfbca17cfdbed6060be3629d894f924a89fdc1efc624f80d41a22f1900",
659 "9503fcc3824ef62ccb9208430c26f2d8ceb2c63488ec4c07437aa4c96c43dd8b",
660 "9289ed00a712ff66ee195dc71f5e4ead02172b63c543d69baf495f5fd63ba7bc",
661 "c633bd309c016e37736da92129d0b053d4ab28d21ad7d8b6fab2a8bbdc8ee647",
662 "d2fbcf2cf426cf892e6f5639e0252993965dfb73ccd277407014ea784aaa280c",
663 "b7b03972bc8b0baa72360bdb44b82415b86b2f260f877791cd33ba8f2d65229b",
666 if (!TEST_true(parse_bigBN(&e, ehex))
667 || !TEST_true(parse_bigBN(&p, phex))
668 || !TEST_true(parse_bigBN(&m, mhex))
669 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
670 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
671 || !TEST_BN_eq(a, d))
676 if (!TEST_true(BN_bntest_rand(p, 1024, 0, 0)))
679 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
680 || !TEST_BN_eq_zero(d))
684 * Craft an input whose Montgomery representation is 1, i.e., shorter
685 * than the modulus m, in order to test the const time precomputation
686 * scattering/gathering.
688 if (!(TEST_true(BN_one(a))
689 && TEST_true(BN_MONT_CTX_set(mont, m, ctx))))
691 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
692 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
693 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
694 || !TEST_BN_eq(a, d))
697 /* Finally, some regular test vectors. */
698 if (!(TEST_true(BN_bntest_rand(e, 1024, 0, 0))
699 && TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
700 && TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
701 && TEST_BN_eq(a, d)))
707 BN_MONT_CTX_free(mont);
719 #ifndef OPENSSL_NO_EC2M
720 static int test_gf2m_add(void)
722 BIGNUM *a = NULL, *b = NULL, *c = NULL;
725 if (!TEST_ptr(a = BN_new())
726 || !TEST_ptr(b = BN_new())
727 || !TEST_ptr(c = BN_new()))
730 for (i = 0; i < NUM0; i++) {
731 if (!(TEST_true(BN_rand(a, 512, 0, 0))
732 && TEST_ptr(BN_copy(b, BN_value_one()))))
734 BN_set_negative(a, rand_neg());
735 BN_set_negative(b, rand_neg());
736 if (!(TEST_true(BN_GF2m_add(c, a, b))
737 /* Test that two added values have the correct parity. */
738 && TEST_false((BN_is_odd(a) && BN_is_odd(c))
739 || (!BN_is_odd(a) && !BN_is_odd(c)))))
741 if (!(TEST_true(BN_GF2m_add(c, c, c))
742 /* Test that c + c = 0. */
743 && TEST_BN_eq_zero(c)))
754 static int test_gf2m_mod(void)
756 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = 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()))
767 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
768 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
771 for (i = 0; i < NUM0; i++) {
772 if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
774 for (j = 0; j < 2; j++) {
775 if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
776 && TEST_true(BN_GF2m_add(d, a, c))
777 && TEST_true(BN_GF2m_mod(e, d, b[j]))
778 /* Test that a + (a mod p) mod p == 0. */
779 && TEST_BN_eq_zero(e)))
794 static int test_gf2m_mul(void)
796 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
797 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
800 if (!TEST_ptr(a = BN_new())
801 || !TEST_ptr(b[0] = BN_new())
802 || !TEST_ptr(b[1] = BN_new())
803 || !TEST_ptr(c = BN_new())
804 || !TEST_ptr(d = BN_new())
805 || !TEST_ptr(e = BN_new())
806 || !TEST_ptr(f = BN_new())
807 || !TEST_ptr(g = BN_new())
808 || !TEST_ptr(h = BN_new()))
811 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
812 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
815 for (i = 0; i < NUM0; i++) {
816 if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))
817 && TEST_true(BN_bntest_rand(c, 1024, 0, 0))
818 && TEST_true(BN_bntest_rand(d, 1024, 0, 0))))
820 for (j = 0; j < 2; j++) {
821 if (!(TEST_true(BN_GF2m_mod_mul(e, a, c, b[j], ctx))
822 && TEST_true(BN_GF2m_add(f, a, d))
823 && TEST_true(BN_GF2m_mod_mul(g, f, c, b[j], ctx))
824 && TEST_true(BN_GF2m_mod_mul(h, d, c, b[j], ctx))
825 && TEST_true(BN_GF2m_add(f, e, g))
826 && TEST_true(BN_GF2m_add(f, f, h))
827 /* Test that (a+d)*c = a*c + d*c. */
828 && TEST_BN_eq_zero(f)))
847 static int test_gf2m_sqr(void)
849 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
852 if (!TEST_ptr(a = BN_new())
853 || !TEST_ptr(b[0] = BN_new())
854 || !TEST_ptr(b[1] = BN_new())
855 || !TEST_ptr(c = BN_new())
856 || !TEST_ptr(d = BN_new()))
859 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
860 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
863 for (i = 0; i < NUM0; i++) {
864 if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
866 for (j = 0; j < 2; j++) {
867 if (!(TEST_true(BN_GF2m_mod_sqr(c, a, b[j], ctx))
868 && TEST_true(BN_copy(d, a))
869 && TEST_true(BN_GF2m_mod_mul(d, a, d, b[j], ctx))
870 && TEST_true(BN_GF2m_add(d, c, d))
871 /* Test that a*a = a^2. */
872 && TEST_BN_eq_zero(d)))
886 static int test_gf2m_modinv(void)
888 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
891 if (!TEST_ptr(a = BN_new())
892 || !TEST_ptr(b[0] = BN_new())
893 || !TEST_ptr(b[1] = BN_new())
894 || !TEST_ptr(c = BN_new())
895 || !TEST_ptr(d = BN_new()))
898 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
899 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
902 for (i = 0; i < NUM0; i++) {
903 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
905 for (j = 0; j < 2; j++) {
906 if (!(TEST_true(BN_GF2m_mod_inv(c, a, b[j], ctx))
907 && TEST_true(BN_GF2m_mod_mul(d, a, c, b[j], ctx))
908 /* Test that ((1/a)*a) = 1. */
909 && TEST_BN_eq_one(d)))
923 static int test_gf2m_moddiv(void)
925 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
926 BIGNUM *e = NULL, *f = NULL;
929 if (!TEST_ptr(a = BN_new())
930 || !TEST_ptr(b[0] = BN_new())
931 || !TEST_ptr(b[1] = BN_new())
932 || !TEST_ptr(c = BN_new())
933 || !TEST_ptr(d = BN_new())
934 || !TEST_ptr(e = BN_new())
935 || !TEST_ptr(f = BN_new()))
938 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
939 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
942 for (i = 0; i < NUM0; i++) {
943 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
944 && TEST_true(BN_bntest_rand(c, 512, 0, 0))))
946 for (j = 0; j < 2; j++) {
947 if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))
948 && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))
949 && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))
950 /* Test that ((a/c)*c)/a = 1. */
951 && TEST_BN_eq_one(f)))
967 static int test_gf2m_modexp(void)
969 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
970 BIGNUM *e = NULL, *f = NULL;
973 if (!TEST_ptr(a = BN_new())
974 || !TEST_ptr(b[0] = BN_new())
975 || !TEST_ptr(b[1] = BN_new())
976 || !TEST_ptr(c = BN_new())
977 || !TEST_ptr(d = BN_new())
978 || !TEST_ptr(e = BN_new())
979 || !TEST_ptr(f = BN_new()))
982 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
983 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
986 for (i = 0; i < NUM0; i++) {
987 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
988 && TEST_true(BN_bntest_rand(c, 512, 0, 0))
989 && TEST_true(BN_bntest_rand(d, 512, 0, 0))))
991 for (j = 0; j < 2; j++) {
992 if (!(TEST_true(BN_GF2m_mod_exp(e, a, c, b[j], ctx))
993 && TEST_true(BN_GF2m_mod_exp(f, a, d, b[j], ctx))
994 && TEST_true(BN_GF2m_mod_mul(e, e, f, b[j], ctx))
995 && TEST_true(BN_add(f, c, d))
996 && TEST_true(BN_GF2m_mod_exp(f, a, f, b[j], ctx))
997 && TEST_true(BN_GF2m_add(f, e, f))
998 /* Test that a^(c+d)=a^c*a^d. */
999 && TEST_BN_eq_zero(f)))
1015 static int test_gf2m_modsqrt(void)
1017 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
1018 BIGNUM *e = NULL, *f = NULL;
1021 if (!TEST_ptr(a = BN_new())
1022 || !TEST_ptr(b[0] = BN_new())
1023 || !TEST_ptr(b[1] = BN_new())
1024 || !TEST_ptr(c = BN_new())
1025 || !TEST_ptr(d = BN_new())
1026 || !TEST_ptr(e = BN_new())
1027 || !TEST_ptr(f = BN_new()))
1030 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
1031 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
1034 for (i = 0; i < NUM0; i++) {
1035 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1038 for (j = 0; j < 2; j++) {
1039 if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
1040 && TEST_true(BN_GF2m_mod_sqrt(d, a, b[j], ctx))
1041 && TEST_true(BN_GF2m_mod_sqr(e, d, b[j], ctx))
1042 && TEST_true(BN_GF2m_add(f, c, e))
1043 /* Test that d^2 = a, where d = sqrt(a). */
1044 && TEST_BN_eq_zero(f)))
1060 static int test_gf2m_modsolvequad(void)
1062 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
1064 int i, j, s = 0, t, st = 0;
1066 if (!TEST_ptr(a = BN_new())
1067 || !TEST_ptr(b[0] = BN_new())
1068 || !TEST_ptr(b[1] = BN_new())
1069 || !TEST_ptr(c = BN_new())
1070 || !TEST_ptr(d = BN_new())
1071 || !TEST_ptr(e = BN_new()))
1074 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
1075 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
1078 for (i = 0; i < NUM0; i++) {
1079 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1081 for (j = 0; j < 2; j++) {
1082 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1085 if (!(TEST_true(BN_GF2m_mod_sqr(d, c, b[j], ctx))
1086 && TEST_true(BN_GF2m_add(d, c, d))
1087 && TEST_true(BN_GF2m_mod(e, a, b[j]))
1088 && TEST_true(BN_GF2m_add(e, e, d))
1090 * Test that solution of quadratic c
1091 * satisfies c^2 + c = a.
1093 && TEST_BN_eq_zero(e)))
1098 if (!TEST_int_ge(s, 0)) {
1099 TEST_info("%d tests found no roots; probably an error", NUM0);
1114 static int test_kronecker(void)
1116 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
1117 int i, legendre, kronecker, st = 0;
1119 if (!TEST_ptr(a = BN_new())
1120 || !TEST_ptr(b = BN_new())
1121 || !TEST_ptr(r = BN_new())
1122 || !TEST_ptr(t = BN_new()))
1126 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1127 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1128 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1129 * generate a random prime b and compare these values for a number of
1130 * random a's. (That is, we run the Solovay-Strassen primality test to
1131 * confirm that b is prime, except that we don't want to test whether b
1132 * is prime but whether BN_kronecker works.)
1135 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
1137 BN_set_negative(b, rand_neg());
1139 for (i = 0; i < NUM0; i++) {
1140 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1142 BN_set_negative(a, rand_neg());
1144 /* t := (|b|-1)/2 (note that b is odd) */
1145 if (!TEST_true(BN_copy(t, b)))
1147 BN_set_negative(t, 0);
1148 if (!TEST_true(BN_sub_word(t, 1)))
1150 if (!TEST_true(BN_rshift1(t, t)))
1152 /* r := a^t mod b */
1153 BN_set_negative(b, 0);
1155 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
1157 BN_set_negative(b, 1);
1159 if (BN_is_word(r, 1))
1161 else if (BN_is_zero(r))
1164 if (!TEST_true(BN_add_word(r, 1)))
1166 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
1167 TEST_info("Legendre symbol computation failed");
1173 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
1175 /* we actually need BN_kronecker(a, |b|) */
1176 if (BN_is_negative(a) && BN_is_negative(b))
1177 kronecker = -kronecker;
1179 if (!TEST_int_eq(legendre, kronecker))
1192 static int file_sum(STANZA *s)
1194 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
1198 if (!TEST_ptr(a = getBN(s, "A"))
1199 || !TEST_ptr(b = getBN(s, "B"))
1200 || !TEST_ptr(sum = getBN(s, "Sum"))
1201 || !TEST_ptr(ret = BN_new()))
1204 if (!TEST_true(BN_add(ret, a, b))
1205 || !equalBN("A + B", sum, ret)
1206 || !TEST_true(BN_sub(ret, sum, a))
1207 || !equalBN("Sum - A", b, ret)
1208 || !TEST_true(BN_sub(ret, sum, b))
1209 || !equalBN("Sum - B", a, ret))
1213 * Test that the functions work when |r| and |a| point to the same BIGNUM,
1214 * or when |r| and |b| point to the same BIGNUM.
1215 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1217 if (!TEST_true(BN_copy(ret, a))
1218 || !TEST_true(BN_add(ret, ret, b))
1219 || !equalBN("A + B (r is a)", sum, ret)
1220 || !TEST_true(BN_copy(ret, b))
1221 || !TEST_true(BN_add(ret, a, ret))
1222 || !equalBN("A + B (r is b)", sum, ret)
1223 || !TEST_true(BN_copy(ret, sum))
1224 || !TEST_true(BN_sub(ret, ret, a))
1225 || !equalBN("Sum - A (r is a)", b, ret)
1226 || !TEST_true(BN_copy(ret, a))
1227 || !TEST_true(BN_sub(ret, sum, ret))
1228 || !equalBN("Sum - A (r is b)", b, ret)
1229 || !TEST_true(BN_copy(ret, sum))
1230 || !TEST_true(BN_sub(ret, ret, b))
1231 || !equalBN("Sum - B (r is a)", a, ret)
1232 || !TEST_true(BN_copy(ret, b))
1233 || !TEST_true(BN_sub(ret, sum, ret))
1234 || !equalBN("Sum - B (r is b)", a, ret))
1238 * Test BN_uadd() and BN_usub() with the prerequisites they are
1239 * documented as having. Note that these functions are frequently used
1240 * when the prerequisites don't hold. In those cases, they are supposed
1241 * to work as if the prerequisite hold, but we don't test that yet.
1244 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
1245 if (!TEST_true(BN_uadd(ret, a, b))
1246 || !equalBN("A +u B", sum, ret)
1247 || !TEST_true(BN_usub(ret, sum, a))
1248 || !equalBN("Sum -u A", b, ret)
1249 || !TEST_true(BN_usub(ret, sum, b))
1250 || !equalBN("Sum -u B", a, ret))
1253 * Test that the functions work when |r| and |a| point to the same
1254 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1255 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1257 if (!TEST_true(BN_copy(ret, a))
1258 || !TEST_true(BN_uadd(ret, ret, b))
1259 || !equalBN("A +u B (r is a)", sum, ret)
1260 || !TEST_true(BN_copy(ret, b))
1261 || !TEST_true(BN_uadd(ret, a, ret))
1262 || !equalBN("A +u B (r is b)", sum, ret)
1263 || !TEST_true(BN_copy(ret, sum))
1264 || !TEST_true(BN_usub(ret, ret, a))
1265 || !equalBN("Sum -u A (r is a)", b, ret)
1266 || !TEST_true(BN_copy(ret, a))
1267 || !TEST_true(BN_usub(ret, sum, ret))
1268 || !equalBN("Sum -u A (r is b)", b, ret)
1269 || !TEST_true(BN_copy(ret, sum))
1270 || !TEST_true(BN_usub(ret, ret, b))
1271 || !equalBN("Sum -u B (r is a)", a, ret)
1272 || !TEST_true(BN_copy(ret, b))
1273 || !TEST_true(BN_usub(ret, sum, ret))
1274 || !equalBN("Sum -u B (r is b)", a, ret))
1279 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1281 b_word = BN_get_word(b);
1282 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1283 if (!TEST_true(BN_copy(ret, a))
1284 || !TEST_true(BN_add_word(ret, b_word))
1285 || !equalBN("A + B (word)", sum, ret)
1286 || !TEST_true(BN_copy(ret, sum))
1287 || !TEST_true(BN_sub_word(ret, b_word))
1288 || !equalBN("Sum - B (word)", a, ret))
1301 static int file_lshift1(STANZA *s)
1303 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1304 BIGNUM *two = NULL, *remainder = NULL;
1307 if (!TEST_ptr(a = getBN(s, "A"))
1308 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1309 || !TEST_ptr(zero = BN_new())
1310 || !TEST_ptr(ret = BN_new())
1311 || !TEST_ptr(two = BN_new())
1312 || !TEST_ptr(remainder = BN_new()))
1317 if (!TEST_true(BN_set_word(two, 2))
1318 || !TEST_true(BN_add(ret, a, a))
1319 || !equalBN("A + A", lshift1, ret)
1320 || !TEST_true(BN_mul(ret, a, two, ctx))
1321 || !equalBN("A * 2", lshift1, ret)
1322 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1323 || !equalBN("LShift1 / 2", a, ret)
1324 || !equalBN("LShift1 % 2", zero, remainder)
1325 || !TEST_true(BN_lshift1(ret, a))
1326 || !equalBN("A << 1", lshift1, ret)
1327 || !TEST_true(BN_rshift1(ret, lshift1))
1328 || !equalBN("LShift >> 1", a, ret)
1329 || !TEST_true(BN_rshift1(ret, lshift1))
1330 || !equalBN("LShift >> 1", a, ret))
1333 /* Set the LSB to 1 and test rshift1 again. */
1334 if (!TEST_true(BN_set_bit(lshift1, 0))
1335 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1336 || !equalBN("(LShift1 | 1) / 2", a, ret)
1337 || !TEST_true(BN_rshift1(ret, lshift1))
1338 || !equalBN("(LShift | 1) >> 1", a, ret))
1353 static int file_lshift(STANZA *s)
1355 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1358 if (!TEST_ptr(a = getBN(s, "A"))
1359 || !TEST_ptr(lshift = getBN(s, "LShift"))
1360 || !TEST_ptr(ret = BN_new())
1361 || !getint(s, &n, "N"))
1364 if (!TEST_true(BN_lshift(ret, a, n))
1365 || !equalBN("A << N", lshift, ret)
1366 || !TEST_true(BN_rshift(ret, lshift, n))
1367 || !equalBN("A >> N", a, ret))
1378 static int file_rshift(STANZA *s)
1380 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1383 if (!TEST_ptr(a = getBN(s, "A"))
1384 || !TEST_ptr(rshift = getBN(s, "RShift"))
1385 || !TEST_ptr(ret = BN_new())
1386 || !getint(s, &n, "N"))
1389 if (!TEST_true(BN_rshift(ret, a, n))
1390 || !equalBN("A >> N", rshift, ret))
1393 /* If N == 1, try with rshift1 as well */
1395 if (!TEST_true(BN_rshift1(ret, a))
1396 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1408 static int file_square(STANZA *s)
1410 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1411 BIGNUM *remainder = NULL, *tmp = NULL;
1414 if (!TEST_ptr(a = getBN(s, "A"))
1415 || !TEST_ptr(square = getBN(s, "Square"))
1416 || !TEST_ptr(zero = BN_new())
1417 || !TEST_ptr(ret = BN_new())
1418 || !TEST_ptr(remainder = BN_new()))
1422 if (!TEST_true(BN_sqr(ret, a, ctx))
1423 || !equalBN("A^2", square, ret)
1424 || !TEST_true(BN_mul(ret, a, a, ctx))
1425 || !equalBN("A * A", square, ret)
1426 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1427 || !equalBN("Square / A", a, ret)
1428 || !equalBN("Square % A", zero, remainder))
1432 BN_set_negative(a, 0);
1433 if (!TEST_true(BN_sqrt(ret, square, ctx))
1434 || !equalBN("sqrt(Square)", a, ret))
1437 /* BN_sqrt should fail on non-squares and negative numbers. */
1438 if (!TEST_BN_eq_zero(square)) {
1439 if (!TEST_ptr(tmp = BN_new())
1440 || !TEST_true(BN_copy(tmp, square)))
1442 BN_set_negative(tmp, 1);
1444 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1448 BN_set_negative(tmp, 0);
1449 if (BN_add(tmp, tmp, BN_value_one()))
1451 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1468 static int file_product(STANZA *s)
1470 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1471 BIGNUM *remainder = NULL, *zero = NULL;
1474 if (!TEST_ptr(a = getBN(s, "A"))
1475 || !TEST_ptr(b = getBN(s, "B"))
1476 || !TEST_ptr(product = getBN(s, "Product"))
1477 || !TEST_ptr(ret = BN_new())
1478 || !TEST_ptr(remainder = BN_new())
1479 || !TEST_ptr(zero = BN_new()))
1484 if (!TEST_true(BN_mul(ret, a, b, ctx))
1485 || !equalBN("A * B", product, ret)
1486 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1487 || !equalBN("Product / A", b, ret)
1488 || !equalBN("Product % A", zero, remainder)
1489 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1490 || !equalBN("Product / B", a, ret)
1491 || !equalBN("Product % B", zero, remainder))
1505 static int file_quotient(STANZA *s)
1507 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1508 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1509 BN_ULONG b_word, ret_word;
1512 if (!TEST_ptr(a = getBN(s, "A"))
1513 || !TEST_ptr(b = getBN(s, "B"))
1514 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1515 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1516 || !TEST_ptr(ret = BN_new())
1517 || !TEST_ptr(ret2 = BN_new())
1518 || !TEST_ptr(nnmod = BN_new()))
1521 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1522 || !equalBN("A / B", quotient, ret)
1523 || !equalBN("A % B", remainder, ret2)
1524 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1525 || !TEST_true(BN_add(ret, ret, remainder))
1526 || !equalBN("Quotient * B + Remainder", a, ret))
1530 * Test with BN_mod_word() and BN_div_word() if the divisor is
1533 b_word = BN_get_word(b);
1534 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1535 BN_ULONG remainder_word = BN_get_word(remainder);
1537 assert(remainder_word != (BN_ULONG)-1);
1538 if (!TEST_ptr(BN_copy(ret, a)))
1540 ret_word = BN_div_word(ret, b_word);
1541 if (ret_word != remainder_word) {
1544 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1545 ret_word, remainder_word);
1547 TEST_error("Got A %% B (word) mismatch");
1551 if (!equalBN ("A / B (word)", quotient, ret))
1554 ret_word = BN_mod_word(a, b_word);
1555 if (ret_word != remainder_word) {
1558 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1559 ret_word, remainder_word);
1561 TEST_error("Got A %% B (word) mismatch");
1567 /* Test BN_nnmod. */
1568 if (!BN_is_negative(b)) {
1569 if (!TEST_true(BN_copy(nnmod, remainder))
1570 || (BN_is_negative(nnmod)
1571 && !TEST_true(BN_add(nnmod, nnmod, b)))
1572 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1573 || !equalBN("A % B (non-negative)", nnmod, ret))
1589 static int file_modmul(STANZA *s)
1591 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1594 if (!TEST_ptr(a = getBN(s, "A"))
1595 || !TEST_ptr(b = getBN(s, "B"))
1596 || !TEST_ptr(m = getBN(s, "M"))
1597 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1598 || !TEST_ptr(ret = BN_new()))
1601 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1602 || !equalBN("A * B (mod M)", mod_mul, ret))
1606 /* Reduce |a| and |b| and test the Montgomery version. */
1607 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1608 BIGNUM *a_tmp = BN_new();
1609 BIGNUM *b_tmp = BN_new();
1611 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1612 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1613 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1614 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1615 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1616 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1617 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1619 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1620 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1624 BN_MONT_CTX_free(mont);
1641 static int file_modexp(STANZA *s)
1643 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1644 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1647 if (!TEST_ptr(a = getBN(s, "A"))
1648 || !TEST_ptr(e = getBN(s, "E"))
1649 || !TEST_ptr(m = getBN(s, "M"))
1650 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1651 || !TEST_ptr(ret = BN_new())
1652 || !TEST_ptr(d = BN_new()))
1655 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1656 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1660 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1661 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1662 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1664 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1668 /* Regression test for carry propagation bug in sqr8x_reduction */
1669 BN_hex2bn(&a, "050505050505");
1670 BN_hex2bn(&b, "02");
1672 "4141414141414141414141274141414141414141414141414141414141414141"
1673 "4141414141414141414141414141414141414141414141414141414141414141"
1674 "4141414141414141414141800000000000000000000000000000000000000000"
1675 "0000000000000000000000000000000000000000000000000000000000000000"
1676 "0000000000000000000000000000000000000000000000000000000000000000"
1677 "0000000000000000000000000000000000000000000000000000000001");
1678 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1679 || !TEST_true(BN_mul(e, a, a, ctx))
1680 || !TEST_BN_eq(d, e))
1696 static int file_exp(STANZA *s)
1698 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1701 if (!TEST_ptr(a = getBN(s, "A"))
1702 || !TEST_ptr(e = getBN(s, "E"))
1703 || !TEST_ptr(exp = getBN(s, "Exp"))
1704 || !TEST_ptr(ret = BN_new()))
1707 if (!TEST_true(BN_exp(ret, a, e, ctx))
1708 || !equalBN("A ^ E", exp, ret))
1720 static int file_modsqrt(STANZA *s)
1722 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1725 if (!TEST_ptr(a = getBN(s, "A"))
1726 || !TEST_ptr(p = getBN(s, "P"))
1727 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1728 || !TEST_ptr(ret = BN_new())
1729 || !TEST_ptr(ret2 = BN_new()))
1732 /* There are two possible answers. */
1733 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1734 || !TEST_true(BN_sub(ret2, p, ret)))
1737 /* The first condition should NOT be a test. */
1738 if (BN_cmp(ret2, mod_sqrt) != 0
1739 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1752 static int file_gcd(STANZA *s)
1754 BIGNUM *a = NULL, *b = NULL, *gcd = NULL, *ret = NULL;
1757 if (!TEST_ptr(a = getBN(s, "A"))
1758 || !TEST_ptr(b = getBN(s, "B"))
1759 || !TEST_ptr(gcd = getBN(s, "GCD"))
1760 || !TEST_ptr(ret = BN_new()))
1763 if (!TEST_true(BN_gcd(ret, a, b, ctx))
1764 || !equalBN("gcd(A,B)", gcd, ret))
1776 static int test_bn2padded(void)
1778 uint8_t zeros[256], out[256], reference[128];
1783 /* Test edge case at 0. */
1784 if (!TEST_ptr((n = BN_new())))
1786 if (!TEST_int_eq(BN_bn2binpad(n, NULL, 0), 0))
1788 memset(out, -1, sizeof(out));
1789 if (!TEST_int_eq(BN_bn2binpad(n, out, sizeof(out)), sizeof(out)))
1791 memset(zeros, 0, sizeof(zeros));
1792 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1795 /* Test a random numbers at various byte lengths. */
1796 for (bytes = 128 - 7; bytes <= 128; bytes++) {
1797 # define TOP_BIT_ON 0
1798 # define BOTTOM_BIT_NOTOUCH 0
1799 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1801 if (!TEST_int_eq(BN_num_bytes(n), bytes)
1802 || !TEST_int_eq(BN_bn2bin(n, reference), bytes))
1804 /* Empty buffer should fail. */
1805 if (!TEST_int_eq(BN_bn2binpad(n, NULL, 0), -1))
1807 /* One byte short should fail. */
1808 if (!TEST_int_eq(BN_bn2binpad(n, out, bytes - 1), -1))
1810 /* Exactly right size should encode. */
1811 if (!TEST_int_eq(BN_bn2binpad(n, out, bytes), bytes)
1812 || !TEST_mem_eq(out, bytes, reference, bytes))
1814 /* Pad up one byte extra. */
1815 if (!TEST_int_eq(BN_bn2binpad(n, out, bytes + 1), bytes + 1)
1816 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1817 || !TEST_mem_eq(out, 1, zeros, 1))
1819 /* Pad up to 256. */
1820 if (!TEST_int_eq(BN_bn2binpad(n, out, sizeof(out)), sizeof(out))
1821 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1823 || !TEST_mem_eq(out, sizeof(out) - bytes,
1824 zeros, sizeof(out) - bytes))
1834 static int test_dec2bn(void)
1839 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1840 || !TEST_BN_eq_word(bn, 0)
1841 || !TEST_BN_eq_zero(bn)
1842 || !TEST_BN_le_zero(bn)
1843 || !TEST_BN_ge_zero(bn)
1844 || !TEST_BN_even(bn))
1849 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1850 || !TEST_BN_eq_word(bn, 256)
1851 || !TEST_BN_ge_zero(bn)
1852 || !TEST_BN_gt_zero(bn)
1853 || !TEST_BN_ne_zero(bn)
1854 || !TEST_BN_even(bn))
1859 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1860 || !TEST_BN_abs_eq_word(bn, 42)
1861 || !TEST_BN_lt_zero(bn)
1862 || !TEST_BN_le_zero(bn)
1863 || !TEST_BN_ne_zero(bn)
1864 || !TEST_BN_even(bn))
1869 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1870 || !TEST_BN_eq_word(bn, 1)
1871 || !TEST_BN_ne_zero(bn)
1872 || !TEST_BN_gt_zero(bn)
1873 || !TEST_BN_ge_zero(bn)
1874 || !TEST_BN_eq_one(bn)
1875 || !TEST_BN_odd(bn))
1880 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1881 || !TEST_BN_eq_zero(bn)
1882 || !TEST_BN_ge_zero(bn)
1883 || !TEST_BN_le_zero(bn)
1884 || !TEST_BN_even(bn))
1889 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1890 || !TEST_BN_abs_eq_word(bn, 42)
1891 || !TEST_BN_ge_zero(bn)
1892 || !TEST_BN_gt_zero(bn)
1893 || !TEST_BN_ne_zero(bn)
1894 || !TEST_BN_even(bn))
1903 static int test_hex2bn(void)
1908 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1909 || !TEST_BN_eq_zero(bn)
1910 || !TEST_BN_ge_zero(bn)
1911 || !TEST_BN_even(bn))
1916 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1917 || !TEST_BN_eq_word(bn, 0x256)
1918 || !TEST_BN_ge_zero(bn)
1919 || !TEST_BN_gt_zero(bn)
1920 || !TEST_BN_ne_zero(bn)
1921 || !TEST_BN_even(bn))
1926 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1927 || !TEST_BN_abs_eq_word(bn, 0x42)
1928 || !TEST_BN_lt_zero(bn)
1929 || !TEST_BN_le_zero(bn)
1930 || !TEST_BN_ne_zero(bn)
1931 || !TEST_BN_even(bn))
1936 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1937 || !TEST_BN_eq_word(bn, 0xCB)
1938 || !TEST_BN_ge_zero(bn)
1939 || !TEST_BN_gt_zero(bn)
1940 || !TEST_BN_ne_zero(bn)
1941 || !TEST_BN_odd(bn))
1946 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1947 || !TEST_BN_eq_zero(bn)
1948 || !TEST_BN_ge_zero(bn)
1949 || !TEST_BN_le_zero(bn)
1950 || !TEST_BN_even(bn))
1955 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1956 || !TEST_BN_eq_word(bn, 0xabc)
1957 || !TEST_BN_ge_zero(bn)
1958 || !TEST_BN_gt_zero(bn)
1959 || !TEST_BN_ne_zero(bn)
1960 || !TEST_BN_even(bn))
1969 static int test_asc2bn(void)
1974 if (!TEST_ptr(bn = BN_new()))
1977 if (!TEST_true(BN_asc2bn(&bn, "0"))
1978 || !TEST_BN_eq_zero(bn)
1979 || !TEST_BN_ge_zero(bn))
1982 if (!TEST_true(BN_asc2bn(&bn, "256"))
1983 || !TEST_BN_eq_word(bn, 256)
1984 || !TEST_BN_ge_zero(bn))
1987 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1988 || !TEST_BN_abs_eq_word(bn, 42)
1989 || !TEST_BN_lt_zero(bn))
1992 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1993 || !TEST_BN_eq_word(bn, 0x1234)
1994 || !TEST_BN_ge_zero(bn))
1997 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1998 || !TEST_BN_eq_word(bn, 0x1234)
1999 || !TEST_BN_ge_zero(bn))
2002 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
2003 || !TEST_BN_abs_eq_word(bn, 0xabcd)
2004 || !TEST_BN_lt_zero(bn))
2007 if (!TEST_true(BN_asc2bn(&bn, "-0"))
2008 || !TEST_BN_eq_zero(bn)
2009 || !TEST_BN_ge_zero(bn))
2012 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
2013 || !TEST_BN_eq_word(bn, 123)
2014 || !TEST_BN_ge_zero(bn))
2023 static const MPITEST kMPITests[] = {
2024 {"0", "\x00\x00\x00\x00", 4},
2025 {"1", "\x00\x00\x00\x01\x01", 5},
2026 {"-1", "\x00\x00\x00\x01\x81", 5},
2027 {"128", "\x00\x00\x00\x02\x00\x80", 6},
2028 {"256", "\x00\x00\x00\x02\x01\x00", 6},
2029 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
2032 static int test_mpi(int i)
2035 const MPITEST *test = &kMPITests[i];
2036 size_t mpi_len, mpi_len2;
2041 if (!TEST_ptr(bn = BN_new())
2042 || !TEST_true(BN_asc2bn(&bn, test->base10)))
2044 mpi_len = BN_bn2mpi(bn, NULL);
2045 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
2048 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
2049 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
2052 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
2055 if (!TEST_BN_eq(bn, bn2)) {
2067 static int test_rand(void)
2072 if (!TEST_ptr(bn = BN_new()))
2075 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
2076 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
2077 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
2078 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
2079 || !TEST_BN_eq_one(bn)
2080 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
2081 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
2082 || !TEST_BN_eq_one(bn)
2083 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
2084 || !TEST_BN_eq_word(bn, 3))
2093 static int test_negzero(void)
2095 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
2096 BIGNUM *numerator = NULL, *denominator = NULL;
2097 int consttime, st = 0;
2099 if (!TEST_ptr(a = BN_new())
2100 || !TEST_ptr(b = BN_new())
2101 || !TEST_ptr(c = BN_new())
2102 || !TEST_ptr(d = BN_new()))
2105 /* Test that BN_mul never gives negative zero. */
2106 if (!TEST_true(BN_set_word(a, 1)))
2108 BN_set_negative(a, 1);
2110 if (!TEST_true(BN_mul(c, a, b, ctx)))
2112 if (!TEST_BN_eq_zero(c)
2113 || !TEST_BN_ge_zero(c))
2116 for (consttime = 0; consttime < 2; consttime++) {
2117 if (!TEST_ptr(numerator = BN_new())
2118 || !TEST_ptr(denominator = BN_new()))
2121 BN_set_flags(numerator, BN_FLG_CONSTTIME);
2122 BN_set_flags(denominator, BN_FLG_CONSTTIME);
2124 /* Test that BN_div never gives negative zero in the quotient. */
2125 if (!TEST_true(BN_set_word(numerator, 1))
2126 || !TEST_true(BN_set_word(denominator, 2)))
2128 BN_set_negative(numerator, 1);
2129 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
2130 || !TEST_BN_eq_zero(a)
2131 || !TEST_BN_ge_zero(a))
2134 /* Test that BN_div never gives negative zero in the remainder. */
2135 if (!TEST_true(BN_set_word(denominator, 1))
2136 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
2137 || !TEST_BN_eq_zero(b)
2138 || !TEST_BN_ge_zero(b))
2141 BN_free(denominator);
2142 numerator = denominator = NULL;
2145 /* Test that BN_set_negative will not produce a negative zero. */
2147 BN_set_negative(a, 1);
2148 if (BN_is_negative(a))
2158 BN_free(denominator);
2162 static int test_badmod(void)
2164 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
2165 BN_MONT_CTX *mont = NULL;
2168 if (!TEST_ptr(a = BN_new())
2169 || !TEST_ptr(b = BN_new())
2170 || !TEST_ptr(zero = BN_new())
2171 || !TEST_ptr(mont = BN_MONT_CTX_new()))
2175 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
2179 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
2183 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
2187 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2192 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2197 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
2201 /* Some operations also may not be used with an even modulus. */
2202 if (!TEST_true(BN_set_word(b, 16)))
2205 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
2209 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2214 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2224 BN_MONT_CTX_free(mont);
2228 static int test_expmodzero(void)
2230 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
2233 if (!TEST_ptr(zero = BN_new())
2234 || !TEST_ptr(a = BN_new())
2235 || !TEST_ptr(r = BN_new()))
2239 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
2240 || !TEST_BN_eq_zero(r)
2241 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
2243 || !TEST_BN_eq_zero(r)
2244 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
2247 || !TEST_BN_eq_zero(r)
2248 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
2249 BN_value_one(), NULL, NULL))
2250 || !TEST_BN_eq_zero(r))
2261 static int test_expmodone(void)
2264 BIGNUM *r = BN_new();
2265 BIGNUM *a = BN_new();
2266 BIGNUM *p = BN_new();
2267 BIGNUM *m = BN_new();
2274 || !TEST_true(BN_set_word(a, 1))
2275 || !TEST_true(BN_set_word(p, 0))
2276 || !TEST_true(BN_set_word(m, 1)))
2279 /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
2280 for (i = 0; i < 2; i++) {
2281 if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
2282 || !TEST_BN_eq_zero(r)
2283 || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
2284 || !TEST_BN_eq_zero(r)
2285 || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
2286 || !TEST_BN_eq_zero(r)
2287 || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
2288 || !TEST_BN_eq_zero(r)
2289 || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
2290 || !TEST_BN_eq_zero(r)
2291 || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
2292 || !TEST_BN_eq_zero(r))
2294 /* Repeat for r = 1 ^ 0 mod -1 */
2296 BN_set_negative(m, 1);
2308 static int test_smallprime(int kBits)
2313 if (!TEST_ptr(r = BN_new()))
2317 if (!TEST_false(BN_generate_prime_ex(r, kBits, 0,
2321 if (!TEST_true(BN_generate_prime_ex(r, kBits, 0,
2323 || !TEST_int_eq(BN_num_bits(r), kBits))
2333 static int test_smallsafeprime(int kBits)
2338 if (!TEST_ptr(r = BN_new()))
2341 if (kBits <= 5 && kBits != 3) {
2342 if (!TEST_false(BN_generate_prime_ex(r, kBits, 1,
2346 if (!TEST_true(BN_generate_prime_ex(r, kBits, 1,
2348 || !TEST_int_eq(BN_num_bits(r), kBits))
2358 static int primes[] = { 2, 3, 5, 7, 17863 };
2360 static int test_is_prime(int i)
2366 if (!TEST_ptr(r = BN_new()))
2369 for (trial = 0; trial <= 1; ++trial) {
2370 if (!TEST_true(BN_set_word(r, primes[i]))
2371 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL),
2382 static int not_primes[] = { -1, 0, 1, 4 };
2384 static int test_not_prime(int i)
2390 if (!TEST_ptr(r = BN_new()))
2393 for (trial = 0; trial <= 1; ++trial) {
2394 if (!TEST_true(BN_set_word(r, not_primes[i]))
2395 || !TEST_false(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL)))
2405 static int test_ctx_set_ct_flag(BN_CTX *c)
2412 for (i = 0; i < OSSL_NELEM(b); i++) {
2413 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2416 BN_set_flags(b[i], BN_FLG_CONSTTIME);
2425 static int test_ctx_check_ct_flag(BN_CTX *c)
2432 for (i = 0; i < OSSL_NELEM(b); i++) {
2433 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2435 if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
2445 static int test_ctx_consttime_flag(void)
2448 * The constant-time flag should not "leak" among BN_CTX frames:
2450 * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
2451 * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
2452 * from the frame before ending it.
2453 * - test_ctx_check_ct_flag() then starts a new frame and gets a
2454 * number of BIGNUMs from it. In absence of leaks, none of the
2455 * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
2457 * In actual BN_CTX usage inside libcrypto the leak could happen at
2458 * any depth level in the BN_CTX stack, with varying results
2459 * depending on the patterns of sibling trees of nested function
2460 * calls sharing the same BN_CTX object, and the effect of
2461 * unintended BN_FLG_CONSTTIME on the called BN_* functions.
2463 * This simple unit test abstracts away this complexity and verifies
2464 * that the leak does not happen between two sibling functions
2465 * sharing the same BN_CTX object at the same level of nesting.
2468 BN_CTX *nctx = NULL;
2469 BN_CTX *sctx = NULL;
2473 if (!TEST_ptr(nctx = BN_CTX_new())
2474 || !TEST_ptr(sctx = BN_CTX_secure_new()))
2477 for (i = 0; i < 2; i++) {
2478 BN_CTX *c = i == 0 ? nctx : sctx;
2479 if (!TEST_true(test_ctx_set_ct_flag(c))
2480 || !TEST_true(test_ctx_check_ct_flag(c)))
2491 static int test_gcd_prime(void)
2493 BIGNUM *a = NULL, *b = NULL, *gcd = NULL;
2496 if (!TEST_ptr(a = BN_new())
2497 || !TEST_ptr(b = BN_new())
2498 || !TEST_ptr(gcd = BN_new()))
2501 if (!TEST_true(BN_generate_prime_ex(a, 1024, 0, NULL, NULL, NULL)))
2503 for (i = 0; i < NUM0; i++) {
2504 if (!TEST_true(BN_generate_prime_ex(b, 1024, 0,
2506 || !TEST_true(BN_gcd(gcd, a, b, ctx))
2507 || !TEST_true(BN_is_one(gcd)))
2519 typedef struct mod_exp_test_st
2527 static const MOD_EXP_TEST ModExpTests[] = {
2528 /* original test vectors for rsaz_512_sqr bug, by OSS-Fuzz */
2530 "1166180238001879113042182292626169621106255558914000595999312084"
2531 "4627946820899490684928760491249738643524880720584249698100907201"
2532 "002086675047927600340800371",
2533 "8000000000000000000000000000000000000000000000000000000000000000"
2534 "0000000000000000000000000000000000000000000000000000000000000000"
2536 "1340780792684523720980737645613191762604395855615117867483316354"
2537 "3294276330515137663421134775482798690129946803802212663956180562"
2538 "088664022929883876655300863",
2539 "8243904058268085430037326628480645845409758077568738532059032482"
2540 "8294114415890603594730158120426756266457928475330450251339773498"
2541 "26758407619521544102068438"
2544 "4974270041410803822078866696159586946995877618987010219312844726"
2545 "0284386121835740784990869050050504348861513337232530490826340663"
2546 "197278031692737429054",
2547 "4974270041410803822078866696159586946995877428188754995041148539"
2548 "1663243362592271353668158565195557417149981094324650322556843202"
2549 "946445882670777892608",
2550 "1340780716511420227215592830971452482815377482627251725537099028"
2551 "4429769497230131760206012644403029349547320953206103351725462999"
2552 "947509743623340557059752191",
2553 "5296244594780707015616522701706118082963369547253192207884519362"
2554 "1767869984947542695665420219028522815539559194793619684334900442"
2555 "49304558011362360473525933"
2557 /* test vectors for rsaz_512_srq bug, with rcx/rbx=1 */
2558 { /* between first and second iteration */
2559 "5148719036160389201525610950887605325980251964889646556085286545"
2560 "3931548809178823413169359635978762036512397113080988070677858033"
2561 "36463909753993540214027190",
2562 "6703903964971298549787012499102923063739682910296196688861780721"
2563 "8608820150367734884009371490834517138450159290932430254268769414"
2564 "05973284973216824503042158",
2565 "6703903964971298549787012499102923063739682910296196688861780721"
2566 "8608820150367734884009371490834517138450159290932430254268769414"
2567 "05973284973216824503042159",
2570 { /* between second and third iteration */
2571 "8908340854353752577419678771330460827942371434853054158622636544"
2572 "8151360109722890949471912566649465436296659601091730745087014189"
2573 "2672764191218875181826063",
2574 "6703903964971298549787012499102923063739682910296196688861780721"
2575 "8608820150367734884009371490834517138450159290932430254268769414"
2576 "05973284973216824503042158",
2577 "6703903964971298549787012499102923063739682910296196688861780721"
2578 "8608820150367734884009371490834517138450159290932430254268769414"
2579 "05973284973216824503042159",
2582 { /* between third and fourth iteration */
2583 "3427446396505596330634350984901719674479522569002785244080234738"
2584 "4288743635435746136297299366444548736533053717416735379073185344"
2585 "26985272974404612945608761",
2586 "6703903964971298549787012499102923063739682910296196688861780721"
2587 "8608820150367734884009371490834517138450159290932430254268769414"
2588 "05973284973216824503042158",
2589 "6703903964971298549787012499102923063739682910296196688861780721"
2590 "8608820150367734884009371490834517138450159290932430254268769414"
2591 "05973284973216824503042159",
2594 { /* between fourth and fifth iteration */
2595 "3472743044917564564078857826111874560045331237315597383869652985"
2596 "6919870028890895988478351133601517365908445058405433832718206902"
2597 "4088133164805266956353542",
2598 "6703903964971298549787012499102923063739682910296196688861780721"
2599 "8608820150367734884009371490834517138450159290932430254268769414"
2600 "05973284973216824503042158",
2601 "6703903964971298549787012499102923063739682910296196688861780721"
2602 "8608820150367734884009371490834517138450159290932430254268769414"
2603 "05973284973216824503042159",
2606 { /* between fifth and sixth iteration */
2607 "3608632990153469264412378349742339216742409743898601587274768025"
2608 "0110772032985643555192767717344946174122842255204082586753499651"
2609 "14483434992887431333675068",
2610 "6703903964971298549787012499102923063739682910296196688861780721"
2611 "8608820150367734884009371490834517138450159290932430254268769414"
2612 "05973284973216824503042158",
2613 "6703903964971298549787012499102923063739682910296196688861780721"
2614 "8608820150367734884009371490834517138450159290932430254268769414"
2615 "05973284973216824503042159",
2618 { /* between sixth and seventh iteration */
2619 "8455374370234070242910508226941981520235709767260723212165264877"
2620 "8689064388017521524568434328264431772644802567028663962962025746"
2621 "9283458217850119569539086",
2622 "6703903964971298549787012499102923063739682910296196688861780721"
2623 "8608820150367734884009371490834517138450159290932430254268769414"
2624 "05973284973216824503042158",
2625 "6703903964971298549787012499102923063739682910296196688861780721"
2626 "8608820150367734884009371490834517138450159290932430254268769414"
2627 "05973284973216824503042159",
2630 { /* between seventh and eighth iteration */
2631 "5155371529688532178421209781159131443543419764974688878527112131"
2632 "7446518205609427412336183157918981038066636807317733319323257603"
2633 "04416292040754017461076359",
2634 "1005585594745694782468051874865438459560952436544429503329267108"
2635 "2791323022555160232601405723625177570767523893639864538140315412"
2636 "108959927459825236754563832",
2637 "1005585594745694782468051874865438459560952436544429503329267108"
2638 "2791323022555160232601405723625177570767523893639864538140315412"
2639 "108959927459825236754563833",
2642 /* test vectors for rsaz_512_srq bug, with rcx/rbx=2 */
2643 { /* between first and second iteration */
2644 "3155666506033786929967309937640790361084670559125912405342594979"
2645 "4345142818528956285490897841406338022378565972533508820577760065"
2646 "58494345853302083699912572",
2647 "6703903964971298549787012499102923063739682910296196688861780721"
2648 "8608820150367734884009371490834517138450159290932430254268769414"
2649 "05973284973216824503042158",
2650 "6703903964971298549787012499102923063739682910296196688861780721"
2651 "8608820150367734884009371490834517138450159290932430254268769414"
2652 "05973284973216824503042159",
2655 { /* between second and third iteration */
2656 "3789819583801342198190405714582958759005991915505282362397087750"
2657 "4213544724644823098843135685133927198668818185338794377239590049"
2658 "41019388529192775771488319",
2659 "6703903964971298549787012499102923063739682910296196688861780721"
2660 "8608820150367734884009371490834517138450159290932430254268769414"
2661 "05973284973216824503042158",
2662 "6703903964971298549787012499102923063739682910296196688861780721"
2663 "8608820150367734884009371490834517138450159290932430254268769414"
2664 "05973284973216824503042159",
2667 { /* between third and forth iteration */
2668 "4695752552040706867080542538786056470322165281761525158189220280"
2669 "4025547447667484759200742764246905647644662050122968912279199065"
2670 "48065034299166336940507214",
2671 "6703903964971298549787012499102923063739682910296196688861780721"
2672 "8608820150367734884009371490834517138450159290932430254268769414"
2673 "05973284973216824503042158",
2674 "6703903964971298549787012499102923063739682910296196688861780721"
2675 "8608820150367734884009371490834517138450159290932430254268769414"
2676 "05973284973216824503042159",
2679 { /* between forth and fifth iteration */
2680 "2159140240970485794188159431017382878636879856244045329971239574"
2681 "8919691133560661162828034323196457386059819832804593989740268964"
2682 "74502911811812651475927076",
2683 "6703903964971298549787012499102923063739682910296196688861780721"
2684 "8608820150367734884009371490834517138450159290932430254268769414"
2685 "05973284973216824503042158",
2686 "6703903964971298549787012499102923063739682910296196688861780721"
2687 "8608820150367734884009371490834517138450159290932430254268769414"
2688 "05973284973216824503042159",
2691 { /* between fifth and sixth iteration */
2692 "5239312332984325668414624633307915097111691815000872662334695514"
2693 "5436533521392362443557163429336808208137221322444780490437871903"
2694 "99972784701334569424519255",
2695 "6703903964971298549787012499102923063739682910296196688861780721"
2696 "8608820150367734884009371490834517138450159290932430254268769414"
2697 "05973284973216824503042158",
2698 "6703903964971298549787012499102923063739682910296196688861780721"
2699 "8608820150367734884009371490834517138450159290932430254268769414"
2700 "05973284973216824503042159",
2703 { /* between sixth and seventh iteration */
2704 "1977953647322612860406858017869125467496941904523063466791308891"
2705 "1172796739058531929470539758361774569875505293428856181093904091"
2706 "33788264851714311303725089",
2707 "6703903964971298549787012499102923063739682910296196688861780721"
2708 "8608820150367734884009371490834517138450159290932430254268769414"
2709 "05973284973216824503042158",
2710 "6703903964971298549787012499102923063739682910296196688861780721"
2711 "8608820150367734884009371490834517138450159290932430254268769414"
2712 "05973284973216824503042159",
2715 { /* between seventh and eighth iteration */
2716 "6456987954117763835533395796948878140715006860263624787492985786"
2717 "8514630216966738305923915688821526449499763719943997120302368211"
2718 "04813318117996225041943964",
2719 "1340780792994259709957402499820584612747936582059239337772356144"
2720 "3721764030073546976801874298166903427690031858186486050853753882"
2721 "811946551499689575296532556",
2722 "1340780792994259709957402499820584612747936582059239337772356144"
2723 "3721764030073546976801874298166903427690031858186486050853753882"
2724 "811946551499689575296532557",
2729 static int test_mod_exp(int i)
2731 const MOD_EXP_TEST *test = &ModExpTests[i];
2733 BIGNUM* result = NULL;
2734 BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
2737 if (!TEST_ptr(result = BN_new())
2738 || !TEST_true(BN_dec2bn(&base, test->base))
2739 || !TEST_true(BN_dec2bn(&exponent, test->exp))
2740 || !TEST_true(BN_dec2bn(&modulo, test->mod)))
2743 if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
2746 if (!TEST_ptr(s = BN_bn2dec(result)))
2749 if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
2763 static int test_mod_exp_consttime(int i)
2765 const MOD_EXP_TEST *test = &ModExpTests[i];
2767 BIGNUM* result = NULL;
2768 BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
2771 if (!TEST_ptr(result = BN_new())
2772 || !TEST_true(BN_dec2bn(&base, test->base))
2773 || !TEST_true(BN_dec2bn(&exponent, test->exp))
2774 || !TEST_true(BN_dec2bn(&modulo, test->mod)))
2777 BN_set_flags(base, BN_FLG_CONSTTIME);
2778 BN_set_flags(exponent, BN_FLG_CONSTTIME);
2779 BN_set_flags(modulo, BN_FLG_CONSTTIME);
2781 if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
2784 if (!TEST_ptr(s = BN_bn2dec(result)))
2787 if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
2801 static int file_test_run(STANZA *s)
2803 static const FILETEST filetests[] = {
2805 {"LShift1", file_lshift1},
2806 {"LShift", file_lshift},
2807 {"RShift", file_rshift},
2808 {"Square", file_square},
2809 {"Product", file_product},
2810 {"Quotient", file_quotient},
2811 {"ModMul", file_modmul},
2812 {"ModExp", file_modexp},
2814 {"ModSqrt", file_modsqrt},
2817 int numtests = OSSL_NELEM(filetests);
2818 const FILETEST *tp = filetests;
2820 for ( ; --numtests >= 0; tp++) {
2821 if (findattr(s, tp->name) != NULL) {
2823 TEST_info("%s:%d: Failed %s test",
2824 s->test_file, s->start, tp->name);
2830 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2834 static int run_file_tests(int i)
2837 char *testfile = test_get_argument(i);
2840 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2842 if (!test_start_file(s, testfile)) {
2847 /* Read test file. */
2848 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2849 if (s->numpairs == 0)
2851 if (!file_test_run(s))
2854 test_clearstanza(s);
2864 int setup_tests(void)
2866 int n = test_get_argument_count();
2868 if (!TEST_ptr(ctx = BN_CTX_new()))
2873 ADD_TEST(test_div_recip);
2874 ADD_ALL_TESTS(test_signed_mod_replace_ab, OSSL_NELEM(signed_mod_tests));
2875 ADD_ALL_TESTS(test_signed_mod_replace_ba, OSSL_NELEM(signed_mod_tests));
2877 ADD_TEST(test_modexp_mont5);
2878 ADD_TEST(test_kronecker);
2879 ADD_TEST(test_rand);
2880 ADD_TEST(test_bn2padded);
2881 ADD_TEST(test_dec2bn);
2882 ADD_TEST(test_hex2bn);
2883 ADD_TEST(test_asc2bn);
2884 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2885 ADD_TEST(test_negzero);
2886 ADD_TEST(test_badmod);
2887 ADD_TEST(test_expmodzero);
2888 ADD_TEST(test_expmodone);
2889 ADD_ALL_TESTS(test_smallprime, 16);
2890 ADD_ALL_TESTS(test_smallsafeprime, 16);
2891 ADD_TEST(test_swap);
2892 ADD_TEST(test_ctx_consttime_flag);
2893 #ifndef OPENSSL_NO_EC2M
2894 ADD_TEST(test_gf2m_add);
2895 ADD_TEST(test_gf2m_mod);
2896 ADD_TEST(test_gf2m_mul);
2897 ADD_TEST(test_gf2m_sqr);
2898 ADD_TEST(test_gf2m_modinv);
2899 ADD_TEST(test_gf2m_moddiv);
2900 ADD_TEST(test_gf2m_modexp);
2901 ADD_TEST(test_gf2m_modsqrt);
2902 ADD_TEST(test_gf2m_modsolvequad);
2904 ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
2905 ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
2906 ADD_TEST(test_gcd_prime);
2907 ADD_ALL_TESTS(test_mod_exp, (int)OSSL_NELEM(ModExpTests));
2908 ADD_ALL_TESTS(test_mod_exp_consttime, (int)OSSL_NELEM(ModExpTests));
2910 ADD_ALL_TESTS(run_file_tests, n);
2915 void cleanup_tests(void)