Rename 'buffer' to 'data' in OSSL_PARAM
[openssl.git] / test / bntest.c
1 /*
2  * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 #include <assert.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14
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"
21 #include "testutil.h"
22
23 #ifdef OPENSSL_SYS_WINDOWS
24 # define strcasecmp _stricmp
25 #endif
26
27 /*
28  * Things in boring, not in openssl.  TODO we should add them.
29  */
30 #define HAVE_BN_PADDED 0
31 #define HAVE_BN_SQRT 0
32
33 typedef struct filetest_st {
34     const char *name;
35     int (*func)(STANZA *s);
36 } FILETEST;
37
38 typedef struct mpitest_st {
39     const char *base10;
40     const char *mpi;
41     size_t mpi_len;
42 } MPITEST;
43
44 static const int NUM0 = 100;           /* number of tests */
45 static const int NUM1 = 50;            /* additional tests for some functions */
46 static BN_CTX *ctx;
47
48 /*
49  * Polynomial coefficients used in GFM tests.
50  */
51 #ifndef OPENSSL_NO_EC2M
52 static int p0[] = { 163, 7, 6, 3, 0, -1 };
53 static int p1[] = { 193, 15, 0, -1 };
54 #endif
55
56 /*
57  * Look for |key| in the stanza and return it or NULL if not found.
58  */
59 static const char *findattr(STANZA *s, const char *key)
60 {
61     int i = s->numpairs;
62     PAIR *pp = s->pairs;
63
64     for ( ; --i >= 0; pp++)
65         if (strcasecmp(pp->key, key) == 0)
66             return pp->value;
67     return NULL;
68 }
69
70 /*
71  * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
72  */
73 static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
74 {
75     char *bigstring = glue_strings(bn_strings, NULL);
76     int ret = BN_hex2bn(out, bigstring);
77
78     OPENSSL_free(bigstring);
79     return ret;
80 }
81
82 /*
83  * Parse BIGNUM, return number of bytes parsed.
84  */
85 static int parseBN(BIGNUM **out, const char *in)
86 {
87     *out = NULL;
88     return BN_hex2bn(out, in);
89 }
90
91 static int parsedecBN(BIGNUM **out, const char *in)
92 {
93     *out = NULL;
94     return BN_dec2bn(out, in);
95 }
96
97 static BIGNUM *getBN(STANZA *s, const char *attribute)
98 {
99     const char *hex;
100     BIGNUM *ret = NULL;
101
102     if ((hex = findattr(s, attribute)) == NULL) {
103         TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
104         return NULL;
105     }
106
107     if (parseBN(&ret, hex) != (int)strlen(hex)) {
108         TEST_error("Could not decode '%s'", hex);
109         return NULL;
110     }
111     return ret;
112 }
113
114 static int getint(STANZA *s, int *out, const char *attribute)
115 {
116     BIGNUM *ret;
117     BN_ULONG word;
118     int st = 0;
119
120     if (!TEST_ptr(ret = getBN(s, attribute))
121             || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
122         goto err;
123
124     *out = (int)word;
125     st = 1;
126  err:
127     BN_free(ret);
128     return st;
129 }
130
131 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
132 {
133     if (BN_cmp(expected, actual) == 0)
134         return 1;
135
136     TEST_error("unexpected %s value", op);
137     TEST_BN_eq(expected, actual);
138     return 0;
139 }
140
141 /*
142  * Return a "random" flag for if a BN should be negated.
143  */
144 static int rand_neg(void)
145 {
146     static unsigned int neg = 0;
147     static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
148
149     return sign[(neg++) % 8];
150 }
151
152 static int test_swap(void)
153 {
154     BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
155     int top, cond, st = 0;
156
157     if (!TEST_ptr(a = BN_new())
158             || !TEST_ptr(b = BN_new())
159             || !TEST_ptr(c = BN_new())
160             || !TEST_ptr(d = BN_new()))
161         goto err;
162
163     BN_bntest_rand(a, 1024, 1, 0);
164     BN_bntest_rand(b, 1024, 1, 0);
165     BN_copy(c, a);
166     BN_copy(d, b);
167     top = BN_num_bits(a) / BN_BITS2;
168
169     /* regular swap */
170     BN_swap(a, b);
171     if (!equalBN("swap", a, d)
172             || !equalBN("swap", b, c))
173         goto err;
174
175     /* conditional swap: true */
176     cond = 1;
177     BN_consttime_swap(cond, a, b, top);
178     if (!equalBN("cswap true", a, c)
179             || !equalBN("cswap true", b, d))
180         goto err;
181
182     /* conditional swap: false */
183     cond = 0;
184     BN_consttime_swap(cond, a, b, top);
185     if (!equalBN("cswap false", a, c)
186             || !equalBN("cswap false", b, d))
187         goto err;
188
189     /* same tests but checking flag swap */
190     BN_set_flags(a, BN_FLG_CONSTTIME);
191
192     BN_swap(a, b);
193     if (!equalBN("swap, flags", a, d)
194             || !equalBN("swap, flags", b, c)
195             || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
196             || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
197         goto err;
198
199     cond = 1;
200     BN_consttime_swap(cond, a, b, top);
201     if (!equalBN("cswap true, flags", a, c)
202             || !equalBN("cswap true, flags", b, d)
203             || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
204             || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
205         goto err;
206
207     cond = 0;
208     BN_consttime_swap(cond, a, b, top);
209     if (!equalBN("cswap false, flags", a, c)
210             || !equalBN("cswap false, flags", b, d)
211             || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
212             || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
213         goto err;
214
215     st = 1;
216  err:
217     BN_free(a);
218     BN_free(b);
219     BN_free(c);
220     BN_free(d);
221     return st;
222 }
223
224 static int test_sub(void)
225 {
226     BIGNUM *a = NULL, *b = NULL, *c = NULL;
227     int i, st = 0;
228
229     if (!TEST_ptr(a = BN_new())
230             || !TEST_ptr(b = BN_new())
231             || !TEST_ptr(c = BN_new()))
232         goto err;
233
234     for (i = 0; i < NUM0 + NUM1; i++) {
235         if (i < NUM1) {
236             BN_bntest_rand(a, 512, 0, 0);
237             BN_copy(b, a);
238             if (!TEST_int_ne(BN_set_bit(a, i), 0))
239                 goto err;
240             BN_add_word(b, i);
241         } else {
242             BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
243             BN_set_negative(a, rand_neg());
244             BN_set_negative(b, rand_neg());
245         }
246         BN_sub(c, a, b);
247         BN_add(c, c, b);
248         BN_sub(c, c, a);
249         if (!TEST_BN_eq_zero(c))
250             goto err;
251     }
252     st = 1;
253  err:
254     BN_free(a);
255     BN_free(b);
256     BN_free(c);
257     return st;
258 }
259
260 static int test_div_recip(void)
261 {
262     BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
263     BN_RECP_CTX *recp = NULL;
264     int st = 0, i;
265
266     if (!TEST_ptr(a = BN_new())
267             || !TEST_ptr(b = BN_new())
268             || !TEST_ptr(c = BN_new())
269             || !TEST_ptr(d = BN_new())
270             || !TEST_ptr(e = BN_new())
271             || !TEST_ptr(recp = BN_RECP_CTX_new()))
272         goto err;
273
274     for (i = 0; i < NUM0 + NUM1; i++) {
275         if (i < NUM1) {
276             BN_bntest_rand(a, 400, 0, 0);
277             BN_copy(b, a);
278             BN_lshift(a, a, i);
279             BN_add_word(a, i);
280         } else
281             BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
282         BN_set_negative(a, rand_neg());
283         BN_set_negative(b, rand_neg());
284         BN_RECP_CTX_set(recp, b, ctx);
285         BN_div_recp(d, c, a, recp, ctx);
286         BN_mul(e, d, b, ctx);
287         BN_add(d, e, c);
288         BN_sub(d, d, a);
289         if (!TEST_BN_eq_zero(d))
290             goto err;
291     }
292     st = 1;
293  err:
294     BN_free(a);
295     BN_free(b);
296     BN_free(c);
297     BN_free(d);
298     BN_free(e);
299     BN_RECP_CTX_free(recp);
300     return st;
301 }
302
303 static int test_mod(void)
304 {
305     BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
306     int st = 0, i;
307
308     if (!TEST_ptr(a = BN_new())
309             || !TEST_ptr(b = BN_new())
310             || !TEST_ptr(c = BN_new())
311             || !TEST_ptr(d = BN_new())
312             || !TEST_ptr(e = BN_new()))
313         goto err;
314
315     BN_bntest_rand(a, 1024, 0, 0);
316     for (i = 0; i < NUM0; i++) {
317         BN_bntest_rand(b, 450 + i * 10, 0, 0);
318         BN_set_negative(a, rand_neg());
319         BN_set_negative(b, rand_neg());
320         BN_mod(c, a, b, ctx);
321         BN_div(d, e, a, b, ctx);
322         BN_sub(e, e, c);
323         if (!TEST_BN_eq_zero(e))
324             goto err;
325     }
326     st = 1;
327  err:
328     BN_free(a);
329     BN_free(b);
330     BN_free(c);
331     BN_free(d);
332     BN_free(e);
333     return st;
334 }
335
336 static const char *bn1strings[] = {
337     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
338     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
339     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
340     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
341     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
342     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
343     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
344     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
345     "0000000000000000000000000000000000000000000000000000000000000000",
346     "0000000000000000000000000000000000000000000000000000000000000000",
347     "0000000000000000000000000000000000000000000000000000000000000000",
348     "0000000000000000000000000000000000000000000000000000000000000000",
349     "0000000000000000000000000000000000000000000000000000000000000000",
350     "0000000000000000000000000000000000000000000000000000000000000000",
351     "0000000000000000000000000000000000000000000000000000000000000000",
352     "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
353     NULL
354 };
355
356 static const char *bn2strings[] = {
357     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
358     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
359     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
360     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
361     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
362     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
363     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
364     "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
365     "0000000000000000000000000000000000000000000000000000000000000000",
366     "0000000000000000000000000000000000000000000000000000000000000000",
367     "0000000000000000000000000000000000000000000000000000000000000000",
368     "0000000000000000000000000000000000000000000000000000000000000000",
369     "0000000000000000000000000000000000000000000000000000000000000000",
370     "0000000000000000000000000000000000000000000000000000000000000000",
371     "0000000000000000000000000000000000000000000000000000000000000000",
372     "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
373     NULL
374 };
375
376 /*
377  * Test constant-time modular exponentiation with 1024-bit inputs, which on
378  * x86_64 cause a different code branch to be taken.
379  */
380 static int test_modexp_mont5(void)
381 {
382     BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
383     BIGNUM *b = NULL, *n = NULL, *c = NULL;
384     BN_MONT_CTX *mont = NULL;
385     int st = 0;
386
387     if (!TEST_ptr(a = BN_new())
388             || !TEST_ptr(p = BN_new())
389             || !TEST_ptr(m = BN_new())
390             || !TEST_ptr(d = BN_new())
391             || !TEST_ptr(e = BN_new())
392             || !TEST_ptr(b = BN_new())
393             || !TEST_ptr(n = BN_new())
394             || !TEST_ptr(c = BN_new())
395             || !TEST_ptr(mont = BN_MONT_CTX_new()))
396         goto err;
397
398     BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
399     /* Zero exponent */
400     BN_bntest_rand(a, 1024, 0, 0);
401     BN_zero(p);
402     if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
403         goto err;
404     if (!TEST_BN_eq_one(d))
405         goto err;
406
407     /* Regression test for carry bug in mulx4x_mont */
408     BN_hex2bn(&a,
409         "7878787878787878787878787878787878787878787878787878787878787878"
410         "7878787878787878787878787878787878787878787878787878787878787878"
411         "7878787878787878787878787878787878787878787878787878787878787878"
412         "7878787878787878787878787878787878787878787878787878787878787878");
413     BN_hex2bn(&b,
414         "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
415         "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
416         "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
417         "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
418     BN_hex2bn(&n,
419         "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
420         "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
421         "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
422         "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
423     BN_MONT_CTX_set(mont, n, ctx);
424     BN_mod_mul_montgomery(c, a, b, mont, ctx);
425     BN_mod_mul_montgomery(d, b, a, mont, ctx);
426     if (!TEST_BN_eq(c, d))
427         goto err;
428
429     /* Regression test for carry bug in sqr[x]8x_mont */
430     parse_bigBN(&n, bn1strings);
431     parse_bigBN(&a, bn2strings);
432     BN_free(b);
433     b = BN_dup(a);
434     BN_MONT_CTX_set(mont, n, ctx);
435     BN_mod_mul_montgomery(c, a, a, mont, ctx);
436     BN_mod_mul_montgomery(d, a, b, mont, ctx);
437     if (!TEST_BN_eq(c, d))
438         goto err;
439
440     /* Regression test for carry bug in bn_sqrx8x_internal */
441     {
442         static const char *ahex[] = {
443                       "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
444             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
445             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
446             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
447             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
448             "9544D954000000006C0000000000000000000000000000000000000000000000",
449             "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
450             "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
451             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
452             "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
453             "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
454             "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
455             NULL
456         };
457         static const char *nhex[] = {
458                       "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
459             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
460             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
461             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
462             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
463             "00000010000000006C0000000000000000000000000000000000000000000000",
464             "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
465             "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
466             "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
467             "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
468             "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
469             "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
470             NULL
471         };
472
473         parse_bigBN(&a, ahex);
474         parse_bigBN(&n, nhex);
475     }
476     BN_free(b);
477     b = BN_dup(a);
478     BN_MONT_CTX_set(mont, n, ctx);
479     if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
480             || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
481             || !TEST_BN_eq(c, d))
482         goto err;
483
484     /* Regression test for bug in BN_from_montgomery_word */
485     BN_hex2bn(&a,
486         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
487         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
488         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
489     BN_hex2bn(&n,
490         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
491         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
492     BN_MONT_CTX_set(mont, n, ctx);
493     if (!TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx)))
494         goto err;
495
496     /* Regression test for bug in rsaz_1024_mul_avx2 */
497     BN_hex2bn(&a,
498         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
499         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
500         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
501         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
502     BN_hex2bn(&b,
503         "2020202020202020202020202020202020202020202020202020202020202020"
504         "2020202020202020202020202020202020202020202020202020202020202020"
505         "20202020202020FF202020202020202020202020202020202020202020202020"
506         "2020202020202020202020202020202020202020202020202020202020202020");
507     BN_hex2bn(&n,
508         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
509         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
510         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
511         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF");
512     BN_MONT_CTX_set(mont, n, ctx);
513     BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont);
514     BN_mod_exp_mont(d, a, b, n, ctx, mont);
515     if (!TEST_BN_eq(c, d))
516         goto err;
517
518     /*
519      * rsaz_1024_mul_avx2 expects fully-reduced inputs.
520      * BN_mod_exp_mont_consttime should reduce the input first.
521      */
522     BN_hex2bn(&a,
523         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
524         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
525         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
526         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
527     BN_hex2bn(&b,
528         "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
529         "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
530         "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
531         "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E");
532     BN_hex2bn(&n,
533         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
534         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
535         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
536         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF");
537     BN_MONT_CTX_set(mont, n, ctx);
538     BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont);
539     BN_zero(d);
540     if (!TEST_BN_eq(c, d))
541         goto err;
542
543     /* Zero input */
544     BN_bntest_rand(p, 1024, 0, 0);
545     BN_zero(a);
546     if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
547             || !TEST_BN_eq_zero(d))
548         goto err;
549
550     /*
551      * Craft an input whose Montgomery representation is 1, i.e., shorter
552      * than the modulus m, in order to test the const time precomputation
553      * scattering/gathering.
554      */
555     BN_one(a);
556     BN_MONT_CTX_set(mont, m, ctx);
557     if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
558             || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
559             || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
560             || !TEST_BN_eq(a, d))
561         goto err;
562
563     /* Finally, some regular test vectors. */
564     BN_bntest_rand(e, 1024, 0, 0);
565     if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
566             || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
567             || !TEST_BN_eq(a, d))
568         goto err;
569
570     st = 1;
571
572  err:
573     BN_MONT_CTX_free(mont);
574     BN_free(a);
575     BN_free(p);
576     BN_free(m);
577     BN_free(d);
578     BN_free(e);
579     BN_free(b);
580     BN_free(n);
581     BN_free(c);
582     return st;
583 }
584
585 #ifndef OPENSSL_NO_EC2M
586 static int test_gf2m_add(void)
587 {
588     BIGNUM *a = NULL, *b = NULL, *c = NULL;
589     int i, st = 0;
590
591     if (!TEST_ptr(a = BN_new())
592             || !TEST_ptr(b = BN_new())
593             || !TEST_ptr(c = BN_new()))
594         goto err;
595
596     for (i = 0; i < NUM0; i++) {
597         BN_rand(a, 512, 0, 0);
598         BN_copy(b, BN_value_one());
599         BN_set_negative(a, rand_neg());
600         BN_set_negative(b, rand_neg());
601         BN_GF2m_add(c, a, b);
602         /* Test that two added values have the correct parity. */
603         if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
604                         || (!BN_is_odd(a) && !BN_is_odd(c))))
605             goto err;
606         BN_GF2m_add(c, c, c);
607         /* Test that c + c = 0. */
608         if (!TEST_BN_eq_zero(c))
609             goto err;
610     }
611     st = 1;
612  err:
613     BN_free(a);
614     BN_free(b);
615     BN_free(c);
616     return st;
617 }
618
619 static int test_gf2m_mod(void)
620 {
621     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
622     int i, j, st = 0;
623
624     if (!TEST_ptr(a = BN_new())
625             || !TEST_ptr(b[0] = BN_new())
626             || !TEST_ptr(b[1] = BN_new())
627             || !TEST_ptr(c = BN_new())
628             || !TEST_ptr(d = BN_new())
629             || !TEST_ptr(e = BN_new()))
630         goto err;
631
632     BN_GF2m_arr2poly(p0, b[0]);
633     BN_GF2m_arr2poly(p1, b[1]);
634
635     for (i = 0; i < NUM0; i++) {
636         BN_bntest_rand(a, 1024, 0, 0);
637         for (j = 0; j < 2; j++) {
638             BN_GF2m_mod(c, a, b[j]);
639             BN_GF2m_add(d, a, c);
640             BN_GF2m_mod(e, d, b[j]);
641             /* Test that a + (a mod p) mod p == 0. */
642             if (!TEST_BN_eq_zero(e))
643                 goto err;
644         }
645     }
646     st = 1;
647  err:
648     BN_free(a);
649     BN_free(b[0]);
650     BN_free(b[1]);
651     BN_free(c);
652     BN_free(d);
653     BN_free(e);
654     return st;
655 }
656
657 static int test_gf2m_mul(void)
658 {
659     BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
660     BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
661     int i, j, st = 0;
662
663     if (!TEST_ptr(a = BN_new())
664             || !TEST_ptr(b[0] = BN_new())
665             || !TEST_ptr(b[1] = BN_new())
666             || !TEST_ptr(c = BN_new())
667             || !TEST_ptr(d = BN_new())
668             || !TEST_ptr(e = BN_new())
669             || !TEST_ptr(f = BN_new())
670             || !TEST_ptr(g = BN_new())
671             || !TEST_ptr(h = BN_new()))
672         goto err;
673
674     BN_GF2m_arr2poly(p0, b[0]);
675     BN_GF2m_arr2poly(p1, b[1]);
676
677     for (i = 0; i < NUM0; i++) {
678         BN_bntest_rand(a, 1024, 0, 0);
679         BN_bntest_rand(c, 1024, 0, 0);
680         BN_bntest_rand(d, 1024, 0, 0);
681         for (j = 0; j < 2; j++) {
682             BN_GF2m_mod_mul(e, a, c, b[j], ctx);
683             BN_GF2m_add(f, a, d);
684             BN_GF2m_mod_mul(g, f, c, b[j], ctx);
685             BN_GF2m_mod_mul(h, d, c, b[j], ctx);
686             BN_GF2m_add(f, e, g);
687             BN_GF2m_add(f, f, h);
688             /* Test that (a+d)*c = a*c + d*c. */
689             if (!TEST_BN_eq_zero(f))
690                 goto err;
691         }
692     }
693     st = 1;
694
695  err:
696     BN_free(a);
697     BN_free(b[0]);
698     BN_free(b[1]);
699     BN_free(c);
700     BN_free(d);
701     BN_free(e);
702     BN_free(f);
703     BN_free(g);
704     BN_free(h);
705     return st;
706 }
707
708 static int test_gf2m_sqr(void)
709 {
710     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
711     int i, j, st = 0;
712
713     if (!TEST_ptr(a = BN_new())
714             || !TEST_ptr(b[0] = BN_new())
715             || !TEST_ptr(b[1] = BN_new())
716             || !TEST_ptr(c = BN_new())
717             || !TEST_ptr(d = BN_new()))
718         goto err;
719
720     BN_GF2m_arr2poly(p0, b[0]);
721     BN_GF2m_arr2poly(p1, b[1]);
722
723     for (i = 0; i < NUM0; i++) {
724         BN_bntest_rand(a, 1024, 0, 0);
725         for (j = 0; j < 2; j++) {
726             BN_GF2m_mod_sqr(c, a, b[j], ctx);
727             BN_copy(d, a);
728             BN_GF2m_mod_mul(d, a, d, b[j], ctx);
729             BN_GF2m_add(d, c, d);
730             /* Test that a*a = a^2. */
731             if (!TEST_BN_eq_zero(d))
732                 goto err;
733         }
734     }
735     st = 1;
736  err:
737     BN_free(a);
738     BN_free(b[0]);
739     BN_free(b[1]);
740     BN_free(c);
741     BN_free(d);
742     return st;
743 }
744
745 static int test_gf2m_modinv(void)
746 {
747     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
748     int i, j, st = 0;
749
750     if (!TEST_ptr(a = BN_new())
751             || !TEST_ptr(b[0] = BN_new())
752             || !TEST_ptr(b[1] = BN_new())
753             || !TEST_ptr(c = BN_new())
754             || !TEST_ptr(d = BN_new()))
755         goto err;
756
757     BN_GF2m_arr2poly(p0, b[0]);
758     BN_GF2m_arr2poly(p1, b[1]);
759
760     for (i = 0; i < NUM0; i++) {
761         BN_bntest_rand(a, 512, 0, 0);
762         for (j = 0; j < 2; j++) {
763             BN_GF2m_mod_inv(c, a, b[j], ctx);
764             BN_GF2m_mod_mul(d, a, c, b[j], ctx);
765             /* Test that ((1/a)*a) = 1. */
766             if (!TEST_BN_eq_one(d))
767                 goto err;
768         }
769     }
770     st = 1;
771  err:
772     BN_free(a);
773     BN_free(b[0]);
774     BN_free(b[1]);
775     BN_free(c);
776     BN_free(d);
777     return st;
778 }
779
780 static int test_gf2m_moddiv(void)
781 {
782     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
783     BIGNUM *e = NULL, *f = NULL;
784     int i, j, st = 0;
785
786     if (!TEST_ptr(a = BN_new())
787             || !TEST_ptr(b[0] = BN_new())
788             || !TEST_ptr(b[1] = BN_new())
789             || !TEST_ptr(c = BN_new())
790             || !TEST_ptr(d = BN_new())
791             || !TEST_ptr(e = BN_new())
792             || !TEST_ptr(f = BN_new()))
793         goto err;
794
795     BN_GF2m_arr2poly(p0, b[0]);
796     BN_GF2m_arr2poly(p1, b[1]);
797
798     for (i = 0; i < NUM0; i++) {
799         BN_bntest_rand(a, 512, 0, 0);
800         BN_bntest_rand(c, 512, 0, 0);
801         for (j = 0; j < 2; j++) {
802             BN_GF2m_mod_div(d, a, c, b[j], ctx);
803             BN_GF2m_mod_mul(e, d, c, b[j], ctx);
804             BN_GF2m_mod_div(f, a, e, b[j], ctx);
805             /* Test that ((a/c)*c)/a = 1. */
806             if (!TEST_BN_eq_one(f))
807                 goto err;
808         }
809     }
810     st = 1;
811  err:
812     BN_free(a);
813     BN_free(b[0]);
814     BN_free(b[1]);
815     BN_free(c);
816     BN_free(d);
817     BN_free(e);
818     BN_free(f);
819     return st;
820 }
821
822 static int test_gf2m_modexp(void)
823 {
824     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
825     BIGNUM *e = NULL, *f = NULL;
826     int i, j, st = 0;
827
828     if (!TEST_ptr(a = BN_new())
829             || !TEST_ptr(b[0] = BN_new())
830             || !TEST_ptr(b[1] = BN_new())
831             || !TEST_ptr(c = BN_new())
832             || !TEST_ptr(d = BN_new())
833             || !TEST_ptr(e = BN_new())
834             || !TEST_ptr(f = BN_new()))
835         goto err;
836
837     BN_GF2m_arr2poly(p0, b[0]);
838     BN_GF2m_arr2poly(p1, b[1]);
839
840     for (i = 0; i < NUM0; i++) {
841         BN_bntest_rand(a, 512, 0, 0);
842         BN_bntest_rand(c, 512, 0, 0);
843         BN_bntest_rand(d, 512, 0, 0);
844         for (j = 0; j < 2; j++) {
845             BN_GF2m_mod_exp(e, a, c, b[j], ctx);
846             BN_GF2m_mod_exp(f, a, d, b[j], ctx);
847             BN_GF2m_mod_mul(e, e, f, b[j], ctx);
848             BN_add(f, c, d);
849             BN_GF2m_mod_exp(f, a, f, b[j], ctx);
850             BN_GF2m_add(f, e, f);
851             /* Test that a^(c+d)=a^c*a^d. */
852             if (!TEST_BN_eq_zero(f))
853                 goto err;
854         }
855     }
856     st = 1;
857  err:
858     BN_free(a);
859     BN_free(b[0]);
860     BN_free(b[1]);
861     BN_free(c);
862     BN_free(d);
863     BN_free(e);
864     BN_free(f);
865     return st;
866 }
867
868 static int test_gf2m_modsqrt(void)
869 {
870     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
871     BIGNUM *e = NULL, *f = NULL;
872     int i, j, st = 0;
873
874     if (!TEST_ptr(a = BN_new())
875             || !TEST_ptr(b[0] = BN_new())
876             || !TEST_ptr(b[1] = BN_new())
877             || !TEST_ptr(c = BN_new())
878             || !TEST_ptr(d = BN_new())
879             || !TEST_ptr(e = BN_new())
880             || !TEST_ptr(f = BN_new()))
881         goto err;
882
883     BN_GF2m_arr2poly(p0, b[0]);
884     BN_GF2m_arr2poly(p1, b[1]);
885
886     for (i = 0; i < NUM0; i++) {
887         BN_bntest_rand(a, 512, 0, 0);
888         for (j = 0; j < 2; j++) {
889             BN_GF2m_mod(c, a, b[j]);
890             BN_GF2m_mod_sqrt(d, a, b[j], ctx);
891             BN_GF2m_mod_sqr(e, d, b[j], ctx);
892             BN_GF2m_add(f, c, e);
893             /* Test that d^2 = a, where d = sqrt(a). */
894             if (!TEST_BN_eq_zero(f))
895                 goto err;
896         }
897     }
898     st = 1;
899  err:
900     BN_free(a);
901     BN_free(b[0]);
902     BN_free(b[1]);
903     BN_free(c);
904     BN_free(d);
905     BN_free(e);
906     BN_free(f);
907     return st;
908 }
909
910 static int test_gf2m_modsolvequad(void)
911 {
912     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
913     BIGNUM *e = NULL;
914     int i, j, s = 0, t, st = 0;
915
916     if (!TEST_ptr(a = BN_new())
917             || !TEST_ptr(b[0] = BN_new())
918             || !TEST_ptr(b[1] = BN_new())
919             || !TEST_ptr(c = BN_new())
920             || !TEST_ptr(d = BN_new())
921             || !TEST_ptr(e = BN_new()))
922         goto err;
923
924     BN_GF2m_arr2poly(p0, b[0]);
925     BN_GF2m_arr2poly(p1, b[1]);
926
927     for (i = 0; i < NUM0; i++) {
928         BN_bntest_rand(a, 512, 0, 0);
929         for (j = 0; j < 2; j++) {
930             t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
931             if (t) {
932                 s++;
933                 BN_GF2m_mod_sqr(d, c, b[j], ctx);
934                 BN_GF2m_add(d, c, d);
935                 BN_GF2m_mod(e, a, b[j]);
936                 BN_GF2m_add(e, e, d);
937                 /*
938                  * Test that solution of quadratic c satisfies c^2 + c = a.
939                  */
940                 if (!TEST_BN_eq_zero(e))
941                     goto err;
942             }
943         }
944     }
945     if (!TEST_int_ge(s, 0)) {
946         TEST_info("%d tests found no roots; probably an error", NUM0);
947         goto err;
948     }
949     st = 1;
950  err:
951     BN_free(a);
952     BN_free(b[0]);
953     BN_free(b[1]);
954     BN_free(c);
955     BN_free(d);
956     BN_free(e);
957     return st;
958 }
959 #endif
960
961 static int test_kronecker(void)
962 {
963     BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
964     int i, legendre, kronecker, st = 0;
965
966     if (!TEST_ptr(a = BN_new())
967             || !TEST_ptr(b = BN_new())
968             || !TEST_ptr(r = BN_new())
969             || !TEST_ptr(t = BN_new()))
970         goto err;
971
972     /*
973      * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
974      * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
975      * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
976      * generate a random prime b and compare these values for a number of
977      * random a's.  (That is, we run the Solovay-Strassen primality test to
978      * confirm that b is prime, except that we don't want to test whether b
979      * is prime but whether BN_kronecker works.)
980      */
981
982     if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
983         goto err;
984     BN_set_negative(b, rand_neg());
985
986     for (i = 0; i < NUM0; i++) {
987         if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
988             goto err;
989         BN_set_negative(a, rand_neg());
990
991         /* t := (|b|-1)/2  (note that b is odd) */
992         if (!TEST_true(BN_copy(t, b)))
993             goto err;
994         BN_set_negative(t, 0);
995         if (!TEST_true(BN_sub_word(t, 1)))
996             goto err;
997         if (!TEST_true(BN_rshift1(t, t)))
998             goto err;
999         /* r := a^t mod b */
1000         BN_set_negative(b, 0);
1001
1002         if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
1003             goto err;
1004         BN_set_negative(b, 1);
1005
1006         if (BN_is_word(r, 1))
1007             legendre = 1;
1008         else if (BN_is_zero(r))
1009             legendre = 0;
1010         else {
1011             if (!TEST_true(BN_add_word(r, 1)))
1012                 goto err;
1013             if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
1014                 TEST_info("Legendre symbol computation failed");
1015                 goto err;
1016             }
1017             legendre = -1;
1018         }
1019
1020         if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
1021             goto err;
1022         /* we actually need BN_kronecker(a, |b|) */
1023         if (BN_is_negative(a) && BN_is_negative(b))
1024             kronecker = -kronecker;
1025
1026         if (!TEST_int_eq(legendre, kronecker))
1027             goto err;
1028     }
1029
1030     st = 1;
1031  err:
1032     BN_free(a);
1033     BN_free(b);
1034     BN_free(r);
1035     BN_free(t);
1036     return st;
1037 }
1038
1039 static int file_sum(STANZA *s)
1040 {
1041     BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
1042     BN_ULONG b_word;
1043     int st = 0;
1044
1045     if (!TEST_ptr(a = getBN(s, "A"))
1046             || !TEST_ptr(b = getBN(s, "B"))
1047             || !TEST_ptr(sum = getBN(s, "Sum"))
1048             || !TEST_ptr(ret = BN_new()))
1049         goto err;
1050
1051     if (!TEST_true(BN_add(ret, a, b))
1052             || !equalBN("A + B", sum, ret)
1053             || !TEST_true(BN_sub(ret, sum, a))
1054             || !equalBN("Sum - A", b, ret)
1055             || !TEST_true(BN_sub(ret, sum, b))
1056             || !equalBN("Sum - B", a, ret))
1057         goto err;
1058
1059     /*
1060      * Test that the functions work when |r| and |a| point to the same BIGNUM,
1061      * or when |r| and |b| point to the same BIGNUM.
1062      * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1063      */
1064     if (!TEST_true(BN_copy(ret, a))
1065             || !TEST_true(BN_add(ret, ret, b))
1066             || !equalBN("A + B (r is a)", sum, ret)
1067             || !TEST_true(BN_copy(ret, b))
1068             || !TEST_true(BN_add(ret, a, ret))
1069             || !equalBN("A + B (r is b)", sum, ret)
1070             || !TEST_true(BN_copy(ret, sum))
1071             || !TEST_true(BN_sub(ret, ret, a))
1072             || !equalBN("Sum - A (r is a)", b, ret)
1073             || !TEST_true(BN_copy(ret, a))
1074             || !TEST_true(BN_sub(ret, sum, ret))
1075             || !equalBN("Sum - A (r is b)", b, ret)
1076             || !TEST_true(BN_copy(ret, sum))
1077             || !TEST_true(BN_sub(ret, ret, b))
1078             || !equalBN("Sum - B (r is a)", a, ret)
1079             || !TEST_true(BN_copy(ret, b))
1080             || !TEST_true(BN_sub(ret, sum, ret))
1081             || !equalBN("Sum - B (r is b)", a, ret))
1082         goto err;
1083
1084     /*
1085      * Test BN_uadd() and BN_usub() with the prerequisites they are
1086      * documented as having. Note that these functions are frequently used
1087      * when the prerequisites don't hold. In those cases, they are supposed
1088      * to work as if the prerequisite hold, but we don't test that yet.
1089      * TODO: test that.
1090      */
1091     if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
1092         if (!TEST_true(BN_uadd(ret, a, b))
1093                 || !equalBN("A +u B", sum, ret)
1094                 || !TEST_true(BN_usub(ret, sum, a))
1095                 || !equalBN("Sum -u A", b, ret)
1096                 || !TEST_true(BN_usub(ret, sum, b))
1097                 || !equalBN("Sum -u B", a, ret))
1098             goto err;
1099         /*
1100          * Test that the functions work when |r| and |a| point to the same
1101          * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1102          * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1103          */
1104         if (!TEST_true(BN_copy(ret, a))
1105                 || !TEST_true(BN_uadd(ret, ret, b))
1106                 || !equalBN("A +u B (r is a)", sum, ret)
1107                 || !TEST_true(BN_copy(ret, b))
1108                 || !TEST_true(BN_uadd(ret, a, ret))
1109                 || !equalBN("A +u B (r is b)", sum, ret)
1110                 || !TEST_true(BN_copy(ret, sum))
1111                 || !TEST_true(BN_usub(ret, ret, a))
1112                 || !equalBN("Sum -u A (r is a)", b, ret)
1113                 || !TEST_true(BN_copy(ret, a))
1114                 || !TEST_true(BN_usub(ret, sum, ret))
1115                 || !equalBN("Sum -u A (r is b)", b, ret)
1116                 || !TEST_true(BN_copy(ret, sum))
1117                 || !TEST_true(BN_usub(ret, ret, b))
1118                 || !equalBN("Sum -u B (r is a)", a, ret)
1119                 || !TEST_true(BN_copy(ret, b))
1120                 || !TEST_true(BN_usub(ret, sum, ret))
1121                 || !equalBN("Sum -u B (r is b)", a, ret))
1122             goto err;
1123     }
1124
1125     /*
1126      * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1127      */
1128     b_word = BN_get_word(b);
1129     if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1130         if (!TEST_true(BN_copy(ret, a))
1131                 || !TEST_true(BN_add_word(ret, b_word))
1132                 || !equalBN("A + B (word)", sum, ret)
1133                 || !TEST_true(BN_copy(ret, sum))
1134                 || !TEST_true(BN_sub_word(ret, b_word))
1135                 || !equalBN("Sum - B (word)", a, ret))
1136             goto err;
1137     }
1138     st = 1;
1139
1140  err:
1141     BN_free(a);
1142     BN_free(b);
1143     BN_free(sum);
1144     BN_free(ret);
1145     return st;
1146 }
1147
1148 static int file_lshift1(STANZA *s)
1149 {
1150     BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1151     BIGNUM *two = NULL, *remainder = NULL;
1152     int st = 0;
1153
1154     if (!TEST_ptr(a = getBN(s, "A"))
1155             || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1156             || !TEST_ptr(zero = BN_new())
1157             || !TEST_ptr(ret = BN_new())
1158             || !TEST_ptr(two = BN_new())
1159             || !TEST_ptr(remainder = BN_new()))
1160         goto err;
1161
1162     BN_zero(zero);
1163
1164     if (!TEST_true(BN_set_word(two, 2))
1165             || !TEST_true(BN_add(ret, a, a))
1166             || !equalBN("A + A", lshift1, ret)
1167             || !TEST_true(BN_mul(ret, a, two, ctx))
1168             || !equalBN("A * 2", lshift1, ret)
1169             || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1170             || !equalBN("LShift1 / 2", a, ret)
1171             || !equalBN("LShift1 % 2", zero, remainder)
1172             || !TEST_true(BN_lshift1(ret, a))
1173             || !equalBN("A << 1", lshift1, ret)
1174             || !TEST_true(BN_rshift1(ret, lshift1))
1175             || !equalBN("LShift >> 1", a, ret)
1176             || !TEST_true(BN_rshift1(ret, lshift1))
1177             || !equalBN("LShift >> 1", a, ret))
1178         goto err;
1179
1180     /* Set the LSB to 1 and test rshift1 again. */
1181     if (!TEST_true(BN_set_bit(lshift1, 0))
1182             || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1183             || !equalBN("(LShift1 | 1) / 2", a, ret)
1184             || !TEST_true(BN_rshift1(ret, lshift1))
1185             || !equalBN("(LShift | 1) >> 1", a, ret))
1186         goto err;
1187
1188     st = 1;
1189  err:
1190     BN_free(a);
1191     BN_free(lshift1);
1192     BN_free(zero);
1193     BN_free(ret);
1194     BN_free(two);
1195     BN_free(remainder);
1196
1197     return st;
1198 }
1199
1200 static int file_lshift(STANZA *s)
1201 {
1202     BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1203     int n = 0, st = 0;
1204
1205     if (!TEST_ptr(a = getBN(s, "A"))
1206             || !TEST_ptr(lshift = getBN(s, "LShift"))
1207             || !TEST_ptr(ret = BN_new())
1208             || !getint(s, &n, "N"))
1209         goto err;
1210
1211     if (!TEST_true(BN_lshift(ret, a, n))
1212             || !equalBN("A << N", lshift, ret)
1213             || !TEST_true(BN_rshift(ret, lshift, n))
1214             || !equalBN("A >> N", a, ret))
1215         goto err;
1216
1217     st = 1;
1218  err:
1219     BN_free(a);
1220     BN_free(lshift);
1221     BN_free(ret);
1222     return st;
1223 }
1224
1225 static int file_rshift(STANZA *s)
1226 {
1227     BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1228     int n = 0, st = 0;
1229
1230     if (!TEST_ptr(a = getBN(s, "A"))
1231             || !TEST_ptr(rshift = getBN(s, "RShift"))
1232             || !TEST_ptr(ret = BN_new())
1233             || !getint(s, &n, "N"))
1234         goto err;
1235
1236     if (!TEST_true(BN_rshift(ret, a, n))
1237             || !equalBN("A >> N", rshift, ret))
1238         goto err;
1239
1240     /* If N == 1, try with rshift1 as well */
1241     if (n == 1) {
1242         if (!TEST_true(BN_rshift1(ret, a))
1243                 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1244             goto err;
1245     }
1246     st = 1;
1247
1248  err:
1249     BN_free(a);
1250     BN_free(rshift);
1251     BN_free(ret);
1252     return st;
1253 }
1254
1255 static int file_square(STANZA *s)
1256 {
1257     BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1258     BIGNUM *remainder = NULL, *tmp = NULL;
1259     int st = 0;
1260
1261     if (!TEST_ptr(a = getBN(s, "A"))
1262             || !TEST_ptr(square = getBN(s, "Square"))
1263             || !TEST_ptr(zero = BN_new())
1264             || !TEST_ptr(ret = BN_new())
1265             || !TEST_ptr(remainder = BN_new()))
1266         goto err;
1267
1268     BN_zero(zero);
1269     if (!TEST_true(BN_sqr(ret, a, ctx))
1270             || !equalBN("A^2", square, ret)
1271             || !TEST_true(BN_mul(ret, a, a, ctx))
1272             || !equalBN("A * A", square, ret)
1273             || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1274             || !equalBN("Square / A", a, ret)
1275             || !equalBN("Square % A", zero, remainder))
1276         goto err;
1277
1278 #if HAVE_BN_SQRT
1279     BN_set_negative(a, 0);
1280     if (!TEST_true(BN_sqrt(ret, square, ctx))
1281             || !equalBN("sqrt(Square)", a, ret))
1282         goto err;
1283
1284     /* BN_sqrt should fail on non-squares and negative numbers. */
1285     if (!TEST_BN_eq_zero(square)) {
1286         if (!TEST_ptr(tmp = BN_new())
1287                 || !TEST_true(BN_copy(tmp, square)))
1288             goto err;
1289         BN_set_negative(tmp, 1);
1290
1291         if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1292             goto err;
1293         ERR_clear_error();
1294
1295         BN_set_negative(tmp, 0);
1296         if (BN_add(tmp, tmp, BN_value_one()))
1297             goto err;
1298         if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1299             goto err;
1300         ERR_clear_error();
1301     }
1302 #endif
1303
1304     st = 1;
1305  err:
1306     BN_free(a);
1307     BN_free(square);
1308     BN_free(zero);
1309     BN_free(ret);
1310     BN_free(remainder);
1311     BN_free(tmp);
1312     return st;
1313 }
1314
1315 static int file_product(STANZA *s)
1316 {
1317     BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1318     BIGNUM *remainder = NULL, *zero = NULL;
1319     int st = 0;
1320
1321     if (!TEST_ptr(a = getBN(s, "A"))
1322             || !TEST_ptr(b = getBN(s, "B"))
1323             || !TEST_ptr(product = getBN(s, "Product"))
1324             || !TEST_ptr(ret = BN_new())
1325             || !TEST_ptr(remainder = BN_new())
1326             || !TEST_ptr(zero = BN_new()))
1327         goto err;
1328
1329     BN_zero(zero);
1330
1331     if (!TEST_true(BN_mul(ret, a, b, ctx))
1332             || !equalBN("A * B", product, ret)
1333             || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1334             || !equalBN("Product / A", b, ret)
1335             || !equalBN("Product % A", zero, remainder)
1336             || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1337             || !equalBN("Product / B", a, ret)
1338             || !equalBN("Product % B", zero, remainder))
1339         goto err;
1340
1341     st = 1;
1342  err:
1343     BN_free(a);
1344     BN_free(b);
1345     BN_free(product);
1346     BN_free(ret);
1347     BN_free(remainder);
1348     BN_free(zero);
1349     return st;
1350 }
1351
1352 static int file_quotient(STANZA *s)
1353 {
1354     BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1355     BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1356     BN_ULONG b_word, ret_word;
1357     int st = 0;
1358
1359     if (!TEST_ptr(a = getBN(s, "A"))
1360             || !TEST_ptr(b = getBN(s, "B"))
1361             || !TEST_ptr(quotient = getBN(s, "Quotient"))
1362             || !TEST_ptr(remainder = getBN(s, "Remainder"))
1363             || !TEST_ptr(ret = BN_new())
1364             || !TEST_ptr(ret2 = BN_new())
1365             || !TEST_ptr(nnmod = BN_new()))
1366         goto err;
1367
1368     if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1369             || !equalBN("A / B", quotient, ret)
1370             || !equalBN("A % B", remainder, ret2)
1371             || !TEST_true(BN_mul(ret, quotient, b, ctx))
1372             || !TEST_true(BN_add(ret, ret, remainder))
1373             || !equalBN("Quotient * B + Remainder", a, ret))
1374         goto err;
1375
1376     /*
1377      * Test with BN_mod_word() and BN_div_word() if the divisor is
1378      * small enough.
1379      */
1380     b_word = BN_get_word(b);
1381     if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1382         BN_ULONG remainder_word = BN_get_word(remainder);
1383
1384         assert(remainder_word != (BN_ULONG)-1);
1385         if (!TEST_ptr(BN_copy(ret, a)))
1386             goto err;
1387         ret_word = BN_div_word(ret, b_word);
1388         if (ret_word != remainder_word) {
1389 #ifdef BN_DEC_FMT1
1390             TEST_error(
1391                     "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1392                     ret_word, remainder_word);
1393 #else
1394             TEST_error("Got A %% B (word) mismatch");
1395 #endif
1396             goto err;
1397         }
1398         if (!equalBN ("A / B (word)", quotient, ret))
1399             goto err;
1400
1401         ret_word = BN_mod_word(a, b_word);
1402         if (ret_word != remainder_word) {
1403 #ifdef BN_DEC_FMT1
1404             TEST_error(
1405                     "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1406                     ret_word, remainder_word);
1407 #else
1408             TEST_error("Got A %% B (word) mismatch");
1409 #endif
1410             goto err;
1411         }
1412     }
1413
1414     /* Test BN_nnmod. */
1415     if (!BN_is_negative(b)) {
1416         if (!TEST_true(BN_copy(nnmod, remainder))
1417                 || (BN_is_negative(nnmod)
1418                         && !TEST_true(BN_add(nnmod, nnmod, b)))
1419                 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1420                 || !equalBN("A % B (non-negative)", nnmod, ret))
1421             goto err;
1422     }
1423
1424     st = 1;
1425  err:
1426     BN_free(a);
1427     BN_free(b);
1428     BN_free(quotient);
1429     BN_free(remainder);
1430     BN_free(ret);
1431     BN_free(ret2);
1432     BN_free(nnmod);
1433     return st;
1434 }
1435
1436 static int file_modmul(STANZA *s)
1437 {
1438     BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1439     int st = 0;
1440
1441     if (!TEST_ptr(a = getBN(s, "A"))
1442             || !TEST_ptr(b = getBN(s, "B"))
1443             || !TEST_ptr(m = getBN(s, "M"))
1444             || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1445             || !TEST_ptr(ret = BN_new()))
1446         goto err;
1447
1448     if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1449             || !equalBN("A * B (mod M)", mod_mul, ret))
1450         goto err;
1451
1452     if (BN_is_odd(m)) {
1453         /* Reduce |a| and |b| and test the Montgomery version. */
1454         BN_MONT_CTX *mont = BN_MONT_CTX_new();
1455         BIGNUM *a_tmp = BN_new();
1456         BIGNUM *b_tmp = BN_new();
1457
1458         if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1459                 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1460                 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1461                 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1462                 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1463                 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1464                 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1465                                                     mont, ctx))
1466                 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1467                 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1468             st = 0;
1469         else
1470             st = 1;
1471         BN_MONT_CTX_free(mont);
1472         BN_free(a_tmp);
1473         BN_free(b_tmp);
1474         if (st == 0)
1475             goto err;
1476     }
1477
1478     st = 1;
1479  err:
1480     BN_free(a);
1481     BN_free(b);
1482     BN_free(m);
1483     BN_free(mod_mul);
1484     BN_free(ret);
1485     return st;
1486 }
1487
1488 static int file_modexp(STANZA *s)
1489 {
1490     BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1491     BIGNUM *b = NULL, *c = NULL, *d = NULL;
1492     int st = 0;
1493
1494     if (!TEST_ptr(a = getBN(s, "A"))
1495             || !TEST_ptr(e = getBN(s, "E"))
1496             || !TEST_ptr(m = getBN(s, "M"))
1497             || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1498             || !TEST_ptr(ret = BN_new())
1499             || !TEST_ptr(d = BN_new()))
1500         goto err;
1501
1502     if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1503             || !equalBN("A ^ E (mod M)", mod_exp, ret))
1504         goto err;
1505
1506     if (BN_is_odd(m)) {
1507         if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1508                 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1509                 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1510                                                         ctx, NULL))
1511                 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1512             goto err;
1513     }
1514
1515     /* Regression test for carry propagation bug in sqr8x_reduction */
1516     BN_hex2bn(&a, "050505050505");
1517     BN_hex2bn(&b, "02");
1518     BN_hex2bn(&c,
1519         "4141414141414141414141274141414141414141414141414141414141414141"
1520         "4141414141414141414141414141414141414141414141414141414141414141"
1521         "4141414141414141414141800000000000000000000000000000000000000000"
1522         "0000000000000000000000000000000000000000000000000000000000000000"
1523         "0000000000000000000000000000000000000000000000000000000000000000"
1524         "0000000000000000000000000000000000000000000000000000000001");
1525     if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1526         || !TEST_true(BN_mul(e, a, a, ctx))
1527         || !TEST_BN_eq(d, e))
1528         goto err;
1529
1530     st = 1;
1531  err:
1532     BN_free(a);
1533     BN_free(b);
1534     BN_free(c);
1535     BN_free(d);
1536     BN_free(e);
1537     BN_free(m);
1538     BN_free(mod_exp);
1539     BN_free(ret);
1540     return st;
1541 }
1542
1543 static int file_exp(STANZA *s)
1544 {
1545     BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1546     int st = 0;
1547
1548     if (!TEST_ptr(a = getBN(s, "A"))
1549             || !TEST_ptr(e = getBN(s, "E"))
1550             || !TEST_ptr(exp = getBN(s, "Exp"))
1551             || !TEST_ptr(ret = BN_new()))
1552         goto err;
1553
1554     if (!TEST_true(BN_exp(ret, a, e, ctx))
1555             || !equalBN("A ^ E", exp, ret))
1556         goto err;
1557
1558     st = 1;
1559  err:
1560     BN_free(a);
1561     BN_free(e);
1562     BN_free(exp);
1563     BN_free(ret);
1564     return st;
1565 }
1566
1567 static int file_modsqrt(STANZA *s)
1568 {
1569     BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1570     int st = 0;
1571
1572     if (!TEST_ptr(a = getBN(s, "A"))
1573             || !TEST_ptr(p = getBN(s, "P"))
1574             || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1575             || !TEST_ptr(ret = BN_new())
1576             || !TEST_ptr(ret2 = BN_new()))
1577         goto err;
1578
1579     /* There are two possible answers. */
1580     if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1581             || !TEST_true(BN_sub(ret2, p, ret)))
1582         goto err;
1583
1584     /* The first condition should NOT be a test. */
1585     if (BN_cmp(ret2, mod_sqrt) != 0
1586             && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1587         goto err;
1588
1589     st = 1;
1590  err:
1591     BN_free(a);
1592     BN_free(p);
1593     BN_free(mod_sqrt);
1594     BN_free(ret);
1595     BN_free(ret2);
1596     return st;
1597 }
1598
1599 static int test_bn2padded(void)
1600 {
1601 #if HAVE_BN_PADDED
1602     uint8_t zeros[256], out[256], reference[128];
1603     BIGNUM *n = BN_new();
1604     int st = 0;
1605
1606     /* Test edge case at 0. */
1607     if (n == NULL)
1608         goto err;
1609     if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1610         goto err;
1611     memset(out, -1, sizeof(out));
1612     if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1613         goto err;
1614     memset(zeros, 0, sizeof(zeros));
1615     if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1616         goto err;
1617
1618     /* Test a random numbers at various byte lengths. */
1619     for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1620 # define TOP_BIT_ON 0
1621 # define BOTTOM_BIT_NOTOUCH 0
1622         if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1623             goto err;
1624         if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1625                 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1626             goto err;
1627         /* Empty buffer should fail. */
1628         if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1629             goto err;
1630         /* One byte short should fail. */
1631         if (BN_bn2bin_padded(out, bytes - 1, n))
1632             goto err;
1633         /* Exactly right size should encode. */
1634         if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1635                 || TEST_mem_eq(out, bytes, reference, bytes))
1636             goto err;
1637         /* Pad up one byte extra. */
1638         if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1639                 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1640                 || !TEST_mem_eq(out, 1, zeros, 1))
1641             goto err;
1642         /* Pad up to 256. */
1643         if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1644                 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1645                                 reference, bytes)
1646                 || !TEST_mem_eq(out, sizseof(out) - bytes,
1647                                 zeros, sizeof(out) - bytes))
1648             goto err;
1649     }
1650
1651     st = 1;
1652  err:
1653     BN_free(n);
1654     return st;
1655 #else
1656     return ctx != NULL;
1657 #endif
1658 }
1659
1660 static int test_dec2bn(void)
1661 {
1662     BIGNUM *bn = NULL;
1663     int st = 0;
1664
1665     if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1666             || !TEST_BN_eq_word(bn, 0)
1667             || !TEST_BN_eq_zero(bn)
1668             || !TEST_BN_le_zero(bn)
1669             || !TEST_BN_ge_zero(bn)
1670             || !TEST_BN_even(bn))
1671         goto err;
1672     BN_free(bn);
1673     bn = NULL;
1674
1675     if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1676             || !TEST_BN_eq_word(bn, 256)
1677             || !TEST_BN_ge_zero(bn)
1678             || !TEST_BN_gt_zero(bn)
1679             || !TEST_BN_ne_zero(bn)
1680             || !TEST_BN_even(bn))
1681         goto err;
1682     BN_free(bn);
1683     bn = NULL;
1684
1685     if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1686             || !TEST_BN_abs_eq_word(bn, 42)
1687             || !TEST_BN_lt_zero(bn)
1688             || !TEST_BN_le_zero(bn)
1689             || !TEST_BN_ne_zero(bn)
1690             || !TEST_BN_even(bn))
1691         goto err;
1692     BN_free(bn);
1693     bn = NULL;
1694
1695     if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1696             || !TEST_BN_eq_word(bn, 1)
1697             || !TEST_BN_ne_zero(bn)
1698             || !TEST_BN_gt_zero(bn)
1699             || !TEST_BN_ge_zero(bn)
1700             || !TEST_BN_eq_one(bn)
1701             || !TEST_BN_odd(bn))
1702         goto err;
1703     BN_free(bn);
1704     bn = NULL;
1705
1706     if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1707             || !TEST_BN_eq_zero(bn)
1708             || !TEST_BN_ge_zero(bn)
1709             || !TEST_BN_le_zero(bn)
1710             || !TEST_BN_even(bn))
1711         goto err;
1712     BN_free(bn);
1713     bn = NULL;
1714
1715     if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1716             || !TEST_BN_abs_eq_word(bn, 42)
1717             || !TEST_BN_ge_zero(bn)
1718             || !TEST_BN_gt_zero(bn)
1719             || !TEST_BN_ne_zero(bn)
1720             || !TEST_BN_even(bn))
1721         goto err;
1722
1723     st = 1;
1724  err:
1725     BN_free(bn);
1726     return st;
1727 }
1728
1729 static int test_hex2bn(void)
1730 {
1731     BIGNUM *bn = NULL;
1732     int st = 0;
1733
1734     if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1735             || !TEST_BN_eq_zero(bn)
1736             || !TEST_BN_ge_zero(bn)
1737             || !TEST_BN_even(bn))
1738         goto err;
1739     BN_free(bn);
1740     bn = NULL;
1741
1742     if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1743             || !TEST_BN_eq_word(bn, 0x256)
1744             || !TEST_BN_ge_zero(bn)
1745             || !TEST_BN_gt_zero(bn)
1746             || !TEST_BN_ne_zero(bn)
1747             || !TEST_BN_even(bn))
1748         goto err;
1749     BN_free(bn);
1750     bn = NULL;
1751
1752     if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1753             || !TEST_BN_abs_eq_word(bn, 0x42)
1754             || !TEST_BN_lt_zero(bn)
1755             || !TEST_BN_le_zero(bn)
1756             || !TEST_BN_ne_zero(bn)
1757             || !TEST_BN_even(bn))
1758         goto err;
1759     BN_free(bn);
1760     bn = NULL;
1761
1762     if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1763             || !TEST_BN_eq_word(bn, 0xCB)
1764             || !TEST_BN_ge_zero(bn)
1765             || !TEST_BN_gt_zero(bn)
1766             || !TEST_BN_ne_zero(bn)
1767             || !TEST_BN_odd(bn))
1768         goto err;
1769     BN_free(bn);
1770     bn = NULL;
1771
1772     if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1773             || !TEST_BN_eq_zero(bn)
1774             || !TEST_BN_ge_zero(bn)
1775             || !TEST_BN_le_zero(bn)
1776             || !TEST_BN_even(bn))
1777         goto err;
1778     BN_free(bn);
1779     bn = NULL;
1780
1781     if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1782             || !TEST_BN_eq_word(bn, 0xabc)
1783             || !TEST_BN_ge_zero(bn)
1784             || !TEST_BN_gt_zero(bn)
1785             || !TEST_BN_ne_zero(bn)
1786             || !TEST_BN_even(bn))
1787         goto err;
1788     st = 1;
1789
1790  err:
1791     BN_free(bn);
1792     return st;
1793 }
1794
1795 static int test_asc2bn(void)
1796 {
1797     BIGNUM *bn = NULL;
1798     int st = 0;
1799
1800     if (!TEST_ptr(bn = BN_new()))
1801         goto err;
1802
1803     if (!TEST_true(BN_asc2bn(&bn, "0"))
1804             || !TEST_BN_eq_zero(bn)
1805             || !TEST_BN_ge_zero(bn))
1806         goto err;
1807
1808     if (!TEST_true(BN_asc2bn(&bn, "256"))
1809             || !TEST_BN_eq_word(bn, 256)
1810             || !TEST_BN_ge_zero(bn))
1811         goto err;
1812
1813     if (!TEST_true(BN_asc2bn(&bn, "-42"))
1814             || !TEST_BN_abs_eq_word(bn, 42)
1815             || !TEST_BN_lt_zero(bn))
1816         goto err;
1817
1818     if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1819             || !TEST_BN_eq_word(bn, 0x1234)
1820             || !TEST_BN_ge_zero(bn))
1821         goto err;
1822
1823     if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1824             || !TEST_BN_eq_word(bn, 0x1234)
1825             || !TEST_BN_ge_zero(bn))
1826         goto err;
1827
1828     if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1829             || !TEST_BN_abs_eq_word(bn, 0xabcd)
1830             || !TEST_BN_lt_zero(bn))
1831         goto err;
1832
1833     if (!TEST_true(BN_asc2bn(&bn, "-0"))
1834             || !TEST_BN_eq_zero(bn)
1835             || !TEST_BN_ge_zero(bn))
1836         goto err;
1837
1838     if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1839             || !TEST_BN_eq_word(bn, 123)
1840             || !TEST_BN_ge_zero(bn))
1841         goto err;
1842
1843     st = 1;
1844  err:
1845     BN_free(bn);
1846     return st;
1847 }
1848
1849 static const MPITEST kMPITests[] = {
1850     {"0", "\x00\x00\x00\x00", 4},
1851     {"1", "\x00\x00\x00\x01\x01", 5},
1852     {"-1", "\x00\x00\x00\x01\x81", 5},
1853     {"128", "\x00\x00\x00\x02\x00\x80", 6},
1854     {"256", "\x00\x00\x00\x02\x01\x00", 6},
1855     {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1856 };
1857
1858 static int test_mpi(int i)
1859 {
1860     uint8_t scratch[8];
1861     const MPITEST *test = &kMPITests[i];
1862     size_t mpi_len, mpi_len2;
1863     BIGNUM *bn = NULL;
1864     BIGNUM *bn2 = NULL;
1865     int st = 0;
1866
1867     if (!TEST_ptr(bn = BN_new())
1868             || !TEST_true(BN_asc2bn(&bn, test->base10)))
1869         goto err;
1870     mpi_len = BN_bn2mpi(bn, NULL);
1871     if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1872         goto err;
1873
1874     if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1875             || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1876         goto err;
1877
1878     if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1879         goto err;
1880
1881     if (!TEST_BN_eq(bn, bn2)) {
1882         BN_free(bn2);
1883         goto err;
1884     }
1885     BN_free(bn2);
1886
1887     st = 1;
1888  err:
1889     BN_free(bn);
1890     return st;
1891 }
1892
1893 static int test_rand(void)
1894 {
1895     BIGNUM *bn = NULL;
1896     int st = 0;
1897
1898     if (!TEST_ptr(bn = BN_new()))
1899         return 0;
1900
1901     /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
1902     if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
1903             || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
1904             || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
1905             || !TEST_BN_eq_one(bn)
1906             || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
1907             || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
1908             || !TEST_BN_eq_one(bn)
1909             || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
1910             || !TEST_BN_eq_word(bn, 3))
1911         goto err;
1912
1913     st = 1;
1914  err:
1915     BN_free(bn);
1916     return st;
1917 }
1918
1919 static int test_negzero(void)
1920 {
1921     BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
1922     BIGNUM *numerator = NULL, *denominator = NULL;
1923     int consttime, st = 0;
1924
1925     if (!TEST_ptr(a = BN_new())
1926             || !TEST_ptr(b = BN_new())
1927             || !TEST_ptr(c = BN_new())
1928             || !TEST_ptr(d = BN_new()))
1929         goto err;
1930
1931     /* Test that BN_mul never gives negative zero. */
1932     if (!TEST_true(BN_set_word(a, 1)))
1933         goto err;
1934     BN_set_negative(a, 1);
1935     BN_zero(b);
1936     if (!TEST_true(BN_mul(c, a, b, ctx)))
1937         goto err;
1938     if (!TEST_BN_eq_zero(c)
1939             || !TEST_BN_ge_zero(c))
1940         goto err;
1941
1942     for (consttime = 0; consttime < 2; consttime++) {
1943         if (!TEST_ptr(numerator = BN_new())
1944                 || !TEST_ptr(denominator = BN_new()))
1945             goto err;
1946         if (consttime) {
1947             BN_set_flags(numerator, BN_FLG_CONSTTIME);
1948             BN_set_flags(denominator, BN_FLG_CONSTTIME);
1949         }
1950         /* Test that BN_div never gives negative zero in the quotient. */
1951         if (!TEST_true(BN_set_word(numerator, 1))
1952                 || !TEST_true(BN_set_word(denominator, 2)))
1953             goto err;
1954         BN_set_negative(numerator, 1);
1955         if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
1956                 || !TEST_BN_eq_zero(a)
1957                 || !TEST_BN_ge_zero(a))
1958             goto err;
1959
1960         /* Test that BN_div never gives negative zero in the remainder. */
1961         if (!TEST_true(BN_set_word(denominator, 1))
1962                 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
1963                 || !TEST_BN_eq_zero(b)
1964                 || !TEST_BN_ge_zero(b))
1965             goto err;
1966         BN_free(numerator);
1967         BN_free(denominator);
1968         numerator = denominator = NULL;
1969     }
1970
1971     /* Test that BN_set_negative will not produce a negative zero. */
1972     BN_zero(a);
1973     BN_set_negative(a, 1);
1974     if (BN_is_negative(a))
1975         goto err;
1976     st = 1;
1977
1978  err:
1979     BN_free(a);
1980     BN_free(b);
1981     BN_free(c);
1982     BN_free(d);
1983     BN_free(numerator);
1984     BN_free(denominator);
1985     return st;
1986 }
1987
1988 static int test_badmod(void)
1989 {
1990     BIGNUM *a = NULL, *b = NULL, *zero = NULL;
1991     BN_MONT_CTX *mont = NULL;
1992     int st = 0;
1993
1994     if (!TEST_ptr(a = BN_new())
1995             || !TEST_ptr(b = BN_new())
1996             || !TEST_ptr(zero = BN_new())
1997             || !TEST_ptr(mont = BN_MONT_CTX_new()))
1998         goto err;
1999     BN_zero(zero);
2000
2001     if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
2002         goto err;
2003     ERR_clear_error();
2004
2005     if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
2006         goto err;
2007     ERR_clear_error();
2008
2009     if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
2010         goto err;
2011     ERR_clear_error();
2012
2013     if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2014                                     zero, ctx, NULL)))
2015         goto err;
2016     ERR_clear_error();
2017
2018     if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2019                                               zero, ctx, NULL)))
2020         goto err;
2021     ERR_clear_error();
2022
2023     if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
2024         goto err;
2025     ERR_clear_error();
2026
2027     /* Some operations also may not be used with an even modulus. */
2028     if (!TEST_true(BN_set_word(b, 16)))
2029         goto err;
2030
2031     if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
2032         goto err;
2033     ERR_clear_error();
2034
2035     if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2036                                     b, ctx, NULL)))
2037         goto err;
2038     ERR_clear_error();
2039
2040     if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2041                                               b, ctx, NULL)))
2042         goto err;
2043     ERR_clear_error();
2044
2045     st = 1;
2046  err:
2047     BN_free(a);
2048     BN_free(b);
2049     BN_free(zero);
2050     BN_MONT_CTX_free(mont);
2051     return st;
2052 }
2053
2054 static int test_expmodzero(void)
2055 {
2056     BIGNUM *a = NULL, *r = NULL, *zero = NULL;
2057     int st = 0;
2058
2059     if (!TEST_ptr(zero = BN_new())
2060             || !TEST_ptr(a = BN_new())
2061             || !TEST_ptr(r = BN_new()))
2062         goto err;
2063     BN_zero(zero);
2064
2065     if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
2066             || !TEST_BN_eq_zero(r)
2067             || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
2068                                           NULL, NULL))
2069             || !TEST_BN_eq_zero(r)
2070             || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
2071                                                     BN_value_one(),
2072                                                     NULL, NULL))
2073             || !TEST_BN_eq_zero(r)
2074             || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
2075                                                BN_value_one(), NULL, NULL))
2076             || !TEST_BN_eq_zero(r))
2077         goto err;
2078
2079     st = 1;
2080  err:
2081     BN_free(zero);
2082     BN_free(a);
2083     BN_free(r);
2084     return st;
2085 }
2086
2087 static int test_expmodone(void)
2088 {
2089     int ret = 0, i;
2090     BIGNUM *r = BN_new();
2091     BIGNUM *a = BN_new();
2092     BIGNUM *p = BN_new();
2093     BIGNUM *m = BN_new();
2094
2095     if (!TEST_ptr(r)
2096             || !TEST_ptr(a)
2097             || !TEST_ptr(p)
2098             || !TEST_ptr(p)
2099             || !TEST_ptr(m)
2100             || !TEST_true(BN_set_word(a, 1))
2101             || !TEST_true(BN_set_word(p, 0))
2102             || !TEST_true(BN_set_word(m, 1)))
2103         goto err;
2104
2105     /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
2106     for (i = 0; i < 2; i++) {
2107         if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
2108                 || !TEST_BN_eq_zero(r)
2109                 || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
2110                 || !TEST_BN_eq_zero(r)
2111                 || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
2112                 || !TEST_BN_eq_zero(r)
2113                 || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
2114                 || !TEST_BN_eq_zero(r)
2115                 || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
2116                 || !TEST_BN_eq_zero(r)
2117                 || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
2118                 || !TEST_BN_eq_zero(r))
2119             goto err;
2120         /* Repeat for r = 1 ^ 0 mod -1 */
2121         if (i == 0)
2122             BN_set_negative(m, 1);
2123     }
2124
2125     ret = 1;
2126  err:
2127     BN_free(r);
2128     BN_free(a);
2129     BN_free(p);
2130     BN_free(m);
2131     return ret;
2132 }
2133
2134 static int test_smallprime(void)
2135 {
2136     static const int kBits = 10;
2137     BIGNUM *r;
2138     int st = 0;
2139
2140     if (!TEST_ptr(r = BN_new())
2141             || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
2142                                                NULL, NULL, NULL))
2143             || !TEST_int_eq(BN_num_bits(r), kBits))
2144         goto err;
2145
2146     st = 1;
2147  err:
2148     BN_free(r);
2149     return st;
2150 }
2151
2152 static int primes[] = { 2, 3, 5, 7, 17863 };
2153
2154 static int test_is_prime(int i)
2155 {
2156     int ret = 0;
2157     BIGNUM *r = NULL;
2158     int trial;
2159
2160     if (!TEST_ptr(r = BN_new()))
2161         goto err;
2162
2163     for (trial = 0; trial <= 1; ++trial) {
2164         if (!TEST_true(BN_set_word(r, primes[i]))
2165                 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL),
2166                                 1))
2167             goto err;
2168     }
2169
2170     ret = 1;
2171  err:
2172     BN_free(r);
2173     return ret;
2174 }
2175
2176 static int not_primes[] = { -1, 0, 1, 4 };
2177
2178 static int test_not_prime(int i)
2179 {
2180     int ret = 0;
2181     BIGNUM *r = NULL;
2182     int trial;
2183
2184     if (!TEST_ptr(r = BN_new()))
2185         goto err;
2186
2187     for (trial = 0; trial <= 1; ++trial) {
2188         if (!TEST_true(BN_set_word(r, not_primes[i]))
2189                 || !TEST_false(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL)))
2190             goto err;
2191     }
2192
2193     ret = 1;
2194  err:
2195     BN_free(r);
2196     return ret;
2197 }
2198
2199 static int test_ctx_set_ct_flag(BN_CTX *c)
2200 {
2201     int st = 0;
2202     size_t i;
2203     BIGNUM *b[15];
2204
2205     BN_CTX_start(c);
2206     for (i = 0; i < OSSL_NELEM(b); i++) {
2207         if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2208             goto err;
2209         if (i % 2 == 1)
2210             BN_set_flags(b[i], BN_FLG_CONSTTIME);
2211     }
2212
2213     st = 1;
2214  err:
2215     BN_CTX_end(c);
2216     return st;
2217 }
2218
2219 static int test_ctx_check_ct_flag(BN_CTX *c)
2220 {
2221     int st = 0;
2222     size_t i;
2223     BIGNUM *b[30];
2224
2225     BN_CTX_start(c);
2226     for (i = 0; i < OSSL_NELEM(b); i++) {
2227         if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2228             goto err;
2229         if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
2230             goto err;
2231     }
2232
2233     st = 1;
2234  err:
2235     BN_CTX_end(c);
2236     return st;
2237 }
2238
2239 static int test_ctx_consttime_flag(void)
2240 {
2241     /*-
2242      * The constant-time flag should not "leak" among BN_CTX frames:
2243      *
2244      * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
2245      *   sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
2246      *   from the frame before ending it.
2247      * - test_ctx_check_ct_flag() then starts a new frame and gets a
2248      *   number of BIGNUMs from it. In absence of leaks, none of the
2249      *   BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
2250      *
2251      * In actual BN_CTX usage inside libcrypto the leak could happen at
2252      * any depth level in the BN_CTX stack, with varying results
2253      * depending on the patterns of sibling trees of nested function
2254      * calls sharing the same BN_CTX object, and the effect of
2255      * unintended BN_FLG_CONSTTIME on the called BN_* functions.
2256      *
2257      * This simple unit test abstracts away this complexity and verifies
2258      * that the leak does not happen between two sibling functions
2259      * sharing the same BN_CTX object at the same level of nesting.
2260      *
2261      */
2262     BN_CTX *nctx = NULL;
2263     BN_CTX *sctx = NULL;
2264     size_t i = 0;
2265     int st = 0;
2266
2267     if (!TEST_ptr(nctx = BN_CTX_new())
2268             || !TEST_ptr(sctx = BN_CTX_secure_new()))
2269         goto err;
2270
2271     for (i = 0; i < 2; i++) {
2272         BN_CTX *c = i == 0 ? nctx : sctx;
2273         if (!TEST_true(test_ctx_set_ct_flag(c))
2274                 || !TEST_true(test_ctx_check_ct_flag(c)))
2275             goto err;
2276     }
2277
2278     st = 1;
2279  err:
2280     BN_CTX_free(nctx);
2281     BN_CTX_free(sctx);
2282     return st;
2283 }
2284
2285 static int file_test_run(STANZA *s)
2286 {
2287     static const FILETEST filetests[] = {
2288         {"Sum", file_sum},
2289         {"LShift1", file_lshift1},
2290         {"LShift", file_lshift},
2291         {"RShift", file_rshift},
2292         {"Square", file_square},
2293         {"Product", file_product},
2294         {"Quotient", file_quotient},
2295         {"ModMul", file_modmul},
2296         {"ModExp", file_modexp},
2297         {"Exp", file_exp},
2298         {"ModSqrt", file_modsqrt},
2299     };
2300     int numtests = OSSL_NELEM(filetests);
2301     const FILETEST *tp = filetests;
2302
2303     for ( ; --numtests >= 0; tp++) {
2304         if (findattr(s, tp->name) != NULL) {
2305             if (!tp->func(s)) {
2306                 TEST_info("%s:%d: Failed %s test",
2307                           s->test_file, s->start, tp->name);
2308                 return 0;
2309             }
2310             return 1;
2311         }
2312     }
2313     TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2314     return 0;
2315 }
2316
2317 static int run_file_tests(int i)
2318 {
2319     STANZA *s = NULL;
2320     char *testfile = test_get_argument(i);
2321     int c;
2322
2323     if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2324         return 0;
2325     if (!test_start_file(s, testfile)) {
2326         OPENSSL_free(s);
2327         return 0;
2328     }
2329
2330     /* Read test file. */
2331     while (!BIO_eof(s->fp) && test_readstanza(s)) {
2332         if (s->numpairs == 0)
2333             continue;
2334         if (!file_test_run(s))
2335             s->errors++;
2336         s->numtests++;
2337         test_clearstanza(s);
2338     }
2339     test_end_file(s);
2340     c = s->errors;
2341     OPENSSL_free(s);
2342
2343     return c == 0;
2344 }
2345
2346 const OPTIONS *test_get_options(void)
2347 {
2348     enum { OPT_TEST_ENUM };
2349     static const OPTIONS test_options[] = {
2350         OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
2351         { OPT_HELP_STR, 1, '-',
2352           "file\tFile to run tests on. Normal tests are not run\n" },
2353         { NULL }
2354     };
2355     return test_options;
2356 }
2357
2358 int setup_tests(void)
2359 {
2360     int n = test_get_argument_count();
2361
2362     if (!TEST_ptr(ctx = BN_CTX_new()))
2363         return 0;
2364
2365     if (n == 0) {
2366         ADD_TEST(test_sub);
2367         ADD_TEST(test_div_recip);
2368         ADD_TEST(test_mod);
2369         ADD_TEST(test_modexp_mont5);
2370         ADD_TEST(test_kronecker);
2371         ADD_TEST(test_rand);
2372         ADD_TEST(test_bn2padded);
2373         ADD_TEST(test_dec2bn);
2374         ADD_TEST(test_hex2bn);
2375         ADD_TEST(test_asc2bn);
2376         ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2377         ADD_TEST(test_negzero);
2378         ADD_TEST(test_badmod);
2379         ADD_TEST(test_expmodzero);
2380         ADD_TEST(test_expmodone);
2381         ADD_TEST(test_smallprime);
2382         ADD_TEST(test_swap);
2383         ADD_TEST(test_ctx_consttime_flag);
2384 #ifndef OPENSSL_NO_EC2M
2385         ADD_TEST(test_gf2m_add);
2386         ADD_TEST(test_gf2m_mod);
2387         ADD_TEST(test_gf2m_mul);
2388         ADD_TEST(test_gf2m_sqr);
2389         ADD_TEST(test_gf2m_modinv);
2390         ADD_TEST(test_gf2m_moddiv);
2391         ADD_TEST(test_gf2m_modexp);
2392         ADD_TEST(test_gf2m_modsqrt);
2393         ADD_TEST(test_gf2m_modsolvequad);
2394 #endif
2395         ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
2396         ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
2397     } else {
2398         ADD_ALL_TESTS(run_file_tests, n);
2399     }
2400     return 1;
2401 }
2402
2403 void cleanup_tests(void)
2404 {
2405     BN_CTX_free(ctx);
2406 }