1 /*
2 * Copyright 1995-2019 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 */
findattr(STANZA * s,const char * key)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 */
parse_bigBN(BIGNUM ** out,const char * bn_strings[])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 */
parseBN(BIGNUM ** out,const char * in)85 static int parseBN(BIGNUM **out, const char *in)
86 {
87 *out = NULL;
88 return BN_hex2bn(out, in);
89 }
90
parsedecBN(BIGNUM ** out,const char * in)91 static int parsedecBN(BIGNUM **out, const char *in)
92 {
93 *out = NULL;
94 return BN_dec2bn(out, in);
95 }
96
getBN(STANZA * s,const char * attribute)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
getint(STANZA * s,int * out,const char * attribute)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
equalBN(const char * op,const BIGNUM * expected,const BIGNUM * actual)131 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
132 {
133 if (BN_cmp(expected, actual) == 0)
134 return 1;
135
136 TEST_error("unexpected %s value", op);
137 TEST_BN_eq(expected, actual);
138 return 0;
139 }
140
141 /*
142 * Return a "random" flag for if a BN should be negated.
143 */
rand_neg(void)144 static int rand_neg(void)
145 {
146 static unsigned int neg = 0;
147 static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
148
149 return sign[(neg++) % 8];
150 }
151
test_swap(void)152 static int test_swap(void)
153 {
154 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
155 int top, cond, st = 0;
156
157 if (!TEST_ptr(a = BN_new())
158 || !TEST_ptr(b = BN_new())
159 || !TEST_ptr(c = BN_new())
160 || !TEST_ptr(d = BN_new()))
161 goto err;
162
163 if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))
164 && TEST_true(BN_bntest_rand(b, 1024, 1, 0))
165 && TEST_ptr(BN_copy(c, a))
166 && TEST_ptr(BN_copy(d, b))))
167 goto err;
168 top = BN_num_bits(a) / BN_BITS2;
169
170 /* regular swap */
171 BN_swap(a, b);
172 if (!equalBN("swap", a, d)
173 || !equalBN("swap", b, c))
174 goto err;
175
176 /* conditional swap: true */
177 cond = 1;
178 BN_consttime_swap(cond, a, b, top);
179 if (!equalBN("cswap true", a, c)
180 || !equalBN("cswap true", b, d))
181 goto err;
182
183 /* conditional swap: false */
184 cond = 0;
185 BN_consttime_swap(cond, a, b, top);
186 if (!equalBN("cswap false", a, c)
187 || !equalBN("cswap false", b, d))
188 goto err;
189
190 /* same tests but checking flag swap */
191 BN_set_flags(a, BN_FLG_CONSTTIME);
192
193 BN_swap(a, b);
194 if (!equalBN("swap, flags", a, d)
195 || !equalBN("swap, flags", b, c)
196 || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
197 || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
198 goto err;
199
200 cond = 1;
201 BN_consttime_swap(cond, a, b, top);
202 if (!equalBN("cswap true, flags", a, c)
203 || !equalBN("cswap true, flags", b, d)
204 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
205 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
206 goto err;
207
208 cond = 0;
209 BN_consttime_swap(cond, a, b, top);
210 if (!equalBN("cswap false, flags", a, c)
211 || !equalBN("cswap false, flags", b, d)
212 || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
213 || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
214 goto err;
215
216 st = 1;
217 err:
218 BN_free(a);
219 BN_free(b);
220 BN_free(c);
221 BN_free(d);
222 return st;
223 }
224
test_sub(void)225 static int test_sub(void)
226 {
227 BIGNUM *a = NULL, *b = NULL, *c = NULL;
228 int i, st = 0;
229
230 if (!TEST_ptr(a = BN_new())
231 || !TEST_ptr(b = BN_new())
232 || !TEST_ptr(c = BN_new()))
233 goto err;
234
235 for (i = 0; i < NUM0 + NUM1; i++) {
236 if (i < NUM1) {
237 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0)))
238 && TEST_ptr(BN_copy(b, a))
239 && TEST_int_ne(BN_set_bit(a, i), 0)
240 && TEST_true(BN_add_word(b, i)))
241 goto err;
242 } else {
243 if (!TEST_true(BN_bntest_rand(b, 400 + i - NUM1, 0, 0)))
244 goto err;
245 BN_set_negative(a, rand_neg());
246 BN_set_negative(b, rand_neg());
247 }
248 if (!(TEST_true(BN_sub(c, a, b))
249 && TEST_true(BN_add(c, c, b))
250 && TEST_true(BN_sub(c, c, a))
251 && 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
test_div_recip(void)262 static int test_div_recip(void)
263 {
264 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
265 BN_RECP_CTX *recp = NULL;
266 int st = 0, i;
267
268 if (!TEST_ptr(a = BN_new())
269 || !TEST_ptr(b = BN_new())
270 || !TEST_ptr(c = BN_new())
271 || !TEST_ptr(d = BN_new())
272 || !TEST_ptr(e = BN_new())
273 || !TEST_ptr(recp = BN_RECP_CTX_new()))
274 goto err;
275
276 for (i = 0; i < NUM0 + NUM1; i++) {
277 if (i < NUM1) {
278 if (!(TEST_true(BN_bntest_rand(a, 400, 0, 0))
279 && TEST_ptr(BN_copy(b, a))
280 && TEST_true(BN_lshift(a, a, i))
281 && TEST_true(BN_add_word(a, i))))
282 goto err;
283 } else {
284 if (!(TEST_true(BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0))))
285 goto err;
286 }
287 BN_set_negative(a, rand_neg());
288 BN_set_negative(b, rand_neg());
289 if (!(TEST_true(BN_RECP_CTX_set(recp, b, ctx))
290 && TEST_true(BN_div_recp(d, c, a, recp, ctx))
291 && TEST_true(BN_mul(e, d, b, ctx))
292 && TEST_true(BN_add(d, e, c))
293 && TEST_true(BN_sub(d, d, a))
294 && TEST_BN_eq_zero(d)))
295 goto err;
296 }
297 st = 1;
298 err:
299 BN_free(a);
300 BN_free(b);
301 BN_free(c);
302 BN_free(d);
303 BN_free(e);
304 BN_RECP_CTX_free(recp);
305 return st;
306 }
307
test_mod(void)308 static int test_mod(void)
309 {
310 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
311 int st = 0, i;
312
313 if (!TEST_ptr(a = BN_new())
314 || !TEST_ptr(b = BN_new())
315 || !TEST_ptr(c = BN_new())
316 || !TEST_ptr(d = BN_new())
317 || !TEST_ptr(e = BN_new()))
318 goto err;
319
320 if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
321 goto err;
322 for (i = 0; i < NUM0; i++) {
323 if (!(TEST_true(BN_bntest_rand(b, 450 + i * 10, 0, 0))))
324 goto err;
325 BN_set_negative(a, rand_neg());
326 BN_set_negative(b, rand_neg());
327 if (!(TEST_true(BN_mod(c, a, b, ctx))
328 && TEST_true(BN_div(d, e, a, b, ctx))
329 && TEST_true(BN_sub(e, e, c))
330 && TEST_BN_eq_zero(e)))
331 goto err;
332 }
333 st = 1;
334 err:
335 BN_free(a);
336 BN_free(b);
337 BN_free(c);
338 BN_free(d);
339 BN_free(e);
340 return st;
341 }
342
343 static const char *bn1strings[] = {
344 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
345 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
346 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
347 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
348 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
349 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
350 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
351 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
352 "0000000000000000000000000000000000000000000000000000000000000000",
353 "0000000000000000000000000000000000000000000000000000000000000000",
354 "0000000000000000000000000000000000000000000000000000000000000000",
355 "0000000000000000000000000000000000000000000000000000000000000000",
356 "0000000000000000000000000000000000000000000000000000000000000000",
357 "0000000000000000000000000000000000000000000000000000000000000000",
358 "0000000000000000000000000000000000000000000000000000000000000000",
359 "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
360 NULL
361 };
362
363 static const char *bn2strings[] = {
364 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
365 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
366 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
367 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
368 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
369 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
370 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
371 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
372 "0000000000000000000000000000000000000000000000000000000000000000",
373 "0000000000000000000000000000000000000000000000000000000000000000",
374 "0000000000000000000000000000000000000000000000000000000000000000",
375 "0000000000000000000000000000000000000000000000000000000000000000",
376 "0000000000000000000000000000000000000000000000000000000000000000",
377 "0000000000000000000000000000000000000000000000000000000000000000",
378 "0000000000000000000000000000000000000000000000000000000000000000",
379 "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
380 NULL
381 };
382
383 /*
384 * Test constant-time modular exponentiation with 1024-bit inputs, which on
385 * x86_64 cause a different code branch to be taken.
386 */
test_modexp_mont5(void)387 static int test_modexp_mont5(void)
388 {
389 BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
390 BIGNUM *b = NULL, *n = NULL, *c = NULL;
391 BN_MONT_CTX *mont = NULL;
392 int st = 0;
393
394 if (!TEST_ptr(a = BN_new())
395 || !TEST_ptr(p = BN_new())
396 || !TEST_ptr(m = BN_new())
397 || !TEST_ptr(d = BN_new())
398 || !TEST_ptr(e = BN_new())
399 || !TEST_ptr(b = BN_new())
400 || !TEST_ptr(n = BN_new())
401 || !TEST_ptr(c = BN_new())
402 || !TEST_ptr(mont = BN_MONT_CTX_new()))
403 goto err;
404
405 /* must be odd for montgomery */
406 if (!(TEST_true(BN_bntest_rand(m, 1024, 0, 1))
407 /* Zero exponent */
408 && TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
409 goto err;
410 BN_zero(p);
411
412 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
413 goto err;
414 if (!TEST_BN_eq_one(d))
415 goto err;
416
417 /* Regression test for carry bug in mulx4x_mont */
418 if (!(TEST_true(BN_hex2bn(&a,
419 "7878787878787878787878787878787878787878787878787878787878787878"
420 "7878787878787878787878787878787878787878787878787878787878787878"
421 "7878787878787878787878787878787878787878787878787878787878787878"
422 "7878787878787878787878787878787878787878787878787878787878787878"))
423 && TEST_true(BN_hex2bn(&b,
424 "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
425 "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
426 "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
427 "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81"))
428 && TEST_true(BN_hex2bn(&n,
429 "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
430 "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
431 "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
432 "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF"))))
433 goto err;
434
435 if (!(TEST_true(BN_MONT_CTX_set(mont, n, ctx))
436 && TEST_true(BN_mod_mul_montgomery(c, a, b, mont, ctx))
437 && TEST_true(BN_mod_mul_montgomery(d, b, a, mont, ctx))
438 && TEST_BN_eq(c, d)))
439 goto err;
440
441 /* Regression test for carry bug in sqr[x]8x_mont */
442 if (!(TEST_true(parse_bigBN(&n, bn1strings))
443 && TEST_true(parse_bigBN(&a, bn2strings))))
444 goto err;
445 BN_free(b);
446 if (!(TEST_ptr(b = BN_dup(a))
447 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
448 && TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
449 && TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
450 && TEST_BN_eq(c, d)))
451 goto err;
452
453 /* Regression test for carry bug in bn_sqrx8x_internal */
454 {
455 static const char *ahex[] = {
456 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
457 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
458 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
459 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
460 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
461 "9544D954000000006C0000000000000000000000000000000000000000000000",
462 "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
463 "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
464 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
465 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
466 "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
467 "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
468 NULL
469 };
470 static const char *nhex[] = {
471 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
472 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
473 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
474 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
475 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
476 "00000010000000006C0000000000000000000000000000000000000000000000",
477 "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
478 "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
479 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
480 "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
481 "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
482 "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
483 NULL
484 };
485
486 if (!(TEST_true(parse_bigBN(&a, ahex))
487 && TEST_true(parse_bigBN(&n, nhex))))
488 goto err;
489 }
490 BN_free(b);
491 if (!(TEST_ptr(b = BN_dup(a))
492 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))))
493 goto err;
494
495 if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
496 || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
497 || !TEST_BN_eq(c, d))
498 goto err;
499
500 /* Regression test for bug in BN_from_montgomery_word */
501 if (!(TEST_true(BN_hex2bn(&a,
502 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
503 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
504 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
505 && TEST_true(BN_hex2bn(&n,
506 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
507 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
508 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
509 && TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx))))
510 goto err;
511
512 /* Regression test for bug in rsaz_1024_mul_avx2 */
513 if (!(TEST_true(BN_hex2bn(&a,
514 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
515 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
516 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
517 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
518 && TEST_true(BN_hex2bn(&b,
519 "2020202020202020202020202020202020202020202020202020202020202020"
520 "2020202020202020202020202020202020202020202020202020202020202020"
521 "20202020202020FF202020202020202020202020202020202020202020202020"
522 "2020202020202020202020202020202020202020202020202020202020202020"))
523 && TEST_true(BN_hex2bn(&n,
524 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
525 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
526 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
527 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF"))
528 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
529 && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))
530 && TEST_true(BN_mod_exp_mont(d, a, b, n, ctx, mont))
531 && TEST_BN_eq(c, d)))
532 goto err;
533
534 /*
535 * rsaz_1024_mul_avx2 expects fully-reduced inputs.
536 * BN_mod_exp_mont_consttime should reduce the input first.
537 */
538 if (!(TEST_true(BN_hex2bn(&a,
539 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
540 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
541 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
542 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
543 && TEST_true(BN_hex2bn(&b,
544 "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
545 "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
546 "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
547 "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E"))
548 && TEST_true(BN_hex2bn(&n,
549 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
550 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
551 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
552 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
553 && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
554 && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))))
555 goto err;
556 BN_zero(d);
557 if (!TEST_BN_eq(c, d))
558 goto err;
559
560 /*
561 * Regression test for overflow bug in bn_sqr_comba4/8 for
562 * mips-linux-gnu and mipsel-linux-gnu 32bit targets.
563 */
564 {
565 static const char *ehex[] = {
566 "95564994a96c45954227b845a1e99cb939d5a1da99ee91acc962396ae999a9ee",
567 "38603790448f2f7694c242a875f0cad0aae658eba085f312d2febbbd128dd2b5",
568 "8f7d1149f03724215d704344d0d62c587ae3c5939cba4b9b5f3dc5e8e911ef9a",
569 "5ce1a5a749a4989d0d8368f6e1f8cdf3a362a6c97fb02047ff152b480a4ad985",
570 "2d45efdf0770542992afca6a0590d52930434bba96017afbc9f99e112950a8b1",
571 "a359473ec376f329bdae6a19f503be6d4be7393c4e43468831234e27e3838680",
572 "b949390d2e416a3f9759e5349ab4c253f6f29f819a6fe4cbfd27ada34903300e",
573 "da021f62839f5878a36f1bc3085375b00fd5fa3e68d316c0fdace87a97558465",
574 NULL};
575 static const char *phex[] = {
576 "f95dc0f980fbd22e90caa5a387cc4a369f3f830d50dd321c40db8c09a7e1a241",
577 "a536e096622d3280c0c1ba849c1f4a79bf490f60006d081e8cf69960189f0d31",
578 "2cd9e17073a3fba7881b21474a13b334116cb2f5dbf3189a6de3515d0840f053",
579 "c776d3982d391b6d04d642dda5cc6d1640174c09875addb70595658f89efb439",
580 "dc6fbd55f903aadd307982d3f659207f265e1ec6271b274521b7a5e28e8fd7a5",
581 "5df089292820477802a43cf5b6b94e999e8c9944ddebb0d0e95a60f88cb7e813",
582 "ba110d20e1024774107dd02949031864923b3cb8c3f7250d6d1287b0a40db6a4",
583 "7bd5a469518eb65aa207ddc47d8c6e5fc8e0c105be8fc1d4b57b2e27540471d5",
584 NULL};
585 static const char *mhex[] = {
586 "fef15d5ce4625f1bccfbba49fc8439c72bf8202af039a2259678941b60bb4a8f",
587 "2987e965d58fd8cf86a856674d519763d0e1211cc9f8596971050d56d9b35db3",
588 "785866cfbca17cfdbed6060be3629d894f924a89fdc1efc624f80d41a22f1900",
589 "9503fcc3824ef62ccb9208430c26f2d8ceb2c63488ec4c07437aa4c96c43dd8b",
590 "9289ed00a712ff66ee195dc71f5e4ead02172b63c543d69baf495f5fd63ba7bc",
591 "c633bd309c016e37736da92129d0b053d4ab28d21ad7d8b6fab2a8bbdc8ee647",
592 "d2fbcf2cf426cf892e6f5639e0252993965dfb73ccd277407014ea784aaa280c",
593 "b7b03972bc8b0baa72360bdb44b82415b86b2f260f877791cd33ba8f2d65229b",
594 NULL};
595
596 if (!TEST_true(parse_bigBN(&e, ehex))
597 || !TEST_true(parse_bigBN(&p, phex))
598 || !TEST_true(parse_bigBN(&m, mhex))
599 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
600 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
601 || !TEST_BN_eq(a, d))
602 goto err;
603 }
604
605 /* Zero input */
606 if (!TEST_true(BN_bntest_rand(p, 1024, 0, 0)))
607 goto err;
608 BN_zero(a);
609 if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
610 || !TEST_BN_eq_zero(d))
611 goto err;
612
613 /*
614 * Craft an input whose Montgomery representation is 1, i.e., shorter
615 * than the modulus m, in order to test the const time precomputation
616 * scattering/gathering.
617 */
618 if (!(TEST_true(BN_one(a))
619 && TEST_true(BN_MONT_CTX_set(mont, m, ctx))))
620 goto err;
621 if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
622 || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
623 || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
624 || !TEST_BN_eq(a, d))
625 goto err;
626
627 /* Finally, some regular test vectors. */
628 if (!(TEST_true(BN_bntest_rand(e, 1024, 0, 0))
629 && TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
630 && TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
631 && TEST_BN_eq(a, d)))
632 goto err;
633
634 st = 1;
635
636 err:
637 BN_MONT_CTX_free(mont);
638 BN_free(a);
639 BN_free(p);
640 BN_free(m);
641 BN_free(d);
642 BN_free(e);
643 BN_free(b);
644 BN_free(n);
645 BN_free(c);
646 return st;
647 }
648
649 #ifndef OPENSSL_NO_EC2M
test_gf2m_add(void)650 static int test_gf2m_add(void)
651 {
652 BIGNUM *a = NULL, *b = NULL, *c = NULL;
653 int i, st = 0;
654
655 if (!TEST_ptr(a = BN_new())
656 || !TEST_ptr(b = BN_new())
657 || !TEST_ptr(c = BN_new()))
658 goto err;
659
660 for (i = 0; i < NUM0; i++) {
661 if (!(TEST_true(BN_rand(a, 512, 0, 0))
662 && TEST_ptr(BN_copy(b, BN_value_one()))))
663 goto err;
664 BN_set_negative(a, rand_neg());
665 BN_set_negative(b, rand_neg());
666 if (!(TEST_true(BN_GF2m_add(c, a, b))
667 /* Test that two added values have the correct parity. */
668 && TEST_false((BN_is_odd(a) && BN_is_odd(c))
669 || (!BN_is_odd(a) && !BN_is_odd(c)))))
670 goto err;
671 if (!(TEST_true(BN_GF2m_add(c, c, c))
672 /* Test that c + c = 0. */
673 && TEST_BN_eq_zero(c)))
674 goto err;
675 }
676 st = 1;
677 err:
678 BN_free(a);
679 BN_free(b);
680 BN_free(c);
681 return st;
682 }
683
test_gf2m_mod(void)684 static int test_gf2m_mod(void)
685 {
686 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
687 int i, j, st = 0;
688
689 if (!TEST_ptr(a = BN_new())
690 || !TEST_ptr(b[0] = BN_new())
691 || !TEST_ptr(b[1] = BN_new())
692 || !TEST_ptr(c = BN_new())
693 || !TEST_ptr(d = BN_new())
694 || !TEST_ptr(e = BN_new()))
695 goto err;
696
697 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
698 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
699 goto err;
700
701 for (i = 0; i < NUM0; i++) {
702 if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
703 goto err;
704 for (j = 0; j < 2; j++) {
705 if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
706 && TEST_true(BN_GF2m_add(d, a, c))
707 && TEST_true(BN_GF2m_mod(e, d, b[j]))
708 /* Test that a + (a mod p) mod p == 0. */
709 && TEST_BN_eq_zero(e)))
710 goto err;
711 }
712 }
713 st = 1;
714 err:
715 BN_free(a);
716 BN_free(b[0]);
717 BN_free(b[1]);
718 BN_free(c);
719 BN_free(d);
720 BN_free(e);
721 return st;
722 }
723
test_gf2m_mul(void)724 static int test_gf2m_mul(void)
725 {
726 BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
727 BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
728 int i, j, st = 0;
729
730 if (!TEST_ptr(a = BN_new())
731 || !TEST_ptr(b[0] = BN_new())
732 || !TEST_ptr(b[1] = BN_new())
733 || !TEST_ptr(c = BN_new())
734 || !TEST_ptr(d = BN_new())
735 || !TEST_ptr(e = BN_new())
736 || !TEST_ptr(f = BN_new())
737 || !TEST_ptr(g = BN_new())
738 || !TEST_ptr(h = BN_new()))
739 goto err;
740
741 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
742 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
743 goto err;
744
745 for (i = 0; i < NUM0; i++) {
746 if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))
747 && TEST_true(BN_bntest_rand(c, 1024, 0, 0))
748 && TEST_true(BN_bntest_rand(d, 1024, 0, 0))))
749 goto err;
750 for (j = 0; j < 2; j++) {
751 if (!(TEST_true(BN_GF2m_mod_mul(e, a, c, b[j], ctx))
752 && TEST_true(BN_GF2m_add(f, a, d))
753 && TEST_true(BN_GF2m_mod_mul(g, f, c, b[j], ctx))
754 && TEST_true(BN_GF2m_mod_mul(h, d, c, b[j], ctx))
755 && TEST_true(BN_GF2m_add(f, e, g))
756 && TEST_true(BN_GF2m_add(f, f, h))
757 /* Test that (a+d)*c = a*c + d*c. */
758 && TEST_BN_eq_zero(f)))
759 goto err;
760 }
761 }
762 st = 1;
763
764 err:
765 BN_free(a);
766 BN_free(b[0]);
767 BN_free(b[1]);
768 BN_free(c);
769 BN_free(d);
770 BN_free(e);
771 BN_free(f);
772 BN_free(g);
773 BN_free(h);
774 return st;
775 }
776
test_gf2m_sqr(void)777 static int test_gf2m_sqr(void)
778 {
779 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
780 int i, j, st = 0;
781
782 if (!TEST_ptr(a = BN_new())
783 || !TEST_ptr(b[0] = BN_new())
784 || !TEST_ptr(b[1] = BN_new())
785 || !TEST_ptr(c = BN_new())
786 || !TEST_ptr(d = BN_new()))
787 goto err;
788
789 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
790 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
791 goto err;
792
793 for (i = 0; i < NUM0; i++) {
794 if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
795 goto err;
796 for (j = 0; j < 2; j++) {
797 if (!(TEST_true(BN_GF2m_mod_sqr(c, a, b[j], ctx))
798 && TEST_true(BN_copy(d, a))
799 && TEST_true(BN_GF2m_mod_mul(d, a, d, b[j], ctx))
800 && TEST_true(BN_GF2m_add(d, c, d))
801 /* Test that a*a = a^2. */
802 && TEST_BN_eq_zero(d)))
803 goto err;
804 }
805 }
806 st = 1;
807 err:
808 BN_free(a);
809 BN_free(b[0]);
810 BN_free(b[1]);
811 BN_free(c);
812 BN_free(d);
813 return st;
814 }
815
test_gf2m_modinv(void)816 static int test_gf2m_modinv(void)
817 {
818 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
819 int i, j, st = 0;
820
821 if (!TEST_ptr(a = BN_new())
822 || !TEST_ptr(b[0] = BN_new())
823 || !TEST_ptr(b[1] = BN_new())
824 || !TEST_ptr(c = BN_new())
825 || !TEST_ptr(d = BN_new()))
826 goto err;
827
828 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
829 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
830 goto err;
831
832 for (i = 0; i < NUM0; i++) {
833 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
834 goto err;
835 for (j = 0; j < 2; j++) {
836 if (!(TEST_true(BN_GF2m_mod_inv(c, a, b[j], ctx))
837 && TEST_true(BN_GF2m_mod_mul(d, a, c, b[j], ctx))
838 /* Test that ((1/a)*a) = 1. */
839 && TEST_BN_eq_one(d)))
840 goto err;
841 }
842 }
843 st = 1;
844 err:
845 BN_free(a);
846 BN_free(b[0]);
847 BN_free(b[1]);
848 BN_free(c);
849 BN_free(d);
850 return st;
851 }
852
test_gf2m_moddiv(void)853 static int test_gf2m_moddiv(void)
854 {
855 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
856 BIGNUM *e = NULL, *f = NULL;
857 int i, j, st = 0;
858
859 if (!TEST_ptr(a = BN_new())
860 || !TEST_ptr(b[0] = BN_new())
861 || !TEST_ptr(b[1] = BN_new())
862 || !TEST_ptr(c = BN_new())
863 || !TEST_ptr(d = BN_new())
864 || !TEST_ptr(e = BN_new())
865 || !TEST_ptr(f = BN_new()))
866 goto err;
867
868 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
869 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
870 goto err;
871
872 for (i = 0; i < NUM0; i++) {
873 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
874 && TEST_true(BN_bntest_rand(c, 512, 0, 0))))
875 goto err;
876 for (j = 0; j < 2; j++) {
877 if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))
878 && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))
879 && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))
880 /* Test that ((a/c)*c)/a = 1. */
881 && TEST_BN_eq_one(f)))
882 goto err;
883 }
884 }
885 st = 1;
886 err:
887 BN_free(a);
888 BN_free(b[0]);
889 BN_free(b[1]);
890 BN_free(c);
891 BN_free(d);
892 BN_free(e);
893 BN_free(f);
894 return st;
895 }
896
test_gf2m_modexp(void)897 static int test_gf2m_modexp(void)
898 {
899 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
900 BIGNUM *e = NULL, *f = NULL;
901 int i, j, st = 0;
902
903 if (!TEST_ptr(a = BN_new())
904 || !TEST_ptr(b[0] = BN_new())
905 || !TEST_ptr(b[1] = BN_new())
906 || !TEST_ptr(c = BN_new())
907 || !TEST_ptr(d = BN_new())
908 || !TEST_ptr(e = BN_new())
909 || !TEST_ptr(f = BN_new()))
910 goto err;
911
912 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
913 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
914 goto err;
915
916 for (i = 0; i < NUM0; i++) {
917 if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
918 && TEST_true(BN_bntest_rand(c, 512, 0, 0))
919 && TEST_true(BN_bntest_rand(d, 512, 0, 0))))
920 goto err;
921 for (j = 0; j < 2; j++) {
922 if (!(TEST_true(BN_GF2m_mod_exp(e, a, c, b[j], ctx))
923 && TEST_true(BN_GF2m_mod_exp(f, a, d, b[j], ctx))
924 && TEST_true(BN_GF2m_mod_mul(e, e, f, b[j], ctx))
925 && TEST_true(BN_add(f, c, d))
926 && TEST_true(BN_GF2m_mod_exp(f, a, f, b[j], ctx))
927 && TEST_true(BN_GF2m_add(f, e, f))
928 /* Test that a^(c+d)=a^c*a^d. */
929 && TEST_BN_eq_zero(f)))
930 goto err;
931 }
932 }
933 st = 1;
934 err:
935 BN_free(a);
936 BN_free(b[0]);
937 BN_free(b[1]);
938 BN_free(c);
939 BN_free(d);
940 BN_free(e);
941 BN_free(f);
942 return st;
943 }
944
test_gf2m_modsqrt(void)945 static int test_gf2m_modsqrt(void)
946 {
947 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
948 BIGNUM *e = NULL, *f = NULL;
949 int i, j, st = 0;
950
951 if (!TEST_ptr(a = BN_new())
952 || !TEST_ptr(b[0] = BN_new())
953 || !TEST_ptr(b[1] = BN_new())
954 || !TEST_ptr(c = BN_new())
955 || !TEST_ptr(d = BN_new())
956 || !TEST_ptr(e = BN_new())
957 || !TEST_ptr(f = BN_new()))
958 goto err;
959
960 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
961 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
962 goto err;
963
964 for (i = 0; i < NUM0; i++) {
965 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
966 goto err;
967
968 for (j = 0; j < 2; j++) {
969 if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
970 && TEST_true(BN_GF2m_mod_sqrt(d, a, b[j], ctx))
971 && TEST_true(BN_GF2m_mod_sqr(e, d, b[j], ctx))
972 && TEST_true(BN_GF2m_add(f, c, e))
973 /* Test that d^2 = a, where d = sqrt(a). */
974 && TEST_BN_eq_zero(f)))
975 goto err;
976 }
977 }
978 st = 1;
979 err:
980 BN_free(a);
981 BN_free(b[0]);
982 BN_free(b[1]);
983 BN_free(c);
984 BN_free(d);
985 BN_free(e);
986 BN_free(f);
987 return st;
988 }
989
test_gf2m_modsolvequad(void)990 static int test_gf2m_modsolvequad(void)
991 {
992 BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
993 BIGNUM *e = NULL;
994 int i, j, s = 0, t, st = 0;
995
996 if (!TEST_ptr(a = BN_new())
997 || !TEST_ptr(b[0] = BN_new())
998 || !TEST_ptr(b[1] = BN_new())
999 || !TEST_ptr(c = BN_new())
1000 || !TEST_ptr(d = BN_new())
1001 || !TEST_ptr(e = BN_new()))
1002 goto err;
1003
1004 if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
1005 && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
1006 goto err;
1007
1008 for (i = 0; i < NUM0; i++) {
1009 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1010 goto err;
1011 for (j = 0; j < 2; j++) {
1012 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1013 if (t) {
1014 s++;
1015 if (!(TEST_true(BN_GF2m_mod_sqr(d, c, b[j], ctx))
1016 && TEST_true(BN_GF2m_add(d, c, d))
1017 && TEST_true(BN_GF2m_mod(e, a, b[j]))
1018 && TEST_true(BN_GF2m_add(e, e, d))
1019 /*
1020 * Test that solution of quadratic c
1021 * satisfies c^2 + c = a.
1022 */
1023 && TEST_BN_eq_zero(e)))
1024 goto err;
1025 }
1026 }
1027 }
1028 if (!TEST_int_ge(s, 0)) {
1029 TEST_info("%d tests found no roots; probably an error", NUM0);
1030 goto err;
1031 }
1032 st = 1;
1033 err:
1034 BN_free(a);
1035 BN_free(b[0]);
1036 BN_free(b[1]);
1037 BN_free(c);
1038 BN_free(d);
1039 BN_free(e);
1040 return st;
1041 }
1042 #endif
1043
test_kronecker(void)1044 static int test_kronecker(void)
1045 {
1046 BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
1047 int i, legendre, kronecker, st = 0;
1048
1049 if (!TEST_ptr(a = BN_new())
1050 || !TEST_ptr(b = BN_new())
1051 || !TEST_ptr(r = BN_new())
1052 || !TEST_ptr(t = BN_new()))
1053 goto err;
1054
1055 /*
1056 * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1057 * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1058 * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1059 * generate a random prime b and compare these values for a number of
1060 * random a's. (That is, we run the Solovay-Strassen primality test to
1061 * confirm that b is prime, except that we don't want to test whether b
1062 * is prime but whether BN_kronecker works.)
1063 */
1064
1065 if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
1066 goto err;
1067 BN_set_negative(b, rand_neg());
1068
1069 for (i = 0; i < NUM0; i++) {
1070 if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
1071 goto err;
1072 BN_set_negative(a, rand_neg());
1073
1074 /* t := (|b|-1)/2 (note that b is odd) */
1075 if (!TEST_true(BN_copy(t, b)))
1076 goto err;
1077 BN_set_negative(t, 0);
1078 if (!TEST_true(BN_sub_word(t, 1)))
1079 goto err;
1080 if (!TEST_true(BN_rshift1(t, t)))
1081 goto err;
1082 /* r := a^t mod b */
1083 BN_set_negative(b, 0);
1084
1085 if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
1086 goto err;
1087 BN_set_negative(b, 1);
1088
1089 if (BN_is_word(r, 1))
1090 legendre = 1;
1091 else if (BN_is_zero(r))
1092 legendre = 0;
1093 else {
1094 if (!TEST_true(BN_add_word(r, 1)))
1095 goto err;
1096 if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
1097 TEST_info("Legendre symbol computation failed");
1098 goto err;
1099 }
1100 legendre = -1;
1101 }
1102
1103 if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
1104 goto err;
1105 /* we actually need BN_kronecker(a, |b|) */
1106 if (BN_is_negative(a) && BN_is_negative(b))
1107 kronecker = -kronecker;
1108
1109 if (!TEST_int_eq(legendre, kronecker))
1110 goto err;
1111 }
1112
1113 st = 1;
1114 err:
1115 BN_free(a);
1116 BN_free(b);
1117 BN_free(r);
1118 BN_free(t);
1119 return st;
1120 }
1121
file_sum(STANZA * s)1122 static int file_sum(STANZA *s)
1123 {
1124 BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
1125 BN_ULONG b_word;
1126 int st = 0;
1127
1128 if (!TEST_ptr(a = getBN(s, "A"))
1129 || !TEST_ptr(b = getBN(s, "B"))
1130 || !TEST_ptr(sum = getBN(s, "Sum"))
1131 || !TEST_ptr(ret = BN_new()))
1132 goto err;
1133
1134 if (!TEST_true(BN_add(ret, a, b))
1135 || !equalBN("A + B", sum, ret)
1136 || !TEST_true(BN_sub(ret, sum, a))
1137 || !equalBN("Sum - A", b, ret)
1138 || !TEST_true(BN_sub(ret, sum, b))
1139 || !equalBN("Sum - B", a, ret))
1140 goto err;
1141
1142 /*
1143 * Test that the functions work when |r| and |a| point to the same BIGNUM,
1144 * or when |r| and |b| point to the same BIGNUM.
1145 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1146 */
1147 if (!TEST_true(BN_copy(ret, a))
1148 || !TEST_true(BN_add(ret, ret, b))
1149 || !equalBN("A + B (r is a)", sum, ret)
1150 || !TEST_true(BN_copy(ret, b))
1151 || !TEST_true(BN_add(ret, a, ret))
1152 || !equalBN("A + B (r is b)", sum, ret)
1153 || !TEST_true(BN_copy(ret, sum))
1154 || !TEST_true(BN_sub(ret, ret, a))
1155 || !equalBN("Sum - A (r is a)", b, ret)
1156 || !TEST_true(BN_copy(ret, a))
1157 || !TEST_true(BN_sub(ret, sum, ret))
1158 || !equalBN("Sum - A (r is b)", b, ret)
1159 || !TEST_true(BN_copy(ret, sum))
1160 || !TEST_true(BN_sub(ret, ret, b))
1161 || !equalBN("Sum - B (r is a)", a, ret)
1162 || !TEST_true(BN_copy(ret, b))
1163 || !TEST_true(BN_sub(ret, sum, ret))
1164 || !equalBN("Sum - B (r is b)", a, ret))
1165 goto err;
1166
1167 /*
1168 * Test BN_uadd() and BN_usub() with the prerequisites they are
1169 * documented as having. Note that these functions are frequently used
1170 * when the prerequisites don't hold. In those cases, they are supposed
1171 * to work as if the prerequisite hold, but we don't test that yet.
1172 * TODO: test that.
1173 */
1174 if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
1175 if (!TEST_true(BN_uadd(ret, a, b))
1176 || !equalBN("A +u B", sum, ret)
1177 || !TEST_true(BN_usub(ret, sum, a))
1178 || !equalBN("Sum -u A", b, ret)
1179 || !TEST_true(BN_usub(ret, sum, b))
1180 || !equalBN("Sum -u B", a, ret))
1181 goto err;
1182 /*
1183 * Test that the functions work when |r| and |a| point to the same
1184 * BIGNUM, or when |r| and |b| point to the same BIGNUM.
1185 * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
1186 */
1187 if (!TEST_true(BN_copy(ret, a))
1188 || !TEST_true(BN_uadd(ret, ret, b))
1189 || !equalBN("A +u B (r is a)", sum, ret)
1190 || !TEST_true(BN_copy(ret, b))
1191 || !TEST_true(BN_uadd(ret, a, ret))
1192 || !equalBN("A +u B (r is b)", sum, ret)
1193 || !TEST_true(BN_copy(ret, sum))
1194 || !TEST_true(BN_usub(ret, ret, a))
1195 || !equalBN("Sum -u A (r is a)", b, ret)
1196 || !TEST_true(BN_copy(ret, a))
1197 || !TEST_true(BN_usub(ret, sum, ret))
1198 || !equalBN("Sum -u A (r is b)", b, ret)
1199 || !TEST_true(BN_copy(ret, sum))
1200 || !TEST_true(BN_usub(ret, ret, b))
1201 || !equalBN("Sum -u B (r is a)", a, ret)
1202 || !TEST_true(BN_copy(ret, b))
1203 || !TEST_true(BN_usub(ret, sum, ret))
1204 || !equalBN("Sum -u B (r is b)", a, ret))
1205 goto err;
1206 }
1207
1208 /*
1209 * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
1210 */
1211 b_word = BN_get_word(b);
1212 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1213 if (!TEST_true(BN_copy(ret, a))
1214 || !TEST_true(BN_add_word(ret, b_word))
1215 || !equalBN("A + B (word)", sum, ret)
1216 || !TEST_true(BN_copy(ret, sum))
1217 || !TEST_true(BN_sub_word(ret, b_word))
1218 || !equalBN("Sum - B (word)", a, ret))
1219 goto err;
1220 }
1221 st = 1;
1222
1223 err:
1224 BN_free(a);
1225 BN_free(b);
1226 BN_free(sum);
1227 BN_free(ret);
1228 return st;
1229 }
1230
file_lshift1(STANZA * s)1231 static int file_lshift1(STANZA *s)
1232 {
1233 BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
1234 BIGNUM *two = NULL, *remainder = NULL;
1235 int st = 0;
1236
1237 if (!TEST_ptr(a = getBN(s, "A"))
1238 || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
1239 || !TEST_ptr(zero = BN_new())
1240 || !TEST_ptr(ret = BN_new())
1241 || !TEST_ptr(two = BN_new())
1242 || !TEST_ptr(remainder = BN_new()))
1243 goto err;
1244
1245 BN_zero(zero);
1246
1247 if (!TEST_true(BN_set_word(two, 2))
1248 || !TEST_true(BN_add(ret, a, a))
1249 || !equalBN("A + A", lshift1, ret)
1250 || !TEST_true(BN_mul(ret, a, two, ctx))
1251 || !equalBN("A * 2", lshift1, ret)
1252 || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
1253 || !equalBN("LShift1 / 2", a, ret)
1254 || !equalBN("LShift1 % 2", zero, remainder)
1255 || !TEST_true(BN_lshift1(ret, a))
1256 || !equalBN("A << 1", lshift1, ret)
1257 || !TEST_true(BN_rshift1(ret, lshift1))
1258 || !equalBN("LShift >> 1", a, ret)
1259 || !TEST_true(BN_rshift1(ret, lshift1))
1260 || !equalBN("LShift >> 1", a, ret))
1261 goto err;
1262
1263 /* Set the LSB to 1 and test rshift1 again. */
1264 if (!TEST_true(BN_set_bit(lshift1, 0))
1265 || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
1266 || !equalBN("(LShift1 | 1) / 2", a, ret)
1267 || !TEST_true(BN_rshift1(ret, lshift1))
1268 || !equalBN("(LShift | 1) >> 1", a, ret))
1269 goto err;
1270
1271 st = 1;
1272 err:
1273 BN_free(a);
1274 BN_free(lshift1);
1275 BN_free(zero);
1276 BN_free(ret);
1277 BN_free(two);
1278 BN_free(remainder);
1279
1280 return st;
1281 }
1282
file_lshift(STANZA * s)1283 static int file_lshift(STANZA *s)
1284 {
1285 BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
1286 int n = 0, st = 0;
1287
1288 if (!TEST_ptr(a = getBN(s, "A"))
1289 || !TEST_ptr(lshift = getBN(s, "LShift"))
1290 || !TEST_ptr(ret = BN_new())
1291 || !getint(s, &n, "N"))
1292 goto err;
1293
1294 if (!TEST_true(BN_lshift(ret, a, n))
1295 || !equalBN("A << N", lshift, ret)
1296 || !TEST_true(BN_rshift(ret, lshift, n))
1297 || !equalBN("A >> N", a, ret))
1298 goto err;
1299
1300 st = 1;
1301 err:
1302 BN_free(a);
1303 BN_free(lshift);
1304 BN_free(ret);
1305 return st;
1306 }
1307
file_rshift(STANZA * s)1308 static int file_rshift(STANZA *s)
1309 {
1310 BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
1311 int n = 0, st = 0;
1312
1313 if (!TEST_ptr(a = getBN(s, "A"))
1314 || !TEST_ptr(rshift = getBN(s, "RShift"))
1315 || !TEST_ptr(ret = BN_new())
1316 || !getint(s, &n, "N"))
1317 goto err;
1318
1319 if (!TEST_true(BN_rshift(ret, a, n))
1320 || !equalBN("A >> N", rshift, ret))
1321 goto err;
1322
1323 /* If N == 1, try with rshift1 as well */
1324 if (n == 1) {
1325 if (!TEST_true(BN_rshift1(ret, a))
1326 || !equalBN("A >> 1 (rshift1)", rshift, ret))
1327 goto err;
1328 }
1329 st = 1;
1330
1331 err:
1332 BN_free(a);
1333 BN_free(rshift);
1334 BN_free(ret);
1335 return st;
1336 }
1337
file_square(STANZA * s)1338 static int file_square(STANZA *s)
1339 {
1340 BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
1341 BIGNUM *remainder = NULL, *tmp = NULL;
1342 int st = 0;
1343
1344 if (!TEST_ptr(a = getBN(s, "A"))
1345 || !TEST_ptr(square = getBN(s, "Square"))
1346 || !TEST_ptr(zero = BN_new())
1347 || !TEST_ptr(ret = BN_new())
1348 || !TEST_ptr(remainder = BN_new()))
1349 goto err;
1350
1351 BN_zero(zero);
1352 if (!TEST_true(BN_sqr(ret, a, ctx))
1353 || !equalBN("A^2", square, ret)
1354 || !TEST_true(BN_mul(ret, a, a, ctx))
1355 || !equalBN("A * A", square, ret)
1356 || !TEST_true(BN_div(ret, remainder, square, a, ctx))
1357 || !equalBN("Square / A", a, ret)
1358 || !equalBN("Square % A", zero, remainder))
1359 goto err;
1360
1361 #if HAVE_BN_SQRT
1362 BN_set_negative(a, 0);
1363 if (!TEST_true(BN_sqrt(ret, square, ctx))
1364 || !equalBN("sqrt(Square)", a, ret))
1365 goto err;
1366
1367 /* BN_sqrt should fail on non-squares and negative numbers. */
1368 if (!TEST_BN_eq_zero(square)) {
1369 if (!TEST_ptr(tmp = BN_new())
1370 || !TEST_true(BN_copy(tmp, square)))
1371 goto err;
1372 BN_set_negative(tmp, 1);
1373
1374 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
1375 goto err;
1376 ERR_clear_error();
1377
1378 BN_set_negative(tmp, 0);
1379 if (BN_add(tmp, tmp, BN_value_one()))
1380 goto err;
1381 if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
1382 goto err;
1383 ERR_clear_error();
1384 }
1385 #endif
1386
1387 st = 1;
1388 err:
1389 BN_free(a);
1390 BN_free(square);
1391 BN_free(zero);
1392 BN_free(ret);
1393 BN_free(remainder);
1394 BN_free(tmp);
1395 return st;
1396 }
1397
file_product(STANZA * s)1398 static int file_product(STANZA *s)
1399 {
1400 BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
1401 BIGNUM *remainder = NULL, *zero = NULL;
1402 int st = 0;
1403
1404 if (!TEST_ptr(a = getBN(s, "A"))
1405 || !TEST_ptr(b = getBN(s, "B"))
1406 || !TEST_ptr(product = getBN(s, "Product"))
1407 || !TEST_ptr(ret = BN_new())
1408 || !TEST_ptr(remainder = BN_new())
1409 || !TEST_ptr(zero = BN_new()))
1410 goto err;
1411
1412 BN_zero(zero);
1413
1414 if (!TEST_true(BN_mul(ret, a, b, ctx))
1415 || !equalBN("A * B", product, ret)
1416 || !TEST_true(BN_div(ret, remainder, product, a, ctx))
1417 || !equalBN("Product / A", b, ret)
1418 || !equalBN("Product % A", zero, remainder)
1419 || !TEST_true(BN_div(ret, remainder, product, b, ctx))
1420 || !equalBN("Product / B", a, ret)
1421 || !equalBN("Product % B", zero, remainder))
1422 goto err;
1423
1424 st = 1;
1425 err:
1426 BN_free(a);
1427 BN_free(b);
1428 BN_free(product);
1429 BN_free(ret);
1430 BN_free(remainder);
1431 BN_free(zero);
1432 return st;
1433 }
1434
file_quotient(STANZA * s)1435 static int file_quotient(STANZA *s)
1436 {
1437 BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
1438 BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
1439 BN_ULONG b_word, ret_word;
1440 int st = 0;
1441
1442 if (!TEST_ptr(a = getBN(s, "A"))
1443 || !TEST_ptr(b = getBN(s, "B"))
1444 || !TEST_ptr(quotient = getBN(s, "Quotient"))
1445 || !TEST_ptr(remainder = getBN(s, "Remainder"))
1446 || !TEST_ptr(ret = BN_new())
1447 || !TEST_ptr(ret2 = BN_new())
1448 || !TEST_ptr(nnmod = BN_new()))
1449 goto err;
1450
1451 if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
1452 || !equalBN("A / B", quotient, ret)
1453 || !equalBN("A % B", remainder, ret2)
1454 || !TEST_true(BN_mul(ret, quotient, b, ctx))
1455 || !TEST_true(BN_add(ret, ret, remainder))
1456 || !equalBN("Quotient * B + Remainder", a, ret))
1457 goto err;
1458
1459 /*
1460 * Test with BN_mod_word() and BN_div_word() if the divisor is
1461 * small enough.
1462 */
1463 b_word = BN_get_word(b);
1464 if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
1465 BN_ULONG remainder_word = BN_get_word(remainder);
1466
1467 assert(remainder_word != (BN_ULONG)-1);
1468 if (!TEST_ptr(BN_copy(ret, a)))
1469 goto err;
1470 ret_word = BN_div_word(ret, b_word);
1471 if (ret_word != remainder_word) {
1472 #ifdef BN_DEC_FMT1
1473 TEST_error(
1474 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
1475 ret_word, remainder_word);
1476 #else
1477 TEST_error("Got A %% B (word) mismatch");
1478 #endif
1479 goto err;
1480 }
1481 if (!equalBN ("A / B (word)", quotient, ret))
1482 goto err;
1483
1484 ret_word = BN_mod_word(a, b_word);
1485 if (ret_word != remainder_word) {
1486 #ifdef BN_DEC_FMT1
1487 TEST_error(
1488 "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
1489 ret_word, remainder_word);
1490 #else
1491 TEST_error("Got A %% B (word) mismatch");
1492 #endif
1493 goto err;
1494 }
1495 }
1496
1497 /* Test BN_nnmod. */
1498 if (!BN_is_negative(b)) {
1499 if (!TEST_true(BN_copy(nnmod, remainder))
1500 || (BN_is_negative(nnmod)
1501 && !TEST_true(BN_add(nnmod, nnmod, b)))
1502 || !TEST_true(BN_nnmod(ret, a, b, ctx))
1503 || !equalBN("A % B (non-negative)", nnmod, ret))
1504 goto err;
1505 }
1506
1507 st = 1;
1508 err:
1509 BN_free(a);
1510 BN_free(b);
1511 BN_free(quotient);
1512 BN_free(remainder);
1513 BN_free(ret);
1514 BN_free(ret2);
1515 BN_free(nnmod);
1516 return st;
1517 }
1518
file_modmul(STANZA * s)1519 static int file_modmul(STANZA *s)
1520 {
1521 BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
1522 int st = 0;
1523
1524 if (!TEST_ptr(a = getBN(s, "A"))
1525 || !TEST_ptr(b = getBN(s, "B"))
1526 || !TEST_ptr(m = getBN(s, "M"))
1527 || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
1528 || !TEST_ptr(ret = BN_new()))
1529 goto err;
1530
1531 if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
1532 || !equalBN("A * B (mod M)", mod_mul, ret))
1533 goto err;
1534
1535 if (BN_is_odd(m)) {
1536 /* Reduce |a| and |b| and test the Montgomery version. */
1537 BN_MONT_CTX *mont = BN_MONT_CTX_new();
1538 BIGNUM *a_tmp = BN_new();
1539 BIGNUM *b_tmp = BN_new();
1540
1541 if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1542 || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
1543 || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
1544 || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
1545 || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
1546 || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
1547 || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
1548 mont, ctx))
1549 || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
1550 || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
1551 st = 0;
1552 else
1553 st = 1;
1554 BN_MONT_CTX_free(mont);
1555 BN_free(a_tmp);
1556 BN_free(b_tmp);
1557 if (st == 0)
1558 goto err;
1559 }
1560
1561 st = 1;
1562 err:
1563 BN_free(a);
1564 BN_free(b);
1565 BN_free(m);
1566 BN_free(mod_mul);
1567 BN_free(ret);
1568 return st;
1569 }
1570
file_modexp(STANZA * s)1571 static int file_modexp(STANZA *s)
1572 {
1573 BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
1574 BIGNUM *b = NULL, *c = NULL, *d = NULL;
1575 int st = 0;
1576
1577 if (!TEST_ptr(a = getBN(s, "A"))
1578 || !TEST_ptr(e = getBN(s, "E"))
1579 || !TEST_ptr(m = getBN(s, "M"))
1580 || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
1581 || !TEST_ptr(ret = BN_new())
1582 || !TEST_ptr(d = BN_new()))
1583 goto err;
1584
1585 if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
1586 || !equalBN("A ^ E (mod M)", mod_exp, ret))
1587 goto err;
1588
1589 if (BN_is_odd(m)) {
1590 if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
1591 || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
1592 || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
1593 ctx, NULL))
1594 || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
1595 goto err;
1596 }
1597
1598 /* Regression test for carry propagation bug in sqr8x_reduction */
1599 BN_hex2bn(&a, "050505050505");
1600 BN_hex2bn(&b, "02");
1601 BN_hex2bn(&c,
1602 "4141414141414141414141274141414141414141414141414141414141414141"
1603 "4141414141414141414141414141414141414141414141414141414141414141"
1604 "4141414141414141414141800000000000000000000000000000000000000000"
1605 "0000000000000000000000000000000000000000000000000000000000000000"
1606 "0000000000000000000000000000000000000000000000000000000000000000"
1607 "0000000000000000000000000000000000000000000000000000000001");
1608 if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
1609 || !TEST_true(BN_mul(e, a, a, ctx))
1610 || !TEST_BN_eq(d, e))
1611 goto err;
1612
1613 st = 1;
1614 err:
1615 BN_free(a);
1616 BN_free(b);
1617 BN_free(c);
1618 BN_free(d);
1619 BN_free(e);
1620 BN_free(m);
1621 BN_free(mod_exp);
1622 BN_free(ret);
1623 return st;
1624 }
1625
file_exp(STANZA * s)1626 static int file_exp(STANZA *s)
1627 {
1628 BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
1629 int st = 0;
1630
1631 if (!TEST_ptr(a = getBN(s, "A"))
1632 || !TEST_ptr(e = getBN(s, "E"))
1633 || !TEST_ptr(exp = getBN(s, "Exp"))
1634 || !TEST_ptr(ret = BN_new()))
1635 goto err;
1636
1637 if (!TEST_true(BN_exp(ret, a, e, ctx))
1638 || !equalBN("A ^ E", exp, ret))
1639 goto err;
1640
1641 st = 1;
1642 err:
1643 BN_free(a);
1644 BN_free(e);
1645 BN_free(exp);
1646 BN_free(ret);
1647 return st;
1648 }
1649
file_modsqrt(STANZA * s)1650 static int file_modsqrt(STANZA *s)
1651 {
1652 BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
1653 int st = 0;
1654
1655 if (!TEST_ptr(a = getBN(s, "A"))
1656 || !TEST_ptr(p = getBN(s, "P"))
1657 || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
1658 || !TEST_ptr(ret = BN_new())
1659 || !TEST_ptr(ret2 = BN_new()))
1660 goto err;
1661
1662 /* There are two possible answers. */
1663 if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
1664 || !TEST_true(BN_sub(ret2, p, ret)))
1665 goto err;
1666
1667 /* The first condition should NOT be a test. */
1668 if (BN_cmp(ret2, mod_sqrt) != 0
1669 && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
1670 goto err;
1671
1672 st = 1;
1673 err:
1674 BN_free(a);
1675 BN_free(p);
1676 BN_free(mod_sqrt);
1677 BN_free(ret);
1678 BN_free(ret2);
1679 return st;
1680 }
1681
file_gcd(STANZA * s)1682 static int file_gcd(STANZA *s)
1683 {
1684 BIGNUM *a = NULL, *b = NULL, *gcd = NULL, *ret = NULL;
1685 int st = 0;
1686
1687 if (!TEST_ptr(a = getBN(s, "A"))
1688 || !TEST_ptr(b = getBN(s, "B"))
1689 || !TEST_ptr(gcd = getBN(s, "GCD"))
1690 || !TEST_ptr(ret = BN_new()))
1691 goto err;
1692
1693 if (!TEST_true(BN_gcd(ret, a, b, ctx))
1694 || !equalBN("gcd(A,B)", gcd, ret))
1695 goto err;
1696
1697 st = 1;
1698 err:
1699 BN_free(a);
1700 BN_free(b);
1701 BN_free(gcd);
1702 BN_free(ret);
1703 return st;
1704 }
1705
test_bn2padded(void)1706 static int test_bn2padded(void)
1707 {
1708 #if HAVE_BN_PADDED
1709 uint8_t zeros[256], out[256], reference[128];
1710 BIGNUM *n = BN_new();
1711 int st = 0;
1712
1713 /* Test edge case at 0. */
1714 if (n == NULL)
1715 goto err;
1716 if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
1717 goto err;
1718 memset(out, -1, sizeof(out));
1719 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
1720 goto err;
1721 memset(zeros, 0, sizeof(zeros));
1722 if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
1723 goto err;
1724
1725 /* Test a random numbers at various byte lengths. */
1726 for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
1727 # define TOP_BIT_ON 0
1728 # define BOTTOM_BIT_NOTOUCH 0
1729 if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
1730 goto err;
1731 if (!TEST_int_eq(BN_num_bytes(n),A) bytes
1732 || TEST_int_eq(BN_bn2bin(n, reference), bytes))
1733 goto err;
1734 /* Empty buffer should fail. */
1735 if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
1736 goto err;
1737 /* One byte short should fail. */
1738 if (BN_bn2bin_padded(out, bytes - 1, n))
1739 goto err;
1740 /* Exactly right size should encode. */
1741 if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
1742 || TEST_mem_eq(out, bytes, reference, bytes))
1743 goto err;
1744 /* Pad up one byte extra. */
1745 if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
1746 || !TEST_mem_eq(out + 1, bytes, reference, bytes)
1747 || !TEST_mem_eq(out, 1, zeros, 1))
1748 goto err;
1749 /* Pad up to 256. */
1750 if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
1751 || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
1752 reference, bytes)
1753 || !TEST_mem_eq(out, sizseof(out) - bytes,
1754 zeros, sizeof(out) - bytes))
1755 goto err;
1756 }
1757
1758 st = 1;
1759 err:
1760 BN_free(n);
1761 return st;
1762 #else
1763 return ctx != NULL;
1764 #endif
1765 }
1766
test_dec2bn(void)1767 static int test_dec2bn(void)
1768 {
1769 BIGNUM *bn = NULL;
1770 int st = 0;
1771
1772 if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
1773 || !TEST_BN_eq_word(bn, 0)
1774 || !TEST_BN_eq_zero(bn)
1775 || !TEST_BN_le_zero(bn)
1776 || !TEST_BN_ge_zero(bn)
1777 || !TEST_BN_even(bn))
1778 goto err;
1779 BN_free(bn);
1780 bn = NULL;
1781
1782 if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
1783 || !TEST_BN_eq_word(bn, 256)
1784 || !TEST_BN_ge_zero(bn)
1785 || !TEST_BN_gt_zero(bn)
1786 || !TEST_BN_ne_zero(bn)
1787 || !TEST_BN_even(bn))
1788 goto err;
1789 BN_free(bn);
1790 bn = NULL;
1791
1792 if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
1793 || !TEST_BN_abs_eq_word(bn, 42)
1794 || !TEST_BN_lt_zero(bn)
1795 || !TEST_BN_le_zero(bn)
1796 || !TEST_BN_ne_zero(bn)
1797 || !TEST_BN_even(bn))
1798 goto err;
1799 BN_free(bn);
1800 bn = NULL;
1801
1802 if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
1803 || !TEST_BN_eq_word(bn, 1)
1804 || !TEST_BN_ne_zero(bn)
1805 || !TEST_BN_gt_zero(bn)
1806 || !TEST_BN_ge_zero(bn)
1807 || !TEST_BN_eq_one(bn)
1808 || !TEST_BN_odd(bn))
1809 goto err;
1810 BN_free(bn);
1811 bn = NULL;
1812
1813 if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
1814 || !TEST_BN_eq_zero(bn)
1815 || !TEST_BN_ge_zero(bn)
1816 || !TEST_BN_le_zero(bn)
1817 || !TEST_BN_even(bn))
1818 goto err;
1819 BN_free(bn);
1820 bn = NULL;
1821
1822 if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
1823 || !TEST_BN_abs_eq_word(bn, 42)
1824 || !TEST_BN_ge_zero(bn)
1825 || !TEST_BN_gt_zero(bn)
1826 || !TEST_BN_ne_zero(bn)
1827 || !TEST_BN_even(bn))
1828 goto err;
1829
1830 st = 1;
1831 err:
1832 BN_free(bn);
1833 return st;
1834 }
1835
test_hex2bn(void)1836 static int test_hex2bn(void)
1837 {
1838 BIGNUM *bn = NULL;
1839 int st = 0;
1840
1841 if (!TEST_int_eq(parseBN(&bn, "0"), 1)
1842 || !TEST_BN_eq_zero(bn)
1843 || !TEST_BN_ge_zero(bn)
1844 || !TEST_BN_even(bn))
1845 goto err;
1846 BN_free(bn);
1847 bn = NULL;
1848
1849 if (!TEST_int_eq(parseBN(&bn, "256"), 3)
1850 || !TEST_BN_eq_word(bn, 0x256)
1851 || !TEST_BN_ge_zero(bn)
1852 || !TEST_BN_gt_zero(bn)
1853 || !TEST_BN_ne_zero(bn)
1854 || !TEST_BN_even(bn))
1855 goto err;
1856 BN_free(bn);
1857 bn = NULL;
1858
1859 if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
1860 || !TEST_BN_abs_eq_word(bn, 0x42)
1861 || !TEST_BN_lt_zero(bn)
1862 || !TEST_BN_le_zero(bn)
1863 || !TEST_BN_ne_zero(bn)
1864 || !TEST_BN_even(bn))
1865 goto err;
1866 BN_free(bn);
1867 bn = NULL;
1868
1869 if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
1870 || !TEST_BN_eq_word(bn, 0xCB)
1871 || !TEST_BN_ge_zero(bn)
1872 || !TEST_BN_gt_zero(bn)
1873 || !TEST_BN_ne_zero(bn)
1874 || !TEST_BN_odd(bn))
1875 goto err;
1876 BN_free(bn);
1877 bn = NULL;
1878
1879 if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
1880 || !TEST_BN_eq_zero(bn)
1881 || !TEST_BN_ge_zero(bn)
1882 || !TEST_BN_le_zero(bn)
1883 || !TEST_BN_even(bn))
1884 goto err;
1885 BN_free(bn);
1886 bn = NULL;
1887
1888 if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
1889 || !TEST_BN_eq_word(bn, 0xabc)
1890 || !TEST_BN_ge_zero(bn)
1891 || !TEST_BN_gt_zero(bn)
1892 || !TEST_BN_ne_zero(bn)
1893 || !TEST_BN_even(bn))
1894 goto err;
1895 st = 1;
1896
1897 err:
1898 BN_free(bn);
1899 return st;
1900 }
1901
test_asc2bn(void)1902 static int test_asc2bn(void)
1903 {
1904 BIGNUM *bn = NULL;
1905 int st = 0;
1906
1907 if (!TEST_ptr(bn = BN_new()))
1908 goto err;
1909
1910 if (!TEST_true(BN_asc2bn(&bn, "0"))
1911 || !TEST_BN_eq_zero(bn)
1912 || !TEST_BN_ge_zero(bn))
1913 goto err;
1914
1915 if (!TEST_true(BN_asc2bn(&bn, "256"))
1916 || !TEST_BN_eq_word(bn, 256)
1917 || !TEST_BN_ge_zero(bn))
1918 goto err;
1919
1920 if (!TEST_true(BN_asc2bn(&bn, "-42"))
1921 || !TEST_BN_abs_eq_word(bn, 42)
1922 || !TEST_BN_lt_zero(bn))
1923 goto err;
1924
1925 if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
1926 || !TEST_BN_eq_word(bn, 0x1234)
1927 || !TEST_BN_ge_zero(bn))
1928 goto err;
1929
1930 if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
1931 || !TEST_BN_eq_word(bn, 0x1234)
1932 || !TEST_BN_ge_zero(bn))
1933 goto err;
1934
1935 if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
1936 || !TEST_BN_abs_eq_word(bn, 0xabcd)
1937 || !TEST_BN_lt_zero(bn))
1938 goto err;
1939
1940 if (!TEST_true(BN_asc2bn(&bn, "-0"))
1941 || !TEST_BN_eq_zero(bn)
1942 || !TEST_BN_ge_zero(bn))
1943 goto err;
1944
1945 if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
1946 || !TEST_BN_eq_word(bn, 123)
1947 || !TEST_BN_ge_zero(bn))
1948 goto err;
1949
1950 st = 1;
1951 err:
1952 BN_free(bn);
1953 return st;
1954 }
1955
1956 static const MPITEST kMPITests[] = {
1957 {"0", "\x00\x00\x00\x00", 4},
1958 {"1", "\x00\x00\x00\x01\x01", 5},
1959 {"-1", "\x00\x00\x00\x01\x81", 5},
1960 {"128", "\x00\x00\x00\x02\x00\x80", 6},
1961 {"256", "\x00\x00\x00\x02\x01\x00", 6},
1962 {"-256", "\x00\x00\x00\x02\x81\x00", 6},
1963 };
1964
test_mpi(int i)1965 static int test_mpi(int i)
1966 {
1967 uint8_t scratch[8];
1968 const MPITEST *test = &kMPITests[i];
1969 size_t mpi_len, mpi_len2;
1970 BIGNUM *bn = NULL;
1971 BIGNUM *bn2 = NULL;
1972 int st = 0;
1973
1974 if (!TEST_ptr(bn = BN_new())
1975 || !TEST_true(BN_asc2bn(&bn, test->base10)))
1976 goto err;
1977 mpi_len = BN_bn2mpi(bn, NULL);
1978 if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
1979 goto err;
1980
1981 if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
1982 || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
1983 goto err;
1984
1985 if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
1986 goto err;
1987
1988 if (!TEST_BN_eq(bn, bn2)) {
1989 BN_free(bn2);
1990 goto err;
1991 }
1992 BN_free(bn2);
1993
1994 st = 1;
1995 err:
1996 BN_free(bn);
1997 return st;
1998 }
1999
test_rand(void)2000 static int test_rand(void)
2001 {
2002 BIGNUM *bn = NULL;
2003 int st = 0;
2004
2005 if (!TEST_ptr(bn = BN_new()))
2006 return 0;
2007
2008 /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
2009 if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
2010 || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
2011 || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
2012 || !TEST_BN_eq_one(bn)
2013 || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
2014 || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
2015 || !TEST_BN_eq_one(bn)
2016 || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
2017 || !TEST_BN_eq_word(bn, 3))
2018 goto err;
2019
2020 st = 1;
2021 err:
2022 BN_free(bn);
2023 return st;
2024 }
2025
test_negzero(void)2026 static int test_negzero(void)
2027 {
2028 BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
2029 BIGNUM *numerator = NULL, *denominator = NULL;
2030 int consttime, st = 0;
2031
2032 if (!TEST_ptr(a = BN_new())
2033 || !TEST_ptr(b = BN_new())
2034 || !TEST_ptr(c = BN_new())
2035 || !TEST_ptr(d = BN_new()))
2036 goto err;
2037
2038 /* Test that BN_mul never gives negative zero. */
2039 if (!TEST_true(BN_set_word(a, 1)))
2040 goto err;
2041 BN_set_negative(a, 1);
2042 BN_zero(b);
2043 if (!TEST_true(BN_mul(c, a, b, ctx)))
2044 goto err;
2045 if (!TEST_BN_eq_zero(c)
2046 || !TEST_BN_ge_zero(c))
2047 goto err;
2048
2049 for (consttime = 0; consttime < 2; consttime++) {
2050 if (!TEST_ptr(numerator = BN_new())
2051 || !TEST_ptr(denominator = BN_new()))
2052 goto err;
2053 if (consttime) {
2054 BN_set_flags(numerator, BN_FLG_CONSTTIME);
2055 BN_set_flags(denominator, BN_FLG_CONSTTIME);
2056 }
2057 /* Test that BN_div never gives negative zero in the quotient. */
2058 if (!TEST_true(BN_set_word(numerator, 1))
2059 || !TEST_true(BN_set_word(denominator, 2)))
2060 goto err;
2061 BN_set_negative(numerator, 1);
2062 if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
2063 || !TEST_BN_eq_zero(a)
2064 || !TEST_BN_ge_zero(a))
2065 goto err;
2066
2067 /* Test that BN_div never gives negative zero in the remainder. */
2068 if (!TEST_true(BN_set_word(denominator, 1))
2069 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
2070 || !TEST_BN_eq_zero(b)
2071 || !TEST_BN_ge_zero(b))
2072 goto err;
2073 BN_free(numerator);
2074 BN_free(denominator);
2075 numerator = denominator = NULL;
2076 }
2077
2078 /* Test that BN_set_negative will not produce a negative zero. */
2079 BN_zero(a);
2080 BN_set_negative(a, 1);
2081 if (BN_is_negative(a))
2082 goto err;
2083 st = 1;
2084
2085 err:
2086 BN_free(a);
2087 BN_free(b);
2088 BN_free(c);
2089 BN_free(d);
2090 BN_free(numerator);
2091 BN_free(denominator);
2092 return st;
2093 }
2094
test_badmod(void)2095 static int test_badmod(void)
2096 {
2097 BIGNUM *a = NULL, *b = NULL, *zero = NULL;
2098 BN_MONT_CTX *mont = NULL;
2099 int st = 0;
2100
2101 if (!TEST_ptr(a = BN_new())
2102 || !TEST_ptr(b = BN_new())
2103 || !TEST_ptr(zero = BN_new())
2104 || !TEST_ptr(mont = BN_MONT_CTX_new()))
2105 goto err;
2106 BN_zero(zero);
2107
2108 if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
2109 goto err;
2110 ERR_clear_error();
2111
2112 if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
2113 goto err;
2114 ERR_clear_error();
2115
2116 if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
2117 goto err;
2118 ERR_clear_error();
2119
2120 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2121 zero, ctx, NULL)))
2122 goto err;
2123 ERR_clear_error();
2124
2125 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2126 zero, ctx, NULL)))
2127 goto err;
2128 ERR_clear_error();
2129
2130 if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
2131 goto err;
2132 ERR_clear_error();
2133
2134 /* Some operations also may not be used with an even modulus. */
2135 if (!TEST_true(BN_set_word(b, 16)))
2136 goto err;
2137
2138 if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
2139 goto err;
2140 ERR_clear_error();
2141
2142 if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
2143 b, ctx, NULL)))
2144 goto err;
2145 ERR_clear_error();
2146
2147 if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
2148 b, ctx, NULL)))
2149 goto err;
2150 ERR_clear_error();
2151
2152 st = 1;
2153 err:
2154 BN_free(a);
2155 BN_free(b);
2156 BN_free(zero);
2157 BN_MONT_CTX_free(mont);
2158 return st;
2159 }
2160
test_expmodzero(void)2161 static int test_expmodzero(void)
2162 {
2163 BIGNUM *a = NULL, *r = NULL, *zero = NULL;
2164 int st = 0;
2165
2166 if (!TEST_ptr(zero = BN_new())
2167 || !TEST_ptr(a = BN_new())
2168 || !TEST_ptr(r = BN_new()))
2169 goto err;
2170 BN_zero(zero);
2171
2172 if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
2173 || !TEST_BN_eq_zero(r)
2174 || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
2175 NULL, NULL))
2176 || !TEST_BN_eq_zero(r)
2177 || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
2178 BN_value_one(),
2179 NULL, NULL))
2180 || !TEST_BN_eq_zero(r)
2181 || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
2182 BN_value_one(), NULL, NULL))
2183 || !TEST_BN_eq_zero(r))
2184 goto err;
2185
2186 st = 1;
2187 err:
2188 BN_free(zero);
2189 BN_free(a);
2190 BN_free(r);
2191 return st;
2192 }
2193
test_expmodone(void)2194 static int test_expmodone(void)
2195 {
2196 int ret = 0, i;
2197 BIGNUM *r = BN_new();
2198 BIGNUM *a = BN_new();
2199 BIGNUM *p = BN_new();
2200 BIGNUM *m = BN_new();
2201
2202 if (!TEST_ptr(r)
2203 || !TEST_ptr(a)
2204 || !TEST_ptr(p)
2205 || !TEST_ptr(p)
2206 || !TEST_ptr(m)
2207 || !TEST_true(BN_set_word(a, 1))
2208 || !TEST_true(BN_set_word(p, 0))
2209 || !TEST_true(BN_set_word(m, 1)))
2210 goto err;
2211
2212 /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
2213 for (i = 0; i < 2; i++) {
2214 if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
2215 || !TEST_BN_eq_zero(r)
2216 || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
2217 || !TEST_BN_eq_zero(r)
2218 || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
2219 || !TEST_BN_eq_zero(r)
2220 || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
2221 || !TEST_BN_eq_zero(r)
2222 || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
2223 || !TEST_BN_eq_zero(r)
2224 || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
2225 || !TEST_BN_eq_zero(r))
2226 goto err;
2227 /* Repeat for r = 1 ^ 0 mod -1 */
2228 if (i == 0)
2229 BN_set_negative(m, 1);
2230 }
2231
2232 ret = 1;
2233 err:
2234 BN_free(r);
2235 BN_free(a);
2236 BN_free(p);
2237 BN_free(m);
2238 return ret;
2239 }
2240
test_smallprime(int kBits)2241 static int test_smallprime(int kBits)
2242 {
2243 BIGNUM *r;
2244 int st = 0;
2245
2246 if (!TEST_ptr(r = BN_new()))
2247 goto err;
2248
2249 if (kBits <= 1) {
2250 if (!TEST_false(BN_generate_prime_ex(r, kBits, 0,
2251 NULL, NULL, NULL)))
2252 goto err;
2253 } else {
2254 if (!TEST_true(BN_generate_prime_ex(r, kBits, 0,
2255 NULL, NULL, NULL))
2256 || !TEST_int_eq(BN_num_bits(r), kBits))
2257 goto err;
2258 }
2259
2260 st = 1;
2261 err:
2262 BN_free(r);
2263 return st;
2264 }
2265
test_smallsafeprime(int kBits)2266 static int test_smallsafeprime(int kBits)
2267 {
2268 BIGNUM *r;
2269 int st = 0;
2270
2271 if (!TEST_ptr(r = BN_new()))
2272 goto err;
2273
2274 if (kBits <= 5 && kBits != 3) {
2275 if (!TEST_false(BN_generate_prime_ex(r, kBits, 1,
2276 NULL, NULL, NULL)))
2277 goto err;
2278 } else {
2279 if (!TEST_true(BN_generate_prime_ex(r, kBits, 1,
2280 NULL, NULL, NULL))
2281 || !TEST_int_eq(BN_num_bits(r), kBits))
2282 goto err;
2283 }
2284
2285 st = 1;
2286 err:
2287 BN_free(r);
2288 return st;
2289 }
2290
2291 static int primes[] = { 2, 3, 5, 7, 17863 };
2292
test_is_prime(int i)2293 static int test_is_prime(int i)
2294 {
2295 int ret = 0;
2296 BIGNUM *r = NULL;
2297 int trial;
2298
2299 if (!TEST_ptr(r = BN_new()))
2300 goto err;
2301
2302 for (trial = 0; trial <= 1; ++trial) {
2303 if (!TEST_true(BN_set_word(r, primes[i]))
2304 || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL),
2305 1))
2306 goto err;
2307 }
2308
2309 ret = 1;
2310 err:
2311 BN_free(r);
2312 return ret;
2313 }
2314
2315 static int not_primes[] = { -1, 0, 1, 4 };
2316
test_not_prime(int i)2317 static int test_not_prime(int i)
2318 {
2319 int ret = 0;
2320 BIGNUM *r = NULL;
2321 int trial;
2322
2323 if (!TEST_ptr(r = BN_new()))
2324 goto err;
2325
2326 for (trial = 0; trial <= 1; ++trial) {
2327 if (!TEST_true(BN_set_word(r, not_primes[i]))
2328 || !TEST_false(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL)))
2329 goto err;
2330 }
2331
2332 ret = 1;
2333 err:
2334 BN_free(r);
2335 return ret;
2336 }
2337
test_ctx_set_ct_flag(BN_CTX * c)2338 static int test_ctx_set_ct_flag(BN_CTX *c)
2339 {
2340 int st = 0;
2341 size_t i;
2342 BIGNUM *b[15];
2343
2344 BN_CTX_start(c);
2345 for (i = 0; i < OSSL_NELEM(b); i++) {
2346 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2347 goto err;
2348 if (i % 2 == 1)
2349 BN_set_flags(b[i], BN_FLG_CONSTTIME);
2350 }
2351
2352 st = 1;
2353 err:
2354 BN_CTX_end(c);
2355 return st;
2356 }
2357
test_ctx_check_ct_flag(BN_CTX * c)2358 static int test_ctx_check_ct_flag(BN_CTX *c)
2359 {
2360 int st = 0;
2361 size_t i;
2362 BIGNUM *b[30];
2363
2364 BN_CTX_start(c);
2365 for (i = 0; i < OSSL_NELEM(b); i++) {
2366 if (!TEST_ptr(b[i] = BN_CTX_get(c)))
2367 goto err;
2368 if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
2369 goto err;
2370 }
2371
2372 st = 1;
2373 err:
2374 BN_CTX_end(c);
2375 return st;
2376 }
2377
test_ctx_consttime_flag(void)2378 static int test_ctx_consttime_flag(void)
2379 {
2380 /*-
2381 * The constant-time flag should not "leak" among BN_CTX frames:
2382 *
2383 * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
2384 * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
2385 * from the frame before ending it.
2386 * - test_ctx_check_ct_flag() then starts a new frame and gets a
2387 * number of BIGNUMs from it. In absence of leaks, none of the
2388 * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
2389 *
2390 * In actual BN_CTX usage inside libcrypto the leak could happen at
2391 * any depth level in the BN_CTX stack, with varying results
2392 * depending on the patterns of sibling trees of nested function
2393 * calls sharing the same BN_CTX object, and the effect of
2394 * unintended BN_FLG_CONSTTIME on the called BN_* functions.
2395 *
2396 * This simple unit test abstracts away this complexity and verifies
2397 * that the leak does not happen between two sibling functions
2398 * sharing the same BN_CTX object at the same level of nesting.
2399 *
2400 */
2401 BN_CTX *nctx = NULL;
2402 BN_CTX *sctx = NULL;
2403 size_t i = 0;
2404 int st = 0;
2405
2406 if (!TEST_ptr(nctx = BN_CTX_new())
2407 || !TEST_ptr(sctx = BN_CTX_secure_new()))
2408 goto err;
2409
2410 for (i = 0; i < 2; i++) {
2411 BN_CTX *c = i == 0 ? nctx : sctx;
2412 if (!TEST_true(test_ctx_set_ct_flag(c))
2413 || !TEST_true(test_ctx_check_ct_flag(c)))
2414 goto err;
2415 }
2416
2417 st = 1;
2418 err:
2419 BN_CTX_free(nctx);
2420 BN_CTX_free(sctx);
2421 return st;
2422 }
2423
test_gcd_prime(void)2424 static int test_gcd_prime(void)
2425 {
2426 BIGNUM *a = NULL, *b = NULL, *gcd = NULL;
2427 int i, st = 0;
2428
2429 if (!TEST_ptr(a = BN_new())
2430 || !TEST_ptr(b = BN_new())
2431 || !TEST_ptr(gcd = BN_new()))
2432 goto err;
2433
2434 if (!TEST_true(BN_generate_prime_ex(a, 1024, 0, NULL, NULL, NULL)))
2435 goto err;
2436 for (i = 0; i < NUM0; i++) {
2437 if (!TEST_true(BN_generate_prime_ex(b, 1024, 0,
2438 NULL, NULL, NULL))
2439 || !TEST_true(BN_gcd(gcd, a, b, ctx))
2440 || !TEST_true(BN_is_one(gcd)))
2441 goto err;
2442 }
2443
2444 st = 1;
2445 err:
2446 BN_free(a);
2447 BN_free(b);
2448 BN_free(gcd);
2449 return st;
2450 }
2451
2452 typedef struct mod_exp_test_st
2453 {
2454 const char *base;
2455 const char *exp;
2456 const char *mod;
2457 const char *res;
2458 } MOD_EXP_TEST;
2459
2460 static const MOD_EXP_TEST ModExpTests[] = {
2461 /* original test vectors for rsaz_512_sqr bug, by OSS-Fuzz */
2462 {
2463 "1166180238001879113042182292626169621106255558914000595999312084"
2464 "4627946820899490684928760491249738643524880720584249698100907201"
2465 "002086675047927600340800371",
2466 "8000000000000000000000000000000000000000000000000000000000000000"
2467 "0000000000000000000000000000000000000000000000000000000000000000"
2468 "00000000",
2469 "1340780792684523720980737645613191762604395855615117867483316354"
2470 "3294276330515137663421134775482798690129946803802212663956180562"
2471 "088664022929883876655300863",
2472 "8243904058268085430037326628480645845409758077568738532059032482"
2473 "8294114415890603594730158120426756266457928475330450251339773498"
2474 "26758407619521544102068438"
2475 },
2476 {
2477 "4974270041410803822078866696159586946995877618987010219312844726"
2478 "0284386121835740784990869050050504348861513337232530490826340663"
2479 "197278031692737429054",
2480 "4974270041410803822078866696159586946995877428188754995041148539"
2481 "1663243362592271353668158565195557417149981094324650322556843202"
2482 "946445882670777892608",
2483 "1340780716511420227215592830971452482815377482627251725537099028"
2484 "4429769497230131760206012644403029349547320953206103351725462999"
2485 "947509743623340557059752191",
2486 "5296244594780707015616522701706118082963369547253192207884519362"
2487 "1767869984947542695665420219028522815539559194793619684334900442"
2488 "49304558011362360473525933"
2489 },
2490 /* test vectors for rsaz_512_srq bug, with rcx/rbx=1 */
2491 { /* between first and second iteration */
2492 "5148719036160389201525610950887605325980251964889646556085286545"
2493 "3931548809178823413169359635978762036512397113080988070677858033"
2494 "36463909753993540214027190",
2495 "6703903964971298549787012499102923063739682910296196688861780721"
2496 "8608820150367734884009371490834517138450159290932430254268769414"
2497 "05973284973216824503042158",
2498 "6703903964971298549787012499102923063739682910296196688861780721"
2499 "8608820150367734884009371490834517138450159290932430254268769414"
2500 "05973284973216824503042159",
2501 "1"
2502 },
2503 { /* between second and third iteration */
2504 "8908340854353752577419678771330460827942371434853054158622636544"
2505 "8151360109722890949471912566649465436296659601091730745087014189"
2506 "2672764191218875181826063",
2507 "6703903964971298549787012499102923063739682910296196688861780721"
2508 "8608820150367734884009371490834517138450159290932430254268769414"
2509 "05973284973216824503042158",
2510 "6703903964971298549787012499102923063739682910296196688861780721"
2511 "8608820150367734884009371490834517138450159290932430254268769414"
2512 "05973284973216824503042159",
2513 "1"
2514 },
2515 { /* between third and fourth iteration */
2516 "3427446396505596330634350984901719674479522569002785244080234738"
2517 "4288743635435746136297299366444548736533053717416735379073185344"
2518 "26985272974404612945608761",
2519 "6703903964971298549787012499102923063739682910296196688861780721"
2520 "8608820150367734884009371490834517138450159290932430254268769414"
2521 "05973284973216824503042158",
2522 "6703903964971298549787012499102923063739682910296196688861780721"
2523 "8608820150367734884009371490834517138450159290932430254268769414"
2524 "05973284973216824503042159",
2525 "1"
2526 },
2527 { /* between fourth and fifth iteration */
2528 "3472743044917564564078857826111874560045331237315597383869652985"
2529 "6919870028890895988478351133601517365908445058405433832718206902"
2530 "4088133164805266956353542",
2531 "6703903964971298549787012499102923063739682910296196688861780721"
2532 "8608820150367734884009371490834517138450159290932430254268769414"
2533 "05973284973216824503042158",
2534 "6703903964971298549787012499102923063739682910296196688861780721"
2535 "8608820150367734884009371490834517138450159290932430254268769414"
2536 "05973284973216824503042159",
2537 "1"
2538 },
2539 { /* between fifth and sixth iteration */
2540 "3608632990153469264412378349742339216742409743898601587274768025"
2541 "0110772032985643555192767717344946174122842255204082586753499651"
2542 "14483434992887431333675068",
2543 "6703903964971298549787012499102923063739682910296196688861780721"
2544 "8608820150367734884009371490834517138450159290932430254268769414"
2545 "05973284973216824503042158",
2546 "6703903964971298549787012499102923063739682910296196688861780721"
2547 "8608820150367734884009371490834517138450159290932430254268769414"
2548 "05973284973216824503042159",
2549 "1"
2550 },
2551 { /* between sixth and seventh iteration */
2552 "8455374370234070242910508226941981520235709767260723212165264877"
2553 "8689064388017521524568434328264431772644802567028663962962025746"
2554 "9283458217850119569539086",
2555 "6703903964971298549787012499102923063739682910296196688861780721"
2556 "8608820150367734884009371490834517138450159290932430254268769414"
2557 "05973284973216824503042158",
2558 "6703903964971298549787012499102923063739682910296196688861780721"
2559 "8608820150367734884009371490834517138450159290932430254268769414"
2560 "05973284973216824503042159",
2561 "1"
2562 },
2563 { /* between seventh and eighth iteration */
2564 "5155371529688532178421209781159131443543419764974688878527112131"
2565 "7446518205609427412336183157918981038066636807317733319323257603"
2566 "04416292040754017461076359",
2567 "1005585594745694782468051874865438459560952436544429503329267108"
2568 "2791323022555160232601405723625177570767523893639864538140315412"
2569 "108959927459825236754563832",
2570 "1005585594745694782468051874865438459560952436544429503329267108"
2571 "2791323022555160232601405723625177570767523893639864538140315412"
2572 "108959927459825236754563833",
2573 "1"
2574 },
2575 /* test vectors for rsaz_512_srq bug, with rcx/rbx=2 */
2576 { /* between first and second iteration */
2577 "3155666506033786929967309937640790361084670559125912405342594979"
2578 "4345142818528956285490897841406338022378565972533508820577760065"
2579 "58494345853302083699912572",
2580 "6703903964971298549787012499102923063739682910296196688861780721"
2581 "8608820150367734884009371490834517138450159290932430254268769414"
2582 "05973284973216824503042158",
2583 "6703903964971298549787012499102923063739682910296196688861780721"
2584 "8608820150367734884009371490834517138450159290932430254268769414"
2585 "05973284973216824503042159",
2586 "1"
2587 },
2588 { /* between second and third iteration */
2589 "3789819583801342198190405714582958759005991915505282362397087750"
2590 "4213544724644823098843135685133927198668818185338794377239590049"
2591 "41019388529192775771488319",
2592 "6703903964971298549787012499102923063739682910296196688861780721"
2593 "8608820150367734884009371490834517138450159290932430254268769414"
2594 "05973284973216824503042158",
2595 "6703903964971298549787012499102923063739682910296196688861780721"
2596 "8608820150367734884009371490834517138450159290932430254268769414"
2597 "05973284973216824503042159",
2598 "1"
2599 },
2600 { /* between third and forth iteration */
2601 "4695752552040706867080542538786056470322165281761525158189220280"
2602 "4025547447667484759200742764246905647644662050122968912279199065"
2603 "48065034299166336940507214",
2604 "6703903964971298549787012499102923063739682910296196688861780721"
2605 "8608820150367734884009371490834517138450159290932430254268769414"
2606 "05973284973216824503042158",
2607 "6703903964971298549787012499102923063739682910296196688861780721"
2608 "8608820150367734884009371490834517138450159290932430254268769414"
2609 "05973284973216824503042159",
2610 "1"
2611 },
2612 { /* between forth and fifth iteration */
2613 "2159140240970485794188159431017382878636879856244045329971239574"
2614 "8919691133560661162828034323196457386059819832804593989740268964"
2615 "74502911811812651475927076",
2616 "6703903964971298549787012499102923063739682910296196688861780721"
2617 "8608820150367734884009371490834517138450159290932430254268769414"
2618 "05973284973216824503042158",
2619 "6703903964971298549787012499102923063739682910296196688861780721"
2620 "8608820150367734884009371490834517138450159290932430254268769414"
2621 "05973284973216824503042159",
2622 "1"
2623 },
2624 { /* between fifth and sixth iteration */
2625 "5239312332984325668414624633307915097111691815000872662334695514"
2626 "5436533521392362443557163429336808208137221322444780490437871903"
2627 "99972784701334569424519255",
2628 "6703903964971298549787012499102923063739682910296196688861780721"
2629 "8608820150367734884009371490834517138450159290932430254268769414"
2630 "05973284973216824503042158",
2631 "6703903964971298549787012499102923063739682910296196688861780721"
2632 "8608820150367734884009371490834517138450159290932430254268769414"
2633 "05973284973216824503042159",
2634 "1"
2635 },
2636 { /* between sixth and seventh iteration */
2637 "1977953647322612860406858017869125467496941904523063466791308891"
2638 "1172796739058531929470539758361774569875505293428856181093904091"
2639 "33788264851714311303725089",
2640 "6703903964971298549787012499102923063739682910296196688861780721"
2641 "8608820150367734884009371490834517138450159290932430254268769414"
2642 "05973284973216824503042158",
2643 "6703903964971298549787012499102923063739682910296196688861780721"
2644 "8608820150367734884009371490834517138450159290932430254268769414"
2645 "05973284973216824503042159",
2646 "1"
2647 },
2648 { /* between seventh and eighth iteration */
2649 "6456987954117763835533395796948878140715006860263624787492985786"
2650 "8514630216966738305923915688821526449499763719943997120302368211"
2651 "04813318117996225041943964",
2652 "1340780792994259709957402499820584612747936582059239337772356144"
2653 "3721764030073546976801874298166903427690031858186486050853753882"
2654 "811946551499689575296532556",
2655 "1340780792994259709957402499820584612747936582059239337772356144"
2656 "3721764030073546976801874298166903427690031858186486050853753882"
2657 "811946551499689575296532557",
2658 "1"
2659 }
2660 };
2661
test_mod_exp(int i)2662 static int test_mod_exp(int i)
2663 {
2664 const MOD_EXP_TEST *test = &ModExpTests[i];
2665 int res = 0;
2666 BIGNUM* result = NULL;
2667 BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
2668 char *s = NULL;
2669
2670 if (!TEST_ptr(result = BN_new())
2671 || !TEST_true(BN_dec2bn(&base, test->base))
2672 || !TEST_true(BN_dec2bn(&exponent, test->exp))
2673 || !TEST_true(BN_dec2bn(&modulo, test->mod)))
2674 goto err;
2675
2676 if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
2677 goto err;
2678
2679 if (!TEST_ptr(s = BN_bn2dec(result)))
2680 goto err;
2681
2682 if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
2683 goto err;
2684
2685 res = 1;
2686
2687 err:
2688 OPENSSL_free(s);
2689 BN_free(result);
2690 BN_free(base);
2691 BN_free(exponent);
2692 BN_free(modulo);
2693 return res;
2694 }
2695
test_mod_exp_consttime(int i)2696 static int test_mod_exp_consttime(int i)
2697 {
2698 const MOD_EXP_TEST *test = &ModExpTests[i];
2699 int res = 0;
2700 BIGNUM* result = NULL;
2701 BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
2702 char *s = NULL;
2703
2704 if (!TEST_ptr(result = BN_new())
2705 || !TEST_true(BN_dec2bn(&base, test->base))
2706 || !TEST_true(BN_dec2bn(&exponent, test->exp))
2707 || !TEST_true(BN_dec2bn(&modulo, test->mod)))
2708 goto err;
2709
2710 BN_set_flags(base, BN_FLG_CONSTTIME);
2711 BN_set_flags(exponent, BN_FLG_CONSTTIME);
2712 BN_set_flags(modulo, BN_FLG_CONSTTIME);
2713
2714 if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
2715 goto err;
2716
2717 if (!TEST_ptr(s = BN_bn2dec(result)))
2718 goto err;
2719
2720 if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
2721 goto err;
2722
2723 res = 1;
2724
2725 err:
2726 OPENSSL_free(s);
2727 BN_free(result);
2728 BN_free(base);
2729 BN_free(exponent);
2730 BN_free(modulo);
2731 return res;
2732 }
2733
file_test_run(STANZA * s)2734 static int file_test_run(STANZA *s)
2735 {
2736 static const FILETEST filetests[] = {
2737 {"Sum", file_sum},
2738 {"LShift1", file_lshift1},
2739 {"LShift", file_lshift},
2740 {"RShift", file_rshift},
2741 {"Square", file_square},
2742 {"Product", file_product},
2743 {"Quotient", file_quotient},
2744 {"ModMul", file_modmul},
2745 {"ModExp", file_modexp},
2746 {"Exp", file_exp},
2747 {"ModSqrt", file_modsqrt},
2748 {"GCD", file_gcd},
2749 };
2750 int numtests = OSSL_NELEM(filetests);
2751 const FILETEST *tp = filetests;
2752
2753 for ( ; --numtests >= 0; tp++) {
2754 if (findattr(s, tp->name) != NULL) {
2755 if (!tp->func(s)) {
2756 TEST_info("%s:%d: Failed %s test",
2757 s->test_file, s->start, tp->name);
2758 return 0;
2759 }
2760 return 1;
2761 }
2762 }
2763 TEST_info("%s:%d: Unknown test", s->test_file, s->start);
2764 return 0;
2765 }
2766
run_file_tests(int i)2767 static int run_file_tests(int i)
2768 {
2769 STANZA *s = NULL;
2770 char *testfile = test_get_argument(i);
2771 int c;
2772
2773 if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
2774 return 0;
2775 if (!test_start_file(s, testfile)) {
2776 OPENSSL_free(s);
2777 return 0;
2778 }
2779
2780 /* Read test file. */
2781 while (!BIO_eof(s->fp) && test_readstanza(s)) {
2782 if (s->numpairs == 0)
2783 continue;
2784 if (!file_test_run(s))
2785 s->errors++;
2786 s->numtests++;
2787 test_clearstanza(s);
2788 }
2789 test_end_file(s);
2790 c = s->errors;
2791 OPENSSL_free(s);
2792
2793 return c == 0;
2794 }
2795
2796
setup_tests(void)2797 int setup_tests(void)
2798 {
2799 int n = test_get_argument_count();
2800
2801 if (!TEST_ptr(ctx = BN_CTX_new()))
2802 return 0;
2803
2804 if (n == 0) {
2805 ADD_TEST(test_sub);
2806 ADD_TEST(test_div_recip);
2807 ADD_TEST(test_mod);
2808 ADD_TEST(test_modexp_mont5);
2809 ADD_TEST(test_kronecker);
2810 ADD_TEST(test_rand);
2811 ADD_TEST(test_bn2padded);
2812 ADD_TEST(test_dec2bn);
2813 ADD_TEST(test_hex2bn);
2814 ADD_TEST(test_asc2bn);
2815 ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
2816 ADD_TEST(test_negzero);
2817 ADD_TEST(test_badmod);
2818 ADD_TEST(test_expmodzero);
2819 ADD_TEST(test_expmodone);
2820 ADD_ALL_TESTS(test_smallprime, 16);
2821 ADD_ALL_TESTS(test_smallsafeprime, 16);
2822 ADD_TEST(test_swap);
2823 ADD_TEST(test_ctx_consttime_flag);
2824 #ifndef OPENSSL_NO_EC2M
2825 ADD_TEST(test_gf2m_add);
2826 ADD_TEST(test_gf2m_mod);
2827 ADD_TEST(test_gf2m_mul);
2828 ADD_TEST(test_gf2m_sqr);
2829 ADD_TEST(test_gf2m_modinv);
2830 ADD_TEST(test_gf2m_moddiv);
2831 ADD_TEST(test_gf2m_modexp);
2832 ADD_TEST(test_gf2m_modsqrt);
2833 ADD_TEST(test_gf2m_modsolvequad);
2834 #endif
2835 ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
2836 ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
2837 ADD_TEST(test_gcd_prime);
2838 ADD_ALL_TESTS(test_mod_exp, (int)OSSL_NELEM(ModExpTests));
2839 ADD_ALL_TESTS(test_mod_exp_consttime, (int)OSSL_NELEM(ModExpTests));
2840 } else {
2841 ADD_ALL_TESTS(run_file_tests, n);
2842 }
2843 return 1;
2844 }
2845
cleanup_tests(void)2846 void cleanup_tests(void)
2847 {
2848 BN_CTX_free(ctx);
2849 }
2850