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