2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
16 #include "internal/nelem.h"
17 #include "internal/numbers.h"
18 #include <openssl/bn.h>
19 #include <openssl/crypto.h>
20 #include <openssl/err.h>
21 #include <openssl/rand.h>
25 * In bn_lcl.h, bn_expand() is defined as a static ossl_inline function.
26 * This is fine in itself, it will end up as an unused static function in
27 * the worst case. However, it references bn_expand2(), which is a private
28 * function in libcrypto and therefore unavailable on some systems. This
29 * may result in a linker error because of unresolved symbols.
31 * To avoid this, we define a dummy variant of bn_expand2() here, and to
32 * avoid possible clashes with libcrypto, we rename it first, using a macro.
34 #define bn_expand2 dummy_bn_expand2
35 BIGNUM *bn_expand2(BIGNUM *b, int words);
36 BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; }
37 #include "../crypto/bn/bn_lcl.h"
40 * Things in boring, not in openssl. TODO we should add them.
42 #define HAVE_BN_PADDED 0
43 #define HAVE_BN_SQRT 0
45 typedef struct filetest_st {
47 int (*func)(STANZA *s);
50 typedef struct mpitest_st {
56 static const int NUM0 = 100; /* number of tests */
57 static const int NUM1 = 50; /* additional tests for some functions */
61 * Polynomial coefficients used in GFM tests.
63 #ifndef OPENSSL_NO_EC2M
64 static int p0[] = { 163, 7, 6, 3, 0, -1 };
65 static int p1[] = { 193, 15, 0, -1 };
69 * Look for |key| in the stanza and return it or NULL if not found.
71 static const char *findattr(STANZA *s, const char *key)
76 for ( ; --i >= 0; pp++)
77 if (strcasecmp(pp->key, key) == 0)
83 * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
85 static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
87 char *bigstring = glue_strings(bn_strings, NULL);
88 int ret = BN_hex2bn(out, bigstring);
90 OPENSSL_free(bigstring);
95 * Parse BIGNUM, return number of bytes parsed.
97 static int parseBN(BIGNUM **out, const char *in)
100 return BN_hex2bn(out, in);
103 static int parsedecBN(BIGNUM **out, const char *in)
106 return BN_dec2bn(out, in);
109 static BIGNUM *getBN(STANZA *s, const char *attribute)
114 if ((hex = findattr(s, attribute)) == NULL) {
115 TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
119 if (parseBN(&ret, hex) != (int)strlen(hex)) {
120 TEST_error("Could not decode '%s'", hex);
126 static int getint(STANZA *s, int *out, const char *attribute)
132 if (!TEST_ptr(ret = getBN(s, attribute))
133 || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
143 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
145 if (BN_cmp(expected, actual) == 0)
148 TEST_error("unexpected %s value", op);
149 TEST_BN_eq(expected, actual);
155 * Return a "random" flag for if a BN should be negated.
157 static int rand_neg(void)
159 static unsigned int neg = 0;
160 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
162 return sign[(neg++) % 8];
166 static int test_sub(void)
168 BIGNUM *a = NULL, *b = NULL, *c = NULL;
171 if (!TEST_ptr(a = BN_new())
172 || !TEST_ptr(b = BN_new())
173 || !TEST_ptr(c = BN_new()))
176 for (i = 0; i < NUM0 + NUM1; i++) {
178 BN_bntest_rand(a, 512, 0, 0);
180 if (!TEST_int_ne(BN_set_bit(a, i), 0))
184 BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
191 if (!TEST_BN_eq_zero(c))
203 static int test_div_recip(void)
205 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
206 BN_RECP_CTX *recp = NULL;
209 if (!TEST_ptr(a = BN_new())
210 || !TEST_ptr(b = BN_new())
211 || !TEST_ptr(c = BN_new())
212 || !TEST_ptr(d = BN_new())
213 || !TEST_ptr(e = BN_new())
214 || !TEST_ptr(recp = BN_RECP_CTX_new()))
217 for (i = 0; i < NUM0 + NUM1; i++) {
219 BN_bntest_rand(a, 400, 0, 0);
224 BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
227 BN_RECP_CTX_set(recp, b, ctx);
228 BN_div_recp(d, c, a, recp, ctx);
229 BN_mul(e, d, b, ctx);
232 if (!TEST_BN_eq_zero(d))
242 BN_RECP_CTX_free(recp);
247 static int test_mod(void)
249 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
252 if (!TEST_ptr(a = BN_new())
253 || !TEST_ptr(b = BN_new())
254 || !TEST_ptr(c = BN_new())
255 || !TEST_ptr(d = BN_new())
256 || !TEST_ptr(e = BN_new()))
259 BN_bntest_rand(a, 1024, 0, 0);
260 for (i = 0; i < NUM0; i++) {
261 BN_bntest_rand(b, 450 + i * 10, 0, 0);
264 BN_mod(c, a, b, ctx);
265 BN_div(d, e, a, b, ctx);
267 if (!TEST_BN_eq_zero(e))
280 static const char *bn1strings[] = {
281 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
282 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
283 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
284 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
285 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
286 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
287 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
288 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
289 "0000000000000000000000000000000000000000000000000000000000000000",
290 "0000000000000000000000000000000000000000000000000000000000000000",
291 "0000000000000000000000000000000000000000000000000000000000000000",
292 "0000000000000000000000000000000000000000000000000000000000000000",
293 "0000000000000000000000000000000000000000000000000000000000000000",
294 "0000000000000000000000000000000000000000000000000000000000000000",
295 "0000000000000000000000000000000000000000000000000000000000000000",
296 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
300 static const char *bn2strings[] = {
301 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
302 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
303 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
304 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
305 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
306 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
307 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
308 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
309 "0000000000000000000000000000000000000000000000000000000000000000",
310 "0000000000000000000000000000000000000000000000000000000000000000",
311 "0000000000000000000000000000000000000000000000000000000000000000",
312 "0000000000000000000000000000000000000000000000000000000000000000",
313 "0000000000000000000000000000000000000000000000000000000000000000",
314 "0000000000000000000000000000000000000000000000000000000000000000",
315 "0000000000000000000000000000000000000000000000000000000000000000",
316 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
321 * Test constant-time modular exponentiation with 1024-bit inputs, which on
322 * x86_64 cause a different code branch to be taken.
324 static int test_modexp_mont5(void)
326 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
327 BIGNUM *b = NULL, *n = NULL, *c = NULL;
328 BN_MONT_CTX *mont = NULL;
331 if (!TEST_ptr(a = BN_new())
332 || !TEST_ptr(p = BN_new())
333 || !TEST_ptr(m = BN_new())
334 || !TEST_ptr(d = BN_new())
335 || !TEST_ptr(e = BN_new())
336 || !TEST_ptr(b = BN_new())
337 || !TEST_ptr(n = BN_new())
338 || !TEST_ptr(c = BN_new())
339 || !TEST_ptr(mont = BN_MONT_CTX_new()))
342 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
344 BN_bntest_rand(a, 1024, 0, 0);
346 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
348 if (!TEST_BN_eq_one(d))
351 /* Regression test for carry bug in mulx4x_mont */
353 "7878787878787878787878787878787878787878787878787878787878787878"
354 "7878787878787878787878787878787878787878787878787878787878787878"
355 "7878787878787878787878787878787878787878787878787878787878787878"
356 "7878787878787878787878787878787878787878787878787878787878787878");
358 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
359 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
360 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
361 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
363 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
364 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
365 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
366 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
367 BN_MONT_CTX_set(mont, n, ctx);
368 BN_mod_mul_montgomery(c, a, b, mont, ctx);
369 BN_mod_mul_montgomery(d, b, a, mont, ctx);
370 if (!TEST_BN_eq(c, d))
373 /* Regression test for carry bug in sqr[x]8x_mont */
374 parse_bigBN(&n, bn1strings);
375 parse_bigBN(&a, bn2strings);
378 BN_MONT_CTX_set(mont, n, ctx);
379 BN_mod_mul_montgomery(c, a, a, mont, ctx);
380 BN_mod_mul_montgomery(d, a, b, mont, ctx);
381 if (!TEST_BN_eq(c, d))
384 /* Regression test for carry bug in bn_sqrx8x_internal */
386 static const char *ahex[] = {
387 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
388 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
389 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
390 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
391 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
392 "9544D954000000006C0000000000000000000000000000000000000000000000",
393 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
394 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
395 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
396 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
397 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
398 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
401 static const char *nhex[] = {
402 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
403 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
404 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
405 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
406 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
407 "00000010000000006C0000000000000000000000000000000000000000000000",
408 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
409 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
410 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
411 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
412 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
413 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
417 parse_bigBN(&a, ahex);
418 parse_bigBN(&n, nhex);
422 BN_MONT_CTX_set(mont, n, ctx);
423 BN_mod_mul_montgomery(c, a, a, mont, ctx);
424 BN_mod_mul_montgomery(d, a, b, mont, ctx);
425 if (!TEST_BN_eq(c, d))
429 BN_bntest_rand(p, 1024, 0, 0);
431 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
432 || !TEST_BN_eq_zero(d))
436 * Craft an input whose Montgomery representation is 1, i.e., shorter
437 * than the modulus m, in order to test the const time precomputation
438 * scattering/gathering.
441 BN_MONT_CTX_set(mont, m, ctx);
442 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
443 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
444 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
445 || !TEST_BN_eq(a, d))
448 /* Finally, some regular test vectors. */
449 BN_bntest_rand(e, 1024, 0, 0);
450 if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
451 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
452 || !TEST_BN_eq(a, d))
458 BN_MONT_CTX_free(mont);
470 #ifndef OPENSSL_NO_EC2M
471 static int test_gf2m_add(void)
473 BIGNUM *a = NULL, *b = NULL, *c = NULL;
476 if (!TEST_ptr(a = BN_new())
477 || !TEST_ptr(b = BN_new())
478 || !TEST_ptr(c = BN_new()))
481 for (i = 0; i < NUM0; i++) {
482 BN_rand(a, 512, 0, 0);
483 BN_copy(b, BN_value_one());
486 BN_GF2m_add(c, a, b);
487 /* Test that two added values have the correct parity. */
488 if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
489 || (!BN_is_odd(a) && !BN_is_odd(c))))
491 BN_GF2m_add(c, c, c);
492 /* Test that c + c = 0. */
493 if (!TEST_BN_eq_zero(c))
504 static int test_gf2m_mod(void)
506 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
509 if (!TEST_ptr(a = BN_new())
510 || !TEST_ptr(b[0] = BN_new())
511 || !TEST_ptr(b[1] = BN_new())
512 || !TEST_ptr(c = BN_new())
513 || !TEST_ptr(d = BN_new())
514 || !TEST_ptr(e = BN_new()))
517 BN_GF2m_arr2poly(p0, b[0]);
518 BN_GF2m_arr2poly(p1, b[1]);
520 for (i = 0; i < NUM0; i++) {
521 BN_bntest_rand(a, 1024, 0, 0);
522 for (j = 0; j < 2; j++) {
523 BN_GF2m_mod(c, a, b[j]);
524 BN_GF2m_add(d, a, c);
525 BN_GF2m_mod(e, d, b[j]);
526 /* Test that a + (a mod p) mod p == 0. */
527 if (!TEST_BN_eq_zero(e))
542 static int test_gf2m_mul(void)
544 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
545 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
548 if (!TEST_ptr(a = BN_new())
549 || !TEST_ptr(b[0] = BN_new())
550 || !TEST_ptr(b[1] = BN_new())
551 || !TEST_ptr(c = BN_new())
552 || !TEST_ptr(d = BN_new())
553 || !TEST_ptr(e = BN_new())
554 || !TEST_ptr(f = BN_new())
555 || !TEST_ptr(g = BN_new())
556 || !TEST_ptr(h = BN_new()))
559 BN_GF2m_arr2poly(p0, b[0]);
560 BN_GF2m_arr2poly(p1, b[1]);
562 for (i = 0; i < NUM0; i++) {
563 BN_bntest_rand(a, 1024, 0, 0);
564 BN_bntest_rand(c, 1024, 0, 0);
565 BN_bntest_rand(d, 1024, 0, 0);
566 for (j = 0; j < 2; j++) {
567 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
568 BN_GF2m_add(f, a, d);
569 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
570 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
571 BN_GF2m_add(f, e, g);
572 BN_GF2m_add(f, f, h);
573 /* Test that (a+d)*c = a*c + d*c. */
574 if (!TEST_BN_eq_zero(f))
593 static int test_gf2m_sqr(void)
595 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
598 if (!TEST_ptr(a = BN_new())
599 || !TEST_ptr(b[0] = BN_new())
600 || !TEST_ptr(b[1] = BN_new())
601 || !TEST_ptr(c = BN_new())
602 || !TEST_ptr(d = BN_new()))
605 BN_GF2m_arr2poly(p0, b[0]);
606 BN_GF2m_arr2poly(p1, b[1]);
608 for (i = 0; i < NUM0; i++) {
609 BN_bntest_rand(a, 1024, 0, 0);
610 for (j = 0; j < 2; j++) {
611 BN_GF2m_mod_sqr(c, a, b[j], ctx);
613 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
614 BN_GF2m_add(d, c, d);
615 /* Test that a*a = a^2. */
616 if (!TEST_BN_eq_zero(d))
630 static int test_gf2m_modinv(void)
632 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
635 if (!TEST_ptr(a = BN_new())
636 || !TEST_ptr(b[0] = BN_new())
637 || !TEST_ptr(b[1] = BN_new())
638 || !TEST_ptr(c = BN_new())
639 || !TEST_ptr(d = BN_new()))
642 BN_GF2m_arr2poly(p0, b[0]);
643 BN_GF2m_arr2poly(p1, b[1]);
645 for (i = 0; i < NUM0; i++) {
646 BN_bntest_rand(a, 512, 0, 0);
647 for (j = 0; j < 2; j++) {
648 BN_GF2m_mod_inv(c, a, b[j], ctx);
649 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
650 /* Test that ((1/a)*a) = 1. */
651 if (!TEST_BN_eq_one(d))
665 static int test_gf2m_moddiv(void)
667 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
668 BIGNUM *e = NULL, *f = NULL;
671 if (!TEST_ptr(a = BN_new())
672 || !TEST_ptr(b[0] = BN_new())
673 || !TEST_ptr(b[1] = BN_new())
674 || !TEST_ptr(c = BN_new())
675 || !TEST_ptr(d = BN_new())
676 || !TEST_ptr(e = BN_new())
677 || !TEST_ptr(f = BN_new()))
680 BN_GF2m_arr2poly(p0, b[0]);
681 BN_GF2m_arr2poly(p1, b[1]);
683 for (i = 0; i < NUM0; i++) {
684 BN_bntest_rand(a, 512, 0, 0);
685 BN_bntest_rand(c, 512, 0, 0);
686 for (j = 0; j < 2; j++) {
687 BN_GF2m_mod_div(d, a, c, b[j], ctx);
688 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
689 BN_GF2m_mod_div(f, a, e, b[j], ctx);
690 /* Test that ((a/c)*c)/a = 1. */
691 if (!TEST_BN_eq_one(f))
707 static int test_gf2m_modexp(void)
709 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
710 BIGNUM *e = NULL, *f = 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())
718 || !TEST_ptr(e = BN_new())
719 || !TEST_ptr(f = BN_new()))
722 BN_GF2m_arr2poly(p0, b[0]);
723 BN_GF2m_arr2poly(p1, b[1]);
725 for (i = 0; i < NUM0; i++) {
726 BN_bntest_rand(a, 512, 0, 0);
727 BN_bntest_rand(c, 512, 0, 0);
728 BN_bntest_rand(d, 512, 0, 0);
729 for (j = 0; j < 2; j++) {
730 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
731 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
732 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
734 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
735 BN_GF2m_add(f, e, f);
736 /* Test that a^(c+d)=a^c*a^d. */
737 if (!TEST_BN_eq_zero(f))
753 static int test_gf2m_modsqrt(void)
755 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
756 BIGNUM *e = NULL, *f = NULL;
759 if (!TEST_ptr(a = BN_new())
760 || !TEST_ptr(b[0] = BN_new())
761 || !TEST_ptr(b[1] = BN_new())
762 || !TEST_ptr(c = BN_new())
763 || !TEST_ptr(d = BN_new())
764 || !TEST_ptr(e = BN_new())
765 || !TEST_ptr(f = BN_new()))
768 BN_GF2m_arr2poly(p0, b[0]);
769 BN_GF2m_arr2poly(p1, b[1]);
771 for (i = 0; i < NUM0; i++) {
772 BN_bntest_rand(a, 512, 0, 0);
773 for (j = 0; j < 2; j++) {
774 BN_GF2m_mod(c, a, b[j]);
775 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
776 BN_GF2m_mod_sqr(e, d, b[j], ctx);
777 BN_GF2m_add(f, c, e);
778 /* Test that d^2 = a, where d = sqrt(a). */
779 if (!TEST_BN_eq_zero(f))
795 static int test_gf2m_modsolvequad(void)
797 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
799 int i, j, s = 0, t, st = 0;
801 if (!TEST_ptr(a = BN_new())
802 || !TEST_ptr(b[0] = BN_new())
803 || !TEST_ptr(b[1] = BN_new())
804 || !TEST_ptr(c = BN_new())
805 || !TEST_ptr(d = BN_new())
806 || !TEST_ptr(e = BN_new()))
809 BN_GF2m_arr2poly(p0, b[0]);
810 BN_GF2m_arr2poly(p1, b[1]);
812 for (i = 0; i < NUM0; i++) {
813 BN_bntest_rand(a, 512, 0, 0);
814 for (j = 0; j < 2; j++) {
815 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
818 BN_GF2m_mod_sqr(d, c, b[j], ctx);
819 BN_GF2m_add(d, c, d);
820 BN_GF2m_mod(e, a, b[j]);
821 BN_GF2m_add(e, e, d);
823 * Test that solution of quadratic c satisfies c^2 + c = a.
825 if (!TEST_BN_eq_zero(e))
830 if (!TEST_int_ge(s, 0)) {
831 TEST_info("%d tests found no roots; probably an error", NUM0);
846 static int test_kronecker(void)
848 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
849 int i, legendre, kronecker, st = 0;
851 if (!TEST_ptr(a = BN_new())
852 || !TEST_ptr(b = BN_new())
853 || !TEST_ptr(r = BN_new())
854 || !TEST_ptr(t = BN_new()))
858 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
859 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
860 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
861 * generate a random prime b and compare these values for a number of
862 * random a's. (That is, we run the Solovay-Strassen primality test to
863 * confirm that b is prime, except that we don't want to test whether b
864 * is prime but whether BN_kronecker works.)
867 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
871 for (i = 0; i < NUM0; i++) {
872 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
876 /* t := (|b|-1)/2 (note that b is odd) */
877 if (!TEST_true(BN_copy(t, b)))
880 if (!TEST_true(BN_sub_word(t, 1)))
882 if (!TEST_true(BN_rshift1(t, t)))
887 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
891 if (BN_is_word(r, 1))
893 else if (BN_is_zero(r))
896 if (!TEST_true(BN_add_word(r, 1)))
898 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
899 TEST_info("Legendre symbol computation failed");
905 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
907 /* we actually need BN_kronecker(a, |b|) */
908 if (a->neg && b->neg)
909 kronecker = -kronecker;
911 if (!TEST_int_eq(legendre, kronecker))
924 static int file_sum(STANZA *s)
926 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
930 if (!TEST_ptr(a = getBN(s, "A"))
931 || !TEST_ptr(b = getBN(s, "B"))
932 || !TEST_ptr(sum = getBN(s, "Sum"))
933 || !TEST_ptr(ret = BN_new()))
936 if (!TEST_true(BN_add(ret, a, b))
937 || !equalBN("A + B", sum, ret)
938 || !TEST_true(BN_sub(ret, sum, a))
939 || !equalBN("Sum - A", b, ret)
940 || !TEST_true(BN_sub(ret, sum, b))
941 || !equalBN("Sum - B", a, ret))
945 * Test that the functions work when |r| and |a| point to the same BIGNUM,
946 * or when |r| and |b| point to the same BIGNUM.
947 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
949 if (!TEST_true(BN_copy(ret, a))
950 || !TEST_true(BN_add(ret, ret, b))
951 || !equalBN("A + B (r is a)", sum, ret)
952 || !TEST_true(BN_copy(ret, b))
953 || !TEST_true(BN_add(ret, a, ret))
954 || !equalBN("A + B (r is b)", sum, ret)
955 || !TEST_true(BN_copy(ret, sum))
956 || !TEST_true(BN_sub(ret, ret, a))
957 || !equalBN("Sum - A (r is a)", b, ret)
958 || !TEST_true(BN_copy(ret, a))
959 || !TEST_true(BN_sub(ret, sum, ret))
960 || !equalBN("Sum - A (r is b)", b, ret)
961 || !TEST_true(BN_copy(ret, sum))
962 || !TEST_true(BN_sub(ret, ret, b))
963 || !equalBN("Sum - B (r is a)", a, ret)
964 || !TEST_true(BN_copy(ret, b))
965 || !TEST_true(BN_sub(ret, sum, ret))
966 || !equalBN("Sum - B (r is b)", a, ret))
970 * Test BN_uadd() and BN_usub() with the prerequisites they are
971 * documented as having. Note that these functions are frequently used
972 * when the prerequisites don't hold. In those cases, they are supposed
973 * to work as if the prerequisite hold, but we don't test that yet.
976 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
977 if (!TEST_true(BN_uadd(ret, a, b))
978 || !equalBN("A +u B", sum, ret)
979 || !TEST_true(BN_usub(ret, sum, a))
980 || !equalBN("Sum -u A", b, ret)
981 || !TEST_true(BN_usub(ret, sum, b))
982 || !equalBN("Sum -u B", a, ret))
985 * Test that the functions work when |r| and |a| point to the same
986 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
987 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
989 if (!TEST_true(BN_copy(ret, a))
990 || !TEST_true(BN_uadd(ret, ret, b))
991 || !equalBN("A +u B (r is a)", sum, ret)
992 || !TEST_true(BN_copy(ret, b))
993 || !TEST_true(BN_uadd(ret, a, ret))
994 || !equalBN("A +u B (r is b)", sum, ret)
995 || !TEST_true(BN_copy(ret, sum))
996 || !TEST_true(BN_usub(ret, ret, a))
997 || !equalBN("Sum -u A (r is a)", b, ret)
998 || !TEST_true(BN_copy(ret, a))
999 || !TEST_true(BN_usub(ret, sum, ret))
1000 || !equalBN("Sum -u A (r is b)", b, ret)
1001 || !TEST_true(BN_copy(ret, sum))
1002 || !TEST_true(BN_usub(ret, ret, b))
1003 || !equalBN("Sum -u B (r is a)", a, ret)
1004 || !TEST_true(BN_copy(ret, b))
1005 || !TEST_true(BN_usub(ret, sum, ret))
1006 || !equalBN("Sum -u B (r is b)", a, ret))
1011 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1013 b_word = BN_get_word(b);
1014 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1015 if (!TEST_true(BN_copy(ret, a))
1016 || !TEST_true(BN_add_word(ret, b_word))
1017 || !equalBN("A + B (word)", sum, ret)
1018 || !TEST_true(BN_copy(ret, sum))
1019 || !TEST_true(BN_sub_word(ret, b_word))
1020 || !equalBN("Sum - B (word)", a, ret))
1033 static int file_lshift1(STANZA *s)
1035 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1036 BIGNUM *two = NULL, *remainder = NULL;
1039 if (!TEST_ptr(a = getBN(s, "A"))
1040 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1041 || !TEST_ptr(zero = BN_new())
1042 || !TEST_ptr(ret = BN_new())
1043 || !TEST_ptr(two = BN_new())
1044 || !TEST_ptr(remainder = BN_new()))
1049 if (!TEST_true(BN_set_word(two, 2))
1050 || !TEST_true(BN_add(ret, a, a))
1051 || !equalBN("A + A", lshift1, ret)
1052 || !TEST_true(BN_mul(ret, a, two, ctx))
1053 || !equalBN("A * 2", lshift1, ret)
1054 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1055 || !equalBN("LShift1 / 2", a, ret)
1056 || !equalBN("LShift1 % 2", zero, remainder)
1057 || !TEST_true(BN_lshift1(ret, a))
1058 || !equalBN("A << 1", lshift1, ret)
1059 || !TEST_true(BN_rshift1(ret, lshift1))
1060 || !equalBN("LShift >> 1", a, ret)
1061 || !TEST_true(BN_rshift1(ret, lshift1))
1062 || !equalBN("LShift >> 1", a, ret))
1065 /* Set the LSB to 1 and test rshift1 again. */
1066 if (!TEST_true(BN_set_bit(lshift1, 0))
1067 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1068 || !equalBN("(LShift1 | 1) / 2", a, ret)
1069 || !TEST_true(BN_rshift1(ret, lshift1))
1070 || !equalBN("(LShift | 1) >> 1", a, ret))
1085 static int file_lshift(STANZA *s)
1087 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1090 if (!TEST_ptr(a = getBN(s, "A"))
1091 || !TEST_ptr(lshift = getBN(s, "LShift"))
1092 || !TEST_ptr(ret = BN_new()))
1094 if (!TEST_true(BN_lshift(ret, a, n))
1095 || !equalBN("A << N", lshift, ret)
1096 || !TEST_true(BN_rshift(ret, lshift, n))
1097 || !equalBN("A >> N", a, ret))
1108 static int file_rshift(STANZA *s)
1110 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1113 if (!TEST_ptr(a = getBN(s, "A"))
1114 || !TEST_ptr(rshift = getBN(s, "RShift"))
1115 || !TEST_ptr(ret = BN_new())
1116 || !getint(s, &n, "N"))
1119 if (!TEST_true(BN_rshift(ret, a, n))
1120 || !equalBN("A >> N", rshift, ret))
1123 /* If N == 1, try with rshift1 as well */
1125 if (!TEST_true(BN_rshift1(ret, a))
1126 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1138 static int file_square(STANZA *s)
1140 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1141 BIGNUM *remainder = NULL, *tmp = NULL;
1144 if (!TEST_ptr(a = getBN(s, "A"))
1145 || !TEST_ptr(square = getBN(s, "Square"))
1146 || !TEST_ptr(zero = BN_new())
1147 || !TEST_ptr(ret = BN_new())
1148 || !TEST_ptr(remainder = BN_new()))
1152 if (!TEST_true(BN_sqr(ret, a, ctx))
1153 || !equalBN("A^2", square, ret)
1154 || !TEST_true(BN_mul(ret, a, a, ctx))
1155 || !equalBN("A * A", square, ret)
1156 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1157 || !equalBN("Square / A", a, ret)
1158 || !equalBN("Square % A", zero, remainder))
1162 BN_set_negative(a, 0);
1163 if (!TEST_true(BN_sqrt(ret, square, ctx))
1164 || !equalBN("sqrt(Square)", a, ret))
1167 /* BN_sqrt should fail on non-squares and negative numbers. */
1168 if (!TEST_BN_eq_zero(square)) {
1169 if (!TEST_ptr(tmp = BN_new())
1170 || !TEST_true(BN_copy(tmp, square)))
1172 BN_set_negative(tmp, 1);
1174 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1178 BN_set_negative(tmp, 0);
1179 if (BN_add(tmp, tmp, BN_value_one()))
1181 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1198 static int file_product(STANZA *s)
1200 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1201 BIGNUM *remainder = NULL, *zero = NULL;
1204 if (!TEST_ptr(a = getBN(s, "A"))
1205 || !TEST_ptr(b = getBN(s, "B"))
1206 || !TEST_ptr(product = getBN(s, "Product"))
1207 || !TEST_ptr(ret = BN_new())
1208 || !TEST_ptr(remainder = BN_new())
1209 || !TEST_ptr(zero = BN_new()))
1214 if (!TEST_true(BN_mul(ret, a, b, ctx))
1215 || !equalBN("A * B", product, ret)
1216 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1217 || !equalBN("Product / A", b, ret)
1218 || !equalBN("Product % A", zero, remainder)
1219 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1220 || !equalBN("Product / B", a, ret)
1221 || !equalBN("Product % B", zero, remainder))
1235 static int file_quotient(STANZA *s)
1237 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1238 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1239 BN_ULONG b_word, ret_word;
1242 if (!TEST_ptr(a = getBN(s, "A"))
1243 || !TEST_ptr(b = getBN(s, "B"))
1244 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1245 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1246 || !TEST_ptr(ret = BN_new())
1247 || !TEST_ptr(ret2 = BN_new())
1248 || !TEST_ptr(nnmod = BN_new()))
1251 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1252 || !equalBN("A / B", quotient, ret)
1253 || !equalBN("A % B", remainder, ret2)
1254 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1255 || !TEST_true(BN_add(ret, ret, remainder))
1256 || !equalBN("Quotient * B + Remainder", a, ret))
1260 * Test with BN_mod_word() and BN_div_word() if the divisor is
1263 b_word = BN_get_word(b);
1264 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1265 BN_ULONG remainder_word = BN_get_word(remainder);
1267 assert(remainder_word != (BN_ULONG)-1);
1268 if (!TEST_ptr(BN_copy(ret, a)))
1270 ret_word = BN_div_word(ret, b_word);
1271 if (ret_word != remainder_word) {
1274 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1275 ret_word, remainder_word);
1277 TEST_error("Got A %% B (word) mismatch");
1281 if (!equalBN ("A / B (word)", quotient, ret))
1284 ret_word = BN_mod_word(a, b_word);
1285 if (ret_word != remainder_word) {
1288 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1289 ret_word, remainder_word);
1291 TEST_error("Got A %% B (word) mismatch");
1297 /* Test BN_nnmod. */
1298 if (!BN_is_negative(b)) {
1299 if (!TEST_true(BN_copy(nnmod, remainder))
1300 || (BN_is_negative(nnmod)
1301 && !TEST_true(BN_add(nnmod, nnmod, b)))
1302 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1303 || !equalBN("A % B (non-negative)", nnmod, ret))
1319 static int file_modmul(STANZA *s)
1321 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1324 if (!TEST_ptr(a = getBN(s, "A"))
1325 || !TEST_ptr(b = getBN(s, "B"))
1326 || !TEST_ptr(m = getBN(s, "M"))
1327 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1328 || !TEST_ptr(ret = BN_new()))
1331 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1332 || !equalBN("A * B (mod M)", mod_mul, ret))
1336 /* Reduce |a| and |b| and test the Montgomery version. */
1337 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1338 BIGNUM *a_tmp = BN_new();
1339 BIGNUM *b_tmp = BN_new();
1341 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1342 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1343 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1344 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1345 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1346 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1347 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1349 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1350 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1354 BN_MONT_CTX_free(mont);
1371 static int file_modexp(STANZA *s)
1373 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1374 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1377 if (!TEST_ptr(a = getBN(s, "A"))
1378 || !TEST_ptr(e = getBN(s, "E"))
1379 || !TEST_ptr(m = getBN(s, "M"))
1380 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1381 || !TEST_ptr(ret = BN_new())
1382 || !TEST_ptr(d = BN_new()))
1385 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1386 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1390 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1391 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1392 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1394 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1398 /* Regression test for carry propagation bug in sqr8x_reduction */
1399 BN_hex2bn(&a, "050505050505");
1400 BN_hex2bn(&b, "02");
1402 "4141414141414141414141274141414141414141414141414141414141414141"
1403 "4141414141414141414141414141414141414141414141414141414141414141"
1404 "4141414141414141414141800000000000000000000000000000000000000000"
1405 "0000000000000000000000000000000000000000000000000000000000000000"
1406 "0000000000000000000000000000000000000000000000000000000000000000"
1407 "0000000000000000000000000000000000000000000000000000000001");
1408 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1409 || !TEST_true(BN_mul(e, a, a, ctx))
1410 || !TEST_BN_eq(d, e))
1426 static int file_exp(STANZA *s)
1428 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1431 if (!TEST_ptr(a = getBN(s, "A"))
1432 || !TEST_ptr(e = getBN(s, "E"))
1433 || !TEST_ptr(exp = getBN(s, "Exp"))
1434 || !TEST_ptr(ret = BN_new()))
1437 if (!TEST_true(BN_exp(ret, a, e, ctx))
1438 || !equalBN("A ^ E", exp, ret))
1450 static int file_modsqrt(STANZA *s)
1452 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1455 if (!TEST_ptr(a = getBN(s, "A"))
1456 || !TEST_ptr(p = getBN(s, "P"))
1457 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1458 || !TEST_ptr(ret = BN_new())
1459 || !TEST_ptr(ret2 = BN_new()))
1462 /* There are two possible answers. */
1463 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1464 || !TEST_true(BN_sub(ret2, p, ret)))
1467 /* The first condition should NOT be a test. */
1468 if (BN_cmp(ret2, mod_sqrt) != 0
1469 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1482 static int test_bn2padded(void)
1485 uint8_t zeros[256], out[256], reference[128];
1486 BIGNUM *n = BN_new();
1489 /* Test edge case at 0. */
1492 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1494 memset(out, -1, sizeof(out));
1495 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1497 memset(zeros, 0, sizeof(zeros));
1498 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1501 /* Test a random numbers at various byte lengths. */
1502 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1503 #define TOP_BIT_ON 0
1504 #define BOTTOM_BIT_NOTOUCH 0
1505 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1507 if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1508 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1510 /* Empty buffer should fail. */
1511 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1513 /* One byte short should fail. */
1514 if (BN_bn2bin_padded(out, bytes - 1, n))
1516 /* Exactly right size should encode. */
1517 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1518 || TEST_mem_eq(out, bytes, reference, bytes))
1520 /* Pad up one byte extra. */
1521 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1522 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1523 || !TEST_mem_eq(out, 1, zeros, 1))
1525 /* Pad up to 256. */
1526 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1527 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1529 || !TEST_mem_eq(out, sizseof(out) - bytes,
1530 zeros, sizeof(out) - bytes))
1543 static int test_dec2bn(void)
1548 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1549 || !TEST_BN_eq_word(bn, 0)
1550 || !TEST_BN_eq_zero(bn)
1551 || !TEST_BN_le_zero(bn)
1552 || !TEST_BN_ge_zero(bn)
1553 || !TEST_BN_even(bn))
1558 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1559 || !TEST_BN_eq_word(bn, 256)
1560 || !TEST_BN_ge_zero(bn)
1561 || !TEST_BN_gt_zero(bn)
1562 || !TEST_BN_ne_zero(bn)
1563 || !TEST_BN_even(bn))
1568 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1569 || !TEST_BN_abs_eq_word(bn, 42)
1570 || !TEST_BN_lt_zero(bn)
1571 || !TEST_BN_le_zero(bn)
1572 || !TEST_BN_ne_zero(bn)
1573 || !TEST_BN_even(bn))
1578 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1579 || !TEST_BN_eq_word(bn, 1)
1580 || !TEST_BN_ne_zero(bn)
1581 || !TEST_BN_gt_zero(bn)
1582 || !TEST_BN_ge_zero(bn)
1583 || !TEST_BN_eq_one(bn)
1584 || !TEST_BN_odd(bn))
1589 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1590 || !TEST_BN_eq_zero(bn)
1591 || !TEST_BN_ge_zero(bn)
1592 || !TEST_BN_le_zero(bn)
1593 || !TEST_BN_even(bn))
1598 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1599 || !TEST_BN_abs_eq_word(bn, 42)
1600 || !TEST_BN_ge_zero(bn)
1601 || !TEST_BN_gt_zero(bn)
1602 || !TEST_BN_ne_zero(bn)
1603 || !TEST_BN_even(bn))
1612 static int test_hex2bn(void)
1617 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1618 || !TEST_BN_eq_zero(bn)
1619 || !TEST_BN_ge_zero(bn)
1620 || !TEST_BN_even(bn))
1625 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1626 || !TEST_BN_eq_word(bn, 0x256)
1627 || !TEST_BN_ge_zero(bn)
1628 || !TEST_BN_gt_zero(bn)
1629 || !TEST_BN_ne_zero(bn)
1630 || !TEST_BN_even(bn))
1635 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1636 || !TEST_BN_abs_eq_word(bn, 0x42)
1637 || !TEST_BN_lt_zero(bn)
1638 || !TEST_BN_le_zero(bn)
1639 || !TEST_BN_ne_zero(bn)
1640 || !TEST_BN_even(bn))
1645 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1646 || !TEST_BN_eq_word(bn, 0xCB)
1647 || !TEST_BN_ge_zero(bn)
1648 || !TEST_BN_gt_zero(bn)
1649 || !TEST_BN_ne_zero(bn)
1650 || !TEST_BN_odd(bn))
1655 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1656 || !TEST_BN_eq_zero(bn)
1657 || !TEST_BN_ge_zero(bn)
1658 || !TEST_BN_le_zero(bn)
1659 || !TEST_BN_even(bn))
1664 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1665 || !TEST_BN_eq_word(bn, 0xabc)
1666 || !TEST_BN_ge_zero(bn)
1667 || !TEST_BN_gt_zero(bn)
1668 || !TEST_BN_ne_zero(bn)
1669 || !TEST_BN_even(bn))
1678 static int test_asc2bn(void)
1683 if (!TEST_ptr(bn = BN_new()))
1686 if (!TEST_true(BN_asc2bn(&bn, "0"))
1687 || !TEST_BN_eq_zero(bn)
1688 || !TEST_BN_ge_zero(bn))
1691 if (!TEST_true(BN_asc2bn(&bn, "256"))
1692 || !TEST_BN_eq_word(bn, 256)
1693 || !TEST_BN_ge_zero(bn))
1696 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1697 || !TEST_BN_abs_eq_word(bn, 42)
1698 || !TEST_BN_lt_zero(bn))
1701 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1702 || !TEST_BN_eq_word(bn, 0x1234)
1703 || !TEST_BN_ge_zero(bn))
1706 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1707 || !TEST_BN_eq_word(bn, 0x1234)
1708 || !TEST_BN_ge_zero(bn))
1711 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1712 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1713 || !TEST_BN_lt_zero(bn))
1716 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1717 || !TEST_BN_eq_zero(bn)
1718 || !TEST_BN_ge_zero(bn))
1721 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1722 || !TEST_BN_eq_word(bn, 123)
1723 || !TEST_BN_ge_zero(bn))
1732 static const MPITEST kMPITests[] = {
1733 {"0", "\x00\x00\x00\x00", 4},
1734 {"1", "\x00\x00\x00\x01\x01", 5},
1735 {"-1", "\x00\x00\x00\x01\x81", 5},
1736 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1737 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1738 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1741 static int test_mpi(int i)
1744 const MPITEST *test = &kMPITests[i];
1745 size_t mpi_len, mpi_len2;
1750 if (!TEST_ptr(bn = BN_new())
1751 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1753 mpi_len = BN_bn2mpi(bn, NULL);
1754 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1757 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1758 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1761 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1764 if (!TEST_BN_eq(bn, bn2)) {
1776 static int test_rand(void)
1781 if (!TEST_ptr(bn = BN_new()))
1784 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1785 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
1786 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
1787 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
1788 || !TEST_BN_eq_one(bn)
1789 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
1790 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
1791 || !TEST_BN_eq_one(bn)
1792 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
1793 || !TEST_BN_eq_word(bn, 3))
1802 static int test_negzero(void)
1804 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
1805 BIGNUM *numerator = NULL, *denominator = NULL;
1806 int consttime, st = 0;
1808 if (!TEST_ptr(a = BN_new())
1809 || !TEST_ptr(b = BN_new())
1810 || !TEST_ptr(c = BN_new())
1811 || !TEST_ptr(d = BN_new()))
1814 /* Test that BN_mul never gives negative zero. */
1815 if (!TEST_true(BN_set_word(a, 1)))
1817 BN_set_negative(a, 1);
1819 if (!TEST_true(BN_mul(c, a, b, ctx)))
1821 if (!TEST_BN_eq_zero(c)
1822 || !TEST_BN_ge_zero(c))
1825 for (consttime = 0; consttime < 2; consttime++) {
1826 if (!TEST_ptr(numerator = BN_new())
1827 || !TEST_ptr(denominator = BN_new()))
1830 BN_set_flags(numerator, BN_FLG_CONSTTIME);
1831 BN_set_flags(denominator, BN_FLG_CONSTTIME);
1833 /* Test that BN_div never gives negative zero in the quotient. */
1834 if (!TEST_true(BN_set_word(numerator, 1))
1835 || !TEST_true(BN_set_word(denominator, 2)))
1837 BN_set_negative(numerator, 1);
1838 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
1839 || !TEST_BN_eq_zero(a)
1840 || !TEST_BN_ge_zero(a))
1843 /* Test that BN_div never gives negative zero in the remainder. */
1844 if (!TEST_true(BN_set_word(denominator, 1))
1845 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
1846 || !TEST_BN_eq_zero(b)
1847 || !TEST_BN_ge_zero(b))
1850 BN_free(denominator);
1851 numerator = denominator = NULL;
1854 /* Test that BN_set_negative will not produce a negative zero. */
1856 BN_set_negative(a, 1);
1857 if (BN_is_negative(a))
1867 BN_free(denominator);
1871 static int test_badmod(void)
1873 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
1874 BN_MONT_CTX *mont = NULL;
1877 if (!TEST_ptr(a = BN_new())
1878 || !TEST_ptr(b = BN_new())
1879 || !TEST_ptr(zero = BN_new())
1880 || !TEST_ptr(mont = BN_MONT_CTX_new()))
1884 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
1888 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
1892 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
1896 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1901 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1906 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
1910 /* Some operations also may not be used with an even modulus. */
1911 if (!TEST_true(BN_set_word(b, 16)))
1914 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
1918 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1923 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1933 BN_MONT_CTX_free(mont);
1937 static int test_expmodzero(void)
1939 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
1942 if (!TEST_ptr(zero = BN_new())
1943 || !TEST_ptr(a = BN_new())
1944 || !TEST_ptr(r = BN_new()))
1948 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
1949 || !TEST_BN_eq_zero(r)
1950 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
1952 || !TEST_BN_eq_zero(r)
1953 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
1956 || !TEST_BN_eq_zero(r)
1957 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
1958 BN_value_one(), NULL, NULL))
1959 || !TEST_BN_eq_zero(r))
1970 static int test_smallprime(void)
1972 static const int kBits = 10;
1976 if (!TEST_ptr(r = BN_new())
1977 || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
1979 || !TEST_int_eq(BN_num_bits(r), kBits))
1988 static int test_3_is_prime(void)
1994 * For a long time, small primes were not considered prime when
1995 * do_trial_division was set.
1997 if (!TEST_ptr(r = BN_new())
1998 || !TEST_true(BN_set_word(r, 3))
1999 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
2000 0 /* do_trial_division */, NULL), 1)
2001 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
2002 1 /* do_trial_division */, NULL), 1))
2012 static int file_test_run(STANZA *s)
2014 static const FILETEST filetests[] = {
2016 {"LShift1", file_lshift1},
2017 {"LShift", file_lshift},
2018 {"RShift", file_rshift},
2019 {"Square", file_square},
2020 {"Product", file_product},
2021 {"Quotient", file_quotient},
2022 {"ModMul", file_modmul},
2023 {"ModExp", file_modexp},
2025 {"ModSqrt", file_modsqrt},
2027 int numtests = OSSL_NELEM(filetests);
2028 const FILETEST *tp = filetests;
2030 for ( ; --numtests >= 0; tp++) {
2031 if (findattr(s, tp->name) != NULL) {
2033 TEST_info("%s:%d: Failed %s test",
2034 s->test_file, s->start, tp->name);
2040 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2044 static int run_file_tests(int i)
2047 char *testfile = test_get_argument(i);
2050 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2052 if (!test_start_file(s, testfile)) {
2057 /* Read test file. */
2058 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2059 if (s->numpairs == 0)
2061 if (!file_test_run(s))
2064 test_clearstanza(s);
2074 int setup_tests(void)
2076 int n = test_get_argument_count();
2078 if (!TEST_ptr(ctx = BN_CTX_new()))
2083 ADD_TEST(test_div_recip);
2085 ADD_TEST(test_modexp_mont5);
2086 ADD_TEST(test_kronecker);
2087 ADD_TEST(test_rand);
2088 ADD_TEST(test_bn2padded);
2089 ADD_TEST(test_dec2bn);
2090 ADD_TEST(test_hex2bn);
2091 ADD_TEST(test_asc2bn);
2092 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2093 ADD_TEST(test_negzero);
2094 ADD_TEST(test_badmod);
2095 ADD_TEST(test_expmodzero);
2096 ADD_TEST(test_smallprime);
2097 #ifndef OPENSSL_NO_EC2M
2098 ADD_TEST(test_gf2m_add);
2099 ADD_TEST(test_gf2m_mod);
2100 ADD_TEST(test_gf2m_mul);
2101 ADD_TEST(test_gf2m_sqr);
2102 ADD_TEST(test_gf2m_modinv);
2103 ADD_TEST(test_gf2m_moddiv);
2104 ADD_TEST(test_gf2m_modexp);
2105 ADD_TEST(test_gf2m_modsqrt);
2106 ADD_TEST(test_gf2m_modsolvequad);
2108 ADD_TEST(test_3_is_prime);
2110 ADD_ALL_TESTS(run_file_tests, n);
2115 void cleanup_tests(void)