2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
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);
142 * Return a "random" flag for if a BN should be negated.
144 static int rand_neg(void)
146 static unsigned int neg = 0;
147 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
149 return sign[(neg++) % 8];
152 static int test_swap(void)
154 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
155 int top, cond, st = 0;
157 if (!TEST_ptr(a = BN_new())
158 || !TEST_ptr(b = BN_new())
159 || !TEST_ptr(c = BN_new())
160 || !TEST_ptr(d = BN_new()))
163 BN_bntest_rand(a, 1024, 1, 0);
164 BN_bntest_rand(b, 1024, 1, 0);
167 top = BN_num_bits(a) / BN_BITS2;
171 if (!equalBN("swap", a, d)
172 || !equalBN("swap", b, c))
175 /* conditional swap: true */
177 BN_consttime_swap(cond, a, b, top);
178 if (!equalBN("cswap true", a, c)
179 || !equalBN("cswap true", b, d))
182 /* conditional swap: false */
184 BN_consttime_swap(cond, a, b, top);
185 if (!equalBN("cswap false", a, c)
186 || !equalBN("cswap false", b, d))
189 /* same tests but checking flag swap */
190 BN_set_flags(a, BN_FLG_CONSTTIME);
193 if (!equalBN("swap, flags", a, d)
194 || !equalBN("swap, flags", b, c)
195 || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
196 || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
200 BN_consttime_swap(cond, a, b, top);
201 if (!equalBN("cswap true, flags", a, c)
202 || !equalBN("cswap true, flags", b, d)
203 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
204 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
208 BN_consttime_swap(cond, a, b, top);
209 if (!equalBN("cswap false, flags", a, c)
210 || !equalBN("cswap false, flags", b, d)
211 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
212 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
224 static int test_sub(void)
226 BIGNUM *a = NULL, *b = NULL, *c = NULL;
229 if (!TEST_ptr(a = BN_new())
230 || !TEST_ptr(b = BN_new())
231 || !TEST_ptr(c = BN_new()))
234 for (i = 0; i < NUM0 + NUM1; i++) {
236 BN_bntest_rand(a, 512, 0, 0);
238 if (!TEST_int_ne(BN_set_bit(a, i), 0))
242 BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
243 BN_set_negative(a, rand_neg());
244 BN_set_negative(b, rand_neg());
249 if (!TEST_BN_eq_zero(c))
260 static int test_div_recip(void)
262 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
263 BN_RECP_CTX *recp = NULL;
266 if (!TEST_ptr(a = BN_new())
267 || !TEST_ptr(b = BN_new())
268 || !TEST_ptr(c = BN_new())
269 || !TEST_ptr(d = BN_new())
270 || !TEST_ptr(e = BN_new())
271 || !TEST_ptr(recp = BN_RECP_CTX_new()))
274 for (i = 0; i < NUM0 + NUM1; i++) {
276 BN_bntest_rand(a, 400, 0, 0);
281 BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
282 BN_set_negative(a, rand_neg());
283 BN_set_negative(b, rand_neg());
284 BN_RECP_CTX_set(recp, b, ctx);
285 BN_div_recp(d, c, a, recp, ctx);
286 BN_mul(e, d, b, ctx);
289 if (!TEST_BN_eq_zero(d))
299 BN_RECP_CTX_free(recp);
303 static int test_mod(void)
305 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
308 if (!TEST_ptr(a = BN_new())
309 || !TEST_ptr(b = BN_new())
310 || !TEST_ptr(c = BN_new())
311 || !TEST_ptr(d = BN_new())
312 || !TEST_ptr(e = BN_new()))
315 BN_bntest_rand(a, 1024, 0, 0);
316 for (i = 0; i < NUM0; i++) {
317 BN_bntest_rand(b, 450 + i * 10, 0, 0);
318 BN_set_negative(a, rand_neg());
319 BN_set_negative(b, rand_neg());
320 BN_mod(c, a, b, ctx);
321 BN_div(d, e, a, b, ctx);
323 if (!TEST_BN_eq_zero(e))
336 static const char *bn1strings[] = {
337 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
338 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
339 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
340 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
341 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
342 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
343 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
344 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
345 "0000000000000000000000000000000000000000000000000000000000000000",
346 "0000000000000000000000000000000000000000000000000000000000000000",
347 "0000000000000000000000000000000000000000000000000000000000000000",
348 "0000000000000000000000000000000000000000000000000000000000000000",
349 "0000000000000000000000000000000000000000000000000000000000000000",
350 "0000000000000000000000000000000000000000000000000000000000000000",
351 "0000000000000000000000000000000000000000000000000000000000000000",
352 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
356 static const char *bn2strings[] = {
357 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
358 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
359 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
360 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
361 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
362 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
363 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
364 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
365 "0000000000000000000000000000000000000000000000000000000000000000",
366 "0000000000000000000000000000000000000000000000000000000000000000",
367 "0000000000000000000000000000000000000000000000000000000000000000",
368 "0000000000000000000000000000000000000000000000000000000000000000",
369 "0000000000000000000000000000000000000000000000000000000000000000",
370 "0000000000000000000000000000000000000000000000000000000000000000",
371 "0000000000000000000000000000000000000000000000000000000000000000",
372 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
377 * Test constant-time modular exponentiation with 1024-bit inputs, which on
378 * x86_64 cause a different code branch to be taken.
380 static int test_modexp_mont5(void)
382 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
383 BIGNUM *b = NULL, *n = NULL, *c = NULL;
384 BN_MONT_CTX *mont = NULL;
387 if (!TEST_ptr(a = BN_new())
388 || !TEST_ptr(p = BN_new())
389 || !TEST_ptr(m = BN_new())
390 || !TEST_ptr(d = BN_new())
391 || !TEST_ptr(e = BN_new())
392 || !TEST_ptr(b = BN_new())
393 || !TEST_ptr(n = BN_new())
394 || !TEST_ptr(c = BN_new())
395 || !TEST_ptr(mont = BN_MONT_CTX_new()))
398 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
400 BN_bntest_rand(a, 1024, 0, 0);
402 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
404 if (!TEST_BN_eq_one(d))
407 /* Regression test for carry bug in mulx4x_mont */
409 "7878787878787878787878787878787878787878787878787878787878787878"
410 "7878787878787878787878787878787878787878787878787878787878787878"
411 "7878787878787878787878787878787878787878787878787878787878787878"
412 "7878787878787878787878787878787878787878787878787878787878787878");
414 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
415 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
416 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
417 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
419 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
420 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
421 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
422 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
423 BN_MONT_CTX_set(mont, n, ctx);
424 BN_mod_mul_montgomery(c, a, b, mont, ctx);
425 BN_mod_mul_montgomery(d, b, a, mont, ctx);
426 if (!TEST_BN_eq(c, d))
429 /* Regression test for carry bug in sqr[x]8x_mont */
430 parse_bigBN(&n, bn1strings);
431 parse_bigBN(&a, bn2strings);
434 BN_MONT_CTX_set(mont, n, ctx);
435 BN_mod_mul_montgomery(c, a, a, mont, ctx);
436 BN_mod_mul_montgomery(d, a, b, mont, ctx);
437 if (!TEST_BN_eq(c, d))
440 /* Regression test for carry bug in bn_sqrx8x_internal */
442 static const char *ahex[] = {
443 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
444 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
445 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
446 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
447 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
448 "9544D954000000006C0000000000000000000000000000000000000000000000",
449 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
450 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
451 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
452 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
453 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
454 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
457 static const char *nhex[] = {
458 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
459 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
460 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
461 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
462 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
463 "00000010000000006C0000000000000000000000000000000000000000000000",
464 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
465 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
466 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
467 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
468 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
469 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
473 parse_bigBN(&a, ahex);
474 parse_bigBN(&n, nhex);
478 BN_MONT_CTX_set(mont, n, ctx);
479 if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
480 || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
481 || !TEST_BN_eq(c, d))
484 /* Regression test for bug in BN_from_montgomery_word */
486 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
487 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
488 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
490 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
491 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
492 BN_MONT_CTX_set(mont, n, ctx);
493 if (!TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx)))
496 /* Regression test for bug in rsaz_1024_mul_avx2 */
498 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
499 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
500 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
501 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
503 "2020202020202020202020202020202020202020202020202020202020202020"
504 "2020202020202020202020202020202020202020202020202020202020202020"
505 "20202020202020FF202020202020202020202020202020202020202020202020"
506 "2020202020202020202020202020202020202020202020202020202020202020");
508 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
509 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
510 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
511 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF");
512 BN_MONT_CTX_set(mont, n, ctx);
513 BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont);
514 BN_mod_exp_mont(d, a, b, n, ctx, mont);
515 if (!TEST_BN_eq(c, d))
519 * rsaz_1024_mul_avx2 expects fully-reduced inputs.
520 * BN_mod_exp_mont_consttime should reduce the input first.
523 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
524 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
525 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
526 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
528 "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
529 "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
530 "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
531 "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E");
533 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
534 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
535 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
536 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
537 BN_MONT_CTX_set(mont, n, ctx);
538 BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont);
540 if (!TEST_BN_eq(c, d))
544 BN_bntest_rand(p, 1024, 0, 0);
546 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
547 || !TEST_BN_eq_zero(d))
551 * Craft an input whose Montgomery representation is 1, i.e., shorter
552 * than the modulus m, in order to test the const time precomputation
553 * scattering/gathering.
556 BN_MONT_CTX_set(mont, m, ctx);
557 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
558 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
559 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
560 || !TEST_BN_eq(a, d))
563 /* Finally, some regular test vectors. */
564 BN_bntest_rand(e, 1024, 0, 0);
565 if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
566 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
567 || !TEST_BN_eq(a, d))
573 BN_MONT_CTX_free(mont);
585 #ifndef OPENSSL_NO_EC2M
586 static int test_gf2m_add(void)
588 BIGNUM *a = NULL, *b = NULL, *c = NULL;
591 if (!TEST_ptr(a = BN_new())
592 || !TEST_ptr(b = BN_new())
593 || !TEST_ptr(c = BN_new()))
596 for (i = 0; i < NUM0; i++) {
597 BN_rand(a, 512, 0, 0);
598 BN_copy(b, BN_value_one());
599 BN_set_negative(a, rand_neg());
600 BN_set_negative(b, rand_neg());
601 BN_GF2m_add(c, a, b);
602 /* Test that two added values have the correct parity. */
603 if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
604 || (!BN_is_odd(a) && !BN_is_odd(c))))
606 BN_GF2m_add(c, c, c);
607 /* Test that c + c = 0. */
608 if (!TEST_BN_eq_zero(c))
619 static int test_gf2m_mod(void)
621 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
624 if (!TEST_ptr(a = BN_new())
625 || !TEST_ptr(b[0] = BN_new())
626 || !TEST_ptr(b[1] = BN_new())
627 || !TEST_ptr(c = BN_new())
628 || !TEST_ptr(d = BN_new())
629 || !TEST_ptr(e = BN_new()))
632 BN_GF2m_arr2poly(p0, b[0]);
633 BN_GF2m_arr2poly(p1, b[1]);
635 for (i = 0; i < NUM0; i++) {
636 BN_bntest_rand(a, 1024, 0, 0);
637 for (j = 0; j < 2; j++) {
638 BN_GF2m_mod(c, a, b[j]);
639 BN_GF2m_add(d, a, c);
640 BN_GF2m_mod(e, d, b[j]);
641 /* Test that a + (a mod p) mod p == 0. */
642 if (!TEST_BN_eq_zero(e))
657 static int test_gf2m_mul(void)
659 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
660 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
663 if (!TEST_ptr(a = BN_new())
664 || !TEST_ptr(b[0] = BN_new())
665 || !TEST_ptr(b[1] = BN_new())
666 || !TEST_ptr(c = BN_new())
667 || !TEST_ptr(d = BN_new())
668 || !TEST_ptr(e = BN_new())
669 || !TEST_ptr(f = BN_new())
670 || !TEST_ptr(g = BN_new())
671 || !TEST_ptr(h = BN_new()))
674 BN_GF2m_arr2poly(p0, b[0]);
675 BN_GF2m_arr2poly(p1, b[1]);
677 for (i = 0; i < NUM0; i++) {
678 BN_bntest_rand(a, 1024, 0, 0);
679 BN_bntest_rand(c, 1024, 0, 0);
680 BN_bntest_rand(d, 1024, 0, 0);
681 for (j = 0; j < 2; j++) {
682 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
683 BN_GF2m_add(f, a, d);
684 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
685 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
686 BN_GF2m_add(f, e, g);
687 BN_GF2m_add(f, f, h);
688 /* Test that (a+d)*c = a*c + d*c. */
689 if (!TEST_BN_eq_zero(f))
708 static int test_gf2m_sqr(void)
710 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
713 if (!TEST_ptr(a = BN_new())
714 || !TEST_ptr(b[0] = BN_new())
715 || !TEST_ptr(b[1] = BN_new())
716 || !TEST_ptr(c = BN_new())
717 || !TEST_ptr(d = BN_new()))
720 BN_GF2m_arr2poly(p0, b[0]);
721 BN_GF2m_arr2poly(p1, b[1]);
723 for (i = 0; i < NUM0; i++) {
724 BN_bntest_rand(a, 1024, 0, 0);
725 for (j = 0; j < 2; j++) {
726 BN_GF2m_mod_sqr(c, a, b[j], ctx);
728 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
729 BN_GF2m_add(d, c, d);
730 /* Test that a*a = a^2. */
731 if (!TEST_BN_eq_zero(d))
745 static int test_gf2m_modinv(void)
747 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
750 if (!TEST_ptr(a = BN_new())
751 || !TEST_ptr(b[0] = BN_new())
752 || !TEST_ptr(b[1] = BN_new())
753 || !TEST_ptr(c = BN_new())
754 || !TEST_ptr(d = BN_new()))
757 BN_GF2m_arr2poly(p0, b[0]);
758 BN_GF2m_arr2poly(p1, b[1]);
760 for (i = 0; i < NUM0; i++) {
761 BN_bntest_rand(a, 512, 0, 0);
762 for (j = 0; j < 2; j++) {
763 BN_GF2m_mod_inv(c, a, b[j], ctx);
764 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
765 /* Test that ((1/a)*a) = 1. */
766 if (!TEST_BN_eq_one(d))
780 static int test_gf2m_moddiv(void)
782 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
783 BIGNUM *e = NULL, *f = NULL;
786 if (!TEST_ptr(a = BN_new())
787 || !TEST_ptr(b[0] = BN_new())
788 || !TEST_ptr(b[1] = BN_new())
789 || !TEST_ptr(c = BN_new())
790 || !TEST_ptr(d = BN_new())
791 || !TEST_ptr(e = BN_new())
792 || !TEST_ptr(f = BN_new()))
795 BN_GF2m_arr2poly(p0, b[0]);
796 BN_GF2m_arr2poly(p1, b[1]);
798 for (i = 0; i < NUM0; i++) {
799 BN_bntest_rand(a, 512, 0, 0);
800 BN_bntest_rand(c, 512, 0, 0);
801 for (j = 0; j < 2; j++) {
802 BN_GF2m_mod_div(d, a, c, b[j], ctx);
803 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
804 BN_GF2m_mod_div(f, a, e, b[j], ctx);
805 /* Test that ((a/c)*c)/a = 1. */
806 if (!TEST_BN_eq_one(f))
822 static int test_gf2m_modexp(void)
824 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
825 BIGNUM *e = NULL, *f = NULL;
828 if (!TEST_ptr(a = BN_new())
829 || !TEST_ptr(b[0] = BN_new())
830 || !TEST_ptr(b[1] = BN_new())
831 || !TEST_ptr(c = BN_new())
832 || !TEST_ptr(d = BN_new())
833 || !TEST_ptr(e = BN_new())
834 || !TEST_ptr(f = BN_new()))
837 BN_GF2m_arr2poly(p0, b[0]);
838 BN_GF2m_arr2poly(p1, b[1]);
840 for (i = 0; i < NUM0; i++) {
841 BN_bntest_rand(a, 512, 0, 0);
842 BN_bntest_rand(c, 512, 0, 0);
843 BN_bntest_rand(d, 512, 0, 0);
844 for (j = 0; j < 2; j++) {
845 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
846 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
847 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
849 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
850 BN_GF2m_add(f, e, f);
851 /* Test that a^(c+d)=a^c*a^d. */
852 if (!TEST_BN_eq_zero(f))
868 static int test_gf2m_modsqrt(void)
870 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
871 BIGNUM *e = NULL, *f = NULL;
874 if (!TEST_ptr(a = BN_new())
875 || !TEST_ptr(b[0] = BN_new())
876 || !TEST_ptr(b[1] = BN_new())
877 || !TEST_ptr(c = BN_new())
878 || !TEST_ptr(d = BN_new())
879 || !TEST_ptr(e = BN_new())
880 || !TEST_ptr(f = BN_new()))
883 BN_GF2m_arr2poly(p0, b[0]);
884 BN_GF2m_arr2poly(p1, b[1]);
886 for (i = 0; i < NUM0; i++) {
887 BN_bntest_rand(a, 512, 0, 0);
888 for (j = 0; j < 2; j++) {
889 BN_GF2m_mod(c, a, b[j]);
890 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
891 BN_GF2m_mod_sqr(e, d, b[j], ctx);
892 BN_GF2m_add(f, c, e);
893 /* Test that d^2 = a, where d = sqrt(a). */
894 if (!TEST_BN_eq_zero(f))
910 static int test_gf2m_modsolvequad(void)
912 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
914 int i, j, s = 0, t, st = 0;
916 if (!TEST_ptr(a = BN_new())
917 || !TEST_ptr(b[0] = BN_new())
918 || !TEST_ptr(b[1] = BN_new())
919 || !TEST_ptr(c = BN_new())
920 || !TEST_ptr(d = BN_new())
921 || !TEST_ptr(e = BN_new()))
924 BN_GF2m_arr2poly(p0, b[0]);
925 BN_GF2m_arr2poly(p1, b[1]);
927 for (i = 0; i < NUM0; i++) {
928 BN_bntest_rand(a, 512, 0, 0);
929 for (j = 0; j < 2; j++) {
930 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
933 BN_GF2m_mod_sqr(d, c, b[j], ctx);
934 BN_GF2m_add(d, c, d);
935 BN_GF2m_mod(e, a, b[j]);
936 BN_GF2m_add(e, e, d);
938 * Test that solution of quadratic c satisfies c^2 + c = a.
940 if (!TEST_BN_eq_zero(e))
945 if (!TEST_int_ge(s, 0)) {
946 TEST_info("%d tests found no roots; probably an error", NUM0);
961 static int test_kronecker(void)
963 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
964 int i, legendre, kronecker, st = 0;
966 if (!TEST_ptr(a = BN_new())
967 || !TEST_ptr(b = BN_new())
968 || !TEST_ptr(r = BN_new())
969 || !TEST_ptr(t = BN_new()))
973 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
974 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
975 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
976 * generate a random prime b and compare these values for a number of
977 * random a's. (That is, we run the Solovay-Strassen primality test to
978 * confirm that b is prime, except that we don't want to test whether b
979 * is prime but whether BN_kronecker works.)
982 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
984 BN_set_negative(b, rand_neg());
986 for (i = 0; i < NUM0; i++) {
987 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
989 BN_set_negative(a, rand_neg());
991 /* t := (|b|-1)/2 (note that b is odd) */
992 if (!TEST_true(BN_copy(t, b)))
994 BN_set_negative(t, 0);
995 if (!TEST_true(BN_sub_word(t, 1)))
997 if (!TEST_true(BN_rshift1(t, t)))
1000 BN_set_negative(b, 0);
1002 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
1004 BN_set_negative(b, 1);
1006 if (BN_is_word(r, 1))
1008 else if (BN_is_zero(r))
1011 if (!TEST_true(BN_add_word(r, 1)))
1013 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
1014 TEST_info("Legendre symbol computation failed");
1020 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
1022 /* we actually need BN_kronecker(a, |b|) */
1023 if (BN_is_negative(a) && BN_is_negative(b))
1024 kronecker = -kronecker;
1026 if (!TEST_int_eq(legendre, kronecker))
1039 static int file_sum(STANZA *s)
1041 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
1045 if (!TEST_ptr(a = getBN(s, "A"))
1046 || !TEST_ptr(b = getBN(s, "B"))
1047 || !TEST_ptr(sum = getBN(s, "Sum"))
1048 || !TEST_ptr(ret = BN_new()))
1051 if (!TEST_true(BN_add(ret, a, b))
1052 || !equalBN("A + B", sum, ret)
1053 || !TEST_true(BN_sub(ret, sum, a))
1054 || !equalBN("Sum - A", b, ret)
1055 || !TEST_true(BN_sub(ret, sum, b))
1056 || !equalBN("Sum - B", a, ret))
1060 * Test that the functions work when |r| and |a| point to the same BIGNUM,
1061 * or when |r| and |b| point to the same BIGNUM.
1062 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1064 if (!TEST_true(BN_copy(ret, a))
1065 || !TEST_true(BN_add(ret, ret, b))
1066 || !equalBN("A + B (r is a)", sum, ret)
1067 || !TEST_true(BN_copy(ret, b))
1068 || !TEST_true(BN_add(ret, a, ret))
1069 || !equalBN("A + B (r is b)", sum, ret)
1070 || !TEST_true(BN_copy(ret, sum))
1071 || !TEST_true(BN_sub(ret, ret, a))
1072 || !equalBN("Sum - A (r is a)", b, ret)
1073 || !TEST_true(BN_copy(ret, a))
1074 || !TEST_true(BN_sub(ret, sum, ret))
1075 || !equalBN("Sum - A (r is b)", b, ret)
1076 || !TEST_true(BN_copy(ret, sum))
1077 || !TEST_true(BN_sub(ret, ret, b))
1078 || !equalBN("Sum - B (r is a)", a, ret)
1079 || !TEST_true(BN_copy(ret, b))
1080 || !TEST_true(BN_sub(ret, sum, ret))
1081 || !equalBN("Sum - B (r is b)", a, ret))
1085 * Test BN_uadd() and BN_usub() with the prerequisites they are
1086 * documented as having. Note that these functions are frequently used
1087 * when the prerequisites don't hold. In those cases, they are supposed
1088 * to work as if the prerequisite hold, but we don't test that yet.
1091 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
1092 if (!TEST_true(BN_uadd(ret, a, b))
1093 || !equalBN("A +u B", sum, ret)
1094 || !TEST_true(BN_usub(ret, sum, a))
1095 || !equalBN("Sum -u A", b, ret)
1096 || !TEST_true(BN_usub(ret, sum, b))
1097 || !equalBN("Sum -u B", a, ret))
1100 * Test that the functions work when |r| and |a| point to the same
1101 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1102 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1104 if (!TEST_true(BN_copy(ret, a))
1105 || !TEST_true(BN_uadd(ret, ret, b))
1106 || !equalBN("A +u B (r is a)", sum, ret)
1107 || !TEST_true(BN_copy(ret, b))
1108 || !TEST_true(BN_uadd(ret, a, ret))
1109 || !equalBN("A +u B (r is b)", sum, ret)
1110 || !TEST_true(BN_copy(ret, sum))
1111 || !TEST_true(BN_usub(ret, ret, a))
1112 || !equalBN("Sum -u A (r is a)", b, ret)
1113 || !TEST_true(BN_copy(ret, a))
1114 || !TEST_true(BN_usub(ret, sum, ret))
1115 || !equalBN("Sum -u A (r is b)", b, ret)
1116 || !TEST_true(BN_copy(ret, sum))
1117 || !TEST_true(BN_usub(ret, ret, b))
1118 || !equalBN("Sum -u B (r is a)", a, ret)
1119 || !TEST_true(BN_copy(ret, b))
1120 || !TEST_true(BN_usub(ret, sum, ret))
1121 || !equalBN("Sum -u B (r is b)", a, ret))
1126 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1128 b_word = BN_get_word(b);
1129 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1130 if (!TEST_true(BN_copy(ret, a))
1131 || !TEST_true(BN_add_word(ret, b_word))
1132 || !equalBN("A + B (word)", sum, ret)
1133 || !TEST_true(BN_copy(ret, sum))
1134 || !TEST_true(BN_sub_word(ret, b_word))
1135 || !equalBN("Sum - B (word)", a, ret))
1148 static int file_lshift1(STANZA *s)
1150 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1151 BIGNUM *two = NULL, *remainder = NULL;
1154 if (!TEST_ptr(a = getBN(s, "A"))
1155 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1156 || !TEST_ptr(zero = BN_new())
1157 || !TEST_ptr(ret = BN_new())
1158 || !TEST_ptr(two = BN_new())
1159 || !TEST_ptr(remainder = BN_new()))
1164 if (!TEST_true(BN_set_word(two, 2))
1165 || !TEST_true(BN_add(ret, a, a))
1166 || !equalBN("A + A", lshift1, ret)
1167 || !TEST_true(BN_mul(ret, a, two, ctx))
1168 || !equalBN("A * 2", lshift1, ret)
1169 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1170 || !equalBN("LShift1 / 2", a, ret)
1171 || !equalBN("LShift1 % 2", zero, remainder)
1172 || !TEST_true(BN_lshift1(ret, a))
1173 || !equalBN("A << 1", lshift1, ret)
1174 || !TEST_true(BN_rshift1(ret, lshift1))
1175 || !equalBN("LShift >> 1", a, ret)
1176 || !TEST_true(BN_rshift1(ret, lshift1))
1177 || !equalBN("LShift >> 1", a, ret))
1180 /* Set the LSB to 1 and test rshift1 again. */
1181 if (!TEST_true(BN_set_bit(lshift1, 0))
1182 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1183 || !equalBN("(LShift1 | 1) / 2", a, ret)
1184 || !TEST_true(BN_rshift1(ret, lshift1))
1185 || !equalBN("(LShift | 1) >> 1", a, ret))
1200 static int file_lshift(STANZA *s)
1202 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1205 if (!TEST_ptr(a = getBN(s, "A"))
1206 || !TEST_ptr(lshift = getBN(s, "LShift"))
1207 || !TEST_ptr(ret = BN_new())
1208 || !getint(s, &n, "N"))
1211 if (!TEST_true(BN_lshift(ret, a, n))
1212 || !equalBN("A << N", lshift, ret)
1213 || !TEST_true(BN_rshift(ret, lshift, n))
1214 || !equalBN("A >> N", a, ret))
1225 static int file_rshift(STANZA *s)
1227 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1230 if (!TEST_ptr(a = getBN(s, "A"))
1231 || !TEST_ptr(rshift = getBN(s, "RShift"))
1232 || !TEST_ptr(ret = BN_new())
1233 || !getint(s, &n, "N"))
1236 if (!TEST_true(BN_rshift(ret, a, n))
1237 || !equalBN("A >> N", rshift, ret))
1240 /* If N == 1, try with rshift1 as well */
1242 if (!TEST_true(BN_rshift1(ret, a))
1243 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1255 static int file_square(STANZA *s)
1257 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1258 BIGNUM *remainder = NULL, *tmp = NULL;
1261 if (!TEST_ptr(a = getBN(s, "A"))
1262 || !TEST_ptr(square = getBN(s, "Square"))
1263 || !TEST_ptr(zero = BN_new())
1264 || !TEST_ptr(ret = BN_new())
1265 || !TEST_ptr(remainder = BN_new()))
1269 if (!TEST_true(BN_sqr(ret, a, ctx))
1270 || !equalBN("A^2", square, ret)
1271 || !TEST_true(BN_mul(ret, a, a, ctx))
1272 || !equalBN("A * A", square, ret)
1273 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1274 || !equalBN("Square / A", a, ret)
1275 || !equalBN("Square % A", zero, remainder))
1279 BN_set_negative(a, 0);
1280 if (!TEST_true(BN_sqrt(ret, square, ctx))
1281 || !equalBN("sqrt(Square)", a, ret))
1284 /* BN_sqrt should fail on non-squares and negative numbers. */
1285 if (!TEST_BN_eq_zero(square)) {
1286 if (!TEST_ptr(tmp = BN_new())
1287 || !TEST_true(BN_copy(tmp, square)))
1289 BN_set_negative(tmp, 1);
1291 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1295 BN_set_negative(tmp, 0);
1296 if (BN_add(tmp, tmp, BN_value_one()))
1298 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1315 static int file_product(STANZA *s)
1317 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1318 BIGNUM *remainder = NULL, *zero = NULL;
1321 if (!TEST_ptr(a = getBN(s, "A"))
1322 || !TEST_ptr(b = getBN(s, "B"))
1323 || !TEST_ptr(product = getBN(s, "Product"))
1324 || !TEST_ptr(ret = BN_new())
1325 || !TEST_ptr(remainder = BN_new())
1326 || !TEST_ptr(zero = BN_new()))
1331 if (!TEST_true(BN_mul(ret, a, b, ctx))
1332 || !equalBN("A * B", product, ret)
1333 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1334 || !equalBN("Product / A", b, ret)
1335 || !equalBN("Product % A", zero, remainder)
1336 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1337 || !equalBN("Product / B", a, ret)
1338 || !equalBN("Product % B", zero, remainder))
1352 static int file_quotient(STANZA *s)
1354 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1355 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1356 BN_ULONG b_word, ret_word;
1359 if (!TEST_ptr(a = getBN(s, "A"))
1360 || !TEST_ptr(b = getBN(s, "B"))
1361 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1362 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1363 || !TEST_ptr(ret = BN_new())
1364 || !TEST_ptr(ret2 = BN_new())
1365 || !TEST_ptr(nnmod = BN_new()))
1368 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1369 || !equalBN("A / B", quotient, ret)
1370 || !equalBN("A % B", remainder, ret2)
1371 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1372 || !TEST_true(BN_add(ret, ret, remainder))
1373 || !equalBN("Quotient * B + Remainder", a, ret))
1377 * Test with BN_mod_word() and BN_div_word() if the divisor is
1380 b_word = BN_get_word(b);
1381 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1382 BN_ULONG remainder_word = BN_get_word(remainder);
1384 assert(remainder_word != (BN_ULONG)-1);
1385 if (!TEST_ptr(BN_copy(ret, a)))
1387 ret_word = BN_div_word(ret, b_word);
1388 if (ret_word != remainder_word) {
1391 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1392 ret_word, remainder_word);
1394 TEST_error("Got A %% B (word) mismatch");
1398 if (!equalBN ("A / B (word)", quotient, ret))
1401 ret_word = BN_mod_word(a, b_word);
1402 if (ret_word != remainder_word) {
1405 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1406 ret_word, remainder_word);
1408 TEST_error("Got A %% B (word) mismatch");
1414 /* Test BN_nnmod. */
1415 if (!BN_is_negative(b)) {
1416 if (!TEST_true(BN_copy(nnmod, remainder))
1417 || (BN_is_negative(nnmod)
1418 && !TEST_true(BN_add(nnmod, nnmod, b)))
1419 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1420 || !equalBN("A % B (non-negative)", nnmod, ret))
1436 static int file_modmul(STANZA *s)
1438 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1441 if (!TEST_ptr(a = getBN(s, "A"))
1442 || !TEST_ptr(b = getBN(s, "B"))
1443 || !TEST_ptr(m = getBN(s, "M"))
1444 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1445 || !TEST_ptr(ret = BN_new()))
1448 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1449 || !equalBN("A * B (mod M)", mod_mul, ret))
1453 /* Reduce |a| and |b| and test the Montgomery version. */
1454 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1455 BIGNUM *a_tmp = BN_new();
1456 BIGNUM *b_tmp = BN_new();
1458 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1459 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1460 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1461 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1462 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1463 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1464 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1466 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1467 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1471 BN_MONT_CTX_free(mont);
1488 static int file_modexp(STANZA *s)
1490 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1491 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1494 if (!TEST_ptr(a = getBN(s, "A"))
1495 || !TEST_ptr(e = getBN(s, "E"))
1496 || !TEST_ptr(m = getBN(s, "M"))
1497 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1498 || !TEST_ptr(ret = BN_new())
1499 || !TEST_ptr(d = BN_new()))
1502 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1503 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1507 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1508 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1509 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1511 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1515 /* Regression test for carry propagation bug in sqr8x_reduction */
1516 BN_hex2bn(&a, "050505050505");
1517 BN_hex2bn(&b, "02");
1519 "4141414141414141414141274141414141414141414141414141414141414141"
1520 "4141414141414141414141414141414141414141414141414141414141414141"
1521 "4141414141414141414141800000000000000000000000000000000000000000"
1522 "0000000000000000000000000000000000000000000000000000000000000000"
1523 "0000000000000000000000000000000000000000000000000000000000000000"
1524 "0000000000000000000000000000000000000000000000000000000001");
1525 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1526 || !TEST_true(BN_mul(e, a, a, ctx))
1527 || !TEST_BN_eq(d, e))
1543 static int file_exp(STANZA *s)
1545 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1548 if (!TEST_ptr(a = getBN(s, "A"))
1549 || !TEST_ptr(e = getBN(s, "E"))
1550 || !TEST_ptr(exp = getBN(s, "Exp"))
1551 || !TEST_ptr(ret = BN_new()))
1554 if (!TEST_true(BN_exp(ret, a, e, ctx))
1555 || !equalBN("A ^ E", exp, ret))
1567 static int file_modsqrt(STANZA *s)
1569 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1572 if (!TEST_ptr(a = getBN(s, "A"))
1573 || !TEST_ptr(p = getBN(s, "P"))
1574 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1575 || !TEST_ptr(ret = BN_new())
1576 || !TEST_ptr(ret2 = BN_new()))
1579 /* There are two possible answers. */
1580 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1581 || !TEST_true(BN_sub(ret2, p, ret)))
1584 /* The first condition should NOT be a test. */
1585 if (BN_cmp(ret2, mod_sqrt) != 0
1586 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1599 static int test_bn2padded(void)
1602 uint8_t zeros[256], out[256], reference[128];
1603 BIGNUM *n = BN_new();
1606 /* Test edge case at 0. */
1609 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1611 memset(out, -1, sizeof(out));
1612 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1614 memset(zeros, 0, sizeof(zeros));
1615 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1618 /* Test a random numbers at various byte lengths. */
1619 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1620 # define TOP_BIT_ON 0
1621 # define BOTTOM_BIT_NOTOUCH 0
1622 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1624 if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1625 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1627 /* Empty buffer should fail. */
1628 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1630 /* One byte short should fail. */
1631 if (BN_bn2bin_padded(out, bytes - 1, n))
1633 /* Exactly right size should encode. */
1634 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1635 || TEST_mem_eq(out, bytes, reference, bytes))
1637 /* Pad up one byte extra. */
1638 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1639 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1640 || !TEST_mem_eq(out, 1, zeros, 1))
1642 /* Pad up to 256. */
1643 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1644 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1646 || !TEST_mem_eq(out, sizseof(out) - bytes,
1647 zeros, sizeof(out) - bytes))
1660 static int test_dec2bn(void)
1665 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1666 || !TEST_BN_eq_word(bn, 0)
1667 || !TEST_BN_eq_zero(bn)
1668 || !TEST_BN_le_zero(bn)
1669 || !TEST_BN_ge_zero(bn)
1670 || !TEST_BN_even(bn))
1675 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1676 || !TEST_BN_eq_word(bn, 256)
1677 || !TEST_BN_ge_zero(bn)
1678 || !TEST_BN_gt_zero(bn)
1679 || !TEST_BN_ne_zero(bn)
1680 || !TEST_BN_even(bn))
1685 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1686 || !TEST_BN_abs_eq_word(bn, 42)
1687 || !TEST_BN_lt_zero(bn)
1688 || !TEST_BN_le_zero(bn)
1689 || !TEST_BN_ne_zero(bn)
1690 || !TEST_BN_even(bn))
1695 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1696 || !TEST_BN_eq_word(bn, 1)
1697 || !TEST_BN_ne_zero(bn)
1698 || !TEST_BN_gt_zero(bn)
1699 || !TEST_BN_ge_zero(bn)
1700 || !TEST_BN_eq_one(bn)
1701 || !TEST_BN_odd(bn))
1706 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1707 || !TEST_BN_eq_zero(bn)
1708 || !TEST_BN_ge_zero(bn)
1709 || !TEST_BN_le_zero(bn)
1710 || !TEST_BN_even(bn))
1715 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1716 || !TEST_BN_abs_eq_word(bn, 42)
1717 || !TEST_BN_ge_zero(bn)
1718 || !TEST_BN_gt_zero(bn)
1719 || !TEST_BN_ne_zero(bn)
1720 || !TEST_BN_even(bn))
1729 static int test_hex2bn(void)
1734 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1735 || !TEST_BN_eq_zero(bn)
1736 || !TEST_BN_ge_zero(bn)
1737 || !TEST_BN_even(bn))
1742 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1743 || !TEST_BN_eq_word(bn, 0x256)
1744 || !TEST_BN_ge_zero(bn)
1745 || !TEST_BN_gt_zero(bn)
1746 || !TEST_BN_ne_zero(bn)
1747 || !TEST_BN_even(bn))
1752 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1753 || !TEST_BN_abs_eq_word(bn, 0x42)
1754 || !TEST_BN_lt_zero(bn)
1755 || !TEST_BN_le_zero(bn)
1756 || !TEST_BN_ne_zero(bn)
1757 || !TEST_BN_even(bn))
1762 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1763 || !TEST_BN_eq_word(bn, 0xCB)
1764 || !TEST_BN_ge_zero(bn)
1765 || !TEST_BN_gt_zero(bn)
1766 || !TEST_BN_ne_zero(bn)
1767 || !TEST_BN_odd(bn))
1772 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1773 || !TEST_BN_eq_zero(bn)
1774 || !TEST_BN_ge_zero(bn)
1775 || !TEST_BN_le_zero(bn)
1776 || !TEST_BN_even(bn))
1781 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1782 || !TEST_BN_eq_word(bn, 0xabc)
1783 || !TEST_BN_ge_zero(bn)
1784 || !TEST_BN_gt_zero(bn)
1785 || !TEST_BN_ne_zero(bn)
1786 || !TEST_BN_even(bn))
1795 static int test_asc2bn(void)
1800 if (!TEST_ptr(bn = BN_new()))
1803 if (!TEST_true(BN_asc2bn(&bn, "0"))
1804 || !TEST_BN_eq_zero(bn)
1805 || !TEST_BN_ge_zero(bn))
1808 if (!TEST_true(BN_asc2bn(&bn, "256"))
1809 || !TEST_BN_eq_word(bn, 256)
1810 || !TEST_BN_ge_zero(bn))
1813 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1814 || !TEST_BN_abs_eq_word(bn, 42)
1815 || !TEST_BN_lt_zero(bn))
1818 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1819 || !TEST_BN_eq_word(bn, 0x1234)
1820 || !TEST_BN_ge_zero(bn))
1823 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1824 || !TEST_BN_eq_word(bn, 0x1234)
1825 || !TEST_BN_ge_zero(bn))
1828 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1829 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1830 || !TEST_BN_lt_zero(bn))
1833 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1834 || !TEST_BN_eq_zero(bn)
1835 || !TEST_BN_ge_zero(bn))
1838 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1839 || !TEST_BN_eq_word(bn, 123)
1840 || !TEST_BN_ge_zero(bn))
1849 static const MPITEST kMPITests[] = {
1850 {"0", "\x00\x00\x00\x00", 4},
1851 {"1", "\x00\x00\x00\x01\x01", 5},
1852 {"-1", "\x00\x00\x00\x01\x81", 5},
1853 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1854 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1855 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1858 static int test_mpi(int i)
1861 const MPITEST *test = &kMPITests[i];
1862 size_t mpi_len, mpi_len2;
1867 if (!TEST_ptr(bn = BN_new())
1868 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1870 mpi_len = BN_bn2mpi(bn, NULL);
1871 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1874 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1875 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1878 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1881 if (!TEST_BN_eq(bn, bn2)) {
1893 static int test_rand(void)
1898 if (!TEST_ptr(bn = BN_new()))
1901 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1902 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
1903 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
1904 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
1905 || !TEST_BN_eq_one(bn)
1906 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
1907 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
1908 || !TEST_BN_eq_one(bn)
1909 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
1910 || !TEST_BN_eq_word(bn, 3))
1919 static int test_negzero(void)
1921 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
1922 BIGNUM *numerator = NULL, *denominator = NULL;
1923 int consttime, st = 0;
1925 if (!TEST_ptr(a = BN_new())
1926 || !TEST_ptr(b = BN_new())
1927 || !TEST_ptr(c = BN_new())
1928 || !TEST_ptr(d = BN_new()))
1931 /* Test that BN_mul never gives negative zero. */
1932 if (!TEST_true(BN_set_word(a, 1)))
1934 BN_set_negative(a, 1);
1936 if (!TEST_true(BN_mul(c, a, b, ctx)))
1938 if (!TEST_BN_eq_zero(c)
1939 || !TEST_BN_ge_zero(c))
1942 for (consttime = 0; consttime < 2; consttime++) {
1943 if (!TEST_ptr(numerator = BN_new())
1944 || !TEST_ptr(denominator = BN_new()))
1947 BN_set_flags(numerator, BN_FLG_CONSTTIME);
1948 BN_set_flags(denominator, BN_FLG_CONSTTIME);
1950 /* Test that BN_div never gives negative zero in the quotient. */
1951 if (!TEST_true(BN_set_word(numerator, 1))
1952 || !TEST_true(BN_set_word(denominator, 2)))
1954 BN_set_negative(numerator, 1);
1955 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
1956 || !TEST_BN_eq_zero(a)
1957 || !TEST_BN_ge_zero(a))
1960 /* Test that BN_div never gives negative zero in the remainder. */
1961 if (!TEST_true(BN_set_word(denominator, 1))
1962 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
1963 || !TEST_BN_eq_zero(b)
1964 || !TEST_BN_ge_zero(b))
1967 BN_free(denominator);
1968 numerator = denominator = NULL;
1971 /* Test that BN_set_negative will not produce a negative zero. */
1973 BN_set_negative(a, 1);
1974 if (BN_is_negative(a))
1984 BN_free(denominator);
1988 static int test_badmod(void)
1990 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
1991 BN_MONT_CTX *mont = NULL;
1994 if (!TEST_ptr(a = BN_new())
1995 || !TEST_ptr(b = BN_new())
1996 || !TEST_ptr(zero = BN_new())
1997 || !TEST_ptr(mont = BN_MONT_CTX_new()))
2001 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
2005 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
2009 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
2013 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2018 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2023 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
2027 /* Some operations also may not be used with an even modulus. */
2028 if (!TEST_true(BN_set_word(b, 16)))
2031 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
2035 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2040 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2050 BN_MONT_CTX_free(mont);
2054 static int test_expmodzero(void)
2056 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
2059 if (!TEST_ptr(zero = BN_new())
2060 || !TEST_ptr(a = BN_new())
2061 || !TEST_ptr(r = BN_new()))
2065 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
2066 || !TEST_BN_eq_zero(r)
2067 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
2069 || !TEST_BN_eq_zero(r)
2070 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
2073 || !TEST_BN_eq_zero(r)
2074 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
2075 BN_value_one(), NULL, NULL))
2076 || !TEST_BN_eq_zero(r))
2087 static int test_expmodone(void)
2090 BIGNUM *r = BN_new();
2091 BIGNUM *a = BN_new();
2092 BIGNUM *p = BN_new();
2093 BIGNUM *m = BN_new();
2100 || !TEST_true(BN_set_word(a, 1))
2101 || !TEST_true(BN_set_word(p, 0))
2102 || !TEST_true(BN_set_word(m, 1)))
2105 /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
2106 for (i = 0; i < 2; i++) {
2107 if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
2108 || !TEST_BN_eq_zero(r)
2109 || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
2110 || !TEST_BN_eq_zero(r)
2111 || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
2112 || !TEST_BN_eq_zero(r)
2113 || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
2114 || !TEST_BN_eq_zero(r)
2115 || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
2116 || !TEST_BN_eq_zero(r)
2117 || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
2118 || !TEST_BN_eq_zero(r))
2120 /* Repeat for r = 1 ^ 0 mod -1 */
2122 BN_set_negative(m, 1);
2134 static int test_smallprime(void)
2136 static const int kBits = 10;
2140 if (!TEST_ptr(r = BN_new())
2141 || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
2143 || !TEST_int_eq(BN_num_bits(r), kBits))
2152 static int primes[] = { 2, 3, 5, 7, 17863 };
2154 static int test_is_prime(int i)
2160 if (!TEST_ptr(r = BN_new()))
2163 for (trial = 0; trial <= 1; ++trial) {
2164 if (!TEST_true(BN_set_word(r, primes[i]))
2165 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL),
2176 static int not_primes[] = { -1, 0, 1, 4 };
2178 static int test_not_prime(int i)
2184 if (!TEST_ptr(r = BN_new()))
2187 for (trial = 0; trial <= 1; ++trial) {
2188 if (!TEST_true(BN_set_word(r, not_primes[i]))
2189 || !TEST_false(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL)))
2199 static int test_ctx_set_ct_flag(BN_CTX *c)
2206 for (i = 0; i < OSSL_NELEM(b); i++) {
2207 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2210 BN_set_flags(b[i], BN_FLG_CONSTTIME);
2219 static int test_ctx_check_ct_flag(BN_CTX *c)
2226 for (i = 0; i < OSSL_NELEM(b); i++) {
2227 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2229 if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
2239 static int test_ctx_consttime_flag(void)
2242 * The constant-time flag should not "leak" among BN_CTX frames:
2244 * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
2245 * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
2246 * from the frame before ending it.
2247 * - test_ctx_check_ct_flag() then starts a new frame and gets a
2248 * number of BIGNUMs from it. In absence of leaks, none of the
2249 * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
2251 * In actual BN_CTX usage inside libcrypto the leak could happen at
2252 * any depth level in the BN_CTX stack, with varying results
2253 * depending on the patterns of sibling trees of nested function
2254 * calls sharing the same BN_CTX object, and the effect of
2255 * unintended BN_FLG_CONSTTIME on the called BN_* functions.
2257 * This simple unit test abstracts away this complexity and verifies
2258 * that the leak does not happen between two sibling functions
2259 * sharing the same BN_CTX object at the same level of nesting.
2262 BN_CTX *nctx = NULL;
2263 BN_CTX *sctx = NULL;
2267 if (!TEST_ptr(nctx = BN_CTX_new())
2268 || !TEST_ptr(sctx = BN_CTX_secure_new()))
2271 for (i = 0; i < 2; i++) {
2272 BN_CTX *c = i == 0 ? nctx : sctx;
2273 if (!TEST_true(test_ctx_set_ct_flag(c))
2274 || !TEST_true(test_ctx_check_ct_flag(c)))
2285 static int file_test_run(STANZA *s)
2287 static const FILETEST filetests[] = {
2289 {"LShift1", file_lshift1},
2290 {"LShift", file_lshift},
2291 {"RShift", file_rshift},
2292 {"Square", file_square},
2293 {"Product", file_product},
2294 {"Quotient", file_quotient},
2295 {"ModMul", file_modmul},
2296 {"ModExp", file_modexp},
2298 {"ModSqrt", file_modsqrt},
2300 int numtests = OSSL_NELEM(filetests);
2301 const FILETEST *tp = filetests;
2303 for ( ; --numtests >= 0; tp++) {
2304 if (findattr(s, tp->name) != NULL) {
2306 TEST_info("%s:%d: Failed %s test",
2307 s->test_file, s->start, tp->name);
2313 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2317 static int run_file_tests(int i)
2320 char *testfile = test_get_argument(i);
2323 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2325 if (!test_start_file(s, testfile)) {
2330 /* Read test file. */
2331 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2332 if (s->numpairs == 0)
2334 if (!file_test_run(s))
2337 test_clearstanza(s);
2346 const OPTIONS *test_get_options(void)
2348 enum { OPT_TEST_ENUM };
2349 static const OPTIONS test_options[] = {
2350 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
2351 { OPT_HELP_STR, 1, '-',
2352 "file\tFile to run tests on. Normal tests are not run\n" },
2355 return test_options;
2358 int setup_tests(void)
2360 int n = test_get_argument_count();
2362 if (!TEST_ptr(ctx = BN_CTX_new()))
2367 ADD_TEST(test_div_recip);
2369 ADD_TEST(test_modexp_mont5);
2370 ADD_TEST(test_kronecker);
2371 ADD_TEST(test_rand);
2372 ADD_TEST(test_bn2padded);
2373 ADD_TEST(test_dec2bn);
2374 ADD_TEST(test_hex2bn);
2375 ADD_TEST(test_asc2bn);
2376 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2377 ADD_TEST(test_negzero);
2378 ADD_TEST(test_badmod);
2379 ADD_TEST(test_expmodzero);
2380 ADD_TEST(test_expmodone);
2381 ADD_TEST(test_smallprime);
2382 ADD_TEST(test_swap);
2383 ADD_TEST(test_ctx_consttime_flag);
2384 #ifndef OPENSSL_NO_EC2M
2385 ADD_TEST(test_gf2m_add);
2386 ADD_TEST(test_gf2m_mod);
2387 ADD_TEST(test_gf2m_mul);
2388 ADD_TEST(test_gf2m_sqr);
2389 ADD_TEST(test_gf2m_modinv);
2390 ADD_TEST(test_gf2m_moddiv);
2391 ADD_TEST(test_gf2m_modexp);
2392 ADD_TEST(test_gf2m_modsqrt);
2393 ADD_TEST(test_gf2m_modsolvequad);
2395 ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
2396 ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
2398 ADD_ALL_TESTS(run_file_tests, n);
2403 void cleanup_tests(void)