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