2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
16 #include <internal/numbers.h>
17 #include <openssl/bn.h>
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
22 #include "test_main_custom.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"
42 * Things in boring, not in openssl. TODO we should add them.
44 #define HAVE_BN_PADDED 0
45 #define HAVE_BN_SQRT 0
47 typedef struct pair_st {
52 typedef struct stanza_st {
58 typedef struct filetest_st {
60 int (*func)(STANZA *s);
63 typedef struct mpitest_st {
69 static const int NUM0 = 100; /* number of tests */
70 static const int NUM1 = 50; /* additional tests for some functions */
76 * Look for |key| in the stanza and return it or NULL if not found.
78 static const char *findattr(STANZA *s, const char *key)
83 for ( ; --i >= 0; pp++)
84 if (strcasecmp(pp->key, key) == 0)
90 * Parse BIGNUM, return number of bytes parsed.
92 static int parseBN(BIGNUM **out, const char *in)
95 return BN_hex2bn(out, in);
98 static int parsedecBN(BIGNUM **out, const char *in)
101 return BN_dec2bn(out, in);
104 static BIGNUM *getBN(STANZA *s, const char *attribute)
109 if ((hex = findattr(s, attribute)) == NULL) {
110 fprintf(stderr, "Can't find %s in test at line %d\n",
111 attribute, s->start);
115 if (parseBN(&ret, hex) != (int)strlen(hex)) {
116 fprintf(stderr, "Could not decode '%s'.\n", hex);
122 static int getint(STANZA *s, int *out, const char *attribute)
124 BIGNUM *ret = getBN(s, attribute);
131 if ((word = BN_get_word(ret)) > INT_MAX)
141 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
146 if (BN_cmp(expected, actual) == 0)
149 if (BN_is_zero(expected) && BN_is_negative(expected))
150 exstr = OPENSSL_strdup("-0");
152 exstr = BN_bn2hex(expected);
153 if (BN_is_zero(actual) && BN_is_negative(actual))
154 actstr = OPENSSL_strdup("-0");
156 actstr = BN_bn2hex(actual);
157 if (exstr == NULL || actstr == NULL)
160 fprintf(stderr, "Got %s =\n", op);
161 fprintf(stderr, "\t%s\n", actstr);
162 fprintf(stderr, "wanted:\n");
163 fprintf(stderr, "\t%s\n", exstr);
167 OPENSSL_free(actstr);
173 * Return a "random" flag for if a BN should be negated.
175 static int rand_neg(void)
177 static unsigned int neg = 0;
178 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
180 return sign[(neg++) % 8];
184 static int test_sub()
193 for (i = 0; i < NUM0 + NUM1; i++) {
195 BN_bntest_rand(a, 512, 0, 0);
197 if (BN_set_bit(a, i) == 0)
201 BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
208 if (!BN_is_zero(c)) {
209 printf("Subtract test failed!\n");
220 static int test_div_recip()
222 BIGNUM *a, *b, *c, *d, *e;
226 recp = BN_RECP_CTX_new();
233 for (i = 0; i < NUM0 + NUM1; i++) {
235 BN_bntest_rand(a, 400, 0, 0);
240 BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
243 BN_RECP_CTX_set(recp, b, ctx);
244 BN_div_recp(d, c, a, recp, ctx);
245 BN_mul(e, d, b, ctx);
248 if (!BN_is_zero(d)) {
249 printf("Reciprocal division test failed!\n");
251 BN_print_fp(stdout, a);
253 BN_print_fp(stdout, b);
263 BN_RECP_CTX_free(recp);
268 static int test_mod()
270 BIGNUM *a, *b, *c, *d, *e;
279 BN_bntest_rand(a, 1024, 0, 0);
280 for (i = 0; i < NUM0; i++) {
281 BN_bntest_rand(b, 450 + i * 10, 0, 0);
284 BN_mod(c, a, b, ctx);
285 BN_div(d, e, a, b, ctx);
287 if (!BN_is_zero(e)) {
288 printf("Modulo test failed!\n");
300 static const char *bn1strings[] = {
301 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
302 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
303 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
304 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
305 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
306 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
307 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
308 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
309 "0000000000000000000000000000000000000000000000000000000000000000",
310 "0000000000000000000000000000000000000000000000000000000000000000",
311 "0000000000000000000000000000000000000000000000000000000000000000",
312 "0000000000000000000000000000000000000000000000000000000000000000",
313 "0000000000000000000000000000000000000000000000000000000000000000",
314 "0000000000000000000000000000000000000000000000000000000000000000",
315 "0000000000000000000000000000000000000000000000000000000000000000",
316 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
320 static const char *bn2strings[] = {
321 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
322 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
323 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
324 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
325 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
326 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
327 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
328 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
329 "0000000000000000000000000000000000000000000000000000000000000000",
330 "0000000000000000000000000000000000000000000000000000000000000000",
331 "0000000000000000000000000000000000000000000000000000000000000000",
332 "0000000000000000000000000000000000000000000000000000000000000000",
333 "0000000000000000000000000000000000000000000000000000000000000000",
334 "0000000000000000000000000000000000000000000000000000000000000000",
335 "0000000000000000000000000000000000000000000000000000000000000000",
336 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
340 static char *glue(const char *list[])
346 for (i = 0; list[i] != NULL; i++)
347 len += strlen(list[i]);
348 p = save = OPENSSL_malloc(len + 1);
350 for (i = 0; list[i] != NULL; i++)
351 p += strlen(strcpy(p, list[i]));
357 * Test constant-time modular exponentiation with 1024-bit inputs, which on
358 * x86_64 cause a different code branch to be taken.
360 static int test_modexp_mont5()
362 BIGNUM *a, *p, *m, *d, *e, *b, *n, *c;
374 mont = BN_MONT_CTX_new();
376 BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
378 BN_bntest_rand(a, 1024, 0, 0);
380 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
383 printf("Modular exponentiation test failed!\n");
387 /* Regression test for carry bug in mulx4x_mont */
389 "7878787878787878787878787878787878787878787878787878787878787878"
390 "7878787878787878787878787878787878787878787878787878787878787878"
391 "7878787878787878787878787878787878787878787878787878787878787878"
392 "7878787878787878787878787878787878787878787878787878787878787878");
394 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
395 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
396 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
397 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
399 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
400 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
401 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
402 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
403 BN_MONT_CTX_set(mont, n, ctx);
404 BN_mod_mul_montgomery(c, a, b, mont, ctx);
405 BN_mod_mul_montgomery(d, b, a, mont, ctx);
407 fprintf(stderr, "Montgomery multiplication test failed:"
412 /* Regression test for carry bug in sqr[x]8x_mont */
413 bigstring = glue(bn1strings);
414 BN_hex2bn(&n, bigstring);
415 OPENSSL_free(bigstring);
416 bigstring = glue(bn2strings);
417 BN_hex2bn(&a, bigstring);
418 OPENSSL_free(bigstring);
421 BN_MONT_CTX_set(mont, n, ctx);
422 BN_mod_mul_montgomery(c, a, a, mont, ctx);
423 BN_mod_mul_montgomery(d, a, b, mont, ctx);
425 fprintf(stderr, "Montgomery multiplication test failed:"
431 BN_bntest_rand(p, 1024, 0, 0);
433 if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
435 if (!BN_is_zero(d)) {
436 fprintf(stderr, "Modular exponentiation test failed!\n");
440 * Craft an input whose Montgomery representation is 1, i.e., shorter
441 * than the modulus m, in order to test the const time precomputation
442 * scattering/gathering.
445 BN_MONT_CTX_set(mont, m, ctx);
446 if (!BN_from_montgomery(e, a, mont, ctx))
448 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
450 if (!BN_mod_exp_simple(a, e, p, m, ctx))
452 if (BN_cmp(a, d) != 0) {
453 printf("Modular exponentiation test failed!\n");
456 /* Finally, some regular test vectors. */
457 BN_bntest_rand(e, 1024, 0, 0);
458 if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
460 if (!BN_mod_exp_simple(a, e, p, m, ctx))
462 if (BN_cmp(a, d) != 0) {
463 printf("Modular exponentiation test failed!\n");
466 BN_MONT_CTX_free(mont);
478 #ifndef OPENSSL_NO_EC2M
479 static int test_gf2m_add()
488 for (i = 0; i < NUM0; i++) {
489 BN_rand(a, 512, 0, 0);
490 BN_copy(b, BN_value_one());
493 BN_GF2m_add(c, a, b);
494 /* Test that two added values have the correct parity. */
495 if ((BN_is_odd(a) && BN_is_odd(c))
496 || (!BN_is_odd(a) && !BN_is_odd(c))) {
497 printf("GF(2^m) addition test (a) failed!\n");
500 BN_GF2m_add(c, c, c);
501 /* Test that c + c = 0. */
502 if (!BN_is_zero(c)) {
503 printf("GF(2^m) addition test (b) failed!\n");
515 static int test_gf2m_mod()
517 static int p0[] = { 163, 7, 6, 3, 0, -1 };
518 static int p1[] = { 193, 15, 0, -1 };
519 BIGNUM *a, *b[2], *c, *d, *e;
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 (!BN_is_zero(e)) {
540 printf("GF(2^m) modulo test failed!\n");
556 static int test_gf2m_mul()
558 BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
560 int p0[] = { 163, 7, 6, 3, 0, -1 };
561 int p1[] = { 193, 15, 0, -1 };
573 BN_GF2m_arr2poly(p0, b[0]);
574 BN_GF2m_arr2poly(p1, b[1]);
576 for (i = 0; i < NUM0; i++) {
577 BN_bntest_rand(a, 1024, 0, 0);
578 BN_bntest_rand(c, 1024, 0, 0);
579 BN_bntest_rand(d, 1024, 0, 0);
580 for (j = 0; j < 2; j++) {
581 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
582 BN_GF2m_add(f, a, d);
583 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
584 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
585 BN_GF2m_add(f, e, g);
586 BN_GF2m_add(f, f, h);
587 /* Test that (a+d)*c = a*c + d*c. */
588 if (!BN_is_zero(f)) {
589 printf("GF(2^m) modular multiplication test failed!\n");
608 static int test_gf2m_sqr()
610 BIGNUM *a, *b[2], *c, *d;
612 int p0[] = { 163, 7, 6, 3, 0, -1 };
613 int p1[] = { 193, 15, 0, -1 };
621 BN_GF2m_arr2poly(p0, b[0]);
622 BN_GF2m_arr2poly(p1, b[1]);
624 for (i = 0; i < NUM0; i++) {
625 BN_bntest_rand(a, 1024, 0, 0);
626 for (j = 0; j < 2; j++) {
627 BN_GF2m_mod_sqr(c, a, b[j], ctx);
629 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
630 BN_GF2m_add(d, c, d);
631 /* Test that a*a = a^2. */
632 if (!BN_is_zero(d)) {
633 printf("GF(2^m) modular squaring test failed!\n");
648 static int test_gf2m_modinv()
650 BIGNUM *a, *b[2], *c, *d;
652 int p0[] = { 163, 7, 6, 3, 0, -1 };
653 int p1[] = { 193, 15, 0, -1 };
661 BN_GF2m_arr2poly(p0, b[0]);
662 BN_GF2m_arr2poly(p1, b[1]);
664 for (i = 0; i < NUM0; i++) {
665 BN_bntest_rand(a, 512, 0, 0);
666 for (j = 0; j < 2; j++) {
667 BN_GF2m_mod_inv(c, a, b[j], ctx);
668 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
669 /* Test that ((1/a)*a) = 1. */
671 printf("GF(2^m) modular inversion test failed!\n");
686 static int test_gf2m_moddiv()
688 BIGNUM *a, *b[2], *c, *d, *e, *f;
690 int p0[] = { 163, 7, 6, 3, 0, -1 };
691 int p1[] = { 193, 15, 0, -1 };
701 BN_GF2m_arr2poly(p0, b[0]);
702 BN_GF2m_arr2poly(p1, b[1]);
704 for (i = 0; i < NUM0; i++) {
705 BN_bntest_rand(a, 512, 0, 0);
706 BN_bntest_rand(c, 512, 0, 0);
707 for (j = 0; j < 2; j++) {
708 BN_GF2m_mod_div(d, a, c, b[j], ctx);
709 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
710 BN_GF2m_mod_div(f, a, e, b[j], ctx);
711 /* Test that ((a/c)*c)/a = 1. */
713 printf("GF(2^m) modular division test failed!\n");
730 static int test_gf2m_modexp()
732 BIGNUM *a, *b[2], *c, *d, *e, *f;
734 int p0[] = { 163, 7, 6, 3, 0, -1 };
735 int p1[] = { 193, 15, 0, -1 };
745 BN_GF2m_arr2poly(p0, b[0]);
746 BN_GF2m_arr2poly(p1, b[1]);
748 for (i = 0; i < NUM0; i++) {
749 BN_bntest_rand(a, 512, 0, 0);
750 BN_bntest_rand(c, 512, 0, 0);
751 BN_bntest_rand(d, 512, 0, 0);
752 for (j = 0; j < 2; j++) {
753 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
754 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
755 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
757 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
758 BN_GF2m_add(f, e, f);
759 /* Test that a^(c+d)=a^c*a^d. */
760 if (!BN_is_zero(f)) {
761 printf("GF(2^m) modular exponentiation test failed!\n");
778 static int test_gf2m_modsqrt()
780 BIGNUM *a, *b[2], *c, *d, *e, *f;
782 int p0[] = { 163, 7, 6, 3, 0, -1 };
783 int p1[] = { 193, 15, 0, -1 };
793 BN_GF2m_arr2poly(p0, b[0]);
794 BN_GF2m_arr2poly(p1, b[1]);
796 for (i = 0; i < NUM0; i++) {
797 BN_bntest_rand(a, 512, 0, 0);
798 for (j = 0; j < 2; j++) {
799 BN_GF2m_mod(c, a, b[j]);
800 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
801 BN_GF2m_mod_sqr(e, d, b[j], ctx);
802 BN_GF2m_add(f, c, e);
803 /* Test that d^2 = a, where d = sqrt(a). */
804 if (!BN_is_zero(f)) {
805 printf("GF(2^m) modular square root test failed!\n");
822 static int test_gf2m_modsolvequad()
824 BIGNUM *a, *b[2], *c, *d, *e;
825 int i, j, s = 0, t, st = 0;
826 int p0[] = { 163, 7, 6, 3, 0, -1 };
827 int p1[] = { 193, 15, 0, -1 };
836 BN_GF2m_arr2poly(p0, b[0]);
837 BN_GF2m_arr2poly(p1, b[1]);
839 for (i = 0; i < NUM0; i++) {
840 BN_bntest_rand(a, 512, 0, 0);
841 for (j = 0; j < 2; j++) {
842 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
845 BN_GF2m_mod_sqr(d, c, b[j], ctx);
846 BN_GF2m_add(d, c, d);
847 BN_GF2m_mod(e, a, b[j]);
848 BN_GF2m_add(e, e, d);
850 * Test that solution of quadratic c satisfies c^2 + c = a.
852 if (!BN_is_zero(e)) {
853 printf("GF(2^m) modular solve quadratic test failed!\n");
861 printf("All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
863 printf("this is very unlikely and probably indicates an error.\n");
878 static int test_kronecker()
880 BIGNUM *a, *b, *r, *t;
882 int legendre, kronecker;
889 if (a == NULL || b == NULL || r == NULL || t == NULL)
893 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
894 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
895 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
896 * generate a random prime b and compare these values for a number of
897 * random a's. (That is, we run the Solovay-Strassen primality test to
898 * confirm that b is prime, except that we don't want to test whether b
899 * is prime but whether BN_kronecker works.)
902 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL))
906 for (i = 0; i < NUM0; i++) {
907 if (!BN_bntest_rand(a, 512, 0, 0))
911 /* t := (|b|-1)/2 (note that b is odd) */
915 if (!BN_sub_word(t, 1))
917 if (!BN_rshift1(t, t))
922 if (!BN_mod_exp_recp(r, a, t, b, ctx))
926 if (BN_is_word(r, 1))
928 else if (BN_is_zero(r))
931 if (!BN_add_word(r, 1))
933 if (0 != BN_ucmp(r, b)) {
934 printf("Legendre symbol computation failed\n");
940 kronecker = BN_kronecker(a, b, ctx);
943 /* we actually need BN_kronecker(a, |b|) */
944 if (a->neg && b->neg)
945 kronecker = -kronecker;
947 if (legendre != kronecker) {
948 printf("legendre != kronecker; a = ");
949 BN_print_fp(stdout, a);
951 BN_print_fp(stdout, b);
966 static int file_sum(STANZA *s)
968 BIGNUM *a = getBN(s, "A");
969 BIGNUM *b = getBN(s, "B");
970 BIGNUM *sum = getBN(s, "Sum");
971 BIGNUM *ret = BN_new();
975 if (a == NULL || b == NULL || sum == NULL || ret == NULL)
978 if (!BN_add(ret, a, b)
979 || !equalBN("A + B", sum, ret)
980 || !BN_sub(ret, sum, a)
981 || !equalBN("Sum - A", b, ret)
982 || !BN_sub(ret, sum, b)
983 || !equalBN("Sum - B", a, ret))
987 * Test that the functions work when |r| and |a| point to the same BIGNUM,
988 * or when |r| and |b| point to the same BIGNUM.
989 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
992 || !BN_add(ret, ret, b)
993 || !equalBN("A + B (r is a)", sum, ret)
995 || !BN_add(ret, a, ret)
996 || !equalBN("A + B (r is b)", sum, ret)
997 || !BN_copy(ret, sum)
998 || !BN_sub(ret, ret, a)
999 || !equalBN("Sum - A (r is a)", b, ret)
1001 || !BN_sub(ret, sum, ret)
1002 || !equalBN("Sum - A (r is b)", b, ret)
1003 || !BN_copy(ret, sum)
1004 || !BN_sub(ret, ret, b)
1005 || !equalBN("Sum - B (r is a)", a, ret)
1007 || !BN_sub(ret, sum, ret)
1008 || !equalBN("Sum - B (r is b)", a, ret))
1012 * Test BN_uadd() and BN_usub() with the prerequisites they are
1013 * documented as having. Note that these functions are frequently used
1014 * when the prerequisites don't hold. In those cases, they are supposed
1015 * to work as if the prerequisite hold, but we don't test that yet.
1018 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
1019 if (!BN_uadd(ret, a, b)
1020 || !equalBN("A +u B", sum, ret)
1021 || !BN_usub(ret, sum, a)
1022 || !equalBN("Sum -u A", b, ret)
1023 || !BN_usub(ret, sum, b)
1024 || !equalBN("Sum -u B", a, ret))
1027 * Test that the functions work when |r| and |a| point to the same
1028 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1029 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1031 if (!BN_copy(ret, a)
1032 || !BN_uadd(ret, ret, b)
1033 || !equalBN("A +u B (r is a)", sum, ret)
1035 || !BN_uadd(ret, a, ret)
1036 || !equalBN("A +u B (r is b)", sum, ret)
1037 || !BN_copy(ret, sum)
1038 || !BN_usub(ret, ret, a)
1039 || !equalBN("Sum -u A (r is a)", b, ret)
1041 || !BN_usub(ret, sum, ret)
1042 || !equalBN("Sum -u A (r is b)", b, ret)
1043 || !BN_copy(ret, sum)
1044 || !BN_usub(ret, ret, b)
1045 || !equalBN("Sum -u B (r is a)", a, ret)
1047 || !BN_usub(ret, sum, ret)
1048 || !equalBN("Sum -u B (r is b)", a, ret))
1053 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1055 b_word = BN_get_word(b);
1056 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1057 if (!BN_copy(ret, a)
1058 || !BN_add_word(ret, b_word)
1059 || !equalBN("A + B (word)", sum, ret)
1060 || !BN_copy(ret, sum)
1061 || !BN_sub_word(ret, b_word)
1062 || !equalBN("Sum - B (word)", a, ret))
1075 static int file_lshift1(STANZA *s)
1077 BIGNUM *a = getBN(s, "A");
1078 BIGNUM *lshift1 = getBN(s, "LShift1");
1079 BIGNUM *zero = BN_new();
1080 BIGNUM *ret = BN_new();
1081 BIGNUM *two = BN_new();
1082 BIGNUM *remainder = BN_new();
1085 if (a == NULL || lshift1 == NULL || zero == NULL
1086 || ret == NULL || two == NULL || remainder == NULL)
1091 if (!BN_set_word(two, 2)
1092 || !BN_add(ret, a, a)
1093 || !equalBN("A + A", lshift1, ret)
1094 || !BN_mul(ret, a, two, ctx)
1095 || !equalBN("A * 2", lshift1, ret)
1096 || !BN_div(ret, remainder, lshift1, two, ctx)
1097 || !equalBN("LShift1 / 2", a, ret)
1098 || !equalBN("LShift1 % 2", zero, remainder)
1099 || !BN_lshift1(ret, a)
1100 || !equalBN("A << 1", lshift1, ret)
1101 || !BN_rshift1(ret, lshift1)
1102 || !equalBN("LShift >> 1", a, ret)
1103 || !BN_rshift1(ret, lshift1)
1104 || !equalBN("LShift >> 1", a, ret))
1107 /* Set the LSB to 1 and test rshift1 again. */
1108 if (!BN_set_bit(lshift1, 0)
1109 || !BN_div(ret, NULL /* rem */ , lshift1, two, ctx)
1110 || !equalBN("(LShift1 | 1) / 2", a, ret)
1111 || !BN_rshift1(ret, lshift1)
1112 || !equalBN("(LShift | 1) >> 1", a, ret))
1127 static int file_lshift(STANZA *s)
1129 BIGNUM *a = getBN(s, "A");
1130 BIGNUM *lshift = getBN(s, "LShift");
1131 BIGNUM *ret = BN_new();
1135 if (a == NULL || lshift == NULL || ret == NULL || !getint(s, &n, "N"))
1138 if (!BN_lshift(ret, a, n)
1139 || !equalBN("A << N", lshift, ret)
1140 || !BN_rshift(ret, lshift, n)
1141 || !equalBN("A >> N", a, ret))
1152 static int file_rshift(STANZA *s)
1154 BIGNUM *a = getBN(s, "A");
1155 BIGNUM *rshift = getBN(s, "RShift");
1156 BIGNUM *ret = BN_new();
1160 if (a == NULL || rshift == NULL || ret == NULL || !getint(s, &n, "N"))
1163 if (!BN_rshift(ret, a, n)
1164 || !equalBN("A >> N", rshift, ret))
1175 static int file_square(STANZA *s)
1177 BIGNUM *a = getBN(s, "A");
1178 BIGNUM *square = getBN(s, "Square");
1179 BIGNUM *zero = BN_new();
1180 BIGNUM *ret = BN_new();
1181 BIGNUM *remainder = BN_new();
1185 if (a == NULL || square == NULL || zero == NULL || ret == NULL
1186 || remainder == NULL)
1191 if (!BN_sqr(ret, a, ctx)
1192 || !equalBN("A^2", square, ret)
1193 || !BN_mul(ret, a, a, ctx)
1194 || !equalBN("A * A", square, ret)
1195 || !BN_div(ret, remainder, square, a, ctx)
1196 || !equalBN("Square / A", a, ret)
1197 || !equalBN("Square % A", zero, remainder))
1201 BN_set_negative(a, 0);
1202 if (!BN_sqrt(ret, square, ctx)
1203 || !equalBN("sqrt(Square)", a, ret))
1206 /* BN_sqrt should fail on non-squares and negative numbers. */
1207 if (!BN_is_zero(square)) {
1209 if (tmp == NULL || !BN_copy(tmp, square))
1211 BN_set_negative(tmp, 1);
1213 if (BN_sqrt(ret, tmp, ctx)) {
1214 fprintf(stderr, "BN_sqrt succeeded on a negative number");
1219 BN_set_negative(tmp, 0);
1220 if (BN_add(tmp, tmp, BN_value_one()))
1222 if (BN_sqrt(ret, tmp, ctx)) {
1223 fprintf(stderr, "BN_sqrt succeeded on a non-square");
1241 static int file_product(STANZA *s)
1243 BIGNUM *a = getBN(s, "A");
1244 BIGNUM *b = getBN(s, "B");
1245 BIGNUM *product = getBN(s, "Product");
1246 BIGNUM *ret = BN_new();
1247 BIGNUM *remainder = BN_new();
1248 BIGNUM *zero = BN_new();
1251 if (a == NULL || b == NULL || product == NULL || ret == NULL
1252 || remainder == NULL || zero == NULL)
1257 if (!BN_mul(ret, a, b, ctx)
1258 || !equalBN("A * B", product, ret)
1259 || !BN_div(ret, remainder, product, a, ctx)
1260 || !equalBN("Product / A", b, ret)
1261 || !equalBN("Product % A", zero, remainder)
1262 || !BN_div(ret, remainder, product, b, ctx)
1263 || !equalBN("Product / B", a, ret)
1264 || !equalBN("Product % B", zero, remainder))
1278 static int file_quotient(STANZA *s)
1280 BIGNUM *a = getBN(s, "A");
1281 BIGNUM *b = getBN(s, "B");
1282 BIGNUM *quotient = getBN(s, "Quotient");
1283 BIGNUM *remainder = getBN(s, "Remainder");
1284 BIGNUM *ret = BN_new();
1285 BIGNUM *ret2 = BN_new();
1286 BIGNUM *nnmod = BN_new();
1287 BN_ULONG b_word, ret_word;
1290 if (a == NULL || b == NULL || quotient == NULL || remainder == NULL
1291 || ret == NULL || ret2 == NULL || nnmod == NULL)
1294 if (!BN_div(ret, ret2, a, b, ctx)
1295 || !equalBN("A / B", quotient, ret)
1296 || !equalBN("A % B", remainder, ret2)
1297 || !BN_mul(ret, quotient, b, ctx)
1298 || !BN_add(ret, ret, remainder)
1299 || !equalBN("Quotient * B + Remainder", a, ret))
1303 * Test with BN_mod_word() and BN_div_word() if the divisor is
1306 b_word = BN_get_word(b);
1307 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1308 BN_ULONG remainder_word = BN_get_word(remainder);
1310 assert(remainder_word != (BN_ULONG)-1);
1311 if (!BN_copy(ret, a))
1313 ret_word = BN_div_word(ret, b_word);
1314 if (ret_word != remainder_word) {
1317 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "\n",
1318 ret_word, remainder_word);
1320 fprintf(stderr, "Got A %% B (word) mismatch\n");
1324 if (!equalBN ("A / B (word)", quotient, ret))
1327 ret_word = BN_mod_word(a, b_word);
1328 if (ret_word != remainder_word) {
1331 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "\n",
1332 ret_word, remainder_word);
1334 fprintf(stderr, "Got A %% B (word) mismatch\n");
1340 /* Test BN_nnmod. */
1341 if (!BN_is_negative(b)) {
1342 if (!BN_copy(nnmod, remainder)
1343 || (BN_is_negative(nnmod) && !BN_add(nnmod, nnmod, b))
1344 || !BN_nnmod(ret, a, b, ctx)
1345 || !equalBN("A % B (non-negative)", nnmod, ret))
1361 static int file_modmul(STANZA *s)
1363 BIGNUM *a = getBN(s, "A");
1364 BIGNUM *b = getBN(s, "B");
1365 BIGNUM *m = getBN(s, "M");
1366 BIGNUM *mod_mul = getBN(s, "ModMul");
1367 BIGNUM *ret = BN_new();
1370 if (a == NULL || b == NULL || m == NULL || mod_mul == NULL || ret == NULL)
1373 if (!BN_mod_mul(ret, a, b, m, ctx)
1374 || !equalBN("A * B (mod M)", mod_mul, ret))
1378 /* Reduce |a| and |b| and test the Montgomery version. */
1379 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1380 BIGNUM *a_tmp = BN_new();
1381 BIGNUM *b_tmp = BN_new();
1382 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1383 || !BN_MONT_CTX_set(mont, m, ctx)
1384 || !BN_nnmod(a_tmp, a, m, ctx)
1385 || !BN_nnmod(b_tmp, b, m, ctx)
1386 || !BN_to_montgomery(a_tmp, a_tmp, mont, ctx)
1387 || !BN_to_montgomery(b_tmp, b_tmp, mont, ctx)
1388 || !BN_mod_mul_montgomery(ret, a_tmp, b_tmp, mont, ctx)
1389 || !BN_from_montgomery(ret, ret, mont, ctx)
1390 || !equalBN("A * B (mod M) (mont)", mod_mul, ret)) {
1395 BN_MONT_CTX_free(mont);
1412 static int file_modexp(STANZA *s)
1414 BIGNUM *a = getBN(s, "A");
1415 BIGNUM *e = getBN(s, "E");
1416 BIGNUM *m = getBN(s, "M");
1417 BIGNUM *mod_exp = getBN(s, "ModExp");
1418 BIGNUM *ret = BN_new();
1419 BIGNUM *b = NULL, *c = NULL, *d = BN_new();
1422 if (a == NULL || e == NULL || m == NULL || mod_exp == NULL || ret == NULL)
1425 if (!BN_mod_exp(ret, a, e, m, ctx)
1426 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1430 if (!BN_mod_exp_mont(ret, a, e, m, ctx, NULL)
1431 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1432 || !BN_mod_exp_mont_consttime(ret, a, e, m, ctx, NULL)
1433 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1437 /* Regression test for carry propagation bug in sqr8x_reduction */
1438 BN_hex2bn(&a, "050505050505");
1439 BN_hex2bn(&b, "02");
1441 "4141414141414141414141274141414141414141414141414141414141414141"
1442 "4141414141414141414141414141414141414141414141414141414141414141"
1443 "4141414141414141414141800000000000000000000000000000000000000000"
1444 "0000000000000000000000000000000000000000000000000000000000000000"
1445 "0000000000000000000000000000000000000000000000000000000000000000"
1446 "0000000000000000000000000000000000000000000000000000000001");
1447 BN_mod_exp(d, a, b, c, ctx);
1448 BN_mul(e, a, a, ctx);
1450 fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");
1467 static int file_exp(STANZA *s)
1469 BIGNUM *a = getBN(s, "A");
1470 BIGNUM *e = getBN(s, "E");
1471 BIGNUM *exp = getBN(s, "Exp");
1472 BIGNUM *ret = BN_new();
1475 if (a == NULL || e == NULL || exp == NULL || ret == NULL)
1478 if (!BN_exp(ret, a, e, ctx)
1479 || !equalBN("A ^ E", exp, ret))
1491 static int file_modsqrt(STANZA *s)
1493 BIGNUM *a = getBN(s, "A");
1494 BIGNUM *p = getBN(s, "P");
1495 BIGNUM *mod_sqrt = getBN(s, "ModSqrt");
1496 BIGNUM *ret = BN_new();
1497 BIGNUM *ret2 = BN_new();
1500 if (a == NULL || p == NULL || mod_sqrt == NULL
1501 || ret == NULL || ret2 == NULL)
1504 /* There are two possible answers. */
1505 if (!BN_mod_sqrt(ret, a, p, ctx) || !BN_sub(ret2, p, ret))
1508 if (BN_cmp(ret2, mod_sqrt) != 0
1509 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1522 static int test_bn2padded()
1525 uint8_t zeros[256], out[256], reference[128];
1526 BIGNUM *n = BN_new();
1529 /* Test edge case at 0. */
1532 if (!BN_bn2bin_padded(NULL, 0, n)) {
1534 "BN_bn2bin_padded failed to encode 0 in an empty buffer.\n");
1537 memset(out, -1, sizeof(out));
1538 if (!BN_bn2bin_padded(out, sizeof(out), n)) {
1540 "BN_bn2bin_padded failed to encode 0 in a non-empty buffer.\n");
1543 memset(zeros, 0, sizeof(zeros));
1544 if (memcmp(zeros, out, sizeof(out))) {
1545 fprintf(stderr, "BN_bn2bin_padded did not zero buffer.\n");
1549 /* Test a random numbers at various byte lengths. */
1550 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1551 #define TOP_BIT_ON 0
1552 #define BOTTOM_BIT_NOTOUCH 0
1553 if (!BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)) {
1554 ERR_print_errors_fp(stderr);
1557 if (BN_num_bytes(n) != bytes
1558 || BN_bn2bin(n, reference) != bytes) {
1559 fprintf(stderr, "Bad result from BN_rand; bytes.\n");
1562 /* Empty buffer should fail. */
1563 if (BN_bn2bin_padded(NULL, 0, n)) {
1565 "BN_bn2bin_padded incorrectly succeeded on empty buffer.\n");
1568 /* One byte short should fail. */
1569 if (BN_bn2bin_padded(out, bytes - 1, n)) {
1571 "BN_bn2bin_padded incorrectly succeeded on short.\n");
1574 /* Exactly right size should encode. */
1575 if (!BN_bn2bin_padded(out, bytes, n)
1576 || memcmp(out, reference, bytes) != 0) {
1578 "BN_bn2bin_padded gave a bad result.\n");
1581 /* Pad up one byte extra. */
1582 if (!BN_bn2bin_padded(out, bytes + 1, n)
1583 || memcmp(out + 1, reference, bytes)
1584 || memcmp(out, zeros, 1)) {
1586 "BN_bn2bin_padded gave a bad result.\n");
1589 /* Pad up to 256. */
1590 if (!BN_bn2bin_padded(out, sizeof(out), n)
1591 || memcmp(out + sizeof(out) - bytes, reference, bytes)
1592 || memcmp(out, zeros, sizeof(out) - bytes)) {
1594 "BN_bn2bin_padded gave a bad result.\n");
1608 static int test_dec2bn()
1613 int ret = parsedecBN(&bn, "0");
1614 if (ret != 1 || !BN_is_zero(bn) || BN_is_negative(bn)) {
1615 fprintf(stderr, "BN_dec2bn(0) gave a bad result.\n");
1620 ret = parsedecBN(&bn, "256");
1621 if (ret != 3 || !BN_is_word(bn, 256) || BN_is_negative(bn)) {
1622 fprintf(stderr, "BN_dec2bn(256) gave a bad result.\n");
1627 ret = parsedecBN(&bn, "-42");
1628 if (ret != 3 || !BN_abs_is_word(bn, 42) || !BN_is_negative(bn)) {
1629 fprintf(stderr, "BN_dec2bn(42) gave a bad result.\n");
1634 ret = parsedecBN(&bn, "-0");
1635 if (ret != 2 || !BN_is_zero(bn) || BN_is_negative(bn)) {
1636 fprintf(stderr, "BN_dec2bn(-0) gave a bad result.\n");
1641 ret = parsedecBN(&bn, "42trailing garbage is ignored");
1642 if (ret != 2 || !BN_abs_is_word(bn, 42)
1643 || BN_is_negative(bn)) {
1644 fprintf(stderr, "BN_dec2bn(42trailing...) gave a bad result.\n");
1654 static int test_hex2bn()
1659 ret = parseBN(&bn, "0");
1660 if (ret != 1 || !BN_is_zero(bn) || BN_is_negative(bn)) {
1661 fprintf(stderr, "BN_hex2bn(0) gave a bad result.\n");
1666 ret = parseBN(&bn, "256");
1667 if (ret != 3 || !BN_is_word(bn, 0x256) || BN_is_negative(bn)) {
1668 fprintf(stderr, "BN_hex2bn(256) gave a bad result.\n");
1673 ret = parseBN(&bn, "-42");
1674 if (ret != 3 || !BN_abs_is_word(bn, 0x42) || !BN_is_negative(bn)) {
1675 fprintf(stderr, "BN_hex2bn(-42) gave a bad result.\n");
1680 ret = parseBN(&bn, "-0");
1681 if (ret != 2 || !BN_is_zero(bn) || BN_is_negative(bn)) {
1682 fprintf(stderr, "BN_hex2bn(-0) gave a bad result.\n");
1687 ret = parseBN(&bn, "abctrailing garbage is ignored");
1688 if (ret != 3 || !BN_is_word(bn, 0xabc) || BN_is_negative(bn)) {
1689 fprintf(stderr, "BN_hex2bn(abctrail...) gave a bad result.\n");
1699 static int test_asc2bn()
1701 BIGNUM *bn = BN_new();
1704 if (!BN_asc2bn(&bn, "0") || !BN_is_zero(bn) || BN_is_negative(bn)) {
1705 fprintf(stderr, "BN_asc2bn(0) gave a bad result.\n");
1709 if (!BN_asc2bn(&bn, "256") || !BN_is_word(bn, 256) || BN_is_negative(bn)) {
1710 fprintf(stderr, "BN_asc2bn(256) gave a bad result.\n");
1714 if (!BN_asc2bn(&bn, "-42")
1715 || !BN_abs_is_word(bn, 42) || !BN_is_negative(bn)) {
1716 fprintf(stderr, "BN_asc2bn(-42) gave a bad result.\n");
1720 if (!BN_asc2bn(&bn, "0x1234")
1721 || !BN_is_word(bn, 0x1234) || BN_is_negative(bn)) {
1722 fprintf(stderr, "BN_asc2bn(0x1234) gave a bad result.\n");
1726 if (!BN_asc2bn(&bn, "0X1234")
1727 || !BN_is_word(bn, 0x1234) || BN_is_negative(bn)) {
1728 fprintf(stderr, "BN_asc2bn(0X1234) gave a bad result.\n");
1732 if (!BN_asc2bn(&bn, "-0xabcd")
1733 || !BN_abs_is_word(bn, 0xabcd) || !BN_is_negative(bn)) {
1734 fprintf(stderr, "BN_asc2bn(-0xabcd) gave a bad result.\n");
1738 if (!BN_asc2bn(&bn, "-0") || !BN_is_zero(bn) || BN_is_negative(bn)) {
1739 fprintf(stderr, "BN_asc2bn(-0) gave a bad result.\n");
1743 if (!BN_asc2bn(&bn, "123trailing garbage is ignored")
1744 || !BN_is_word(bn, 123) || BN_is_negative(bn)) {
1745 fprintf(stderr, "BN_asc2bn(123trail...) gave a bad result.\n");
1755 static const MPITEST kMPITests[] = {
1756 {"0", "\x00\x00\x00\x00", 4},
1757 {"1", "\x00\x00\x00\x01\x01", 5},
1758 {"-1", "\x00\x00\x00\x01\x81", 5},
1759 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1760 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1761 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1764 static int test_mpi()
1767 int i = (int)sizeof(kMPITests) / sizeof(kMPITests[0]);
1768 const MPITEST *test = kMPITests;
1769 size_t mpi_len, mpi_len2;
1770 BIGNUM *bn = BN_new();
1774 for ( ; --i >= 0; test++) {
1775 if (!BN_asc2bn(&bn, test->base10)) {
1776 fprintf(stderr, "Can't convert %s\n", test->base10);
1779 mpi_len = BN_bn2mpi(bn, NULL);
1780 if (mpi_len > sizeof (scratch)) {
1782 "MPI test #%u: MPI size is too large to test.\n",
1787 mpi_len2 = BN_bn2mpi(bn, scratch);
1788 if (mpi_len != mpi_len2) {
1789 fprintf(stderr, "MPI test #%u: length changes.\n",
1794 if (mpi_len != test->mpi_len
1795 || memcmp(test->mpi, scratch, mpi_len) != 0) {
1796 fprintf(stderr, "MPI test #%u failed:\n", (unsigned)i);
1800 bn2 = BN_mpi2bn(scratch, mpi_len, NULL);
1802 fprintf(stderr, "MPI test #%u: failed to parse\n",
1807 if (BN_cmp(bn, bn2) != 0) {
1808 fprintf(stderr, "MPI test #%u: wrong result\n",
1822 static int test_rand()
1824 BIGNUM *bn = BN_new();
1831 * Test BN_rand for degenerate cases with |top| and |bottom| parameters.
1833 if (BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ )) {
1834 fprintf(stderr, "BN_rand1 gave a bad result.\n");
1837 if (BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ )) {
1838 fprintf(stderr, "BN_rand2 gave a bad result.\n");
1842 if (!BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ) || !BN_is_word(bn, 1)) {
1843 fprintf(stderr, "BN_rand3 gave a bad result.\n");
1846 if (BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ )) {
1847 fprintf(stderr, "BN_rand4 gave a bad result.\n");
1850 if (!BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ) || !BN_is_word(bn, 1)) {
1851 fprintf(stderr, "BN_rand5 gave a bad result.\n");
1855 if (!BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ) || !BN_is_word(bn, 3)) {
1856 fprintf(stderr, "BN_rand6 gave a bad result.\n");
1866 static int test_negzero()
1868 BIGNUM *a = BN_new();
1869 BIGNUM *b = BN_new();
1870 BIGNUM *c = BN_new();
1871 BIGNUM *d = BN_new();
1872 BIGNUM *numerator = NULL, *denominator = NULL;
1873 int consttime, st = 0;
1875 if (a == NULL || b == NULL || c == NULL || d == NULL)
1878 /* Test that BN_mul never gives negative zero. */
1879 if (!BN_set_word(a, 1))
1881 BN_set_negative(a, 1);
1883 if (!BN_mul(c, a, b, ctx))
1885 if (!BN_is_zero(c) || BN_is_negative(c)) {
1886 fprintf(stderr, "Multiplication test failed!\n");
1890 for (consttime = 0; consttime < 2; consttime++) {
1891 numerator = BN_new();
1892 denominator = BN_new();
1893 if (numerator == NULL || denominator == NULL)
1896 BN_set_flags(numerator, BN_FLG_CONSTTIME);
1897 BN_set_flags(denominator, BN_FLG_CONSTTIME);
1899 /* Test that BN_div never gives negative zero in the quotient. */
1900 if (!BN_set_word(numerator, 1) || !BN_set_word(denominator, 2))
1902 BN_set_negative(numerator, 1);
1903 if (!BN_div(a, b, numerator, denominator, ctx))
1905 if (!BN_is_zero(a) || BN_is_negative(a)) {
1906 fprintf(stderr, "Incorrect quotient (consttime = %d).\n",
1911 /* Test that BN_div never gives negative zero in the remainder. */
1912 if (!BN_set_word(denominator, 1))
1914 if (!BN_div(a, b, numerator, denominator, ctx))
1916 if (!BN_is_zero(b) || BN_is_negative(b)) {
1917 fprintf(stderr, "Incorrect remainder (consttime = %d).\n",
1922 BN_free(denominator);
1923 numerator = denominator = NULL;
1926 /* Test that BN_set_negative will not produce a negative zero. */
1928 BN_set_negative(a, 1);
1929 if (BN_is_negative(a)) {
1930 fprintf(stderr, "BN_set_negative produced a negative zero.\n");
1941 BN_free(denominator);
1945 static int test_badmod()
1947 BIGNUM *a = BN_new();
1948 BIGNUM *b = BN_new();
1949 BIGNUM *zero = BN_new();
1950 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1953 if (a == NULL || b == NULL || zero == NULL || mont == NULL)
1957 if (BN_div(a, b, BN_value_one(), zero, ctx)) {
1958 fprintf(stderr, "Division by zero succeeded!\n");
1963 if (BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)) {
1964 fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
1969 if (BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)) {
1970 fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
1975 if (BN_mod_exp_mont(a, BN_value_one(), BN_value_one(), zero, ctx, NULL)) {
1976 fprintf(stderr, "BN_mod_exp_mont with zero modulus succeeded!\n");
1981 if (BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
1984 "BN_mod_exp_mont_consttime with zero modulus succeeded!\n");
1989 if (BN_MONT_CTX_set(mont, zero, ctx)) {
1990 fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
1995 /* Some operations also may not be used with an even modulus. */
1996 if (!BN_set_word(b, 16))
1999 if (BN_MONT_CTX_set(mont, b, ctx)) {
2001 "BN_MONT_CTX_set succeeded for even modulus!\n");
2006 if (BN_mod_exp_mont(a, BN_value_one(), BN_value_one(), b, ctx, NULL)) {
2008 "BN_mod_exp_mont with even modulus succeeded!\n");
2013 if (BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2016 "BN_mod_exp_mont_consttime with even modulus succeeded!\n");
2026 BN_MONT_CTX_free(mont);
2030 static int test_expmodzero()
2032 BIGNUM *zero = BN_new();
2033 BIGNUM *a = BN_new();
2034 BIGNUM *r = BN_new();
2037 if (zero == NULL || a == NULL || r == NULL || !BN_rand(a, 1024, 0, 0))
2041 if (!BN_mod_exp(r, a, zero, BN_value_one(), NULL)
2043 || !BN_mod_exp_mont(r, a, zero, BN_value_one(), NULL, NULL)
2045 || !BN_mod_exp_mont_consttime(r, a, zero, BN_value_one(), NULL, NULL)
2047 || !BN_mod_exp_mont_word(r, 42, zero, BN_value_one(), NULL, NULL)
2059 static int test_smallprime()
2061 static const int kBits = 10;
2062 BIGNUM *r = BN_new();
2066 || !BN_generate_prime_ex(r, (int)kBits, 0, NULL, NULL, NULL))
2068 if (BN_num_bits(r) != kBits) {
2069 fprintf(stderr, "Expected %u bit prime, got %u bit number\n",
2070 kBits, BN_num_bits(r));
2081 /* Delete leading and trailing spaces from a string */
2082 static char *strip_spaces(char *p)
2086 /* Skip over leading spaces */
2087 while (*p && isspace(*p))
2092 for (q = p + strlen(p) - 1; q != p && isspace(*q); )
2094 return *p ? p : NULL;
2098 * Read next test stanza; return 1 if found, 0 on EOF or error.
2100 static int readstanza(STANZA *s, int *linesread)
2102 PAIR *pp = s->pairs;
2103 char *p, *equals, *key, *value;
2106 while (fgets(buff, sizeof(buff), fp) != NULL) {
2108 if ((p = strchr(buff, '\n')) == NULL) {
2109 fprintf(stderr, "Line %d too long.\n", s->start);
2114 /* Blank line marks end of tests. */
2115 if (buff[0] == '\0')
2118 /* Lines starting with a pound sign are ignored. */
2122 if ((equals = strchr(buff, '=')) == NULL) {
2123 fprintf(stderr, "Line %d missing equals.\n", s->start);
2128 key = strip_spaces(buff);
2129 value = strip_spaces(equals);
2130 if (key == NULL || value == NULL) {
2131 fprintf(stderr, "Line %d missing field.\n", s->start);
2135 if (s->numpairs >= MAXPAIRS) {
2136 fprintf(stderr, "Line %d too many lines\n", s->start);
2139 pp->key = OPENSSL_strdup(key);
2140 pp->value = OPENSSL_strdup(value);
2144 /* If we read anything, return ok. */
2148 static void clearstanza(STANZA *s)
2150 PAIR *pp = s->pairs;
2151 int i = s->numpairs;
2152 int start = s->start;
2154 for ( ; --i >= 0; pp++) {
2155 OPENSSL_free(pp->key);
2156 OPENSSL_free(pp->value);
2158 memset(s, 0, sizeof(*s));
2162 static int file_test_run(STANZA *s)
2164 static const FILETEST filetests[] = {
2166 {"LShift1", file_lshift1},
2167 {"LShift", file_lshift},
2168 {"RShift", file_rshift},
2169 {"Square", file_square},
2170 {"Product", file_product},
2171 {"Quotient", file_quotient},
2172 {"ModMul", file_modmul},
2173 {"ModExp", file_modexp},
2175 {"ModSqrt", file_modsqrt},
2177 int numtests = OSSL_NELEM(filetests);
2178 const FILETEST *tp = filetests;
2180 for ( ; --numtests >= 0; tp++) {
2181 if (findattr(s, tp->name) != NULL)
2184 fprintf(stderr, "Unknown test at %d\n", s->start);
2188 static int file_tests()
2191 int linesread = 0, result = 0;
2193 /* Read test file. */
2194 memset(&s, 0, sizeof(s));
2195 while (!feof(fp) && readstanza(&s, &linesread)) {
2196 if (s.numpairs == 0)
2198 if (!file_test_run(&s)) {
2200 fprintf(stderr, "Test at %d failed\n", s.start);
2204 s.start = linesread;
2212 int test_main(int argc, char *argv[])
2214 static const char rnd_seed[] =
2215 "If not seeded, BN_generate_prime might fail";
2219 fprintf(stderr, "%s TEST_FILE\n", argv[0]);
2224 ADD_TEST(test_div_recip);
2226 ADD_TEST(test_modexp_mont5);
2227 ADD_TEST(test_kronecker);
2228 ADD_TEST(test_rand);
2229 ADD_TEST(test_bn2padded);
2230 ADD_TEST(test_dec2bn);
2231 ADD_TEST(test_hex2bn);
2232 ADD_TEST(test_asc2bn);
2234 ADD_TEST(test_negzero);
2235 ADD_TEST(test_badmod);
2236 ADD_TEST(test_expmodzero);
2237 ADD_TEST(test_smallprime);
2238 #ifndef OPENSSL_NO_EC2M
2239 ADD_TEST(test_gf2m_add);
2240 ADD_TEST(test_gf2m_mod);
2241 ADD_TEST(test_gf2m_mul);
2242 ADD_TEST(test_gf2m_sqr);
2243 ADD_TEST(test_gf2m_modinv);
2244 ADD_TEST(test_gf2m_moddiv);
2245 ADD_TEST(test_gf2m_modexp);
2246 ADD_TEST(test_gf2m_modsqrt);
2247 ADD_TEST(test_gf2m_modsolvequad);
2249 ADD_TEST(file_tests);
2251 RAND_seed(rnd_seed, sizeof rnd_seed);
2253 TEST_check(ctx != NULL);
2255 fp = fopen(argv[1], "r");
2256 TEST_check(fp != NULL);
2257 result = run_tests(argv[0]);