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