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