2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #include <openssl/bn.h>
16 #include <openssl/crypto.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
19 #include "internal/nelem.h"
20 #include "internal/numbers.h"
23 #ifdef OPENSSL_SYS_WINDOWS
24 # define strcasecmp _stricmp
28 * Things in boring, not in openssl. TODO we should add them.
30 #define HAVE_BN_PADDED 0
31 #define HAVE_BN_SQRT 0
33 typedef struct filetest_st {
35 int (*func)(STANZA *s);
38 typedef struct mpitest_st {
44 static const int NUM0 = 100; /* number of tests */
45 static const int NUM1 = 50; /* additional tests for some functions */
49 * Polynomial coefficients used in GFM tests.
51 #ifndef OPENSSL_NO_EC2M
52 static int p0[] = { 163, 7, 6, 3, 0, -1 };
53 static int p1[] = { 193, 15, 0, -1 };
57 * Look for |key| in the stanza and return it or NULL if not found.
59 static const char *findattr(STANZA *s, const char *key)
64 for ( ; --i >= 0; pp++)
65 if (strcasecmp(pp->key, key) == 0)
71 * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
73 static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
75 char *bigstring = glue_strings(bn_strings, NULL);
76 int ret = BN_hex2bn(out, bigstring);
78 OPENSSL_free(bigstring);
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_swap(void)
156 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
157 int top, cond, st = 0;
159 if (!TEST_ptr(a = BN_new())
160 || !TEST_ptr(b = BN_new())
161 || !TEST_ptr(c = BN_new())
162 || !TEST_ptr(d = BN_new()))
165 BN_bntest_rand(a, 1024, 1, 0);
166 BN_bntest_rand(b, 1024, 1, 0);
169 top = BN_num_bits(a)/BN_BITS2;
173 if (!equalBN("swap", a, d)
174 || !equalBN("swap", b, c))
177 /* conditional swap: true */
179 BN_consttime_swap(cond, a, b, top);
180 if (!equalBN("cswap true", a, c)
181 || !equalBN("cswap true", b, d))
184 /* conditional swap: false */
186 BN_consttime_swap(cond, a, b, top);
187 if (!equalBN("cswap false", a, c)
188 || !equalBN("cswap false", b, d))
191 /* same tests but checking flag swap */
192 BN_set_flags(a, BN_FLG_CONSTTIME);
195 if (!equalBN("swap, flags", a, d)
196 || !equalBN("swap, flags", b, c)
197 || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
198 || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
202 BN_consttime_swap(cond, a, b, top);
203 if (!equalBN("cswap true, flags", a, c)
204 || !equalBN("cswap true, flags", b, d)
205 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
206 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
210 BN_consttime_swap(cond, a, b, top);
211 if (!equalBN("cswap false, flags", a, c)
212 || !equalBN("cswap false, flags", b, d)
213 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
214 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
226 static int test_sub(void)
228 BIGNUM *a = NULL, *b = NULL, *c = NULL;
231 if (!TEST_ptr(a = BN_new())
232 || !TEST_ptr(b = BN_new())
233 || !TEST_ptr(c = BN_new()))
236 for (i = 0; i < NUM0 + NUM1; i++) {
238 BN_bntest_rand(a, 512, 0, 0);
240 if (!TEST_int_ne(BN_set_bit(a, i), 0))
244 BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
245 BN_set_negative(a, rand_neg());
246 BN_set_negative(b, rand_neg());
251 if (!TEST_BN_eq_zero(c))
263 static int test_div_recip(void)
265 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
266 BN_RECP_CTX *recp = NULL;
269 if (!TEST_ptr(a = BN_new())
270 || !TEST_ptr(b = BN_new())
271 || !TEST_ptr(c = BN_new())
272 || !TEST_ptr(d = BN_new())
273 || !TEST_ptr(e = BN_new())
274 || !TEST_ptr(recp = BN_RECP_CTX_new()))
277 for (i = 0; i < NUM0 + NUM1; i++) {
279 BN_bntest_rand(a, 400, 0, 0);
284 BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
285 BN_set_negative(a, rand_neg());
286 BN_set_negative(b, rand_neg());
287 BN_RECP_CTX_set(recp, b, ctx);
288 BN_div_recp(d, c, a, recp, ctx);
289 BN_mul(e, d, b, ctx);
292 if (!TEST_BN_eq_zero(d))
302 BN_RECP_CTX_free(recp);
307 static int test_mod(void)
309 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
312 if (!TEST_ptr(a = BN_new())
313 || !TEST_ptr(b = BN_new())
314 || !TEST_ptr(c = BN_new())
315 || !TEST_ptr(d = BN_new())
316 || !TEST_ptr(e = BN_new()))
319 BN_bntest_rand(a, 1024, 0, 0);
320 for (i = 0; i < NUM0; i++) {
321 BN_bntest_rand(b, 450 + i * 10, 0, 0);
322 BN_set_negative(a, rand_neg());
323 BN_set_negative(b, rand_neg());
324 BN_mod(c, a, b, ctx);
325 BN_div(d, e, a, b, ctx);
327 if (!TEST_BN_eq_zero(e))
340 static const char *bn1strings[] = {
341 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
342 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
343 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
344 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
345 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
346 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
347 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
348 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
349 "0000000000000000000000000000000000000000000000000000000000000000",
350 "0000000000000000000000000000000000000000000000000000000000000000",
351 "0000000000000000000000000000000000000000000000000000000000000000",
352 "0000000000000000000000000000000000000000000000000000000000000000",
353 "0000000000000000000000000000000000000000000000000000000000000000",
354 "0000000000000000000000000000000000000000000000000000000000000000",
355 "0000000000000000000000000000000000000000000000000000000000000000",
356 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
360 static const char *bn2strings[] = {
361 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
362 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
363 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
364 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
365 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
366 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
367 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
368 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
369 "0000000000000000000000000000000000000000000000000000000000000000",
370 "0000000000000000000000000000000000000000000000000000000000000000",
371 "0000000000000000000000000000000000000000000000000000000000000000",
372 "0000000000000000000000000000000000000000000000000000000000000000",
373 "0000000000000000000000000000000000000000000000000000000000000000",
374 "0000000000000000000000000000000000000000000000000000000000000000",
375 "0000000000000000000000000000000000000000000000000000000000000000",
376 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
381 * Test constant-time modular exponentiation with 1024-bit inputs, which on
382 * x86_64 cause a different code branch to be taken.
384 static int test_modexp_mont5(void)
386 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
387 BIGNUM *b = NULL, *n = NULL, *c = NULL;
388 BN_MONT_CTX *mont = NULL;
391 if (!TEST_ptr(a = BN_new())
392 || !TEST_ptr(p = BN_new())
393 || !TEST_ptr(m = BN_new())
394 || !TEST_ptr(d = BN_new())
395 || !TEST_ptr(e = BN_new())
396 || !TEST_ptr(b = BN_new())
397 || !TEST_ptr(n = BN_new())
398 || !TEST_ptr(c = BN_new())
399 || !TEST_ptr(mont = BN_MONT_CTX_new()))
402 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
404 BN_bntest_rand(a, 1024, 0, 0);
406 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
408 if (!TEST_BN_eq_one(d))
411 /* Regression test for carry bug in mulx4x_mont */
413 "7878787878787878787878787878787878787878787878787878787878787878"
414 "7878787878787878787878787878787878787878787878787878787878787878"
415 "7878787878787878787878787878787878787878787878787878787878787878"
416 "7878787878787878787878787878787878787878787878787878787878787878");
418 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
419 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
420 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
421 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
423 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
424 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
425 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
426 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
427 BN_MONT_CTX_set(mont, n, ctx);
428 BN_mod_mul_montgomery(c, a, b, mont, ctx);
429 BN_mod_mul_montgomery(d, b, a, mont, ctx);
430 if (!TEST_BN_eq(c, d))
433 /* Regression test for carry bug in sqr[x]8x_mont */
434 parse_bigBN(&n, bn1strings);
435 parse_bigBN(&a, bn2strings);
438 BN_MONT_CTX_set(mont, n, ctx);
439 BN_mod_mul_montgomery(c, a, a, mont, ctx);
440 BN_mod_mul_montgomery(d, a, b, mont, ctx);
441 if (!TEST_BN_eq(c, d))
444 /* Regression test for carry bug in bn_sqrx8x_internal */
446 static const char *ahex[] = {
447 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
448 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
449 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
450 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
451 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
452 "9544D954000000006C0000000000000000000000000000000000000000000000",
453 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
454 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
455 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
456 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
457 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
458 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
461 static const char *nhex[] = {
462 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
463 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
464 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
465 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
466 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
467 "00000010000000006C0000000000000000000000000000000000000000000000",
468 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
469 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
470 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
471 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
472 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
473 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
477 parse_bigBN(&a, ahex);
478 parse_bigBN(&n, nhex);
482 BN_MONT_CTX_set(mont, n, ctx);
483 if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
484 || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
485 || !TEST_BN_eq(c, d))
488 /* Regression test for bug in BN_from_montgomery_word */
490 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
491 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
492 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
494 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
495 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
496 BN_MONT_CTX_set(mont, n, ctx);
497 if (!TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx)))
500 /* Regression test for bug in rsaz_1024_mul_avx2 */
502 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
503 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
504 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
505 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
507 "2020202020202020202020202020202020202020202020202020202020202020"
508 "2020202020202020202020202020202020202020202020202020202020202020"
509 "20202020202020FF202020202020202020202020202020202020202020202020"
510 "2020202020202020202020202020202020202020202020202020202020202020");
512 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
513 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
514 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
515 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF");
516 BN_MONT_CTX_set(mont, n, ctx);
517 BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont);
518 BN_mod_exp_mont(d, a, b, n, ctx, mont);
519 if (!TEST_BN_eq(c, d))
523 BN_bntest_rand(p, 1024, 0, 0);
525 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
526 || !TEST_BN_eq_zero(d))
530 * Craft an input whose Montgomery representation is 1, i.e., shorter
531 * than the modulus m, in order to test the const time precomputation
532 * scattering/gathering.
535 BN_MONT_CTX_set(mont, m, ctx);
536 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
537 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
538 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
539 || !TEST_BN_eq(a, d))
542 /* Finally, some regular test vectors. */
543 BN_bntest_rand(e, 1024, 0, 0);
544 if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
545 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
546 || !TEST_BN_eq(a, d))
552 BN_MONT_CTX_free(mont);
564 #ifndef OPENSSL_NO_EC2M
565 static int test_gf2m_add(void)
567 BIGNUM *a = NULL, *b = NULL, *c = NULL;
570 if (!TEST_ptr(a = BN_new())
571 || !TEST_ptr(b = BN_new())
572 || !TEST_ptr(c = BN_new()))
575 for (i = 0; i < NUM0; i++) {
576 BN_rand(a, 512, 0, 0);
577 BN_copy(b, BN_value_one());
578 BN_set_negative(a, rand_neg());
579 BN_set_negative(b, rand_neg());
580 BN_GF2m_add(c, a, b);
581 /* Test that two added values have the correct parity. */
582 if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
583 || (!BN_is_odd(a) && !BN_is_odd(c))))
585 BN_GF2m_add(c, c, c);
586 /* Test that c + c = 0. */
587 if (!TEST_BN_eq_zero(c))
598 static int test_gf2m_mod(void)
600 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
603 if (!TEST_ptr(a = BN_new())
604 || !TEST_ptr(b[0] = BN_new())
605 || !TEST_ptr(b[1] = BN_new())
606 || !TEST_ptr(c = BN_new())
607 || !TEST_ptr(d = BN_new())
608 || !TEST_ptr(e = BN_new()))
611 BN_GF2m_arr2poly(p0, b[0]);
612 BN_GF2m_arr2poly(p1, b[1]);
614 for (i = 0; i < NUM0; i++) {
615 BN_bntest_rand(a, 1024, 0, 0);
616 for (j = 0; j < 2; j++) {
617 BN_GF2m_mod(c, a, b[j]);
618 BN_GF2m_add(d, a, c);
619 BN_GF2m_mod(e, d, b[j]);
620 /* Test that a + (a mod p) mod p == 0. */
621 if (!TEST_BN_eq_zero(e))
636 static int test_gf2m_mul(void)
638 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
639 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
642 if (!TEST_ptr(a = BN_new())
643 || !TEST_ptr(b[0] = BN_new())
644 || !TEST_ptr(b[1] = BN_new())
645 || !TEST_ptr(c = BN_new())
646 || !TEST_ptr(d = BN_new())
647 || !TEST_ptr(e = BN_new())
648 || !TEST_ptr(f = BN_new())
649 || !TEST_ptr(g = BN_new())
650 || !TEST_ptr(h = BN_new()))
653 BN_GF2m_arr2poly(p0, b[0]);
654 BN_GF2m_arr2poly(p1, b[1]);
656 for (i = 0; i < NUM0; i++) {
657 BN_bntest_rand(a, 1024, 0, 0);
658 BN_bntest_rand(c, 1024, 0, 0);
659 BN_bntest_rand(d, 1024, 0, 0);
660 for (j = 0; j < 2; j++) {
661 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
662 BN_GF2m_add(f, a, d);
663 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
664 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
665 BN_GF2m_add(f, e, g);
666 BN_GF2m_add(f, f, h);
667 /* Test that (a+d)*c = a*c + d*c. */
668 if (!TEST_BN_eq_zero(f))
687 static int test_gf2m_sqr(void)
689 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
692 if (!TEST_ptr(a = BN_new())
693 || !TEST_ptr(b[0] = BN_new())
694 || !TEST_ptr(b[1] = BN_new())
695 || !TEST_ptr(c = BN_new())
696 || !TEST_ptr(d = 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, 1024, 0, 0);
704 for (j = 0; j < 2; j++) {
705 BN_GF2m_mod_sqr(c, a, b[j], ctx);
707 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
708 BN_GF2m_add(d, c, d);
709 /* Test that a*a = a^2. */
710 if (!TEST_BN_eq_zero(d))
724 static int test_gf2m_modinv(void)
726 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
729 if (!TEST_ptr(a = BN_new())
730 || !TEST_ptr(b[0] = BN_new())
731 || !TEST_ptr(b[1] = BN_new())
732 || !TEST_ptr(c = BN_new())
733 || !TEST_ptr(d = BN_new()))
736 BN_GF2m_arr2poly(p0, b[0]);
737 BN_GF2m_arr2poly(p1, b[1]);
739 for (i = 0; i < NUM0; i++) {
740 BN_bntest_rand(a, 512, 0, 0);
741 for (j = 0; j < 2; j++) {
742 BN_GF2m_mod_inv(c, a, b[j], ctx);
743 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
744 /* Test that ((1/a)*a) = 1. */
745 if (!TEST_BN_eq_one(d))
759 static int test_gf2m_moddiv(void)
761 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
762 BIGNUM *e = NULL, *f = NULL;
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())
771 || !TEST_ptr(f = BN_new()))
774 BN_GF2m_arr2poly(p0, b[0]);
775 BN_GF2m_arr2poly(p1, b[1]);
777 for (i = 0; i < NUM0; i++) {
778 BN_bntest_rand(a, 512, 0, 0);
779 BN_bntest_rand(c, 512, 0, 0);
780 for (j = 0; j < 2; j++) {
781 BN_GF2m_mod_div(d, a, c, b[j], ctx);
782 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
783 BN_GF2m_mod_div(f, a, e, b[j], ctx);
784 /* Test that ((a/c)*c)/a = 1. */
785 if (!TEST_BN_eq_one(f))
801 static int test_gf2m_modexp(void)
803 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
804 BIGNUM *e = NULL, *f = NULL;
807 if (!TEST_ptr(a = BN_new())
808 || !TEST_ptr(b[0] = BN_new())
809 || !TEST_ptr(b[1] = BN_new())
810 || !TEST_ptr(c = BN_new())
811 || !TEST_ptr(d = BN_new())
812 || !TEST_ptr(e = BN_new())
813 || !TEST_ptr(f = BN_new()))
816 BN_GF2m_arr2poly(p0, b[0]);
817 BN_GF2m_arr2poly(p1, b[1]);
819 for (i = 0; i < NUM0; i++) {
820 BN_bntest_rand(a, 512, 0, 0);
821 BN_bntest_rand(c, 512, 0, 0);
822 BN_bntest_rand(d, 512, 0, 0);
823 for (j = 0; j < 2; j++) {
824 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
825 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
826 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
828 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
829 BN_GF2m_add(f, e, f);
830 /* Test that a^(c+d)=a^c*a^d. */
831 if (!TEST_BN_eq_zero(f))
847 static int test_gf2m_modsqrt(void)
849 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
850 BIGNUM *e = NULL, *f = NULL;
853 if (!TEST_ptr(a = BN_new())
854 || !TEST_ptr(b[0] = BN_new())
855 || !TEST_ptr(b[1] = BN_new())
856 || !TEST_ptr(c = BN_new())
857 || !TEST_ptr(d = BN_new())
858 || !TEST_ptr(e = BN_new())
859 || !TEST_ptr(f = BN_new()))
862 BN_GF2m_arr2poly(p0, b[0]);
863 BN_GF2m_arr2poly(p1, b[1]);
865 for (i = 0; i < NUM0; i++) {
866 BN_bntest_rand(a, 512, 0, 0);
867 for (j = 0; j < 2; j++) {
868 BN_GF2m_mod(c, a, b[j]);
869 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
870 BN_GF2m_mod_sqr(e, d, b[j], ctx);
871 BN_GF2m_add(f, c, e);
872 /* Test that d^2 = a, where d = sqrt(a). */
873 if (!TEST_BN_eq_zero(f))
889 static int test_gf2m_modsolvequad(void)
891 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
893 int i, j, s = 0, t, st = 0;
895 if (!TEST_ptr(a = BN_new())
896 || !TEST_ptr(b[0] = BN_new())
897 || !TEST_ptr(b[1] = BN_new())
898 || !TEST_ptr(c = BN_new())
899 || !TEST_ptr(d = BN_new())
900 || !TEST_ptr(e = BN_new()))
903 BN_GF2m_arr2poly(p0, b[0]);
904 BN_GF2m_arr2poly(p1, b[1]);
906 for (i = 0; i < NUM0; i++) {
907 BN_bntest_rand(a, 512, 0, 0);
908 for (j = 0; j < 2; j++) {
909 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
912 BN_GF2m_mod_sqr(d, c, b[j], ctx);
913 BN_GF2m_add(d, c, d);
914 BN_GF2m_mod(e, a, b[j]);
915 BN_GF2m_add(e, e, d);
917 * Test that solution of quadratic c satisfies c^2 + c = a.
919 if (!TEST_BN_eq_zero(e))
924 if (!TEST_int_ge(s, 0)) {
925 TEST_info("%d tests found no roots; probably an error", NUM0);
940 static int test_kronecker(void)
942 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
943 int i, legendre, kronecker, st = 0;
945 if (!TEST_ptr(a = BN_new())
946 || !TEST_ptr(b = BN_new())
947 || !TEST_ptr(r = BN_new())
948 || !TEST_ptr(t = BN_new()))
952 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
953 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
954 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
955 * generate a random prime b and compare these values for a number of
956 * random a's. (That is, we run the Solovay-Strassen primality test to
957 * confirm that b is prime, except that we don't want to test whether b
958 * is prime but whether BN_kronecker works.)
961 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
963 BN_set_negative(b, rand_neg());
965 for (i = 0; i < NUM0; i++) {
966 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
968 BN_set_negative(a, rand_neg());
970 /* t := (|b|-1)/2 (note that b is odd) */
971 if (!TEST_true(BN_copy(t, b)))
973 BN_set_negative(t, 0);
974 if (!TEST_true(BN_sub_word(t, 1)))
976 if (!TEST_true(BN_rshift1(t, t)))
979 BN_set_negative(b, 0);
981 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
983 BN_set_negative(b, 1);
985 if (BN_is_word(r, 1))
987 else if (BN_is_zero(r))
990 if (!TEST_true(BN_add_word(r, 1)))
992 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
993 TEST_info("Legendre symbol computation failed");
999 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
1001 /* we actually need BN_kronecker(a, |b|) */
1002 if (BN_is_negative(a) && BN_is_negative(b))
1003 kronecker = -kronecker;
1005 if (!TEST_int_eq(legendre, kronecker))
1018 static int file_sum(STANZA *s)
1020 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
1024 if (!TEST_ptr(a = getBN(s, "A"))
1025 || !TEST_ptr(b = getBN(s, "B"))
1026 || !TEST_ptr(sum = getBN(s, "Sum"))
1027 || !TEST_ptr(ret = BN_new()))
1030 if (!TEST_true(BN_add(ret, a, b))
1031 || !equalBN("A + B", sum, ret)
1032 || !TEST_true(BN_sub(ret, sum, a))
1033 || !equalBN("Sum - A", b, ret)
1034 || !TEST_true(BN_sub(ret, sum, b))
1035 || !equalBN("Sum - B", a, ret))
1039 * Test that the functions work when |r| and |a| point to the same BIGNUM,
1040 * or when |r| and |b| point to the same BIGNUM.
1041 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1043 if (!TEST_true(BN_copy(ret, a))
1044 || !TEST_true(BN_add(ret, ret, b))
1045 || !equalBN("A + B (r is a)", sum, ret)
1046 || !TEST_true(BN_copy(ret, b))
1047 || !TEST_true(BN_add(ret, a, ret))
1048 || !equalBN("A + B (r is b)", sum, ret)
1049 || !TEST_true(BN_copy(ret, sum))
1050 || !TEST_true(BN_sub(ret, ret, a))
1051 || !equalBN("Sum - A (r is a)", b, ret)
1052 || !TEST_true(BN_copy(ret, a))
1053 || !TEST_true(BN_sub(ret, sum, ret))
1054 || !equalBN("Sum - A (r is b)", b, ret)
1055 || !TEST_true(BN_copy(ret, sum))
1056 || !TEST_true(BN_sub(ret, ret, b))
1057 || !equalBN("Sum - B (r is a)", a, ret)
1058 || !TEST_true(BN_copy(ret, b))
1059 || !TEST_true(BN_sub(ret, sum, ret))
1060 || !equalBN("Sum - B (r is b)", a, ret))
1064 * Test BN_uadd() and BN_usub() with the prerequisites they are
1065 * documented as having. Note that these functions are frequently used
1066 * when the prerequisites don't hold. In those cases, they are supposed
1067 * to work as if the prerequisite hold, but we don't test that yet.
1070 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
1071 if (!TEST_true(BN_uadd(ret, a, b))
1072 || !equalBN("A +u B", sum, ret)
1073 || !TEST_true(BN_usub(ret, sum, a))
1074 || !equalBN("Sum -u A", b, ret)
1075 || !TEST_true(BN_usub(ret, sum, b))
1076 || !equalBN("Sum -u B", a, ret))
1079 * Test that the functions work when |r| and |a| point to the same
1080 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1081 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1083 if (!TEST_true(BN_copy(ret, a))
1084 || !TEST_true(BN_uadd(ret, ret, b))
1085 || !equalBN("A +u B (r is a)", sum, ret)
1086 || !TEST_true(BN_copy(ret, b))
1087 || !TEST_true(BN_uadd(ret, a, ret))
1088 || !equalBN("A +u B (r is b)", sum, ret)
1089 || !TEST_true(BN_copy(ret, sum))
1090 || !TEST_true(BN_usub(ret, ret, a))
1091 || !equalBN("Sum -u A (r is a)", b, ret)
1092 || !TEST_true(BN_copy(ret, a))
1093 || !TEST_true(BN_usub(ret, sum, ret))
1094 || !equalBN("Sum -u A (r is b)", b, ret)
1095 || !TEST_true(BN_copy(ret, sum))
1096 || !TEST_true(BN_usub(ret, ret, b))
1097 || !equalBN("Sum -u B (r is a)", a, ret)
1098 || !TEST_true(BN_copy(ret, b))
1099 || !TEST_true(BN_usub(ret, sum, ret))
1100 || !equalBN("Sum -u B (r is b)", a, ret))
1105 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1107 b_word = BN_get_word(b);
1108 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1109 if (!TEST_true(BN_copy(ret, a))
1110 || !TEST_true(BN_add_word(ret, b_word))
1111 || !equalBN("A + B (word)", sum, ret)
1112 || !TEST_true(BN_copy(ret, sum))
1113 || !TEST_true(BN_sub_word(ret, b_word))
1114 || !equalBN("Sum - B (word)", a, ret))
1127 static int file_lshift1(STANZA *s)
1129 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1130 BIGNUM *two = NULL, *remainder = NULL;
1133 if (!TEST_ptr(a = getBN(s, "A"))
1134 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1135 || !TEST_ptr(zero = BN_new())
1136 || !TEST_ptr(ret = BN_new())
1137 || !TEST_ptr(two = BN_new())
1138 || !TEST_ptr(remainder = BN_new()))
1143 if (!TEST_true(BN_set_word(two, 2))
1144 || !TEST_true(BN_add(ret, a, a))
1145 || !equalBN("A + A", lshift1, ret)
1146 || !TEST_true(BN_mul(ret, a, two, ctx))
1147 || !equalBN("A * 2", lshift1, ret)
1148 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1149 || !equalBN("LShift1 / 2", a, ret)
1150 || !equalBN("LShift1 % 2", zero, remainder)
1151 || !TEST_true(BN_lshift1(ret, a))
1152 || !equalBN("A << 1", lshift1, ret)
1153 || !TEST_true(BN_rshift1(ret, lshift1))
1154 || !equalBN("LShift >> 1", a, ret)
1155 || !TEST_true(BN_rshift1(ret, lshift1))
1156 || !equalBN("LShift >> 1", a, ret))
1159 /* Set the LSB to 1 and test rshift1 again. */
1160 if (!TEST_true(BN_set_bit(lshift1, 0))
1161 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1162 || !equalBN("(LShift1 | 1) / 2", a, ret)
1163 || !TEST_true(BN_rshift1(ret, lshift1))
1164 || !equalBN("(LShift | 1) >> 1", a, ret))
1179 static int file_lshift(STANZA *s)
1181 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1184 if (!TEST_ptr(a = getBN(s, "A"))
1185 || !TEST_ptr(lshift = getBN(s, "LShift"))
1186 || !TEST_ptr(ret = BN_new())
1187 || !getint(s, &n, "N"))
1190 if (!TEST_true(BN_lshift(ret, a, n))
1191 || !equalBN("A << N", lshift, ret)
1192 || !TEST_true(BN_rshift(ret, lshift, n))
1193 || !equalBN("A >> N", a, ret))
1204 static int file_rshift(STANZA *s)
1206 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1209 if (!TEST_ptr(a = getBN(s, "A"))
1210 || !TEST_ptr(rshift = getBN(s, "RShift"))
1211 || !TEST_ptr(ret = BN_new())
1212 || !getint(s, &n, "N"))
1215 if (!TEST_true(BN_rshift(ret, a, n))
1216 || !equalBN("A >> N", rshift, ret))
1219 /* If N == 1, try with rshift1 as well */
1221 if (!TEST_true(BN_rshift1(ret, a))
1222 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1234 static int file_square(STANZA *s)
1236 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1237 BIGNUM *remainder = NULL, *tmp = NULL;
1240 if (!TEST_ptr(a = getBN(s, "A"))
1241 || !TEST_ptr(square = getBN(s, "Square"))
1242 || !TEST_ptr(zero = BN_new())
1243 || !TEST_ptr(ret = BN_new())
1244 || !TEST_ptr(remainder = BN_new()))
1248 if (!TEST_true(BN_sqr(ret, a, ctx))
1249 || !equalBN("A^2", square, ret)
1250 || !TEST_true(BN_mul(ret, a, a, ctx))
1251 || !equalBN("A * A", square, ret)
1252 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1253 || !equalBN("Square / A", a, ret)
1254 || !equalBN("Square % A", zero, remainder))
1258 BN_set_negative(a, 0);
1259 if (!TEST_true(BN_sqrt(ret, square, ctx))
1260 || !equalBN("sqrt(Square)", a, ret))
1263 /* BN_sqrt should fail on non-squares and negative numbers. */
1264 if (!TEST_BN_eq_zero(square)) {
1265 if (!TEST_ptr(tmp = BN_new())
1266 || !TEST_true(BN_copy(tmp, square)))
1268 BN_set_negative(tmp, 1);
1270 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1274 BN_set_negative(tmp, 0);
1275 if (BN_add(tmp, tmp, BN_value_one()))
1277 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1294 static int file_product(STANZA *s)
1296 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1297 BIGNUM *remainder = NULL, *zero = NULL;
1300 if (!TEST_ptr(a = getBN(s, "A"))
1301 || !TEST_ptr(b = getBN(s, "B"))
1302 || !TEST_ptr(product = getBN(s, "Product"))
1303 || !TEST_ptr(ret = BN_new())
1304 || !TEST_ptr(remainder = BN_new())
1305 || !TEST_ptr(zero = BN_new()))
1310 if (!TEST_true(BN_mul(ret, a, b, ctx))
1311 || !equalBN("A * B", product, ret)
1312 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1313 || !equalBN("Product / A", b, ret)
1314 || !equalBN("Product % A", zero, remainder)
1315 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1316 || !equalBN("Product / B", a, ret)
1317 || !equalBN("Product % B", zero, remainder))
1331 static int file_quotient(STANZA *s)
1333 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1334 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1335 BN_ULONG b_word, ret_word;
1338 if (!TEST_ptr(a = getBN(s, "A"))
1339 || !TEST_ptr(b = getBN(s, "B"))
1340 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1341 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1342 || !TEST_ptr(ret = BN_new())
1343 || !TEST_ptr(ret2 = BN_new())
1344 || !TEST_ptr(nnmod = BN_new()))
1347 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1348 || !equalBN("A / B", quotient, ret)
1349 || !equalBN("A % B", remainder, ret2)
1350 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1351 || !TEST_true(BN_add(ret, ret, remainder))
1352 || !equalBN("Quotient * B + Remainder", a, ret))
1356 * Test with BN_mod_word() and BN_div_word() if the divisor is
1359 b_word = BN_get_word(b);
1360 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1361 BN_ULONG remainder_word = BN_get_word(remainder);
1363 assert(remainder_word != (BN_ULONG)-1);
1364 if (!TEST_ptr(BN_copy(ret, a)))
1366 ret_word = BN_div_word(ret, b_word);
1367 if (ret_word != remainder_word) {
1370 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1371 ret_word, remainder_word);
1373 TEST_error("Got A %% B (word) mismatch");
1377 if (!equalBN ("A / B (word)", quotient, ret))
1380 ret_word = BN_mod_word(a, b_word);
1381 if (ret_word != remainder_word) {
1384 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1385 ret_word, remainder_word);
1387 TEST_error("Got A %% B (word) mismatch");
1393 /* Test BN_nnmod. */
1394 if (!BN_is_negative(b)) {
1395 if (!TEST_true(BN_copy(nnmod, remainder))
1396 || (BN_is_negative(nnmod)
1397 && !TEST_true(BN_add(nnmod, nnmod, b)))
1398 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1399 || !equalBN("A % B (non-negative)", nnmod, ret))
1415 static int file_modmul(STANZA *s)
1417 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1420 if (!TEST_ptr(a = getBN(s, "A"))
1421 || !TEST_ptr(b = getBN(s, "B"))
1422 || !TEST_ptr(m = getBN(s, "M"))
1423 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1424 || !TEST_ptr(ret = BN_new()))
1427 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1428 || !equalBN("A * B (mod M)", mod_mul, ret))
1432 /* Reduce |a| and |b| and test the Montgomery version. */
1433 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1434 BIGNUM *a_tmp = BN_new();
1435 BIGNUM *b_tmp = BN_new();
1437 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1438 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1439 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1440 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1441 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1442 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1443 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1445 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1446 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1450 BN_MONT_CTX_free(mont);
1467 static int file_modexp(STANZA *s)
1469 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1470 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1473 if (!TEST_ptr(a = getBN(s, "A"))
1474 || !TEST_ptr(e = getBN(s, "E"))
1475 || !TEST_ptr(m = getBN(s, "M"))
1476 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1477 || !TEST_ptr(ret = BN_new())
1478 || !TEST_ptr(d = BN_new()))
1481 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1482 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1486 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1487 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1488 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1490 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1494 /* Regression test for carry propagation bug in sqr8x_reduction */
1495 BN_hex2bn(&a, "050505050505");
1496 BN_hex2bn(&b, "02");
1498 "4141414141414141414141274141414141414141414141414141414141414141"
1499 "4141414141414141414141414141414141414141414141414141414141414141"
1500 "4141414141414141414141800000000000000000000000000000000000000000"
1501 "0000000000000000000000000000000000000000000000000000000000000000"
1502 "0000000000000000000000000000000000000000000000000000000000000000"
1503 "0000000000000000000000000000000000000000000000000000000001");
1504 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1505 || !TEST_true(BN_mul(e, a, a, ctx))
1506 || !TEST_BN_eq(d, e))
1522 static int file_exp(STANZA *s)
1524 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1527 if (!TEST_ptr(a = getBN(s, "A"))
1528 || !TEST_ptr(e = getBN(s, "E"))
1529 || !TEST_ptr(exp = getBN(s, "Exp"))
1530 || !TEST_ptr(ret = BN_new()))
1533 if (!TEST_true(BN_exp(ret, a, e, ctx))
1534 || !equalBN("A ^ E", exp, ret))
1546 static int file_modsqrt(STANZA *s)
1548 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1551 if (!TEST_ptr(a = getBN(s, "A"))
1552 || !TEST_ptr(p = getBN(s, "P"))
1553 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1554 || !TEST_ptr(ret = BN_new())
1555 || !TEST_ptr(ret2 = BN_new()))
1558 /* There are two possible answers. */
1559 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1560 || !TEST_true(BN_sub(ret2, p, ret)))
1563 /* The first condition should NOT be a test. */
1564 if (BN_cmp(ret2, mod_sqrt) != 0
1565 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1578 static int test_bn2padded(void)
1581 uint8_t zeros[256], out[256], reference[128];
1582 BIGNUM *n = BN_new();
1585 /* Test edge case at 0. */
1588 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1590 memset(out, -1, sizeof(out));
1591 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1593 memset(zeros, 0, sizeof(zeros));
1594 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1597 /* Test a random numbers at various byte lengths. */
1598 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1599 #define TOP_BIT_ON 0
1600 #define BOTTOM_BIT_NOTOUCH 0
1601 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1603 if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1604 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1606 /* Empty buffer should fail. */
1607 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1609 /* One byte short should fail. */
1610 if (BN_bn2bin_padded(out, bytes - 1, n))
1612 /* Exactly right size should encode. */
1613 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1614 || TEST_mem_eq(out, bytes, reference, bytes))
1616 /* Pad up one byte extra. */
1617 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1618 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1619 || !TEST_mem_eq(out, 1, zeros, 1))
1621 /* Pad up to 256. */
1622 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1623 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1625 || !TEST_mem_eq(out, sizseof(out) - bytes,
1626 zeros, sizeof(out) - bytes))
1639 static int test_dec2bn(void)
1644 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1645 || !TEST_BN_eq_word(bn, 0)
1646 || !TEST_BN_eq_zero(bn)
1647 || !TEST_BN_le_zero(bn)
1648 || !TEST_BN_ge_zero(bn)
1649 || !TEST_BN_even(bn))
1654 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1655 || !TEST_BN_eq_word(bn, 256)
1656 || !TEST_BN_ge_zero(bn)
1657 || !TEST_BN_gt_zero(bn)
1658 || !TEST_BN_ne_zero(bn)
1659 || !TEST_BN_even(bn))
1664 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1665 || !TEST_BN_abs_eq_word(bn, 42)
1666 || !TEST_BN_lt_zero(bn)
1667 || !TEST_BN_le_zero(bn)
1668 || !TEST_BN_ne_zero(bn)
1669 || !TEST_BN_even(bn))
1674 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1675 || !TEST_BN_eq_word(bn, 1)
1676 || !TEST_BN_ne_zero(bn)
1677 || !TEST_BN_gt_zero(bn)
1678 || !TEST_BN_ge_zero(bn)
1679 || !TEST_BN_eq_one(bn)
1680 || !TEST_BN_odd(bn))
1685 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1686 || !TEST_BN_eq_zero(bn)
1687 || !TEST_BN_ge_zero(bn)
1688 || !TEST_BN_le_zero(bn)
1689 || !TEST_BN_even(bn))
1694 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1695 || !TEST_BN_abs_eq_word(bn, 42)
1696 || !TEST_BN_ge_zero(bn)
1697 || !TEST_BN_gt_zero(bn)
1698 || !TEST_BN_ne_zero(bn)
1699 || !TEST_BN_even(bn))
1708 static int test_hex2bn(void)
1713 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1714 || !TEST_BN_eq_zero(bn)
1715 || !TEST_BN_ge_zero(bn)
1716 || !TEST_BN_even(bn))
1721 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1722 || !TEST_BN_eq_word(bn, 0x256)
1723 || !TEST_BN_ge_zero(bn)
1724 || !TEST_BN_gt_zero(bn)
1725 || !TEST_BN_ne_zero(bn)
1726 || !TEST_BN_even(bn))
1731 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1732 || !TEST_BN_abs_eq_word(bn, 0x42)
1733 || !TEST_BN_lt_zero(bn)
1734 || !TEST_BN_le_zero(bn)
1735 || !TEST_BN_ne_zero(bn)
1736 || !TEST_BN_even(bn))
1741 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1742 || !TEST_BN_eq_word(bn, 0xCB)
1743 || !TEST_BN_ge_zero(bn)
1744 || !TEST_BN_gt_zero(bn)
1745 || !TEST_BN_ne_zero(bn)
1746 || !TEST_BN_odd(bn))
1751 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1752 || !TEST_BN_eq_zero(bn)
1753 || !TEST_BN_ge_zero(bn)
1754 || !TEST_BN_le_zero(bn)
1755 || !TEST_BN_even(bn))
1760 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1761 || !TEST_BN_eq_word(bn, 0xabc)
1762 || !TEST_BN_ge_zero(bn)
1763 || !TEST_BN_gt_zero(bn)
1764 || !TEST_BN_ne_zero(bn)
1765 || !TEST_BN_even(bn))
1774 static int test_asc2bn(void)
1779 if (!TEST_ptr(bn = BN_new()))
1782 if (!TEST_true(BN_asc2bn(&bn, "0"))
1783 || !TEST_BN_eq_zero(bn)
1784 || !TEST_BN_ge_zero(bn))
1787 if (!TEST_true(BN_asc2bn(&bn, "256"))
1788 || !TEST_BN_eq_word(bn, 256)
1789 || !TEST_BN_ge_zero(bn))
1792 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1793 || !TEST_BN_abs_eq_word(bn, 42)
1794 || !TEST_BN_lt_zero(bn))
1797 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1798 || !TEST_BN_eq_word(bn, 0x1234)
1799 || !TEST_BN_ge_zero(bn))
1802 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1803 || !TEST_BN_eq_word(bn, 0x1234)
1804 || !TEST_BN_ge_zero(bn))
1807 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1808 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1809 || !TEST_BN_lt_zero(bn))
1812 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1813 || !TEST_BN_eq_zero(bn)
1814 || !TEST_BN_ge_zero(bn))
1817 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1818 || !TEST_BN_eq_word(bn, 123)
1819 || !TEST_BN_ge_zero(bn))
1828 static const MPITEST kMPITests[] = {
1829 {"0", "\x00\x00\x00\x00", 4},
1830 {"1", "\x00\x00\x00\x01\x01", 5},
1831 {"-1", "\x00\x00\x00\x01\x81", 5},
1832 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1833 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1834 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1837 static int test_mpi(int i)
1840 const MPITEST *test = &kMPITests[i];
1841 size_t mpi_len, mpi_len2;
1846 if (!TEST_ptr(bn = BN_new())
1847 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1849 mpi_len = BN_bn2mpi(bn, NULL);
1850 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1853 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1854 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1857 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1860 if (!TEST_BN_eq(bn, bn2)) {
1872 static int test_rand(void)
1877 if (!TEST_ptr(bn = BN_new()))
1880 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1881 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
1882 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
1883 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
1884 || !TEST_BN_eq_one(bn)
1885 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
1886 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
1887 || !TEST_BN_eq_one(bn)
1888 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
1889 || !TEST_BN_eq_word(bn, 3))
1898 static int test_negzero(void)
1900 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
1901 BIGNUM *numerator = NULL, *denominator = NULL;
1902 int consttime, st = 0;
1904 if (!TEST_ptr(a = BN_new())
1905 || !TEST_ptr(b = BN_new())
1906 || !TEST_ptr(c = BN_new())
1907 || !TEST_ptr(d = BN_new()))
1910 /* Test that BN_mul never gives negative zero. */
1911 if (!TEST_true(BN_set_word(a, 1)))
1913 BN_set_negative(a, 1);
1915 if (!TEST_true(BN_mul(c, a, b, ctx)))
1917 if (!TEST_BN_eq_zero(c)
1918 || !TEST_BN_ge_zero(c))
1921 for (consttime = 0; consttime < 2; consttime++) {
1922 if (!TEST_ptr(numerator = BN_new())
1923 || !TEST_ptr(denominator = BN_new()))
1926 BN_set_flags(numerator, BN_FLG_CONSTTIME);
1927 BN_set_flags(denominator, BN_FLG_CONSTTIME);
1929 /* Test that BN_div never gives negative zero in the quotient. */
1930 if (!TEST_true(BN_set_word(numerator, 1))
1931 || !TEST_true(BN_set_word(denominator, 2)))
1933 BN_set_negative(numerator, 1);
1934 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
1935 || !TEST_BN_eq_zero(a)
1936 || !TEST_BN_ge_zero(a))
1939 /* Test that BN_div never gives negative zero in the remainder. */
1940 if (!TEST_true(BN_set_word(denominator, 1))
1941 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
1942 || !TEST_BN_eq_zero(b)
1943 || !TEST_BN_ge_zero(b))
1946 BN_free(denominator);
1947 numerator = denominator = NULL;
1950 /* Test that BN_set_negative will not produce a negative zero. */
1952 BN_set_negative(a, 1);
1953 if (BN_is_negative(a))
1963 BN_free(denominator);
1967 static int test_badmod(void)
1969 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
1970 BN_MONT_CTX *mont = NULL;
1973 if (!TEST_ptr(a = BN_new())
1974 || !TEST_ptr(b = BN_new())
1975 || !TEST_ptr(zero = BN_new())
1976 || !TEST_ptr(mont = BN_MONT_CTX_new()))
1980 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
1984 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
1988 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
1992 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1997 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2002 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
2006 /* Some operations also may not be used with an even modulus. */
2007 if (!TEST_true(BN_set_word(b, 16)))
2010 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
2014 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2019 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2029 BN_MONT_CTX_free(mont);
2033 static int test_expmodzero(void)
2035 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
2038 if (!TEST_ptr(zero = BN_new())
2039 || !TEST_ptr(a = BN_new())
2040 || !TEST_ptr(r = BN_new()))
2044 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
2045 || !TEST_BN_eq_zero(r)
2046 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
2048 || !TEST_BN_eq_zero(r)
2049 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
2052 || !TEST_BN_eq_zero(r)
2053 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
2054 BN_value_one(), NULL, NULL))
2055 || !TEST_BN_eq_zero(r))
2066 static int test_expmodone(void)
2069 BIGNUM *r = BN_new();
2070 BIGNUM *a = BN_new();
2071 BIGNUM *p = BN_new();
2072 BIGNUM *m = BN_new();
2079 || !TEST_true(BN_set_word(a, 1))
2080 || !TEST_true(BN_set_word(p, 0))
2081 || !TEST_true(BN_set_word(m, 1)))
2084 /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
2085 for (i = 0; i < 2; i++) {
2086 if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
2087 || !TEST_BN_eq_zero(r)
2088 || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
2089 || !TEST_BN_eq_zero(r)
2090 || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
2091 || !TEST_BN_eq_zero(r)
2092 || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
2093 || !TEST_BN_eq_zero(r)
2094 || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
2095 || !TEST_BN_eq_zero(r)
2096 || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
2097 || !TEST_BN_eq_zero(r))
2099 /* Repeat for r = 1 ^ 0 mod -1 */
2101 BN_set_negative(m, 1);
2113 static int test_smallprime(void)
2115 static const int kBits = 10;
2119 if (!TEST_ptr(r = BN_new())
2120 || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
2122 || !TEST_int_eq(BN_num_bits(r), kBits))
2131 static int primes[] = { 2, 3, 5, 7, 17863 };
2133 static int test_is_prime(int i)
2139 if (!TEST_ptr(r = BN_new()))
2142 for (trial = 0; trial <= 1; ++trial) {
2143 if (!TEST_true(BN_set_word(r, primes[i]))
2144 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL),
2155 static int not_primes[] = { -1, 0, 1, 4 };
2157 static int test_not_prime(int i)
2163 if (!TEST_ptr(r = BN_new()))
2166 for (trial = 0; trial <= 1; ++trial) {
2167 if (!TEST_true(BN_set_word(r, not_primes[i]))
2168 || !TEST_false(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL)))
2178 static int file_test_run(STANZA *s)
2180 static const FILETEST filetests[] = {
2182 {"LShift1", file_lshift1},
2183 {"LShift", file_lshift},
2184 {"RShift", file_rshift},
2185 {"Square", file_square},
2186 {"Product", file_product},
2187 {"Quotient", file_quotient},
2188 {"ModMul", file_modmul},
2189 {"ModExp", file_modexp},
2191 {"ModSqrt", file_modsqrt},
2193 int numtests = OSSL_NELEM(filetests);
2194 const FILETEST *tp = filetests;
2196 for ( ; --numtests >= 0; tp++) {
2197 if (findattr(s, tp->name) != NULL) {
2199 TEST_info("%s:%d: Failed %s test",
2200 s->test_file, s->start, tp->name);
2206 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2210 static int run_file_tests(int i)
2213 char *testfile = test_get_argument(i);
2216 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2218 if (!test_start_file(s, testfile)) {
2223 /* Read test file. */
2224 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2225 if (s->numpairs == 0)
2227 if (!file_test_run(s))
2230 test_clearstanza(s);
2240 int setup_tests(void)
2242 int n = test_get_argument_count();
2244 if (!TEST_ptr(ctx = BN_CTX_new()))
2249 ADD_TEST(test_div_recip);
2251 ADD_TEST(test_modexp_mont5);
2252 ADD_TEST(test_kronecker);
2253 ADD_TEST(test_rand);
2254 ADD_TEST(test_bn2padded);
2255 ADD_TEST(test_dec2bn);
2256 ADD_TEST(test_hex2bn);
2257 ADD_TEST(test_asc2bn);
2258 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2259 ADD_TEST(test_negzero);
2260 ADD_TEST(test_badmod);
2261 ADD_TEST(test_expmodzero);
2262 ADD_TEST(test_expmodone);
2263 ADD_TEST(test_smallprime);
2264 ADD_TEST(test_swap);
2265 #ifndef OPENSSL_NO_EC2M
2266 ADD_TEST(test_gf2m_add);
2267 ADD_TEST(test_gf2m_mod);
2268 ADD_TEST(test_gf2m_mul);
2269 ADD_TEST(test_gf2m_sqr);
2270 ADD_TEST(test_gf2m_modinv);
2271 ADD_TEST(test_gf2m_moddiv);
2272 ADD_TEST(test_gf2m_modexp);
2273 ADD_TEST(test_gf2m_modsqrt);
2274 ADD_TEST(test_gf2m_modsolvequad);
2276 ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
2277 ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
2279 ADD_ALL_TESTS(run_file_tests, n);
2284 void cleanup_tests(void)