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