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