1 /* crypto/ec/ectest.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
55 /* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
64 * In addition, Sun covenants to all licensees who provide a reciprocal
65 * covenant with respect to their own patents if any, not to sue under
66 * current and future patent claims necessarily infringed by the making,
67 * using, practicing, selling, offering for sale and/or otherwise
68 * disposing of the Contribution as delivered hereunder
69 * (or portions thereof), provided that such covenant shall not apply:
70 * 1) for code that a licensee deletes from the Contribution;
71 * 2) separates from the Contribution; or
72 * 3) for infringements caused by:
73 * i) the modification of the Contribution or
74 * ii) the combination of the Contribution with other software or
75 * devices where such combination causes the infringement.
77 * The elliptic curve binary polynomial software is originally written by
78 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
89 int main(int argc
, char * argv
[]) { puts("Elliptic curves are disabled."); return 0; }
93 #include <openssl/ec.h>
94 #include <openssl/engine.h>
95 #include <openssl/err.h>
96 #include <openssl/obj_mac.h>
100 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
101 ERR_print_errors_fp(stderr); \
106 static void timings(EC_GROUP
*group
, int multi
, BN_CTX
*ctx
)
111 BIGNUM
*r
[10], *r0
[10];
115 if (s
== NULL
) ABORT
;
117 fprintf(stdout
, "Timings for %d-bit field, ", EC_GROUP_get_degree(group
));
118 if (!EC_GROUP_get_order(group
, s
, ctx
)) ABORT
;
119 fprintf(stdout
, "%d-bit scalars ", (int)BN_num_bits(s
));
122 P
= EC_POINT_new(group
);
123 if (P
== NULL
) ABORT
;
124 EC_POINT_copy(P
, EC_GROUP_get0_generator(group
));
126 for (i
= 0; i
< 10; i
++)
128 if ((r
[i
] = BN_new()) == NULL
) ABORT
;
129 if (!BN_pseudo_rand(r
[i
], BN_num_bits(s
), 0, 0)) ABORT
;
132 if ((r0
[i
] = BN_new()) == NULL
) ABORT
;
133 if (!BN_pseudo_rand(r0
[i
], BN_num_bits(s
), 0, 0)) ABORT
;
138 for (i
= 0; i
< 10; i
++)
140 for (j
= 0; j
< 10; j
++)
142 if (!EC_POINT_mul(group
, P
, r
[i
], multi
? P
: NULL
, multi
? r0
[i
] : NULL
, ctx
)) ABORT
;
145 fprintf(stdout
, "\n");
147 clck
= clock() - clck
;
149 #ifdef CLOCKS_PER_SEC
150 /* "To determine the time in seconds, the value returned
151 * by the clock function should be divided by the value
152 * of the macro CLOCKS_PER_SEC."
156 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
157 * -- cc on NeXTstep/OpenStep */
158 # define UNIT "units"
159 # define CLOCKS_PER_SEC 1
162 fprintf(stdout
, "%i %s in %.2f " UNIT
"\n", i
*j
,
163 multi
? "s*P+t*Q operations" : "point multiplications",
164 (double)clck
/CLOCKS_PER_SEC
);
165 fprintf(stdout
, "average: %.4f " UNIT
"\n", (double)clck
/(CLOCKS_PER_SEC
*i
*j
));
169 for (i
= 0; i
< 10; i
++)
172 if (multi
) BN_free(r0
[i
]);
177 void prime_field_tests()
182 EC_GROUP
*P_192
= NULL
, *P_224
= NULL
, *P_256
= NULL
, *P_384
= NULL
, *P_521
= NULL
;
185 unsigned char buf
[100];
197 if (!p
|| !a
|| !b
) ABORT
;
199 if (!BN_hex2bn(&p
, "17")) ABORT
;
200 if (!BN_hex2bn(&a
, "1")) ABORT
;
201 if (!BN_hex2bn(&b
, "1")) ABORT
;
203 group
= EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
204 * so that the library gets to choose the EC_METHOD */
207 if (!EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
)) ABORT
;
211 tmp
= EC_GROUP_new(EC_GROUP_method_of(group
));
213 if (!EC_GROUP_copy(tmp
, group
));
214 EC_GROUP_free(group
);
218 if (!EC_GROUP_get_curve_GFp(group
, p
, a
, b
, ctx
)) ABORT
;
220 fprintf(stdout
, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
221 BN_print_fp(stdout
, p
);
222 fprintf(stdout
, ")\n a = 0x");
223 BN_print_fp(stdout
, a
);
224 fprintf(stdout
, "\n b = 0x");
225 BN_print_fp(stdout
, b
);
226 fprintf(stdout
, "\n");
228 P
= EC_POINT_new(group
);
229 Q
= EC_POINT_new(group
);
230 R
= EC_POINT_new(group
);
231 if (!P
|| !Q
|| !R
) ABORT
;
233 if (!EC_POINT_set_to_infinity(group
, P
)) ABORT
;
234 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
237 if (!EC_POINT_oct2point(group
, Q
, buf
, 1, ctx
)) ABORT
;
239 if (!EC_POINT_add(group
, P
, P
, Q
, ctx
)) ABORT
;
240 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
245 if (!x
|| !y
|| !z
) ABORT
;
247 if (!BN_hex2bn(&x
, "D")) ABORT
;
248 if (!EC_POINT_set_compressed_coordinates_GFp(group
, Q
, x
, 1, ctx
)) ABORT
;
249 if (!EC_POINT_is_on_curve(group
, Q
, ctx
))
251 if (!EC_POINT_get_affine_coordinates_GFp(group
, Q
, x
, y
, ctx
)) ABORT
;
252 fprintf(stderr
, "Point is not on curve: x = 0x");
253 BN_print_fp(stderr
, x
);
254 fprintf(stderr
, ", y = 0x");
255 BN_print_fp(stderr
, y
);
256 fprintf(stderr
, "\n");
260 fprintf(stdout
, "A cyclic subgroup:\n");
266 if (EC_POINT_is_at_infinity(group
, P
))
267 fprintf(stdout
, " point at infinity\n");
270 if (!EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)) ABORT
;
272 fprintf(stdout
, " x = 0x");
273 BN_print_fp(stdout
, x
);
274 fprintf(stdout
, ", y = 0x");
275 BN_print_fp(stdout
, y
);
276 fprintf(stdout
, "\n");
279 if (!EC_POINT_copy(R
, P
)) ABORT
;
280 if (!EC_POINT_add(group
, P
, P
, Q
, ctx
)) ABORT
;
289 if (!EC_POINTs_make_affine(group
, 2, points
, ctx
)) ABORT
;
294 while (!EC_POINT_is_at_infinity(group
, P
));
296 if (!EC_POINT_add(group
, P
, Q
, R
, ctx
)) ABORT
;
297 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
299 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_COMPRESSED
, buf
, sizeof buf
, ctx
);
301 if (!EC_POINT_oct2point(group
, P
, buf
, len
, ctx
)) ABORT
;
302 if (0 != EC_POINT_cmp(group
, P
, Q
, ctx
)) ABORT
;
303 fprintf(stdout
, "Generator as octect string, compressed form:\n ");
304 for (i
= 0; i
< len
; i
++) fprintf(stdout
, "%02X", buf
[i
]);
306 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_UNCOMPRESSED
, buf
, sizeof buf
, ctx
);
308 if (!EC_POINT_oct2point(group
, P
, buf
, len
, ctx
)) ABORT
;
309 if (0 != EC_POINT_cmp(group
, P
, Q
, ctx
)) ABORT
;
310 fprintf(stdout
, "\nGenerator as octect string, uncompressed form:\n ");
311 for (i
= 0; i
< len
; i
++) fprintf(stdout
, "%02X", buf
[i
]);
313 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_HYBRID
, buf
, sizeof buf
, ctx
);
315 if (!EC_POINT_oct2point(group
, P
, buf
, len
, ctx
)) ABORT
;
316 if (0 != EC_POINT_cmp(group
, P
, Q
, ctx
)) ABORT
;
317 fprintf(stdout
, "\nGenerator as octect string, hybrid form:\n ");
318 for (i
= 0; i
< len
; i
++) fprintf(stdout
, "%02X", buf
[i
]);
320 if (!EC_POINT_get_Jprojective_coordinates_GFp(group
, R
, x
, y
, z
, ctx
)) ABORT
;
321 fprintf(stdout
, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
322 BN_print_fp(stdout
, x
);
323 fprintf(stdout
, ", Y = 0x");
324 BN_print_fp(stdout
, y
);
325 fprintf(stdout
, ", Z = 0x");
326 BN_print_fp(stdout
, z
);
327 fprintf(stdout
, "\n");
329 if (!EC_POINT_invert(group
, P
, ctx
)) ABORT
;
330 if (0 != EC_POINT_cmp(group
, P
, R
, ctx
)) ABORT
;
333 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
335 if (!BN_hex2bn(&p
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT
;
336 if (1 != BN_is_prime(p
, BN_prime_checks
, 0, ctx
, NULL
)) ABORT
;
337 if (!BN_hex2bn(&a
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT
;
338 if (!BN_hex2bn(&b
, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT
;
339 if (!EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
)) ABORT
;
341 if (!BN_hex2bn(&x
, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT
;
342 if (!EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 1, ctx
)) ABORT
;
343 if (!EC_POINT_is_on_curve(group
, P
, ctx
)) ABORT
;
344 if (!BN_hex2bn(&z
, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT
;
345 if (!EC_GROUP_set_generator(group
, P
, z
, BN_value_one())) ABORT
;
347 if (!EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)) ABORT
;
348 fprintf(stdout
, "\nNIST curve P-192 -- Generator:\n x = 0x");
349 BN_print_fp(stdout
, x
);
350 fprintf(stdout
, "\n y = 0x");
351 BN_print_fp(stdout
, y
);
352 fprintf(stdout
, "\n");
353 /* G_y value taken from the standard: */
354 if (!BN_hex2bn(&z
, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT
;
355 if (0 != BN_cmp(y
, z
)) ABORT
;
357 fprintf(stdout
, "verify degree ...");
358 if (EC_GROUP_get_degree(group
) != 192) ABORT
;
359 fprintf(stdout
, " ok\n");
361 fprintf(stdout
, "verify group order ...");
363 if (!EC_GROUP_get_order(group
, z
, ctx
)) ABORT
;
364 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
365 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
366 fprintf(stdout
, ".");
368 if (!EC_GROUP_precompute_mult(group
, ctx
)) ABORT
;
369 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
370 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
371 fprintf(stdout
, " ok\n");
373 if (!(P_192
= EC_GROUP_new(EC_GROUP_method_of(group
)))) ABORT
;
374 if (!EC_GROUP_copy(P_192
, group
)) ABORT
;
377 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
379 if (!BN_hex2bn(&p
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT
;
380 if (1 != BN_is_prime(p
, BN_prime_checks
, 0, ctx
, NULL
)) ABORT
;
381 if (!BN_hex2bn(&a
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT
;
382 if (!BN_hex2bn(&b
, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT
;
383 if (!EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
)) ABORT
;
385 if (!BN_hex2bn(&x
, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT
;
386 if (!EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 0, ctx
)) ABORT
;
387 if (!EC_POINT_is_on_curve(group
, P
, ctx
)) ABORT
;
388 if (!BN_hex2bn(&z
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT
;
389 if (!EC_GROUP_set_generator(group
, P
, z
, BN_value_one())) ABORT
;
391 if (!EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)) ABORT
;
392 fprintf(stdout
, "\nNIST curve P-224 -- Generator:\n x = 0x");
393 BN_print_fp(stdout
, x
);
394 fprintf(stdout
, "\n y = 0x");
395 BN_print_fp(stdout
, y
);
396 fprintf(stdout
, "\n");
397 /* G_y value taken from the standard: */
398 if (!BN_hex2bn(&z
, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT
;
399 if (0 != BN_cmp(y
, z
)) ABORT
;
401 fprintf(stdout
, "verify degree ...");
402 if (EC_GROUP_get_degree(group
) != 224) ABORT
;
403 fprintf(stdout
, " ok\n");
405 fprintf(stdout
, "verify group order ...");
407 if (!EC_GROUP_get_order(group
, z
, ctx
)) ABORT
;
408 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
409 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
410 fprintf(stdout
, ".");
412 if (!EC_GROUP_precompute_mult(group
, ctx
)) ABORT
;
413 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
414 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
415 fprintf(stdout
, " ok\n");
417 if (!(P_224
= EC_GROUP_new(EC_GROUP_method_of(group
)))) ABORT
;
418 if (!EC_GROUP_copy(P_224
, group
)) ABORT
;
421 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
423 if (!BN_hex2bn(&p
, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT
;
424 if (1 != BN_is_prime(p
, BN_prime_checks
, 0, ctx
, NULL
)) ABORT
;
425 if (!BN_hex2bn(&a
, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT
;
426 if (!BN_hex2bn(&b
, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT
;
427 if (!EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
)) ABORT
;
429 if (!BN_hex2bn(&x
, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT
;
430 if (!EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 1, ctx
)) ABORT
;
431 if (!EC_POINT_is_on_curve(group
, P
, ctx
)) ABORT
;
432 if (!BN_hex2bn(&z
, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
433 "84F3B9CAC2FC632551")) ABORT
;
434 if (!EC_GROUP_set_generator(group
, P
, z
, BN_value_one())) ABORT
;
436 if (!EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)) ABORT
;
437 fprintf(stdout
, "\nNIST curve P-256 -- Generator:\n x = 0x");
438 BN_print_fp(stdout
, x
);
439 fprintf(stdout
, "\n y = 0x");
440 BN_print_fp(stdout
, y
);
441 fprintf(stdout
, "\n");
442 /* G_y value taken from the standard: */
443 if (!BN_hex2bn(&z
, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT
;
444 if (0 != BN_cmp(y
, z
)) ABORT
;
446 fprintf(stdout
, "verify degree ...");
447 if (EC_GROUP_get_degree(group
) != 256) ABORT
;
448 fprintf(stdout
, " ok\n");
450 fprintf(stdout
, "verify group order ...");
452 if (!EC_GROUP_get_order(group
, z
, ctx
)) ABORT
;
453 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
454 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
455 fprintf(stdout
, ".");
457 if (!EC_GROUP_precompute_mult(group
, ctx
)) ABORT
;
458 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
459 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
460 fprintf(stdout
, " ok\n");
462 if (!(P_256
= EC_GROUP_new(EC_GROUP_method_of(group
)))) ABORT
;
463 if (!EC_GROUP_copy(P_256
, group
)) ABORT
;
466 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
468 if (!BN_hex2bn(&p
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
469 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT
;
470 if (1 != BN_is_prime(p
, BN_prime_checks
, 0, ctx
, NULL
)) ABORT
;
471 if (!BN_hex2bn(&a
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
472 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT
;
473 if (!BN_hex2bn(&b
, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
474 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT
;
475 if (!EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
)) ABORT
;
477 if (!BN_hex2bn(&x
, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
478 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT
;
479 if (!EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 1, ctx
)) ABORT
;
480 if (!EC_POINT_is_on_curve(group
, P
, ctx
)) ABORT
;
481 if (!BN_hex2bn(&z
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
482 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT
;
483 if (!EC_GROUP_set_generator(group
, P
, z
, BN_value_one())) ABORT
;
485 if (!EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)) ABORT
;
486 fprintf(stdout
, "\nNIST curve P-384 -- Generator:\n x = 0x");
487 BN_print_fp(stdout
, x
);
488 fprintf(stdout
, "\n y = 0x");
489 BN_print_fp(stdout
, y
);
490 fprintf(stdout
, "\n");
491 /* G_y value taken from the standard: */
492 if (!BN_hex2bn(&z
, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
493 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT
;
494 if (0 != BN_cmp(y
, z
)) ABORT
;
496 fprintf(stdout
, "verify degree ...");
497 if (EC_GROUP_get_degree(group
) != 384) ABORT
;
498 fprintf(stdout
, " ok\n");
500 fprintf(stdout
, "verify group order ...");
502 if (!EC_GROUP_get_order(group
, z
, ctx
)) ABORT
;
503 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
504 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
505 fprintf(stdout
, ".");
507 if (!EC_GROUP_precompute_mult(group
, ctx
)) ABORT
;
508 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
509 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
510 fprintf(stdout
, " ok\n");
512 if (!(P_384
= EC_GROUP_new(EC_GROUP_method_of(group
)))) ABORT
;
513 if (!EC_GROUP_copy(P_384
, group
)) ABORT
;
516 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
518 if (!BN_hex2bn(&p
, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
519 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
520 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT
;
521 if (1 != BN_is_prime(p
, BN_prime_checks
, 0, ctx
, NULL
)) ABORT
;
522 if (!BN_hex2bn(&a
, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
523 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
524 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT
;
525 if (!BN_hex2bn(&b
, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
526 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
527 "DF883D2C34F1EF451FD46B503F00")) ABORT
;
528 if (!EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
)) ABORT
;
530 if (!BN_hex2bn(&x
, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
531 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
532 "3C1856A429BF97E7E31C2E5BD66")) ABORT
;
533 if (!EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 0, ctx
)) ABORT
;
534 if (!EC_POINT_is_on_curve(group
, P
, ctx
)) ABORT
;
535 if (!BN_hex2bn(&z
, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
536 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
537 "C9B8899C47AEBB6FB71E91386409")) ABORT
;
538 if (!EC_GROUP_set_generator(group
, P
, z
, BN_value_one())) ABORT
;
540 if (!EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)) ABORT
;
541 fprintf(stdout
, "\nNIST curve P-521 -- Generator:\n x = 0x");
542 BN_print_fp(stdout
, x
);
543 fprintf(stdout
, "\n y = 0x");
544 BN_print_fp(stdout
, y
);
545 fprintf(stdout
, "\n");
546 /* G_y value taken from the standard: */
547 if (!BN_hex2bn(&z
, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
548 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
549 "7086A272C24088BE94769FD16650")) ABORT
;
550 if (0 != BN_cmp(y
, z
)) ABORT
;
552 fprintf(stdout
, "verify degree ...");
553 if (EC_GROUP_get_degree(group
) != 521) ABORT
;
554 fprintf(stdout
, " ok\n");
556 fprintf(stdout
, "verify group order ...");
558 if (!EC_GROUP_get_order(group
, z
, ctx
)) ABORT
;
559 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
560 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
561 fprintf(stdout
, ".");
563 if (!EC_GROUP_precompute_mult(group
, ctx
)) ABORT
;
564 if (!EC_POINT_mul(group
, Q
, z
, NULL
, NULL
, ctx
)) ABORT
;
565 if (!EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
566 fprintf(stdout
, " ok\n");
568 if (!(P_521
= EC_GROUP_new(EC_GROUP_method_of(group
)))) ABORT
;
569 if (!EC_GROUP_copy(P_521
, group
)) ABORT
;
572 /* more tests using the last curve */
574 if (!EC_POINT_copy(Q
, P
)) ABORT
;
575 if (EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
576 if (!EC_POINT_dbl(group
, P
, P
, ctx
)) ABORT
;
577 if (!EC_POINT_is_on_curve(group
, P
, ctx
)) ABORT
;
578 if (!EC_POINT_invert(group
, Q
, ctx
)) ABORT
; /* P = -2Q */
580 if (!EC_POINT_add(group
, R
, P
, Q
, ctx
)) ABORT
;
581 if (!EC_POINT_add(group
, R
, R
, Q
, ctx
)) ABORT
;
582 if (!EC_POINT_is_at_infinity(group
, R
)) ABORT
; /* R = P + 2Q */
585 const EC_POINT
*points
[3];
586 const BIGNUM
*scalars
[3];
588 if (EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
593 if (!BN_add(y
, z
, BN_value_one())) ABORT
;
594 if (BN_is_odd(y
)) ABORT
;
595 if (!BN_rshift1(y
, y
)) ABORT
;
596 scalars
[0] = y
; /* (group order + 1)/2, so y*Q + y*Q = Q */
599 fprintf(stdout
, "combined multiplication ...");
602 /* z is still the group order */
603 if (!EC_POINTs_mul(group
, P
, NULL
, 2, points
, scalars
, ctx
)) ABORT
;
604 if (!EC_POINTs_mul(group
, R
, z
, 2, points
, scalars
, ctx
)) ABORT
;
605 if (0 != EC_POINT_cmp(group
, P
, R
, ctx
)) ABORT
;
606 if (0 != EC_POINT_cmp(group
, R
, Q
, ctx
)) ABORT
;
608 fprintf(stdout
, ".");
611 if (!BN_pseudo_rand(y
, BN_num_bits(y
), 0, 0)) ABORT
;
612 if (!BN_add(z
, z
, y
)) ABORT
;
615 scalars
[1] = z
; /* z = -(order + y) */
617 if (!EC_POINTs_mul(group
, P
, NULL
, 2, points
, scalars
, ctx
)) ABORT
;
618 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
620 fprintf(stdout
, ".");
623 if (!BN_pseudo_rand(x
, BN_num_bits(y
) - 1, 0, 0)) ABORT
;
624 if (!BN_add(z
, x
, y
)) ABORT
;
628 scalars
[2] = z
; /* z = -(x+y) */
630 if (!EC_POINTs_mul(group
, P
, NULL
, 3, points
, scalars
, ctx
)) ABORT
;
631 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
633 fprintf(stdout
, " ok\n\n");
638 timings(P_192
, 0, ctx
);
639 timings(P_192
, 1, ctx
);
640 timings(P_224
, 0, ctx
);
641 timings(P_224
, 1, ctx
);
642 timings(P_256
, 0, ctx
);
643 timings(P_256
, 1, ctx
);
644 timings(P_384
, 0, ctx
);
645 timings(P_384
, 1, ctx
);
646 timings(P_521
, 0, ctx
);
647 timings(P_521
, 1, ctx
);
653 BN_free(p
); BN_free(a
); BN_free(b
);
654 EC_GROUP_free(group
);
658 BN_free(x
); BN_free(y
); BN_free(z
);
660 if (P_192
) EC_GROUP_free(P_192
);
661 if (P_224
) EC_GROUP_free(P_224
);
662 if (P_256
) EC_GROUP_free(P_256
);
663 if (P_384
) EC_GROUP_free(P_384
);
664 if (P_521
) EC_GROUP_free(P_521
);
668 /* Change test based on whether binary point compression is enabled or not. */
669 #ifdef OPENSSL_EC_BIN_PT_COMP
670 #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
671 if (!BN_hex2bn(&x, _x)) ABORT; \
672 if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
673 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
674 if (!BN_hex2bn(&z, _order)) ABORT; \
675 if (!BN_hex2bn(&cof, _cof)) ABORT; \
676 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
677 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
678 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
679 BN_print_fp(stdout, x); \
680 fprintf(stdout, "\n y = 0x"); \
681 BN_print_fp(stdout, y); \
682 fprintf(stdout, "\n"); \
683 /* G_y value taken from the standard: */ \
684 if (!BN_hex2bn(&z, _y)) ABORT; \
685 if (0 != BN_cmp(y, z)) ABORT;
687 #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
688 if (!BN_hex2bn(&x, _x)) ABORT; \
689 if (!BN_hex2bn(&y, _y)) ABORT; \
690 if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
691 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
692 if (!BN_hex2bn(&z, _order)) ABORT; \
693 if (!BN_hex2bn(&cof, _cof)) ABORT; \
694 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
695 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
696 BN_print_fp(stdout, x); \
697 fprintf(stdout, "\n y = 0x"); \
698 BN_print_fp(stdout, y); \
699 fprintf(stdout, "\n");
702 #define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
703 if (!BN_hex2bn(&p, _p)) ABORT; \
704 if (!BN_hex2bn(&a, _a)) ABORT; \
705 if (!BN_hex2bn(&b, _b)) ABORT; \
706 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
707 CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
708 fprintf(stdout, "verify degree ..."); \
709 if (EC_GROUP_get_degree(group) != _degree) ABORT; \
710 fprintf(stdout, " ok\n"); \
711 fprintf(stdout, "verify group order ..."); \
713 if (!EC_GROUP_get_order(group, z, ctx)) ABORT; \
714 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
715 if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
716 fprintf(stdout, "."); \
718 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; \
719 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
720 if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
721 fprintf(stdout, " ok\n"); \
722 if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
723 if (!EC_GROUP_copy(_variable, group)) ABORT;
725 void char2_field_tests()
730 EC_GROUP
*C2_K163
= NULL
, *C2_K233
= NULL
, *C2_K283
= NULL
, *C2_K409
= NULL
, *C2_K571
= NULL
;
731 EC_GROUP
*C2_B163
= NULL
, *C2_B233
= NULL
, *C2_B283
= NULL
, *C2_B409
= NULL
, *C2_B571
= NULL
;
733 BIGNUM
*x
, *y
, *z
, *cof
;
734 unsigned char buf
[100];
746 if (!p
|| !a
|| !b
) ABORT
;
748 if (!BN_hex2bn(&p
, "13")) ABORT
;
749 if (!BN_hex2bn(&a
, "3")) ABORT
;
750 if (!BN_hex2bn(&b
, "1")) ABORT
;
752 group
= EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GFp
753 * so that the library gets to choose the EC_METHOD */
755 if (!EC_GROUP_set_curve_GF2m(group
, p
, a
, b
, ctx
)) ABORT
;
759 tmp
= EC_GROUP_new(EC_GROUP_method_of(group
));
761 if (!EC_GROUP_copy(tmp
, group
));
762 EC_GROUP_free(group
);
766 if (!EC_GROUP_get_curve_GF2m(group
, p
, a
, b
, ctx
)) ABORT
;
768 fprintf(stdout
, "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
769 BN_print_fp(stdout
, p
);
770 fprintf(stdout
, ")\n a = 0x");
771 BN_print_fp(stdout
, a
);
772 fprintf(stdout
, "\n b = 0x");
773 BN_print_fp(stdout
, b
);
774 fprintf(stdout
, "\n");
776 P
= EC_POINT_new(group
);
777 Q
= EC_POINT_new(group
);
778 R
= EC_POINT_new(group
);
779 if (!P
|| !Q
|| !R
) ABORT
;
781 if (!EC_POINT_set_to_infinity(group
, P
)) ABORT
;
782 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
785 if (!EC_POINT_oct2point(group
, Q
, buf
, 1, ctx
)) ABORT
;
787 if (!EC_POINT_add(group
, P
, P
, Q
, ctx
)) ABORT
;
788 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
794 if (!x
|| !y
|| !z
|| !cof
) ABORT
;
796 if (!BN_hex2bn(&x
, "6")) ABORT
;
797 /* Change test based on whether binary point compression is enabled or not. */
798 #ifdef OPENSSL_EC_BIN_PT_COMP
799 if (!EC_POINT_set_compressed_coordinates_GF2m(group
, Q
, x
, 1, ctx
)) ABORT
;
801 if (!BN_hex2bn(&y
, "8")) ABORT
;
802 if (!EC_POINT_set_affine_coordinates_GF2m(group
, Q
, x
, y
, ctx
)) ABORT
;
804 if (!EC_POINT_is_on_curve(group
, Q
, ctx
))
806 /* Change test based on whether binary point compression is enabled or not. */
807 #ifdef OPENSSL_EC_BIN_PT_COMP
808 if (!EC_POINT_get_affine_coordinates_GF2m(group
, Q
, x
, y
, ctx
)) ABORT
;
810 fprintf(stderr
, "Point is not on curve: x = 0x");
811 BN_print_fp(stderr
, x
);
812 fprintf(stderr
, ", y = 0x");
813 BN_print_fp(stderr
, y
);
814 fprintf(stderr
, "\n");
818 fprintf(stdout
, "A cyclic subgroup:\n");
824 if (EC_POINT_is_at_infinity(group
, P
))
825 fprintf(stdout
, " point at infinity\n");
828 if (!EC_POINT_get_affine_coordinates_GF2m(group
, P
, x
, y
, ctx
)) ABORT
;
830 fprintf(stdout
, " x = 0x");
831 BN_print_fp(stdout
, x
);
832 fprintf(stdout
, ", y = 0x");
833 BN_print_fp(stdout
, y
);
834 fprintf(stdout
, "\n");
837 if (!EC_POINT_copy(R
, P
)) ABORT
;
838 if (!EC_POINT_add(group
, P
, P
, Q
, ctx
)) ABORT
;
840 while (!EC_POINT_is_at_infinity(group
, P
));
842 if (!EC_POINT_add(group
, P
, Q
, R
, ctx
)) ABORT
;
843 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
845 /* Change test based on whether binary point compression is enabled or not. */
846 #ifdef OPENSSL_EC_BIN_PT_COMP
847 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_COMPRESSED
, buf
, sizeof buf
, ctx
);
849 if (!EC_POINT_oct2point(group
, P
, buf
, len
, ctx
)) ABORT
;
850 if (0 != EC_POINT_cmp(group
, P
, Q
, ctx
)) ABORT
;
851 fprintf(stdout
, "Generator as octet string, compressed form:\n ");
852 for (i
= 0; i
< len
; i
++) fprintf(stdout
, "%02X", buf
[i
]);
855 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_UNCOMPRESSED
, buf
, sizeof buf
, ctx
);
857 if (!EC_POINT_oct2point(group
, P
, buf
, len
, ctx
)) ABORT
;
858 if (0 != EC_POINT_cmp(group
, P
, Q
, ctx
)) ABORT
;
859 fprintf(stdout
, "\nGenerator as octet string, uncompressed form:\n ");
860 for (i
= 0; i
< len
; i
++) fprintf(stdout
, "%02X", buf
[i
]);
862 /* Change test based on whether binary point compression is enabled or not. */
863 #ifdef OPENSSL_EC_BIN_PT_COMP
864 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_HYBRID
, buf
, sizeof buf
, ctx
);
866 if (!EC_POINT_oct2point(group
, P
, buf
, len
, ctx
)) ABORT
;
867 if (0 != EC_POINT_cmp(group
, P
, Q
, ctx
)) ABORT
;
868 fprintf(stdout
, "\nGenerator as octet string, hybrid form:\n ");
869 for (i
= 0; i
< len
; i
++) fprintf(stdout
, "%02X", buf
[i
]);
872 fprintf(stdout
, "\n");
874 if (!EC_POINT_invert(group
, P
, ctx
)) ABORT
;
875 if (0 != EC_POINT_cmp(group
, P
, R
, ctx
)) ABORT
;
878 /* Curve K-163 (FIPS PUB 186-2, App. 6) */
882 "0800000000000000000000000000000000000000C9",
885 "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
886 "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
888 "04000000000000000000020108A2E0CC0D99F8A5EF",
894 /* Curve B-163 (FIPS PUB 186-2, App. 6) */
898 "0800000000000000000000000000000000000000C9",
900 "020A601907B8C953CA1481EB10512F78744A3205FD",
901 "03F0EBA16286A2D57EA0991168D4994637E8343E36",
902 "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
904 "040000000000000000000292FE77E70C12A4234C33",
910 /* Curve K-233 (FIPS PUB 186-2, App. 6) */
914 "020000000000000000000000000000000000000004000000000000000001",
917 "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
918 "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
920 "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
926 /* Curve B-233 (FIPS PUB 186-2, App. 6) */
930 "020000000000000000000000000000000000000004000000000000000001",
931 "000000000000000000000000000000000000000000000000000000000001",
932 "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
933 "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
934 "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
936 "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
942 /* Curve K-283 (FIPS PUB 186-2, App. 6) */
946 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
949 "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
950 "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
952 "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
958 /* Curve B-283 (FIPS PUB 186-2, App. 6) */
962 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
963 "000000000000000000000000000000000000000000000000000000000000000000000001",
964 "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
965 "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
966 "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
968 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
974 /* Curve K-409 (FIPS PUB 186-2, App. 6) */
978 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
981 "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
982 "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
984 "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
990 /* Curve B-409 (FIPS PUB 186-2, App. 6) */
994 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
995 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
996 "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
997 "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
998 "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
1000 "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
1006 /* Curve K-571 (FIPS PUB 186-2, App. 6) */
1010 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1013 "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
1014 "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
1016 "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
1022 /* Curve B-571 (FIPS PUB 186-2, App. 6) */
1026 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1027 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1028 "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
1029 "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
1030 "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
1032 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
1038 /* more tests using the last curve */
1040 if (!EC_POINT_copy(Q
, P
)) ABORT
;
1041 if (EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
1042 if (!EC_POINT_dbl(group
, P
, P
, ctx
)) ABORT
;
1043 if (!EC_POINT_is_on_curve(group
, P
, ctx
)) ABORT
;
1044 if (!EC_POINT_invert(group
, Q
, ctx
)) ABORT
; /* P = -2Q */
1046 if (!EC_POINT_add(group
, R
, P
, Q
, ctx
)) ABORT
;
1047 if (!EC_POINT_add(group
, R
, R
, Q
, ctx
)) ABORT
;
1048 if (!EC_POINT_is_at_infinity(group
, R
)) ABORT
; /* R = P + 2Q */
1051 const EC_POINT
*points
[3];
1052 const BIGNUM
*scalars
[3];
1054 if (EC_POINT_is_at_infinity(group
, Q
)) ABORT
;
1059 if (!BN_add(y
, z
, BN_value_one())) ABORT
;
1060 if (BN_is_odd(y
)) ABORT
;
1061 if (!BN_rshift1(y
, y
)) ABORT
;
1062 scalars
[0] = y
; /* (group order + 1)/2, so y*Q + y*Q = Q */
1065 fprintf(stdout
, "combined multiplication ...");
1068 /* z is still the group order */
1069 if (!EC_POINTs_mul(group
, P
, NULL
, 2, points
, scalars
, ctx
)) ABORT
;
1070 if (!EC_POINTs_mul(group
, R
, z
, 2, points
, scalars
, ctx
)) ABORT
;
1071 if (0 != EC_POINT_cmp(group
, P
, R
, ctx
)) ABORT
;
1072 if (0 != EC_POINT_cmp(group
, R
, Q
, ctx
)) ABORT
;
1074 fprintf(stdout
, ".");
1077 if (!BN_pseudo_rand(y
, BN_num_bits(y
), 0, 0)) ABORT
;
1078 if (!BN_add(z
, z
, y
)) ABORT
;
1081 scalars
[1] = z
; /* z = -(order + y) */
1083 if (!EC_POINTs_mul(group
, P
, NULL
, 2, points
, scalars
, ctx
)) ABORT
;
1084 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
1086 fprintf(stdout
, ".");
1089 if (!BN_pseudo_rand(x
, BN_num_bits(y
) - 1, 0, 0)) ABORT
;
1090 if (!BN_add(z
, x
, y
)) ABORT
;
1094 scalars
[2] = z
; /* z = -(x+y) */
1096 if (!EC_POINTs_mul(group
, P
, NULL
, 3, points
, scalars
, ctx
)) ABORT
;
1097 if (!EC_POINT_is_at_infinity(group
, P
)) ABORT
;
1099 fprintf(stdout
, " ok\n\n");
1104 timings(C2_K163
, 0, ctx
);
1105 timings(C2_K163
, 1, ctx
);
1106 timings(C2_B163
, 0, ctx
);
1107 timings(C2_B163
, 1, ctx
);
1108 timings(C2_K233
, 0, ctx
);
1109 timings(C2_K233
, 1, ctx
);
1110 timings(C2_B233
, 0, ctx
);
1111 timings(C2_B233
, 1, ctx
);
1112 timings(C2_K283
, 0, ctx
);
1113 timings(C2_K283
, 1, ctx
);
1114 timings(C2_B283
, 0, ctx
);
1115 timings(C2_B283
, 1, ctx
);
1116 timings(C2_K409
, 0, ctx
);
1117 timings(C2_K409
, 1, ctx
);
1118 timings(C2_B409
, 0, ctx
);
1119 timings(C2_B409
, 1, ctx
);
1120 timings(C2_K571
, 0, ctx
);
1121 timings(C2_K571
, 1, ctx
);
1122 timings(C2_B571
, 0, ctx
);
1123 timings(C2_B571
, 1, ctx
);
1129 BN_free(p
); BN_free(a
); BN_free(b
);
1130 EC_GROUP_free(group
);
1134 BN_free(x
); BN_free(y
); BN_free(z
); BN_free(cof
);
1136 if (C2_K163
) EC_GROUP_free(C2_K163
);
1137 if (C2_B163
) EC_GROUP_free(C2_B163
);
1138 if (C2_K233
) EC_GROUP_free(C2_K233
);
1139 if (C2_B233
) EC_GROUP_free(C2_B233
);
1140 if (C2_K283
) EC_GROUP_free(C2_K283
);
1141 if (C2_B283
) EC_GROUP_free(C2_B283
);
1142 if (C2_K409
) EC_GROUP_free(C2_K409
);
1143 if (C2_B409
) EC_GROUP_free(C2_B409
);
1144 if (C2_K571
) EC_GROUP_free(C2_K571
);
1145 if (C2_B571
) EC_GROUP_free(C2_B571
);
1149 static const char rnd_seed
[] = "string to make the random number generator think it has entropy";
1151 int main(int argc
, char *argv
[])
1154 /* enable memory leak checking unless explicitly disabled */
1155 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL
) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
1157 CRYPTO_malloc_debug_init();
1158 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL
);
1162 /* OPENSSL_DEBUG_MEMORY=off */
1163 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
1165 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
1166 ERR_load_crypto_strings();
1168 RAND_seed(rnd_seed
, sizeof rnd_seed
); /* or BN_generate_prime may fail */
1170 prime_field_tests();
1171 char2_field_tests();
1174 CRYPTO_cleanup_all_ex_data();
1176 ERR_remove_state(0);
1177 CRYPTO_mem_leaks_fp(stderr
);