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
16 #include <internal/numbers.h>
17 #include <openssl/bn.h>
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
24 * In bn_lcl.h, bn_expand() is defined as a static ossl_inline function.
25 * This is fine in itself, it will end up as an unused static function in
26 * the worst case. However, it references bn_expand2(), which is a private
27 * function in libcrypto and therefore unavailable on some systems. This
28 * may result in a linker error because of unresolved symbols.
30 * To avoid this, we define a dummy variant of bn_expand2() here, and to
31 * avoid possible clashes with libcrypto, we rename it first, using a macro.
33 #define bn_expand2 dummy_bn_expand2
34 BIGNUM *bn_expand2(BIGNUM *b, int words);
35 BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; }
36 #include "../crypto/bn/bn_lcl.h"
39 * Things in boring, not in openssl. TODO we should add them.
41 #define HAVE_BN_PADDED 0
42 #define HAVE_BN_SQRT 0
44 typedef struct filetest_st {
46 int (*func)(STANZA *s);
49 typedef struct mpitest_st {
55 static const int NUM0 = 100; /* number of tests */
56 static const int NUM1 = 50; /* additional tests for some functions */
60 * Polynomial coefficients used in GFM tests.
62 #ifndef OPENSSL_NO_EC2M
63 static int p0[] = { 163, 7, 6, 3, 0, -1 };
64 static int p1[] = { 193, 15, 0, -1 };
68 * Look for |key| in the stanza and return it or NULL if not found.
70 static const char *findattr(STANZA *s, const char *key)
75 for ( ; --i >= 0; pp++)
76 if (strcasecmp(pp->key, key) == 0)
82 * Parse BIGNUM, return number of bytes parsed.
84 static int parseBN(BIGNUM **out, const char *in)
87 return BN_hex2bn(out, in);
90 static int parsedecBN(BIGNUM **out, const char *in)
93 return BN_dec2bn(out, in);
96 static BIGNUM *getBN(STANZA *s, const char *attribute)
101 if ((hex = findattr(s, attribute)) == NULL) {
102 TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
106 if (parseBN(&ret, hex) != (int)strlen(hex)) {
107 TEST_error("Could not decode '%s'", hex);
113 static int getint(STANZA *s, int *out, const char *attribute)
119 if (!TEST_ptr(ret = getBN(s, attribute))
120 || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
130 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
132 if (BN_cmp(expected, actual) == 0)
135 TEST_error("unexpected %s value", op);
136 TEST_BN_eq(expected, actual);
142 * Return a "random" flag for if a BN should be negated.
144 static int rand_neg(void)
146 static unsigned int neg = 0;
147 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
149 return sign[(neg++) % 8];
153 static int test_sub(void)
155 BIGNUM *a = NULL, *b = NULL, *c = NULL;
158 if (!TEST_ptr(a = BN_new())
159 || !TEST_ptr(b = BN_new())
160 || !TEST_ptr(c = BN_new()))
163 for (i = 0; i < NUM0 + NUM1; i++) {
165 BN_bntest_rand(a, 512, 0, 0);
167 if (!TEST_int_ne(BN_set_bit(a, i), 0))
171 BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
178 if (!TEST_BN_eq_zero(c))
190 static int test_div_recip(void)
192 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
193 BN_RECP_CTX *recp = NULL;
196 if (!TEST_ptr(a = BN_new())
197 || !TEST_ptr(b = BN_new())
198 || !TEST_ptr(c = BN_new())
199 || !TEST_ptr(d = BN_new())
200 || !TEST_ptr(e = BN_new())
201 || !TEST_ptr(recp = BN_RECP_CTX_new()))
204 for (i = 0; i < NUM0 + NUM1; i++) {
206 BN_bntest_rand(a, 400, 0, 0);
211 BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
214 BN_RECP_CTX_set(recp, b, ctx);
215 BN_div_recp(d, c, a, recp, ctx);
216 BN_mul(e, d, b, ctx);
219 if (!TEST_BN_eq_zero(d))
229 BN_RECP_CTX_free(recp);
234 static int test_mod(void)
236 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
239 if (!TEST_ptr(a = BN_new())
240 || !TEST_ptr(b = BN_new())
241 || !TEST_ptr(c = BN_new())
242 || !TEST_ptr(d = BN_new())
243 || !TEST_ptr(e = BN_new()))
246 BN_bntest_rand(a, 1024, 0, 0);
247 for (i = 0; i < NUM0; i++) {
248 BN_bntest_rand(b, 450 + i * 10, 0, 0);
251 BN_mod(c, a, b, ctx);
252 BN_div(d, e, a, b, ctx);
254 if (!TEST_BN_eq_zero(e))
267 static const char *bn1strings[] = {
268 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
269 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
270 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
271 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
272 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
273 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
274 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
275 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
276 "0000000000000000000000000000000000000000000000000000000000000000",
277 "0000000000000000000000000000000000000000000000000000000000000000",
278 "0000000000000000000000000000000000000000000000000000000000000000",
279 "0000000000000000000000000000000000000000000000000000000000000000",
280 "0000000000000000000000000000000000000000000000000000000000000000",
281 "0000000000000000000000000000000000000000000000000000000000000000",
282 "0000000000000000000000000000000000000000000000000000000000000000",
283 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
287 static const char *bn2strings[] = {
288 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
289 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
290 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
291 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
292 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
293 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
294 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
295 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
296 "0000000000000000000000000000000000000000000000000000000000000000",
297 "0000000000000000000000000000000000000000000000000000000000000000",
298 "0000000000000000000000000000000000000000000000000000000000000000",
299 "0000000000000000000000000000000000000000000000000000000000000000",
300 "0000000000000000000000000000000000000000000000000000000000000000",
301 "0000000000000000000000000000000000000000000000000000000000000000",
302 "0000000000000000000000000000000000000000000000000000000000000000",
303 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
307 static char *glue(const char *list[])
313 for (i = 0; list[i] != NULL; i++)
314 len += strlen(list[i]);
315 if (!TEST_ptr(p = save = OPENSSL_malloc(len + 1)))
317 for (i = 0; list[i] != NULL; i++)
318 p += strlen(strcpy(p, list[i]));
323 * Test constant-time modular exponentiation with 1024-bit inputs, which on
324 * x86_64 cause a different code branch to be taken.
326 static int test_modexp_mont5(void)
328 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
329 BIGNUM *b = NULL, *n = NULL, *c = NULL;
330 BN_MONT_CTX *mont = NULL;
334 if (!TEST_ptr(a = BN_new())
335 || !TEST_ptr(p = BN_new())
336 || !TEST_ptr(m = BN_new())
337 || !TEST_ptr(d = BN_new())
338 || !TEST_ptr(e = BN_new())
339 || !TEST_ptr(b = BN_new())
340 || !TEST_ptr(n = BN_new())
341 || !TEST_ptr(c = BN_new())
342 || !TEST_ptr(mont = BN_MONT_CTX_new()))
345 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
347 BN_bntest_rand(a, 1024, 0, 0);
349 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
351 if (!TEST_BN_eq_one(d))
354 /* Regression test for carry bug in mulx4x_mont */
356 "7878787878787878787878787878787878787878787878787878787878787878"
357 "7878787878787878787878787878787878787878787878787878787878787878"
358 "7878787878787878787878787878787878787878787878787878787878787878"
359 "7878787878787878787878787878787878787878787878787878787878787878");
361 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
362 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
363 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
364 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
366 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
367 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
368 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
369 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
370 BN_MONT_CTX_set(mont, n, ctx);
371 BN_mod_mul_montgomery(c, a, b, mont, ctx);
372 BN_mod_mul_montgomery(d, b, a, mont, ctx);
373 if (!TEST_BN_eq(c, d))
376 /* Regression test for carry bug in sqr[x]8x_mont */
377 bigstring = glue(bn1strings);
378 BN_hex2bn(&n, bigstring);
379 OPENSSL_free(bigstring);
380 bigstring = glue(bn2strings);
381 BN_hex2bn(&a, bigstring);
382 OPENSSL_free(bigstring);
385 BN_MONT_CTX_set(mont, n, ctx);
386 BN_mod_mul_montgomery(c, a, a, mont, ctx);
387 BN_mod_mul_montgomery(d, a, b, mont, ctx);
388 if (!TEST_BN_eq(c, d))
392 BN_bntest_rand(p, 1024, 0, 0);
394 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
395 || !TEST_BN_eq_zero(d))
399 * Craft an input whose Montgomery representation is 1, i.e., shorter
400 * than the modulus m, in order to test the const time precomputation
401 * scattering/gathering.
404 BN_MONT_CTX_set(mont, m, ctx);
405 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
406 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
407 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
408 || !TEST_BN_eq(a, d))
411 /* Finally, some regular test vectors. */
412 BN_bntest_rand(e, 1024, 0, 0);
413 if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
414 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
415 || !TEST_BN_eq(a, d))
421 BN_MONT_CTX_free(mont);
433 #ifndef OPENSSL_NO_EC2M
434 static int test_gf2m_add(void)
436 BIGNUM *a = NULL, *b = NULL, *c = NULL;
439 if (!TEST_ptr(a = BN_new())
440 || !TEST_ptr(b = BN_new())
441 || !TEST_ptr(c = BN_new()))
444 for (i = 0; i < NUM0; i++) {
445 BN_rand(a, 512, 0, 0);
446 BN_copy(b, BN_value_one());
449 BN_GF2m_add(c, a, b);
450 /* Test that two added values have the correct parity. */
451 if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
452 || (!BN_is_odd(a) && !BN_is_odd(c))))
454 BN_GF2m_add(c, c, c);
455 /* Test that c + c = 0. */
456 if (!TEST_BN_eq_zero(c))
467 static int test_gf2m_mod(void)
469 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
472 if (!TEST_ptr(a = BN_new())
473 || !TEST_ptr(b[0] = BN_new())
474 || !TEST_ptr(b[1] = BN_new())
475 || !TEST_ptr(c = BN_new())
476 || !TEST_ptr(d = BN_new())
477 || !TEST_ptr(e = BN_new()))
480 BN_GF2m_arr2poly(p0, b[0]);
481 BN_GF2m_arr2poly(p1, b[1]);
483 for (i = 0; i < NUM0; i++) {
484 BN_bntest_rand(a, 1024, 0, 0);
485 for (j = 0; j < 2; j++) {
486 BN_GF2m_mod(c, a, b[j]);
487 BN_GF2m_add(d, a, c);
488 BN_GF2m_mod(e, d, b[j]);
489 /* Test that a + (a mod p) mod p == 0. */
490 if (!TEST_BN_eq_zero(e))
505 static int test_gf2m_mul(void)
507 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
508 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
511 if (!TEST_ptr(a = BN_new())
512 || !TEST_ptr(b[0] = BN_new())
513 || !TEST_ptr(b[1] = BN_new())
514 || !TEST_ptr(c = BN_new())
515 || !TEST_ptr(d = BN_new())
516 || !TEST_ptr(e = BN_new())
517 || !TEST_ptr(f = BN_new())
518 || !TEST_ptr(g = BN_new())
519 || !TEST_ptr(h = BN_new()))
522 BN_GF2m_arr2poly(p0, b[0]);
523 BN_GF2m_arr2poly(p1, b[1]);
525 for (i = 0; i < NUM0; i++) {
526 BN_bntest_rand(a, 1024, 0, 0);
527 BN_bntest_rand(c, 1024, 0, 0);
528 BN_bntest_rand(d, 1024, 0, 0);
529 for (j = 0; j < 2; j++) {
530 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
531 BN_GF2m_add(f, a, d);
532 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
533 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
534 BN_GF2m_add(f, e, g);
535 BN_GF2m_add(f, f, h);
536 /* Test that (a+d)*c = a*c + d*c. */
537 if (!TEST_BN_eq_zero(f))
556 static int test_gf2m_sqr(void)
558 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
561 if (!TEST_ptr(a = BN_new())
562 || !TEST_ptr(b[0] = BN_new())
563 || !TEST_ptr(b[1] = BN_new())
564 || !TEST_ptr(c = BN_new())
565 || !TEST_ptr(d = BN_new()))
568 BN_GF2m_arr2poly(p0, b[0]);
569 BN_GF2m_arr2poly(p1, b[1]);
571 for (i = 0; i < NUM0; i++) {
572 BN_bntest_rand(a, 1024, 0, 0);
573 for (j = 0; j < 2; j++) {
574 BN_GF2m_mod_sqr(c, a, b[j], ctx);
576 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
577 BN_GF2m_add(d, c, d);
578 /* Test that a*a = a^2. */
579 if (!TEST_BN_eq_zero(d))
593 static int test_gf2m_modinv(void)
595 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
598 if (!TEST_ptr(a = BN_new())
599 || !TEST_ptr(b[0] = BN_new())
600 || !TEST_ptr(b[1] = BN_new())
601 || !TEST_ptr(c = BN_new())
602 || !TEST_ptr(d = BN_new()))
605 BN_GF2m_arr2poly(p0, b[0]);
606 BN_GF2m_arr2poly(p1, b[1]);
608 for (i = 0; i < NUM0; i++) {
609 BN_bntest_rand(a, 512, 0, 0);
610 for (j = 0; j < 2; j++) {
611 BN_GF2m_mod_inv(c, a, b[j], ctx);
612 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
613 /* Test that ((1/a)*a) = 1. */
614 if (!TEST_BN_eq_one(d))
628 static int test_gf2m_moddiv(void)
630 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
631 BIGNUM *e = NULL, *f = NULL;
634 if (!TEST_ptr(a = BN_new())
635 || !TEST_ptr(b[0] = BN_new())
636 || !TEST_ptr(b[1] = BN_new())
637 || !TEST_ptr(c = BN_new())
638 || !TEST_ptr(d = BN_new())
639 || !TEST_ptr(e = BN_new())
640 || !TEST_ptr(f = BN_new()))
643 BN_GF2m_arr2poly(p0, b[0]);
644 BN_GF2m_arr2poly(p1, b[1]);
646 for (i = 0; i < NUM0; i++) {
647 BN_bntest_rand(a, 512, 0, 0);
648 BN_bntest_rand(c, 512, 0, 0);
649 for (j = 0; j < 2; j++) {
650 BN_GF2m_mod_div(d, a, c, b[j], ctx);
651 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
652 BN_GF2m_mod_div(f, a, e, b[j], ctx);
653 /* Test that ((a/c)*c)/a = 1. */
654 if (!TEST_BN_eq_one(f))
670 static int test_gf2m_modexp(void)
672 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
673 BIGNUM *e = NULL, *f = NULL;
676 if (!TEST_ptr(a = BN_new())
677 || !TEST_ptr(b[0] = BN_new())
678 || !TEST_ptr(b[1] = BN_new())
679 || !TEST_ptr(c = BN_new())
680 || !TEST_ptr(d = BN_new())
681 || !TEST_ptr(e = BN_new())
682 || !TEST_ptr(f = BN_new()))
685 BN_GF2m_arr2poly(p0, b[0]);
686 BN_GF2m_arr2poly(p1, b[1]);
688 for (i = 0; i < NUM0; i++) {
689 BN_bntest_rand(a, 512, 0, 0);
690 BN_bntest_rand(c, 512, 0, 0);
691 BN_bntest_rand(d, 512, 0, 0);
692 for (j = 0; j < 2; j++) {
693 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
694 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
695 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
697 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
698 BN_GF2m_add(f, e, f);
699 /* Test that a^(c+d)=a^c*a^d. */
700 if (!TEST_BN_eq_zero(f))
716 static int test_gf2m_modsqrt(void)
718 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
719 BIGNUM *e = NULL, *f = NULL;
722 if (!TEST_ptr(a = BN_new())
723 || !TEST_ptr(b[0] = BN_new())
724 || !TEST_ptr(b[1] = BN_new())
725 || !TEST_ptr(c = BN_new())
726 || !TEST_ptr(d = BN_new())
727 || !TEST_ptr(e = BN_new())
728 || !TEST_ptr(f = BN_new()))
731 BN_GF2m_arr2poly(p0, b[0]);
732 BN_GF2m_arr2poly(p1, b[1]);
734 for (i = 0; i < NUM0; i++) {
735 BN_bntest_rand(a, 512, 0, 0);
736 for (j = 0; j < 2; j++) {
737 BN_GF2m_mod(c, a, b[j]);
738 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
739 BN_GF2m_mod_sqr(e, d, b[j], ctx);
740 BN_GF2m_add(f, c, e);
741 /* Test that d^2 = a, where d = sqrt(a). */
742 if (!TEST_BN_eq_zero(f))
758 static int test_gf2m_modsolvequad(void)
760 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
762 int i, j, s = 0, t, st = 0;
764 if (!TEST_ptr(a = BN_new())
765 || !TEST_ptr(b[0] = BN_new())
766 || !TEST_ptr(b[1] = BN_new())
767 || !TEST_ptr(c = BN_new())
768 || !TEST_ptr(d = BN_new())
769 || !TEST_ptr(e = BN_new()))
772 BN_GF2m_arr2poly(p0, b[0]);
773 BN_GF2m_arr2poly(p1, b[1]);
775 for (i = 0; i < NUM0; i++) {
776 BN_bntest_rand(a, 512, 0, 0);
777 for (j = 0; j < 2; j++) {
778 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
781 BN_GF2m_mod_sqr(d, c, b[j], ctx);
782 BN_GF2m_add(d, c, d);
783 BN_GF2m_mod(e, a, b[j]);
784 BN_GF2m_add(e, e, d);
786 * Test that solution of quadratic c satisfies c^2 + c = a.
788 if (!TEST_BN_eq_zero(e))
793 if (!TEST_int_ge(s, 0)) {
794 TEST_info("%d tests found no roots; probably an error", NUM0);
809 static int test_kronecker(void)
811 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
812 int i, legendre, kronecker, st = 0;
814 if (!TEST_ptr(a = BN_new())
815 || !TEST_ptr(b = BN_new())
816 || !TEST_ptr(r = BN_new())
817 || !TEST_ptr(t = BN_new()))
821 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
822 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
823 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
824 * generate a random prime b and compare these values for a number of
825 * random a's. (That is, we run the Solovay-Strassen primality test to
826 * confirm that b is prime, except that we don't want to test whether b
827 * is prime but whether BN_kronecker works.)
830 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
834 for (i = 0; i < NUM0; i++) {
835 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
839 /* t := (|b|-1)/2 (note that b is odd) */
840 if (!TEST_true(BN_copy(t, b)))
843 if (!TEST_true(BN_sub_word(t, 1)))
845 if (!TEST_true(BN_rshift1(t, t)))
850 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
854 if (BN_is_word(r, 1))
856 else if (BN_is_zero(r))
859 if (!TEST_true(BN_add_word(r, 1)))
861 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
862 TEST_info("Legendre symbol computation failed");
868 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
870 /* we actually need BN_kronecker(a, |b|) */
871 if (a->neg && b->neg)
872 kronecker = -kronecker;
874 if (!TEST_int_eq(legendre, kronecker))
887 static int file_sum(STANZA *s)
889 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
893 if (!TEST_ptr(a = getBN(s, "A"))
894 || !TEST_ptr(b = getBN(s, "B"))
895 || !TEST_ptr(sum = getBN(s, "Sum"))
896 || !TEST_ptr(ret = BN_new()))
899 if (!TEST_true(BN_add(ret, a, b))
900 || !equalBN("A + B", sum, ret)
901 || !TEST_true(BN_sub(ret, sum, a))
902 || !equalBN("Sum - A", b, ret)
903 || !TEST_true(BN_sub(ret, sum, b))
904 || !equalBN("Sum - B", a, ret))
908 * Test that the functions work when |r| and |a| point to the same BIGNUM,
909 * or when |r| and |b| point to the same BIGNUM.
910 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
912 if (!TEST_true(BN_copy(ret, a))
913 || !TEST_true(BN_add(ret, ret, b))
914 || !equalBN("A + B (r is a)", sum, ret)
915 || !TEST_true(BN_copy(ret, b))
916 || !TEST_true(BN_add(ret, a, ret))
917 || !equalBN("A + B (r is b)", sum, ret)
918 || !TEST_true(BN_copy(ret, sum))
919 || !TEST_true(BN_sub(ret, ret, a))
920 || !equalBN("Sum - A (r is a)", b, ret)
921 || !TEST_true(BN_copy(ret, a))
922 || !TEST_true(BN_sub(ret, sum, ret))
923 || !equalBN("Sum - A (r is b)", b, ret)
924 || !TEST_true(BN_copy(ret, sum))
925 || !TEST_true(BN_sub(ret, ret, b))
926 || !equalBN("Sum - B (r is a)", a, ret)
927 || !TEST_true(BN_copy(ret, b))
928 || !TEST_true(BN_sub(ret, sum, ret))
929 || !equalBN("Sum - B (r is b)", a, ret))
933 * Test BN_uadd() and BN_usub() with the prerequisites they are
934 * documented as having. Note that these functions are frequently used
935 * when the prerequisites don't hold. In those cases, they are supposed
936 * to work as if the prerequisite hold, but we don't test that yet.
939 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
940 if (!TEST_true(BN_uadd(ret, a, b))
941 || !equalBN("A +u B", sum, ret)
942 || !TEST_true(BN_usub(ret, sum, a))
943 || !equalBN("Sum -u A", b, ret)
944 || !TEST_true(BN_usub(ret, sum, b))
945 || !equalBN("Sum -u B", a, ret))
948 * Test that the functions work when |r| and |a| point to the same
949 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
950 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
952 if (!TEST_true(BN_copy(ret, a))
953 || !TEST_true(BN_uadd(ret, ret, b))
954 || !equalBN("A +u B (r is a)", sum, ret)
955 || !TEST_true(BN_copy(ret, b))
956 || !TEST_true(BN_uadd(ret, a, ret))
957 || !equalBN("A +u B (r is b)", sum, ret)
958 || !TEST_true(BN_copy(ret, sum))
959 || !TEST_true(BN_usub(ret, ret, a))
960 || !equalBN("Sum -u A (r is a)", b, ret)
961 || !TEST_true(BN_copy(ret, a))
962 || !TEST_true(BN_usub(ret, sum, ret))
963 || !equalBN("Sum -u A (r is b)", b, ret)
964 || !TEST_true(BN_copy(ret, sum))
965 || !TEST_true(BN_usub(ret, ret, b))
966 || !equalBN("Sum -u B (r is a)", a, ret)
967 || !TEST_true(BN_copy(ret, b))
968 || !TEST_true(BN_usub(ret, sum, ret))
969 || !equalBN("Sum -u B (r is b)", a, ret))
974 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
976 b_word = BN_get_word(b);
977 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
978 if (!TEST_true(BN_copy(ret, a))
979 || !TEST_true(BN_add_word(ret, b_word))
980 || !equalBN("A + B (word)", sum, ret)
981 || !TEST_true(BN_copy(ret, sum))
982 || !TEST_true(BN_sub_word(ret, b_word))
983 || !equalBN("Sum - B (word)", a, ret))
996 static int file_lshift1(STANZA *s)
998 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
999 BIGNUM *two = NULL, *remainder = NULL;
1002 if (!TEST_ptr(a = getBN(s, "A"))
1003 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1004 || !TEST_ptr(zero = BN_new())
1005 || !TEST_ptr(ret = BN_new())
1006 || !TEST_ptr(two = BN_new())
1007 || !TEST_ptr(remainder = BN_new()))
1012 if (!TEST_true(BN_set_word(two, 2))
1013 || !TEST_true(BN_add(ret, a, a))
1014 || !equalBN("A + A", lshift1, ret)
1015 || !TEST_true(BN_mul(ret, a, two, ctx))
1016 || !equalBN("A * 2", lshift1, ret)
1017 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1018 || !equalBN("LShift1 / 2", a, ret)
1019 || !equalBN("LShift1 % 2", zero, remainder)
1020 || !TEST_true(BN_lshift1(ret, a))
1021 || !equalBN("A << 1", lshift1, ret)
1022 || !TEST_true(BN_rshift1(ret, lshift1))
1023 || !equalBN("LShift >> 1", a, ret)
1024 || !TEST_true(BN_rshift1(ret, lshift1))
1025 || !equalBN("LShift >> 1", a, ret))
1028 /* Set the LSB to 1 and test rshift1 again. */
1029 if (!TEST_true(BN_set_bit(lshift1, 0))
1030 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1031 || !equalBN("(LShift1 | 1) / 2", a, ret)
1032 || !TEST_true(BN_rshift1(ret, lshift1))
1033 || !equalBN("(LShift | 1) >> 1", a, ret))
1048 static int file_lshift(STANZA *s)
1050 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1053 if (!TEST_ptr(a = getBN(s, "A"))
1054 || !TEST_ptr(lshift = getBN(s, "LShift"))
1055 || !TEST_ptr(ret = BN_new()))
1057 if (!TEST_true(BN_lshift(ret, a, n))
1058 || !equalBN("A << N", lshift, ret)
1059 || !TEST_true(BN_rshift(ret, lshift, n))
1060 || !equalBN("A >> N", a, ret))
1071 static int file_rshift(STANZA *s)
1073 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1076 if (!TEST_ptr(a = getBN(s, "A"))
1077 || !TEST_ptr(rshift = getBN(s, "RShift"))
1078 || !TEST_ptr(ret = BN_new())
1079 || !getint(s, &n, "N"))
1082 if (!TEST_true(BN_rshift(ret, a, n))
1083 || !equalBN("A >> N", rshift, ret))
1086 /* If N == 1, try with rshift1 as well */
1088 if (!TEST_true(BN_rshift1(ret, a))
1089 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1101 static int file_square(STANZA *s)
1103 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1104 BIGNUM *remainder = NULL, *tmp = NULL;
1107 if (!TEST_ptr(a = getBN(s, "A"))
1108 || !TEST_ptr(square = getBN(s, "Square"))
1109 || !TEST_ptr(zero = BN_new())
1110 || !TEST_ptr(ret = BN_new())
1111 || !TEST_ptr(remainder = BN_new()))
1115 if (!TEST_true(BN_sqr(ret, a, ctx))
1116 || !equalBN("A^2", square, ret)
1117 || !TEST_true(BN_mul(ret, a, a, ctx))
1118 || !equalBN("A * A", square, ret)
1119 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1120 || !equalBN("Square / A", a, ret)
1121 || !equalBN("Square % A", zero, remainder))
1125 BN_set_negative(a, 0);
1126 if (!TEST_true(BN_sqrt(ret, square, ctx))
1127 || !equalBN("sqrt(Square)", a, ret))
1130 /* BN_sqrt should fail on non-squares and negative numbers. */
1131 if (!TEST_BN_eq_zero(square)) {
1132 if (!TEST_ptr(tmp = BN_new())
1133 || !TEST_true(BN_copy(tmp, square)))
1135 BN_set_negative(tmp, 1);
1137 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1141 BN_set_negative(tmp, 0);
1142 if (BN_add(tmp, tmp, BN_value_one()))
1144 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1161 static int file_product(STANZA *s)
1163 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1164 BIGNUM *remainder = NULL, *zero = NULL;
1167 if (!TEST_ptr(a = getBN(s, "A"))
1168 || !TEST_ptr(b = getBN(s, "B"))
1169 || !TEST_ptr(product = getBN(s, "Product"))
1170 || !TEST_ptr(ret = BN_new())
1171 || !TEST_ptr(remainder = BN_new())
1172 || !TEST_ptr(zero = BN_new()))
1177 if (!TEST_true(BN_mul(ret, a, b, ctx))
1178 || !equalBN("A * B", product, ret)
1179 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1180 || !equalBN("Product / A", b, ret)
1181 || !equalBN("Product % A", zero, remainder)
1182 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1183 || !equalBN("Product / B", a, ret)
1184 || !equalBN("Product % B", zero, remainder))
1198 static int file_quotient(STANZA *s)
1200 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1201 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1202 BN_ULONG b_word, ret_word;
1205 if (!TEST_ptr(a = getBN(s, "A"))
1206 || !TEST_ptr(b = getBN(s, "B"))
1207 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1208 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1209 || !TEST_ptr(ret = BN_new())
1210 || !TEST_ptr(ret2 = BN_new())
1211 || !TEST_ptr(nnmod = BN_new()))
1214 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1215 || !equalBN("A / B", quotient, ret)
1216 || !equalBN("A % B", remainder, ret2)
1217 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1218 || !TEST_true(BN_add(ret, ret, remainder))
1219 || !equalBN("Quotient * B + Remainder", a, ret))
1223 * Test with BN_mod_word() and BN_div_word() if the divisor is
1226 b_word = BN_get_word(b);
1227 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1228 BN_ULONG remainder_word = BN_get_word(remainder);
1230 assert(remainder_word != (BN_ULONG)-1);
1231 if (!TEST_ptr(BN_copy(ret, a)))
1233 ret_word = BN_div_word(ret, b_word);
1234 if (ret_word != remainder_word) {
1237 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1238 ret_word, remainder_word);
1240 TEST_error("Got A %% B (word) mismatch");
1244 if (!equalBN ("A / B (word)", quotient, ret))
1247 ret_word = BN_mod_word(a, b_word);
1248 if (ret_word != remainder_word) {
1251 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1252 ret_word, remainder_word);
1254 TEST_error("Got A %% B (word) mismatch");
1260 /* Test BN_nnmod. */
1261 if (!BN_is_negative(b)) {
1262 if (!TEST_true(BN_copy(nnmod, remainder))
1263 || (BN_is_negative(nnmod)
1264 && !TEST_true(BN_add(nnmod, nnmod, b)))
1265 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1266 || !equalBN("A % B (non-negative)", nnmod, ret))
1282 static int file_modmul(STANZA *s)
1284 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1287 if (!TEST_ptr(a = getBN(s, "A"))
1288 || !TEST_ptr(b = getBN(s, "B"))
1289 || !TEST_ptr(m = getBN(s, "M"))
1290 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1291 || !TEST_ptr(ret = BN_new()))
1294 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1295 || !equalBN("A * B (mod M)", mod_mul, ret))
1299 /* Reduce |a| and |b| and test the Montgomery version. */
1300 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1301 BIGNUM *a_tmp = BN_new();
1302 BIGNUM *b_tmp = BN_new();
1304 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1305 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1306 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1307 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1308 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1309 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1310 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1312 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1313 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1317 BN_MONT_CTX_free(mont);
1334 static int file_modexp(STANZA *s)
1336 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1337 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1340 if (!TEST_ptr(a = getBN(s, "A"))
1341 || !TEST_ptr(e = getBN(s, "E"))
1342 || !TEST_ptr(m = getBN(s, "M"))
1343 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1344 || !TEST_ptr(ret = BN_new())
1345 || !TEST_ptr(d = BN_new()))
1348 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1349 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1353 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1354 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1355 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1357 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1361 /* Regression test for carry propagation bug in sqr8x_reduction */
1362 BN_hex2bn(&a, "050505050505");
1363 BN_hex2bn(&b, "02");
1365 "4141414141414141414141274141414141414141414141414141414141414141"
1366 "4141414141414141414141414141414141414141414141414141414141414141"
1367 "4141414141414141414141800000000000000000000000000000000000000000"
1368 "0000000000000000000000000000000000000000000000000000000000000000"
1369 "0000000000000000000000000000000000000000000000000000000000000000"
1370 "0000000000000000000000000000000000000000000000000000000001");
1371 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1372 || !TEST_true(BN_mul(e, a, a, ctx))
1373 || !TEST_BN_eq(d, e))
1389 static int file_exp(STANZA *s)
1391 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1394 if (!TEST_ptr(a = getBN(s, "A"))
1395 || !TEST_ptr(e = getBN(s, "E"))
1396 || !TEST_ptr(exp = getBN(s, "Exp"))
1397 || !TEST_ptr(ret = BN_new()))
1400 if (!TEST_true(BN_exp(ret, a, e, ctx))
1401 || !equalBN("A ^ E", exp, ret))
1413 static int file_modsqrt(STANZA *s)
1415 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1418 if (!TEST_ptr(a = getBN(s, "A"))
1419 || !TEST_ptr(p = getBN(s, "P"))
1420 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1421 || !TEST_ptr(ret = BN_new())
1422 || !TEST_ptr(ret2 = BN_new()))
1425 /* There are two possible answers. */
1426 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1427 || !TEST_true(BN_sub(ret2, p, ret)))
1430 /* The first condition should NOT be a test. */
1431 if (BN_cmp(ret2, mod_sqrt) != 0
1432 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1445 static int test_bn2padded(void)
1448 uint8_t zeros[256], out[256], reference[128];
1449 BIGNUM *n = BN_new();
1452 /* Test edge case at 0. */
1455 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1457 memset(out, -1, sizeof(out));
1458 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1460 memset(zeros, 0, sizeof(zeros));
1461 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1464 /* Test a random numbers at various byte lengths. */
1465 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1466 #define TOP_BIT_ON 0
1467 #define BOTTOM_BIT_NOTOUCH 0
1468 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1470 if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1471 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1473 /* Empty buffer should fail. */
1474 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1476 /* One byte short should fail. */
1477 if (BN_bn2bin_padded(out, bytes - 1, n))
1479 /* Exactly right size should encode. */
1480 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1481 || TEST_mem_eq(out, bytes, reference, bytes))
1483 /* Pad up one byte extra. */
1484 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1485 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1486 || !TEST_mem_eq(out, 1, zeros, 1))
1488 /* Pad up to 256. */
1489 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1490 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1492 || !TEST_mem_eq(out, sizseof(out) - bytes,
1493 zeros, sizeof(out) - bytes))
1506 static int test_dec2bn(void)
1511 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1512 || !TEST_BN_eq_word(bn, 0)
1513 || !TEST_BN_eq_zero(bn)
1514 || !TEST_BN_le_zero(bn)
1515 || !TEST_BN_ge_zero(bn)
1516 || !TEST_BN_even(bn))
1521 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1522 || !TEST_BN_eq_word(bn, 256)
1523 || !TEST_BN_ge_zero(bn)
1524 || !TEST_BN_gt_zero(bn)
1525 || !TEST_BN_ne_zero(bn)
1526 || !TEST_BN_even(bn))
1531 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1532 || !TEST_BN_abs_eq_word(bn, 42)
1533 || !TEST_BN_lt_zero(bn)
1534 || !TEST_BN_le_zero(bn)
1535 || !TEST_BN_ne_zero(bn)
1536 || !TEST_BN_even(bn))
1541 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1542 || !TEST_BN_eq_word(bn, 1)
1543 || !TEST_BN_ne_zero(bn)
1544 || !TEST_BN_gt_zero(bn)
1545 || !TEST_BN_ge_zero(bn)
1546 || !TEST_BN_eq_one(bn)
1547 || !TEST_BN_odd(bn))
1552 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1553 || !TEST_BN_eq_zero(bn)
1554 || !TEST_BN_ge_zero(bn)
1555 || !TEST_BN_le_zero(bn)
1556 || !TEST_BN_even(bn))
1561 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1562 || !TEST_BN_abs_eq_word(bn, 42)
1563 || !TEST_BN_ge_zero(bn)
1564 || !TEST_BN_gt_zero(bn)
1565 || !TEST_BN_ne_zero(bn)
1566 || !TEST_BN_even(bn))
1575 static int test_hex2bn(void)
1580 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1581 || !TEST_BN_eq_zero(bn)
1582 || !TEST_BN_ge_zero(bn)
1583 || !TEST_BN_even(bn))
1588 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1589 || !TEST_BN_eq_word(bn, 0x256)
1590 || !TEST_BN_ge_zero(bn)
1591 || !TEST_BN_gt_zero(bn)
1592 || !TEST_BN_ne_zero(bn)
1593 || !TEST_BN_even(bn))
1598 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1599 || !TEST_BN_abs_eq_word(bn, 0x42)
1600 || !TEST_BN_lt_zero(bn)
1601 || !TEST_BN_le_zero(bn)
1602 || !TEST_BN_ne_zero(bn)
1603 || !TEST_BN_even(bn))
1608 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1609 || !TEST_BN_eq_word(bn, 0xCB)
1610 || !TEST_BN_ge_zero(bn)
1611 || !TEST_BN_gt_zero(bn)
1612 || !TEST_BN_ne_zero(bn)
1613 || !TEST_BN_odd(bn))
1618 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1619 || !TEST_BN_eq_zero(bn)
1620 || !TEST_BN_ge_zero(bn)
1621 || !TEST_BN_le_zero(bn)
1622 || !TEST_BN_even(bn))
1627 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1628 || !TEST_BN_eq_word(bn, 0xabc)
1629 || !TEST_BN_ge_zero(bn)
1630 || !TEST_BN_gt_zero(bn)
1631 || !TEST_BN_ne_zero(bn)
1632 || !TEST_BN_even(bn))
1641 static int test_asc2bn(void)
1646 if (!TEST_ptr(bn = BN_new()))
1649 if (!TEST_true(BN_asc2bn(&bn, "0"))
1650 || !TEST_BN_eq_zero(bn)
1651 || !TEST_BN_ge_zero(bn))
1654 if (!TEST_true(BN_asc2bn(&bn, "256"))
1655 || !TEST_BN_eq_word(bn, 256)
1656 || !TEST_BN_ge_zero(bn))
1659 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1660 || !TEST_BN_abs_eq_word(bn, 42)
1661 || !TEST_BN_lt_zero(bn))
1664 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1665 || !TEST_BN_eq_word(bn, 0x1234)
1666 || !TEST_BN_ge_zero(bn))
1669 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1670 || !TEST_BN_eq_word(bn, 0x1234)
1671 || !TEST_BN_ge_zero(bn))
1674 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1675 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1676 || !TEST_BN_lt_zero(bn))
1679 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1680 || !TEST_BN_eq_zero(bn)
1681 || !TEST_BN_ge_zero(bn))
1684 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1685 || !TEST_BN_eq_word(bn, 123)
1686 || !TEST_BN_ge_zero(bn))
1695 static const MPITEST kMPITests[] = {
1696 {"0", "\x00\x00\x00\x00", 4},
1697 {"1", "\x00\x00\x00\x01\x01", 5},
1698 {"-1", "\x00\x00\x00\x01\x81", 5},
1699 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1700 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1701 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1704 static int test_mpi(int i)
1707 const MPITEST *test = &kMPITests[i];
1708 size_t mpi_len, mpi_len2;
1713 if (!TEST_ptr(bn = BN_new())
1714 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1716 mpi_len = BN_bn2mpi(bn, NULL);
1717 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1720 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1721 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1724 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1727 if (!TEST_BN_eq(bn, bn2)) {
1739 static int test_rand(void)
1744 if (!TEST_ptr(bn = BN_new()))
1747 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1748 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
1749 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
1750 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
1751 || !TEST_BN_eq_one(bn)
1752 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
1753 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
1754 || !TEST_BN_eq_one(bn)
1755 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
1756 || !TEST_BN_eq_word(bn, 3))
1765 static int test_negzero(void)
1767 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
1768 BIGNUM *numerator = NULL, *denominator = NULL;
1769 int consttime, st = 0;
1771 if (!TEST_ptr(a = BN_new())
1772 || !TEST_ptr(b = BN_new())
1773 || !TEST_ptr(c = BN_new())
1774 || !TEST_ptr(d = BN_new()))
1777 /* Test that BN_mul never gives negative zero. */
1778 if (!TEST_true(BN_set_word(a, 1)))
1780 BN_set_negative(a, 1);
1782 if (!TEST_true(BN_mul(c, a, b, ctx)))
1784 if (!TEST_BN_eq_zero(c)
1785 || !TEST_BN_ge_zero(c))
1788 for (consttime = 0; consttime < 2; consttime++) {
1789 if (!TEST_ptr(numerator = BN_new())
1790 || !TEST_ptr(denominator = BN_new()))
1793 BN_set_flags(numerator, BN_FLG_CONSTTIME);
1794 BN_set_flags(denominator, BN_FLG_CONSTTIME);
1796 /* Test that BN_div never gives negative zero in the quotient. */
1797 if (!TEST_true(BN_set_word(numerator, 1))
1798 || !TEST_true(BN_set_word(denominator, 2)))
1800 BN_set_negative(numerator, 1);
1801 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
1802 || !TEST_BN_eq_zero(a)
1803 || !TEST_BN_ge_zero(a))
1806 /* Test that BN_div never gives negative zero in the remainder. */
1807 if (!TEST_true(BN_set_word(denominator, 1))
1808 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
1809 || !TEST_BN_eq_zero(b)
1810 || !TEST_BN_ge_zero(b))
1813 BN_free(denominator);
1814 numerator = denominator = NULL;
1817 /* Test that BN_set_negative will not produce a negative zero. */
1819 BN_set_negative(a, 1);
1820 if (BN_is_negative(a))
1830 BN_free(denominator);
1834 static int test_badmod(void)
1836 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
1837 BN_MONT_CTX *mont = NULL;
1840 if (!TEST_ptr(a = BN_new())
1841 || !TEST_ptr(b = BN_new())
1842 || !TEST_ptr(zero = BN_new())
1843 || !TEST_ptr(mont = BN_MONT_CTX_new()))
1847 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
1851 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
1855 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
1859 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1864 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1869 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
1873 /* Some operations also may not be used with an even modulus. */
1874 if (!TEST_true(BN_set_word(b, 16)))
1877 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
1881 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1886 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1896 BN_MONT_CTX_free(mont);
1900 static int test_expmodzero(void)
1902 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
1905 if (!TEST_ptr(zero = BN_new())
1906 || !TEST_ptr(a = BN_new())
1907 || !TEST_ptr(r = BN_new()))
1911 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
1912 || !TEST_BN_eq_zero(r)
1913 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
1915 || !TEST_BN_eq_zero(r)
1916 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
1919 || !TEST_BN_eq_zero(r)
1920 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
1921 BN_value_one(), NULL, NULL))
1922 || !TEST_BN_eq_zero(r))
1933 static int test_smallprime(void)
1935 static const int kBits = 10;
1939 if (!TEST_ptr(r = BN_new())
1940 || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
1942 || !TEST_int_eq(BN_num_bits(r), kBits))
1951 static int test_3_is_prime(void)
1957 * For a long time, small primes were not considered prime when
1958 * do_trial_division was set.
1960 if (!TEST_ptr(r = BN_new())
1961 || !TEST_true(BN_set_word(r, 3))
1962 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
1963 0 /* do_trial_division */, NULL), 1)
1964 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
1965 1 /* do_trial_division */, NULL), 1))
1975 static int file_test_run(STANZA *s)
1977 static const FILETEST filetests[] = {
1979 {"LShift1", file_lshift1},
1980 {"LShift", file_lshift},
1981 {"RShift", file_rshift},
1982 {"Square", file_square},
1983 {"Product", file_product},
1984 {"Quotient", file_quotient},
1985 {"ModMul", file_modmul},
1986 {"ModExp", file_modexp},
1988 {"ModSqrt", file_modsqrt},
1990 int numtests = OSSL_NELEM(filetests);
1991 const FILETEST *tp = filetests;
1993 for ( ; --numtests >= 0; tp++) {
1994 if (findattr(s, tp->name) != NULL) {
1996 TEST_info("%s:%d: Failed %s test",
1997 s->test_file, s->start, tp->name);
2003 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2007 static int run_file_tests(int i)
2010 char *testfile = test_get_argument(i);
2013 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2015 if (!test_start_file(s, testfile)) {
2020 /* Read test file. */
2021 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2022 if (s->numpairs == 0)
2024 if (!file_test_run(s))
2027 test_clearstanza(s);
2037 int setup_tests(void)
2039 int n = test_get_argument_count();
2041 if (!TEST_ptr(ctx = BN_CTX_new()))
2046 ADD_TEST(test_div_recip);
2048 ADD_TEST(test_modexp_mont5);
2049 ADD_TEST(test_kronecker);
2050 ADD_TEST(test_rand);
2051 ADD_TEST(test_bn2padded);
2052 ADD_TEST(test_dec2bn);
2053 ADD_TEST(test_hex2bn);
2054 ADD_TEST(test_asc2bn);
2055 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2056 ADD_TEST(test_negzero);
2057 ADD_TEST(test_badmod);
2058 ADD_TEST(test_expmodzero);
2059 ADD_TEST(test_smallprime);
2060 #ifndef OPENSSL_NO_EC2M
2061 ADD_TEST(test_gf2m_add);
2062 ADD_TEST(test_gf2m_mod);
2063 ADD_TEST(test_gf2m_mul);
2064 ADD_TEST(test_gf2m_sqr);
2065 ADD_TEST(test_gf2m_modinv);
2066 ADD_TEST(test_gf2m_moddiv);
2067 ADD_TEST(test_gf2m_modexp);
2068 ADD_TEST(test_gf2m_modsqrt);
2069 ADD_TEST(test_gf2m_modsolvequad);
2071 ADD_TEST(test_3_is_prime);
2073 ADD_ALL_TESTS(run_file_tests, n);
2078 void cleanup_tests(void)