1 /* $OpenBSD: ectest.c,v 1.24 2024/10/18 19:55:34 tb Exp $ */
2 /*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
5 /* ====================================================================
6 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
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/)"
24 *
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.
29 *
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.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
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 * ====================================================================
52 *
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).
56 *
57 */
58 /* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
66 *
67 * The elliptic curve binary polynomial software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <time.h>
76
77 #include <openssl/ec.h>
78 #include <openssl/err.h>
79 #include <openssl/obj_mac.h>
80 #include <openssl/objects.h>
81 #include <openssl/bn.h>
82 #include <openssl/opensslconf.h>
83
84 #define ABORT do { \
85 fflush(stdout); \
86 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
87 ERR_print_errors_fp(stderr); \
88 exit(1); \
89 } while (0)
90
91 #define TIMING_BASE_PT 0
92 #define TIMING_RAND_PT 1
93 #define TIMING_SIMUL 2
94
95 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
96 const EC_POINT *point, BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx);
97
98 /* test multiplication with group order, long and negative scalars */
99 static void
group_order_tests(EC_GROUP * group)100 group_order_tests(EC_GROUP *group)
101 {
102 BIGNUM *n1, *n2, *order;
103 EC_POINT *P = EC_POINT_new(group);
104 EC_POINT *Q = EC_POINT_new(group);
105 BN_CTX *ctx;
106
107 if ((ctx = BN_CTX_new()) == NULL)
108 ABORT;
109
110 if ((n1 = BN_new()) == NULL)
111 ABORT;
112 if ((n2 = BN_new()) == NULL)
113 ABORT;
114 if ((order = BN_new()) == NULL)
115 ABORT;
116 fprintf(stdout, "verify group order ...");
117 fflush(stdout);
118 if (!EC_GROUP_get_order(group, order, ctx))
119 ABORT;
120 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
121 ABORT;
122 if (!EC_POINT_is_at_infinity(group, Q))
123 ABORT;
124 fprintf(stdout, ".");
125 fflush(stdout);
126 if (!EC_GROUP_precompute_mult(group, ctx))
127 ABORT;
128 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
129 ABORT;
130 if (!EC_POINT_is_at_infinity(group, Q))
131 ABORT;
132 fprintf(stdout, " ok\n");
133 fprintf(stdout, "long/negative scalar tests ... ");
134 /* XXX - switch back to BN_one() after next bump. */
135 if (!BN_set_word(n1, 1))
136 ABORT;
137 /* n1 = 1 - order */
138 if (!BN_sub(n1, n1, order))
139 ABORT;
140 if (!EC_POINT_mul(group, Q, NULL, P, n1, ctx))
141 ABORT;
142 if (0 != EC_POINT_cmp(group, Q, P, ctx))
143 ABORT;
144 /* n2 = 1 + order */
145 if (!BN_add(n2, order, BN_value_one()))
146 ABORT;
147 if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
148 ABORT;
149 if (0 != EC_POINT_cmp(group, Q, P, ctx))
150 ABORT;
151 /* n2 = (1 - order) * (1 + order) */
152 if (!BN_mul(n2, n1, n2, ctx))
153 ABORT;
154 if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
155 ABORT;
156 if (0 != EC_POINT_cmp(group, Q, P, ctx))
157 ABORT;
158 fprintf(stdout, "ok\n");
159 EC_POINT_free(P);
160 EC_POINT_free(Q);
161 BN_free(n1);
162 BN_free(n2);
163 BN_free(order);
164 BN_CTX_free(ctx);
165 }
166
167 static void
prime_field_tests(void)168 prime_field_tests(void)
169 {
170 BN_CTX *ctx = NULL;
171 BIGNUM *p, *a, *b;
172 EC_GROUP *group;
173 EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
174 EC_POINT *P, *Q, *R;
175 BIGNUM *x, *y, *z;
176 unsigned char buf[100];
177 size_t i, len;
178 int k;
179
180 ctx = BN_CTX_new();
181 if (!ctx)
182 ABORT;
183
184 p = BN_new();
185 a = BN_new();
186 b = BN_new();
187 if (!p || !a || !b)
188 ABORT;
189
190 if (!BN_hex2bn(&p, "17"))
191 ABORT;
192 if (!BN_hex2bn(&a, "1"))
193 ABORT;
194 if (!BN_hex2bn(&b, "1"))
195 ABORT;
196
197 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
198 * so that the library gets to choose the EC_METHOD */
199 if (!group)
200 ABORT;
201
202 if (!EC_GROUP_set_curve(group, p, a, b, ctx))
203 ABORT;
204
205 {
206 EC_GROUP *tmp;
207 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
208 if (!tmp)
209 ABORT;
210 if (!EC_GROUP_copy(tmp, group))
211 ABORT;
212 EC_GROUP_free(group);
213 group = tmp;
214 }
215
216 if (!EC_GROUP_get_curve(group, p, a, b, ctx))
217 ABORT;
218
219 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
220 BN_print_fp(stdout, p);
221 fprintf(stdout, ")\n a = 0x");
222 BN_print_fp(stdout, a);
223 fprintf(stdout, "\n b = 0x");
224 BN_print_fp(stdout, b);
225 fprintf(stdout, "\n");
226
227 P = EC_POINT_new(group);
228 Q = EC_POINT_new(group);
229 R = EC_POINT_new(group);
230 if (!P || !Q || !R)
231 ABORT;
232
233 if (!EC_POINT_set_to_infinity(group, P))
234 ABORT;
235 if (!EC_POINT_is_at_infinity(group, P))
236 ABORT;
237
238 buf[0] = 0;
239 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
240 ABORT;
241
242 if (!EC_POINT_add(group, P, P, Q, ctx))
243 ABORT;
244 if (!EC_POINT_is_at_infinity(group, P))
245 ABORT;
246
247 x = BN_new();
248 y = BN_new();
249 z = BN_new();
250 if (!x || !y || !z)
251 ABORT;
252
253 if (!BN_hex2bn(&x, "D"))
254 ABORT;
255 if (!EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx))
256 ABORT;
257 if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
258 if (!EC_POINT_get_affine_coordinates(group, Q, x, y, ctx))
259 ABORT;
260 fprintf(stderr, "Point is not on curve: x = 0x");
261 BN_print_fp(stderr, x);
262 fprintf(stderr, ", y = 0x");
263 BN_print_fp(stderr, y);
264 fprintf(stderr, "\n");
265 ABORT;
266 }
267
268 fprintf(stdout, "A cyclic subgroup:\n");
269 k = 100;
270 do {
271 if (k-- == 0)
272 ABORT;
273
274 if (EC_POINT_is_at_infinity(group, P))
275 fprintf(stdout, " point at infinity\n");
276 else {
277 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx))
278 ABORT;
279
280 fprintf(stdout, " x = 0x");
281 BN_print_fp(stdout, x);
282 fprintf(stdout, ", y = 0x");
283 BN_print_fp(stdout, y);
284 fprintf(stdout, "\n");
285 }
286
287 if (!EC_POINT_copy(R, P))
288 ABORT;
289 if (!EC_POINT_add(group, P, P, Q, ctx))
290 ABORT;
291 } while (!EC_POINT_is_at_infinity(group, P));
292
293 if (!EC_POINT_add(group, P, Q, R, ctx))
294 ABORT;
295 if (!EC_POINT_is_at_infinity(group, P))
296 ABORT;
297
298 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
299 if (len == 0)
300 ABORT;
301 if (!EC_POINT_oct2point(group, P, buf, len, ctx))
302 ABORT;
303 if (0 != EC_POINT_cmp(group, P, Q, ctx))
304 ABORT;
305 fprintf(stdout, "Generator as octet string, compressed form:\n ");
306 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
307
308 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
309 if (len == 0)
310 ABORT;
311 if (!EC_POINT_oct2point(group, P, buf, len, ctx))
312 ABORT;
313 if (0 != EC_POINT_cmp(group, P, Q, ctx))
314 ABORT;
315 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
316 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
317
318 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
319 if (len == 0)
320 ABORT;
321 if (!EC_POINT_oct2point(group, P, buf, len, ctx))
322 ABORT;
323 if (0 != EC_POINT_cmp(group, P, Q, ctx))
324 ABORT;
325 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
326 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
327
328 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx))
329 ABORT;
330 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
331 BN_print_fp(stdout, x);
332 fprintf(stdout, ", Y = 0x");
333 BN_print_fp(stdout, y);
334 fprintf(stdout, ", Z = 0x");
335 BN_print_fp(stdout, z);
336 fprintf(stdout, "\n");
337
338 if (!EC_POINT_invert(group, P, ctx))
339 ABORT;
340 if (0 != EC_POINT_cmp(group, P, R, ctx))
341 ABORT;
342
343
344 /* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000)
345 * -- not a NIST curve, but commonly used */
346
347 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
348 ABORT;
349 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
350 ABORT;
351 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
352 ABORT;
353 if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45"))
354 ABORT;
355 if (!EC_GROUP_set_curve(group, p, a, b, ctx))
356 ABORT;
357
358 if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82"))
359 ABORT;
360 if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32"))
361 ABORT;
362 if (!EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
363 ABORT;
364 if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
365 ABORT;
366 if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257"))
367 ABORT;
368 if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
369 ABORT;
370
371 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx))
372 ABORT;
373 fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
374 BN_print_fp(stdout, x);
375 fprintf(stdout, "\n y = 0x");
376 BN_print_fp(stdout, y);
377 fprintf(stdout, "\n");
378 /* G_y value taken from the standard: */
379 if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32"))
380 ABORT;
381 if (0 != BN_cmp(y, z))
382 ABORT;
383
384 fprintf(stdout, "verify degree ...");
385 if (EC_GROUP_get_degree(group) != 160)
386 ABORT;
387 fprintf(stdout, " ok\n");
388
389 group_order_tests(group);
390
391 if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group))))
392 ABORT;
393 if (!EC_GROUP_copy(P_160, group))
394 ABORT;
395
396
397 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
398
399 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
400 ABORT;
401 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
402 ABORT;
403 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
404 ABORT;
405 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"))
406 ABORT;
407 if (!EC_GROUP_set_curve(group, p, a, b, ctx))
408 ABORT;
409
410 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"))
411 ABORT;
412 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
413 ABORT;
414 if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
415 ABORT;
416 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"))
417 ABORT;
418 if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
419 ABORT;
420
421 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx))
422 ABORT;
423 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
424 BN_print_fp(stdout, x);
425 fprintf(stdout, "\n y = 0x");
426 BN_print_fp(stdout, y);
427 fprintf(stdout, "\n");
428 /* G_y value taken from the standard: */
429 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"))
430 ABORT;
431 if (0 != BN_cmp(y, z))
432 ABORT;
433
434 fprintf(stdout, "verify degree ...");
435 if (EC_GROUP_get_degree(group) != 192)
436 ABORT;
437 fprintf(stdout, " ok\n");
438
439 group_order_tests(group);
440
441 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group))))
442 ABORT;
443 if (!EC_GROUP_copy(P_192, group))
444 ABORT;
445
446
447 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
448
449 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"))
450 ABORT;
451 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
452 ABORT;
453 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
454 ABORT;
455 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"))
456 ABORT;
457 if (!EC_GROUP_set_curve(group, p, a, b, ctx))
458 ABORT;
459
460 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"))
461 ABORT;
462 if (!EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
463 ABORT;
464 if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
465 ABORT;
466 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"))
467 ABORT;
468 if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
469 ABORT;
470
471 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx))
472 ABORT;
473 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
474 BN_print_fp(stdout, x);
475 fprintf(stdout, "\n y = 0x");
476 BN_print_fp(stdout, y);
477 fprintf(stdout, "\n");
478 /* G_y value taken from the standard: */
479 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"))
480 ABORT;
481 if (0 != BN_cmp(y, z))
482 ABORT;
483
484 fprintf(stdout, "verify degree ...");
485 if (EC_GROUP_get_degree(group) != 224)
486 ABORT;
487 fprintf(stdout, " ok\n");
488
489 group_order_tests(group);
490
491 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group))))
492 ABORT;
493 if (!EC_GROUP_copy(P_224, group))
494 ABORT;
495
496
497 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
498
499 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"))
500 ABORT;
501 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
502 ABORT;
503 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC"))
504 ABORT;
505 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"))
506 ABORT;
507 if (!EC_GROUP_set_curve(group, p, a, b, ctx))
508 ABORT;
509
510 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"))
511 ABORT;
512 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
513 ABORT;
514 if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
515 ABORT;
516 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
517 "84F3B9CAC2FC632551")) ABORT;
518 if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
519 ABORT;
520
521 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx))
522 ABORT;
523 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
524 BN_print_fp(stdout, x);
525 fprintf(stdout, "\n y = 0x");
526 BN_print_fp(stdout, y);
527 fprintf(stdout, "\n");
528 /* G_y value taken from the standard: */
529 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"))
530 ABORT;
531 if (0 != BN_cmp(y, z))
532 ABORT;
533
534 fprintf(stdout, "verify degree ...");
535 if (EC_GROUP_get_degree(group) != 256)
536 ABORT;
537 fprintf(stdout, " ok\n");
538
539 group_order_tests(group);
540
541 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group))))
542 ABORT;
543 if (!EC_GROUP_copy(P_256, group))
544 ABORT;
545
546
547 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
548
549 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
550 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
551 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
552 ABORT;
553 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
554 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
555 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
556 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
557 if (!EC_GROUP_set_curve(group, p, a, b, ctx))
558 ABORT;
559
560 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
561 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
562 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
563 ABORT;
564 if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
565 ABORT;
566 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
567 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
568 if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
569 ABORT;
570
571 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx))
572 ABORT;
573 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
574 BN_print_fp(stdout, x);
575 fprintf(stdout, "\n y = 0x");
576 BN_print_fp(stdout, y);
577 fprintf(stdout, "\n");
578 /* G_y value taken from the standard: */
579 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
580 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
581 if (0 != BN_cmp(y, z))
582 ABORT;
583
584 fprintf(stdout, "verify degree ...");
585 if (EC_GROUP_get_degree(group) != 384)
586 ABORT;
587 fprintf(stdout, " ok\n");
588
589 group_order_tests(group);
590
591 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group))))
592 ABORT;
593 if (!EC_GROUP_copy(P_384, group))
594 ABORT;
595
596
597 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
598
599 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
600 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
601 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
602 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
603 ABORT;
604 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
605 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
606 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
607 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
608 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
609 "DF883D2C34F1EF451FD46B503F00")) ABORT;
610 if (!EC_GROUP_set_curve(group, p, a, b, ctx))
611 ABORT;
612
613 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
614 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
615 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
616 if (!EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
617 ABORT;
618 if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
619 ABORT;
620 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
621 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
622 "C9B8899C47AEBB6FB71E91386409")) ABORT;
623 if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
624 ABORT;
625
626 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx))
627 ABORT;
628 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
629 BN_print_fp(stdout, x);
630 fprintf(stdout, "\n y = 0x");
631 BN_print_fp(stdout, y);
632 fprintf(stdout, "\n");
633 /* G_y value taken from the standard: */
634 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
635 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
636 "7086A272C24088BE94769FD16650")) ABORT;
637 if (0 != BN_cmp(y, z))
638 ABORT;
639
640 fprintf(stdout, "verify degree ...");
641 if (EC_GROUP_get_degree(group) != 521)
642 ABORT;
643 fprintf(stdout, " ok\n");
644
645 group_order_tests(group);
646
647 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group))))
648 ABORT;
649 if (!EC_GROUP_copy(P_521, group))
650 ABORT;
651
652
653 /* more tests using the last curve */
654 fprintf(stdout, "infinity tests ...");
655 fflush(stdout);
656 if (!EC_POINT_copy(Q, P))
657 ABORT;
658 if (EC_POINT_is_at_infinity(group, Q))
659 ABORT;
660 /* P := 2P */
661 if (!EC_POINT_dbl(group, P, P, ctx))
662 ABORT;
663 if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
664 ABORT;
665 /* Q := -P */
666 if (!EC_POINT_invert(group, Q, ctx))
667 ABORT;
668 /* R := 2P - P = P */
669 if (!EC_POINT_add(group, R, P, Q, ctx))
670 ABORT;
671 /* R := R + Q = P - P = infty */
672 if (!EC_POINT_add(group, R, R, Q, ctx))
673 ABORT;
674 if (!EC_POINT_is_at_infinity(group, R))
675 ABORT;
676 fprintf(stdout, " ok\n\n");
677
678 if (ctx)
679 BN_CTX_free(ctx);
680 BN_free(p);
681 BN_free(a);
682 BN_free(b);
683 EC_GROUP_free(group);
684 EC_POINT_free(P);
685 EC_POINT_free(Q);
686 EC_POINT_free(R);
687 BN_free(x);
688 BN_free(y);
689 BN_free(z);
690
691 if (P_160)
692 EC_GROUP_free(P_160);
693 if (P_192)
694 EC_GROUP_free(P_192);
695 if (P_224)
696 EC_GROUP_free(P_224);
697 if (P_256)
698 EC_GROUP_free(P_256);
699 if (P_384)
700 EC_GROUP_free(P_384);
701 if (P_521)
702 EC_GROUP_free(P_521);
703
704 }
705
706 int
main(int argc,char * argv[])707 main(int argc, char *argv[])
708 {
709 ERR_load_crypto_strings();
710
711 prime_field_tests();
712
713 CRYPTO_cleanup_all_ex_data();
714 ERR_free_strings();
715 ERR_remove_thread_state(NULL);
716
717 return 0;
718 }
719