]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/bntest.c
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
15 #include <openssl/bn.h>
16 #include <openssl/crypto.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
19 #include "internal/nelem.h"
20 #include "internal/numbers.h"
23 #ifdef OPENSSL_SYS_WINDOWS
24 # define strcasecmp _stricmp
28 * Things in boring, not in openssl. TODO we should add them.
30 #define HAVE_BN_PADDED 0
31 #define HAVE_BN_SQRT 0
33 typedef struct filetest_st
{
35 int (*func
)(STANZA
*s
);
38 typedef struct mpitest_st
{
44 static const int NUM0
= 100; /* number of tests */
45 static const int NUM1
= 50; /* additional tests for some functions */
49 * Polynomial coefficients used in GFM tests.
51 #ifndef OPENSSL_NO_EC2M
52 static int p0
[] = { 163, 7, 6, 3, 0, -1 };
53 static int p1
[] = { 193, 15, 0, -1 };
57 * Look for |key| in the stanza and return it or NULL if not found.
59 static const char *findattr(STANZA
*s
, const char *key
)
64 for ( ; --i
>= 0; pp
++)
65 if (strcasecmp(pp
->key
, key
) == 0)
71 * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
73 static int parse_bigBN(BIGNUM
**out
, const char *bn_strings
[])
75 char *bigstring
= glue_strings(bn_strings
, NULL
);
76 int ret
= BN_hex2bn(out
, bigstring
);
78 OPENSSL_free(bigstring
);
83 * Parse BIGNUM, return number of bytes parsed.
85 static int parseBN(BIGNUM
**out
, const char *in
)
88 return BN_hex2bn(out
, in
);
91 static int parsedecBN(BIGNUM
**out
, const char *in
)
94 return BN_dec2bn(out
, in
);
97 static BIGNUM
*getBN(STANZA
*s
, const char *attribute
)
102 if ((hex
= findattr(s
, attribute
)) == NULL
) {
103 TEST_error("%s:%d: Can't find %s", s
->test_file
, s
->start
, attribute
);
107 if (parseBN(&ret
, hex
) != (int)strlen(hex
)) {
108 TEST_error("Could not decode '%s'", hex
);
114 static int getint(STANZA
*s
, int *out
, const char *attribute
)
120 if (!TEST_ptr(ret
= getBN(s
, attribute
))
121 || !TEST_ulong_le(word
= BN_get_word(ret
), INT_MAX
))
131 static int equalBN(const char *op
, const BIGNUM
*expected
, const BIGNUM
*actual
)
133 if (BN_cmp(expected
, actual
) == 0)
136 TEST_error("unexpected %s value", op
);
137 TEST_BN_eq(expected
, actual
);
143 * Return a "random" flag for if a BN should be negated.
145 static int rand_neg(void)
147 static unsigned int neg
= 0;
148 static int sign
[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
150 return sign
[(neg
++) % 8];
154 static int test_swap(void)
156 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
, *d
= NULL
;
157 int top
, cond
, st
= 0;
159 if (!TEST_ptr(a
= BN_new())
160 || !TEST_ptr(b
= BN_new())
161 || !TEST_ptr(c
= BN_new())
162 || !TEST_ptr(d
= BN_new()))
165 BN_bntest_rand(a
, 1024, 1, 0);
166 BN_bntest_rand(b
, 1024, 1, 0);
169 top
= BN_num_bits(a
)/BN_BITS2
;
173 if (!equalBN("swap", a
, d
)
174 || !equalBN("swap", b
, c
))
177 /* conditional swap: true */
179 BN_consttime_swap(cond
, a
, b
, top
);
180 if (!equalBN("cswap true", a
, c
)
181 || !equalBN("cswap true", b
, d
))
184 /* conditional swap: false */
186 BN_consttime_swap(cond
, a
, b
, top
);
187 if (!equalBN("cswap false", a
, c
)
188 || !equalBN("cswap false", b
, d
))
191 /* same tests but checking flag swap */
192 BN_set_flags(a
, BN_FLG_CONSTTIME
);
195 if (!equalBN("swap, flags", a
, d
)
196 || !equalBN("swap, flags", b
, c
)
197 || !TEST_true(BN_get_flags(b
, BN_FLG_CONSTTIME
))
198 || !TEST_false(BN_get_flags(a
, BN_FLG_CONSTTIME
)))
202 BN_consttime_swap(cond
, a
, b
, top
);
203 if (!equalBN("cswap true, flags", a
, c
)
204 || !equalBN("cswap true, flags", b
, d
)
205 || !TEST_true(BN_get_flags(a
, BN_FLG_CONSTTIME
))
206 || !TEST_false(BN_get_flags(b
, BN_FLG_CONSTTIME
)))
210 BN_consttime_swap(cond
, a
, b
, top
);
211 if (!equalBN("cswap false, flags", a
, c
)
212 || !equalBN("cswap false, flags", b
, d
)
213 || !TEST_true(BN_get_flags(a
, BN_FLG_CONSTTIME
))
214 || !TEST_false(BN_get_flags(b
, BN_FLG_CONSTTIME
)))
226 static int test_sub(void)
228 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
;
231 if (!TEST_ptr(a
= BN_new())
232 || !TEST_ptr(b
= BN_new())
233 || !TEST_ptr(c
= BN_new()))
236 for (i
= 0; i
< NUM0
+ NUM1
; i
++) {
238 BN_bntest_rand(a
, 512, 0, 0);
240 if (!TEST_int_ne(BN_set_bit(a
, i
), 0))
244 BN_bntest_rand(b
, 400 + i
- NUM1
, 0, 0);
245 BN_set_negative(a
, rand_neg());
246 BN_set_negative(b
, rand_neg());
251 if (!TEST_BN_eq_zero(c
))
263 static int test_div_recip(void)
265 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
, *d
= NULL
, *e
= NULL
;
266 BN_RECP_CTX
*recp
= NULL
;
269 if (!TEST_ptr(a
= BN_new())
270 || !TEST_ptr(b
= BN_new())
271 || !TEST_ptr(c
= BN_new())
272 || !TEST_ptr(d
= BN_new())
273 || !TEST_ptr(e
= BN_new())
274 || !TEST_ptr(recp
= BN_RECP_CTX_new()))
277 for (i
= 0; i
< NUM0
+ NUM1
; i
++) {
279 BN_bntest_rand(a
, 400, 0, 0);
284 BN_bntest_rand(b
, 50 + 3 * (i
- NUM1
), 0, 0);
285 BN_set_negative(a
, rand_neg());
286 BN_set_negative(b
, rand_neg());
287 BN_RECP_CTX_set(recp
, b
, ctx
);
288 BN_div_recp(d
, c
, a
, recp
, ctx
);
289 BN_mul(e
, d
, b
, ctx
);
292 if (!TEST_BN_eq_zero(d
))
302 BN_RECP_CTX_free(recp
);
307 static int test_mod(void)
309 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
, *d
= NULL
, *e
= NULL
;
312 if (!TEST_ptr(a
= BN_new())
313 || !TEST_ptr(b
= BN_new())
314 || !TEST_ptr(c
= BN_new())
315 || !TEST_ptr(d
= BN_new())
316 || !TEST_ptr(e
= BN_new()))
319 BN_bntest_rand(a
, 1024, 0, 0);
320 for (i
= 0; i
< NUM0
; i
++) {
321 BN_bntest_rand(b
, 450 + i
* 10, 0, 0);
322 BN_set_negative(a
, rand_neg());
323 BN_set_negative(b
, rand_neg());
324 BN_mod(c
, a
, b
, ctx
);
325 BN_div(d
, e
, a
, b
, ctx
);
327 if (!TEST_BN_eq_zero(e
))
340 static const char *bn1strings
[] = {
341 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
342 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
343 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
344 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
345 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
346 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
347 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
348 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
349 "0000000000000000000000000000000000000000000000000000000000000000",
350 "0000000000000000000000000000000000000000000000000000000000000000",
351 "0000000000000000000000000000000000000000000000000000000000000000",
352 "0000000000000000000000000000000000000000000000000000000000000000",
353 "0000000000000000000000000000000000000000000000000000000000000000",
354 "0000000000000000000000000000000000000000000000000000000000000000",
355 "0000000000000000000000000000000000000000000000000000000000000000",
356 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
360 static const char *bn2strings
[] = {
361 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
362 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
363 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
364 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
365 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
366 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
367 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
368 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
369 "0000000000000000000000000000000000000000000000000000000000000000",
370 "0000000000000000000000000000000000000000000000000000000000000000",
371 "0000000000000000000000000000000000000000000000000000000000000000",
372 "0000000000000000000000000000000000000000000000000000000000000000",
373 "0000000000000000000000000000000000000000000000000000000000000000",
374 "0000000000000000000000000000000000000000000000000000000000000000",
375 "0000000000000000000000000000000000000000000000000000000000000000",
376 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
381 * Test constant-time modular exponentiation with 1024-bit inputs, which on
382 * x86_64 cause a different code branch to be taken.
384 static int test_modexp_mont5(void)
386 BIGNUM
*a
= NULL
, *p
= NULL
, *m
= NULL
, *d
= NULL
, *e
= NULL
;
387 BIGNUM
*b
= NULL
, *n
= NULL
, *c
= NULL
;
388 BN_MONT_CTX
*mont
= NULL
;
391 if (!TEST_ptr(a
= BN_new())
392 || !TEST_ptr(p
= BN_new())
393 || !TEST_ptr(m
= BN_new())
394 || !TEST_ptr(d
= BN_new())
395 || !TEST_ptr(e
= BN_new())
396 || !TEST_ptr(b
= BN_new())
397 || !TEST_ptr(n
= BN_new())
398 || !TEST_ptr(c
= BN_new())
399 || !TEST_ptr(mont
= BN_MONT_CTX_new()))
402 BN_bntest_rand(m
, 1024, 0, 1); /* must be odd for montgomery */
404 BN_bntest_rand(a
, 1024, 0, 0);
406 if (!TEST_true(BN_mod_exp_mont_consttime(d
, a
, p
, m
, ctx
, NULL
)))
408 if (!TEST_BN_eq_one(d
))
411 /* Regression test for carry bug in mulx4x_mont */
413 "7878787878787878787878787878787878787878787878787878787878787878"
414 "7878787878787878787878787878787878787878787878787878787878787878"
415 "7878787878787878787878787878787878787878787878787878787878787878"
416 "7878787878787878787878787878787878787878787878787878787878787878");
418 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
419 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
420 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
421 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
423 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
424 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
425 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
426 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
427 BN_MONT_CTX_set(mont
, n
, ctx
);
428 BN_mod_mul_montgomery(c
, a
, b
, mont
, ctx
);
429 BN_mod_mul_montgomery(d
, b
, a
, mont
, ctx
);
430 if (!TEST_BN_eq(c
, d
))
433 /* Regression test for carry bug in sqr[x]8x_mont */
434 parse_bigBN(&n
, bn1strings
);
435 parse_bigBN(&a
, bn2strings
);
438 BN_MONT_CTX_set(mont
, n
, ctx
);
439 BN_mod_mul_montgomery(c
, a
, a
, mont
, ctx
);
440 BN_mod_mul_montgomery(d
, a
, b
, mont
, ctx
);
441 if (!TEST_BN_eq(c
, d
))
444 /* Regression test for carry bug in bn_sqrx8x_internal */
446 static const char *ahex
[] = {
447 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
448 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
449 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
450 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
451 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
452 "9544D954000000006C0000000000000000000000000000000000000000000000",
453 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
454 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
455 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
456 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
457 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
458 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
461 static const char *nhex
[] = {
462 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
463 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
464 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
465 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
466 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
467 "00000010000000006C0000000000000000000000000000000000000000000000",
468 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
469 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
470 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
471 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
472 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
473 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
477 parse_bigBN(&a
, ahex
);
478 parse_bigBN(&n
, nhex
);
482 BN_MONT_CTX_set(mont
, n
, ctx
);
483 if (!TEST_true(BN_mod_mul_montgomery(c
, a
, a
, mont
, ctx
))
484 || !TEST_true(BN_mod_mul_montgomery(d
, a
, b
, mont
, ctx
))
485 || !TEST_BN_eq(c
, d
))
488 /* Regression test for bug in BN_from_montgomery_word */
490 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
491 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
492 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
494 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
495 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
496 BN_MONT_CTX_set(mont
, n
, ctx
);
497 if (!TEST_false(BN_mod_mul_montgomery(d
, a
, a
, mont
, ctx
)))
500 /* Regression test for bug in rsaz_1024_mul_avx2 */
502 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
503 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
504 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
505 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
507 "2020202020202020202020202020202020202020202020202020202020202020"
508 "2020202020202020202020202020202020202020202020202020202020202020"
509 "20202020202020FF202020202020202020202020202020202020202020202020"
510 "2020202020202020202020202020202020202020202020202020202020202020");
512 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
513 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
514 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
515 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF");
516 BN_MONT_CTX_set(mont
, n
, ctx
);
517 BN_mod_exp_mont_consttime(c
, a
, b
, n
, ctx
, mont
);
518 BN_mod_exp_mont(d
, a
, b
, n
, ctx
, mont
);
519 if (!TEST_BN_eq(c
, d
))
523 * rsaz_1024_mul_avx2 expects fully-reduced inputs.
524 * BN_mod_exp_mont_consttime should reduce the input first.
527 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
528 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
529 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
530 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
532 "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
533 "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
534 "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
535 "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E");
537 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
538 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
539 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
540 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
541 BN_MONT_CTX_set(mont
, n
, ctx
);
542 BN_mod_exp_mont_consttime(c
, a
, b
, n
, ctx
, mont
);
544 if (!TEST_BN_eq(c
, d
))
548 BN_bntest_rand(p
, 1024, 0, 0);
550 if (!TEST_true(BN_mod_exp_mont_consttime(d
, a
, p
, m
, ctx
, NULL
))
551 || !TEST_BN_eq_zero(d
))
555 * Craft an input whose Montgomery representation is 1, i.e., shorter
556 * than the modulus m, in order to test the const time precomputation
557 * scattering/gathering.
560 BN_MONT_CTX_set(mont
, m
, ctx
);
561 if (!TEST_true(BN_from_montgomery(e
, a
, mont
, ctx
))
562 || !TEST_true(BN_mod_exp_mont_consttime(d
, e
, p
, m
, ctx
, NULL
))
563 || !TEST_true(BN_mod_exp_simple(a
, e
, p
, m
, ctx
))
564 || !TEST_BN_eq(a
, d
))
567 /* Finally, some regular test vectors. */
568 BN_bntest_rand(e
, 1024, 0, 0);
569 if (!TEST_true(BN_mod_exp_mont_consttime(d
, e
, p
, m
, ctx
, NULL
))
570 || !TEST_true(BN_mod_exp_simple(a
, e
, p
, m
, ctx
))
571 || !TEST_BN_eq(a
, d
))
577 BN_MONT_CTX_free(mont
);
589 #ifndef OPENSSL_NO_EC2M
590 static int test_gf2m_add(void)
592 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
;
595 if (!TEST_ptr(a
= BN_new())
596 || !TEST_ptr(b
= BN_new())
597 || !TEST_ptr(c
= BN_new()))
600 for (i
= 0; i
< NUM0
; i
++) {
601 BN_rand(a
, 512, 0, 0);
602 BN_copy(b
, BN_value_one());
603 BN_set_negative(a
, rand_neg());
604 BN_set_negative(b
, rand_neg());
605 BN_GF2m_add(c
, a
, b
);
606 /* Test that two added values have the correct parity. */
607 if (!TEST_false((BN_is_odd(a
) && BN_is_odd(c
))
608 || (!BN_is_odd(a
) && !BN_is_odd(c
))))
610 BN_GF2m_add(c
, c
, c
);
611 /* Test that c + c = 0. */
612 if (!TEST_BN_eq_zero(c
))
623 static int test_gf2m_mod(void)
625 BIGNUM
*a
= NULL
, *b
[2] = {NULL
,NULL
}, *c
= NULL
, *d
= NULL
, *e
= NULL
;
628 if (!TEST_ptr(a
= BN_new())
629 || !TEST_ptr(b
[0] = BN_new())
630 || !TEST_ptr(b
[1] = BN_new())
631 || !TEST_ptr(c
= BN_new())
632 || !TEST_ptr(d
= BN_new())
633 || !TEST_ptr(e
= BN_new()))
636 BN_GF2m_arr2poly(p0
, b
[0]);
637 BN_GF2m_arr2poly(p1
, b
[1]);
639 for (i
= 0; i
< NUM0
; i
++) {
640 BN_bntest_rand(a
, 1024, 0, 0);
641 for (j
= 0; j
< 2; j
++) {
642 BN_GF2m_mod(c
, a
, b
[j
]);
643 BN_GF2m_add(d
, a
, c
);
644 BN_GF2m_mod(e
, d
, b
[j
]);
645 /* Test that a + (a mod p) mod p == 0. */
646 if (!TEST_BN_eq_zero(e
))
661 static int test_gf2m_mul(void)
663 BIGNUM
*a
, *b
[2] = {NULL
, NULL
}, *c
= NULL
, *d
= NULL
;
664 BIGNUM
*e
= NULL
, *f
= NULL
, *g
= NULL
, *h
= NULL
;
667 if (!TEST_ptr(a
= BN_new())
668 || !TEST_ptr(b
[0] = BN_new())
669 || !TEST_ptr(b
[1] = BN_new())
670 || !TEST_ptr(c
= BN_new())
671 || !TEST_ptr(d
= BN_new())
672 || !TEST_ptr(e
= BN_new())
673 || !TEST_ptr(f
= BN_new())
674 || !TEST_ptr(g
= BN_new())
675 || !TEST_ptr(h
= BN_new()))
678 BN_GF2m_arr2poly(p0
, b
[0]);
679 BN_GF2m_arr2poly(p1
, b
[1]);
681 for (i
= 0; i
< NUM0
; i
++) {
682 BN_bntest_rand(a
, 1024, 0, 0);
683 BN_bntest_rand(c
, 1024, 0, 0);
684 BN_bntest_rand(d
, 1024, 0, 0);
685 for (j
= 0; j
< 2; j
++) {
686 BN_GF2m_mod_mul(e
, a
, c
, b
[j
], ctx
);
687 BN_GF2m_add(f
, a
, d
);
688 BN_GF2m_mod_mul(g
, f
, c
, b
[j
], ctx
);
689 BN_GF2m_mod_mul(h
, d
, c
, b
[j
], ctx
);
690 BN_GF2m_add(f
, e
, g
);
691 BN_GF2m_add(f
, f
, h
);
692 /* Test that (a+d)*c = a*c + d*c. */
693 if (!TEST_BN_eq_zero(f
))
712 static int test_gf2m_sqr(void)
714 BIGNUM
*a
= NULL
, *b
[2] = {NULL
,NULL
}, *c
= NULL
, *d
= NULL
;
717 if (!TEST_ptr(a
= BN_new())
718 || !TEST_ptr(b
[0] = BN_new())
719 || !TEST_ptr(b
[1] = BN_new())
720 || !TEST_ptr(c
= BN_new())
721 || !TEST_ptr(d
= BN_new()))
724 BN_GF2m_arr2poly(p0
, b
[0]);
725 BN_GF2m_arr2poly(p1
, b
[1]);
727 for (i
= 0; i
< NUM0
; i
++) {
728 BN_bntest_rand(a
, 1024, 0, 0);
729 for (j
= 0; j
< 2; j
++) {
730 BN_GF2m_mod_sqr(c
, a
, b
[j
], ctx
);
732 BN_GF2m_mod_mul(d
, a
, d
, b
[j
], ctx
);
733 BN_GF2m_add(d
, c
, d
);
734 /* Test that a*a = a^2. */
735 if (!TEST_BN_eq_zero(d
))
749 static int test_gf2m_modinv(void)
751 BIGNUM
*a
= NULL
, *b
[2] = {NULL
,NULL
}, *c
= NULL
, *d
= NULL
;
754 if (!TEST_ptr(a
= BN_new())
755 || !TEST_ptr(b
[0] = BN_new())
756 || !TEST_ptr(b
[1] = BN_new())
757 || !TEST_ptr(c
= BN_new())
758 || !TEST_ptr(d
= BN_new()))
761 BN_GF2m_arr2poly(p0
, b
[0]);
762 BN_GF2m_arr2poly(p1
, b
[1]);
764 for (i
= 0; i
< NUM0
; i
++) {
765 BN_bntest_rand(a
, 512, 0, 0);
766 for (j
= 0; j
< 2; j
++) {
767 BN_GF2m_mod_inv(c
, a
, b
[j
], ctx
);
768 BN_GF2m_mod_mul(d
, a
, c
, b
[j
], ctx
);
769 /* Test that ((1/a)*a) = 1. */
770 if (!TEST_BN_eq_one(d
))
784 static int test_gf2m_moddiv(void)
786 BIGNUM
*a
= NULL
, *b
[2] = {NULL
,NULL
}, *c
= NULL
, *d
= NULL
;
787 BIGNUM
*e
= NULL
, *f
= NULL
;
790 if (!TEST_ptr(a
= BN_new())
791 || !TEST_ptr(b
[0] = BN_new())
792 || !TEST_ptr(b
[1] = BN_new())
793 || !TEST_ptr(c
= BN_new())
794 || !TEST_ptr(d
= BN_new())
795 || !TEST_ptr(e
= BN_new())
796 || !TEST_ptr(f
= BN_new()))
799 BN_GF2m_arr2poly(p0
, b
[0]);
800 BN_GF2m_arr2poly(p1
, b
[1]);
802 for (i
= 0; i
< NUM0
; i
++) {
803 BN_bntest_rand(a
, 512, 0, 0);
804 BN_bntest_rand(c
, 512, 0, 0);
805 for (j
= 0; j
< 2; j
++) {
806 BN_GF2m_mod_div(d
, a
, c
, b
[j
], ctx
);
807 BN_GF2m_mod_mul(e
, d
, c
, b
[j
], ctx
);
808 BN_GF2m_mod_div(f
, a
, e
, b
[j
], ctx
);
809 /* Test that ((a/c)*c)/a = 1. */
810 if (!TEST_BN_eq_one(f
))
826 static int test_gf2m_modexp(void)
828 BIGNUM
*a
= NULL
, *b
[2] = {NULL
,NULL
}, *c
= NULL
, *d
= NULL
;
829 BIGNUM
*e
= NULL
, *f
= NULL
;
832 if (!TEST_ptr(a
= BN_new())
833 || !TEST_ptr(b
[0] = BN_new())
834 || !TEST_ptr(b
[1] = BN_new())
835 || !TEST_ptr(c
= BN_new())
836 || !TEST_ptr(d
= BN_new())
837 || !TEST_ptr(e
= BN_new())
838 || !TEST_ptr(f
= BN_new()))
841 BN_GF2m_arr2poly(p0
, b
[0]);
842 BN_GF2m_arr2poly(p1
, b
[1]);
844 for (i
= 0; i
< NUM0
; i
++) {
845 BN_bntest_rand(a
, 512, 0, 0);
846 BN_bntest_rand(c
, 512, 0, 0);
847 BN_bntest_rand(d
, 512, 0, 0);
848 for (j
= 0; j
< 2; j
++) {
849 BN_GF2m_mod_exp(e
, a
, c
, b
[j
], ctx
);
850 BN_GF2m_mod_exp(f
, a
, d
, b
[j
], ctx
);
851 BN_GF2m_mod_mul(e
, e
, f
, b
[j
], ctx
);
853 BN_GF2m_mod_exp(f
, a
, f
, b
[j
], ctx
);
854 BN_GF2m_add(f
, e
, f
);
855 /* Test that a^(c+d)=a^c*a^d. */
856 if (!TEST_BN_eq_zero(f
))
872 static int test_gf2m_modsqrt(void)
874 BIGNUM
*a
= NULL
, *b
[2] = {NULL
,NULL
}, *c
= NULL
, *d
= NULL
;
875 BIGNUM
*e
= NULL
, *f
= NULL
;
878 if (!TEST_ptr(a
= BN_new())
879 || !TEST_ptr(b
[0] = BN_new())
880 || !TEST_ptr(b
[1] = BN_new())
881 || !TEST_ptr(c
= BN_new())
882 || !TEST_ptr(d
= BN_new())
883 || !TEST_ptr(e
= BN_new())
884 || !TEST_ptr(f
= BN_new()))
887 BN_GF2m_arr2poly(p0
, b
[0]);
888 BN_GF2m_arr2poly(p1
, b
[1]);
890 for (i
= 0; i
< NUM0
; i
++) {
891 BN_bntest_rand(a
, 512, 0, 0);
892 for (j
= 0; j
< 2; j
++) {
893 BN_GF2m_mod(c
, a
, b
[j
]);
894 BN_GF2m_mod_sqrt(d
, a
, b
[j
], ctx
);
895 BN_GF2m_mod_sqr(e
, d
, b
[j
], ctx
);
896 BN_GF2m_add(f
, c
, e
);
897 /* Test that d^2 = a, where d = sqrt(a). */
898 if (!TEST_BN_eq_zero(f
))
914 static int test_gf2m_modsolvequad(void)
916 BIGNUM
*a
= NULL
, *b
[2] = {NULL
,NULL
}, *c
= NULL
, *d
= NULL
;
918 int i
, j
, s
= 0, t
, st
= 0;
920 if (!TEST_ptr(a
= BN_new())
921 || !TEST_ptr(b
[0] = BN_new())
922 || !TEST_ptr(b
[1] = BN_new())
923 || !TEST_ptr(c
= BN_new())
924 || !TEST_ptr(d
= BN_new())
925 || !TEST_ptr(e
= BN_new()))
928 BN_GF2m_arr2poly(p0
, b
[0]);
929 BN_GF2m_arr2poly(p1
, b
[1]);
931 for (i
= 0; i
< NUM0
; i
++) {
932 BN_bntest_rand(a
, 512, 0, 0);
933 for (j
= 0; j
< 2; j
++) {
934 t
= BN_GF2m_mod_solve_quad(c
, a
, b
[j
], ctx
);
937 BN_GF2m_mod_sqr(d
, c
, b
[j
], ctx
);
938 BN_GF2m_add(d
, c
, d
);
939 BN_GF2m_mod(e
, a
, b
[j
]);
940 BN_GF2m_add(e
, e
, d
);
942 * Test that solution of quadratic c satisfies c^2 + c = a.
944 if (!TEST_BN_eq_zero(e
))
949 if (!TEST_int_ge(s
, 0)) {
950 TEST_info("%d tests found no roots; probably an error", NUM0
);
965 static int test_kronecker(void)
967 BIGNUM
*a
= NULL
, *b
= NULL
, *r
= NULL
, *t
= NULL
;
968 int i
, legendre
, kronecker
, st
= 0;
970 if (!TEST_ptr(a
= BN_new())
971 || !TEST_ptr(b
= BN_new())
972 || !TEST_ptr(r
= BN_new())
973 || !TEST_ptr(t
= BN_new()))
977 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
978 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
979 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
980 * generate a random prime b and compare these values for a number of
981 * random a's. (That is, we run the Solovay-Strassen primality test to
982 * confirm that b is prime, except that we don't want to test whether b
983 * is prime but whether BN_kronecker works.)
986 if (!TEST_true(BN_generate_prime_ex(b
, 512, 0, NULL
, NULL
, NULL
)))
988 BN_set_negative(b
, rand_neg());
990 for (i
= 0; i
< NUM0
; i
++) {
991 if (!TEST_true(BN_bntest_rand(a
, 512, 0, 0)))
993 BN_set_negative(a
, rand_neg());
995 /* t := (|b|-1)/2 (note that b is odd) */
996 if (!TEST_true(BN_copy(t
, b
)))
998 BN_set_negative(t
, 0);
999 if (!TEST_true(BN_sub_word(t
, 1)))
1001 if (!TEST_true(BN_rshift1(t
, t
)))
1003 /* r := a^t mod b */
1004 BN_set_negative(b
, 0);
1006 if (!TEST_true(BN_mod_exp_recp(r
, a
, t
, b
, ctx
)))
1008 BN_set_negative(b
, 1);
1010 if (BN_is_word(r
, 1))
1012 else if (BN_is_zero(r
))
1015 if (!TEST_true(BN_add_word(r
, 1)))
1017 if (!TEST_int_eq(BN_ucmp(r
, b
), 0)) {
1018 TEST_info("Legendre symbol computation failed");
1024 if (!TEST_int_ge(kronecker
= BN_kronecker(a
, b
, ctx
), -1))
1026 /* we actually need BN_kronecker(a, |b|) */
1027 if (BN_is_negative(a
) && BN_is_negative(b
))
1028 kronecker
= -kronecker
;
1030 if (!TEST_int_eq(legendre
, kronecker
))
1043 static int file_sum(STANZA
*s
)
1045 BIGNUM
*a
= NULL
, *b
= NULL
, *sum
= NULL
, *ret
= NULL
;
1049 if (!TEST_ptr(a
= getBN(s
, "A"))
1050 || !TEST_ptr(b
= getBN(s
, "B"))
1051 || !TEST_ptr(sum
= getBN(s
, "Sum"))
1052 || !TEST_ptr(ret
= BN_new()))
1055 if (!TEST_true(BN_add(ret
, a
, b
))
1056 || !equalBN("A + B", sum
, ret
)
1057 || !TEST_true(BN_sub(ret
, sum
, a
))
1058 || !equalBN("Sum - A", b
, ret
)
1059 || !TEST_true(BN_sub(ret
, sum
, b
))
1060 || !equalBN("Sum - B", a
, ret
))
1064 * Test that the functions work when |r| and |a| point to the same BIGNUM,
1065 * or when |r| and |b| point to the same BIGNUM.
1066 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1068 if (!TEST_true(BN_copy(ret
, a
))
1069 || !TEST_true(BN_add(ret
, ret
, b
))
1070 || !equalBN("A + B (r is a)", sum
, ret
)
1071 || !TEST_true(BN_copy(ret
, b
))
1072 || !TEST_true(BN_add(ret
, a
, ret
))
1073 || !equalBN("A + B (r is b)", sum
, ret
)
1074 || !TEST_true(BN_copy(ret
, sum
))
1075 || !TEST_true(BN_sub(ret
, ret
, a
))
1076 || !equalBN("Sum - A (r is a)", b
, ret
)
1077 || !TEST_true(BN_copy(ret
, a
))
1078 || !TEST_true(BN_sub(ret
, sum
, ret
))
1079 || !equalBN("Sum - A (r is b)", b
, ret
)
1080 || !TEST_true(BN_copy(ret
, sum
))
1081 || !TEST_true(BN_sub(ret
, ret
, b
))
1082 || !equalBN("Sum - B (r is a)", a
, ret
)
1083 || !TEST_true(BN_copy(ret
, b
))
1084 || !TEST_true(BN_sub(ret
, sum
, ret
))
1085 || !equalBN("Sum - B (r is b)", a
, ret
))
1089 * Test BN_uadd() and BN_usub() with the prerequisites they are
1090 * documented as having. Note that these functions are frequently used
1091 * when the prerequisites don't hold. In those cases, they are supposed
1092 * to work as if the prerequisite hold, but we don't test that yet.
1095 if (!BN_is_negative(a
) && !BN_is_negative(b
) && BN_cmp(a
, b
) >= 0) {
1096 if (!TEST_true(BN_uadd(ret
, a
, b
))
1097 || !equalBN("A +u B", sum
, ret
)
1098 || !TEST_true(BN_usub(ret
, sum
, a
))
1099 || !equalBN("Sum -u A", b
, ret
)
1100 || !TEST_true(BN_usub(ret
, sum
, b
))
1101 || !equalBN("Sum -u B", a
, ret
))
1104 * Test that the functions work when |r| and |a| point to the same
1105 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1106 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1108 if (!TEST_true(BN_copy(ret
, a
))
1109 || !TEST_true(BN_uadd(ret
, ret
, b
))
1110 || !equalBN("A +u B (r is a)", sum
, ret
)
1111 || !TEST_true(BN_copy(ret
, b
))
1112 || !TEST_true(BN_uadd(ret
, a
, ret
))
1113 || !equalBN("A +u B (r is b)", sum
, ret
)
1114 || !TEST_true(BN_copy(ret
, sum
))
1115 || !TEST_true(BN_usub(ret
, ret
, a
))
1116 || !equalBN("Sum -u A (r is a)", b
, ret
)
1117 || !TEST_true(BN_copy(ret
, a
))
1118 || !TEST_true(BN_usub(ret
, sum
, ret
))
1119 || !equalBN("Sum -u A (r is b)", b
, ret
)
1120 || !TEST_true(BN_copy(ret
, sum
))
1121 || !TEST_true(BN_usub(ret
, ret
, b
))
1122 || !equalBN("Sum -u B (r is a)", a
, ret
)
1123 || !TEST_true(BN_copy(ret
, b
))
1124 || !TEST_true(BN_usub(ret
, sum
, ret
))
1125 || !equalBN("Sum -u B (r is b)", a
, ret
))
1130 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1132 b_word
= BN_get_word(b
);
1133 if (!BN_is_negative(b
) && b_word
!= (BN_ULONG
)-1) {
1134 if (!TEST_true(BN_copy(ret
, a
))
1135 || !TEST_true(BN_add_word(ret
, b_word
))
1136 || !equalBN("A + B (word)", sum
, ret
)
1137 || !TEST_true(BN_copy(ret
, sum
))
1138 || !TEST_true(BN_sub_word(ret
, b_word
))
1139 || !equalBN("Sum - B (word)", a
, ret
))
1152 static int file_lshift1(STANZA
*s
)
1154 BIGNUM
*a
= NULL
, *lshift1
= NULL
, *zero
= NULL
, *ret
= NULL
;
1155 BIGNUM
*two
= NULL
, *remainder
= NULL
;
1158 if (!TEST_ptr(a
= getBN(s
, "A"))
1159 || !TEST_ptr(lshift1
= getBN(s
, "LShift1"))
1160 || !TEST_ptr(zero
= BN_new())
1161 || !TEST_ptr(ret
= BN_new())
1162 || !TEST_ptr(two
= BN_new())
1163 || !TEST_ptr(remainder
= BN_new()))
1168 if (!TEST_true(BN_set_word(two
, 2))
1169 || !TEST_true(BN_add(ret
, a
, a
))
1170 || !equalBN("A + A", lshift1
, ret
)
1171 || !TEST_true(BN_mul(ret
, a
, two
, ctx
))
1172 || !equalBN("A * 2", lshift1
, ret
)
1173 || !TEST_true(BN_div(ret
, remainder
, lshift1
, two
, ctx
))
1174 || !equalBN("LShift1 / 2", a
, ret
)
1175 || !equalBN("LShift1 % 2", zero
, remainder
)
1176 || !TEST_true(BN_lshift1(ret
, a
))
1177 || !equalBN("A << 1", lshift1
, ret
)
1178 || !TEST_true(BN_rshift1(ret
, lshift1
))
1179 || !equalBN("LShift >> 1", a
, ret
)
1180 || !TEST_true(BN_rshift1(ret
, lshift1
))
1181 || !equalBN("LShift >> 1", a
, ret
))
1184 /* Set the LSB to 1 and test rshift1 again. */
1185 if (!TEST_true(BN_set_bit(lshift1
, 0))
1186 || !TEST_true(BN_div(ret
, NULL
/* rem */ , lshift1
, two
, ctx
))
1187 || !equalBN("(LShift1 | 1) / 2", a
, ret
)
1188 || !TEST_true(BN_rshift1(ret
, lshift1
))
1189 || !equalBN("(LShift | 1) >> 1", a
, ret
))
1204 static int file_lshift(STANZA
*s
)
1206 BIGNUM
*a
= NULL
, *lshift
= NULL
, *ret
= NULL
;
1209 if (!TEST_ptr(a
= getBN(s
, "A"))
1210 || !TEST_ptr(lshift
= getBN(s
, "LShift"))
1211 || !TEST_ptr(ret
= BN_new())
1212 || !getint(s
, &n
, "N"))
1215 if (!TEST_true(BN_lshift(ret
, a
, n
))
1216 || !equalBN("A << N", lshift
, ret
)
1217 || !TEST_true(BN_rshift(ret
, lshift
, n
))
1218 || !equalBN("A >> N", a
, ret
))
1229 static int file_rshift(STANZA
*s
)
1231 BIGNUM
*a
= NULL
, *rshift
= NULL
, *ret
= NULL
;
1234 if (!TEST_ptr(a
= getBN(s
, "A"))
1235 || !TEST_ptr(rshift
= getBN(s
, "RShift"))
1236 || !TEST_ptr(ret
= BN_new())
1237 || !getint(s
, &n
, "N"))
1240 if (!TEST_true(BN_rshift(ret
, a
, n
))
1241 || !equalBN("A >> N", rshift
, ret
))
1244 /* If N == 1, try with rshift1 as well */
1246 if (!TEST_true(BN_rshift1(ret
, a
))
1247 || !equalBN("A >> 1 (rshift1)", rshift
, ret
))
1259 static int file_square(STANZA
*s
)
1261 BIGNUM
*a
= NULL
, *square
= NULL
, *zero
= NULL
, *ret
= NULL
;
1262 BIGNUM
*remainder
= NULL
, *tmp
= NULL
;
1265 if (!TEST_ptr(a
= getBN(s
, "A"))
1266 || !TEST_ptr(square
= getBN(s
, "Square"))
1267 || !TEST_ptr(zero
= BN_new())
1268 || !TEST_ptr(ret
= BN_new())
1269 || !TEST_ptr(remainder
= BN_new()))
1273 if (!TEST_true(BN_sqr(ret
, a
, ctx
))
1274 || !equalBN("A^2", square
, ret
)
1275 || !TEST_true(BN_mul(ret
, a
, a
, ctx
))
1276 || !equalBN("A * A", square
, ret
)
1277 || !TEST_true(BN_div(ret
, remainder
, square
, a
, ctx
))
1278 || !equalBN("Square / A", a
, ret
)
1279 || !equalBN("Square % A", zero
, remainder
))
1283 BN_set_negative(a
, 0);
1284 if (!TEST_true(BN_sqrt(ret
, square
, ctx
))
1285 || !equalBN("sqrt(Square)", a
, ret
))
1288 /* BN_sqrt should fail on non-squares and negative numbers. */
1289 if (!TEST_BN_eq_zero(square
)) {
1290 if (!TEST_ptr(tmp
= BN_new())
1291 || !TEST_true(BN_copy(tmp
, square
)))
1293 BN_set_negative(tmp
, 1);
1295 if (!TEST_int_eq(BN_sqrt(ret
, tmp
, ctx
), 0))
1299 BN_set_negative(tmp
, 0);
1300 if (BN_add(tmp
, tmp
, BN_value_one()))
1302 if (!TEST_int_eq(BN_sqrt(ret
, tmp
, ctx
)))
1319 static int file_product(STANZA
*s
)
1321 BIGNUM
*a
= NULL
, *b
= NULL
, *product
= NULL
, *ret
= NULL
;
1322 BIGNUM
*remainder
= NULL
, *zero
= NULL
;
1325 if (!TEST_ptr(a
= getBN(s
, "A"))
1326 || !TEST_ptr(b
= getBN(s
, "B"))
1327 || !TEST_ptr(product
= getBN(s
, "Product"))
1328 || !TEST_ptr(ret
= BN_new())
1329 || !TEST_ptr(remainder
= BN_new())
1330 || !TEST_ptr(zero
= BN_new()))
1335 if (!TEST_true(BN_mul(ret
, a
, b
, ctx
))
1336 || !equalBN("A * B", product
, ret
)
1337 || !TEST_true(BN_div(ret
, remainder
, product
, a
, ctx
))
1338 || !equalBN("Product / A", b
, ret
)
1339 || !equalBN("Product % A", zero
, remainder
)
1340 || !TEST_true(BN_div(ret
, remainder
, product
, b
, ctx
))
1341 || !equalBN("Product / B", a
, ret
)
1342 || !equalBN("Product % B", zero
, remainder
))
1356 static int file_quotient(STANZA
*s
)
1358 BIGNUM
*a
= NULL
, *b
= NULL
, *quotient
= NULL
, *remainder
= NULL
;
1359 BIGNUM
*ret
= NULL
, *ret2
= NULL
, *nnmod
= NULL
;
1360 BN_ULONG b_word
, ret_word
;
1363 if (!TEST_ptr(a
= getBN(s
, "A"))
1364 || !TEST_ptr(b
= getBN(s
, "B"))
1365 || !TEST_ptr(quotient
= getBN(s
, "Quotient"))
1366 || !TEST_ptr(remainder
= getBN(s
, "Remainder"))
1367 || !TEST_ptr(ret
= BN_new())
1368 || !TEST_ptr(ret2
= BN_new())
1369 || !TEST_ptr(nnmod
= BN_new()))
1372 if (!TEST_true(BN_div(ret
, ret2
, a
, b
, ctx
))
1373 || !equalBN("A / B", quotient
, ret
)
1374 || !equalBN("A % B", remainder
, ret2
)
1375 || !TEST_true(BN_mul(ret
, quotient
, b
, ctx
))
1376 || !TEST_true(BN_add(ret
, ret
, remainder
))
1377 || !equalBN("Quotient * B + Remainder", a
, ret
))
1381 * Test with BN_mod_word() and BN_div_word() if the divisor is
1384 b_word
= BN_get_word(b
);
1385 if (!BN_is_negative(b
) && b_word
!= (BN_ULONG
)-1) {
1386 BN_ULONG remainder_word
= BN_get_word(remainder
);
1388 assert(remainder_word
!= (BN_ULONG
)-1);
1389 if (!TEST_ptr(BN_copy(ret
, a
)))
1391 ret_word
= BN_div_word(ret
, b_word
);
1392 if (ret_word
!= remainder_word
) {
1395 "Got A %% B (word) = " BN_DEC_FMT1
", wanted " BN_DEC_FMT1
,
1396 ret_word
, remainder_word
);
1398 TEST_error("Got A %% B (word) mismatch");
1402 if (!equalBN ("A / B (word)", quotient
, ret
))
1405 ret_word
= BN_mod_word(a
, b_word
);
1406 if (ret_word
!= remainder_word
) {
1409 "Got A %% B (word) = " BN_DEC_FMT1
", wanted " BN_DEC_FMT1
"",
1410 ret_word
, remainder_word
);
1412 TEST_error("Got A %% B (word) mismatch");
1418 /* Test BN_nnmod. */
1419 if (!BN_is_negative(b
)) {
1420 if (!TEST_true(BN_copy(nnmod
, remainder
))
1421 || (BN_is_negative(nnmod
)
1422 && !TEST_true(BN_add(nnmod
, nnmod
, b
)))
1423 || !TEST_true(BN_nnmod(ret
, a
, b
, ctx
))
1424 || !equalBN("A % B (non-negative)", nnmod
, ret
))
1440 static int file_modmul(STANZA
*s
)
1442 BIGNUM
*a
= NULL
, *b
= NULL
, *m
= NULL
, *mod_mul
= NULL
, *ret
= NULL
;
1445 if (!TEST_ptr(a
= getBN(s
, "A"))
1446 || !TEST_ptr(b
= getBN(s
, "B"))
1447 || !TEST_ptr(m
= getBN(s
, "M"))
1448 || !TEST_ptr(mod_mul
= getBN(s
, "ModMul"))
1449 || !TEST_ptr(ret
= BN_new()))
1452 if (!TEST_true(BN_mod_mul(ret
, a
, b
, m
, ctx
))
1453 || !equalBN("A * B (mod M)", mod_mul
, ret
))
1457 /* Reduce |a| and |b| and test the Montgomery version. */
1458 BN_MONT_CTX
*mont
= BN_MONT_CTX_new();
1459 BIGNUM
*a_tmp
= BN_new();
1460 BIGNUM
*b_tmp
= BN_new();
1462 if (mont
== NULL
|| a_tmp
== NULL
|| b_tmp
== NULL
1463 || !TEST_true(BN_MONT_CTX_set(mont
, m
, ctx
))
1464 || !TEST_true(BN_nnmod(a_tmp
, a
, m
, ctx
))
1465 || !TEST_true(BN_nnmod(b_tmp
, b
, m
, ctx
))
1466 || !TEST_true(BN_to_montgomery(a_tmp
, a_tmp
, mont
, ctx
))
1467 || !TEST_true(BN_to_montgomery(b_tmp
, b_tmp
, mont
, ctx
))
1468 || !TEST_true(BN_mod_mul_montgomery(ret
, a_tmp
, b_tmp
,
1470 || !TEST_true(BN_from_montgomery(ret
, ret
, mont
, ctx
))
1471 || !equalBN("A * B (mod M) (mont)", mod_mul
, ret
))
1475 BN_MONT_CTX_free(mont
);
1492 static int file_modexp(STANZA
*s
)
1494 BIGNUM
*a
= NULL
, *e
= NULL
, *m
= NULL
, *mod_exp
= NULL
, *ret
= NULL
;
1495 BIGNUM
*b
= NULL
, *c
= NULL
, *d
= NULL
;
1498 if (!TEST_ptr(a
= getBN(s
, "A"))
1499 || !TEST_ptr(e
= getBN(s
, "E"))
1500 || !TEST_ptr(m
= getBN(s
, "M"))
1501 || !TEST_ptr(mod_exp
= getBN(s
, "ModExp"))
1502 || !TEST_ptr(ret
= BN_new())
1503 || !TEST_ptr(d
= BN_new()))
1506 if (!TEST_true(BN_mod_exp(ret
, a
, e
, m
, ctx
))
1507 || !equalBN("A ^ E (mod M)", mod_exp
, ret
))
1511 if (!TEST_true(BN_mod_exp_mont(ret
, a
, e
, m
, ctx
, NULL
))
1512 || !equalBN("A ^ E (mod M) (mont)", mod_exp
, ret
)
1513 || !TEST_true(BN_mod_exp_mont_consttime(ret
, a
, e
, m
,
1515 || !equalBN("A ^ E (mod M) (mont const", mod_exp
, ret
))
1519 /* Regression test for carry propagation bug in sqr8x_reduction */
1520 BN_hex2bn(&a
, "050505050505");
1521 BN_hex2bn(&b
, "02");
1523 "4141414141414141414141274141414141414141414141414141414141414141"
1524 "4141414141414141414141414141414141414141414141414141414141414141"
1525 "4141414141414141414141800000000000000000000000000000000000000000"
1526 "0000000000000000000000000000000000000000000000000000000000000000"
1527 "0000000000000000000000000000000000000000000000000000000000000000"
1528 "0000000000000000000000000000000000000000000000000000000001");
1529 if (!TEST_true(BN_mod_exp(d
, a
, b
, c
, ctx
))
1530 || !TEST_true(BN_mul(e
, a
, a
, ctx
))
1531 || !TEST_BN_eq(d
, e
))
1547 static int file_exp(STANZA
*s
)
1549 BIGNUM
*a
= NULL
, *e
= NULL
, *exp
= NULL
, *ret
= NULL
;
1552 if (!TEST_ptr(a
= getBN(s
, "A"))
1553 || !TEST_ptr(e
= getBN(s
, "E"))
1554 || !TEST_ptr(exp
= getBN(s
, "Exp"))
1555 || !TEST_ptr(ret
= BN_new()))
1558 if (!TEST_true(BN_exp(ret
, a
, e
, ctx
))
1559 || !equalBN("A ^ E", exp
, ret
))
1571 static int file_modsqrt(STANZA
*s
)
1573 BIGNUM
*a
= NULL
, *p
= NULL
, *mod_sqrt
= NULL
, *ret
= NULL
, *ret2
= NULL
;
1576 if (!TEST_ptr(a
= getBN(s
, "A"))
1577 || !TEST_ptr(p
= getBN(s
, "P"))
1578 || !TEST_ptr(mod_sqrt
= getBN(s
, "ModSqrt"))
1579 || !TEST_ptr(ret
= BN_new())
1580 || !TEST_ptr(ret2
= BN_new()))
1583 /* There are two possible answers. */
1584 if (!TEST_true(BN_mod_sqrt(ret
, a
, p
, ctx
))
1585 || !TEST_true(BN_sub(ret2
, p
, ret
)))
1588 /* The first condition should NOT be a test. */
1589 if (BN_cmp(ret2
, mod_sqrt
) != 0
1590 && !equalBN("sqrt(A) (mod P)", mod_sqrt
, ret
))
1603 static int test_bn2padded(void)
1606 uint8_t zeros
[256], out
[256], reference
[128];
1607 BIGNUM
*n
= BN_new();
1610 /* Test edge case at 0. */
1613 if (!TEST_true(BN_bn2bin_padded(NULL
, 0, n
)))
1615 memset(out
, -1, sizeof(out
));
1616 if (!TEST_true(BN_bn2bin_padded(out
, sizeof(out
)), n
))
1618 memset(zeros
, 0, sizeof(zeros
));
1619 if (!TEST_mem_eq(zeros
, sizeof(zeros
), out
, sizeof(out
)))
1622 /* Test a random numbers at various byte lengths. */
1623 for (size_t bytes
= 128 - 7; bytes
<= 128; bytes
++) {
1624 #define TOP_BIT_ON 0
1625 #define BOTTOM_BIT_NOTOUCH 0
1626 if (!TEST_true(BN_rand(n
, bytes
* 8, TOP_BIT_ON
, BOTTOM_BIT_NOTOUCH
)))
1628 if (!TEST_int_eq(BN_num_bytes(n
),A
) bytes
1629 || TEST_int_eq(BN_bn2bin(n
, reference
), bytes
))
1631 /* Empty buffer should fail. */
1632 if (!TEST_int_eq(BN_bn2bin_padded(NULL
, 0, n
)), 0)
1634 /* One byte short should fail. */
1635 if (BN_bn2bin_padded(out
, bytes
- 1, n
))
1637 /* Exactly right size should encode. */
1638 if (!TEST_true(BN_bn2bin_padded(out
, bytes
, n
))
1639 || TEST_mem_eq(out
, bytes
, reference
, bytes
))
1641 /* Pad up one byte extra. */
1642 if (!TEST_true(BN_bn2bin_padded(out
, bytes
+ 1, n
))
1643 || !TEST_mem_eq(out
+ 1, bytes
, reference
, bytes
)
1644 || !TEST_mem_eq(out
, 1, zeros
, 1))
1646 /* Pad up to 256. */
1647 if (!TEST_true(BN_bn2bin_padded(out
, sizeof(out
)), n
)
1648 || !TEST_mem_eq(out
+ sizeof(out
) - bytes
, bytes
,
1650 || !TEST_mem_eq(out
, sizseof(out
) - bytes
,
1651 zeros
, sizeof(out
) - bytes
))
1664 static int test_dec2bn(void)
1669 if (!TEST_int_eq(parsedecBN(&bn
, "0"), 1)
1670 || !TEST_BN_eq_word(bn
, 0)
1671 || !TEST_BN_eq_zero(bn
)
1672 || !TEST_BN_le_zero(bn
)
1673 || !TEST_BN_ge_zero(bn
)
1674 || !TEST_BN_even(bn
))
1679 if (!TEST_int_eq(parsedecBN(&bn
, "256"), 3)
1680 || !TEST_BN_eq_word(bn
, 256)
1681 || !TEST_BN_ge_zero(bn
)
1682 || !TEST_BN_gt_zero(bn
)
1683 || !TEST_BN_ne_zero(bn
)
1684 || !TEST_BN_even(bn
))
1689 if (!TEST_int_eq(parsedecBN(&bn
, "-42"), 3)
1690 || !TEST_BN_abs_eq_word(bn
, 42)
1691 || !TEST_BN_lt_zero(bn
)
1692 || !TEST_BN_le_zero(bn
)
1693 || !TEST_BN_ne_zero(bn
)
1694 || !TEST_BN_even(bn
))
1699 if (!TEST_int_eq(parsedecBN(&bn
, "1"), 1)
1700 || !TEST_BN_eq_word(bn
, 1)
1701 || !TEST_BN_ne_zero(bn
)
1702 || !TEST_BN_gt_zero(bn
)
1703 || !TEST_BN_ge_zero(bn
)
1704 || !TEST_BN_eq_one(bn
)
1705 || !TEST_BN_odd(bn
))
1710 if (!TEST_int_eq(parsedecBN(&bn
, "-0"), 2)
1711 || !TEST_BN_eq_zero(bn
)
1712 || !TEST_BN_ge_zero(bn
)
1713 || !TEST_BN_le_zero(bn
)
1714 || !TEST_BN_even(bn
))
1719 if (!TEST_int_eq(parsedecBN(&bn
, "42trailing garbage is ignored"), 2)
1720 || !TEST_BN_abs_eq_word(bn
, 42)
1721 || !TEST_BN_ge_zero(bn
)
1722 || !TEST_BN_gt_zero(bn
)
1723 || !TEST_BN_ne_zero(bn
)
1724 || !TEST_BN_even(bn
))
1733 static int test_hex2bn(void)
1738 if (!TEST_int_eq(parseBN(&bn
, "0"), 1)
1739 || !TEST_BN_eq_zero(bn
)
1740 || !TEST_BN_ge_zero(bn
)
1741 || !TEST_BN_even(bn
))
1746 if (!TEST_int_eq(parseBN(&bn
, "256"), 3)
1747 || !TEST_BN_eq_word(bn
, 0x256)
1748 || !TEST_BN_ge_zero(bn
)
1749 || !TEST_BN_gt_zero(bn
)
1750 || !TEST_BN_ne_zero(bn
)
1751 || !TEST_BN_even(bn
))
1756 if (!TEST_int_eq(parseBN(&bn
, "-42"), 3)
1757 || !TEST_BN_abs_eq_word(bn
, 0x42)
1758 || !TEST_BN_lt_zero(bn
)
1759 || !TEST_BN_le_zero(bn
)
1760 || !TEST_BN_ne_zero(bn
)
1761 || !TEST_BN_even(bn
))
1766 if (!TEST_int_eq(parseBN(&bn
, "cb"), 2)
1767 || !TEST_BN_eq_word(bn
, 0xCB)
1768 || !TEST_BN_ge_zero(bn
)
1769 || !TEST_BN_gt_zero(bn
)
1770 || !TEST_BN_ne_zero(bn
)
1771 || !TEST_BN_odd(bn
))
1776 if (!TEST_int_eq(parseBN(&bn
, "-0"), 2)
1777 || !TEST_BN_eq_zero(bn
)
1778 || !TEST_BN_ge_zero(bn
)
1779 || !TEST_BN_le_zero(bn
)
1780 || !TEST_BN_even(bn
))
1785 if (!TEST_int_eq(parseBN(&bn
, "abctrailing garbage is ignored"), 3)
1786 || !TEST_BN_eq_word(bn
, 0xabc)
1787 || !TEST_BN_ge_zero(bn
)
1788 || !TEST_BN_gt_zero(bn
)
1789 || !TEST_BN_ne_zero(bn
)
1790 || !TEST_BN_even(bn
))
1799 static int test_asc2bn(void)
1804 if (!TEST_ptr(bn
= BN_new()))
1807 if (!TEST_true(BN_asc2bn(&bn
, "0"))
1808 || !TEST_BN_eq_zero(bn
)
1809 || !TEST_BN_ge_zero(bn
))
1812 if (!TEST_true(BN_asc2bn(&bn
, "256"))
1813 || !TEST_BN_eq_word(bn
, 256)
1814 || !TEST_BN_ge_zero(bn
))
1817 if (!TEST_true(BN_asc2bn(&bn
, "-42"))
1818 || !TEST_BN_abs_eq_word(bn
, 42)
1819 || !TEST_BN_lt_zero(bn
))
1822 if (!TEST_true(BN_asc2bn(&bn
, "0x1234"))
1823 || !TEST_BN_eq_word(bn
, 0x1234)
1824 || !TEST_BN_ge_zero(bn
))
1827 if (!TEST_true(BN_asc2bn(&bn
, "0X1234"))
1828 || !TEST_BN_eq_word(bn
, 0x1234)
1829 || !TEST_BN_ge_zero(bn
))
1832 if (!TEST_true(BN_asc2bn(&bn
, "-0xabcd"))
1833 || !TEST_BN_abs_eq_word(bn
, 0xabcd)
1834 || !TEST_BN_lt_zero(bn
))
1837 if (!TEST_true(BN_asc2bn(&bn
, "-0"))
1838 || !TEST_BN_eq_zero(bn
)
1839 || !TEST_BN_ge_zero(bn
))
1842 if (!TEST_true(BN_asc2bn(&bn
, "123trailing garbage is ignored"))
1843 || !TEST_BN_eq_word(bn
, 123)
1844 || !TEST_BN_ge_zero(bn
))
1853 static const MPITEST kMPITests
[] = {
1854 {"0", "\x00\x00\x00\x00", 4},
1855 {"1", "\x00\x00\x00\x01\x01", 5},
1856 {"-1", "\x00\x00\x00\x01\x81", 5},
1857 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1858 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1859 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1862 static int test_mpi(int i
)
1865 const MPITEST
*test
= &kMPITests
[i
];
1866 size_t mpi_len
, mpi_len2
;
1871 if (!TEST_ptr(bn
= BN_new())
1872 || !TEST_true(BN_asc2bn(&bn
, test
->base10
)))
1874 mpi_len
= BN_bn2mpi(bn
, NULL
);
1875 if (!TEST_size_t_le(mpi_len
, sizeof(scratch
)))
1878 if (!TEST_size_t_eq(mpi_len2
= BN_bn2mpi(bn
, scratch
), mpi_len
)
1879 || !TEST_mem_eq(test
->mpi
, test
->mpi_len
, scratch
, mpi_len
))
1882 if (!TEST_ptr(bn2
= BN_mpi2bn(scratch
, mpi_len
, NULL
)))
1885 if (!TEST_BN_eq(bn
, bn2
)) {
1897 static int test_rand(void)
1902 if (!TEST_ptr(bn
= BN_new()))
1905 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1906 if (!TEST_false(BN_rand(bn
, 0, 0 /* top */ , 0 /* bottom */ ))
1907 || !TEST_false(BN_rand(bn
, 0, 1 /* top */ , 1 /* bottom */ ))
1908 || !TEST_true(BN_rand(bn
, 1, 0 /* top */ , 0 /* bottom */ ))
1909 || !TEST_BN_eq_one(bn
)
1910 || !TEST_false(BN_rand(bn
, 1, 1 /* top */ , 0 /* bottom */ ))
1911 || !TEST_true(BN_rand(bn
, 1, -1 /* top */ , 1 /* bottom */ ))
1912 || !TEST_BN_eq_one(bn
)
1913 || !TEST_true(BN_rand(bn
, 2, 1 /* top */ , 0 /* bottom */ ))
1914 || !TEST_BN_eq_word(bn
, 3))
1923 static int test_negzero(void)
1925 BIGNUM
*a
= NULL
, *b
= NULL
, *c
= NULL
, *d
= NULL
;
1926 BIGNUM
*numerator
= NULL
, *denominator
= NULL
;
1927 int consttime
, st
= 0;
1929 if (!TEST_ptr(a
= BN_new())
1930 || !TEST_ptr(b
= BN_new())
1931 || !TEST_ptr(c
= BN_new())
1932 || !TEST_ptr(d
= BN_new()))
1935 /* Test that BN_mul never gives negative zero. */
1936 if (!TEST_true(BN_set_word(a
, 1)))
1938 BN_set_negative(a
, 1);
1940 if (!TEST_true(BN_mul(c
, a
, b
, ctx
)))
1942 if (!TEST_BN_eq_zero(c
)
1943 || !TEST_BN_ge_zero(c
))
1946 for (consttime
= 0; consttime
< 2; consttime
++) {
1947 if (!TEST_ptr(numerator
= BN_new())
1948 || !TEST_ptr(denominator
= BN_new()))
1951 BN_set_flags(numerator
, BN_FLG_CONSTTIME
);
1952 BN_set_flags(denominator
, BN_FLG_CONSTTIME
);
1954 /* Test that BN_div never gives negative zero in the quotient. */
1955 if (!TEST_true(BN_set_word(numerator
, 1))
1956 || !TEST_true(BN_set_word(denominator
, 2)))
1958 BN_set_negative(numerator
, 1);
1959 if (!TEST_true(BN_div(a
, b
, numerator
, denominator
, ctx
))
1960 || !TEST_BN_eq_zero(a
)
1961 || !TEST_BN_ge_zero(a
))
1964 /* Test that BN_div never gives negative zero in the remainder. */
1965 if (!TEST_true(BN_set_word(denominator
, 1))
1966 || !TEST_true(BN_div(a
, b
, numerator
, denominator
, ctx
))
1967 || !TEST_BN_eq_zero(b
)
1968 || !TEST_BN_ge_zero(b
))
1971 BN_free(denominator
);
1972 numerator
= denominator
= NULL
;
1975 /* Test that BN_set_negative will not produce a negative zero. */
1977 BN_set_negative(a
, 1);
1978 if (BN_is_negative(a
))
1988 BN_free(denominator
);
1992 static int test_badmod(void)
1994 BIGNUM
*a
= NULL
, *b
= NULL
, *zero
= NULL
;
1995 BN_MONT_CTX
*mont
= NULL
;
1998 if (!TEST_ptr(a
= BN_new())
1999 || !TEST_ptr(b
= BN_new())
2000 || !TEST_ptr(zero
= BN_new())
2001 || !TEST_ptr(mont
= BN_MONT_CTX_new()))
2005 if (!TEST_false(BN_div(a
, b
, BN_value_one(), zero
, ctx
)))
2009 if (!TEST_false(BN_mod_mul(a
, BN_value_one(), BN_value_one(), zero
, ctx
)))
2013 if (!TEST_false(BN_mod_exp(a
, BN_value_one(), BN_value_one(), zero
, ctx
)))
2017 if (!TEST_false(BN_mod_exp_mont(a
, BN_value_one(), BN_value_one(),
2022 if (!TEST_false(BN_mod_exp_mont_consttime(a
, BN_value_one(), BN_value_one(),
2027 if (!TEST_false(BN_MONT_CTX_set(mont
, zero
, ctx
)))
2031 /* Some operations also may not be used with an even modulus. */
2032 if (!TEST_true(BN_set_word(b
, 16)))
2035 if (!TEST_false(BN_MONT_CTX_set(mont
, b
, ctx
)))
2039 if (!TEST_false(BN_mod_exp_mont(a
, BN_value_one(), BN_value_one(),
2044 if (!TEST_false(BN_mod_exp_mont_consttime(a
, BN_value_one(), BN_value_one(),
2054 BN_MONT_CTX_free(mont
);
2058 static int test_expmodzero(void)
2060 BIGNUM
*a
= NULL
, *r
= NULL
, *zero
= NULL
;
2063 if (!TEST_ptr(zero
= BN_new())
2064 || !TEST_ptr(a
= BN_new())
2065 || !TEST_ptr(r
= BN_new()))
2069 if (!TEST_true(BN_mod_exp(r
, a
, zero
, BN_value_one(), NULL
))
2070 || !TEST_BN_eq_zero(r
)
2071 || !TEST_true(BN_mod_exp_mont(r
, a
, zero
, BN_value_one(),
2073 || !TEST_BN_eq_zero(r
)
2074 || !TEST_true(BN_mod_exp_mont_consttime(r
, a
, zero
,
2077 || !TEST_BN_eq_zero(r
)
2078 || !TEST_true(BN_mod_exp_mont_word(r
, 42, zero
,
2079 BN_value_one(), NULL
, NULL
))
2080 || !TEST_BN_eq_zero(r
))
2091 static int test_expmodone(void)
2094 BIGNUM
*r
= BN_new();
2095 BIGNUM
*a
= BN_new();
2096 BIGNUM
*p
= BN_new();
2097 BIGNUM
*m
= BN_new();
2104 || !TEST_true(BN_set_word(a
, 1))
2105 || !TEST_true(BN_set_word(p
, 0))
2106 || !TEST_true(BN_set_word(m
, 1)))
2109 /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
2110 for (i
= 0; i
< 2; i
++) {
2111 if (!TEST_true(BN_mod_exp(r
, a
, p
, m
, NULL
))
2112 || !TEST_BN_eq_zero(r
)
2113 || !TEST_true(BN_mod_exp_mont(r
, a
, p
, m
, NULL
, NULL
))
2114 || !TEST_BN_eq_zero(r
)
2115 || !TEST_true(BN_mod_exp_mont_consttime(r
, a
, p
, m
, NULL
, NULL
))
2116 || !TEST_BN_eq_zero(r
)
2117 || !TEST_true(BN_mod_exp_mont_word(r
, 1, p
, m
, NULL
, NULL
))
2118 || !TEST_BN_eq_zero(r
)
2119 || !TEST_true(BN_mod_exp_simple(r
, a
, p
, m
, NULL
))
2120 || !TEST_BN_eq_zero(r
)
2121 || !TEST_true(BN_mod_exp_recp(r
, a
, p
, m
, NULL
))
2122 || !TEST_BN_eq_zero(r
))
2124 /* Repeat for r = 1 ^ 0 mod -1 */
2126 BN_set_negative(m
, 1);
2138 static int test_smallprime(void)
2140 static const int kBits
= 10;
2144 if (!TEST_ptr(r
= BN_new())
2145 || !TEST_true(BN_generate_prime_ex(r
, (int)kBits
, 0,
2147 || !TEST_int_eq(BN_num_bits(r
), kBits
))
2156 static int primes
[] = { 2, 3, 5, 7, 17863 };
2158 static int test_is_prime(int i
)
2164 if (!TEST_ptr(r
= BN_new()))
2167 for (trial
= 0; trial
<= 1; ++trial
) {
2168 if (!TEST_true(BN_set_word(r
, primes
[i
]))
2169 || !TEST_int_eq(BN_is_prime_fasttest_ex(r
, 1, ctx
, trial
, NULL
),
2180 static int not_primes
[] = { -1, 0, 1, 4 };
2182 static int test_not_prime(int i
)
2188 if (!TEST_ptr(r
= BN_new()))
2191 for (trial
= 0; trial
<= 1; ++trial
) {
2192 if (!TEST_true(BN_set_word(r
, not_primes
[i
]))
2193 || !TEST_false(BN_is_prime_fasttest_ex(r
, 1, ctx
, trial
, NULL
)))
2203 static int file_test_run(STANZA
*s
)
2205 static const FILETEST filetests
[] = {
2207 {"LShift1", file_lshift1
},
2208 {"LShift", file_lshift
},
2209 {"RShift", file_rshift
},
2210 {"Square", file_square
},
2211 {"Product", file_product
},
2212 {"Quotient", file_quotient
},
2213 {"ModMul", file_modmul
},
2214 {"ModExp", file_modexp
},
2216 {"ModSqrt", file_modsqrt
},
2218 int numtests
= OSSL_NELEM(filetests
);
2219 const FILETEST
*tp
= filetests
;
2221 for ( ; --numtests
>= 0; tp
++) {
2222 if (findattr(s
, tp
->name
) != NULL
) {
2224 TEST_info("%s:%d: Failed %s test",
2225 s
->test_file
, s
->start
, tp
->name
);
2231 TEST_info("%s:%d: Unknown test", s
->test_file
, s
->start
);
2235 static int run_file_tests(int i
)
2238 char *testfile
= test_get_argument(i
);
2241 if (!TEST_ptr(s
= OPENSSL_zalloc(sizeof(*s
))))
2243 if (!test_start_file(s
, testfile
)) {
2248 /* Read test file. */
2249 while (!BIO_eof(s
->fp
) && test_readstanza(s
)) {
2250 if (s
->numpairs
== 0)
2252 if (!file_test_run(s
))
2255 test_clearstanza(s
);
2265 int setup_tests(void)
2267 int n
= test_get_argument_count();
2269 if (!TEST_ptr(ctx
= BN_CTX_new()))
2274 ADD_TEST(test_div_recip
);
2276 ADD_TEST(test_modexp_mont5
);
2277 ADD_TEST(test_kronecker
);
2278 ADD_TEST(test_rand
);
2279 ADD_TEST(test_bn2padded
);
2280 ADD_TEST(test_dec2bn
);
2281 ADD_TEST(test_hex2bn
);
2282 ADD_TEST(test_asc2bn
);
2283 ADD_ALL_TESTS(test_mpi
, (int)OSSL_NELEM(kMPITests
));
2284 ADD_TEST(test_negzero
);
2285 ADD_TEST(test_badmod
);
2286 ADD_TEST(test_expmodzero
);
2287 ADD_TEST(test_expmodone
);
2288 ADD_TEST(test_smallprime
);
2289 ADD_TEST(test_swap
);
2290 #ifndef OPENSSL_NO_EC2M
2291 ADD_TEST(test_gf2m_add
);
2292 ADD_TEST(test_gf2m_mod
);
2293 ADD_TEST(test_gf2m_mul
);
2294 ADD_TEST(test_gf2m_sqr
);
2295 ADD_TEST(test_gf2m_modinv
);
2296 ADD_TEST(test_gf2m_moddiv
);
2297 ADD_TEST(test_gf2m_modexp
);
2298 ADD_TEST(test_gf2m_modsqrt
);
2299 ADD_TEST(test_gf2m_modsolvequad
);
2301 ADD_ALL_TESTS(test_is_prime
, (int)OSSL_NELEM(primes
));
2302 ADD_ALL_TESTS(test_not_prime
, (int)OSSL_NELEM(not_primes
));
2304 ADD_ALL_TESTS(run_file_tests
, n
);
2309 void cleanup_tests(void)