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, return number of bytes parsed.
85 static int parseBN(BIGNUM **out, const char *in)
88 return BN_hex2bn(out, in);
91 static int parsedecBN(BIGNUM **out, const char *in)
94 return BN_dec2bn(out, in);
97 static BIGNUM *getBN(STANZA *s, const char *attribute)
102 if ((hex = findattr(s, attribute)) == NULL) {
103 TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
107 if (parseBN(&ret, hex) != (int)strlen(hex)) {
108 TEST_error("Could not decode '%s'", hex);
114 static int getint(STANZA *s, int *out, const char *attribute)
120 if (!TEST_ptr(ret = getBN(s, attribute))
121 || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
131 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
133 if (BN_cmp(expected, actual) == 0)
136 TEST_error("unexpected %s value", op);
137 TEST_BN_eq(expected, actual);
143 * Return a "random" flag for if a BN should be negated.
145 static int rand_neg(void)
147 static unsigned int neg = 0;
148 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
150 return sign[(neg++) % 8];
154 static int test_sub(void)
156 BIGNUM *a = NULL, *b = NULL, *c = NULL;
159 if (!TEST_ptr(a = BN_new())
160 || !TEST_ptr(b = BN_new())
161 || !TEST_ptr(c = BN_new()))
164 for (i = 0; i < NUM0 + NUM1; i++) {
166 BN_bntest_rand(a, 512, 0, 0);
168 if (!TEST_int_ne(BN_set_bit(a, i), 0))
172 BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
179 if (!TEST_BN_eq_zero(c))
191 static int test_div_recip(void)
193 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
194 BN_RECP_CTX *recp = NULL;
197 if (!TEST_ptr(a = BN_new())
198 || !TEST_ptr(b = BN_new())
199 || !TEST_ptr(c = BN_new())
200 || !TEST_ptr(d = BN_new())
201 || !TEST_ptr(e = BN_new())
202 || !TEST_ptr(recp = BN_RECP_CTX_new()))
205 for (i = 0; i < NUM0 + NUM1; i++) {
207 BN_bntest_rand(a, 400, 0, 0);
212 BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
215 BN_RECP_CTX_set(recp, b, ctx);
216 BN_div_recp(d, c, a, recp, ctx);
217 BN_mul(e, d, b, ctx);
220 if (!TEST_BN_eq_zero(d))
230 BN_RECP_CTX_free(recp);
235 static int test_mod(void)
237 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
240 if (!TEST_ptr(a = BN_new())
241 || !TEST_ptr(b = BN_new())
242 || !TEST_ptr(c = BN_new())
243 || !TEST_ptr(d = BN_new())
244 || !TEST_ptr(e = BN_new()))
247 BN_bntest_rand(a, 1024, 0, 0);
248 for (i = 0; i < NUM0; i++) {
249 BN_bntest_rand(b, 450 + i * 10, 0, 0);
252 BN_mod(c, a, b, ctx);
253 BN_div(d, e, a, b, ctx);
255 if (!TEST_BN_eq_zero(e))
268 static const char *bn1strings[] = {
269 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
270 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
271 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
272 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
273 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
274 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
275 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
276 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
277 "0000000000000000000000000000000000000000000000000000000000000000",
278 "0000000000000000000000000000000000000000000000000000000000000000",
279 "0000000000000000000000000000000000000000000000000000000000000000",
280 "0000000000000000000000000000000000000000000000000000000000000000",
281 "0000000000000000000000000000000000000000000000000000000000000000",
282 "0000000000000000000000000000000000000000000000000000000000000000",
283 "0000000000000000000000000000000000000000000000000000000000000000",
284 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
288 static const char *bn2strings[] = {
289 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
290 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
291 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
292 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
293 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
294 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
295 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
296 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
297 "0000000000000000000000000000000000000000000000000000000000000000",
298 "0000000000000000000000000000000000000000000000000000000000000000",
299 "0000000000000000000000000000000000000000000000000000000000000000",
300 "0000000000000000000000000000000000000000000000000000000000000000",
301 "0000000000000000000000000000000000000000000000000000000000000000",
302 "0000000000000000000000000000000000000000000000000000000000000000",
303 "0000000000000000000000000000000000000000000000000000000000000000",
304 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
308 static char *glue(const char *list[])
314 for (i = 0; list[i] != NULL; i++)
315 len += strlen(list[i]);
316 if (!TEST_ptr(p = save = OPENSSL_malloc(len + 1)))
318 for (i = 0; list[i] != NULL; i++)
319 p += strlen(strcpy(p, list[i]));
324 * Test constant-time modular exponentiation with 1024-bit inputs, which on
325 * x86_64 cause a different code branch to be taken.
327 static int test_modexp_mont5(void)
329 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
330 BIGNUM *b = NULL, *n = NULL, *c = NULL;
331 BN_MONT_CTX *mont = NULL;
335 if (!TEST_ptr(a = BN_new())
336 || !TEST_ptr(p = BN_new())
337 || !TEST_ptr(m = BN_new())
338 || !TEST_ptr(d = BN_new())
339 || !TEST_ptr(e = BN_new())
340 || !TEST_ptr(b = BN_new())
341 || !TEST_ptr(n = BN_new())
342 || !TEST_ptr(c = BN_new())
343 || !TEST_ptr(mont = BN_MONT_CTX_new()))
346 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
348 BN_bntest_rand(a, 1024, 0, 0);
350 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
352 if (!TEST_BN_eq_one(d))
355 /* Regression test for carry bug in mulx4x_mont */
357 "7878787878787878787878787878787878787878787878787878787878787878"
358 "7878787878787878787878787878787878787878787878787878787878787878"
359 "7878787878787878787878787878787878787878787878787878787878787878"
360 "7878787878787878787878787878787878787878787878787878787878787878");
362 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
363 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
364 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
365 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
367 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
368 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
369 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
370 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
371 BN_MONT_CTX_set(mont, n, ctx);
372 BN_mod_mul_montgomery(c, a, b, mont, ctx);
373 BN_mod_mul_montgomery(d, b, a, mont, ctx);
374 if (!TEST_BN_eq(c, d))
377 /* Regression test for carry bug in sqr[x]8x_mont */
378 bigstring = glue(bn1strings);
379 BN_hex2bn(&n, bigstring);
380 OPENSSL_free(bigstring);
381 bigstring = glue(bn2strings);
382 BN_hex2bn(&a, bigstring);
383 OPENSSL_free(bigstring);
386 BN_MONT_CTX_set(mont, n, ctx);
387 BN_mod_mul_montgomery(c, a, a, mont, ctx);
388 BN_mod_mul_montgomery(d, a, b, mont, ctx);
389 if (!TEST_BN_eq(c, d))
392 /* Regression test for carry bug in bn_sqrx8x_internal */
394 static const char *ahex[] = {
395 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
396 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
397 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
398 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
399 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
400 "9544D954000000006C0000000000000000000000000000000000000000000000",
401 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
402 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
403 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
404 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
405 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
406 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
409 static const char *nhex[] = {
410 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
411 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
412 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
413 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
414 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
415 "00000010000000006C0000000000000000000000000000000000000000000000",
416 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
417 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
418 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
419 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
420 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
421 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
425 bigstring = glue(ahex);
426 BN_hex2bn(&a, bigstring);
427 OPENSSL_free(bigstring);
428 bigstring = glue(nhex);
429 BN_hex2bn(&n, bigstring);
430 OPENSSL_free(bigstring);
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))
441 BN_bntest_rand(p, 1024, 0, 0);
443 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
444 || !TEST_BN_eq_zero(d))
448 * Craft an input whose Montgomery representation is 1, i.e., shorter
449 * than the modulus m, in order to test the const time precomputation
450 * scattering/gathering.
453 BN_MONT_CTX_set(mont, m, ctx);
454 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
455 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
456 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
457 || !TEST_BN_eq(a, d))
460 /* Finally, some regular test vectors. */
461 BN_bntest_rand(e, 1024, 0, 0);
462 if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
463 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
464 || !TEST_BN_eq(a, d))
470 BN_MONT_CTX_free(mont);
482 #ifndef OPENSSL_NO_EC2M
483 static int test_gf2m_add(void)
485 BIGNUM *a = NULL, *b = NULL, *c = NULL;
488 if (!TEST_ptr(a = BN_new())
489 || !TEST_ptr(b = BN_new())
490 || !TEST_ptr(c = BN_new()))
493 for (i = 0; i < NUM0; i++) {
494 BN_rand(a, 512, 0, 0);
495 BN_copy(b, BN_value_one());
498 BN_GF2m_add(c, a, b);
499 /* Test that two added values have the correct parity. */
500 if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
501 || (!BN_is_odd(a) && !BN_is_odd(c))))
503 BN_GF2m_add(c, c, c);
504 /* Test that c + c = 0. */
505 if (!TEST_BN_eq_zero(c))
516 static int test_gf2m_mod(void)
518 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
521 if (!TEST_ptr(a = BN_new())
522 || !TEST_ptr(b[0] = BN_new())
523 || !TEST_ptr(b[1] = BN_new())
524 || !TEST_ptr(c = BN_new())
525 || !TEST_ptr(d = BN_new())
526 || !TEST_ptr(e = BN_new()))
529 BN_GF2m_arr2poly(p0, b[0]);
530 BN_GF2m_arr2poly(p1, b[1]);
532 for (i = 0; i < NUM0; i++) {
533 BN_bntest_rand(a, 1024, 0, 0);
534 for (j = 0; j < 2; j++) {
535 BN_GF2m_mod(c, a, b[j]);
536 BN_GF2m_add(d, a, c);
537 BN_GF2m_mod(e, d, b[j]);
538 /* Test that a + (a mod p) mod p == 0. */
539 if (!TEST_BN_eq_zero(e))
554 static int test_gf2m_mul(void)
556 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
557 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
560 if (!TEST_ptr(a = BN_new())
561 || !TEST_ptr(b[0] = BN_new())
562 || !TEST_ptr(b[1] = BN_new())
563 || !TEST_ptr(c = BN_new())
564 || !TEST_ptr(d = BN_new())
565 || !TEST_ptr(e = BN_new())
566 || !TEST_ptr(f = BN_new())
567 || !TEST_ptr(g = BN_new())
568 || !TEST_ptr(h = BN_new()))
571 BN_GF2m_arr2poly(p0, b[0]);
572 BN_GF2m_arr2poly(p1, b[1]);
574 for (i = 0; i < NUM0; i++) {
575 BN_bntest_rand(a, 1024, 0, 0);
576 BN_bntest_rand(c, 1024, 0, 0);
577 BN_bntest_rand(d, 1024, 0, 0);
578 for (j = 0; j < 2; j++) {
579 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
580 BN_GF2m_add(f, a, d);
581 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
582 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
583 BN_GF2m_add(f, e, g);
584 BN_GF2m_add(f, f, h);
585 /* Test that (a+d)*c = a*c + d*c. */
586 if (!TEST_BN_eq_zero(f))
605 static int test_gf2m_sqr(void)
607 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
610 if (!TEST_ptr(a = BN_new())
611 || !TEST_ptr(b[0] = BN_new())
612 || !TEST_ptr(b[1] = BN_new())
613 || !TEST_ptr(c = BN_new())
614 || !TEST_ptr(d = BN_new()))
617 BN_GF2m_arr2poly(p0, b[0]);
618 BN_GF2m_arr2poly(p1, b[1]);
620 for (i = 0; i < NUM0; i++) {
621 BN_bntest_rand(a, 1024, 0, 0);
622 for (j = 0; j < 2; j++) {
623 BN_GF2m_mod_sqr(c, a, b[j], ctx);
625 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
626 BN_GF2m_add(d, c, d);
627 /* Test that a*a = a^2. */
628 if (!TEST_BN_eq_zero(d))
642 static int test_gf2m_modinv(void)
644 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
647 if (!TEST_ptr(a = BN_new())
648 || !TEST_ptr(b[0] = BN_new())
649 || !TEST_ptr(b[1] = BN_new())
650 || !TEST_ptr(c = BN_new())
651 || !TEST_ptr(d = BN_new()))
654 BN_GF2m_arr2poly(p0, b[0]);
655 BN_GF2m_arr2poly(p1, b[1]);
657 for (i = 0; i < NUM0; i++) {
658 BN_bntest_rand(a, 512, 0, 0);
659 for (j = 0; j < 2; j++) {
660 BN_GF2m_mod_inv(c, a, b[j], ctx);
661 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
662 /* Test that ((1/a)*a) = 1. */
663 if (!TEST_BN_eq_one(d))
677 static int test_gf2m_moddiv(void)
679 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
680 BIGNUM *e = NULL, *f = NULL;
683 if (!TEST_ptr(a = BN_new())
684 || !TEST_ptr(b[0] = BN_new())
685 || !TEST_ptr(b[1] = BN_new())
686 || !TEST_ptr(c = BN_new())
687 || !TEST_ptr(d = BN_new())
688 || !TEST_ptr(e = BN_new())
689 || !TEST_ptr(f = BN_new()))
692 BN_GF2m_arr2poly(p0, b[0]);
693 BN_GF2m_arr2poly(p1, b[1]);
695 for (i = 0; i < NUM0; i++) {
696 BN_bntest_rand(a, 512, 0, 0);
697 BN_bntest_rand(c, 512, 0, 0);
698 for (j = 0; j < 2; j++) {
699 BN_GF2m_mod_div(d, a, c, b[j], ctx);
700 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
701 BN_GF2m_mod_div(f, a, e, b[j], ctx);
702 /* Test that ((a/c)*c)/a = 1. */
703 if (!TEST_BN_eq_one(f))
719 static int test_gf2m_modexp(void)
721 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
722 BIGNUM *e = NULL, *f = NULL;
725 if (!TEST_ptr(a = BN_new())
726 || !TEST_ptr(b[0] = BN_new())
727 || !TEST_ptr(b[1] = BN_new())
728 || !TEST_ptr(c = BN_new())
729 || !TEST_ptr(d = BN_new())
730 || !TEST_ptr(e = BN_new())
731 || !TEST_ptr(f = BN_new()))
734 BN_GF2m_arr2poly(p0, b[0]);
735 BN_GF2m_arr2poly(p1, b[1]);
737 for (i = 0; i < NUM0; i++) {
738 BN_bntest_rand(a, 512, 0, 0);
739 BN_bntest_rand(c, 512, 0, 0);
740 BN_bntest_rand(d, 512, 0, 0);
741 for (j = 0; j < 2; j++) {
742 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
743 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
744 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
746 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
747 BN_GF2m_add(f, e, f);
748 /* Test that a^(c+d)=a^c*a^d. */
749 if (!TEST_BN_eq_zero(f))
765 static int test_gf2m_modsqrt(void)
767 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
768 BIGNUM *e = NULL, *f = NULL;
771 if (!TEST_ptr(a = BN_new())
772 || !TEST_ptr(b[0] = BN_new())
773 || !TEST_ptr(b[1] = BN_new())
774 || !TEST_ptr(c = BN_new())
775 || !TEST_ptr(d = BN_new())
776 || !TEST_ptr(e = BN_new())
777 || !TEST_ptr(f = BN_new()))
780 BN_GF2m_arr2poly(p0, b[0]);
781 BN_GF2m_arr2poly(p1, b[1]);
783 for (i = 0; i < NUM0; i++) {
784 BN_bntest_rand(a, 512, 0, 0);
785 for (j = 0; j < 2; j++) {
786 BN_GF2m_mod(c, a, b[j]);
787 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
788 BN_GF2m_mod_sqr(e, d, b[j], ctx);
789 BN_GF2m_add(f, c, e);
790 /* Test that d^2 = a, where d = sqrt(a). */
791 if (!TEST_BN_eq_zero(f))
807 static int test_gf2m_modsolvequad(void)
809 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
811 int i, j, s = 0, t, st = 0;
813 if (!TEST_ptr(a = BN_new())
814 || !TEST_ptr(b[0] = BN_new())
815 || !TEST_ptr(b[1] = BN_new())
816 || !TEST_ptr(c = BN_new())
817 || !TEST_ptr(d = BN_new())
818 || !TEST_ptr(e = BN_new()))
821 BN_GF2m_arr2poly(p0, b[0]);
822 BN_GF2m_arr2poly(p1, b[1]);
824 for (i = 0; i < NUM0; i++) {
825 BN_bntest_rand(a, 512, 0, 0);
826 for (j = 0; j < 2; j++) {
827 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
830 BN_GF2m_mod_sqr(d, c, b[j], ctx);
831 BN_GF2m_add(d, c, d);
832 BN_GF2m_mod(e, a, b[j]);
833 BN_GF2m_add(e, e, d);
835 * Test that solution of quadratic c satisfies c^2 + c = a.
837 if (!TEST_BN_eq_zero(e))
842 if (!TEST_int_ge(s, 0)) {
843 TEST_info("%d tests found no roots; probably an error", NUM0);
858 static int test_kronecker(void)
860 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
861 int i, legendre, kronecker, st = 0;
863 if (!TEST_ptr(a = BN_new())
864 || !TEST_ptr(b = BN_new())
865 || !TEST_ptr(r = BN_new())
866 || !TEST_ptr(t = BN_new()))
870 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
871 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
872 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
873 * generate a random prime b and compare these values for a number of
874 * random a's. (That is, we run the Solovay-Strassen primality test to
875 * confirm that b is prime, except that we don't want to test whether b
876 * is prime but whether BN_kronecker works.)
879 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
883 for (i = 0; i < NUM0; i++) {
884 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
888 /* t := (|b|-1)/2 (note that b is odd) */
889 if (!TEST_true(BN_copy(t, b)))
892 if (!TEST_true(BN_sub_word(t, 1)))
894 if (!TEST_true(BN_rshift1(t, t)))
899 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
903 if (BN_is_word(r, 1))
905 else if (BN_is_zero(r))
908 if (!TEST_true(BN_add_word(r, 1)))
910 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
911 TEST_info("Legendre symbol computation failed");
917 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
919 /* we actually need BN_kronecker(a, |b|) */
920 if (a->neg && b->neg)
921 kronecker = -kronecker;
923 if (!TEST_int_eq(legendre, kronecker))
936 static int file_sum(STANZA *s)
938 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
942 if (!TEST_ptr(a = getBN(s, "A"))
943 || !TEST_ptr(b = getBN(s, "B"))
944 || !TEST_ptr(sum = getBN(s, "Sum"))
945 || !TEST_ptr(ret = BN_new()))
948 if (!TEST_true(BN_add(ret, a, b))
949 || !equalBN("A + B", sum, ret)
950 || !TEST_true(BN_sub(ret, sum, a))
951 || !equalBN("Sum - A", b, ret)
952 || !TEST_true(BN_sub(ret, sum, b))
953 || !equalBN("Sum - B", a, ret))
957 * Test that the functions work when |r| and |a| point to the same BIGNUM,
958 * or when |r| and |b| point to the same BIGNUM.
959 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
961 if (!TEST_true(BN_copy(ret, a))
962 || !TEST_true(BN_add(ret, ret, b))
963 || !equalBN("A + B (r is a)", sum, ret)
964 || !TEST_true(BN_copy(ret, b))
965 || !TEST_true(BN_add(ret, a, ret))
966 || !equalBN("A + B (r is b)", sum, ret)
967 || !TEST_true(BN_copy(ret, sum))
968 || !TEST_true(BN_sub(ret, ret, a))
969 || !equalBN("Sum - A (r is a)", b, ret)
970 || !TEST_true(BN_copy(ret, a))
971 || !TEST_true(BN_sub(ret, sum, ret))
972 || !equalBN("Sum - A (r is b)", b, ret)
973 || !TEST_true(BN_copy(ret, sum))
974 || !TEST_true(BN_sub(ret, ret, b))
975 || !equalBN("Sum - B (r is a)", a, ret)
976 || !TEST_true(BN_copy(ret, b))
977 || !TEST_true(BN_sub(ret, sum, ret))
978 || !equalBN("Sum - B (r is b)", a, ret))
982 * Test BN_uadd() and BN_usub() with the prerequisites they are
983 * documented as having. Note that these functions are frequently used
984 * when the prerequisites don't hold. In those cases, they are supposed
985 * to work as if the prerequisite hold, but we don't test that yet.
988 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
989 if (!TEST_true(BN_uadd(ret, a, b))
990 || !equalBN("A +u B", sum, ret)
991 || !TEST_true(BN_usub(ret, sum, a))
992 || !equalBN("Sum -u A", b, ret)
993 || !TEST_true(BN_usub(ret, sum, b))
994 || !equalBN("Sum -u B", a, ret))
997 * Test that the functions work when |r| and |a| point to the same
998 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
999 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1001 if (!TEST_true(BN_copy(ret, a))
1002 || !TEST_true(BN_uadd(ret, ret, b))
1003 || !equalBN("A +u B (r is a)", sum, ret)
1004 || !TEST_true(BN_copy(ret, b))
1005 || !TEST_true(BN_uadd(ret, a, ret))
1006 || !equalBN("A +u B (r is b)", sum, ret)
1007 || !TEST_true(BN_copy(ret, sum))
1008 || !TEST_true(BN_usub(ret, ret, a))
1009 || !equalBN("Sum -u A (r is a)", b, ret)
1010 || !TEST_true(BN_copy(ret, a))
1011 || !TEST_true(BN_usub(ret, sum, ret))
1012 || !equalBN("Sum -u A (r is b)", b, ret)
1013 || !TEST_true(BN_copy(ret, sum))
1014 || !TEST_true(BN_usub(ret, ret, b))
1015 || !equalBN("Sum -u B (r is a)", a, ret)
1016 || !TEST_true(BN_copy(ret, b))
1017 || !TEST_true(BN_usub(ret, sum, ret))
1018 || !equalBN("Sum -u B (r is b)", a, ret))
1023 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1025 b_word = BN_get_word(b);
1026 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1027 if (!TEST_true(BN_copy(ret, a))
1028 || !TEST_true(BN_add_word(ret, b_word))
1029 || !equalBN("A + B (word)", sum, ret)
1030 || !TEST_true(BN_copy(ret, sum))
1031 || !TEST_true(BN_sub_word(ret, b_word))
1032 || !equalBN("Sum - B (word)", a, ret))
1045 static int file_lshift1(STANZA *s)
1047 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1048 BIGNUM *two = NULL, *remainder = NULL;
1051 if (!TEST_ptr(a = getBN(s, "A"))
1052 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1053 || !TEST_ptr(zero = BN_new())
1054 || !TEST_ptr(ret = BN_new())
1055 || !TEST_ptr(two = BN_new())
1056 || !TEST_ptr(remainder = BN_new()))
1061 if (!TEST_true(BN_set_word(two, 2))
1062 || !TEST_true(BN_add(ret, a, a))
1063 || !equalBN("A + A", lshift1, ret)
1064 || !TEST_true(BN_mul(ret, a, two, ctx))
1065 || !equalBN("A * 2", lshift1, ret)
1066 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1067 || !equalBN("LShift1 / 2", a, ret)
1068 || !equalBN("LShift1 % 2", zero, remainder)
1069 || !TEST_true(BN_lshift1(ret, a))
1070 || !equalBN("A << 1", lshift1, ret)
1071 || !TEST_true(BN_rshift1(ret, lshift1))
1072 || !equalBN("LShift >> 1", a, ret)
1073 || !TEST_true(BN_rshift1(ret, lshift1))
1074 || !equalBN("LShift >> 1", a, ret))
1077 /* Set the LSB to 1 and test rshift1 again. */
1078 if (!TEST_true(BN_set_bit(lshift1, 0))
1079 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1080 || !equalBN("(LShift1 | 1) / 2", a, ret)
1081 || !TEST_true(BN_rshift1(ret, lshift1))
1082 || !equalBN("(LShift | 1) >> 1", a, ret))
1097 static int file_lshift(STANZA *s)
1099 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1102 if (!TEST_ptr(a = getBN(s, "A"))
1103 || !TEST_ptr(lshift = getBN(s, "LShift"))
1104 || !TEST_ptr(ret = BN_new()))
1106 if (!TEST_true(BN_lshift(ret, a, n))
1107 || !equalBN("A << N", lshift, ret)
1108 || !TEST_true(BN_rshift(ret, lshift, n))
1109 || !equalBN("A >> N", a, ret))
1120 static int file_rshift(STANZA *s)
1122 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1125 if (!TEST_ptr(a = getBN(s, "A"))
1126 || !TEST_ptr(rshift = getBN(s, "RShift"))
1127 || !TEST_ptr(ret = BN_new())
1128 || !getint(s, &n, "N"))
1131 if (!TEST_true(BN_rshift(ret, a, n))
1132 || !equalBN("A >> N", rshift, ret))
1135 /* If N == 1, try with rshift1 as well */
1137 if (!TEST_true(BN_rshift1(ret, a))
1138 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1150 static int file_square(STANZA *s)
1152 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1153 BIGNUM *remainder = NULL, *tmp = NULL;
1156 if (!TEST_ptr(a = getBN(s, "A"))
1157 || !TEST_ptr(square = getBN(s, "Square"))
1158 || !TEST_ptr(zero = BN_new())
1159 || !TEST_ptr(ret = BN_new())
1160 || !TEST_ptr(remainder = BN_new()))
1164 if (!TEST_true(BN_sqr(ret, a, ctx))
1165 || !equalBN("A^2", square, ret)
1166 || !TEST_true(BN_mul(ret, a, a, ctx))
1167 || !equalBN("A * A", square, ret)
1168 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1169 || !equalBN("Square / A", a, ret)
1170 || !equalBN("Square % A", zero, remainder))
1174 BN_set_negative(a, 0);
1175 if (!TEST_true(BN_sqrt(ret, square, ctx))
1176 || !equalBN("sqrt(Square)", a, ret))
1179 /* BN_sqrt should fail on non-squares and negative numbers. */
1180 if (!TEST_BN_eq_zero(square)) {
1181 if (!TEST_ptr(tmp = BN_new())
1182 || !TEST_true(BN_copy(tmp, square)))
1184 BN_set_negative(tmp, 1);
1186 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1190 BN_set_negative(tmp, 0);
1191 if (BN_add(tmp, tmp, BN_value_one()))
1193 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1210 static int file_product(STANZA *s)
1212 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1213 BIGNUM *remainder = NULL, *zero = NULL;
1216 if (!TEST_ptr(a = getBN(s, "A"))
1217 || !TEST_ptr(b = getBN(s, "B"))
1218 || !TEST_ptr(product = getBN(s, "Product"))
1219 || !TEST_ptr(ret = BN_new())
1220 || !TEST_ptr(remainder = BN_new())
1221 || !TEST_ptr(zero = BN_new()))
1226 if (!TEST_true(BN_mul(ret, a, b, ctx))
1227 || !equalBN("A * B", product, ret)
1228 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1229 || !equalBN("Product / A", b, ret)
1230 || !equalBN("Product % A", zero, remainder)
1231 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1232 || !equalBN("Product / B", a, ret)
1233 || !equalBN("Product % B", zero, remainder))
1247 static int file_quotient(STANZA *s)
1249 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1250 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1251 BN_ULONG b_word, ret_word;
1254 if (!TEST_ptr(a = getBN(s, "A"))
1255 || !TEST_ptr(b = getBN(s, "B"))
1256 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1257 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1258 || !TEST_ptr(ret = BN_new())
1259 || !TEST_ptr(ret2 = BN_new())
1260 || !TEST_ptr(nnmod = BN_new()))
1263 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1264 || !equalBN("A / B", quotient, ret)
1265 || !equalBN("A % B", remainder, ret2)
1266 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1267 || !TEST_true(BN_add(ret, ret, remainder))
1268 || !equalBN("Quotient * B + Remainder", a, ret))
1272 * Test with BN_mod_word() and BN_div_word() if the divisor is
1275 b_word = BN_get_word(b);
1276 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1277 BN_ULONG remainder_word = BN_get_word(remainder);
1279 assert(remainder_word != (BN_ULONG)-1);
1280 if (!TEST_ptr(BN_copy(ret, a)))
1282 ret_word = BN_div_word(ret, b_word);
1283 if (ret_word != remainder_word) {
1286 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1287 ret_word, remainder_word);
1289 TEST_error("Got A %% B (word) mismatch");
1293 if (!equalBN ("A / B (word)", quotient, ret))
1296 ret_word = BN_mod_word(a, b_word);
1297 if (ret_word != remainder_word) {
1300 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1301 ret_word, remainder_word);
1303 TEST_error("Got A %% B (word) mismatch");
1309 /* Test BN_nnmod. */
1310 if (!BN_is_negative(b)) {
1311 if (!TEST_true(BN_copy(nnmod, remainder))
1312 || (BN_is_negative(nnmod)
1313 && !TEST_true(BN_add(nnmod, nnmod, b)))
1314 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1315 || !equalBN("A % B (non-negative)", nnmod, ret))
1331 static int file_modmul(STANZA *s)
1333 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1336 if (!TEST_ptr(a = getBN(s, "A"))
1337 || !TEST_ptr(b = getBN(s, "B"))
1338 || !TEST_ptr(m = getBN(s, "M"))
1339 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1340 || !TEST_ptr(ret = BN_new()))
1343 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1344 || !equalBN("A * B (mod M)", mod_mul, ret))
1348 /* Reduce |a| and |b| and test the Montgomery version. */
1349 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1350 BIGNUM *a_tmp = BN_new();
1351 BIGNUM *b_tmp = BN_new();
1353 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1354 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1355 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1356 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1357 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1358 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1359 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1361 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1362 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1366 BN_MONT_CTX_free(mont);
1383 static int file_modexp(STANZA *s)
1385 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1386 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1389 if (!TEST_ptr(a = getBN(s, "A"))
1390 || !TEST_ptr(e = getBN(s, "E"))
1391 || !TEST_ptr(m = getBN(s, "M"))
1392 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1393 || !TEST_ptr(ret = BN_new())
1394 || !TEST_ptr(d = BN_new()))
1397 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1398 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1402 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1403 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1404 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1406 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1410 /* Regression test for carry propagation bug in sqr8x_reduction */
1411 BN_hex2bn(&a, "050505050505");
1412 BN_hex2bn(&b, "02");
1414 "4141414141414141414141274141414141414141414141414141414141414141"
1415 "4141414141414141414141414141414141414141414141414141414141414141"
1416 "4141414141414141414141800000000000000000000000000000000000000000"
1417 "0000000000000000000000000000000000000000000000000000000000000000"
1418 "0000000000000000000000000000000000000000000000000000000000000000"
1419 "0000000000000000000000000000000000000000000000000000000001");
1420 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1421 || !TEST_true(BN_mul(e, a, a, ctx))
1422 || !TEST_BN_eq(d, e))
1438 static int file_exp(STANZA *s)
1440 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1443 if (!TEST_ptr(a = getBN(s, "A"))
1444 || !TEST_ptr(e = getBN(s, "E"))
1445 || !TEST_ptr(exp = getBN(s, "Exp"))
1446 || !TEST_ptr(ret = BN_new()))
1449 if (!TEST_true(BN_exp(ret, a, e, ctx))
1450 || !equalBN("A ^ E", exp, ret))
1462 static int file_modsqrt(STANZA *s)
1464 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1467 if (!TEST_ptr(a = getBN(s, "A"))
1468 || !TEST_ptr(p = getBN(s, "P"))
1469 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1470 || !TEST_ptr(ret = BN_new())
1471 || !TEST_ptr(ret2 = BN_new()))
1474 /* There are two possible answers. */
1475 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1476 || !TEST_true(BN_sub(ret2, p, ret)))
1479 /* The first condition should NOT be a test. */
1480 if (BN_cmp(ret2, mod_sqrt) != 0
1481 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1494 static int test_bn2padded(void)
1497 uint8_t zeros[256], out[256], reference[128];
1498 BIGNUM *n = BN_new();
1501 /* Test edge case at 0. */
1504 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1506 memset(out, -1, sizeof(out));
1507 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1509 memset(zeros, 0, sizeof(zeros));
1510 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1513 /* Test a random numbers at various byte lengths. */
1514 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1515 #define TOP_BIT_ON 0
1516 #define BOTTOM_BIT_NOTOUCH 0
1517 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1519 if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1520 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1522 /* Empty buffer should fail. */
1523 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1525 /* One byte short should fail. */
1526 if (BN_bn2bin_padded(out, bytes - 1, n))
1528 /* Exactly right size should encode. */
1529 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1530 || TEST_mem_eq(out, bytes, reference, bytes))
1532 /* Pad up one byte extra. */
1533 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1534 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1535 || !TEST_mem_eq(out, 1, zeros, 1))
1537 /* Pad up to 256. */
1538 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1539 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1541 || !TEST_mem_eq(out, sizseof(out) - bytes,
1542 zeros, sizeof(out) - bytes))
1555 static int test_dec2bn(void)
1560 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1561 || !TEST_BN_eq_word(bn, 0)
1562 || !TEST_BN_eq_zero(bn)
1563 || !TEST_BN_le_zero(bn)
1564 || !TEST_BN_ge_zero(bn)
1565 || !TEST_BN_even(bn))
1570 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1571 || !TEST_BN_eq_word(bn, 256)
1572 || !TEST_BN_ge_zero(bn)
1573 || !TEST_BN_gt_zero(bn)
1574 || !TEST_BN_ne_zero(bn)
1575 || !TEST_BN_even(bn))
1580 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1581 || !TEST_BN_abs_eq_word(bn, 42)
1582 || !TEST_BN_lt_zero(bn)
1583 || !TEST_BN_le_zero(bn)
1584 || !TEST_BN_ne_zero(bn)
1585 || !TEST_BN_even(bn))
1590 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1591 || !TEST_BN_eq_word(bn, 1)
1592 || !TEST_BN_ne_zero(bn)
1593 || !TEST_BN_gt_zero(bn)
1594 || !TEST_BN_ge_zero(bn)
1595 || !TEST_BN_eq_one(bn)
1596 || !TEST_BN_odd(bn))
1601 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1602 || !TEST_BN_eq_zero(bn)
1603 || !TEST_BN_ge_zero(bn)
1604 || !TEST_BN_le_zero(bn)
1605 || !TEST_BN_even(bn))
1610 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1611 || !TEST_BN_abs_eq_word(bn, 42)
1612 || !TEST_BN_ge_zero(bn)
1613 || !TEST_BN_gt_zero(bn)
1614 || !TEST_BN_ne_zero(bn)
1615 || !TEST_BN_even(bn))
1624 static int test_hex2bn(void)
1629 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1630 || !TEST_BN_eq_zero(bn)
1631 || !TEST_BN_ge_zero(bn)
1632 || !TEST_BN_even(bn))
1637 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1638 || !TEST_BN_eq_word(bn, 0x256)
1639 || !TEST_BN_ge_zero(bn)
1640 || !TEST_BN_gt_zero(bn)
1641 || !TEST_BN_ne_zero(bn)
1642 || !TEST_BN_even(bn))
1647 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1648 || !TEST_BN_abs_eq_word(bn, 0x42)
1649 || !TEST_BN_lt_zero(bn)
1650 || !TEST_BN_le_zero(bn)
1651 || !TEST_BN_ne_zero(bn)
1652 || !TEST_BN_even(bn))
1657 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1658 || !TEST_BN_eq_word(bn, 0xCB)
1659 || !TEST_BN_ge_zero(bn)
1660 || !TEST_BN_gt_zero(bn)
1661 || !TEST_BN_ne_zero(bn)
1662 || !TEST_BN_odd(bn))
1667 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1668 || !TEST_BN_eq_zero(bn)
1669 || !TEST_BN_ge_zero(bn)
1670 || !TEST_BN_le_zero(bn)
1671 || !TEST_BN_even(bn))
1676 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1677 || !TEST_BN_eq_word(bn, 0xabc)
1678 || !TEST_BN_ge_zero(bn)
1679 || !TEST_BN_gt_zero(bn)
1680 || !TEST_BN_ne_zero(bn)
1681 || !TEST_BN_even(bn))
1690 static int test_asc2bn(void)
1695 if (!TEST_ptr(bn = BN_new()))
1698 if (!TEST_true(BN_asc2bn(&bn, "0"))
1699 || !TEST_BN_eq_zero(bn)
1700 || !TEST_BN_ge_zero(bn))
1703 if (!TEST_true(BN_asc2bn(&bn, "256"))
1704 || !TEST_BN_eq_word(bn, 256)
1705 || !TEST_BN_ge_zero(bn))
1708 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1709 || !TEST_BN_abs_eq_word(bn, 42)
1710 || !TEST_BN_lt_zero(bn))
1713 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1714 || !TEST_BN_eq_word(bn, 0x1234)
1715 || !TEST_BN_ge_zero(bn))
1718 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1719 || !TEST_BN_eq_word(bn, 0x1234)
1720 || !TEST_BN_ge_zero(bn))
1723 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1724 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1725 || !TEST_BN_lt_zero(bn))
1728 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1729 || !TEST_BN_eq_zero(bn)
1730 || !TEST_BN_ge_zero(bn))
1733 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1734 || !TEST_BN_eq_word(bn, 123)
1735 || !TEST_BN_ge_zero(bn))
1744 static const MPITEST kMPITests[] = {
1745 {"0", "\x00\x00\x00\x00", 4},
1746 {"1", "\x00\x00\x00\x01\x01", 5},
1747 {"-1", "\x00\x00\x00\x01\x81", 5},
1748 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1749 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1750 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1753 static int test_mpi(int i)
1756 const MPITEST *test = &kMPITests[i];
1757 size_t mpi_len, mpi_len2;
1762 if (!TEST_ptr(bn = BN_new())
1763 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1765 mpi_len = BN_bn2mpi(bn, NULL);
1766 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1769 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1770 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1773 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1776 if (!TEST_BN_eq(bn, bn2)) {
1788 static int test_rand(void)
1793 if (!TEST_ptr(bn = BN_new()))
1796 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1797 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
1798 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
1799 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
1800 || !TEST_BN_eq_one(bn)
1801 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
1802 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
1803 || !TEST_BN_eq_one(bn)
1804 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
1805 || !TEST_BN_eq_word(bn, 3))
1814 static int test_negzero(void)
1816 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
1817 BIGNUM *numerator = NULL, *denominator = NULL;
1818 int consttime, st = 0;
1820 if (!TEST_ptr(a = BN_new())
1821 || !TEST_ptr(b = BN_new())
1822 || !TEST_ptr(c = BN_new())
1823 || !TEST_ptr(d = BN_new()))
1826 /* Test that BN_mul never gives negative zero. */
1827 if (!TEST_true(BN_set_word(a, 1)))
1829 BN_set_negative(a, 1);
1831 if (!TEST_true(BN_mul(c, a, b, ctx)))
1833 if (!TEST_BN_eq_zero(c)
1834 || !TEST_BN_ge_zero(c))
1837 for (consttime = 0; consttime < 2; consttime++) {
1838 if (!TEST_ptr(numerator = BN_new())
1839 || !TEST_ptr(denominator = BN_new()))
1842 BN_set_flags(numerator, BN_FLG_CONSTTIME);
1843 BN_set_flags(denominator, BN_FLG_CONSTTIME);
1845 /* Test that BN_div never gives negative zero in the quotient. */
1846 if (!TEST_true(BN_set_word(numerator, 1))
1847 || !TEST_true(BN_set_word(denominator, 2)))
1849 BN_set_negative(numerator, 1);
1850 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
1851 || !TEST_BN_eq_zero(a)
1852 || !TEST_BN_ge_zero(a))
1855 /* Test that BN_div never gives negative zero in the remainder. */
1856 if (!TEST_true(BN_set_word(denominator, 1))
1857 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
1858 || !TEST_BN_eq_zero(b)
1859 || !TEST_BN_ge_zero(b))
1862 BN_free(denominator);
1863 numerator = denominator = NULL;
1866 /* Test that BN_set_negative will not produce a negative zero. */
1868 BN_set_negative(a, 1);
1869 if (BN_is_negative(a))
1879 BN_free(denominator);
1883 static int test_badmod(void)
1885 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
1886 BN_MONT_CTX *mont = NULL;
1889 if (!TEST_ptr(a = BN_new())
1890 || !TEST_ptr(b = BN_new())
1891 || !TEST_ptr(zero = BN_new())
1892 || !TEST_ptr(mont = BN_MONT_CTX_new()))
1896 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
1900 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
1904 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
1908 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1913 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1918 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
1922 /* Some operations also may not be used with an even modulus. */
1923 if (!TEST_true(BN_set_word(b, 16)))
1926 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
1930 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
1935 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1945 BN_MONT_CTX_free(mont);
1949 static int test_expmodzero(void)
1951 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
1954 if (!TEST_ptr(zero = BN_new())
1955 || !TEST_ptr(a = BN_new())
1956 || !TEST_ptr(r = BN_new()))
1960 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
1961 || !TEST_BN_eq_zero(r)
1962 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
1964 || !TEST_BN_eq_zero(r)
1965 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
1968 || !TEST_BN_eq_zero(r)
1969 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
1970 BN_value_one(), NULL, NULL))
1971 || !TEST_BN_eq_zero(r))
1982 static int test_smallprime(void)
1984 static const int kBits = 10;
1988 if (!TEST_ptr(r = BN_new())
1989 || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
1991 || !TEST_int_eq(BN_num_bits(r), kBits))
2000 static int test_3_is_prime(void)
2006 * For a long time, small primes were not considered prime when
2007 * do_trial_division was set.
2009 if (!TEST_ptr(r = BN_new())
2010 || !TEST_true(BN_set_word(r, 3))
2011 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
2012 0 /* do_trial_division */, NULL), 1)
2013 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
2014 1 /* do_trial_division */, NULL), 1))
2024 static int file_test_run(STANZA *s)
2026 static const FILETEST filetests[] = {
2028 {"LShift1", file_lshift1},
2029 {"LShift", file_lshift},
2030 {"RShift", file_rshift},
2031 {"Square", file_square},
2032 {"Product", file_product},
2033 {"Quotient", file_quotient},
2034 {"ModMul", file_modmul},
2035 {"ModExp", file_modexp},
2037 {"ModSqrt", file_modsqrt},
2039 int numtests = OSSL_NELEM(filetests);
2040 const FILETEST *tp = filetests;
2042 for ( ; --numtests >= 0; tp++) {
2043 if (findattr(s, tp->name) != NULL) {
2045 TEST_info("%s:%d: Failed %s test",
2046 s->test_file, s->start, tp->name);
2052 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2056 static int run_file_tests(int i)
2059 char *testfile = test_get_argument(i);
2062 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2064 if (!test_start_file(s, testfile)) {
2069 /* Read test file. */
2070 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2071 if (s->numpairs == 0)
2073 if (!file_test_run(s))
2076 test_clearstanza(s);
2086 int setup_tests(void)
2088 int n = test_get_argument_count();
2090 if (!TEST_ptr(ctx = BN_CTX_new()))
2095 ADD_TEST(test_div_recip);
2097 ADD_TEST(test_modexp_mont5);
2098 ADD_TEST(test_kronecker);
2099 ADD_TEST(test_rand);
2100 ADD_TEST(test_bn2padded);
2101 ADD_TEST(test_dec2bn);
2102 ADD_TEST(test_hex2bn);
2103 ADD_TEST(test_asc2bn);
2104 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2105 ADD_TEST(test_negzero);
2106 ADD_TEST(test_badmod);
2107 ADD_TEST(test_expmodzero);
2108 ADD_TEST(test_smallprime);
2109 #ifndef OPENSSL_NO_EC2M
2110 ADD_TEST(test_gf2m_add);
2111 ADD_TEST(test_gf2m_mod);
2112 ADD_TEST(test_gf2m_mul);
2113 ADD_TEST(test_gf2m_sqr);
2114 ADD_TEST(test_gf2m_modinv);
2115 ADD_TEST(test_gf2m_moddiv);
2116 ADD_TEST(test_gf2m_modexp);
2117 ADD_TEST(test_gf2m_modsqrt);
2118 ADD_TEST(test_gf2m_modsolvequad);
2120 ADD_TEST(test_3_is_prime);
2122 ADD_ALL_TESTS(run_file_tests, n);
2127 void cleanup_tests(void)