1 /* crypto/ec/ectest.c */
3 * Originally written by Bodo Moeller for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
58 /* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
67 * The elliptic curve binary polynomial software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
84 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
88 #include <openssl/ec.h>
89 #ifndef OPENSSL_NO_ENGINE
90 #include <openssl/engine.h>
92 #include <openssl/err.h>
93 #include <openssl/obj_mac.h>
94 #include <openssl/objects.h>
98 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
99 ERR_print_errors_fp(stderr); \
103 void prime_field_tests(void);
104 void char2_field_tests(void);
105 void internal_curve_test(void);
108 static void timings(EC_GROUP *group, int multi, BN_CTX *ctx)
113 BIGNUM *r[10], *r0[10];
117 if (s == NULL) ABORT;
119 fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
120 if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
121 fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
124 P = EC_POINT_new(group);
125 if (P == NULL) ABORT;
126 EC_POINT_copy(P, EC_GROUP_get0_generator(group));
128 for (i = 0; i < 10; i++)
130 if ((r[i] = BN_new()) == NULL) ABORT;
131 if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0)) ABORT;
134 if ((r0[i] = BN_new()) == NULL) ABORT;
135 if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0)) ABORT;
140 for (i = 0; i < 10; i++)
142 for (j = 0; j < 10; j++)
144 if (!EC_POINT_mul(group, P, r[i], multi ? P : NULL, multi ? r0[i] : NULL, ctx)) ABORT;
147 fprintf(stdout, "\n");
149 clck = clock() - clck;
151 #ifdef CLOCKS_PER_SEC
152 /* "To determine the time in seconds, the value returned
153 * by the clock function should be divided by the value
154 * of the macro CLOCKS_PER_SEC."
158 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
159 * -- cc on NeXTstep/OpenStep */
160 # define UNIT "units"
161 # define CLOCKS_PER_SEC 1
164 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
165 multi ? "s*P+t*Q operations" : "point multiplications",
166 (double)clck/CLOCKS_PER_SEC);
167 fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
171 for (i = 0; i < 10; i++)
174 if (multi) BN_free(r0[i]);
179 void prime_field_tests()
184 EC_GROUP *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
187 unsigned char buf[100];
199 if (!p || !a || !b) ABORT;
201 if (!BN_hex2bn(&p, "17")) ABORT;
202 if (!BN_hex2bn(&a, "1")) ABORT;
203 if (!BN_hex2bn(&b, "1")) ABORT;
205 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
206 * so that the library gets to choose the EC_METHOD */
209 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
213 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
215 if (!EC_GROUP_copy(tmp, group));
216 EC_GROUP_free(group);
220 if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
222 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
223 BN_print_fp(stdout, p);
224 fprintf(stdout, ")\n a = 0x");
225 BN_print_fp(stdout, a);
226 fprintf(stdout, "\n b = 0x");
227 BN_print_fp(stdout, b);
228 fprintf(stdout, "\n");
230 P = EC_POINT_new(group);
231 Q = EC_POINT_new(group);
232 R = EC_POINT_new(group);
233 if (!P || !Q || !R) ABORT;
235 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
236 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
239 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
241 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
242 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
247 if (!x || !y || !z) ABORT;
249 if (!BN_hex2bn(&x, "D")) ABORT;
250 if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
251 if (!EC_POINT_is_on_curve(group, Q, ctx))
253 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
254 fprintf(stderr, "Point is not on curve: x = 0x");
255 BN_print_fp(stderr, x);
256 fprintf(stderr, ", y = 0x");
257 BN_print_fp(stderr, y);
258 fprintf(stderr, "\n");
262 fprintf(stdout, "A cyclic subgroup:\n");
268 if (EC_POINT_is_at_infinity(group, P))
269 fprintf(stdout, " point at infinity\n");
272 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
274 fprintf(stdout, " x = 0x");
275 BN_print_fp(stdout, x);
276 fprintf(stdout, ", y = 0x");
277 BN_print_fp(stdout, y);
278 fprintf(stdout, "\n");
281 if (!EC_POINT_copy(R, P)) ABORT;
282 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
291 if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
296 while (!EC_POINT_is_at_infinity(group, P));
298 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
299 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
301 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
303 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
304 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
305 fprintf(stdout, "Generator as octect string, compressed form:\n ");
306 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
308 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
310 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
311 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
312 fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n ");
313 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
315 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
317 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
318 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
319 fprintf(stdout, "\nGenerator as octect string, hybrid form:\n ");
320 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
322 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
323 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
324 BN_print_fp(stdout, x);
325 fprintf(stdout, ", Y = 0x");
326 BN_print_fp(stdout, y);
327 fprintf(stdout, ", Z = 0x");
328 BN_print_fp(stdout, z);
329 fprintf(stdout, "\n");
331 if (!EC_POINT_invert(group, P, ctx)) ABORT;
332 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
335 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
337 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
338 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
339 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
340 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
341 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
343 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
344 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
345 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
346 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
347 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
349 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
350 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
351 BN_print_fp(stdout, x);
352 fprintf(stdout, "\n y = 0x");
353 BN_print_fp(stdout, y);
354 fprintf(stdout, "\n");
355 /* G_y value taken from the standard: */
356 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
357 if (0 != BN_cmp(y, z)) ABORT;
359 fprintf(stdout, "verify degree ...");
360 if (EC_GROUP_get_degree(group) != 192) ABORT;
361 fprintf(stdout, " ok\n");
363 fprintf(stdout, "verify group order ...");
365 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
366 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
367 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
368 fprintf(stdout, ".");
370 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
371 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
372 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
373 fprintf(stdout, " ok\n");
375 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
376 if (!EC_GROUP_copy(P_192, group)) ABORT;
379 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
381 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
382 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
383 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
384 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
385 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
387 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
388 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
389 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
390 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
391 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
393 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
394 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
395 BN_print_fp(stdout, x);
396 fprintf(stdout, "\n y = 0x");
397 BN_print_fp(stdout, y);
398 fprintf(stdout, "\n");
399 /* G_y value taken from the standard: */
400 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
401 if (0 != BN_cmp(y, z)) ABORT;
403 fprintf(stdout, "verify degree ...");
404 if (EC_GROUP_get_degree(group) != 224) ABORT;
405 fprintf(stdout, " ok\n");
407 fprintf(stdout, "verify group order ...");
409 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
410 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
411 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
412 fprintf(stdout, ".");
414 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
415 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
416 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
417 fprintf(stdout, " ok\n");
419 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
420 if (!EC_GROUP_copy(P_224, group)) ABORT;
423 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
425 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
426 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
427 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
428 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
429 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
431 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
432 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
433 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
434 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
435 "84F3B9CAC2FC632551")) ABORT;
436 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
438 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
439 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
440 BN_print_fp(stdout, x);
441 fprintf(stdout, "\n y = 0x");
442 BN_print_fp(stdout, y);
443 fprintf(stdout, "\n");
444 /* G_y value taken from the standard: */
445 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
446 if (0 != BN_cmp(y, z)) ABORT;
448 fprintf(stdout, "verify degree ...");
449 if (EC_GROUP_get_degree(group) != 256) ABORT;
450 fprintf(stdout, " ok\n");
452 fprintf(stdout, "verify group order ...");
454 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
455 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
456 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
457 fprintf(stdout, ".");
459 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
460 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
461 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
462 fprintf(stdout, " ok\n");
464 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
465 if (!EC_GROUP_copy(P_256, group)) ABORT;
468 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
470 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
471 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
472 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
473 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
474 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
475 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
476 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
477 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
479 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
480 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
481 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
482 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
483 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
484 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
485 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
487 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
488 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
489 BN_print_fp(stdout, x);
490 fprintf(stdout, "\n y = 0x");
491 BN_print_fp(stdout, y);
492 fprintf(stdout, "\n");
493 /* G_y value taken from the standard: */
494 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
495 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
496 if (0 != BN_cmp(y, z)) ABORT;
498 fprintf(stdout, "verify degree ...");
499 if (EC_GROUP_get_degree(group) != 384) ABORT;
500 fprintf(stdout, " ok\n");
502 fprintf(stdout, "verify group order ...");
504 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
505 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
506 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
507 fprintf(stdout, ".");
509 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
510 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
511 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
512 fprintf(stdout, " ok\n");
514 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
515 if (!EC_GROUP_copy(P_384, group)) ABORT;
518 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
520 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
521 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
522 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
523 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
524 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
525 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
526 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
527 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
528 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
529 "DF883D2C34F1EF451FD46B503F00")) ABORT;
530 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
532 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
533 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
534 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
535 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
536 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
537 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
538 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
539 "C9B8899C47AEBB6FB71E91386409")) ABORT;
540 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
542 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
543 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
544 BN_print_fp(stdout, x);
545 fprintf(stdout, "\n y = 0x");
546 BN_print_fp(stdout, y);
547 fprintf(stdout, "\n");
548 /* G_y value taken from the standard: */
549 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
550 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
551 "7086A272C24088BE94769FD16650")) ABORT;
552 if (0 != BN_cmp(y, z)) ABORT;
554 fprintf(stdout, "verify degree ...");
555 if (EC_GROUP_get_degree(group) != 521) ABORT;
556 fprintf(stdout, " ok\n");
558 fprintf(stdout, "verify group order ...");
560 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
561 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
562 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
563 fprintf(stdout, ".");
565 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
566 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
567 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
568 fprintf(stdout, " ok\n");
570 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
571 if (!EC_GROUP_copy(P_521, group)) ABORT;
574 /* more tests using the last curve */
576 if (!EC_POINT_copy(Q, P)) ABORT;
577 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
578 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
579 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
580 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
582 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
583 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
584 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
587 const EC_POINT *points[3];
588 const BIGNUM *scalars[3];
590 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
595 if (!BN_add(y, z, BN_value_one())) ABORT;
596 if (BN_is_odd(y)) ABORT;
597 if (!BN_rshift1(y, y)) ABORT;
598 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
601 fprintf(stdout, "combined multiplication ...");
604 /* z is still the group order */
605 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
606 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
607 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
608 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
610 fprintf(stdout, ".");
613 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
614 if (!BN_add(z, z, y)) ABORT;
617 scalars[1] = z; /* z = -(order + y) */
619 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
620 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
622 fprintf(stdout, ".");
625 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
626 if (!BN_add(z, x, y)) ABORT;
630 scalars[2] = z; /* z = -(x+y) */
632 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
633 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
635 fprintf(stdout, " ok\n\n");
640 timings(P_192, 0, ctx);
641 timings(P_192, 1, ctx);
642 timings(P_224, 0, ctx);
643 timings(P_224, 1, ctx);
644 timings(P_256, 0, ctx);
645 timings(P_256, 1, ctx);
646 timings(P_384, 0, ctx);
647 timings(P_384, 1, ctx);
648 timings(P_521, 0, ctx);
649 timings(P_521, 1, ctx);
655 BN_free(p); BN_free(a); BN_free(b);
656 EC_GROUP_free(group);
660 BN_free(x); BN_free(y); BN_free(z);
662 if (P_192) EC_GROUP_free(P_192);
663 if (P_224) EC_GROUP_free(P_224);
664 if (P_256) EC_GROUP_free(P_256);
665 if (P_384) EC_GROUP_free(P_384);
666 if (P_521) EC_GROUP_free(P_521);
670 /* Change test based on whether binary point compression is enabled or not. */
671 #ifdef OPENSSL_EC_BIN_PT_COMP
672 #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
673 if (!BN_hex2bn(&x, _x)) ABORT; \
674 if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
675 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
676 if (!BN_hex2bn(&z, _order)) ABORT; \
677 if (!BN_hex2bn(&cof, _cof)) ABORT; \
678 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
679 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
680 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
681 BN_print_fp(stdout, x); \
682 fprintf(stdout, "\n y = 0x"); \
683 BN_print_fp(stdout, y); \
684 fprintf(stdout, "\n"); \
685 /* G_y value taken from the standard: */ \
686 if (!BN_hex2bn(&z, _y)) ABORT; \
687 if (0 != BN_cmp(y, z)) ABORT;
689 #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
690 if (!BN_hex2bn(&x, _x)) ABORT; \
691 if (!BN_hex2bn(&y, _y)) ABORT; \
692 if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
693 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
694 if (!BN_hex2bn(&z, _order)) ABORT; \
695 if (!BN_hex2bn(&cof, _cof)) ABORT; \
696 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
697 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
698 BN_print_fp(stdout, x); \
699 fprintf(stdout, "\n y = 0x"); \
700 BN_print_fp(stdout, y); \
701 fprintf(stdout, "\n");
704 #define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
705 if (!BN_hex2bn(&p, _p)) ABORT; \
706 if (!BN_hex2bn(&a, _a)) ABORT; \
707 if (!BN_hex2bn(&b, _b)) ABORT; \
708 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
709 CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
710 fprintf(stdout, "verify degree ..."); \
711 if (EC_GROUP_get_degree(group) != _degree) ABORT; \
712 fprintf(stdout, " ok\n"); \
713 fprintf(stdout, "verify group order ..."); \
715 if (!EC_GROUP_get_order(group, z, ctx)) ABORT; \
716 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
717 if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
718 fprintf(stdout, "."); \
720 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; \
721 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
722 if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
723 fprintf(stdout, " ok\n"); \
724 if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
725 if (!EC_GROUP_copy(_variable, group)) ABORT;
727 void char2_field_tests()
732 EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 = NULL, *C2_K571 = NULL;
733 EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 = NULL, *C2_B571 = NULL;
735 BIGNUM *x, *y, *z, *cof;
736 unsigned char buf[100];
748 if (!p || !a || !b) ABORT;
750 if (!BN_hex2bn(&p, "13")) ABORT;
751 if (!BN_hex2bn(&a, "3")) ABORT;
752 if (!BN_hex2bn(&b, "1")) ABORT;
754 group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
755 * so that the library gets to choose the EC_METHOD */
757 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
761 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
763 if (!EC_GROUP_copy(tmp, group));
764 EC_GROUP_free(group);
768 if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)) ABORT;
770 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
771 BN_print_fp(stdout, p);
772 fprintf(stdout, ")\n a = 0x");
773 BN_print_fp(stdout, a);
774 fprintf(stdout, "\n b = 0x");
775 BN_print_fp(stdout, b);
776 fprintf(stdout, "\n(0x... means binary polynomial)\n");
778 P = EC_POINT_new(group);
779 Q = EC_POINT_new(group);
780 R = EC_POINT_new(group);
781 if (!P || !Q || !R) ABORT;
783 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
784 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
787 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
789 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
790 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
796 if (!x || !y || !z || !cof) ABORT;
798 if (!BN_hex2bn(&x, "6")) ABORT;
799 /* Change test based on whether binary point compression is enabled or not. */
800 #ifdef OPENSSL_EC_BIN_PT_COMP
801 if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx)) ABORT;
803 if (!BN_hex2bn(&y, "8")) ABORT;
804 if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
806 if (!EC_POINT_is_on_curve(group, Q, ctx))
808 /* Change test based on whether binary point compression is enabled or not. */
809 #ifdef OPENSSL_EC_BIN_PT_COMP
810 if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
812 fprintf(stderr, "Point is not on curve: x = 0x");
813 BN_print_fp(stderr, x);
814 fprintf(stderr, ", y = 0x");
815 BN_print_fp(stderr, y);
816 fprintf(stderr, "\n");
820 fprintf(stdout, "A cyclic subgroup:\n");
826 if (EC_POINT_is_at_infinity(group, P))
827 fprintf(stdout, " point at infinity\n");
830 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT;
832 fprintf(stdout, " x = 0x");
833 BN_print_fp(stdout, x);
834 fprintf(stdout, ", y = 0x");
835 BN_print_fp(stdout, y);
836 fprintf(stdout, "\n");
839 if (!EC_POINT_copy(R, P)) ABORT;
840 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
842 while (!EC_POINT_is_at_infinity(group, P));
844 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
845 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
847 /* Change test based on whether binary point compression is enabled or not. */
848 #ifdef OPENSSL_EC_BIN_PT_COMP
849 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
851 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
852 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
853 fprintf(stdout, "Generator as octet string, compressed form:\n ");
854 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
857 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
859 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
860 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
861 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
862 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
864 /* Change test based on whether binary point compression is enabled or not. */
865 #ifdef OPENSSL_EC_BIN_PT_COMP
866 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
868 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
869 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
870 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
871 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
874 fprintf(stdout, "\n");
876 if (!EC_POINT_invert(group, P, ctx)) ABORT;
877 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
880 /* Curve K-163 (FIPS PUB 186-2, App. 6) */
884 "0800000000000000000000000000000000000000C9",
887 "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
888 "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
890 "04000000000000000000020108A2E0CC0D99F8A5EF",
896 /* Curve B-163 (FIPS PUB 186-2, App. 6) */
900 "0800000000000000000000000000000000000000C9",
902 "020A601907B8C953CA1481EB10512F78744A3205FD",
903 "03F0EBA16286A2D57EA0991168D4994637E8343E36",
904 "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
906 "040000000000000000000292FE77E70C12A4234C33",
912 /* Curve K-233 (FIPS PUB 186-2, App. 6) */
916 "020000000000000000000000000000000000000004000000000000000001",
919 "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
920 "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
922 "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
928 /* Curve B-233 (FIPS PUB 186-2, App. 6) */
932 "020000000000000000000000000000000000000004000000000000000001",
933 "000000000000000000000000000000000000000000000000000000000001",
934 "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
935 "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
936 "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
938 "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
944 /* Curve K-283 (FIPS PUB 186-2, App. 6) */
948 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
951 "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
952 "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
954 "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
960 /* Curve B-283 (FIPS PUB 186-2, App. 6) */
964 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
965 "000000000000000000000000000000000000000000000000000000000000000000000001",
966 "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
967 "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
968 "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
970 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
976 /* Curve K-409 (FIPS PUB 186-2, App. 6) */
980 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
983 "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
984 "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
986 "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
992 /* Curve B-409 (FIPS PUB 186-2, App. 6) */
996 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
997 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
998 "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
999 "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
1000 "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
1002 "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
1008 /* Curve K-571 (FIPS PUB 186-2, App. 6) */
1012 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1015 "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
1016 "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
1018 "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
1024 /* Curve B-571 (FIPS PUB 186-2, App. 6) */
1028 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1029 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1030 "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
1031 "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
1032 "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
1034 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
1040 /* more tests using the last curve */
1042 if (!EC_POINT_copy(Q, P)) ABORT;
1043 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1044 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
1045 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
1046 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
1048 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
1049 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
1050 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
1053 const EC_POINT *points[3];
1054 const BIGNUM *scalars[3];
1056 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1061 if (!BN_add(y, z, BN_value_one())) ABORT;
1062 if (BN_is_odd(y)) ABORT;
1063 if (!BN_rshift1(y, y)) ABORT;
1064 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
1067 fprintf(stdout, "combined multiplication ...");
1070 /* z is still the group order */
1071 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1072 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
1073 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
1074 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
1076 fprintf(stdout, ".");
1079 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
1080 if (!BN_add(z, z, y)) ABORT;
1083 scalars[1] = z; /* z = -(order + y) */
1085 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1086 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1088 fprintf(stdout, ".");
1091 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
1092 if (!BN_add(z, x, y)) ABORT;
1096 scalars[2] = z; /* z = -(x+y) */
1098 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
1099 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1101 fprintf(stdout, " ok\n\n");
1106 timings(C2_K163, 0, ctx);
1107 timings(C2_K163, 1, ctx);
1108 timings(C2_B163, 0, ctx);
1109 timings(C2_B163, 1, ctx);
1110 timings(C2_K233, 0, ctx);
1111 timings(C2_K233, 1, ctx);
1112 timings(C2_B233, 0, ctx);
1113 timings(C2_B233, 1, ctx);
1114 timings(C2_K283, 0, ctx);
1115 timings(C2_K283, 1, ctx);
1116 timings(C2_B283, 0, ctx);
1117 timings(C2_B283, 1, ctx);
1118 timings(C2_K409, 0, ctx);
1119 timings(C2_K409, 1, ctx);
1120 timings(C2_B409, 0, ctx);
1121 timings(C2_B409, 1, ctx);
1122 timings(C2_K571, 0, ctx);
1123 timings(C2_K571, 1, ctx);
1124 timings(C2_B571, 0, ctx);
1125 timings(C2_B571, 1, ctx);
1131 BN_free(p); BN_free(a); BN_free(b);
1132 EC_GROUP_free(group);
1136 BN_free(x); BN_free(y); BN_free(z); BN_free(cof);
1138 if (C2_K163) EC_GROUP_free(C2_K163);
1139 if (C2_B163) EC_GROUP_free(C2_B163);
1140 if (C2_K233) EC_GROUP_free(C2_K233);
1141 if (C2_B233) EC_GROUP_free(C2_B233);
1142 if (C2_K283) EC_GROUP_free(C2_K283);
1143 if (C2_B283) EC_GROUP_free(C2_B283);
1144 if (C2_K409) EC_GROUP_free(C2_K409);
1145 if (C2_B409) EC_GROUP_free(C2_B409);
1146 if (C2_K571) EC_GROUP_free(C2_K571);
1147 if (C2_B571) EC_GROUP_free(C2_B571);
1151 void internal_curve_test(void)
1153 EC_builtin_curve *curves = NULL;
1154 size_t crv_len = 0, n = 0;
1157 crv_len = EC_get_builtin_curves(NULL, 0);
1159 curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
1164 if (!EC_get_builtin_curves(curves, crv_len))
1166 OPENSSL_free(curves);
1170 fprintf(stdout, "testing internal curves: ");
1172 for (n = 0; n < crv_len; n++)
1174 EC_GROUP *group = NULL;
1175 int nid = curves[n].nid;
1176 if ((group = EC_GROUP_new_by_nid(nid)) == NULL)
1179 fprintf(stdout, "\nEC_GROUP_new_by_nid() failed with"
1180 " curve %s\n", OBJ_nid2sn(nid));
1181 /* try next curve */
1184 if (!EC_GROUP_check(group, NULL))
1187 fprintf(stdout, "\nEC_GROUP_check() failed with"
1188 " curve %s\n", OBJ_nid2sn(nid));
1189 EC_GROUP_free(group);
1190 /* try the next curve */
1193 fprintf(stdout, ".");
1195 EC_GROUP_free(group);
1198 fprintf(stdout, " ok\n");
1200 fprintf(stdout, " failed\n");
1201 OPENSSL_free(curves);
1205 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1207 int main(int argc, char *argv[])
1210 /* enable memory leak checking unless explicitly disabled */
1211 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
1213 CRYPTO_malloc_debug_init();
1214 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1218 /* OPENSSL_DEBUG_MEMORY=off */
1219 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
1221 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1222 ERR_load_crypto_strings();
1224 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
1226 prime_field_tests();
1228 char2_field_tests();
1229 /* test the internal curves */
1230 internal_curve_test();
1232 #ifndef OPENSSL_NO_ENGINE
1235 CRYPTO_cleanup_all_ex_data();
1237 ERR_remove_state(0);
1238 CRYPTO_mem_leaks_fp(stderr);