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