2 * Copyright 1995-2016 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"
41 * Things in boring, not in openssl. TODO we should add them.
43 #define HAVE_BN_PADDED 0
44 #define HAVE_BN_SQRT 0
46 typedef struct pair_st {
51 typedef struct stanza_st {
52 int curr; /* Current line in file */
53 int start; /* Line where test starts */
58 typedef struct filetest_st {
60 int (*func)(STANZA *s);
63 typedef struct mpitest_st {
69 static const int NUM0 = 100; /* number of tests */
70 static const int NUM1 = 50; /* additional tests for some functions */
75 * Polynomial coefficients used in GFM tests.
77 static int p0[] = { 163, 7, 6, 3, 0, -1 };
78 static int p1[] = { 193, 15, 0, -1 };
82 * Look for |key| in the stanza and return it or NULL if not found.
84 static const char *findattr(STANZA *s, const char *key)
89 for ( ; --i >= 0; pp++)
90 if (strcasecmp(pp->key, key) == 0)
96 * Parse BIGNUM, return number of bytes parsed.
98 static int parseBN(BIGNUM **out, const char *in)
101 return BN_hex2bn(out, in);
104 static int parsedecBN(BIGNUM **out, const char *in)
107 return BN_dec2bn(out, in);
110 static BIGNUM *getBN(STANZA *s, const char *attribute)
115 if ((hex = findattr(s, attribute)) == NULL) {
116 TEST_error("Can't find %s in test at line %d", attribute, s->start);
120 if (parseBN(&ret, hex) != (int)strlen(hex)) {
121 TEST_error("Could not decode '%s'", hex);
127 static int getint(STANZA *s, int *out, const char *attribute)
133 if (!TEST_ptr(ret = getBN(s, attribute))
134 || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
144 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
146 if (BN_cmp(expected, actual) == 0)
149 TEST_error("unexpected %s value", op);
150 TEST_BN_eq(expected, actual);
156 * Return a "random" flag for if a BN should be negated.
158 static int rand_neg(void)
160 static unsigned int neg = 0;
161 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
163 return sign[(neg++) % 8];
167 static int test_sub()
169 BIGNUM *a = NULL, *b = NULL, *c = NULL;
172 if (!TEST_ptr(a = BN_new())
173 || !TEST_ptr(b = BN_new())
174 || !TEST_ptr(c = BN_new()))
177 for (i = 0; i < NUM0 + NUM1; i++) {
179 BN_bntest_rand(a, 512, 0, 0);
181 if (!TEST_int_ne(BN_set_bit(a, i), 0))
185 BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
192 if (!TEST_BN_eq_zero(c))
204 static int test_div_recip()
206 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
207 BN_RECP_CTX *recp = NULL;
210 if (!TEST_ptr(a = BN_new())
211 || !TEST_ptr(b = BN_new())
212 || !TEST_ptr(c = BN_new())
213 || !TEST_ptr(d = BN_new())
214 || !TEST_ptr(e = BN_new())
215 || !TEST_ptr(recp = BN_RECP_CTX_new()))
218 for (i = 0; i < NUM0 + NUM1; i++) {
220 BN_bntest_rand(a, 400, 0, 0);
225 BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
228 BN_RECP_CTX_set(recp, b, ctx);
229 BN_div_recp(d, c, a, recp, ctx);
230 BN_mul(e, d, b, ctx);
233 if (!TEST_BN_eq_zero(d))
243 BN_RECP_CTX_free(recp);
248 static int test_mod()
250 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
253 if (!TEST_ptr(a = BN_new())
254 || !TEST_ptr(b = BN_new())
255 || !TEST_ptr(c = BN_new())
256 || !TEST_ptr(d = BN_new())
257 || !TEST_ptr(e = BN_new()))
260 BN_bntest_rand(a, 1024, 0, 0);
261 for (i = 0; i < NUM0; i++) {
262 BN_bntest_rand(b, 450 + i * 10, 0, 0);
265 BN_mod(c, a, b, ctx);
266 BN_div(d, e, a, b, ctx);
268 if (!TEST_BN_eq_zero(e))
281 static const char *bn1strings[] = {
282 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
283 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
284 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
285 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
286 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
287 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
288 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
289 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
290 "0000000000000000000000000000000000000000000000000000000000000000",
291 "0000000000000000000000000000000000000000000000000000000000000000",
292 "0000000000000000000000000000000000000000000000000000000000000000",
293 "0000000000000000000000000000000000000000000000000000000000000000",
294 "0000000000000000000000000000000000000000000000000000000000000000",
295 "0000000000000000000000000000000000000000000000000000000000000000",
296 "0000000000000000000000000000000000000000000000000000000000000000",
297 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
301 static const char *bn2strings[] = {
302 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
303 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
304 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
305 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
306 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
307 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
308 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
309 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
310 "0000000000000000000000000000000000000000000000000000000000000000",
311 "0000000000000000000000000000000000000000000000000000000000000000",
312 "0000000000000000000000000000000000000000000000000000000000000000",
313 "0000000000000000000000000000000000000000000000000000000000000000",
314 "0000000000000000000000000000000000000000000000000000000000000000",
315 "0000000000000000000000000000000000000000000000000000000000000000",
316 "0000000000000000000000000000000000000000000000000000000000000000",
317 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
321 static char *glue(const char *list[])
327 for (i = 0; list[i] != NULL; i++)
328 len += strlen(list[i]);
329 if (!TEST_ptr(p = save = OPENSSL_malloc(len + 1)))
331 for (i = 0; list[i] != NULL; i++)
332 p += strlen(strcpy(p, list[i]));
337 * Test constant-time modular exponentiation with 1024-bit inputs, which on
338 * x86_64 cause a different code branch to be taken.
340 static int test_modexp_mont5()
342 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
343 BIGNUM *b = NULL, *n = NULL, *c = NULL;
344 BN_MONT_CTX *mont = NULL;
348 if (!TEST_ptr(a = BN_new())
349 || !TEST_ptr(p = BN_new())
350 || !TEST_ptr(m = BN_new())
351 || !TEST_ptr(d = BN_new())
352 || !TEST_ptr(e = BN_new())
353 || !TEST_ptr(b = BN_new())
354 || !TEST_ptr(n = BN_new())
355 || !TEST_ptr(c = BN_new())
356 || !TEST_ptr(mont = BN_MONT_CTX_new()))
359 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
361 BN_bntest_rand(a, 1024, 0, 0);
363 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
365 if (!TEST_BN_eq_one(d))
368 /* Regression test for carry bug in mulx4x_mont */
370 "7878787878787878787878787878787878787878787878787878787878787878"
371 "7878787878787878787878787878787878787878787878787878787878787878"
372 "7878787878787878787878787878787878787878787878787878787878787878"
373 "7878787878787878787878787878787878787878787878787878787878787878");
375 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
376 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
377 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
378 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
380 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
381 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
382 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
383 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
384 BN_MONT_CTX_set(mont, n, ctx);
385 BN_mod_mul_montgomery(c, a, b, mont, ctx);
386 BN_mod_mul_montgomery(d, b, a, mont, ctx);
387 if (!TEST_BN_eq(c, d))
390 /* Regression test for carry bug in sqr[x]8x_mont */
391 bigstring = glue(bn1strings);
392 BN_hex2bn(&n, bigstring);
393 OPENSSL_free(bigstring);
394 bigstring = glue(bn2strings);
395 BN_hex2bn(&a, bigstring);
396 OPENSSL_free(bigstring);
399 BN_MONT_CTX_set(mont, n, ctx);
400 BN_mod_mul_montgomery(c, a, a, mont, ctx);
401 BN_mod_mul_montgomery(d, a, b, mont, ctx);
402 if (!TEST_BN_eq(c, d))
406 BN_bntest_rand(p, 1024, 0, 0);
408 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
409 || !TEST_BN_eq_zero(d))
413 * Craft an input whose Montgomery representation is 1, i.e., shorter
414 * than the modulus m, in order to test the const time precomputation
415 * scattering/gathering.
418 BN_MONT_CTX_set(mont, m, ctx);
419 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
420 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
421 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
422 || !TEST_BN_eq(a, d))
425 /* Finally, some regular test vectors. */
426 BN_bntest_rand(e, 1024, 0, 0);
427 if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
428 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
429 || !TEST_BN_eq(a, d))
435 BN_MONT_CTX_free(mont);
447 #ifndef OPENSSL_NO_EC2M
448 static int test_gf2m_add()
450 BIGNUM *a = NULL, *b = NULL, *c = NULL;
453 if (!TEST_ptr(a = BN_new())
454 || !TEST_ptr(b = BN_new())
455 || !TEST_ptr(c = BN_new()))
458 for (i = 0; i < NUM0; i++) {
459 BN_rand(a, 512, 0, 0);
460 BN_copy(b, BN_value_one());
463 BN_GF2m_add(c, a, b);
464 /* Test that two added values have the correct parity. */
465 if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
466 || (!BN_is_odd(a) && !BN_is_odd(c))))
468 BN_GF2m_add(c, c, c);
469 /* Test that c + c = 0. */
470 if (!TEST_BN_eq_zero(c))
481 static int test_gf2m_mod()
483 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
486 if (!TEST_ptr(a = BN_new())
487 || !TEST_ptr(b[0] = BN_new())
488 || !TEST_ptr(b[1] = BN_new())
489 || !TEST_ptr(c = BN_new())
490 || !TEST_ptr(d = BN_new())
491 || !TEST_ptr(e = BN_new()))
494 BN_GF2m_arr2poly(p0, b[0]);
495 BN_GF2m_arr2poly(p1, b[1]);
497 for (i = 0; i < NUM0; i++) {
498 BN_bntest_rand(a, 1024, 0, 0);
499 for (j = 0; j < 2; j++) {
500 BN_GF2m_mod(c, a, b[j]);
501 BN_GF2m_add(d, a, c);
502 BN_GF2m_mod(e, d, b[j]);
503 /* Test that a + (a mod p) mod p == 0. */
504 if (!TEST_BN_eq_zero(e))
519 static int test_gf2m_mul()
521 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
522 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
525 if (!TEST_ptr(a = BN_new())
526 || !TEST_ptr(b[0] = BN_new())
527 || !TEST_ptr(b[1] = BN_new())
528 || !TEST_ptr(c = BN_new())
529 || !TEST_ptr(d = BN_new())
530 || !TEST_ptr(e = BN_new())
531 || !TEST_ptr(f = BN_new())
532 || !TEST_ptr(g = BN_new())
533 || !TEST_ptr(h = BN_new()))
536 BN_GF2m_arr2poly(p0, b[0]);
537 BN_GF2m_arr2poly(p1, b[1]);
539 for (i = 0; i < NUM0; i++) {
540 BN_bntest_rand(a, 1024, 0, 0);
541 BN_bntest_rand(c, 1024, 0, 0);
542 BN_bntest_rand(d, 1024, 0, 0);
543 for (j = 0; j < 2; j++) {
544 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
545 BN_GF2m_add(f, a, d);
546 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
547 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
548 BN_GF2m_add(f, e, g);
549 BN_GF2m_add(f, f, h);
550 /* Test that (a+d)*c = a*c + d*c. */
551 if (!TEST_BN_eq_zero(f))
570 static int test_gf2m_sqr()
572 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
575 if (!TEST_ptr(a = BN_new())
576 || !TEST_ptr(b[0] = BN_new())
577 || !TEST_ptr(b[1] = BN_new())
578 || !TEST_ptr(c = BN_new())
579 || !TEST_ptr(d = BN_new()))
582 BN_GF2m_arr2poly(p0, b[0]);
583 BN_GF2m_arr2poly(p1, b[1]);
585 for (i = 0; i < NUM0; i++) {
586 BN_bntest_rand(a, 1024, 0, 0);
587 for (j = 0; j < 2; j++) {
588 BN_GF2m_mod_sqr(c, a, b[j], ctx);
590 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
591 BN_GF2m_add(d, c, d);
592 /* Test that a*a = a^2. */
593 if (!TEST_BN_eq_zero(d))
607 static int test_gf2m_modinv()
609 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
612 if (!TEST_ptr(a = BN_new())
613 || !TEST_ptr(b[0] = BN_new())
614 || !TEST_ptr(b[1] = BN_new())
615 || !TEST_ptr(c = BN_new())
616 || !TEST_ptr(d = BN_new()))
619 BN_GF2m_arr2poly(p0, b[0]);
620 BN_GF2m_arr2poly(p1, b[1]);
622 for (i = 0; i < NUM0; i++) {
623 BN_bntest_rand(a, 512, 0, 0);
624 for (j = 0; j < 2; j++) {
625 BN_GF2m_mod_inv(c, a, b[j], ctx);
626 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
627 /* Test that ((1/a)*a) = 1. */
628 if (!TEST_BN_eq_one(d))
642 static int test_gf2m_moddiv()
644 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
645 BIGNUM *e = NULL, *f = NULL;
648 if (!TEST_ptr(a = BN_new())
649 || !TEST_ptr(b[0] = BN_new())
650 || !TEST_ptr(b[1] = BN_new())
651 || !TEST_ptr(c = BN_new())
652 || !TEST_ptr(d = BN_new())
653 || !TEST_ptr(e = BN_new())
654 || !TEST_ptr(f = BN_new()))
657 BN_GF2m_arr2poly(p0, b[0]);
658 BN_GF2m_arr2poly(p1, b[1]);
660 for (i = 0; i < NUM0; i++) {
661 BN_bntest_rand(a, 512, 0, 0);
662 BN_bntest_rand(c, 512, 0, 0);
663 for (j = 0; j < 2; j++) {
664 BN_GF2m_mod_div(d, a, c, b[j], ctx);
665 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
666 BN_GF2m_mod_div(f, a, e, b[j], ctx);
667 /* Test that ((a/c)*c)/a = 1. */
668 if (!TEST_BN_eq_one(f))
684 static int test_gf2m_modexp()
686 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
687 BIGNUM *e = NULL, *f = NULL;
690 if (!TEST_ptr(a = BN_new())
691 || !TEST_ptr(b[0] = BN_new())
692 || !TEST_ptr(b[1] = BN_new())
693 || !TEST_ptr(c = BN_new())
694 || !TEST_ptr(d = BN_new())
695 || !TEST_ptr(e = BN_new())
696 || !TEST_ptr(f = BN_new()))
699 BN_GF2m_arr2poly(p0, b[0]);
700 BN_GF2m_arr2poly(p1, b[1]);
702 for (i = 0; i < NUM0; i++) {
703 BN_bntest_rand(a, 512, 0, 0);
704 BN_bntest_rand(c, 512, 0, 0);
705 BN_bntest_rand(d, 512, 0, 0);
706 for (j = 0; j < 2; j++) {
707 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
708 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
709 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
711 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
712 BN_GF2m_add(f, e, f);
713 /* Test that a^(c+d)=a^c*a^d. */
714 if (!TEST_BN_eq_zero(f))
730 static int test_gf2m_modsqrt()
732 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
733 BIGNUM *e = NULL, *f = NULL;
736 if (!TEST_ptr(a = BN_new())
737 || !TEST_ptr(b[0] = BN_new())
738 || !TEST_ptr(b[1] = BN_new())
739 || !TEST_ptr(c = BN_new())
740 || !TEST_ptr(d = BN_new())
741 || !TEST_ptr(e = BN_new())
742 || !TEST_ptr(f = BN_new()))
745 BN_GF2m_arr2poly(p0, b[0]);
746 BN_GF2m_arr2poly(p1, b[1]);
748 for (i = 0; i < NUM0; i++) {
749 BN_bntest_rand(a, 512, 0, 0);
750 for (j = 0; j < 2; j++) {
751 BN_GF2m_mod(c, a, b[j]);
752 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
753 BN_GF2m_mod_sqr(e, d, b[j], ctx);
754 BN_GF2m_add(f, c, e);
755 /* Test that d^2 = a, where d = sqrt(a). */
756 if (!TEST_BN_eq_zero(f))
772 static int test_gf2m_modsolvequad()
774 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
776 int i, j, s = 0, t, st = 0;
778 if (!TEST_ptr(a = BN_new())
779 || !TEST_ptr(b[0] = BN_new())
780 || !TEST_ptr(b[1] = BN_new())
781 || !TEST_ptr(c = BN_new())
782 || !TEST_ptr(d = BN_new())
783 || !TEST_ptr(e = BN_new()))
786 BN_GF2m_arr2poly(p0, b[0]);
787 BN_GF2m_arr2poly(p1, b[1]);
789 for (i = 0; i < NUM0; i++) {
790 BN_bntest_rand(a, 512, 0, 0);
791 for (j = 0; j < 2; j++) {
792 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
795 BN_GF2m_mod_sqr(d, c, b[j], ctx);
796 BN_GF2m_add(d, c, d);
797 BN_GF2m_mod(e, a, b[j]);
798 BN_GF2m_add(e, e, d);
800 * Test that solution of quadratic c satisfies c^2 + c = a.
802 if (!TEST_BN_eq_zero(e))
807 if (!TEST_int_ge(s, 0)) {
808 TEST_info("%d tests found no roots; probably an error", NUM0);
823 static int test_kronecker()
825 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
826 int i, legendre, kronecker, st = 0;
828 if (!TEST_ptr(a = BN_new())
829 || !TEST_ptr(b = BN_new())
830 || !TEST_ptr(r = BN_new())
831 || !TEST_ptr(t = BN_new()))
835 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
836 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
837 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
838 * generate a random prime b and compare these values for a number of
839 * random a's. (That is, we run the Solovay-Strassen primality test to
840 * confirm that b is prime, except that we don't want to test whether b
841 * is prime but whether BN_kronecker works.)
844 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
848 for (i = 0; i < NUM0; i++) {
849 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
853 /* t := (|b|-1)/2 (note that b is odd) */
854 if (!TEST_true(BN_copy(t, b)))
857 if (!TEST_true(BN_sub_word(t, 1)))
859 if (!TEST_true(BN_rshift1(t, t)))
864 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
868 if (BN_is_word(r, 1))
870 else if (BN_is_zero(r))
873 if (!TEST_true(BN_add_word(r, 1)))
875 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
876 TEST_info("Legendre symbol computation failed");
882 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
884 /* we actually need BN_kronecker(a, |b|) */
885 if (a->neg && b->neg)
886 kronecker = -kronecker;
888 if (!TEST_int_eq(legendre, kronecker))
901 static int file_sum(STANZA *s)
903 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
907 if (!TEST_ptr(a = getBN(s, "A"))
908 || !TEST_ptr(b = getBN(s, "B"))
909 || !TEST_ptr(sum = getBN(s, "Sum"))
910 || !TEST_ptr(ret = BN_new()))
913 if (!TEST_true(BN_add(ret, a, b))
914 || !equalBN("A + B", sum, ret)
915 || !TEST_true(BN_sub(ret, sum, a))
916 || !equalBN("Sum - A", b, ret)
917 || !TEST_true(BN_sub(ret, sum, b))
918 || !equalBN("Sum - B", a, ret))
922 * Test that the functions work when |r| and |a| point to the same BIGNUM,
923 * or when |r| and |b| point to the same BIGNUM.
924 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
926 if (!TEST_true(BN_copy(ret, a))
927 || !TEST_true(BN_add(ret, ret, b))
928 || !equalBN("A + B (r is a)", sum, ret)
929 || !TEST_true(BN_copy(ret, b))
930 || !TEST_true(BN_add(ret, a, ret))
931 || !equalBN("A + B (r is b)", sum, ret)
932 || !TEST_true(BN_copy(ret, sum))
933 || !TEST_true(BN_sub(ret, ret, a))
934 || !equalBN("Sum - A (r is a)", b, ret)
935 || !TEST_true(BN_copy(ret, a))
936 || !TEST_true(BN_sub(ret, sum, ret))
937 || !equalBN("Sum - A (r is b)", b, ret)
938 || !TEST_true(BN_copy(ret, sum))
939 || !TEST_true(BN_sub(ret, ret, b))
940 || !equalBN("Sum - B (r is a)", a, ret)
941 || !TEST_true(BN_copy(ret, b))
942 || !TEST_true(BN_sub(ret, sum, ret))
943 || !equalBN("Sum - B (r is b)", a, ret))
947 * Test BN_uadd() and BN_usub() with the prerequisites they are
948 * documented as having. Note that these functions are frequently used
949 * when the prerequisites don't hold. In those cases, they are supposed
950 * to work as if the prerequisite hold, but we don't test that yet.
953 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
954 if (!TEST_true(BN_uadd(ret, a, b))
955 || !equalBN("A +u B", sum, ret)
956 || !TEST_true(BN_usub(ret, sum, a))
957 || !equalBN("Sum -u A", b, ret)
958 || !TEST_true(BN_usub(ret, sum, b))
959 || !equalBN("Sum -u B", a, ret))
962 * Test that the functions work when |r| and |a| point to the same
963 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
964 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
966 if (!TEST_true(BN_copy(ret, a))
967 || !TEST_true(BN_uadd(ret, ret, b))
968 || !equalBN("A +u B (r is a)", sum, ret)
969 || !TEST_true(BN_copy(ret, b))
970 || !TEST_true(BN_uadd(ret, a, ret))
971 || !equalBN("A +u B (r is b)", sum, ret)
972 || !TEST_true(BN_copy(ret, sum))
973 || !TEST_true(BN_usub(ret, ret, a))
974 || !equalBN("Sum -u A (r is a)", b, ret)
975 || !TEST_true(BN_copy(ret, a))
976 || !TEST_true(BN_usub(ret, sum, ret))
977 || !equalBN("Sum -u A (r is b)", b, ret)
978 || !TEST_true(BN_copy(ret, sum))
979 || !TEST_true(BN_usub(ret, ret, b))
980 || !equalBN("Sum -u B (r is a)", a, ret)
981 || !TEST_true(BN_copy(ret, b))
982 || !TEST_true(BN_usub(ret, sum, ret))
983 || !equalBN("Sum -u B (r is b)", a, ret))
988 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
990 b_word = BN_get_word(b);
991 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
992 if (!TEST_true(BN_copy(ret, a))
993 || !TEST_true(BN_add_word(ret, b_word))
994 || !equalBN("A + B (word)", sum, ret)
995 || !TEST_true(BN_copy(ret, sum))
996 || !TEST_true(BN_sub_word(ret, b_word))
997 || !equalBN("Sum - B (word)", a, ret))
1010 static int file_lshift1(STANZA *s)
1012 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1013 BIGNUM *two = NULL, *remainder = NULL;
1016 if (!TEST_ptr(a = getBN(s, "A"))
1017 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1018 || !TEST_ptr(zero = BN_new())
1019 || !TEST_ptr(ret = BN_new())
1020 || !TEST_ptr(two = BN_new())
1021 || !TEST_ptr(remainder = BN_new()))
1026 if (!TEST_true(BN_set_word(two, 2))
1027 || !TEST_true(BN_add(ret, a, a))
1028 || !equalBN("A + A", lshift1, ret)
1029 || !TEST_true(BN_mul(ret, a, two, ctx))
1030 || !equalBN("A * 2", lshift1, ret)
1031 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1032 || !equalBN("LShift1 / 2", a, ret)
1033 || !equalBN("LShift1 % 2", zero, remainder)
1034 || !TEST_true(BN_lshift1(ret, a))
1035 || !equalBN("A << 1", lshift1, ret)
1036 || !TEST_true(BN_rshift1(ret, lshift1))
1037 || !equalBN("LShift >> 1", a, ret)
1038 || !TEST_true(BN_rshift1(ret, lshift1))
1039 || !equalBN("LShift >> 1", a, ret))
1042 /* Set the LSB to 1 and test rshift1 again. */
1043 if (!TEST_true(BN_set_bit(lshift1, 0))
1044 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1045 || !equalBN("(LShift1 | 1) / 2", a, ret)
1046 || !TEST_true(BN_rshift1(ret, lshift1))
1047 || !equalBN("(LShift | 1) >> 1", a, ret))
1062 static int file_lshift(STANZA *s)
1064 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1067 if (!TEST_ptr(a = getBN(s, "A"))
1068 || !TEST_ptr(lshift = getBN(s, "LShift"))
1069 || !TEST_ptr(ret = BN_new()))
1071 if (!TEST_true(BN_lshift(ret, a, n))
1072 || !equalBN("A << N", lshift, ret)
1073 || !TEST_true(BN_rshift(ret, lshift, n))
1074 || !equalBN("A >> N", a, ret))
1085 static int file_rshift(STANZA *s)
1087 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1090 if (!TEST_ptr(a = getBN(s, "A"))
1091 || !TEST_ptr(rshift = getBN(s, "RShift"))
1092 || !TEST_ptr(ret = BN_new())
1093 || !getint(s, &n, "N"))
1096 if (!TEST_true(BN_rshift(ret, a, n))
1097 || !equalBN("A >> N", rshift, ret))
1100 /* If N == 1, try with rshift1 as well */
1102 if (!TEST_true(BN_rshift1(ret, a))
1103 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1115 static int file_square(STANZA *s)
1117 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1118 BIGNUM *remainder = NULL, *tmp = NULL;
1121 if (!TEST_ptr(a = getBN(s, "A"))
1122 || !TEST_ptr(square = getBN(s, "Square"))
1123 || !TEST_ptr(zero = BN_new())
1124 || !TEST_ptr(ret = BN_new())
1125 || !TEST_ptr(remainder = BN_new()))
1129 if (!TEST_true(BN_sqr(ret, a, ctx))
1130 || !equalBN("A^2", square, ret)
1131 || !TEST_true(BN_mul(ret, a, a, ctx))
1132 || !equalBN("A * A", square, ret)
1133 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1134 || !equalBN("Square / A", a, ret)
1135 || !equalBN("Square % A", zero, remainder))
1139 BN_set_negative(a, 0);
1140 if (!TEST_true(BN_sqrt(ret, square, ctx))
1141 || !equalBN("sqrt(Square)", a, ret))
1144 /* BN_sqrt should fail on non-squares and negative numbers. */
1145 if (!TEST_BN_eq_zero(square)) {
1146 if (!TEST_ptr(tmp = BN_new())
1147 || !TEST_true(BN_copy(tmp, square)))
1149 BN_set_negative(tmp, 1);
1151 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1155 BN_set_negative(tmp, 0);
1156 if (BN_add(tmp, tmp, BN_value_one()))
1158 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1175 static int file_product(STANZA *s)
1177 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1178 BIGNUM *remainder = NULL, *zero = NULL;
1181 if (!TEST_ptr(a = getBN(s, "A"))
1182 || !TEST_ptr(b = getBN(s, "B"))
1183 || !TEST_ptr(product = getBN(s, "Product"))
1184 || !TEST_ptr(ret = BN_new())
1185 || !TEST_ptr(remainder = BN_new())
1186 || !TEST_ptr(zero = BN_new()))
1191 if (!TEST_true(BN_mul(ret, a, b, ctx))
1192 || !equalBN("A * B", product, ret)
1193 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1194 || !equalBN("Product / A", b, ret)
1195 || !equalBN("Product % A", zero, remainder)
1196 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1197 || !equalBN("Product / B", a, ret)
1198 || !equalBN("Product % B", zero, remainder))
1212 static int file_quotient(STANZA *s)
1214 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1215 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1216 BN_ULONG b_word, ret_word;
1219 if (!TEST_ptr(a = getBN(s, "A"))
1220 || !TEST_ptr(b = getBN(s, "B"))
1221 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1222 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1223 || !TEST_ptr(ret = BN_new())
1224 || !TEST_ptr(ret2 = BN_new())
1225 || !TEST_ptr(nnmod = BN_new()))
1228 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1229 || !equalBN("A / B", quotient, ret)
1230 || !equalBN("A % B", remainder, ret2)
1231 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1232 || !TEST_true(BN_add(ret, ret, remainder))
1233 || !equalBN("Quotient * B + Remainder", a, ret))
1237 * Test with BN_mod_word() and BN_div_word() if the divisor is
1240 b_word = BN_get_word(b);
1241 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1242 BN_ULONG remainder_word = BN_get_word(remainder);
1244 assert(remainder_word != (BN_ULONG)-1);
1245 if (!TEST_ptr(BN_copy(ret, a)))
1247 ret_word = BN_div_word(ret, 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");
1258 if (!equalBN ("A / B (word)", quotient, ret))
1261 ret_word = BN_mod_word(a, b_word);
1262 if (ret_word != remainder_word) {
1265 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1266 ret_word, remainder_word);
1268 TEST_error("Got A %% B (word) mismatch");
1274 /* Test BN_nnmod. */
1275 if (!BN_is_negative(b)) {
1276 if (!TEST_true(BN_copy(nnmod, remainder))
1277 || (BN_is_negative(nnmod)
1278 && !TEST_true(BN_add(nnmod, nnmod, b)))
1279 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1280 || !equalBN("A % B (non-negative)", nnmod, ret))
1296 static int file_modmul(STANZA *s)
1298 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1301 if (!TEST_ptr(a = getBN(s, "A"))
1302 || !TEST_ptr(b = getBN(s, "B"))
1303 || !TEST_ptr(m = getBN(s, "M"))
1304 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1305 || !TEST_ptr(ret = BN_new()))
1308 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1309 || !equalBN("A * B (mod M)", mod_mul, ret))
1313 /* Reduce |a| and |b| and test the Montgomery version. */
1314 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1315 BIGNUM *a_tmp = BN_new();
1316 BIGNUM *b_tmp = BN_new();
1318 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1319 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1320 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1321 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1322 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1323 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1324 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1326 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1327 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1331 BN_MONT_CTX_free(mont);
1348 static int file_modexp(STANZA *s)
1350 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1351 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1354 if (!TEST_ptr(a = getBN(s, "A"))
1355 || !TEST_ptr(e = getBN(s, "E"))
1356 || !TEST_ptr(m = getBN(s, "M"))
1357 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1358 || !TEST_ptr(ret = BN_new())
1359 || !TEST_ptr(d = BN_new()))
1362 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1363 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1367 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1368 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1369 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1371 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1375 /* Regression test for carry propagation bug in sqr8x_reduction */
1376 BN_hex2bn(&a, "050505050505");
1377 BN_hex2bn(&b, "02");
1379 "4141414141414141414141274141414141414141414141414141414141414141"
1380 "4141414141414141414141414141414141414141414141414141414141414141"
1381 "4141414141414141414141800000000000000000000000000000000000000000"
1382 "0000000000000000000000000000000000000000000000000000000000000000"
1383 "0000000000000000000000000000000000000000000000000000000000000000"
1384 "0000000000000000000000000000000000000000000000000000000001");
1385 BN_mod_exp(d, a, b, c, ctx);
1386 BN_mul(e, a, a, ctx);
1387 if (!TEST_BN_eq(d, e))
1403 static int file_exp(STANZA *s)
1405 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1408 if (!TEST_ptr(a = getBN(s, "A"))
1409 || !TEST_ptr(e = getBN(s, "E"))
1410 || !TEST_ptr(exp = getBN(s, "Exp"))
1411 || !TEST_ptr(ret = BN_new()))
1414 if (!TEST_true(BN_exp(ret, a, e, ctx))
1415 || !equalBN("A ^ E", exp, ret))
1427 static int file_modsqrt(STANZA *s)
1429 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1432 if (!TEST_ptr(a = getBN(s, "A"))
1433 || !TEST_ptr(p = getBN(s, "P"))
1434 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1435 || !TEST_ptr(ret = BN_new())
1436 || !TEST_ptr(ret2 = BN_new()))
1439 /* There are two possible answers. */
1440 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1441 || !TEST_true(BN_sub(ret2, p, ret)))
1444 /* The first condition should NOT be a test. */
1445 if (BN_cmp(ret2, mod_sqrt) != 0
1446 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1459 static int test_bn2padded()
1462 uint8_t zeros[256], out[256], reference[128];
1463 BIGNUM *n = BN_new();
1466 /* Test edge case at 0. */
1469 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1471 memset(out, -1, sizeof(out));
1472 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1474 memset(zeros, 0, sizeof(zeros));
1475 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1478 /* Test a random numbers at various byte lengths. */
1479 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1480 #define TOP_BIT_ON 0
1481 #define BOTTOM_BIT_NOTOUCH 0
1482 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1484 if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1485 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1487 /* Empty buffer should fail. */
1488 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1490 /* One byte short should fail. */
1491 if (BN_bn2bin_padded(out, bytes - 1, n))
1493 /* Exactly right size should encode. */
1494 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1495 || TEST_mem_eq(out, bytes, reference, bytes))
1497 /* Pad up one byte extra. */
1498 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1499 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1500 || !TEST_mem_eq(out, 1, zeros, 1))
1502 /* Pad up to 256. */
1503 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1504 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1506 || !TEST_mem_eq(out, sizseof(out) - bytes,
1507 zeros, sizeof(out) - bytes))
1520 static int test_dec2bn()
1525 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1526 || !TEST_BN_eq_word(bn, 0)
1527 || !TEST_BN_eq_zero(bn)
1528 || !TEST_BN_le_zero(bn)
1529 || !TEST_BN_ge_zero(bn)
1530 || !TEST_BN_even(bn))
1535 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1536 || !TEST_BN_eq_word(bn, 256)
1537 || !TEST_BN_ge_zero(bn)
1538 || !TEST_BN_gt_zero(bn)
1539 || !TEST_BN_ne_zero(bn)
1540 || !TEST_BN_even(bn))
1545 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1546 || !TEST_BN_abs_eq_word(bn, 42)
1547 || !TEST_BN_lt_zero(bn)
1548 || !TEST_BN_le_zero(bn)
1549 || !TEST_BN_ne_zero(bn)
1550 || !TEST_BN_even(bn))
1555 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1556 || !TEST_BN_eq_word(bn, 1)
1557 || !TEST_BN_ne_zero(bn)
1558 || !TEST_BN_gt_zero(bn)
1559 || !TEST_BN_ge_zero(bn)
1560 || !TEST_BN_eq_one(bn)
1561 || !TEST_BN_odd(bn))
1566 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1567 || !TEST_BN_eq_zero(bn)
1568 || !TEST_BN_ge_zero(bn)
1569 || !TEST_BN_le_zero(bn)
1570 || !TEST_BN_even(bn))
1575 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1576 || !TEST_BN_abs_eq_word(bn, 42)
1577 || !TEST_BN_ge_zero(bn)
1578 || !TEST_BN_gt_zero(bn)
1579 || !TEST_BN_ne_zero(bn)
1580 || !TEST_BN_even(bn))
1589 static int test_hex2bn()
1594 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1595 || !TEST_BN_eq_zero(bn)
1596 || !TEST_BN_ge_zero(bn)
1597 || !TEST_BN_even(bn))
1602 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1603 || !TEST_BN_eq_word(bn, 0x256)
1604 || !TEST_BN_ge_zero(bn)
1605 || !TEST_BN_gt_zero(bn)
1606 || !TEST_BN_ne_zero(bn)
1607 || !TEST_BN_even(bn))
1612 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1613 || !TEST_BN_abs_eq_word(bn, 0x42)
1614 || !TEST_BN_lt_zero(bn)
1615 || !TEST_BN_le_zero(bn)
1616 || !TEST_BN_ne_zero(bn)
1617 || !TEST_BN_even(bn))
1622 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1623 || !TEST_BN_eq_word(bn, 0xCB)
1624 || !TEST_BN_ge_zero(bn)
1625 || !TEST_BN_gt_zero(bn)
1626 || !TEST_BN_ne_zero(bn)
1627 || !TEST_BN_odd(bn))
1632 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1633 || !TEST_BN_eq_zero(bn)
1634 || !TEST_BN_ge_zero(bn)
1635 || !TEST_BN_le_zero(bn)
1636 || !TEST_BN_even(bn))
1641 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1642 || !TEST_BN_eq_word(bn, 0xabc)
1643 || !TEST_BN_ge_zero(bn)
1644 || !TEST_BN_gt_zero(bn)
1645 || !TEST_BN_ne_zero(bn)
1646 || !TEST_BN_even(bn))
1655 static int test_asc2bn()
1660 if (!TEST_ptr(bn = BN_new()))
1663 if (!TEST_true(BN_asc2bn(&bn, "0"))
1664 || !TEST_BN_eq_zero(bn)
1665 || !TEST_BN_ge_zero(bn))
1668 if (!TEST_true(BN_asc2bn(&bn, "256"))
1669 || !TEST_BN_eq_word(bn, 256)
1670 || !TEST_BN_ge_zero(bn))
1673 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1674 || !TEST_BN_abs_eq_word(bn, 42)
1675 || !TEST_BN_lt_zero(bn))
1678 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1679 || !TEST_BN_eq_word(bn, 0x1234)
1680 || !TEST_BN_ge_zero(bn))
1683 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1684 || !TEST_BN_eq_word(bn, 0x1234)
1685 || !TEST_BN_ge_zero(bn))
1688 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1689 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1690 || !TEST_BN_lt_zero(bn))
1693 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1694 || !TEST_BN_eq_zero(bn)
1695 || !TEST_BN_ge_zero(bn))
1698 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1699 || !TEST_BN_eq_word(bn, 123)
1700 || !TEST_BN_ge_zero(bn))
1709 static const MPITEST kMPITests[] = {
1710 {"0", "\x00\x00\x00\x00", 4},
1711 {"1", "\x00\x00\x00\x01\x01", 5},
1712 {"-1", "\x00\x00\x00\x01\x81", 5},
1713 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1714 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1715 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1718 static int test_mpi(int i)
1721 const MPITEST *test = &kMPITests[i];
1722 size_t mpi_len, mpi_len2;
1727 if (!TEST_ptr(bn = BN_new())
1728 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1730 mpi_len = BN_bn2mpi(bn, NULL);
1731 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1734 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1735 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1738 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1741 if (!TEST_BN_eq(bn, bn2)) {
1753 static int test_rand()
1758 if (!TEST_ptr(bn = BN_new()))
1761 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1762 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
1763 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
1764 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
1765 || !TEST_BN_eq_one(bn)
1766 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
1767 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
1768 || !TEST_BN_eq_one(bn)
1769 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
1770 || !TEST_BN_eq_word(bn, 3))
1779 static int test_negzero()
1781 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
1782 BIGNUM *numerator = NULL, *denominator = NULL;
1783 int consttime, st = 0;
1785 if (!TEST_ptr(a = BN_new())
1786 || !TEST_ptr(b = BN_new())
1787 || !TEST_ptr(c = BN_new())
1788 || !TEST_ptr(d = BN_new()))
1791 /* Test that BN_mul never gives negative zero. */
1792 if (!TEST_true(BN_set_word(a, 1)))
1794 BN_set_negative(a, 1);
1796 if (!TEST_true(BN_mul(c, a, b, ctx)))
1798 if (!TEST_BN_eq_zero(c)
1799 || !TEST_BN_ge_zero(c))
1802 for (consttime = 0; consttime < 2; consttime++) {
1803 if (!TEST_ptr(numerator = BN_new())
1804 || !TEST_ptr(denominator = BN_new()))
1807 BN_set_flags(numerator, BN_FLG_CONSTTIME);
1808 BN_set_flags(denominator, BN_FLG_CONSTTIME);
1810 /* Test that BN_div never gives negative zero in the quotient. */
1811 if (!TEST_true(BN_set_word(numerator, 1))
1812 || !TEST_true(BN_set_word(denominator, 2)))
1814 BN_set_negative(numerator, 1);
1815 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
1816 || !TEST_BN_eq_zero(a)
1817 || !TEST_BN_ge_zero(a))
1820 /* Test that BN_div never gives negative zero in the remainder. */
1821 if (!TEST_true(BN_set_word(denominator, 1))
1822 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
1823 || !TEST_BN_eq_zero(b)
1824 || !TEST_BN_ge_zero(b))
1827 BN_free(denominator);
1828 numerator = denominator = NULL;
1831 /* Test that BN_set_negative will not produce a negative zero. */
1833 BN_set_negative(a, 1);
1834 if (BN_is_negative(a))
1844 BN_free(denominator);
1848 static int test_badmod()
1850 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
1851 BN_MONT_CTX *mont = NULL;
1854 if (!TEST_ptr(a = BN_new())
1855 || !TEST_ptr(b = BN_new())
1856 || !TEST_ptr(zero = BN_new())
1857 || !TEST_ptr(mont = BN_MONT_CTX_new()))
1861 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
1865 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
1869 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
1873 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1878 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1883 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
1887 /* Some operations also may not be used with an even modulus. */
1888 if (!TEST_true(BN_set_word(b, 16)))
1891 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
1895 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1900 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1910 BN_MONT_CTX_free(mont);
1914 static int test_expmodzero()
1916 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
1919 if (!TEST_ptr(zero = BN_new())
1920 || !TEST_ptr(a = BN_new())
1921 || !TEST_ptr(r = BN_new()))
1925 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
1926 || !TEST_BN_eq_zero(r)
1927 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
1929 || !TEST_BN_eq_zero(r)
1930 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
1933 || !TEST_BN_eq_zero(r)
1934 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
1935 BN_value_one(), NULL, NULL))
1936 || !TEST_BN_eq_zero(r))
1947 static int test_smallprime()
1949 static const int kBits = 10;
1953 if (!TEST_ptr(r = BN_new())
1954 || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
1956 || !TEST_int_eq(BN_num_bits(r), kBits))
1965 static int test_3_is_prime()
1971 * For a long time, small primes were not considered prime when
1972 * do_trial_division was set.
1974 if (!TEST_ptr(r = BN_new())
1975 || !TEST_true(BN_set_word(r, 3))
1976 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
1977 0 /* do_trial_division */, NULL), 1)
1978 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
1979 1 /* do_trial_division */, NULL), 1))
1990 /* Delete leading and trailing spaces from a string */
1991 static char *strip_spaces(char *p)
1995 /* Skip over leading spaces */
1996 while (*p && isspace(*p))
2001 for (q = p + strlen(p) - 1; q != p && isspace(*q); )
2003 return *p ? p : NULL;
2007 * Read next test stanza; return 1 if found, 0 on EOF or error.
2009 static int readstanza(STANZA *s)
2011 PAIR *pp = s->pairs;
2012 char *p, *equals, *key, *value;
2015 while (BIO_gets(fp, buff, sizeof(buff))) {
2017 if (!TEST_ptr(p = strchr(buff, '\n'))) {
2018 TEST_info("Line %d too long", s->curr);
2023 /* Blank line marks end of tests. */
2024 if (buff[0] == '\0')
2027 /* Lines starting with a pound sign are ignored. */
2031 if (!TEST_ptr(equals = strchr(buff, '='))) {
2032 TEST_info("Missing = at line %d\n", s->curr);
2036 if (s->numpairs == 0)
2039 if (!TEST_ptr(key = strip_spaces(buff))
2040 || !TEST_ptr(value = strip_spaces(equals))
2041 || !TEST_int_lt(s->numpairs++, MAXPAIRS)
2042 || !TEST_ptr(pp->key = OPENSSL_strdup(key))
2043 || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
2048 /* If we read anything, return ok. */
2052 static void clearstanza(STANZA *s)
2054 PAIR *pp = s->pairs;
2055 int i = s->numpairs;
2056 int start = s->start;
2059 for ( ; --i >= 0; pp++) {
2060 OPENSSL_free(pp->key);
2061 OPENSSL_free(pp->value);
2063 memset(s, 0, sizeof(*s));
2068 static int file_test_run(STANZA *s)
2070 static const FILETEST filetests[] = {
2072 {"LShift1", file_lshift1},
2073 {"LShift", file_lshift},
2074 {"RShift", file_rshift},
2075 {"Square", file_square},
2076 {"Product", file_product},
2077 {"Quotient", file_quotient},
2078 {"ModMul", file_modmul},
2079 {"ModExp", file_modexp},
2081 {"ModSqrt", file_modsqrt},
2083 int numtests = OSSL_NELEM(filetests);
2084 const FILETEST *tp = filetests;
2086 for ( ; --numtests >= 0; tp++) {
2087 if (findattr(s, tp->name) != NULL) {
2089 TEST_info("Failed %s test at %d", tp->name, s->start);
2095 TEST_info("Unknown test at %d", s->start);
2099 static char * const *testfiles;
2101 static int run_file_tests(int i)
2106 if (!TEST_ptr(fp = BIO_new_file(testfiles[i], "rb")))
2109 /* Read test file. */
2110 set_test_title(testfiles[i]);
2111 memset(&s, 0, sizeof(s));
2112 while (!BIO_eof(fp) && readstanza(&s)) {
2113 if (s.numpairs == 0)
2115 if (!file_test_run(&s)) {
2126 int test_main(int argc, char *argv[])
2128 static const char rnd_seed[] =
2129 "If not seeded, BN_generate_prime might fail";
2130 int result = EXIT_FAILURE;
2133 RAND_seed(rnd_seed, sizeof rnd_seed);
2134 if (!TEST_ptr(ctx = BN_CTX_new()))
2139 ADD_TEST(test_div_recip);
2141 ADD_TEST(test_modexp_mont5);
2142 ADD_TEST(test_kronecker);
2143 ADD_TEST(test_rand);
2144 ADD_TEST(test_bn2padded);
2145 ADD_TEST(test_dec2bn);
2146 ADD_TEST(test_hex2bn);
2147 ADD_TEST(test_asc2bn);
2148 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2149 ADD_TEST(test_negzero);
2150 ADD_TEST(test_badmod);
2151 ADD_TEST(test_expmodzero);
2152 ADD_TEST(test_smallprime);
2153 #ifndef OPENSSL_NO_EC2M
2154 ADD_TEST(test_gf2m_add);
2155 ADD_TEST(test_gf2m_mod);
2156 ADD_TEST(test_gf2m_mul);
2157 ADD_TEST(test_gf2m_sqr);
2158 ADD_TEST(test_gf2m_modinv);
2159 ADD_TEST(test_gf2m_moddiv);
2160 ADD_TEST(test_gf2m_modexp);
2161 ADD_TEST(test_gf2m_modsqrt);
2162 ADD_TEST(test_gf2m_modsolvequad);
2164 ADD_TEST(test_3_is_prime);
2166 testfiles = &argv[1];
2167 ADD_ALL_TESTS(run_file_tests, argc - 1);
2170 result = run_tests(argv[0]);