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