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