2 * Copyright 1995-2017 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 "internal/nelem.h"
17 #include "internal/numbers.h"
18 #include <openssl/bn.h>
19 #include <openssl/crypto.h>
20 #include <openssl/err.h>
21 #include <openssl/rand.h>
25 * In bn_lcl.h, bn_expand() is defined as a static ossl_inline function.
26 * This is fine in itself, it will end up as an unused static function in
27 * the worst case. However, it references bn_expand2(), which is a private
28 * function in libcrypto and therefore unavailable on some systems. This
29 * may result in a linker error because of unresolved symbols.
31 * To avoid this, we define a dummy variant of bn_expand2() here, and to
32 * avoid possible clashes with libcrypto, we rename it first, using a macro.
34 #define bn_expand2 dummy_bn_expand2
35 BIGNUM *bn_expand2(BIGNUM *b, int words);
36 BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; }
37 #include "../crypto/bn/bn_lcl.h"
40 * Things in boring, not in openssl. TODO we should add them.
42 #define HAVE_BN_PADDED 0
43 #define HAVE_BN_SQRT 0
45 typedef struct filetest_st {
47 int (*func)(STANZA *s);
50 typedef struct mpitest_st {
56 static const int NUM0 = 100; /* number of tests */
57 static const int NUM1 = 50; /* additional tests for some functions */
61 * Polynomial coefficients used in GFM tests.
63 #ifndef OPENSSL_NO_EC2M
64 static int p0[] = { 163, 7, 6, 3, 0, -1 };
65 static int p1[] = { 193, 15, 0, -1 };
69 * Look for |key| in the stanza and return it or NULL if not found.
71 static const char *findattr(STANZA *s, const char *key)
76 for ( ; --i >= 0; pp++)
77 if (strcasecmp(pp->key, key) == 0)
83 * Parse BIGNUM, return number of bytes parsed.
85 static int parseBN(BIGNUM **out, const char *in)
88 return BN_hex2bn(out, in);
91 static int parsedecBN(BIGNUM **out, const char *in)
94 return BN_dec2bn(out, in);
97 static BIGNUM *getBN(STANZA *s, const char *attribute)
102 if ((hex = findattr(s, attribute)) == NULL) {
103 TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
107 if (parseBN(&ret, hex) != (int)strlen(hex)) {
108 TEST_error("Could not decode '%s'", hex);
114 static int getint(STANZA *s, int *out, const char *attribute)
120 if (!TEST_ptr(ret = getBN(s, attribute))
121 || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
131 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
133 if (BN_cmp(expected, actual) == 0)
136 TEST_error("unexpected %s value", op);
137 TEST_BN_eq(expected, actual);
143 * Return a "random" flag for if a BN should be negated.
145 static int rand_neg(void)
147 static unsigned int neg = 0;
148 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
150 return sign[(neg++) % 8];
154 static int test_sub(void)
156 BIGNUM *a = NULL, *b = NULL, *c = NULL;
159 if (!TEST_ptr(a = BN_new())
160 || !TEST_ptr(b = BN_new())
161 || !TEST_ptr(c = BN_new()))
164 for (i = 0; i < NUM0 + NUM1; i++) {
166 BN_bntest_rand(a, 512, 0, 0);
168 if (!TEST_int_ne(BN_set_bit(a, i), 0))
172 BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
179 if (!TEST_BN_eq_zero(c))
191 static int test_div_recip(void)
193 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
194 BN_RECP_CTX *recp = NULL;
197 if (!TEST_ptr(a = BN_new())
198 || !TEST_ptr(b = BN_new())
199 || !TEST_ptr(c = BN_new())
200 || !TEST_ptr(d = BN_new())
201 || !TEST_ptr(e = BN_new())
202 || !TEST_ptr(recp = BN_RECP_CTX_new()))
205 for (i = 0; i < NUM0 + NUM1; i++) {
207 BN_bntest_rand(a, 400, 0, 0);
212 BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
215 BN_RECP_CTX_set(recp, b, ctx);
216 BN_div_recp(d, c, a, recp, ctx);
217 BN_mul(e, d, b, ctx);
220 if (!TEST_BN_eq_zero(d))
230 BN_RECP_CTX_free(recp);
235 static int test_mod(void)
237 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
240 if (!TEST_ptr(a = BN_new())
241 || !TEST_ptr(b = BN_new())
242 || !TEST_ptr(c = BN_new())
243 || !TEST_ptr(d = BN_new())
244 || !TEST_ptr(e = BN_new()))
247 BN_bntest_rand(a, 1024, 0, 0);
248 for (i = 0; i < NUM0; i++) {
249 BN_bntest_rand(b, 450 + i * 10, 0, 0);
252 BN_mod(c, a, b, ctx);
253 BN_div(d, e, a, b, ctx);
255 if (!TEST_BN_eq_zero(e))
268 static const char *bn1strings[] = {
269 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
270 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
271 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
272 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
273 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
274 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
275 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
276 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
277 "0000000000000000000000000000000000000000000000000000000000000000",
278 "0000000000000000000000000000000000000000000000000000000000000000",
279 "0000000000000000000000000000000000000000000000000000000000000000",
280 "0000000000000000000000000000000000000000000000000000000000000000",
281 "0000000000000000000000000000000000000000000000000000000000000000",
282 "0000000000000000000000000000000000000000000000000000000000000000",
283 "0000000000000000000000000000000000000000000000000000000000000000",
284 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
288 static const char *bn2strings[] = {
289 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
290 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
291 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
292 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
293 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
294 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
295 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
296 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
297 "0000000000000000000000000000000000000000000000000000000000000000",
298 "0000000000000000000000000000000000000000000000000000000000000000",
299 "0000000000000000000000000000000000000000000000000000000000000000",
300 "0000000000000000000000000000000000000000000000000000000000000000",
301 "0000000000000000000000000000000000000000000000000000000000000000",
302 "0000000000000000000000000000000000000000000000000000000000000000",
303 "0000000000000000000000000000000000000000000000000000000000000000",
304 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
308 static char *glue(const char *list[])
314 for (i = 0; list[i] != NULL; i++)
315 len += strlen(list[i]);
316 if (!TEST_ptr(p = save = OPENSSL_malloc(len + 1)))
318 for (i = 0; list[i] != NULL; i++)
319 p += strlen(strcpy(p, list[i]));
324 * Test constant-time modular exponentiation with 1024-bit inputs, which on
325 * x86_64 cause a different code branch to be taken.
327 static int test_modexp_mont5(void)
329 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
330 BIGNUM *b = NULL, *n = NULL, *c = NULL;
331 BN_MONT_CTX *mont = NULL;
335 if (!TEST_ptr(a = BN_new())
336 || !TEST_ptr(p = BN_new())
337 || !TEST_ptr(m = BN_new())
338 || !TEST_ptr(d = BN_new())
339 || !TEST_ptr(e = BN_new())
340 || !TEST_ptr(b = BN_new())
341 || !TEST_ptr(n = BN_new())
342 || !TEST_ptr(c = BN_new())
343 || !TEST_ptr(mont = BN_MONT_CTX_new()))
346 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
348 BN_bntest_rand(a, 1024, 0, 0);
350 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
352 if (!TEST_BN_eq_one(d))
355 /* Regression test for carry bug in mulx4x_mont */
357 "7878787878787878787878787878787878787878787878787878787878787878"
358 "7878787878787878787878787878787878787878787878787878787878787878"
359 "7878787878787878787878787878787878787878787878787878787878787878"
360 "7878787878787878787878787878787878787878787878787878787878787878");
362 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
363 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
364 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
365 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
367 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
368 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
369 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
370 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
371 BN_MONT_CTX_set(mont, n, ctx);
372 BN_mod_mul_montgomery(c, a, b, mont, ctx);
373 BN_mod_mul_montgomery(d, b, a, mont, ctx);
374 if (!TEST_BN_eq(c, d))
377 /* Regression test for carry bug in sqr[x]8x_mont */
378 bigstring = glue(bn1strings);
379 BN_hex2bn(&n, bigstring);
380 OPENSSL_free(bigstring);
381 bigstring = glue(bn2strings);
382 BN_hex2bn(&a, bigstring);
383 OPENSSL_free(bigstring);
386 BN_MONT_CTX_set(mont, n, ctx);
387 BN_mod_mul_montgomery(c, a, a, mont, ctx);
388 BN_mod_mul_montgomery(d, a, b, mont, ctx);
389 if (!TEST_BN_eq(c, d))
393 BN_bntest_rand(p, 1024, 0, 0);
395 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
396 || !TEST_BN_eq_zero(d))
400 * Craft an input whose Montgomery representation is 1, i.e., shorter
401 * than the modulus m, in order to test the const time precomputation
402 * scattering/gathering.
405 BN_MONT_CTX_set(mont, m, ctx);
406 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
407 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
408 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
409 || !TEST_BN_eq(a, d))
412 /* Finally, some regular test vectors. */
413 BN_bntest_rand(e, 1024, 0, 0);
414 if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
415 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
416 || !TEST_BN_eq(a, d))
422 BN_MONT_CTX_free(mont);
434 #ifndef OPENSSL_NO_EC2M
435 static int test_gf2m_add(void)
437 BIGNUM *a = NULL, *b = NULL, *c = NULL;
440 if (!TEST_ptr(a = BN_new())
441 || !TEST_ptr(b = BN_new())
442 || !TEST_ptr(c = BN_new()))
445 for (i = 0; i < NUM0; i++) {
446 BN_rand(a, 512, 0, 0);
447 BN_copy(b, BN_value_one());
450 BN_GF2m_add(c, a, b);
451 /* Test that two added values have the correct parity. */
452 if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
453 || (!BN_is_odd(a) && !BN_is_odd(c))))
455 BN_GF2m_add(c, c, c);
456 /* Test that c + c = 0. */
457 if (!TEST_BN_eq_zero(c))
468 static int test_gf2m_mod(void)
470 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
473 if (!TEST_ptr(a = BN_new())
474 || !TEST_ptr(b[0] = BN_new())
475 || !TEST_ptr(b[1] = BN_new())
476 || !TEST_ptr(c = BN_new())
477 || !TEST_ptr(d = BN_new())
478 || !TEST_ptr(e = BN_new()))
481 BN_GF2m_arr2poly(p0, b[0]);
482 BN_GF2m_arr2poly(p1, b[1]);
484 for (i = 0; i < NUM0; i++) {
485 BN_bntest_rand(a, 1024, 0, 0);
486 for (j = 0; j < 2; j++) {
487 BN_GF2m_mod(c, a, b[j]);
488 BN_GF2m_add(d, a, c);
489 BN_GF2m_mod(e, d, b[j]);
490 /* Test that a + (a mod p) mod p == 0. */
491 if (!TEST_BN_eq_zero(e))
506 static int test_gf2m_mul(void)
508 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
509 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
512 if (!TEST_ptr(a = BN_new())
513 || !TEST_ptr(b[0] = BN_new())
514 || !TEST_ptr(b[1] = BN_new())
515 || !TEST_ptr(c = BN_new())
516 || !TEST_ptr(d = BN_new())
517 || !TEST_ptr(e = BN_new())
518 || !TEST_ptr(f = BN_new())
519 || !TEST_ptr(g = BN_new())
520 || !TEST_ptr(h = BN_new()))
523 BN_GF2m_arr2poly(p0, b[0]);
524 BN_GF2m_arr2poly(p1, b[1]);
526 for (i = 0; i < NUM0; i++) {
527 BN_bntest_rand(a, 1024, 0, 0);
528 BN_bntest_rand(c, 1024, 0, 0);
529 BN_bntest_rand(d, 1024, 0, 0);
530 for (j = 0; j < 2; j++) {
531 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
532 BN_GF2m_add(f, a, d);
533 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
534 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
535 BN_GF2m_add(f, e, g);
536 BN_GF2m_add(f, f, h);
537 /* Test that (a+d)*c = a*c + d*c. */
538 if (!TEST_BN_eq_zero(f))
557 static int test_gf2m_sqr(void)
559 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
562 if (!TEST_ptr(a = BN_new())
563 || !TEST_ptr(b[0] = BN_new())
564 || !TEST_ptr(b[1] = BN_new())
565 || !TEST_ptr(c = BN_new())
566 || !TEST_ptr(d = BN_new()))
569 BN_GF2m_arr2poly(p0, b[0]);
570 BN_GF2m_arr2poly(p1, b[1]);
572 for (i = 0; i < NUM0; i++) {
573 BN_bntest_rand(a, 1024, 0, 0);
574 for (j = 0; j < 2; j++) {
575 BN_GF2m_mod_sqr(c, a, b[j], ctx);
577 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
578 BN_GF2m_add(d, c, d);
579 /* Test that a*a = a^2. */
580 if (!TEST_BN_eq_zero(d))
594 static int test_gf2m_modinv(void)
596 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
599 if (!TEST_ptr(a = BN_new())
600 || !TEST_ptr(b[0] = BN_new())
601 || !TEST_ptr(b[1] = BN_new())
602 || !TEST_ptr(c = BN_new())
603 || !TEST_ptr(d = BN_new()))
606 BN_GF2m_arr2poly(p0, b[0]);
607 BN_GF2m_arr2poly(p1, b[1]);
609 for (i = 0; i < NUM0; i++) {
610 BN_bntest_rand(a, 512, 0, 0);
611 for (j = 0; j < 2; j++) {
612 BN_GF2m_mod_inv(c, a, b[j], ctx);
613 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
614 /* Test that ((1/a)*a) = 1. */
615 if (!TEST_BN_eq_one(d))
629 static int test_gf2m_moddiv(void)
631 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
632 BIGNUM *e = NULL, *f = NULL;
635 if (!TEST_ptr(a = BN_new())
636 || !TEST_ptr(b[0] = BN_new())
637 || !TEST_ptr(b[1] = BN_new())
638 || !TEST_ptr(c = BN_new())
639 || !TEST_ptr(d = BN_new())
640 || !TEST_ptr(e = BN_new())
641 || !TEST_ptr(f = BN_new()))
644 BN_GF2m_arr2poly(p0, b[0]);
645 BN_GF2m_arr2poly(p1, b[1]);
647 for (i = 0; i < NUM0; i++) {
648 BN_bntest_rand(a, 512, 0, 0);
649 BN_bntest_rand(c, 512, 0, 0);
650 for (j = 0; j < 2; j++) {
651 BN_GF2m_mod_div(d, a, c, b[j], ctx);
652 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
653 BN_GF2m_mod_div(f, a, e, b[j], ctx);
654 /* Test that ((a/c)*c)/a = 1. */
655 if (!TEST_BN_eq_one(f))
671 static int test_gf2m_modexp(void)
673 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
674 BIGNUM *e = NULL, *f = NULL;
677 if (!TEST_ptr(a = BN_new())
678 || !TEST_ptr(b[0] = BN_new())
679 || !TEST_ptr(b[1] = BN_new())
680 || !TEST_ptr(c = BN_new())
681 || !TEST_ptr(d = BN_new())
682 || !TEST_ptr(e = BN_new())
683 || !TEST_ptr(f = BN_new()))
686 BN_GF2m_arr2poly(p0, b[0]);
687 BN_GF2m_arr2poly(p1, b[1]);
689 for (i = 0; i < NUM0; i++) {
690 BN_bntest_rand(a, 512, 0, 0);
691 BN_bntest_rand(c, 512, 0, 0);
692 BN_bntest_rand(d, 512, 0, 0);
693 for (j = 0; j < 2; j++) {
694 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
695 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
696 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
698 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
699 BN_GF2m_add(f, e, f);
700 /* Test that a^(c+d)=a^c*a^d. */
701 if (!TEST_BN_eq_zero(f))
717 static int test_gf2m_modsqrt(void)
719 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
720 BIGNUM *e = NULL, *f = NULL;
723 if (!TEST_ptr(a = BN_new())
724 || !TEST_ptr(b[0] = BN_new())
725 || !TEST_ptr(b[1] = BN_new())
726 || !TEST_ptr(c = BN_new())
727 || !TEST_ptr(d = BN_new())
728 || !TEST_ptr(e = BN_new())
729 || !TEST_ptr(f = BN_new()))
732 BN_GF2m_arr2poly(p0, b[0]);
733 BN_GF2m_arr2poly(p1, b[1]);
735 for (i = 0; i < NUM0; i++) {
736 BN_bntest_rand(a, 512, 0, 0);
737 for (j = 0; j < 2; j++) {
738 BN_GF2m_mod(c, a, b[j]);
739 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
740 BN_GF2m_mod_sqr(e, d, b[j], ctx);
741 BN_GF2m_add(f, c, e);
742 /* Test that d^2 = a, where d = sqrt(a). */
743 if (!TEST_BN_eq_zero(f))
759 static int test_gf2m_modsolvequad(void)
761 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
763 int i, j, s = 0, t, st = 0;
765 if (!TEST_ptr(a = BN_new())
766 || !TEST_ptr(b[0] = BN_new())
767 || !TEST_ptr(b[1] = BN_new())
768 || !TEST_ptr(c = BN_new())
769 || !TEST_ptr(d = BN_new())
770 || !TEST_ptr(e = BN_new()))
773 BN_GF2m_arr2poly(p0, b[0]);
774 BN_GF2m_arr2poly(p1, b[1]);
776 for (i = 0; i < NUM0; i++) {
777 BN_bntest_rand(a, 512, 0, 0);
778 for (j = 0; j < 2; j++) {
779 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
782 BN_GF2m_mod_sqr(d, c, b[j], ctx);
783 BN_GF2m_add(d, c, d);
784 BN_GF2m_mod(e, a, b[j]);
785 BN_GF2m_add(e, e, d);
787 * Test that solution of quadratic c satisfies c^2 + c = a.
789 if (!TEST_BN_eq_zero(e))
794 if (!TEST_int_ge(s, 0)) {
795 TEST_info("%d tests found no roots; probably an error", NUM0);
810 static int test_kronecker(void)
812 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
813 int i, legendre, kronecker, st = 0;
815 if (!TEST_ptr(a = BN_new())
816 || !TEST_ptr(b = BN_new())
817 || !TEST_ptr(r = BN_new())
818 || !TEST_ptr(t = BN_new()))
822 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
823 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
824 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
825 * generate a random prime b and compare these values for a number of
826 * random a's. (That is, we run the Solovay-Strassen primality test to
827 * confirm that b is prime, except that we don't want to test whether b
828 * is prime but whether BN_kronecker works.)
831 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
835 for (i = 0; i < NUM0; i++) {
836 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
840 /* t := (|b|-1)/2 (note that b is odd) */
841 if (!TEST_true(BN_copy(t, b)))
844 if (!TEST_true(BN_sub_word(t, 1)))
846 if (!TEST_true(BN_rshift1(t, t)))
851 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
855 if (BN_is_word(r, 1))
857 else if (BN_is_zero(r))
860 if (!TEST_true(BN_add_word(r, 1)))
862 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
863 TEST_info("Legendre symbol computation failed");
869 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
871 /* we actually need BN_kronecker(a, |b|) */
872 if (a->neg && b->neg)
873 kronecker = -kronecker;
875 if (!TEST_int_eq(legendre, kronecker))
888 static int file_sum(STANZA *s)
890 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
894 if (!TEST_ptr(a = getBN(s, "A"))
895 || !TEST_ptr(b = getBN(s, "B"))
896 || !TEST_ptr(sum = getBN(s, "Sum"))
897 || !TEST_ptr(ret = BN_new()))
900 if (!TEST_true(BN_add(ret, a, b))
901 || !equalBN("A + B", sum, ret)
902 || !TEST_true(BN_sub(ret, sum, a))
903 || !equalBN("Sum - A", b, ret)
904 || !TEST_true(BN_sub(ret, sum, b))
905 || !equalBN("Sum - B", a, ret))
909 * Test that the functions work when |r| and |a| point to the same BIGNUM,
910 * or when |r| and |b| point to the same BIGNUM.
911 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
913 if (!TEST_true(BN_copy(ret, a))
914 || !TEST_true(BN_add(ret, ret, b))
915 || !equalBN("A + B (r is a)", sum, ret)
916 || !TEST_true(BN_copy(ret, b))
917 || !TEST_true(BN_add(ret, a, ret))
918 || !equalBN("A + B (r is b)", sum, ret)
919 || !TEST_true(BN_copy(ret, sum))
920 || !TEST_true(BN_sub(ret, ret, a))
921 || !equalBN("Sum - A (r is a)", b, ret)
922 || !TEST_true(BN_copy(ret, a))
923 || !TEST_true(BN_sub(ret, sum, ret))
924 || !equalBN("Sum - A (r is b)", b, ret)
925 || !TEST_true(BN_copy(ret, sum))
926 || !TEST_true(BN_sub(ret, ret, b))
927 || !equalBN("Sum - B (r is a)", a, ret)
928 || !TEST_true(BN_copy(ret, b))
929 || !TEST_true(BN_sub(ret, sum, ret))
930 || !equalBN("Sum - B (r is b)", a, ret))
934 * Test BN_uadd() and BN_usub() with the prerequisites they are
935 * documented as having. Note that these functions are frequently used
936 * when the prerequisites don't hold. In those cases, they are supposed
937 * to work as if the prerequisite hold, but we don't test that yet.
940 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
941 if (!TEST_true(BN_uadd(ret, a, b))
942 || !equalBN("A +u B", sum, ret)
943 || !TEST_true(BN_usub(ret, sum, a))
944 || !equalBN("Sum -u A", b, ret)
945 || !TEST_true(BN_usub(ret, sum, b))
946 || !equalBN("Sum -u B", a, ret))
949 * Test that the functions work when |r| and |a| point to the same
950 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
951 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
953 if (!TEST_true(BN_copy(ret, a))
954 || !TEST_true(BN_uadd(ret, ret, b))
955 || !equalBN("A +u B (r is a)", sum, ret)
956 || !TEST_true(BN_copy(ret, b))
957 || !TEST_true(BN_uadd(ret, a, ret))
958 || !equalBN("A +u B (r is b)", sum, ret)
959 || !TEST_true(BN_copy(ret, sum))
960 || !TEST_true(BN_usub(ret, ret, a))
961 || !equalBN("Sum -u A (r is a)", b, ret)
962 || !TEST_true(BN_copy(ret, a))
963 || !TEST_true(BN_usub(ret, sum, ret))
964 || !equalBN("Sum -u A (r is b)", b, ret)
965 || !TEST_true(BN_copy(ret, sum))
966 || !TEST_true(BN_usub(ret, ret, b))
967 || !equalBN("Sum -u B (r is a)", a, ret)
968 || !TEST_true(BN_copy(ret, b))
969 || !TEST_true(BN_usub(ret, sum, ret))
970 || !equalBN("Sum -u B (r is b)", a, ret))
975 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
977 b_word = BN_get_word(b);
978 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
979 if (!TEST_true(BN_copy(ret, a))
980 || !TEST_true(BN_add_word(ret, b_word))
981 || !equalBN("A + B (word)", sum, ret)
982 || !TEST_true(BN_copy(ret, sum))
983 || !TEST_true(BN_sub_word(ret, b_word))
984 || !equalBN("Sum - B (word)", a, ret))
997 static int file_lshift1(STANZA *s)
999 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1000 BIGNUM *two = NULL, *remainder = NULL;
1003 if (!TEST_ptr(a = getBN(s, "A"))
1004 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1005 || !TEST_ptr(zero = BN_new())
1006 || !TEST_ptr(ret = BN_new())
1007 || !TEST_ptr(two = BN_new())
1008 || !TEST_ptr(remainder = BN_new()))
1013 if (!TEST_true(BN_set_word(two, 2))
1014 || !TEST_true(BN_add(ret, a, a))
1015 || !equalBN("A + A", lshift1, ret)
1016 || !TEST_true(BN_mul(ret, a, two, ctx))
1017 || !equalBN("A * 2", lshift1, ret)
1018 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1019 || !equalBN("LShift1 / 2", a, ret)
1020 || !equalBN("LShift1 % 2", zero, remainder)
1021 || !TEST_true(BN_lshift1(ret, a))
1022 || !equalBN("A << 1", lshift1, ret)
1023 || !TEST_true(BN_rshift1(ret, lshift1))
1024 || !equalBN("LShift >> 1", a, ret)
1025 || !TEST_true(BN_rshift1(ret, lshift1))
1026 || !equalBN("LShift >> 1", a, ret))
1029 /* Set the LSB to 1 and test rshift1 again. */
1030 if (!TEST_true(BN_set_bit(lshift1, 0))
1031 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1032 || !equalBN("(LShift1 | 1) / 2", a, ret)
1033 || !TEST_true(BN_rshift1(ret, lshift1))
1034 || !equalBN("(LShift | 1) >> 1", a, ret))
1049 static int file_lshift(STANZA *s)
1051 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1054 if (!TEST_ptr(a = getBN(s, "A"))
1055 || !TEST_ptr(lshift = getBN(s, "LShift"))
1056 || !TEST_ptr(ret = BN_new()))
1058 if (!TEST_true(BN_lshift(ret, a, n))
1059 || !equalBN("A << N", lshift, ret)
1060 || !TEST_true(BN_rshift(ret, lshift, n))
1061 || !equalBN("A >> N", a, ret))
1072 static int file_rshift(STANZA *s)
1074 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1077 if (!TEST_ptr(a = getBN(s, "A"))
1078 || !TEST_ptr(rshift = getBN(s, "RShift"))
1079 || !TEST_ptr(ret = BN_new())
1080 || !getint(s, &n, "N"))
1083 if (!TEST_true(BN_rshift(ret, a, n))
1084 || !equalBN("A >> N", rshift, ret))
1087 /* If N == 1, try with rshift1 as well */
1089 if (!TEST_true(BN_rshift1(ret, a))
1090 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1102 static int file_square(STANZA *s)
1104 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1105 BIGNUM *remainder = NULL, *tmp = NULL;
1108 if (!TEST_ptr(a = getBN(s, "A"))
1109 || !TEST_ptr(square = getBN(s, "Square"))
1110 || !TEST_ptr(zero = BN_new())
1111 || !TEST_ptr(ret = BN_new())
1112 || !TEST_ptr(remainder = BN_new()))
1116 if (!TEST_true(BN_sqr(ret, a, ctx))
1117 || !equalBN("A^2", square, ret)
1118 || !TEST_true(BN_mul(ret, a, a, ctx))
1119 || !equalBN("A * A", square, ret)
1120 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1121 || !equalBN("Square / A", a, ret)
1122 || !equalBN("Square % A", zero, remainder))
1126 BN_set_negative(a, 0);
1127 if (!TEST_true(BN_sqrt(ret, square, ctx))
1128 || !equalBN("sqrt(Square)", a, ret))
1131 /* BN_sqrt should fail on non-squares and negative numbers. */
1132 if (!TEST_BN_eq_zero(square)) {
1133 if (!TEST_ptr(tmp = BN_new())
1134 || !TEST_true(BN_copy(tmp, square)))
1136 BN_set_negative(tmp, 1);
1138 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1142 BN_set_negative(tmp, 0);
1143 if (BN_add(tmp, tmp, BN_value_one()))
1145 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1162 static int file_product(STANZA *s)
1164 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1165 BIGNUM *remainder = NULL, *zero = NULL;
1168 if (!TEST_ptr(a = getBN(s, "A"))
1169 || !TEST_ptr(b = getBN(s, "B"))
1170 || !TEST_ptr(product = getBN(s, "Product"))
1171 || !TEST_ptr(ret = BN_new())
1172 || !TEST_ptr(remainder = BN_new())
1173 || !TEST_ptr(zero = BN_new()))
1178 if (!TEST_true(BN_mul(ret, a, b, ctx))
1179 || !equalBN("A * B", product, ret)
1180 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1181 || !equalBN("Product / A", b, ret)
1182 || !equalBN("Product % A", zero, remainder)
1183 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1184 || !equalBN("Product / B", a, ret)
1185 || !equalBN("Product % B", zero, remainder))
1199 static int file_quotient(STANZA *s)
1201 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1202 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1203 BN_ULONG b_word, ret_word;
1206 if (!TEST_ptr(a = getBN(s, "A"))
1207 || !TEST_ptr(b = getBN(s, "B"))
1208 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1209 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1210 || !TEST_ptr(ret = BN_new())
1211 || !TEST_ptr(ret2 = BN_new())
1212 || !TEST_ptr(nnmod = BN_new()))
1215 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1216 || !equalBN("A / B", quotient, ret)
1217 || !equalBN("A % B", remainder, ret2)
1218 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1219 || !TEST_true(BN_add(ret, ret, remainder))
1220 || !equalBN("Quotient * B + Remainder", a, ret))
1224 * Test with BN_mod_word() and BN_div_word() if the divisor is
1227 b_word = BN_get_word(b);
1228 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1229 BN_ULONG remainder_word = BN_get_word(remainder);
1231 assert(remainder_word != (BN_ULONG)-1);
1232 if (!TEST_ptr(BN_copy(ret, a)))
1234 ret_word = BN_div_word(ret, b_word);
1235 if (ret_word != remainder_word) {
1238 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1239 ret_word, remainder_word);
1241 TEST_error("Got A %% B (word) mismatch");
1245 if (!equalBN ("A / B (word)", quotient, ret))
1248 ret_word = BN_mod_word(a, b_word);
1249 if (ret_word != remainder_word) {
1252 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1253 ret_word, remainder_word);
1255 TEST_error("Got A %% B (word) mismatch");
1261 /* Test BN_nnmod. */
1262 if (!BN_is_negative(b)) {
1263 if (!TEST_true(BN_copy(nnmod, remainder))
1264 || (BN_is_negative(nnmod)
1265 && !TEST_true(BN_add(nnmod, nnmod, b)))
1266 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1267 || !equalBN("A % B (non-negative)", nnmod, ret))
1283 static int file_modmul(STANZA *s)
1285 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1288 if (!TEST_ptr(a = getBN(s, "A"))
1289 || !TEST_ptr(b = getBN(s, "B"))
1290 || !TEST_ptr(m = getBN(s, "M"))
1291 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1292 || !TEST_ptr(ret = BN_new()))
1295 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1296 || !equalBN("A * B (mod M)", mod_mul, ret))
1300 /* Reduce |a| and |b| and test the Montgomery version. */
1301 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1302 BIGNUM *a_tmp = BN_new();
1303 BIGNUM *b_tmp = BN_new();
1305 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1306 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1307 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1308 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1309 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1310 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1311 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1313 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1314 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1318 BN_MONT_CTX_free(mont);
1335 static int file_modexp(STANZA *s)
1337 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1338 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1341 if (!TEST_ptr(a = getBN(s, "A"))
1342 || !TEST_ptr(e = getBN(s, "E"))
1343 || !TEST_ptr(m = getBN(s, "M"))
1344 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1345 || !TEST_ptr(ret = BN_new())
1346 || !TEST_ptr(d = BN_new()))
1349 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1350 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1354 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1355 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1356 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1358 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1362 /* Regression test for carry propagation bug in sqr8x_reduction */
1363 BN_hex2bn(&a, "050505050505");
1364 BN_hex2bn(&b, "02");
1366 "4141414141414141414141274141414141414141414141414141414141414141"
1367 "4141414141414141414141414141414141414141414141414141414141414141"
1368 "4141414141414141414141800000000000000000000000000000000000000000"
1369 "0000000000000000000000000000000000000000000000000000000000000000"
1370 "0000000000000000000000000000000000000000000000000000000000000000"
1371 "0000000000000000000000000000000000000000000000000000000001");
1372 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1373 || !TEST_true(BN_mul(e, a, a, ctx))
1374 || !TEST_BN_eq(d, e))
1390 static int file_exp(STANZA *s)
1392 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1395 if (!TEST_ptr(a = getBN(s, "A"))
1396 || !TEST_ptr(e = getBN(s, "E"))
1397 || !TEST_ptr(exp = getBN(s, "Exp"))
1398 || !TEST_ptr(ret = BN_new()))
1401 if (!TEST_true(BN_exp(ret, a, e, ctx))
1402 || !equalBN("A ^ E", exp, ret))
1414 static int file_modsqrt(STANZA *s)
1416 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1419 if (!TEST_ptr(a = getBN(s, "A"))
1420 || !TEST_ptr(p = getBN(s, "P"))
1421 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1422 || !TEST_ptr(ret = BN_new())
1423 || !TEST_ptr(ret2 = BN_new()))
1426 /* There are two possible answers. */
1427 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1428 || !TEST_true(BN_sub(ret2, p, ret)))
1431 /* The first condition should NOT be a test. */
1432 if (BN_cmp(ret2, mod_sqrt) != 0
1433 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1446 static int test_bn2padded(void)
1449 uint8_t zeros[256], out[256], reference[128];
1450 BIGNUM *n = BN_new();
1453 /* Test edge case at 0. */
1456 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1458 memset(out, -1, sizeof(out));
1459 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1461 memset(zeros, 0, sizeof(zeros));
1462 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1465 /* Test a random numbers at various byte lengths. */
1466 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1467 #define TOP_BIT_ON 0
1468 #define BOTTOM_BIT_NOTOUCH 0
1469 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1471 if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1472 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1474 /* Empty buffer should fail. */
1475 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1477 /* One byte short should fail. */
1478 if (BN_bn2bin_padded(out, bytes - 1, n))
1480 /* Exactly right size should encode. */
1481 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1482 || TEST_mem_eq(out, bytes, reference, bytes))
1484 /* Pad up one byte extra. */
1485 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1486 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1487 || !TEST_mem_eq(out, 1, zeros, 1))
1489 /* Pad up to 256. */
1490 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1491 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1493 || !TEST_mem_eq(out, sizseof(out) - bytes,
1494 zeros, sizeof(out) - bytes))
1507 static int test_dec2bn(void)
1512 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1513 || !TEST_BN_eq_word(bn, 0)
1514 || !TEST_BN_eq_zero(bn)
1515 || !TEST_BN_le_zero(bn)
1516 || !TEST_BN_ge_zero(bn)
1517 || !TEST_BN_even(bn))
1522 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1523 || !TEST_BN_eq_word(bn, 256)
1524 || !TEST_BN_ge_zero(bn)
1525 || !TEST_BN_gt_zero(bn)
1526 || !TEST_BN_ne_zero(bn)
1527 || !TEST_BN_even(bn))
1532 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1533 || !TEST_BN_abs_eq_word(bn, 42)
1534 || !TEST_BN_lt_zero(bn)
1535 || !TEST_BN_le_zero(bn)
1536 || !TEST_BN_ne_zero(bn)
1537 || !TEST_BN_even(bn))
1542 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1543 || !TEST_BN_eq_word(bn, 1)
1544 || !TEST_BN_ne_zero(bn)
1545 || !TEST_BN_gt_zero(bn)
1546 || !TEST_BN_ge_zero(bn)
1547 || !TEST_BN_eq_one(bn)
1548 || !TEST_BN_odd(bn))
1553 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1554 || !TEST_BN_eq_zero(bn)
1555 || !TEST_BN_ge_zero(bn)
1556 || !TEST_BN_le_zero(bn)
1557 || !TEST_BN_even(bn))
1562 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1563 || !TEST_BN_abs_eq_word(bn, 42)
1564 || !TEST_BN_ge_zero(bn)
1565 || !TEST_BN_gt_zero(bn)
1566 || !TEST_BN_ne_zero(bn)
1567 || !TEST_BN_even(bn))
1576 static int test_hex2bn(void)
1581 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1582 || !TEST_BN_eq_zero(bn)
1583 || !TEST_BN_ge_zero(bn)
1584 || !TEST_BN_even(bn))
1589 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1590 || !TEST_BN_eq_word(bn, 0x256)
1591 || !TEST_BN_ge_zero(bn)
1592 || !TEST_BN_gt_zero(bn)
1593 || !TEST_BN_ne_zero(bn)
1594 || !TEST_BN_even(bn))
1599 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1600 || !TEST_BN_abs_eq_word(bn, 0x42)
1601 || !TEST_BN_lt_zero(bn)
1602 || !TEST_BN_le_zero(bn)
1603 || !TEST_BN_ne_zero(bn)
1604 || !TEST_BN_even(bn))
1609 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1610 || !TEST_BN_eq_word(bn, 0xCB)
1611 || !TEST_BN_ge_zero(bn)
1612 || !TEST_BN_gt_zero(bn)
1613 || !TEST_BN_ne_zero(bn)
1614 || !TEST_BN_odd(bn))
1619 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1620 || !TEST_BN_eq_zero(bn)
1621 || !TEST_BN_ge_zero(bn)
1622 || !TEST_BN_le_zero(bn)
1623 || !TEST_BN_even(bn))
1628 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1629 || !TEST_BN_eq_word(bn, 0xabc)
1630 || !TEST_BN_ge_zero(bn)
1631 || !TEST_BN_gt_zero(bn)
1632 || !TEST_BN_ne_zero(bn)
1633 || !TEST_BN_even(bn))
1642 static int test_asc2bn(void)
1647 if (!TEST_ptr(bn = BN_new()))
1650 if (!TEST_true(BN_asc2bn(&bn, "0"))
1651 || !TEST_BN_eq_zero(bn)
1652 || !TEST_BN_ge_zero(bn))
1655 if (!TEST_true(BN_asc2bn(&bn, "256"))
1656 || !TEST_BN_eq_word(bn, 256)
1657 || !TEST_BN_ge_zero(bn))
1660 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1661 || !TEST_BN_abs_eq_word(bn, 42)
1662 || !TEST_BN_lt_zero(bn))
1665 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1666 || !TEST_BN_eq_word(bn, 0x1234)
1667 || !TEST_BN_ge_zero(bn))
1670 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1671 || !TEST_BN_eq_word(bn, 0x1234)
1672 || !TEST_BN_ge_zero(bn))
1675 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1676 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1677 || !TEST_BN_lt_zero(bn))
1680 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1681 || !TEST_BN_eq_zero(bn)
1682 || !TEST_BN_ge_zero(bn))
1685 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1686 || !TEST_BN_eq_word(bn, 123)
1687 || !TEST_BN_ge_zero(bn))
1696 static const MPITEST kMPITests[] = {
1697 {"0", "\x00\x00\x00\x00", 4},
1698 {"1", "\x00\x00\x00\x01\x01", 5},
1699 {"-1", "\x00\x00\x00\x01\x81", 5},
1700 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1701 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1702 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1705 static int test_mpi(int i)
1708 const MPITEST *test = &kMPITests[i];
1709 size_t mpi_len, mpi_len2;
1714 if (!TEST_ptr(bn = BN_new())
1715 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1717 mpi_len = BN_bn2mpi(bn, NULL);
1718 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1721 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1722 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1725 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1728 if (!TEST_BN_eq(bn, bn2)) {
1740 static int test_rand(void)
1745 if (!TEST_ptr(bn = BN_new()))
1748 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1749 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
1750 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
1751 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
1752 || !TEST_BN_eq_one(bn)
1753 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
1754 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
1755 || !TEST_BN_eq_one(bn)
1756 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
1757 || !TEST_BN_eq_word(bn, 3))
1766 static int test_negzero(void)
1768 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
1769 BIGNUM *numerator = NULL, *denominator = NULL;
1770 int consttime, st = 0;
1772 if (!TEST_ptr(a = BN_new())
1773 || !TEST_ptr(b = BN_new())
1774 || !TEST_ptr(c = BN_new())
1775 || !TEST_ptr(d = BN_new()))
1778 /* Test that BN_mul never gives negative zero. */
1779 if (!TEST_true(BN_set_word(a, 1)))
1781 BN_set_negative(a, 1);
1783 if (!TEST_true(BN_mul(c, a, b, ctx)))
1785 if (!TEST_BN_eq_zero(c)
1786 || !TEST_BN_ge_zero(c))
1789 for (consttime = 0; consttime < 2; consttime++) {
1790 if (!TEST_ptr(numerator = BN_new())
1791 || !TEST_ptr(denominator = BN_new()))
1794 BN_set_flags(numerator, BN_FLG_CONSTTIME);
1795 BN_set_flags(denominator, BN_FLG_CONSTTIME);
1797 /* Test that BN_div never gives negative zero in the quotient. */
1798 if (!TEST_true(BN_set_word(numerator, 1))
1799 || !TEST_true(BN_set_word(denominator, 2)))
1801 BN_set_negative(numerator, 1);
1802 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
1803 || !TEST_BN_eq_zero(a)
1804 || !TEST_BN_ge_zero(a))
1807 /* Test that BN_div never gives negative zero in the remainder. */
1808 if (!TEST_true(BN_set_word(denominator, 1))
1809 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
1810 || !TEST_BN_eq_zero(b)
1811 || !TEST_BN_ge_zero(b))
1814 BN_free(denominator);
1815 numerator = denominator = NULL;
1818 /* Test that BN_set_negative will not produce a negative zero. */
1820 BN_set_negative(a, 1);
1821 if (BN_is_negative(a))
1831 BN_free(denominator);
1835 static int test_badmod(void)
1837 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
1838 BN_MONT_CTX *mont = NULL;
1841 if (!TEST_ptr(a = BN_new())
1842 || !TEST_ptr(b = BN_new())
1843 || !TEST_ptr(zero = BN_new())
1844 || !TEST_ptr(mont = BN_MONT_CTX_new()))
1848 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
1852 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
1856 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
1860 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1865 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1870 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
1874 /* Some operations also may not be used with an even modulus. */
1875 if (!TEST_true(BN_set_word(b, 16)))
1878 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
1882 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1887 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1897 BN_MONT_CTX_free(mont);
1901 static int test_expmodzero(void)
1903 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
1906 if (!TEST_ptr(zero = BN_new())
1907 || !TEST_ptr(a = BN_new())
1908 || !TEST_ptr(r = BN_new()))
1912 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
1913 || !TEST_BN_eq_zero(r)
1914 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
1916 || !TEST_BN_eq_zero(r)
1917 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
1920 || !TEST_BN_eq_zero(r)
1921 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
1922 BN_value_one(), NULL, NULL))
1923 || !TEST_BN_eq_zero(r))
1934 static int test_smallprime(void)
1936 static const int kBits = 10;
1940 if (!TEST_ptr(r = BN_new())
1941 || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
1943 || !TEST_int_eq(BN_num_bits(r), kBits))
1952 static int test_3_is_prime(void)
1958 * For a long time, small primes were not considered prime when
1959 * do_trial_division was set.
1961 if (!TEST_ptr(r = BN_new())
1962 || !TEST_true(BN_set_word(r, 3))
1963 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
1964 0 /* do_trial_division */, NULL), 1)
1965 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
1966 1 /* do_trial_division */, NULL), 1))
1976 static int file_test_run(STANZA *s)
1978 static const FILETEST filetests[] = {
1980 {"LShift1", file_lshift1},
1981 {"LShift", file_lshift},
1982 {"RShift", file_rshift},
1983 {"Square", file_square},
1984 {"Product", file_product},
1985 {"Quotient", file_quotient},
1986 {"ModMul", file_modmul},
1987 {"ModExp", file_modexp},
1989 {"ModSqrt", file_modsqrt},
1991 int numtests = OSSL_NELEM(filetests);
1992 const FILETEST *tp = filetests;
1994 for ( ; --numtests >= 0; tp++) {
1995 if (findattr(s, tp->name) != NULL) {
1997 TEST_info("%s:%d: Failed %s test",
1998 s->test_file, s->start, tp->name);
2004 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2008 static int run_file_tests(int i)
2011 char *testfile = test_get_argument(i);
2014 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2016 if (!test_start_file(s, testfile)) {
2021 /* Read test file. */
2022 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2023 if (s->numpairs == 0)
2025 if (!file_test_run(s))
2028 test_clearstanza(s);
2038 int setup_tests(void)
2040 int n = test_get_argument_count();
2042 if (!TEST_ptr(ctx = BN_CTX_new()))
2047 ADD_TEST(test_div_recip);
2049 ADD_TEST(test_modexp_mont5);
2050 ADD_TEST(test_kronecker);
2051 ADD_TEST(test_rand);
2052 ADD_TEST(test_bn2padded);
2053 ADD_TEST(test_dec2bn);
2054 ADD_TEST(test_hex2bn);
2055 ADD_TEST(test_asc2bn);
2056 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2057 ADD_TEST(test_negzero);
2058 ADD_TEST(test_badmod);
2059 ADD_TEST(test_expmodzero);
2060 ADD_TEST(test_smallprime);
2061 #ifndef OPENSSL_NO_EC2M
2062 ADD_TEST(test_gf2m_add);
2063 ADD_TEST(test_gf2m_mod);
2064 ADD_TEST(test_gf2m_mul);
2065 ADD_TEST(test_gf2m_sqr);
2066 ADD_TEST(test_gf2m_modinv);
2067 ADD_TEST(test_gf2m_moddiv);
2068 ADD_TEST(test_gf2m_modexp);
2069 ADD_TEST(test_gf2m_modsqrt);
2070 ADD_TEST(test_gf2m_modsolvequad);
2072 ADD_TEST(test_3_is_prime);
2074 ADD_ALL_TESTS(run_file_tests, n);
2079 void cleanup_tests(void)