2 * Copyright 1995-2022 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"
27 * Things in boring, not in openssl.
29 #define HAVE_BN_SQRT 0
31 typedef struct filetest_st {
33 int (*func)(STANZA *s);
36 typedef struct mpitest_st {
42 static const int NUM0 = 100; /* number of tests */
43 static const int NUM1 = 50; /* additional tests for some functions */
47 * Polynomial coefficients used in GFM tests.
49 #ifndef OPENSSL_NO_EC2M
50 static int p0[] = { 163, 7, 6, 3, 0, -1 };
51 static int p1[] = { 193, 15, 0, -1 };
55 * Look for |key| in the stanza and return it or NULL if not found.
57 static const char *findattr(STANZA *s, const char *key)
62 for ( ; --i >= 0; pp++)
63 if (OPENSSL_strcasecmp(pp->key, key) == 0)
69 * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
71 static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
73 char *bigstring = glue_strings(bn_strings, NULL);
74 int ret = BN_hex2bn(out, bigstring);
76 OPENSSL_free(bigstring);
81 * Parse BIGNUM, return number of bytes parsed.
83 static int parseBN(BIGNUM **out, const char *in)
86 return BN_hex2bn(out, in);
89 static int parsedecBN(BIGNUM **out, const char *in)
92 return BN_dec2bn(out, in);
95 static BIGNUM *getBN(STANZA *s, const char *attribute)
100 if ((hex = findattr(s, attribute)) == NULL) {
101 TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
105 if (parseBN(&ret, hex) != (int)strlen(hex)) {
106 TEST_error("Could not decode '%s'", hex);
112 static int getint(STANZA *s, int *out, const char *attribute)
118 if (!TEST_ptr(ret = getBN(s, attribute))
119 || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
129 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
131 if (BN_cmp(expected, actual) == 0)
134 TEST_error("unexpected %s value", op);
135 TEST_BN_eq(expected, actual);
140 * Return a "random" flag for if a BN should be negated.
142 static int rand_neg(void)
144 static unsigned int neg = 0;
145 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
147 return sign[(neg++) % 8];
150 static int test_swap(void)
152 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
153 int top, cond, st = 0;
155 if (!TEST_ptr(a = BN_new())
156 || !TEST_ptr(b = BN_new())
157 || !TEST_ptr(c = BN_new())
158 || !TEST_ptr(d = BN_new()))
161 if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))
162 && TEST_true(BN_bntest_rand(b, 1024, 1, 0))
163 && TEST_ptr(BN_copy(c, a))
164 && TEST_ptr(BN_copy(d, b))))
166 top = BN_num_bits(a) / BN_BITS2;
170 if (!equalBN("swap", a, d)
171 || !equalBN("swap", b, c))
174 /* regular swap: same pointer */
176 if (!equalBN("swap with same pointer", a, d))
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: true, same pointer */
187 BN_consttime_swap(cond, a, a, top);
188 if (!equalBN("cswap true", a, c))
191 /* conditional swap: false */
193 BN_consttime_swap(cond, a, b, top);
194 if (!equalBN("cswap false", a, c)
195 || !equalBN("cswap false", b, d))
198 /* conditional swap: false, same pointer */
199 BN_consttime_swap(cond, a, a, top);
200 if (!equalBN("cswap false", a, c))
203 /* same tests but checking flag swap */
204 BN_set_flags(a, BN_FLG_CONSTTIME);
207 if (!equalBN("swap, flags", a, d)
208 || !equalBN("swap, flags", b, c)
209 || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
210 || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
214 BN_consttime_swap(cond, a, b, top);
215 if (!equalBN("cswap true, flags", a, c)
216 || !equalBN("cswap true, flags", b, d)
217 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
218 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
222 BN_consttime_swap(cond, a, b, top);
223 if (!equalBN("cswap false, flags", a, c)
224 || !equalBN("cswap false, flags", b, d)
225 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
226 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
238 static int test_sub(void)
240 BIGNUM *a = NULL, *b = NULL, *c = NULL;
243 if (!TEST_ptr(a = BN_new())
244 || !TEST_ptr(b = BN_new())
245 || !TEST_ptr(c = BN_new()))
248 for (i = 0; i < NUM0 + NUM1; i++) {
250 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0)))
251 && TEST_ptr(BN_copy(b, a))
252 && TEST_int_ne(BN_set_bit(a, i), 0)
253 && TEST_true(BN_add_word(b, i)))
256 if (!TEST_true(BN_bntest_rand(b, 400 + i - NUM1, 0, 0)))
258 BN_set_negative(a, rand_neg());
259 BN_set_negative(b, rand_neg());
261 if (!(TEST_true(BN_sub(c, a, b))
262 && TEST_true(BN_add(c, c, b))
263 && TEST_true(BN_sub(c, c, a))
264 && TEST_BN_eq_zero(c)))
275 static int test_div_recip(void)
277 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
278 BN_RECP_CTX *recp = NULL;
281 if (!TEST_ptr(a = BN_new())
282 || !TEST_ptr(b = BN_new())
283 || !TEST_ptr(c = BN_new())
284 || !TEST_ptr(d = BN_new())
285 || !TEST_ptr(e = BN_new())
286 || !TEST_ptr(recp = BN_RECP_CTX_new()))
289 for (i = 0; i < NUM0 + NUM1; i++) {
291 if (!(TEST_true(BN_bntest_rand(a, 400, 0, 0))
292 && TEST_ptr(BN_copy(b, a))
293 && TEST_true(BN_lshift(a, a, i))
294 && TEST_true(BN_add_word(a, i))))
297 if (!(TEST_true(BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0))))
300 BN_set_negative(a, rand_neg());
301 BN_set_negative(b, rand_neg());
302 if (!(TEST_true(BN_RECP_CTX_set(recp, b, ctx))
303 && TEST_true(BN_div_recp(d, c, a, recp, ctx))
304 && TEST_true(BN_mul(e, d, b, ctx))
305 && TEST_true(BN_add(d, e, c))
306 && TEST_true(BN_sub(d, d, a))
307 && TEST_BN_eq_zero(d)))
317 BN_RECP_CTX_free(recp);
322 int n, divisor, result, remainder;
323 } signed_mod_tests[] = {
330 static BIGNUM *set_signed_bn(int value)
332 BIGNUM *bn = BN_new();
336 if (!BN_set_word(bn, value < 0 ? -value : value)) {
340 BN_set_negative(bn, value < 0);
344 static int test_signed_mod_replace_ab(int n)
346 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
349 if (!TEST_ptr(a = set_signed_bn(signed_mod_tests[n].n))
350 || !TEST_ptr(b = set_signed_bn(signed_mod_tests[n].divisor))
351 || !TEST_ptr(c = set_signed_bn(signed_mod_tests[n].result))
352 || !TEST_ptr(d = set_signed_bn(signed_mod_tests[n].remainder)))
355 if (TEST_true(BN_div(a, b, a, b, ctx))
367 static int test_signed_mod_replace_ba(int n)
369 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
372 if (!TEST_ptr(a = set_signed_bn(signed_mod_tests[n].n))
373 || !TEST_ptr(b = set_signed_bn(signed_mod_tests[n].divisor))
374 || !TEST_ptr(c = set_signed_bn(signed_mod_tests[n].result))
375 || !TEST_ptr(d = set_signed_bn(signed_mod_tests[n].remainder)))
378 if (TEST_true(BN_div(b, a, a, b, ctx))
390 static int test_mod(void)
392 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
395 if (!TEST_ptr(a = BN_new())
396 || !TEST_ptr(b = BN_new())
397 || !TEST_ptr(c = BN_new())
398 || !TEST_ptr(d = BN_new())
399 || !TEST_ptr(e = BN_new()))
402 if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
404 for (i = 0; i < NUM0; i++) {
405 if (!(TEST_true(BN_bntest_rand(b, 450 + i * 10, 0, 0))))
407 BN_set_negative(a, rand_neg());
408 BN_set_negative(b, rand_neg());
409 if (!(TEST_true(BN_mod(c, a, b, ctx))
410 && TEST_true(BN_div(d, e, a, b, ctx))
412 && TEST_true(BN_mul(c, d, b, ctx))
413 && TEST_true(BN_add(d, c, e))
414 && TEST_BN_eq(d, a)))
427 static const char *bn1strings[] = {
428 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
429 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
430 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
431 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
432 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
433 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
434 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
435 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
436 "0000000000000000000000000000000000000000000000000000000000000000",
437 "0000000000000000000000000000000000000000000000000000000000000000",
438 "0000000000000000000000000000000000000000000000000000000000000000",
439 "0000000000000000000000000000000000000000000000000000000000000000",
440 "0000000000000000000000000000000000000000000000000000000000000000",
441 "0000000000000000000000000000000000000000000000000000000000000000",
442 "0000000000000000000000000000000000000000000000000000000000000000",
443 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
447 static const char *bn2strings[] = {
448 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
449 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
450 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
451 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
452 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
453 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
454 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
455 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
456 "0000000000000000000000000000000000000000000000000000000000000000",
457 "0000000000000000000000000000000000000000000000000000000000000000",
458 "0000000000000000000000000000000000000000000000000000000000000000",
459 "0000000000000000000000000000000000000000000000000000000000000000",
460 "0000000000000000000000000000000000000000000000000000000000000000",
461 "0000000000000000000000000000000000000000000000000000000000000000",
462 "0000000000000000000000000000000000000000000000000000000000000000",
463 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
468 * Test constant-time modular exponentiation with 1024-bit inputs, which on
469 * x86_64 cause a different code branch to be taken.
471 static int test_modexp_mont5(void)
473 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
474 BIGNUM *b = NULL, *n = NULL, *c = NULL;
475 BN_MONT_CTX *mont = NULL;
478 if (!TEST_ptr(a = BN_new())
479 || !TEST_ptr(p = BN_new())
480 || !TEST_ptr(m = BN_new())
481 || !TEST_ptr(d = BN_new())
482 || !TEST_ptr(e = BN_new())
483 || !TEST_ptr(b = BN_new())
484 || !TEST_ptr(n = BN_new())
485 || !TEST_ptr(c = BN_new())
486 || !TEST_ptr(mont = BN_MONT_CTX_new()))
489 /* must be odd for montgomery */
490 if (!(TEST_true(BN_bntest_rand(m, 1024, 0, 1))
492 && TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
496 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
498 if (!TEST_BN_eq_one(d))
501 /* Regression test for carry bug in mulx4x_mont */
502 if (!(TEST_true(BN_hex2bn(&a,
503 "7878787878787878787878787878787878787878787878787878787878787878"
504 "7878787878787878787878787878787878787878787878787878787878787878"
505 "7878787878787878787878787878787878787878787878787878787878787878"
506 "7878787878787878787878787878787878787878787878787878787878787878"))
507 && TEST_true(BN_hex2bn(&b,
508 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
509 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
510 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
511 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81"))
512 && TEST_true(BN_hex2bn(&n,
513 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
514 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
515 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
516 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF"))))
519 if (!(TEST_true(BN_MONT_CTX_set(mont, n, ctx))
520 && TEST_true(BN_mod_mul_montgomery(c, a, b, mont, ctx))
521 && TEST_true(BN_mod_mul_montgomery(d, b, a, mont, ctx))
522 && TEST_BN_eq(c, d)))
525 /* Regression test for carry bug in sqr[x]8x_mont */
526 if (!(TEST_true(parse_bigBN(&n, bn1strings))
527 && TEST_true(parse_bigBN(&a, bn2strings))))
530 if (!(TEST_ptr(b = BN_dup(a))
531 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
532 && TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
533 && TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
534 && TEST_BN_eq(c, d)))
537 /* Regression test for carry bug in bn_sqrx8x_internal */
539 static const char *ahex[] = {
540 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
541 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
542 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
543 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
544 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
545 "9544D954000000006C0000000000000000000000000000000000000000000000",
546 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
547 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
548 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
549 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
550 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
551 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
554 static const char *nhex[] = {
555 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
556 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
557 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
558 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
559 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
560 "00000010000000006C0000000000000000000000000000000000000000000000",
561 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
562 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
563 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
564 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
565 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
566 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
570 if (!(TEST_true(parse_bigBN(&a, ahex))
571 && TEST_true(parse_bigBN(&n, nhex))))
575 if (!(TEST_ptr(b = BN_dup(a))
576 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))))
579 if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
580 || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
581 || !TEST_BN_eq(c, d))
584 /* Regression test for bug in BN_from_montgomery_word */
585 if (!(TEST_true(BN_hex2bn(&a,
586 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
587 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
588 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
589 && TEST_true(BN_hex2bn(&n,
590 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
591 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
592 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
593 && TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx))))
596 /* Regression test for bug in rsaz_1024_mul_avx2 */
597 if (!(TEST_true(BN_hex2bn(&a,
598 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
599 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
600 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
601 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
602 && TEST_true(BN_hex2bn(&b,
603 "2020202020202020202020202020202020202020202020202020202020202020"
604 "2020202020202020202020202020202020202020202020202020202020202020"
605 "20202020202020FF202020202020202020202020202020202020202020202020"
606 "2020202020202020202020202020202020202020202020202020202020202020"))
607 && TEST_true(BN_hex2bn(&n,
608 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
609 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
610 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
611 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF"))
612 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
613 && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))
614 && TEST_true(BN_mod_exp_mont(d, a, b, n, ctx, mont))
615 && TEST_BN_eq(c, d)))
619 * rsaz_1024_mul_avx2 expects fully-reduced inputs.
620 * BN_mod_exp_mont_consttime should reduce the input first.
622 if (!(TEST_true(BN_hex2bn(&a,
623 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
624 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
625 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
626 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
627 && TEST_true(BN_hex2bn(&b,
628 "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
629 "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
630 "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
631 "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E"))
632 && TEST_true(BN_hex2bn(&n,
633 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
634 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
635 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
636 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
637 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
638 && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))))
641 if (!TEST_BN_eq(c, d))
645 * Regression test for overflow bug in bn_sqr_comba4/8 for
646 * mips-linux-gnu and mipsel-linux-gnu 32bit targets.
649 static const char *ehex[] = {
650 "95564994a96c45954227b845a1e99cb939d5a1da99ee91acc962396ae999a9ee",
651 "38603790448f2f7694c242a875f0cad0aae658eba085f312d2febbbd128dd2b5",
652 "8f7d1149f03724215d704344d0d62c587ae3c5939cba4b9b5f3dc5e8e911ef9a",
653 "5ce1a5a749a4989d0d8368f6e1f8cdf3a362a6c97fb02047ff152b480a4ad985",
654 "2d45efdf0770542992afca6a0590d52930434bba96017afbc9f99e112950a8b1",
655 "a359473ec376f329bdae6a19f503be6d4be7393c4e43468831234e27e3838680",
656 "b949390d2e416a3f9759e5349ab4c253f6f29f819a6fe4cbfd27ada34903300e",
657 "da021f62839f5878a36f1bc3085375b00fd5fa3e68d316c0fdace87a97558465",
659 static const char *phex[] = {
660 "f95dc0f980fbd22e90caa5a387cc4a369f3f830d50dd321c40db8c09a7e1a241",
661 "a536e096622d3280c0c1ba849c1f4a79bf490f60006d081e8cf69960189f0d31",
662 "2cd9e17073a3fba7881b21474a13b334116cb2f5dbf3189a6de3515d0840f053",
663 "c776d3982d391b6d04d642dda5cc6d1640174c09875addb70595658f89efb439",
664 "dc6fbd55f903aadd307982d3f659207f265e1ec6271b274521b7a5e28e8fd7a5",
665 "5df089292820477802a43cf5b6b94e999e8c9944ddebb0d0e95a60f88cb7e813",
666 "ba110d20e1024774107dd02949031864923b3cb8c3f7250d6d1287b0a40db6a4",
667 "7bd5a469518eb65aa207ddc47d8c6e5fc8e0c105be8fc1d4b57b2e27540471d5",
669 static const char *mhex[] = {
670 "fef15d5ce4625f1bccfbba49fc8439c72bf8202af039a2259678941b60bb4a8f",
671 "2987e965d58fd8cf86a856674d519763d0e1211cc9f8596971050d56d9b35db3",
672 "785866cfbca17cfdbed6060be3629d894f924a89fdc1efc624f80d41a22f1900",
673 "9503fcc3824ef62ccb9208430c26f2d8ceb2c63488ec4c07437aa4c96c43dd8b",
674 "9289ed00a712ff66ee195dc71f5e4ead02172b63c543d69baf495f5fd63ba7bc",
675 "c633bd309c016e37736da92129d0b053d4ab28d21ad7d8b6fab2a8bbdc8ee647",
676 "d2fbcf2cf426cf892e6f5639e0252993965dfb73ccd277407014ea784aaa280c",
677 "b7b03972bc8b0baa72360bdb44b82415b86b2f260f877791cd33ba8f2d65229b",
680 if (!TEST_true(parse_bigBN(&e, ehex))
681 || !TEST_true(parse_bigBN(&p, phex))
682 || !TEST_true(parse_bigBN(&m, mhex))
683 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
684 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
685 || !TEST_BN_eq(a, d))
690 if (!TEST_true(BN_bntest_rand(p, 1024, 0, 0)))
693 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
694 || !TEST_BN_eq_zero(d))
698 * Craft an input whose Montgomery representation is 1, i.e., shorter
699 * than the modulus m, in order to test the const time precomputation
700 * scattering/gathering.
702 if (!(TEST_true(BN_one(a))
703 && TEST_true(BN_MONT_CTX_set(mont, m, ctx))))
705 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
706 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
707 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
708 || !TEST_BN_eq(a, d))
711 /* Finally, some regular test vectors. */
712 if (!(TEST_true(BN_bntest_rand(e, 1024, 0, 0))
713 && TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
714 && TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
715 && TEST_BN_eq(a, d)))
721 BN_MONT_CTX_free(mont);
733 #ifndef OPENSSL_NO_EC2M
734 static int test_gf2m_add(void)
736 BIGNUM *a = NULL, *b = NULL, *c = NULL;
739 if (!TEST_ptr(a = BN_new())
740 || !TEST_ptr(b = BN_new())
741 || !TEST_ptr(c = BN_new()))
744 for (i = 0; i < NUM0; i++) {
745 if (!(TEST_true(BN_rand(a, 512, 0, 0))
746 && TEST_ptr(BN_copy(b, BN_value_one()))))
748 BN_set_negative(a, rand_neg());
749 BN_set_negative(b, rand_neg());
750 if (!(TEST_true(BN_GF2m_add(c, a, b))
751 /* Test that two added values have the correct parity. */
752 && TEST_false((BN_is_odd(a) && BN_is_odd(c))
753 || (!BN_is_odd(a) && !BN_is_odd(c)))))
755 if (!(TEST_true(BN_GF2m_add(c, c, c))
756 /* Test that c + c = 0. */
757 && TEST_BN_eq_zero(c)))
768 static int test_gf2m_mod(void)
770 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL, *e = NULL;
773 if (!TEST_ptr(a = BN_new())
774 || !TEST_ptr(b[0] = BN_new())
775 || !TEST_ptr(b[1] = BN_new())
776 || !TEST_ptr(c = BN_new())
777 || !TEST_ptr(d = BN_new())
778 || !TEST_ptr(e = BN_new()))
781 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
782 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
785 for (i = 0; i < NUM0; i++) {
786 if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
788 for (j = 0; j < 2; j++) {
789 if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
790 && TEST_true(BN_GF2m_add(d, a, c))
791 && TEST_true(BN_GF2m_mod(e, d, b[j]))
792 /* Test that a + (a mod p) mod p == 0. */
793 && TEST_BN_eq_zero(e)))
808 static int test_gf2m_mul(void)
810 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
811 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
814 if (!TEST_ptr(a = BN_new())
815 || !TEST_ptr(b[0] = BN_new())
816 || !TEST_ptr(b[1] = BN_new())
817 || !TEST_ptr(c = BN_new())
818 || !TEST_ptr(d = BN_new())
819 || !TEST_ptr(e = BN_new())
820 || !TEST_ptr(f = BN_new())
821 || !TEST_ptr(g = BN_new())
822 || !TEST_ptr(h = BN_new()))
825 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
826 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
829 for (i = 0; i < NUM0; i++) {
830 if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))
831 && TEST_true(BN_bntest_rand(c, 1024, 0, 0))
832 && TEST_true(BN_bntest_rand(d, 1024, 0, 0))))
834 for (j = 0; j < 2; j++) {
835 if (!(TEST_true(BN_GF2m_mod_mul(e, a, c, b[j], ctx))
836 && TEST_true(BN_GF2m_add(f, a, d))
837 && TEST_true(BN_GF2m_mod_mul(g, f, c, b[j], ctx))
838 && TEST_true(BN_GF2m_mod_mul(h, d, c, b[j], ctx))
839 && TEST_true(BN_GF2m_add(f, e, g))
840 && TEST_true(BN_GF2m_add(f, f, h))
841 /* Test that (a+d)*c = a*c + d*c. */
842 && TEST_BN_eq_zero(f)))
861 static int test_gf2m_sqr(void)
863 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
866 if (!TEST_ptr(a = BN_new())
867 || !TEST_ptr(b[0] = BN_new())
868 || !TEST_ptr(b[1] = BN_new())
869 || !TEST_ptr(c = BN_new())
870 || !TEST_ptr(d = BN_new()))
873 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
874 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
877 for (i = 0; i < NUM0; i++) {
878 if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
880 for (j = 0; j < 2; j++) {
881 if (!(TEST_true(BN_GF2m_mod_sqr(c, a, b[j], ctx))
882 && TEST_true(BN_copy(d, a))
883 && TEST_true(BN_GF2m_mod_mul(d, a, d, b[j], ctx))
884 && TEST_true(BN_GF2m_add(d, c, d))
885 /* Test that a*a = a^2. */
886 && TEST_BN_eq_zero(d)))
900 static int test_gf2m_modinv(void)
902 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
905 if (!TEST_ptr(a = BN_new())
906 || !TEST_ptr(b[0] = BN_new())
907 || !TEST_ptr(b[1] = BN_new())
908 || !TEST_ptr(c = BN_new())
909 || !TEST_ptr(d = BN_new()))
912 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
913 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
916 for (i = 0; i < NUM0; i++) {
917 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
919 for (j = 0; j < 2; j++) {
920 if (!(TEST_true(BN_GF2m_mod_inv(c, a, b[j], ctx))
921 && TEST_true(BN_GF2m_mod_mul(d, a, c, b[j], ctx))
922 /* Test that ((1/a)*a) = 1. */
923 && TEST_BN_eq_one(d)))
937 static int test_gf2m_moddiv(void)
939 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
940 BIGNUM *e = NULL, *f = NULL;
943 if (!TEST_ptr(a = BN_new())
944 || !TEST_ptr(b[0] = BN_new())
945 || !TEST_ptr(b[1] = BN_new())
946 || !TEST_ptr(c = BN_new())
947 || !TEST_ptr(d = BN_new())
948 || !TEST_ptr(e = BN_new())
949 || !TEST_ptr(f = BN_new()))
952 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
953 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
956 for (i = 0; i < NUM0; i++) {
957 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
958 && TEST_true(BN_bntest_rand(c, 512, 0, 0))))
960 for (j = 0; j < 2; j++) {
961 if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))
962 && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))
963 && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))
964 /* Test that ((a/c)*c)/a = 1. */
965 && TEST_BN_eq_one(f)))
981 static int test_gf2m_modexp(void)
983 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
984 BIGNUM *e = NULL, *f = NULL;
987 if (!TEST_ptr(a = BN_new())
988 || !TEST_ptr(b[0] = BN_new())
989 || !TEST_ptr(b[1] = BN_new())
990 || !TEST_ptr(c = BN_new())
991 || !TEST_ptr(d = BN_new())
992 || !TEST_ptr(e = BN_new())
993 || !TEST_ptr(f = BN_new()))
996 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
997 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
1000 for (i = 0; i < NUM0; i++) {
1001 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
1002 && TEST_true(BN_bntest_rand(c, 512, 0, 0))
1003 && TEST_true(BN_bntest_rand(d, 512, 0, 0))))
1005 for (j = 0; j < 2; j++) {
1006 if (!(TEST_true(BN_GF2m_mod_exp(e, a, c, b[j], ctx))
1007 && TEST_true(BN_GF2m_mod_exp(f, a, d, b[j], ctx))
1008 && TEST_true(BN_GF2m_mod_mul(e, e, f, b[j], ctx))
1009 && TEST_true(BN_add(f, c, d))
1010 && TEST_true(BN_GF2m_mod_exp(f, a, f, b[j], ctx))
1011 && TEST_true(BN_GF2m_add(f, e, f))
1012 /* Test that a^(c+d)=a^c*a^d. */
1013 && TEST_BN_eq_zero(f)))
1029 static int test_gf2m_modsqrt(void)
1031 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
1032 BIGNUM *e = NULL, *f = NULL;
1035 if (!TEST_ptr(a = BN_new())
1036 || !TEST_ptr(b[0] = BN_new())
1037 || !TEST_ptr(b[1] = BN_new())
1038 || !TEST_ptr(c = BN_new())
1039 || !TEST_ptr(d = BN_new())
1040 || !TEST_ptr(e = BN_new())
1041 || !TEST_ptr(f = BN_new()))
1044 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
1045 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
1048 for (i = 0; i < NUM0; i++) {
1049 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1052 for (j = 0; j < 2; j++) {
1053 if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
1054 && TEST_true(BN_GF2m_mod_sqrt(d, a, b[j], ctx))
1055 && TEST_true(BN_GF2m_mod_sqr(e, d, b[j], ctx))
1056 && TEST_true(BN_GF2m_add(f, c, e))
1057 /* Test that d^2 = a, where d = sqrt(a). */
1058 && TEST_BN_eq_zero(f)))
1074 static int test_gf2m_modsolvequad(void)
1076 BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
1078 int i, j, s = 0, t, st = 0;
1080 if (!TEST_ptr(a = BN_new())
1081 || !TEST_ptr(b[0] = BN_new())
1082 || !TEST_ptr(b[1] = BN_new())
1083 || !TEST_ptr(c = BN_new())
1084 || !TEST_ptr(d = BN_new())
1085 || !TEST_ptr(e = BN_new()))
1088 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
1089 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
1092 for (i = 0; i < NUM0; i++) {
1093 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1095 for (j = 0; j < 2; j++) {
1096 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1099 if (!(TEST_true(BN_GF2m_mod_sqr(d, c, b[j], ctx))
1100 && TEST_true(BN_GF2m_add(d, c, d))
1101 && TEST_true(BN_GF2m_mod(e, a, b[j]))
1102 && TEST_true(BN_GF2m_add(e, e, d))
1104 * Test that solution of quadratic c
1105 * satisfies c^2 + c = a.
1107 && TEST_BN_eq_zero(e)))
1112 if (!TEST_int_ge(s, 0)) {
1113 TEST_info("%d tests found no roots; probably an error", NUM0);
1128 static int test_kronecker(void)
1130 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
1131 int i, legendre, kronecker, st = 0;
1133 if (!TEST_ptr(a = BN_new())
1134 || !TEST_ptr(b = BN_new())
1135 || !TEST_ptr(r = BN_new())
1136 || !TEST_ptr(t = BN_new()))
1140 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1141 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1142 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1143 * generate a random prime b and compare these values for a number of
1144 * random a's. (That is, we run the Solovay-Strassen primality test to
1145 * confirm that b is prime, except that we don't want to test whether b
1146 * is prime but whether BN_kronecker works.)
1149 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
1151 BN_set_negative(b, rand_neg());
1153 for (i = 0; i < NUM0; i++) {
1154 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1156 BN_set_negative(a, rand_neg());
1158 /* t := (|b|-1)/2 (note that b is odd) */
1159 if (!TEST_true(BN_copy(t, b)))
1161 BN_set_negative(t, 0);
1162 if (!TEST_true(BN_sub_word(t, 1)))
1164 if (!TEST_true(BN_rshift1(t, t)))
1166 /* r := a^t mod b */
1167 BN_set_negative(b, 0);
1169 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
1171 BN_set_negative(b, 1);
1173 if (BN_is_word(r, 1))
1175 else if (BN_is_zero(r))
1178 if (!TEST_true(BN_add_word(r, 1)))
1180 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
1181 TEST_info("Legendre symbol computation failed");
1187 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
1189 /* we actually need BN_kronecker(a, |b|) */
1190 if (BN_is_negative(a) && BN_is_negative(b))
1191 kronecker = -kronecker;
1193 if (!TEST_int_eq(legendre, kronecker))
1206 static int file_sum(STANZA *s)
1208 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
1212 if (!TEST_ptr(a = getBN(s, "A"))
1213 || !TEST_ptr(b = getBN(s, "B"))
1214 || !TEST_ptr(sum = getBN(s, "Sum"))
1215 || !TEST_ptr(ret = BN_new()))
1218 if (!TEST_true(BN_add(ret, a, b))
1219 || !equalBN("A + B", sum, ret)
1220 || !TEST_true(BN_sub(ret, sum, a))
1221 || !equalBN("Sum - A", b, ret)
1222 || !TEST_true(BN_sub(ret, sum, b))
1223 || !equalBN("Sum - B", a, ret))
1227 * Test that the functions work when |r| and |a| point to the same BIGNUM,
1228 * or when |r| and |b| point to the same BIGNUM.
1229 * There is no test for all of |r|, |a|, and |b| pointint to the same BIGNUM.
1231 if (!TEST_true(BN_copy(ret, a))
1232 || !TEST_true(BN_add(ret, ret, b))
1233 || !equalBN("A + B (r is a)", sum, ret)
1234 || !TEST_true(BN_copy(ret, b))
1235 || !TEST_true(BN_add(ret, a, ret))
1236 || !equalBN("A + B (r is b)", sum, ret)
1237 || !TEST_true(BN_copy(ret, sum))
1238 || !TEST_true(BN_sub(ret, ret, a))
1239 || !equalBN("Sum - A (r is a)", b, ret)
1240 || !TEST_true(BN_copy(ret, a))
1241 || !TEST_true(BN_sub(ret, sum, ret))
1242 || !equalBN("Sum - A (r is b)", b, ret)
1243 || !TEST_true(BN_copy(ret, sum))
1244 || !TEST_true(BN_sub(ret, ret, b))
1245 || !equalBN("Sum - B (r is a)", a, ret)
1246 || !TEST_true(BN_copy(ret, b))
1247 || !TEST_true(BN_sub(ret, sum, ret))
1248 || !equalBN("Sum - B (r is b)", a, ret))
1252 * Test BN_uadd() and BN_usub() with the prerequisites they are
1253 * documented as having. Note that these functions are frequently used
1254 * when the prerequisites don't hold. In those cases, they are supposed
1255 * to work as if the prerequisite hold, but we don't test that yet.
1257 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
1258 if (!TEST_true(BN_uadd(ret, a, b))
1259 || !equalBN("A +u B", sum, ret)
1260 || !TEST_true(BN_usub(ret, sum, a))
1261 || !equalBN("Sum -u A", b, ret)
1262 || !TEST_true(BN_usub(ret, sum, b))
1263 || !equalBN("Sum -u B", a, ret))
1266 * Test that the functions work when |r| and |a| point to the same
1267 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1268 * There is no test for all of |r|, |a|, and |b| pointint to the same
1271 if (!TEST_true(BN_copy(ret, a))
1272 || !TEST_true(BN_uadd(ret, ret, b))
1273 || !equalBN("A +u B (r is a)", sum, ret)
1274 || !TEST_true(BN_copy(ret, b))
1275 || !TEST_true(BN_uadd(ret, a, ret))
1276 || !equalBN("A +u B (r is b)", sum, ret)
1277 || !TEST_true(BN_copy(ret, sum))
1278 || !TEST_true(BN_usub(ret, ret, a))
1279 || !equalBN("Sum -u A (r is a)", b, ret)
1280 || !TEST_true(BN_copy(ret, a))
1281 || !TEST_true(BN_usub(ret, sum, ret))
1282 || !equalBN("Sum -u A (r is b)", b, ret)
1283 || !TEST_true(BN_copy(ret, sum))
1284 || !TEST_true(BN_usub(ret, ret, b))
1285 || !equalBN("Sum -u B (r is a)", a, ret)
1286 || !TEST_true(BN_copy(ret, b))
1287 || !TEST_true(BN_usub(ret, sum, ret))
1288 || !equalBN("Sum -u B (r is b)", a, ret))
1293 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1295 b_word = BN_get_word(b);
1296 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1297 if (!TEST_true(BN_copy(ret, a))
1298 || !TEST_true(BN_add_word(ret, b_word))
1299 || !equalBN("A + B (word)", sum, ret)
1300 || !TEST_true(BN_copy(ret, sum))
1301 || !TEST_true(BN_sub_word(ret, b_word))
1302 || !equalBN("Sum - B (word)", a, ret))
1315 static int file_lshift1(STANZA *s)
1317 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1318 BIGNUM *two = NULL, *remainder = NULL;
1321 if (!TEST_ptr(a = getBN(s, "A"))
1322 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1323 || !TEST_ptr(zero = BN_new())
1324 || !TEST_ptr(ret = BN_new())
1325 || !TEST_ptr(two = BN_new())
1326 || !TEST_ptr(remainder = BN_new()))
1331 if (!TEST_true(BN_set_word(two, 2))
1332 || !TEST_true(BN_add(ret, a, a))
1333 || !equalBN("A + A", lshift1, ret)
1334 || !TEST_true(BN_mul(ret, a, two, ctx))
1335 || !equalBN("A * 2", lshift1, ret)
1336 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1337 || !equalBN("LShift1 / 2", a, ret)
1338 || !equalBN("LShift1 % 2", zero, remainder)
1339 || !TEST_true(BN_lshift1(ret, a))
1340 || !equalBN("A << 1", lshift1, ret)
1341 || !TEST_true(BN_rshift1(ret, lshift1))
1342 || !equalBN("LShift >> 1", a, ret)
1343 || !TEST_true(BN_rshift1(ret, lshift1))
1344 || !equalBN("LShift >> 1", a, ret))
1347 /* Set the LSB to 1 and test rshift1 again. */
1348 if (!TEST_true(BN_set_bit(lshift1, 0))
1349 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1350 || !equalBN("(LShift1 | 1) / 2", a, ret)
1351 || !TEST_true(BN_rshift1(ret, lshift1))
1352 || !equalBN("(LShift | 1) >> 1", a, ret))
1367 static int file_lshift(STANZA *s)
1369 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1372 if (!TEST_ptr(a = getBN(s, "A"))
1373 || !TEST_ptr(lshift = getBN(s, "LShift"))
1374 || !TEST_ptr(ret = BN_new())
1375 || !getint(s, &n, "N"))
1378 if (!TEST_true(BN_lshift(ret, a, n))
1379 || !equalBN("A << N", lshift, ret)
1380 || !TEST_true(BN_rshift(ret, lshift, n))
1381 || !equalBN("A >> N", a, ret))
1392 static int file_rshift(STANZA *s)
1394 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1397 if (!TEST_ptr(a = getBN(s, "A"))
1398 || !TEST_ptr(rshift = getBN(s, "RShift"))
1399 || !TEST_ptr(ret = BN_new())
1400 || !getint(s, &n, "N"))
1403 if (!TEST_true(BN_rshift(ret, a, n))
1404 || !equalBN("A >> N", rshift, ret))
1407 /* If N == 1, try with rshift1 as well */
1409 if (!TEST_true(BN_rshift1(ret, a))
1410 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1422 static int file_square(STANZA *s)
1424 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1425 BIGNUM *remainder = NULL, *tmp = NULL;
1428 if (!TEST_ptr(a = getBN(s, "A"))
1429 || !TEST_ptr(square = getBN(s, "Square"))
1430 || !TEST_ptr(zero = BN_new())
1431 || !TEST_ptr(ret = BN_new())
1432 || !TEST_ptr(remainder = BN_new()))
1436 if (!TEST_true(BN_sqr(ret, a, ctx))
1437 || !equalBN("A^2", square, ret)
1438 || !TEST_true(BN_mul(ret, a, a, ctx))
1439 || !equalBN("A * A", square, ret)
1440 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1441 || !equalBN("Square / A", a, ret)
1442 || !equalBN("Square % A", zero, remainder))
1446 BN_set_negative(a, 0);
1447 if (!TEST_true(BN_sqrt(ret, square, ctx))
1448 || !equalBN("sqrt(Square)", a, ret))
1451 /* BN_sqrt should fail on non-squares and negative numbers. */
1452 if (!TEST_BN_eq_zero(square)) {
1453 if (!TEST_ptr(tmp = BN_new())
1454 || !TEST_true(BN_copy(tmp, square)))
1456 BN_set_negative(tmp, 1);
1458 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1462 BN_set_negative(tmp, 0);
1463 if (BN_add(tmp, tmp, BN_value_one()))
1465 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1482 static int file_product(STANZA *s)
1484 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1485 BIGNUM *remainder = NULL, *zero = NULL;
1488 if (!TEST_ptr(a = getBN(s, "A"))
1489 || !TEST_ptr(b = getBN(s, "B"))
1490 || !TEST_ptr(product = getBN(s, "Product"))
1491 || !TEST_ptr(ret = BN_new())
1492 || !TEST_ptr(remainder = BN_new())
1493 || !TEST_ptr(zero = BN_new()))
1498 if (!TEST_true(BN_mul(ret, a, b, ctx))
1499 || !equalBN("A * B", product, ret)
1500 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1501 || !equalBN("Product / A", b, ret)
1502 || !equalBN("Product % A", zero, remainder)
1503 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1504 || !equalBN("Product / B", a, ret)
1505 || !equalBN("Product % B", zero, remainder))
1519 static int file_quotient(STANZA *s)
1521 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1522 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1523 BN_ULONG b_word, ret_word;
1526 if (!TEST_ptr(a = getBN(s, "A"))
1527 || !TEST_ptr(b = getBN(s, "B"))
1528 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1529 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1530 || !TEST_ptr(ret = BN_new())
1531 || !TEST_ptr(ret2 = BN_new())
1532 || !TEST_ptr(nnmod = BN_new()))
1535 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1536 || !equalBN("A / B", quotient, ret)
1537 || !equalBN("A % B", remainder, ret2)
1538 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1539 || !TEST_true(BN_add(ret, ret, remainder))
1540 || !equalBN("Quotient * B + Remainder", a, ret))
1544 * Test with BN_mod_word() and BN_div_word() if the divisor is
1547 b_word = BN_get_word(b);
1548 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1549 BN_ULONG remainder_word = BN_get_word(remainder);
1551 assert(remainder_word != (BN_ULONG)-1);
1552 if (!TEST_ptr(BN_copy(ret, a)))
1554 ret_word = BN_div_word(ret, 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");
1565 if (!equalBN ("A / B (word)", quotient, ret))
1568 ret_word = BN_mod_word(a, b_word);
1569 if (ret_word != remainder_word) {
1572 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1573 ret_word, remainder_word);
1575 TEST_error("Got A %% B (word) mismatch");
1581 /* Test BN_nnmod. */
1582 if (!BN_is_negative(b)) {
1583 if (!TEST_true(BN_copy(nnmod, remainder))
1584 || (BN_is_negative(nnmod)
1585 && !TEST_true(BN_add(nnmod, nnmod, b)))
1586 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1587 || !equalBN("A % B (non-negative)", nnmod, ret))
1603 static int file_modmul(STANZA *s)
1605 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1608 if (!TEST_ptr(a = getBN(s, "A"))
1609 || !TEST_ptr(b = getBN(s, "B"))
1610 || !TEST_ptr(m = getBN(s, "M"))
1611 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1612 || !TEST_ptr(ret = BN_new()))
1615 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1616 || !equalBN("A * B (mod M)", mod_mul, ret))
1620 /* Reduce |a| and |b| and test the Montgomery version. */
1621 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1622 BIGNUM *a_tmp = BN_new();
1623 BIGNUM *b_tmp = BN_new();
1625 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1626 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1627 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1628 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1629 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1630 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1631 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1633 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1634 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1638 BN_MONT_CTX_free(mont);
1655 static int file_modexp(STANZA *s)
1657 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1658 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1661 if (!TEST_ptr(a = getBN(s, "A"))
1662 || !TEST_ptr(e = getBN(s, "E"))
1663 || !TEST_ptr(m = getBN(s, "M"))
1664 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1665 || !TEST_ptr(ret = BN_new())
1666 || !TEST_ptr(d = BN_new()))
1669 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1670 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1674 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1675 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1676 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1678 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1682 /* Regression test for carry propagation bug in sqr8x_reduction */
1683 BN_hex2bn(&a, "050505050505");
1684 BN_hex2bn(&b, "02");
1686 "4141414141414141414141274141414141414141414141414141414141414141"
1687 "4141414141414141414141414141414141414141414141414141414141414141"
1688 "4141414141414141414141800000000000000000000000000000000000000000"
1689 "0000000000000000000000000000000000000000000000000000000000000000"
1690 "0000000000000000000000000000000000000000000000000000000000000000"
1691 "0000000000000000000000000000000000000000000000000000000001");
1692 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1693 || !TEST_true(BN_mul(e, a, a, ctx))
1694 || !TEST_BN_eq(d, e))
1710 static int file_exp(STANZA *s)
1712 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1715 if (!TEST_ptr(a = getBN(s, "A"))
1716 || !TEST_ptr(e = getBN(s, "E"))
1717 || !TEST_ptr(exp = getBN(s, "Exp"))
1718 || !TEST_ptr(ret = BN_new()))
1721 if (!TEST_true(BN_exp(ret, a, e, ctx))
1722 || !equalBN("A ^ E", exp, ret))
1734 static int file_modsqrt(STANZA *s)
1736 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1739 if (!TEST_ptr(a = getBN(s, "A"))
1740 || !TEST_ptr(p = getBN(s, "P"))
1741 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1742 || !TEST_ptr(ret = BN_new())
1743 || !TEST_ptr(ret2 = BN_new()))
1746 if (BN_is_negative(mod_sqrt)) {
1747 /* A negative testcase */
1748 if (!TEST_ptr_null(BN_mod_sqrt(ret, a, p, ctx)))
1755 /* There are two possible answers. */
1756 if (!TEST_ptr(BN_mod_sqrt(ret, a, p, ctx))
1757 || !TEST_true(BN_sub(ret2, p, ret)))
1760 /* The first condition should NOT be a test. */
1761 if (BN_cmp(ret2, mod_sqrt) != 0
1762 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1775 static int file_gcd(STANZA *s)
1777 BIGNUM *a = NULL, *b = NULL, *gcd = NULL, *ret = NULL;
1780 if (!TEST_ptr(a = getBN(s, "A"))
1781 || !TEST_ptr(b = getBN(s, "B"))
1782 || !TEST_ptr(gcd = getBN(s, "GCD"))
1783 || !TEST_ptr(ret = BN_new()))
1786 if (!TEST_true(BN_gcd(ret, a, b, ctx))
1787 || !equalBN("gcd(A,B)", gcd, ret))
1799 static int test_bn2padded(void)
1801 uint8_t zeros[256], out[256], reference[128];
1806 /* Test edge case at 0. */
1807 if (!TEST_ptr((n = BN_new())))
1809 if (!TEST_int_eq(BN_bn2binpad(n, NULL, 0), 0))
1811 memset(out, -1, sizeof(out));
1812 if (!TEST_int_eq(BN_bn2binpad(n, out, sizeof(out)), sizeof(out)))
1814 memset(zeros, 0, sizeof(zeros));
1815 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1818 /* Test a random numbers at various byte lengths. */
1819 for (bytes = 128 - 7; bytes <= 128; bytes++) {
1820 # define TOP_BIT_ON 0
1821 # define BOTTOM_BIT_NOTOUCH 0
1822 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1824 if (!TEST_int_eq(BN_num_bytes(n), bytes)
1825 || !TEST_int_eq(BN_bn2bin(n, reference), bytes))
1827 /* Empty buffer should fail. */
1828 if (!TEST_int_eq(BN_bn2binpad(n, NULL, 0), -1))
1830 /* One byte short should fail. */
1831 if (!TEST_int_eq(BN_bn2binpad(n, out, bytes - 1), -1))
1833 /* Exactly right size should encode. */
1834 if (!TEST_int_eq(BN_bn2binpad(n, out, bytes), bytes)
1835 || !TEST_mem_eq(out, bytes, reference, bytes))
1837 /* Pad up one byte extra. */
1838 if (!TEST_int_eq(BN_bn2binpad(n, out, bytes + 1), bytes + 1)
1839 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1840 || !TEST_mem_eq(out, 1, zeros, 1))
1842 /* Pad up to 256. */
1843 if (!TEST_int_eq(BN_bn2binpad(n, out, sizeof(out)), sizeof(out))
1844 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1846 || !TEST_mem_eq(out, sizeof(out) - bytes,
1847 zeros, sizeof(out) - bytes))
1857 static const MPITEST kSignedTests_BE[] = {
1862 * The above cover the basics, now let's go for possible bignum
1863 * chunk edges and other word edges (for a broad definition of
1864 * "word", i.e. 1 byte included).
1868 {"-127", "\x81", 1},
1869 {"128", "\x00\x80", 2},
1870 {"-128", "\x80", 1},
1871 {"129", "\x00\x81", 2},
1872 {"-129", "\xff\x7f", 2},
1873 {"255", "\x00\xff", 2},
1874 {"-255", "\xff\x01", 2},
1875 {"256", "\x01\x00", 2},
1876 {"-256", "\xff\x00", 2},
1878 {"32767", "\x7f\xff", 2},
1879 {"-32767", "\x80\x01", 2},
1880 {"32768", "\x00\x80\x00", 3},
1881 {"-32768", "\x80\x00", 2},
1882 {"32769", "\x00\x80\x01", 3},
1883 {"-32769", "\xff\x7f\xff", 3},
1884 {"65535", "\x00\xff\xff", 3},
1885 {"-65535", "\xff\x00\x01", 3},
1886 {"65536", "\x01\x00\x00", 3},
1887 {"-65536", "\xff\x00\x00", 3},
1889 {"2147483647", "\x7f\xff\xff\xff", 4},
1890 {"-2147483647", "\x80\x00\x00\x01", 4},
1891 {"2147483648", "\x00\x80\x00\x00\x00", 5},
1892 {"-2147483648", "\x80\x00\x00\x00", 4},
1893 {"2147483649", "\x00\x80\x00\x00\x01", 5},
1894 {"-2147483649", "\xff\x7f\xff\xff\xff", 5},
1895 {"4294967295", "\x00\xff\xff\xff\xff", 5},
1896 {"-4294967295", "\xff\x00\x00\x00\x01", 5},
1897 {"4294967296", "\x01\x00\x00\x00\x00", 5},
1898 {"-4294967296", "\xff\x00\x00\x00\x00", 5},
1900 {"9223372036854775807", "\x7f\xff\xff\xff\xff\xff\xff\xff", 8},
1901 {"-9223372036854775807", "\x80\x00\x00\x00\x00\x00\x00\x01", 8},
1902 {"9223372036854775808", "\x00\x80\x00\x00\x00\x00\x00\x00\x00", 9},
1903 {"-9223372036854775808", "\x80\x00\x00\x00\x00\x00\x00\x00", 8},
1904 {"9223372036854775809", "\x00\x80\x00\x00\x00\x00\x00\x00\x01", 9},
1905 {"-9223372036854775809", "\xff\x7f\xff\xff\xff\xff\xff\xff\xff", 9},
1906 {"18446744073709551615", "\x00\xff\xff\xff\xff\xff\xff\xff\xff", 9},
1907 {"-18446744073709551615", "\xff\x00\x00\x00\x00\x00\x00\x00\x01", 9},
1908 {"18446744073709551616", "\x01\x00\x00\x00\x00\x00\x00\x00\x00", 9},
1909 {"-18446744073709551616", "\xff\x00\x00\x00\x00\x00\x00\x00\x00", 9},
1912 static int copy_reversed(uint8_t *dst, uint8_t *src, size_t len)
1914 for (dst += len - 1; len > 0; src++, dst--, len--)
1919 static int test_bn2signed(int i)
1921 uint8_t scratch[10], reversed[10];
1922 const MPITEST *test = &kSignedTests_BE[i];
1923 BIGNUM *bn = NULL, *bn2 = NULL;
1926 if (!TEST_ptr(bn = BN_new())
1927 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1931 * Check BN_signed_bn2bin() / BN_signed_bin2bn()
1932 * The interesting stuff happens in the last bytes of the buffers,
1933 * the beginning is just padding (i.e. sign extension).
1935 i = sizeof(scratch) - test->mpi_len;
1936 if (!TEST_int_eq(BN_signed_bn2bin(bn, scratch, sizeof(scratch)),
1938 || !TEST_true(copy_reversed(reversed, scratch, sizeof(scratch)))
1939 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch + i, test->mpi_len))
1942 if (!TEST_ptr(bn2 = BN_signed_bin2bn(scratch, sizeof(scratch), NULL))
1943 || !TEST_BN_eq(bn, bn2))
1949 /* Check that a parse of the reversed buffer works too */
1950 if (!TEST_ptr(bn2 = BN_signed_lebin2bn(reversed, sizeof(reversed), NULL))
1951 || !TEST_BN_eq(bn, bn2))
1958 * Check BN_signed_bn2lebin() / BN_signed_lebin2bn()
1959 * The interesting stuff happens in the first bytes of the buffers,
1960 * the end is just padding (i.e. sign extension).
1962 i = sizeof(reversed) - test->mpi_len;
1963 if (!TEST_int_eq(BN_signed_bn2lebin(bn, scratch, sizeof(scratch)),
1965 || !TEST_true(copy_reversed(reversed, scratch, sizeof(scratch)))
1966 || !TEST_mem_eq(test->mpi, test->mpi_len, reversed + i, test->mpi_len))
1969 if (!TEST_ptr(bn2 = BN_signed_lebin2bn(scratch, sizeof(scratch), NULL))
1970 || !TEST_BN_eq(bn, bn2))
1976 /* Check that a parse of the reversed buffer works too */
1977 if (!TEST_ptr(bn2 = BN_signed_bin2bn(reversed, sizeof(reversed), NULL))
1978 || !TEST_BN_eq(bn, bn2))
1988 static int test_dec2bn(void)
1993 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1994 || !TEST_BN_eq_word(bn, 0)
1995 || !TEST_BN_eq_zero(bn)
1996 || !TEST_BN_le_zero(bn)
1997 || !TEST_BN_ge_zero(bn)
1998 || !TEST_BN_even(bn))
2003 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
2004 || !TEST_BN_eq_word(bn, 256)
2005 || !TEST_BN_ge_zero(bn)
2006 || !TEST_BN_gt_zero(bn)
2007 || !TEST_BN_ne_zero(bn)
2008 || !TEST_BN_even(bn))
2013 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
2014 || !TEST_BN_abs_eq_word(bn, 42)
2015 || !TEST_BN_lt_zero(bn)
2016 || !TEST_BN_le_zero(bn)
2017 || !TEST_BN_ne_zero(bn)
2018 || !TEST_BN_even(bn))
2023 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
2024 || !TEST_BN_eq_word(bn, 1)
2025 || !TEST_BN_ne_zero(bn)
2026 || !TEST_BN_gt_zero(bn)
2027 || !TEST_BN_ge_zero(bn)
2028 || !TEST_BN_eq_one(bn)
2029 || !TEST_BN_odd(bn))
2034 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
2035 || !TEST_BN_eq_zero(bn)
2036 || !TEST_BN_ge_zero(bn)
2037 || !TEST_BN_le_zero(bn)
2038 || !TEST_BN_even(bn))
2043 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
2044 || !TEST_BN_abs_eq_word(bn, 42)
2045 || !TEST_BN_ge_zero(bn)
2046 || !TEST_BN_gt_zero(bn)
2047 || !TEST_BN_ne_zero(bn)
2048 || !TEST_BN_even(bn))
2057 static int test_hex2bn(void)
2062 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
2063 || !TEST_BN_eq_zero(bn)
2064 || !TEST_BN_ge_zero(bn)
2065 || !TEST_BN_even(bn))
2070 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
2071 || !TEST_BN_eq_word(bn, 0x256)
2072 || !TEST_BN_ge_zero(bn)
2073 || !TEST_BN_gt_zero(bn)
2074 || !TEST_BN_ne_zero(bn)
2075 || !TEST_BN_even(bn))
2080 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
2081 || !TEST_BN_abs_eq_word(bn, 0x42)
2082 || !TEST_BN_lt_zero(bn)
2083 || !TEST_BN_le_zero(bn)
2084 || !TEST_BN_ne_zero(bn)
2085 || !TEST_BN_even(bn))
2090 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
2091 || !TEST_BN_eq_word(bn, 0xCB)
2092 || !TEST_BN_ge_zero(bn)
2093 || !TEST_BN_gt_zero(bn)
2094 || !TEST_BN_ne_zero(bn)
2095 || !TEST_BN_odd(bn))
2100 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
2101 || !TEST_BN_eq_zero(bn)
2102 || !TEST_BN_ge_zero(bn)
2103 || !TEST_BN_le_zero(bn)
2104 || !TEST_BN_even(bn))
2109 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
2110 || !TEST_BN_eq_word(bn, 0xabc)
2111 || !TEST_BN_ge_zero(bn)
2112 || !TEST_BN_gt_zero(bn)
2113 || !TEST_BN_ne_zero(bn)
2114 || !TEST_BN_even(bn))
2123 static int test_asc2bn(void)
2128 if (!TEST_ptr(bn = BN_new()))
2131 if (!TEST_true(BN_asc2bn(&bn, "0"))
2132 || !TEST_BN_eq_zero(bn)
2133 || !TEST_BN_ge_zero(bn))
2136 if (!TEST_true(BN_asc2bn(&bn, "256"))
2137 || !TEST_BN_eq_word(bn, 256)
2138 || !TEST_BN_ge_zero(bn))
2141 if (!TEST_true(BN_asc2bn(&bn, "-42"))
2142 || !TEST_BN_abs_eq_word(bn, 42)
2143 || !TEST_BN_lt_zero(bn))
2146 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
2147 || !TEST_BN_eq_word(bn, 0x1234)
2148 || !TEST_BN_ge_zero(bn))
2151 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
2152 || !TEST_BN_eq_word(bn, 0x1234)
2153 || !TEST_BN_ge_zero(bn))
2156 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
2157 || !TEST_BN_abs_eq_word(bn, 0xabcd)
2158 || !TEST_BN_lt_zero(bn))
2161 if (!TEST_true(BN_asc2bn(&bn, "-0"))
2162 || !TEST_BN_eq_zero(bn)
2163 || !TEST_BN_ge_zero(bn))
2166 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
2167 || !TEST_BN_eq_word(bn, 123)
2168 || !TEST_BN_ge_zero(bn))
2177 static const MPITEST kMPITests[] = {
2178 {"0", "\x00\x00\x00\x00", 4},
2179 {"1", "\x00\x00\x00\x01\x01", 5},
2180 {"-1", "\x00\x00\x00\x01\x81", 5},
2181 {"128", "\x00\x00\x00\x02\x00\x80", 6},
2182 {"256", "\x00\x00\x00\x02\x01\x00", 6},
2183 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
2186 static int test_mpi(int i)
2189 const MPITEST *test = &kMPITests[i];
2190 size_t mpi_len, mpi_len2;
2195 if (!TEST_ptr(bn = BN_new())
2196 || !TEST_true(BN_asc2bn(&bn, test->base10)))
2198 mpi_len = BN_bn2mpi(bn, NULL);
2199 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
2202 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
2203 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
2206 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
2209 if (!TEST_BN_eq(bn, bn2)) {
2221 static int test_rand(void)
2226 if (!TEST_ptr(bn = BN_new()))
2229 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
2230 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
2231 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
2232 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
2233 || !TEST_BN_eq_one(bn)
2234 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
2235 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
2236 || !TEST_BN_eq_one(bn)
2237 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
2238 || !TEST_BN_eq_word(bn, 3))
2248 * Run some statistical tests to provide a degree confidence that the
2249 * BN_rand_range() function works as expected. The test cases and
2250 * critical values are generated by the bn_rand_range script.
2252 * Each individual test is a Chi^2 goodness of fit for a specified number
2253 * of samples and range. The samples are assumed to be independent and
2254 * that they are from a discrete uniform distribution.
2256 * Some of these individual tests are expected to fail, the success/failure
2257 * of each is an independent Bernoulli trial. The number of such successes
2258 * will form a binomial distribution. The count of the successes is compared
2259 * against a precomputed critical value to determine the overall outcome.
2261 struct rand_range_case {
2263 unsigned int iterations;
2267 #include "bn_rand_range.h"
2269 static int test_rand_range_single(size_t n)
2271 const unsigned int range = rand_range_cases[n].range;
2272 const unsigned int iterations = rand_range_cases[n].iterations;
2273 const double critical = rand_range_cases[n].critical;
2274 const double expected = iterations / (double)range;
2276 BIGNUM *rng = NULL, *val = NULL;
2281 if (!TEST_ptr(counts = OPENSSL_zalloc(sizeof(*counts) * range))
2282 || !TEST_ptr(rng = BN_new())
2283 || !TEST_ptr(val = BN_new())
2284 || !TEST_true(BN_set_word(rng, range)))
2286 for (i = 0; i < iterations; i++) {
2287 if (!TEST_true(BN_rand_range(val, rng))
2288 || !TEST_uint_lt(v = (unsigned int)BN_get_word(val), range))
2293 for (i = 0; i < range; i++) {
2294 const double delta = counts[i] - expected;
2295 sum += delta * delta;
2299 if (sum > critical) {
2300 TEST_info("Chi^2 test negative %.4f > %4.f", sum, critical);
2301 TEST_note("test case %zu range %u iterations %u", n + 1, range,
2310 OPENSSL_free(counts);
2314 static int test_rand_range(void)
2319 for (i = 0; i < OSSL_NELEM(rand_range_cases); i++)
2320 n_success += test_rand_range_single(i);
2321 if (TEST_int_ge(n_success, binomial_critical))
2323 TEST_note("This test is expected to fail by chance 0.01%% of the time.");
2327 static int test_negzero(void)
2329 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
2330 BIGNUM *numerator = NULL, *denominator = NULL;
2331 int consttime, st = 0;
2333 if (!TEST_ptr(a = BN_new())
2334 || !TEST_ptr(b = BN_new())
2335 || !TEST_ptr(c = BN_new())
2336 || !TEST_ptr(d = BN_new()))
2339 /* Test that BN_mul never gives negative zero. */
2340 if (!TEST_true(BN_set_word(a, 1)))
2342 BN_set_negative(a, 1);
2344 if (!TEST_true(BN_mul(c, a, b, ctx)))
2346 if (!TEST_BN_eq_zero(c)
2347 || !TEST_BN_ge_zero(c))
2350 for (consttime = 0; consttime < 2; consttime++) {
2351 if (!TEST_ptr(numerator = BN_new())
2352 || !TEST_ptr(denominator = BN_new()))
2355 BN_set_flags(numerator, BN_FLG_CONSTTIME);
2356 BN_set_flags(denominator, BN_FLG_CONSTTIME);
2358 /* Test that BN_div never gives negative zero in the quotient. */
2359 if (!TEST_true(BN_set_word(numerator, 1))
2360 || !TEST_true(BN_set_word(denominator, 2)))
2362 BN_set_negative(numerator, 1);
2363 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
2364 || !TEST_BN_eq_zero(a)
2365 || !TEST_BN_ge_zero(a))
2368 /* Test that BN_div never gives negative zero in the remainder. */
2369 if (!TEST_true(BN_set_word(denominator, 1))
2370 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
2371 || !TEST_BN_eq_zero(b)
2372 || !TEST_BN_ge_zero(b))
2375 BN_free(denominator);
2376 numerator = denominator = NULL;
2379 /* Test that BN_set_negative will not produce a negative zero. */
2381 BN_set_negative(a, 1);
2382 if (BN_is_negative(a))
2392 BN_free(denominator);
2396 static int test_badmod(void)
2398 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
2399 BN_MONT_CTX *mont = NULL;
2402 if (!TEST_ptr(a = BN_new())
2403 || !TEST_ptr(b = BN_new())
2404 || !TEST_ptr(zero = BN_new())
2405 || !TEST_ptr(mont = BN_MONT_CTX_new()))
2409 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
2413 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
2417 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
2421 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2426 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2431 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
2435 /* Some operations also may not be used with an even modulus. */
2436 if (!TEST_true(BN_set_word(b, 16)))
2439 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
2443 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2448 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2458 BN_MONT_CTX_free(mont);
2462 static int test_expmodzero(void)
2464 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
2467 if (!TEST_ptr(zero = BN_new())
2468 || !TEST_ptr(a = BN_new())
2469 || !TEST_ptr(r = BN_new()))
2473 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
2474 || !TEST_BN_eq_zero(r)
2475 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
2477 || !TEST_BN_eq_zero(r)
2478 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
2481 || !TEST_BN_eq_zero(r)
2482 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
2483 BN_value_one(), NULL, NULL))
2484 || !TEST_BN_eq_zero(r))
2495 static int test_expmodone(void)
2498 BIGNUM *r = BN_new();
2499 BIGNUM *a = BN_new();
2500 BIGNUM *p = BN_new();
2501 BIGNUM *m = BN_new();
2508 || !TEST_true(BN_set_word(a, 1))
2509 || !TEST_true(BN_set_word(p, 0))
2510 || !TEST_true(BN_set_word(m, 1)))
2513 /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
2514 for (i = 0; i < 2; i++) {
2515 if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
2516 || !TEST_BN_eq_zero(r)
2517 || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
2518 || !TEST_BN_eq_zero(r)
2519 || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
2520 || !TEST_BN_eq_zero(r)
2521 || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
2522 || !TEST_BN_eq_zero(r)
2523 || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
2524 || !TEST_BN_eq_zero(r)
2525 || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
2526 || !TEST_BN_eq_zero(r))
2528 /* Repeat for r = 1 ^ 0 mod -1 */
2530 BN_set_negative(m, 1);
2542 static int test_smallprime(int kBits)
2547 if (!TEST_ptr(r = BN_new()))
2551 if (!TEST_false(BN_generate_prime_ex(r, kBits, 0,
2555 if (!TEST_true(BN_generate_prime_ex(r, kBits, 0,
2557 || !TEST_int_eq(BN_num_bits(r), kBits))
2567 static int test_smallsafeprime(int kBits)
2572 if (!TEST_ptr(r = BN_new()))
2575 if (kBits <= 5 && kBits != 3) {
2576 if (!TEST_false(BN_generate_prime_ex(r, kBits, 1,
2580 if (!TEST_true(BN_generate_prime_ex(r, kBits, 1,
2582 || !TEST_int_eq(BN_num_bits(r), kBits))
2592 static int primes[] = { 2, 3, 5, 7, 17863 };
2594 static int test_is_prime(int i)
2600 if (!TEST_ptr(r = BN_new()))
2603 for (trial = 0; trial <= 1; ++trial) {
2604 if (!TEST_true(BN_set_word(r, primes[i]))
2605 || !TEST_int_eq(BN_check_prime(r, ctx, NULL),
2616 static int not_primes[] = { -1, 0, 1, 4 };
2618 static int test_not_prime(int i)
2624 if (!TEST_ptr(r = BN_new()))
2627 for (trial = 0; trial <= 1; ++trial) {
2628 if (!TEST_true(BN_set_word(r, not_primes[i]))
2629 || !TEST_false(BN_check_prime(r, ctx, NULL)))
2639 static int test_ctx_set_ct_flag(BN_CTX *c)
2646 for (i = 0; i < OSSL_NELEM(b); i++) {
2647 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2650 BN_set_flags(b[i], BN_FLG_CONSTTIME);
2659 static int test_ctx_check_ct_flag(BN_CTX *c)
2666 for (i = 0; i < OSSL_NELEM(b); i++) {
2667 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2669 if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
2679 static int test_ctx_consttime_flag(void)
2682 * The constant-time flag should not "leak" among BN_CTX frames:
2684 * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
2685 * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
2686 * from the frame before ending it.
2687 * - test_ctx_check_ct_flag() then starts a new frame and gets a
2688 * number of BIGNUMs from it. In absence of leaks, none of the
2689 * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
2691 * In actual BN_CTX usage inside libcrypto the leak could happen at
2692 * any depth level in the BN_CTX stack, with varying results
2693 * depending on the patterns of sibling trees of nested function
2694 * calls sharing the same BN_CTX object, and the effect of
2695 * unintended BN_FLG_CONSTTIME on the called BN_* functions.
2697 * This simple unit test abstracts away this complexity and verifies
2698 * that the leak does not happen between two sibling functions
2699 * sharing the same BN_CTX object at the same level of nesting.
2702 BN_CTX *nctx = NULL;
2703 BN_CTX *sctx = NULL;
2707 if (!TEST_ptr(nctx = BN_CTX_new())
2708 || !TEST_ptr(sctx = BN_CTX_secure_new()))
2711 for (i = 0; i < 2; i++) {
2712 BN_CTX *c = i == 0 ? nctx : sctx;
2713 if (!TEST_true(test_ctx_set_ct_flag(c))
2714 || !TEST_true(test_ctx_check_ct_flag(c)))
2725 static int test_gcd_prime(void)
2727 BIGNUM *a = NULL, *b = NULL, *gcd = NULL;
2730 if (!TEST_ptr(a = BN_new())
2731 || !TEST_ptr(b = BN_new())
2732 || !TEST_ptr(gcd = BN_new()))
2735 if (!TEST_true(BN_generate_prime_ex(a, 1024, 0, NULL, NULL, NULL)))
2737 for (i = 0; i < NUM0; i++) {
2738 if (!TEST_true(BN_generate_prime_ex(b, 1024, 0,
2740 || !TEST_true(BN_gcd(gcd, a, b, ctx))
2741 || !TEST_true(BN_is_one(gcd)))
2753 typedef struct mod_exp_test_st
2761 static const MOD_EXP_TEST ModExpTests[] = {
2762 /* original test vectors for rsaz_512_sqr bug, by OSS-Fuzz */
2764 "1166180238001879113042182292626169621106255558914000595999312084"
2765 "4627946820899490684928760491249738643524880720584249698100907201"
2766 "002086675047927600340800371",
2767 "8000000000000000000000000000000000000000000000000000000000000000"
2768 "0000000000000000000000000000000000000000000000000000000000000000"
2770 "1340780792684523720980737645613191762604395855615117867483316354"
2771 "3294276330515137663421134775482798690129946803802212663956180562"
2772 "088664022929883876655300863",
2773 "8243904058268085430037326628480645845409758077568738532059032482"
2774 "8294114415890603594730158120426756266457928475330450251339773498"
2775 "26758407619521544102068438"
2778 "4974270041410803822078866696159586946995877618987010219312844726"
2779 "0284386121835740784990869050050504348861513337232530490826340663"
2780 "197278031692737429054",
2781 "4974270041410803822078866696159586946995877428188754995041148539"
2782 "1663243362592271353668158565195557417149981094324650322556843202"
2783 "946445882670777892608",
2784 "1340780716511420227215592830971452482815377482627251725537099028"
2785 "4429769497230131760206012644403029349547320953206103351725462999"
2786 "947509743623340557059752191",
2787 "5296244594780707015616522701706118082963369547253192207884519362"
2788 "1767869984947542695665420219028522815539559194793619684334900442"
2789 "49304558011362360473525933"
2791 /* test vectors for rsaz_512_srq bug, with rcx/rbx=1 */
2792 { /* between first and second iteration */
2793 "5148719036160389201525610950887605325980251964889646556085286545"
2794 "3931548809178823413169359635978762036512397113080988070677858033"
2795 "36463909753993540214027190",
2796 "6703903964971298549787012499102923063739682910296196688861780721"
2797 "8608820150367734884009371490834517138450159290932430254268769414"
2798 "05973284973216824503042158",
2799 "6703903964971298549787012499102923063739682910296196688861780721"
2800 "8608820150367734884009371490834517138450159290932430254268769414"
2801 "05973284973216824503042159",
2804 { /* between second and third iteration */
2805 "8908340854353752577419678771330460827942371434853054158622636544"
2806 "8151360109722890949471912566649465436296659601091730745087014189"
2807 "2672764191218875181826063",
2808 "6703903964971298549787012499102923063739682910296196688861780721"
2809 "8608820150367734884009371490834517138450159290932430254268769414"
2810 "05973284973216824503042158",
2811 "6703903964971298549787012499102923063739682910296196688861780721"
2812 "8608820150367734884009371490834517138450159290932430254268769414"
2813 "05973284973216824503042159",
2816 { /* between third and fourth iteration */
2817 "3427446396505596330634350984901719674479522569002785244080234738"
2818 "4288743635435746136297299366444548736533053717416735379073185344"
2819 "26985272974404612945608761",
2820 "6703903964971298549787012499102923063739682910296196688861780721"
2821 "8608820150367734884009371490834517138450159290932430254268769414"
2822 "05973284973216824503042158",
2823 "6703903964971298549787012499102923063739682910296196688861780721"
2824 "8608820150367734884009371490834517138450159290932430254268769414"
2825 "05973284973216824503042159",
2828 { /* between fourth and fifth iteration */
2829 "3472743044917564564078857826111874560045331237315597383869652985"
2830 "6919870028890895988478351133601517365908445058405433832718206902"
2831 "4088133164805266956353542",
2832 "6703903964971298549787012499102923063739682910296196688861780721"
2833 "8608820150367734884009371490834517138450159290932430254268769414"
2834 "05973284973216824503042158",
2835 "6703903964971298549787012499102923063739682910296196688861780721"
2836 "8608820150367734884009371490834517138450159290932430254268769414"
2837 "05973284973216824503042159",
2840 { /* between fifth and sixth iteration */
2841 "3608632990153469264412378349742339216742409743898601587274768025"
2842 "0110772032985643555192767717344946174122842255204082586753499651"
2843 "14483434992887431333675068",
2844 "6703903964971298549787012499102923063739682910296196688861780721"
2845 "8608820150367734884009371490834517138450159290932430254268769414"
2846 "05973284973216824503042158",
2847 "6703903964971298549787012499102923063739682910296196688861780721"
2848 "8608820150367734884009371490834517138450159290932430254268769414"
2849 "05973284973216824503042159",
2852 { /* between sixth and seventh iteration */
2853 "8455374370234070242910508226941981520235709767260723212165264877"
2854 "8689064388017521524568434328264431772644802567028663962962025746"
2855 "9283458217850119569539086",
2856 "6703903964971298549787012499102923063739682910296196688861780721"
2857 "8608820150367734884009371490834517138450159290932430254268769414"
2858 "05973284973216824503042158",
2859 "6703903964971298549787012499102923063739682910296196688861780721"
2860 "8608820150367734884009371490834517138450159290932430254268769414"
2861 "05973284973216824503042159",
2864 { /* between seventh and eighth iteration */
2865 "5155371529688532178421209781159131443543419764974688878527112131"
2866 "7446518205609427412336183157918981038066636807317733319323257603"
2867 "04416292040754017461076359",
2868 "1005585594745694782468051874865438459560952436544429503329267108"
2869 "2791323022555160232601405723625177570767523893639864538140315412"
2870 "108959927459825236754563832",
2871 "1005585594745694782468051874865438459560952436544429503329267108"
2872 "2791323022555160232601405723625177570767523893639864538140315412"
2873 "108959927459825236754563833",
2876 /* test vectors for rsaz_512_srq bug, with rcx/rbx=2 */
2877 { /* between first and second iteration */
2878 "3155666506033786929967309937640790361084670559125912405342594979"
2879 "4345142818528956285490897841406338022378565972533508820577760065"
2880 "58494345853302083699912572",
2881 "6703903964971298549787012499102923063739682910296196688861780721"
2882 "8608820150367734884009371490834517138450159290932430254268769414"
2883 "05973284973216824503042158",
2884 "6703903964971298549787012499102923063739682910296196688861780721"
2885 "8608820150367734884009371490834517138450159290932430254268769414"
2886 "05973284973216824503042159",
2889 { /* between second and third iteration */
2890 "3789819583801342198190405714582958759005991915505282362397087750"
2891 "4213544724644823098843135685133927198668818185338794377239590049"
2892 "41019388529192775771488319",
2893 "6703903964971298549787012499102923063739682910296196688861780721"
2894 "8608820150367734884009371490834517138450159290932430254268769414"
2895 "05973284973216824503042158",
2896 "6703903964971298549787012499102923063739682910296196688861780721"
2897 "8608820150367734884009371490834517138450159290932430254268769414"
2898 "05973284973216824503042159",
2901 { /* between third and forth iteration */
2902 "4695752552040706867080542538786056470322165281761525158189220280"
2903 "4025547447667484759200742764246905647644662050122968912279199065"
2904 "48065034299166336940507214",
2905 "6703903964971298549787012499102923063739682910296196688861780721"
2906 "8608820150367734884009371490834517138450159290932430254268769414"
2907 "05973284973216824503042158",
2908 "6703903964971298549787012499102923063739682910296196688861780721"
2909 "8608820150367734884009371490834517138450159290932430254268769414"
2910 "05973284973216824503042159",
2913 { /* between forth and fifth iteration */
2914 "2159140240970485794188159431017382878636879856244045329971239574"
2915 "8919691133560661162828034323196457386059819832804593989740268964"
2916 "74502911811812651475927076",
2917 "6703903964971298549787012499102923063739682910296196688861780721"
2918 "8608820150367734884009371490834517138450159290932430254268769414"
2919 "05973284973216824503042158",
2920 "6703903964971298549787012499102923063739682910296196688861780721"
2921 "8608820150367734884009371490834517138450159290932430254268769414"
2922 "05973284973216824503042159",
2925 { /* between fifth and sixth iteration */
2926 "5239312332984325668414624633307915097111691815000872662334695514"
2927 "5436533521392362443557163429336808208137221322444780490437871903"
2928 "99972784701334569424519255",
2929 "6703903964971298549787012499102923063739682910296196688861780721"
2930 "8608820150367734884009371490834517138450159290932430254268769414"
2931 "05973284973216824503042158",
2932 "6703903964971298549787012499102923063739682910296196688861780721"
2933 "8608820150367734884009371490834517138450159290932430254268769414"
2934 "05973284973216824503042159",
2937 { /* between sixth and seventh iteration */
2938 "1977953647322612860406858017869125467496941904523063466791308891"
2939 "1172796739058531929470539758361774569875505293428856181093904091"
2940 "33788264851714311303725089",
2941 "6703903964971298549787012499102923063739682910296196688861780721"
2942 "8608820150367734884009371490834517138450159290932430254268769414"
2943 "05973284973216824503042158",
2944 "6703903964971298549787012499102923063739682910296196688861780721"
2945 "8608820150367734884009371490834517138450159290932430254268769414"
2946 "05973284973216824503042159",
2949 { /* between seventh and eighth iteration */
2950 "6456987954117763835533395796948878140715006860263624787492985786"
2951 "8514630216966738305923915688821526449499763719943997120302368211"
2952 "04813318117996225041943964",
2953 "1340780792994259709957402499820584612747936582059239337772356144"
2954 "3721764030073546976801874298166903427690031858186486050853753882"
2955 "811946551499689575296532556",
2956 "1340780792994259709957402499820584612747936582059239337772356144"
2957 "3721764030073546976801874298166903427690031858186486050853753882"
2958 "811946551499689575296532557",
2963 static int test_mod_exp(int i)
2965 const MOD_EXP_TEST *test = &ModExpTests[i];
2967 BIGNUM* result = NULL;
2968 BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
2971 if (!TEST_ptr(result = BN_new())
2972 || !TEST_true(BN_dec2bn(&base, test->base))
2973 || !TEST_true(BN_dec2bn(&exponent, test->exp))
2974 || !TEST_true(BN_dec2bn(&modulo, test->mod)))
2977 if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
2980 if (!TEST_ptr(s = BN_bn2dec(result)))
2983 if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
2997 static int test_mod_exp_consttime(int i)
2999 const MOD_EXP_TEST *test = &ModExpTests[i];
3001 BIGNUM* result = NULL;
3002 BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
3005 if (!TEST_ptr(result = BN_new())
3006 || !TEST_true(BN_dec2bn(&base, test->base))
3007 || !TEST_true(BN_dec2bn(&exponent, test->exp))
3008 || !TEST_true(BN_dec2bn(&modulo, test->mod)))
3011 BN_set_flags(base, BN_FLG_CONSTTIME);
3012 BN_set_flags(exponent, BN_FLG_CONSTTIME);
3013 BN_set_flags(modulo, BN_FLG_CONSTTIME);
3015 if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
3018 if (!TEST_ptr(s = BN_bn2dec(result)))
3021 if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
3036 * Regression test to ensure BN_mod_exp2_mont fails safely if argument m is
3039 static int test_mod_exp2_mont(void)
3042 BIGNUM *exp_result = NULL;
3043 BIGNUM *exp_a1 = NULL, *exp_p1 = NULL, *exp_a2 = NULL, *exp_p2 = NULL,
3046 if (!TEST_ptr(exp_result = BN_new())
3047 || !TEST_ptr(exp_a1 = BN_new())
3048 || !TEST_ptr(exp_p1 = BN_new())
3049 || !TEST_ptr(exp_a2 = BN_new())
3050 || !TEST_ptr(exp_p2 = BN_new())
3051 || !TEST_ptr(exp_m = BN_new()))
3054 if (!TEST_true(BN_one(exp_a1))
3055 || !TEST_true(BN_one(exp_p1))
3056 || !TEST_true(BN_one(exp_a2))
3057 || !TEST_true(BN_one(exp_p2)))
3062 /* input of 0 is even, so must fail */
3063 if (!TEST_int_eq(BN_mod_exp2_mont(exp_result, exp_a1, exp_p1, exp_a2,
3064 exp_p2, exp_m, ctx, NULL), 0))
3070 BN_free(exp_result);
3079 static int file_test_run(STANZA *s)
3081 static const FILETEST filetests[] = {
3083 {"LShift1", file_lshift1},
3084 {"LShift", file_lshift},
3085 {"RShift", file_rshift},
3086 {"Square", file_square},
3087 {"Product", file_product},
3088 {"Quotient", file_quotient},
3089 {"ModMul", file_modmul},
3090 {"ModExp", file_modexp},
3092 {"ModSqrt", file_modsqrt},
3095 int numtests = OSSL_NELEM(filetests);
3096 const FILETEST *tp = filetests;
3098 for ( ; --numtests >= 0; tp++) {
3099 if (findattr(s, tp->name) != NULL) {
3101 TEST_info("%s:%d: Failed %s test",
3102 s->test_file, s->start, tp->name);
3108 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
3112 static int run_file_tests(int i)
3115 char *testfile = test_get_argument(i);
3118 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
3120 if (!test_start_file(s, testfile)) {
3125 /* Read test file. */
3126 while (!BIO_eof(s->fp) && test_readstanza(s)) {
3127 if (s->numpairs == 0)
3129 if (!file_test_run(s))
3132 test_clearstanza(s);
3141 typedef enum OPTION_choice {
3144 OPT_STOCHASTIC_TESTS,
3148 const OPTIONS *test_get_options(void)
3150 static const OPTIONS test_options[] = {
3151 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
3152 { "stochastic", OPT_STOCHASTIC_TESTS, '-', "Run stochastic tests" },
3153 { OPT_HELP_STR, 1, '-',
3154 "file\tFile to run tests on. Normal tests are not run\n" },
3157 return test_options;
3160 int setup_tests(void)
3163 int n, stochastic = 0;
3165 while ((o = opt_next()) != OPT_EOF) {
3167 case OPT_STOCHASTIC_TESTS:
3170 case OPT_TEST_CASES:
3177 n = test_get_argument_count();
3179 if (!TEST_ptr(ctx = BN_CTX_new()))
3184 ADD_TEST(test_div_recip);
3185 ADD_ALL_TESTS(test_signed_mod_replace_ab, OSSL_NELEM(signed_mod_tests));
3186 ADD_ALL_TESTS(test_signed_mod_replace_ba, OSSL_NELEM(signed_mod_tests));
3188 ADD_TEST(test_modexp_mont5);
3189 ADD_TEST(test_kronecker);
3190 ADD_TEST(test_rand);
3191 ADD_TEST(test_bn2padded);
3192 ADD_TEST(test_dec2bn);
3193 ADD_TEST(test_hex2bn);
3194 ADD_TEST(test_asc2bn);
3195 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
3196 ADD_ALL_TESTS(test_bn2signed, (int)OSSL_NELEM(kSignedTests_BE));
3197 ADD_TEST(test_negzero);
3198 ADD_TEST(test_badmod);
3199 ADD_TEST(test_expmodzero);
3200 ADD_TEST(test_expmodone);
3201 ADD_ALL_TESTS(test_smallprime, 16);
3202 ADD_ALL_TESTS(test_smallsafeprime, 16);
3203 ADD_TEST(test_swap);
3204 ADD_TEST(test_ctx_consttime_flag);
3205 #ifndef OPENSSL_NO_EC2M
3206 ADD_TEST(test_gf2m_add);
3207 ADD_TEST(test_gf2m_mod);
3208 ADD_TEST(test_gf2m_mul);
3209 ADD_TEST(test_gf2m_sqr);
3210 ADD_TEST(test_gf2m_modinv);
3211 ADD_TEST(test_gf2m_moddiv);
3212 ADD_TEST(test_gf2m_modexp);
3213 ADD_TEST(test_gf2m_modsqrt);
3214 ADD_TEST(test_gf2m_modsolvequad);
3216 ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
3217 ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
3218 ADD_TEST(test_gcd_prime);
3219 ADD_ALL_TESTS(test_mod_exp, (int)OSSL_NELEM(ModExpTests));
3220 ADD_ALL_TESTS(test_mod_exp_consttime, (int)OSSL_NELEM(ModExpTests));
3221 ADD_TEST(test_mod_exp2_mont);
3223 ADD_TEST(test_rand_range);
3225 ADD_ALL_TESTS(run_file_tests, n);
3230 void cleanup_tests(void)