Remove some obsolete/obscure internal define switches:
[openssl.git] / test / ectest.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* ====================================================================
11  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12  *
13  * Portions of the attached software ("Contribution") are developed by
14  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
15  *
16  * The Contribution is licensed pursuant to the OpenSSL open source
17  * license provided above.
18  *
19  * The elliptic curve binary polynomial software is originally written by
20  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
21  *
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28
29 #include "e_os.h"
30
31 #ifdef OPENSSL_NO_EC
32 int main(int argc, char *argv[])
33 {
34     puts("Elliptic curves are disabled.");
35     return 0;
36 }
37 #else
38
39 # include <openssl/ec.h>
40 # ifndef OPENSSL_NO_ENGINE
41 #  include <openssl/engine.h>
42 # endif
43 # include <openssl/err.h>
44 # include <openssl/obj_mac.h>
45 # include <openssl/objects.h>
46 # include <openssl/rand.h>
47 # include <openssl/bn.h>
48 # include <openssl/opensslconf.h>
49
50 # if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
51 /* suppress "too big too optimize" warning */
52 #  pragma warning(disable:4959)
53 # endif
54
55 # define ABORT do { \
56         fflush(stdout); \
57         fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
58         ERR_print_errors_fp(stderr); \
59         EXIT(1); \
60 } while (0)
61
62 # define TIMING_BASE_PT 0
63 # define TIMING_RAND_PT 1
64 # define TIMING_SIMUL 2
65
66 /* test multiplication with group order, long and negative scalars */
67 static void group_order_tests(EC_GROUP *group)
68 {
69     BIGNUM *n1, *n2, *order;
70     EC_POINT *P = EC_POINT_new(group);
71     EC_POINT *Q = EC_POINT_new(group);
72     EC_POINT *R = EC_POINT_new(group);
73     EC_POINT *S = EC_POINT_new(group);
74     BN_CTX *ctx = BN_CTX_new();
75     int i;
76
77     n1 = BN_new();
78     n2 = BN_new();
79     order = BN_new();
80     fprintf(stdout, "verify group order ...");
81     fflush(stdout);
82     if (!EC_GROUP_get_order(group, order, ctx))
83         ABORT;
84     if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
85         ABORT;
86     if (!EC_POINT_is_at_infinity(group, Q))
87         ABORT;
88     fprintf(stdout, ".");
89     fflush(stdout);
90     if (!EC_GROUP_precompute_mult(group, ctx))
91         ABORT;
92     if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
93         ABORT;
94     if (!EC_POINT_is_at_infinity(group, Q))
95         ABORT;
96     fprintf(stdout, " ok\n");
97     fprintf(stdout, "long/negative scalar tests ");
98     for (i = 1; i <= 2; i++) {
99         const BIGNUM *scalars[6];
100         const EC_POINT *points[6];
101
102         fprintf(stdout, i == 1 ?
103                 "allowing precomputation ... " :
104                 "without precomputation ... ");
105         if (!BN_set_word(n1, i))
106             ABORT;
107         /*
108          * If i == 1, P will be the predefined generator for which
109          * EC_GROUP_precompute_mult has set up precomputation.
110          */
111         if (!EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
112             ABORT;
113
114         if (!BN_one(n1))
115             ABORT;
116         /* n1 = 1 - order */
117         if (!BN_sub(n1, n1, order))
118             ABORT;
119         if (!EC_POINT_mul(group, Q, NULL, P, n1, ctx))
120             ABORT;
121         if (0 != EC_POINT_cmp(group, Q, P, ctx))
122             ABORT;
123
124         /* n2 = 1 + order */
125         if (!BN_add(n2, order, BN_value_one()))
126             ABORT;
127         if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
128             ABORT;
129         if (0 != EC_POINT_cmp(group, Q, P, ctx))
130             ABORT;
131
132         /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
133         if (!BN_mul(n2, n1, n2, ctx))
134             ABORT;
135         if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
136             ABORT;
137         if (0 != EC_POINT_cmp(group, Q, P, ctx))
138             ABORT;
139
140         /* n2 = order^2 - 1 */
141         BN_set_negative(n2, 0);
142         if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
143             ABORT;
144         /* Add P to verify the result. */
145         if (!EC_POINT_add(group, Q, Q, P, ctx))
146             ABORT;
147         if (!EC_POINT_is_at_infinity(group, Q))
148             ABORT;
149
150         /* Exercise EC_POINTs_mul, including corner cases. */
151         if (EC_POINT_is_at_infinity(group, P))
152             ABORT;
153
154         scalars[0] = scalars[1] = BN_value_one();
155         points[0]  = points[1]  = P;
156
157         if (!EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))
158             ABORT;
159         if (!EC_POINT_dbl(group, S, points[0], ctx))
160             ABORT;
161         if (0 != EC_POINT_cmp(group, R, S, ctx))
162             ABORT;
163
164         scalars[0] = n1;
165         points[0] = Q;          /* => infinity */
166         scalars[1] = n2;
167         points[1] = P;          /* => -P */
168         scalars[2] = n1;
169         points[2] = Q;          /* => infinity */
170         scalars[3] = n2;
171         points[3] = Q;          /* => infinity */
172         scalars[4] = n1;
173         points[4] = P;          /* => P */
174         scalars[5] = n2;
175         points[5] = Q;          /* => infinity */
176         if (!EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
177             ABORT;
178         if (!EC_POINT_is_at_infinity(group, P))
179             ABORT;
180     }
181     fprintf(stdout, "ok\n");
182
183     EC_POINT_free(P);
184     EC_POINT_free(Q);
185     EC_POINT_free(R);
186     EC_POINT_free(S);
187     BN_free(n1);
188     BN_free(n2);
189     BN_free(order);
190     BN_CTX_free(ctx);
191 }
192
193 static void prime_field_tests(void)
194 {
195     BN_CTX *ctx = NULL;
196     BIGNUM *p, *a, *b;
197     EC_GROUP *group;
198     EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 =
199         NULL, *P_384 = NULL, *P_521 = NULL;
200     EC_POINT *P, *Q, *R;
201     BIGNUM *x, *y, *z, *yplusone;
202     unsigned char buf[100];
203     size_t i, len;
204     int k;
205
206     ctx = BN_CTX_new();
207     if (!ctx)
208         ABORT;
209
210     p = BN_new();
211     a = BN_new();
212     b = BN_new();
213     if (!p || !a || !b)
214         ABORT;
215
216     if (!BN_hex2bn(&p, "17"))
217         ABORT;
218     if (!BN_hex2bn(&a, "1"))
219         ABORT;
220     if (!BN_hex2bn(&b, "1"))
221         ABORT;
222
223     group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use
224                                                  * EC_GROUP_new_curve_GFp so
225                                                  * that the library gets to
226                                                  * choose the EC_METHOD */
227     if (!group)
228         ABORT;
229
230     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
231         ABORT;
232
233     {
234         EC_GROUP *tmp;
235         tmp = EC_GROUP_new(EC_GROUP_method_of(group));
236         if (!tmp)
237             ABORT;
238         if (!EC_GROUP_copy(tmp, group))
239             ABORT;
240         EC_GROUP_free(group);
241         group = tmp;
242     }
243
244     if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx))
245         ABORT;
246
247     fprintf(stdout,
248             "Curve defined by Weierstrass equation\n     y^2 = x^3 + a*x + b  (mod 0x");
249     BN_print_fp(stdout, p);
250     fprintf(stdout, ")\n     a = 0x");
251     BN_print_fp(stdout, a);
252     fprintf(stdout, "\n     b = 0x");
253     BN_print_fp(stdout, b);
254     fprintf(stdout, "\n");
255
256     P = EC_POINT_new(group);
257     Q = EC_POINT_new(group);
258     R = EC_POINT_new(group);
259     if (!P || !Q || !R)
260         ABORT;
261
262     if (!EC_POINT_set_to_infinity(group, P))
263         ABORT;
264     if (!EC_POINT_is_at_infinity(group, P))
265         ABORT;
266
267     buf[0] = 0;
268     if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
269         ABORT;
270
271     if (!EC_POINT_add(group, P, P, Q, ctx))
272         ABORT;
273     if (!EC_POINT_is_at_infinity(group, P))
274         ABORT;
275
276     x = BN_new();
277     y = BN_new();
278     z = BN_new();
279     yplusone = BN_new();
280     if (x == NULL || y == NULL || z == NULL || yplusone == NULL)
281         ABORT;
282
283     if (!BN_hex2bn(&x, "D"))
284         ABORT;
285     if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx))
286         ABORT;
287     if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
288         if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx))
289             ABORT;
290         fprintf(stderr, "Point is not on curve: x = 0x");
291         BN_print_fp(stderr, x);
292         fprintf(stderr, ", y = 0x");
293         BN_print_fp(stderr, y);
294         fprintf(stderr, "\n");
295         ABORT;
296     }
297
298     fprintf(stdout, "A cyclic subgroup:\n");
299     k = 100;
300     do {
301         if (k-- == 0)
302             ABORT;
303
304         if (EC_POINT_is_at_infinity(group, P))
305             fprintf(stdout, "     point at infinity\n");
306         else {
307             if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
308                 ABORT;
309
310             fprintf(stdout, "     x = 0x");
311             BN_print_fp(stdout, x);
312             fprintf(stdout, ", y = 0x");
313             BN_print_fp(stdout, y);
314             fprintf(stdout, "\n");
315         }
316
317         if (!EC_POINT_copy(R, P))
318             ABORT;
319         if (!EC_POINT_add(group, P, P, Q, ctx))
320             ABORT;
321
322     }
323     while (!EC_POINT_is_at_infinity(group, P));
324
325     if (!EC_POINT_add(group, P, Q, R, ctx))
326         ABORT;
327     if (!EC_POINT_is_at_infinity(group, P))
328         ABORT;
329
330     len =
331         EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
332                            sizeof buf, ctx);
333     if (len == 0)
334         ABORT;
335     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
336         ABORT;
337     if (0 != EC_POINT_cmp(group, P, Q, ctx))
338         ABORT;
339     fprintf(stdout, "Generator as octet string, compressed form:\n     ");
340     for (i = 0; i < len; i++)
341         fprintf(stdout, "%02X", buf[i]);
342
343     len =
344         EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,
345                            sizeof buf, ctx);
346     if (len == 0)
347         ABORT;
348     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
349         ABORT;
350     if (0 != EC_POINT_cmp(group, P, Q, ctx))
351         ABORT;
352     fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n     ");
353     for (i = 0; i < len; i++)
354         fprintf(stdout, "%02X", buf[i]);
355
356     len =
357         EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,
358                            ctx);
359     if (len == 0)
360         ABORT;
361     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
362         ABORT;
363     if (0 != EC_POINT_cmp(group, P, Q, ctx))
364         ABORT;
365     fprintf(stdout, "\nGenerator as octet string, hybrid form:\n     ");
366     for (i = 0; i < len; i++)
367         fprintf(stdout, "%02X", buf[i]);
368
369     if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx))
370         ABORT;
371     fprintf(stdout,
372             "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n     X = 0x");
373     BN_print_fp(stdout, x);
374     fprintf(stdout, ", Y = 0x");
375     BN_print_fp(stdout, y);
376     fprintf(stdout, ", Z = 0x");
377     BN_print_fp(stdout, z);
378     fprintf(stdout, "\n");
379
380     if (!EC_POINT_invert(group, P, ctx))
381         ABORT;
382     if (0 != EC_POINT_cmp(group, P, R, ctx))
383         ABORT;
384
385     /*
386      * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
387      * 2000) -- not a NIST curve, but commonly used
388      */
389
390     if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
391         ABORT;
392     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
393         ABORT;
394     if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
395         ABORT;
396     if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45"))
397         ABORT;
398     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
399         ABORT;
400
401     if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82"))
402         ABORT;
403     if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32"))
404         ABORT;
405     if (!BN_add(yplusone, y, BN_value_one()))
406         ABORT;
407     /*
408      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
409      * and therefore setting the coordinates should fail.
410      */
411     if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
412         ABORT;
413     if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
414         ABORT;
415     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
416         ABORT;
417     if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257"))
418         ABORT;
419     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
420         ABORT;
421
422     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
423         ABORT;
424     fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n     x = 0x");
425     BN_print_fp(stdout, x);
426     fprintf(stdout, "\n     y = 0x");
427     BN_print_fp(stdout, y);
428     fprintf(stdout, "\n");
429     /* G_y value taken from the standard: */
430     if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32"))
431         ABORT;
432     if (0 != BN_cmp(y, z))
433         ABORT;
434
435     fprintf(stdout, "verify degree ...");
436     if (EC_GROUP_get_degree(group) != 160)
437         ABORT;
438     fprintf(stdout, " ok\n");
439
440     group_order_tests(group);
441
442     if ((P_160 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
443         ABORT;
444     if (!EC_GROUP_copy(P_160, group))
445         ABORT;
446
447     /* Curve P-192 (FIPS PUB 186-2, App. 6) */
448
449     if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
450         ABORT;
451     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
452         ABORT;
453     if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
454         ABORT;
455     if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"))
456         ABORT;
457     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
458         ABORT;
459
460     if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"))
461         ABORT;
462     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
463         ABORT;
464     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
465         ABORT;
466     if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"))
467         ABORT;
468     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
469         ABORT;
470
471     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
472         ABORT;
473     fprintf(stdout, "\nNIST curve P-192 -- 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, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"))
480         ABORT;
481     if (0 != BN_cmp(y, z))
482         ABORT;
483
484     if (!BN_add(yplusone, y, BN_value_one()))
485         ABORT;
486     /*
487      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
488      * and therefore setting the coordinates should fail.
489      */
490     if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
491         ABORT;
492
493     fprintf(stdout, "verify degree ...");
494     if (EC_GROUP_get_degree(group) != 192)
495         ABORT;
496     fprintf(stdout, " ok\n");
497
498     group_order_tests(group);
499
500     if ((P_192 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
501         ABORT;
502     if (!EC_GROUP_copy(P_192, group))
503         ABORT;
504
505     /* Curve P-224 (FIPS PUB 186-2, App. 6) */
506
507     if (!BN_hex2bn
508         (&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"))
509         ABORT;
510     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
511         ABORT;
512     if (!BN_hex2bn
513         (&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
514         ABORT;
515     if (!BN_hex2bn
516         (&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"))
517         ABORT;
518     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
519         ABORT;
520
521     if (!BN_hex2bn
522         (&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"))
523         ABORT;
524     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
525         ABORT;
526     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
527         ABORT;
528     if (!BN_hex2bn
529         (&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"))
530         ABORT;
531     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
532         ABORT;
533
534     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
535         ABORT;
536     fprintf(stdout, "\nNIST curve P-224 -- Generator:\n     x = 0x");
537     BN_print_fp(stdout, x);
538     fprintf(stdout, "\n     y = 0x");
539     BN_print_fp(stdout, y);
540     fprintf(stdout, "\n");
541     /* G_y value taken from the standard: */
542     if (!BN_hex2bn
543         (&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"))
544         ABORT;
545     if (0 != BN_cmp(y, z))
546         ABORT;
547
548     if (!BN_add(yplusone, y, BN_value_one()))
549         ABORT;
550     /*
551      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
552      * and therefore setting the coordinates should fail.
553      */
554     if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
555         ABORT;
556
557     fprintf(stdout, "verify degree ...");
558     if (EC_GROUP_get_degree(group) != 224)
559         ABORT;
560     fprintf(stdout, " ok\n");
561
562     group_order_tests(group);
563
564     if ((P_224 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
565         ABORT;
566     if (!EC_GROUP_copy(P_224, group))
567         ABORT;
568
569     /* Curve P-256 (FIPS PUB 186-2, App. 6) */
570
571     if (!BN_hex2bn
572         (&p,
573          "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"))
574         ABORT;
575     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
576         ABORT;
577     if (!BN_hex2bn
578         (&a,
579          "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC"))
580         ABORT;
581     if (!BN_hex2bn
582         (&b,
583          "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"))
584         ABORT;
585     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
586         ABORT;
587
588     if (!BN_hex2bn
589         (&x,
590          "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"))
591         ABORT;
592     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
593         ABORT;
594     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
595         ABORT;
596     if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
597                    "84F3B9CAC2FC632551"))
598         ABORT;
599     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
600         ABORT;
601
602     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
603         ABORT;
604     fprintf(stdout, "\nNIST curve P-256 -- Generator:\n     x = 0x");
605     BN_print_fp(stdout, x);
606     fprintf(stdout, "\n     y = 0x");
607     BN_print_fp(stdout, y);
608     fprintf(stdout, "\n");
609     /* G_y value taken from the standard: */
610     if (!BN_hex2bn
611         (&z,
612          "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"))
613         ABORT;
614     if (0 != BN_cmp(y, z))
615         ABORT;
616
617     if (!BN_add(yplusone, y, BN_value_one()))
618         ABORT;
619     /*
620      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
621      * and therefore setting the coordinates should fail.
622      */
623     if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
624         ABORT;
625
626     fprintf(stdout, "verify degree ...");
627     if (EC_GROUP_get_degree(group) != 256)
628         ABORT;
629     fprintf(stdout, " ok\n");
630
631     group_order_tests(group);
632
633     if ((P_256 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
634         ABORT;
635     if (!EC_GROUP_copy(P_256, group))
636         ABORT;
637
638     /* Curve P-384 (FIPS PUB 186-2, App. 6) */
639
640     if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
641                    "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"))
642         ABORT;
643     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
644         ABORT;
645     if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
646                    "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC"))
647         ABORT;
648     if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
649                    "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"))
650         ABORT;
651     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
652         ABORT;
653
654     if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
655                    "9859F741E082542A385502F25DBF55296C3A545E3872760AB7"))
656         ABORT;
657     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
658         ABORT;
659     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
660         ABORT;
661     if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
662                    "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"))
663         ABORT;
664     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
665         ABORT;
666
667     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
668         ABORT;
669     fprintf(stdout, "\nNIST curve P-384 -- Generator:\n     x = 0x");
670     BN_print_fp(stdout, x);
671     fprintf(stdout, "\n     y = 0x");
672     BN_print_fp(stdout, y);
673     fprintf(stdout, "\n");
674     /* G_y value taken from the standard: */
675     if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
676                    "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"))
677         ABORT;
678     if (0 != BN_cmp(y, z))
679         ABORT;
680
681     if (!BN_add(yplusone, y, BN_value_one()))
682         ABORT;
683     /*
684      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
685      * and therefore setting the coordinates should fail.
686      */
687     if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
688         ABORT;
689
690     fprintf(stdout, "verify degree ...");
691     if (EC_GROUP_get_degree(group) != 384)
692         ABORT;
693     fprintf(stdout, " ok\n");
694
695     group_order_tests(group);
696
697     if ((P_384 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
698         ABORT;
699     if (!EC_GROUP_copy(P_384, group))
700         ABORT;
701
702     /* Curve P-521 (FIPS PUB 186-2, App. 6) */
703
704     if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
705                    "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
706                    "FFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
707         ABORT;
708     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
709         ABORT;
710     if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
711                    "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
712                    "FFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
713         ABORT;
714     if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
715                    "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
716                    "DF883D2C34F1EF451FD46B503F00"))
717         ABORT;
718     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
719         ABORT;
720
721     if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
722                    "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
723                    "3C1856A429BF97E7E31C2E5BD66"))
724         ABORT;
725     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
726         ABORT;
727     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
728         ABORT;
729     if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
730                    "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
731                    "C9B8899C47AEBB6FB71E91386409"))
732         ABORT;
733     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
734         ABORT;
735
736     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
737         ABORT;
738     fprintf(stdout, "\nNIST curve P-521 -- Generator:\n     x = 0x");
739     BN_print_fp(stdout, x);
740     fprintf(stdout, "\n     y = 0x");
741     BN_print_fp(stdout, y);
742     fprintf(stdout, "\n");
743     /* G_y value taken from the standard: */
744     if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
745                    "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
746                    "7086A272C24088BE94769FD16650"))
747         ABORT;
748     if (0 != BN_cmp(y, z))
749         ABORT;
750
751     if (!BN_add(yplusone, y, BN_value_one()))
752         ABORT;
753     /*
754      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
755      * and therefore setting the coordinates should fail.
756      */
757     if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
758         ABORT;
759
760     fprintf(stdout, "verify degree ...");
761     if (EC_GROUP_get_degree(group) != 521)
762         ABORT;
763     fprintf(stdout, " ok\n");
764
765     group_order_tests(group);
766
767     if ((P_521 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
768         ABORT;
769     if (!EC_GROUP_copy(P_521, group))
770         ABORT;
771
772     /* more tests using the last curve */
773
774     /* Restore the point that got mangled in the (x, y + 1) test. */
775     if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
776         ABORT;
777
778     if (!EC_POINT_copy(Q, P))
779         ABORT;
780     if (EC_POINT_is_at_infinity(group, Q))
781         ABORT;
782     if (!EC_POINT_dbl(group, P, P, ctx))
783         ABORT;
784     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
785         ABORT;
786     if (!EC_POINT_invert(group, Q, ctx))
787         ABORT;                  /* P = -2Q */
788
789     if (!EC_POINT_add(group, R, P, Q, ctx))
790         ABORT;
791     if (!EC_POINT_add(group, R, R, Q, ctx))
792         ABORT;
793     if (!EC_POINT_is_at_infinity(group, R))
794         ABORT;                  /* R = P + 2Q */
795
796     {
797         const EC_POINT *points[4];
798         const BIGNUM *scalars[4];
799         BIGNUM *scalar3;
800
801         if (EC_POINT_is_at_infinity(group, Q))
802             ABORT;
803         points[0] = Q;
804         points[1] = Q;
805         points[2] = Q;
806         points[3] = Q;
807
808         if (!EC_GROUP_get_order(group, z, ctx))
809             ABORT;
810         if (!BN_add(y, z, BN_value_one()))
811             ABORT;
812         if (BN_is_odd(y))
813             ABORT;
814         if (!BN_rshift1(y, y))
815             ABORT;
816         scalars[0] = y;         /* (group order + 1)/2, so y*Q + y*Q = Q */
817         scalars[1] = y;
818
819         fprintf(stdout, "combined multiplication ...");
820         fflush(stdout);
821
822         /* z is still the group order */
823         if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
824             ABORT;
825         if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
826             ABORT;
827         if (0 != EC_POINT_cmp(group, P, R, ctx))
828             ABORT;
829         if (0 != EC_POINT_cmp(group, R, Q, ctx))
830             ABORT;
831
832         fprintf(stdout, ".");
833         fflush(stdout);
834
835         if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
836             ABORT;
837         if (!BN_add(z, z, y))
838             ABORT;
839         BN_set_negative(z, 1);
840         scalars[0] = y;
841         scalars[1] = z;         /* z = -(order + y) */
842
843         if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
844             ABORT;
845         if (!EC_POINT_is_at_infinity(group, P))
846             ABORT;
847
848         fprintf(stdout, ".");
849         fflush(stdout);
850
851         if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
852             ABORT;
853         if (!BN_add(z, x, y))
854             ABORT;
855         BN_set_negative(z, 1);
856         scalars[0] = x;
857         scalars[1] = y;
858         scalars[2] = z;         /* z = -(x+y) */
859
860         scalar3 = BN_new();
861         if (!scalar3)
862             ABORT;
863         BN_zero(scalar3);
864         scalars[3] = scalar3;
865
866         if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))
867             ABORT;
868         if (!EC_POINT_is_at_infinity(group, P))
869             ABORT;
870
871         fprintf(stdout, " ok\n\n");
872
873         BN_free(scalar3);
874     }
875
876     BN_CTX_free(ctx);
877     BN_free(p);
878     BN_free(a);
879     BN_free(b);
880     EC_GROUP_free(group);
881     EC_POINT_free(P);
882     EC_POINT_free(Q);
883     EC_POINT_free(R);
884     BN_free(x);
885     BN_free(y);
886     BN_free(z);
887     BN_free(yplusone);
888
889     EC_GROUP_free(P_160);
890     EC_GROUP_free(P_192);
891     EC_GROUP_free(P_224);
892     EC_GROUP_free(P_256);
893     EC_GROUP_free(P_384);
894     EC_GROUP_free(P_521);
895
896 }
897
898 /* Change test based on whether binary point compression is enabled or not. */
899 # ifdef OPENSSL_EC_BIN_PT_COMP
900 #  define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
901         if (!BN_hex2bn(&x, _x)) ABORT; \
902         if (!BN_hex2bn(&y, _y)) ABORT; \
903         if (!BN_add(yplusone, y, BN_value_one())) ABORT;        \
904         /* \
905          * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, \
906          * and therefore setting the coordinates should fail. \
907          */ \
908         if (EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone, ctx)) ABORT; \
909         if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
910         if (EC_POINT_is_on_curve(group, P, ctx) <= 0) ABORT; \
911         if (!BN_hex2bn(&z, _order)) ABORT; \
912         if (!BN_hex2bn(&cof, _cof)) ABORT; \
913         if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
914         if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
915         fprintf(stdout, "\n%s -- Generator:\n     x = 0x", _name); \
916         BN_print_fp(stdout, x); \
917         fprintf(stdout, "\n     y = 0x"); \
918         BN_print_fp(stdout, y); \
919         fprintf(stdout, "\n"); \
920         /* G_y value taken from the standard: */ \
921         if (!BN_hex2bn(&z, _y)) ABORT; \
922         if (0 != BN_cmp(y, z)) ABORT;
923 # else
924 #  define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
925         if (!BN_hex2bn(&x, _x)) ABORT; \
926         if (!BN_hex2bn(&y, _y)) ABORT; \
927         if (!BN_add(yplusone, y, BN_value_one())) ABORT;        \
928         /* \
929          * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, \
930          * and therefore setting the coordinates should fail. \
931          */ \
932         if (EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone, ctx)) ABORT; \
933         if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
934         if (EC_POINT_is_on_curve(group, P, ctx) <= 0) ABORT; \
935         if (!BN_hex2bn(&z, _order)) ABORT; \
936         if (!BN_hex2bn(&cof, _cof)) ABORT; \
937         if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
938         fprintf(stdout, "\n%s -- Generator:\n     x = 0x", _name); \
939         BN_print_fp(stdout, x); \
940         fprintf(stdout, "\n     y = 0x"); \
941         BN_print_fp(stdout, y); \
942         fprintf(stdout, "\n");
943 # endif
944
945 # define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
946         if (!BN_hex2bn(&p, _p)) ABORT; \
947         if (!BN_hex2bn(&a, _a)) ABORT; \
948         if (!BN_hex2bn(&b, _b)) ABORT; \
949         if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
950         CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
951         fprintf(stdout, "verify degree ..."); \
952         if (EC_GROUP_get_degree(group) != _degree) ABORT; \
953         fprintf(stdout, " ok\n"); \
954         group_order_tests(group); \
955         if ((_variable = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL) ABORT; \
956         if (!EC_GROUP_copy(_variable, group)) ABORT; \
957
958 # ifndef OPENSSL_NO_EC2M
959
960 static void char2_field_tests(void)
961 {
962     BN_CTX *ctx = NULL;
963     BIGNUM *p, *a, *b;
964     EC_GROUP *group;
965     EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 =
966         NULL, *C2_K571 = NULL;
967     EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 =
968         NULL, *C2_B571 = NULL;
969     EC_POINT *P, *Q, *R;
970     BIGNUM *x, *y, *z, *cof, *yplusone;
971     unsigned char buf[100];
972     size_t i, len;
973     int k;
974
975     ctx = BN_CTX_new();
976     if (!ctx)
977         ABORT;
978
979     p = BN_new();
980     a = BN_new();
981     b = BN_new();
982     if (p == NULL || a == NULL || b == NULL)
983         ABORT;
984
985     if (!BN_hex2bn(&p, "13"))
986         ABORT;
987     if (!BN_hex2bn(&a, "3"))
988         ABORT;
989     if (!BN_hex2bn(&b, "1"))
990         ABORT;
991
992     group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use
993                                                     * EC_GROUP_new_curve_GF2m
994                                                     * so that the library gets
995                                                     * to choose the EC_METHOD */
996     if (!group)
997         ABORT;
998     if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx))
999         ABORT;
1000
1001     {
1002         EC_GROUP *tmp;
1003         tmp = EC_GROUP_new(EC_GROUP_method_of(group));
1004         if (!tmp)
1005             ABORT;
1006         if (!EC_GROUP_copy(tmp, group))
1007             ABORT;
1008         EC_GROUP_free(group);
1009         group = tmp;
1010     }
1011
1012     if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx))
1013         ABORT;
1014
1015     fprintf(stdout,
1016             "Curve defined by Weierstrass equation\n     y^2 + x*y = x^3 + a*x^2 + b  (mod 0x");
1017     BN_print_fp(stdout, p);
1018     fprintf(stdout, ")\n     a = 0x");
1019     BN_print_fp(stdout, a);
1020     fprintf(stdout, "\n     b = 0x");
1021     BN_print_fp(stdout, b);
1022     fprintf(stdout, "\n(0x... means binary polynomial)\n");
1023
1024     P = EC_POINT_new(group);
1025     Q = EC_POINT_new(group);
1026     R = EC_POINT_new(group);
1027     if (!P || !Q || !R)
1028         ABORT;
1029
1030     if (!EC_POINT_set_to_infinity(group, P))
1031         ABORT;
1032     if (!EC_POINT_is_at_infinity(group, P))
1033         ABORT;
1034
1035     buf[0] = 0;
1036     if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
1037         ABORT;
1038
1039     if (!EC_POINT_add(group, P, P, Q, ctx))
1040         ABORT;
1041     if (!EC_POINT_is_at_infinity(group, P))
1042         ABORT;
1043
1044     x = BN_new();
1045     y = BN_new();
1046     z = BN_new();
1047     cof = BN_new();
1048     yplusone = BN_new();
1049     if (x == NULL || y == NULL || z == NULL || cof == NULL || yplusone == NULL)
1050         ABORT;
1051
1052     if (!BN_hex2bn(&x, "6"))
1053         ABORT;
1054 /* Change test based on whether binary point compression is enabled or not. */
1055 #  ifdef OPENSSL_EC_BIN_PT_COMP
1056     if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx))
1057         ABORT;
1058 #  else
1059     if (!BN_hex2bn(&y, "8"))
1060         ABORT;
1061     if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx))
1062         ABORT;
1063 #  endif
1064     if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
1065 /* Change test based on whether binary point compression is enabled or not. */
1066 #  ifdef OPENSSL_EC_BIN_PT_COMP
1067         if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx))
1068             ABORT;
1069 #  endif
1070         fprintf(stderr, "Point is not on curve: x = 0x");
1071         BN_print_fp(stderr, x);
1072         fprintf(stderr, ", y = 0x");
1073         BN_print_fp(stderr, y);
1074         fprintf(stderr, "\n");
1075         ABORT;
1076     }
1077
1078     fprintf(stdout, "A cyclic subgroup:\n");
1079     k = 100;
1080     do {
1081         if (k-- == 0)
1082             ABORT;
1083
1084         if (EC_POINT_is_at_infinity(group, P))
1085             fprintf(stdout, "     point at infinity\n");
1086         else {
1087             if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx))
1088                 ABORT;
1089
1090             fprintf(stdout, "     x = 0x");
1091             BN_print_fp(stdout, x);
1092             fprintf(stdout, ", y = 0x");
1093             BN_print_fp(stdout, y);
1094             fprintf(stdout, "\n");
1095         }
1096
1097         if (!EC_POINT_copy(R, P))
1098             ABORT;
1099         if (!EC_POINT_add(group, P, P, Q, ctx))
1100             ABORT;
1101     }
1102     while (!EC_POINT_is_at_infinity(group, P));
1103
1104     if (!EC_POINT_add(group, P, Q, R, ctx))
1105         ABORT;
1106     if (!EC_POINT_is_at_infinity(group, P))
1107         ABORT;
1108
1109 /* Change test based on whether binary point compression is enabled or not. */
1110 #  ifdef OPENSSL_EC_BIN_PT_COMP
1111     len =
1112         EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
1113                            sizeof buf, ctx);
1114     if (len == 0)
1115         ABORT;
1116     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
1117         ABORT;
1118     if (0 != EC_POINT_cmp(group, P, Q, ctx))
1119         ABORT;
1120     fprintf(stdout, "Generator as octet string, compressed form:\n     ");
1121     for (i = 0; i < len; i++)
1122         fprintf(stdout, "%02X", buf[i]);
1123 #  endif
1124
1125     len =
1126         EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,
1127                            sizeof buf, ctx);
1128     if (len == 0)
1129         ABORT;
1130     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
1131         ABORT;
1132     if (0 != EC_POINT_cmp(group, P, Q, ctx))
1133         ABORT;
1134     fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n     ");
1135     for (i = 0; i < len; i++)
1136         fprintf(stdout, "%02X", buf[i]);
1137
1138 /* Change test based on whether binary point compression is enabled or not. */
1139 #  ifdef OPENSSL_EC_BIN_PT_COMP
1140     len =
1141         EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,
1142                            ctx);
1143     if (len == 0)
1144         ABORT;
1145     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
1146         ABORT;
1147     if (0 != EC_POINT_cmp(group, P, Q, ctx))
1148         ABORT;
1149     fprintf(stdout, "\nGenerator as octet string, hybrid form:\n     ");
1150     for (i = 0; i < len; i++)
1151         fprintf(stdout, "%02X", buf[i]);
1152 #  endif
1153
1154     fprintf(stdout, "\n");
1155
1156     if (!EC_POINT_invert(group, P, ctx))
1157         ABORT;
1158     if (0 != EC_POINT_cmp(group, P, R, ctx))
1159         ABORT;
1160
1161     /* Curve K-163 (FIPS PUB 186-2, App. 6) */
1162     CHAR2_CURVE_TEST
1163         ("NIST curve K-163",
1164          "0800000000000000000000000000000000000000C9",
1165          "1",
1166          "1",
1167          "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
1168          "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
1169          1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163, C2_K163);
1170
1171     /* Curve B-163 (FIPS PUB 186-2, App. 6) */
1172     CHAR2_CURVE_TEST
1173         ("NIST curve B-163",
1174          "0800000000000000000000000000000000000000C9",
1175          "1",
1176          "020A601907B8C953CA1481EB10512F78744A3205FD",
1177          "03F0EBA16286A2D57EA0991168D4994637E8343E36",
1178          "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
1179          1, "040000000000000000000292FE77E70C12A4234C33", "2", 163, C2_B163);
1180
1181     /* Curve K-233 (FIPS PUB 186-2, App. 6) */
1182     CHAR2_CURVE_TEST
1183         ("NIST curve K-233",
1184          "020000000000000000000000000000000000000004000000000000000001",
1185          "0",
1186          "1",
1187          "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
1188          "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
1189          0,
1190          "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
1191          "4", 233, C2_K233);
1192
1193     /* Curve B-233 (FIPS PUB 186-2, App. 6) */
1194     CHAR2_CURVE_TEST
1195         ("NIST curve B-233",
1196          "020000000000000000000000000000000000000004000000000000000001",
1197          "000000000000000000000000000000000000000000000000000000000001",
1198          "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
1199          "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
1200          "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
1201          1,
1202          "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
1203          "2", 233, C2_B233);
1204
1205     /* Curve K-283 (FIPS PUB 186-2, App. 6) */
1206     CHAR2_CURVE_TEST
1207         ("NIST curve K-283",
1208          "0800000000000000000000000000000000000000000000000000000000000000000010A1",
1209          "0",
1210          "1",
1211          "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
1212          "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
1213          0,
1214          "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
1215          "4", 283, C2_K283);
1216
1217     /* Curve B-283 (FIPS PUB 186-2, App. 6) */
1218     CHAR2_CURVE_TEST
1219         ("NIST curve B-283",
1220          "0800000000000000000000000000000000000000000000000000000000000000000010A1",
1221          "000000000000000000000000000000000000000000000000000000000000000000000001",
1222          "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
1223          "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
1224          "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
1225          1,
1226          "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
1227          "2", 283, C2_B283);
1228
1229     /* Curve K-409 (FIPS PUB 186-2, App. 6) */
1230     CHAR2_CURVE_TEST
1231         ("NIST curve K-409",
1232          "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
1233          "0",
1234          "1",
1235          "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
1236          "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
1237          1,
1238          "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
1239          "4", 409, C2_K409);
1240
1241     /* Curve B-409 (FIPS PUB 186-2, App. 6) */
1242     CHAR2_CURVE_TEST
1243         ("NIST curve B-409",
1244          "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
1245          "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1246          "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
1247          "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
1248          "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
1249          1,
1250          "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
1251          "2", 409, C2_B409);
1252
1253     /* Curve K-571 (FIPS PUB 186-2, App. 6) */
1254     CHAR2_CURVE_TEST
1255         ("NIST curve K-571",
1256          "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1257          "0",
1258          "1",
1259          "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
1260          "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
1261          0,
1262          "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
1263          "4", 571, C2_K571);
1264
1265     /* Curve B-571 (FIPS PUB 186-2, App. 6) */
1266     CHAR2_CURVE_TEST
1267         ("NIST curve B-571",
1268          "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1269          "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1270          "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
1271          "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
1272          "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
1273          1,
1274          "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
1275          "2", 571, C2_B571);
1276
1277     /* more tests using the last curve */
1278
1279     if (!EC_POINT_copy(Q, P))
1280         ABORT;
1281     if (EC_POINT_is_at_infinity(group, Q))
1282         ABORT;
1283     if (!EC_POINT_dbl(group, P, P, ctx))
1284         ABORT;
1285     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
1286         ABORT;
1287     if (!EC_POINT_invert(group, Q, ctx))
1288         ABORT;                  /* P = -2Q */
1289
1290     if (!EC_POINT_add(group, R, P, Q, ctx))
1291         ABORT;
1292     if (!EC_POINT_add(group, R, R, Q, ctx))
1293         ABORT;
1294     if (!EC_POINT_is_at_infinity(group, R))
1295         ABORT;                  /* R = P + 2Q */
1296
1297     {
1298         const EC_POINT *points[3];
1299         const BIGNUM *scalars[3];
1300
1301         if (EC_POINT_is_at_infinity(group, Q))
1302             ABORT;
1303         points[0] = Q;
1304         points[1] = Q;
1305         points[2] = Q;
1306
1307         if (!BN_add(y, z, BN_value_one()))
1308             ABORT;
1309         if (BN_is_odd(y))
1310             ABORT;
1311         if (!BN_rshift1(y, y))
1312             ABORT;
1313         scalars[0] = y;         /* (group order + 1)/2, so y*Q + y*Q = Q */
1314         scalars[1] = y;
1315
1316         fprintf(stdout, "combined multiplication ...");
1317         fflush(stdout);
1318
1319         /* z is still the group order */
1320         if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
1321             ABORT;
1322         if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
1323             ABORT;
1324         if (0 != EC_POINT_cmp(group, P, R, ctx))
1325             ABORT;
1326         if (0 != EC_POINT_cmp(group, R, Q, ctx))
1327             ABORT;
1328
1329         fprintf(stdout, ".");
1330         fflush(stdout);
1331
1332         if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
1333             ABORT;
1334         if (!BN_add(z, z, y))
1335             ABORT;
1336         BN_set_negative(z, 1);
1337         scalars[0] = y;
1338         scalars[1] = z;         /* z = -(order + y) */
1339
1340         if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
1341             ABORT;
1342         if (!EC_POINT_is_at_infinity(group, P))
1343             ABORT;
1344
1345         fprintf(stdout, ".");
1346         fflush(stdout);
1347
1348         if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
1349             ABORT;
1350         if (!BN_add(z, x, y))
1351             ABORT;
1352         BN_set_negative(z, 1);
1353         scalars[0] = x;
1354         scalars[1] = y;
1355         scalars[2] = z;         /* z = -(x+y) */
1356
1357         if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
1358             ABORT;
1359         if (!EC_POINT_is_at_infinity(group, P))
1360             ABORT;
1361
1362         fprintf(stdout, " ok\n\n");
1363     }
1364
1365     BN_CTX_free(ctx);
1366     BN_free(p);
1367     BN_free(a);
1368     BN_free(b);
1369     EC_GROUP_free(group);
1370     EC_POINT_free(P);
1371     EC_POINT_free(Q);
1372     EC_POINT_free(R);
1373     BN_free(x);
1374     BN_free(y);
1375     BN_free(z);
1376     BN_free(cof);
1377     BN_free(yplusone);
1378
1379     EC_GROUP_free(C2_K163);
1380     EC_GROUP_free(C2_B163);
1381     EC_GROUP_free(C2_K233);
1382     EC_GROUP_free(C2_B233);
1383     EC_GROUP_free(C2_K283);
1384     EC_GROUP_free(C2_B283);
1385     EC_GROUP_free(C2_K409);
1386     EC_GROUP_free(C2_B409);
1387     EC_GROUP_free(C2_K571);
1388     EC_GROUP_free(C2_B571);
1389
1390 }
1391 # endif
1392
1393 static void internal_curve_test(void)
1394 {
1395     EC_builtin_curve *curves = NULL;
1396     size_t crv_len = 0, n = 0;
1397     int ok = 1;
1398
1399     crv_len = EC_get_builtin_curves(NULL, 0);
1400     curves = OPENSSL_malloc(sizeof(*curves) * crv_len);
1401     if (curves == NULL)
1402         return;
1403
1404     if (!EC_get_builtin_curves(curves, crv_len)) {
1405         OPENSSL_free(curves);
1406         return;
1407     }
1408
1409     fprintf(stdout, "testing internal curves: ");
1410
1411     for (n = 0; n < crv_len; n++) {
1412         EC_GROUP *group = NULL;
1413         int nid = curves[n].nid;
1414         if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
1415             ok = 0;
1416             fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
1417                     " curve %s\n", OBJ_nid2sn(nid));
1418             /* try next curve */
1419             continue;
1420         }
1421         if (!EC_GROUP_check(group, NULL)) {
1422             ok = 0;
1423             fprintf(stdout, "\nEC_GROUP_check() failed with"
1424                     " curve %s\n", OBJ_nid2sn(nid));
1425             EC_GROUP_free(group);
1426             /* try the next curve */
1427             continue;
1428         }
1429         fprintf(stdout, ".");
1430         fflush(stdout);
1431         EC_GROUP_free(group);
1432     }
1433     if (ok)
1434         fprintf(stdout, " ok\n\n");
1435     else {
1436         fprintf(stdout, " failed\n\n");
1437         ABORT;
1438     }
1439
1440     /* Test all built-in curves and let the library choose the EC_METHOD */
1441     for (n = 0; n < crv_len; n++) {
1442         EC_GROUP *group = NULL;
1443         int nid = curves[n].nid;
1444         /*
1445          * Skip for X25519 because low level operations such as EC_POINT_mul()
1446          * are not supported for this curve
1447          */
1448         if (nid == NID_X25519)
1449             continue;
1450         fprintf(stdout, "%s:\n", OBJ_nid2sn(nid));
1451         fflush(stdout);
1452         if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
1453             ABORT;
1454         }
1455         group_order_tests(group);
1456         EC_GROUP_free(group);
1457     }
1458
1459     OPENSSL_free(curves);
1460     return;
1461 }
1462
1463 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1464 /*
1465  * nistp_test_params contains magic numbers for testing our optimized
1466  * implementations of several NIST curves with characteristic > 3.
1467  */
1468 struct nistp_test_params {
1469     const EC_METHOD *(*meth) ();
1470     int degree;
1471     /*
1472      * Qx, Qy and D are taken from
1473      * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
1474      * Otherwise, values are standard curve parameters from FIPS 180-3
1475      */
1476     const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
1477 };
1478
1479 static const struct nistp_test_params nistp_tests_params[] = {
1480     {
1481      /* P-224 */
1482      EC_GFp_nistp224_method,
1483      224,
1484      /* p */
1485      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
1486      /* a */
1487      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
1488      /* b */
1489      "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
1490      /* Qx */
1491      "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
1492      /* Qy */
1493      "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
1494      /* Gx */
1495      "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
1496      /* Gy */
1497      "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
1498      /* order */
1499      "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
1500      /* d */
1501      "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
1502      },
1503     {
1504      /* P-256 */
1505      EC_GFp_nistp256_method,
1506      256,
1507      /* p */
1508      "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
1509      /* a */
1510      "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
1511      /* b */
1512      "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
1513      /* Qx */
1514      "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
1515      /* Qy */
1516      "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
1517      /* Gx */
1518      "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
1519      /* Gy */
1520      "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
1521      /* order */
1522      "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
1523      /* d */
1524      "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
1525      },
1526     {
1527      /* P-521 */
1528      EC_GFp_nistp521_method,
1529      521,
1530      /* p */
1531      "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
1532      /* a */
1533      "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
1534      /* b */
1535      "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
1536      /* Qx */
1537      "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
1538      /* Qy */
1539      "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
1540      /* Gx */
1541      "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
1542      /* Gy */
1543      "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
1544      /* order */
1545      "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
1546      /* d */
1547      "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
1548      },
1549 };
1550
1551 static void nistp_single_test(const struct nistp_test_params *test)
1552 {
1553     BN_CTX *ctx;
1554     BIGNUM *p, *a, *b, *x, *y, *n, *m, *order, *yplusone;
1555     EC_GROUP *NISTP;
1556     EC_POINT *G, *P, *Q, *Q_CHECK;
1557
1558     fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n",
1559             test->degree);
1560     ctx = BN_CTX_new();
1561     p = BN_new();
1562     a = BN_new();
1563     b = BN_new();
1564     x = BN_new();
1565     y = BN_new();
1566     m = BN_new();
1567     n = BN_new();
1568     order = BN_new();
1569     yplusone = BN_new();
1570
1571     NISTP = EC_GROUP_new(test->meth());
1572     if (!NISTP)
1573         ABORT;
1574     if (!BN_hex2bn(&p, test->p))
1575         ABORT;
1576     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
1577         ABORT;
1578     if (!BN_hex2bn(&a, test->a))
1579         ABORT;
1580     if (!BN_hex2bn(&b, test->b))
1581         ABORT;
1582     if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx))
1583         ABORT;
1584     G = EC_POINT_new(NISTP);
1585     P = EC_POINT_new(NISTP);
1586     Q = EC_POINT_new(NISTP);
1587     Q_CHECK = EC_POINT_new(NISTP);
1588     if (!BN_hex2bn(&x, test->Qx))
1589         ABORT;
1590     if (!BN_hex2bn(&y, test->Qy))
1591         ABORT;
1592     if (!BN_add(yplusone, y, BN_value_one()))
1593         ABORT;
1594     /*
1595      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
1596      * and therefore setting the coordinates should fail.
1597      */
1598     if (EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, yplusone, ctx))
1599         ABORT;
1600     if (!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx))
1601         ABORT;
1602     if (!BN_hex2bn(&x, test->Gx))
1603         ABORT;
1604     if (!BN_hex2bn(&y, test->Gy))
1605         ABORT;
1606     if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx))
1607         ABORT;
1608     if (!BN_hex2bn(&order, test->order))
1609         ABORT;
1610     if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
1611         ABORT;
1612
1613     fprintf(stdout, "verify degree ... ");
1614     if (EC_GROUP_get_degree(NISTP) != test->degree)
1615         ABORT;
1616     fprintf(stdout, "ok\n");
1617
1618     fprintf(stdout, "NIST test vectors ... ");
1619     if (!BN_hex2bn(&n, test->d))
1620         ABORT;
1621     /* fixed point multiplication */
1622     EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1623     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1624         ABORT;
1625     /* random point multiplication */
1626     EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1627     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1628         ABORT;
1629
1630     /* set generator to P = 2*G, where G is the standard generator */
1631     if (!EC_POINT_dbl(NISTP, P, G, ctx))
1632         ABORT;
1633     if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
1634         ABORT;
1635     /* set the scalar to m=n/2, where n is the NIST test scalar */
1636     if (!BN_rshift(m, n, 1))
1637         ABORT;
1638
1639     /* test the non-standard generator */
1640     /* fixed point multiplication */
1641     EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1642     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1643         ABORT;
1644     /* random point multiplication */
1645     EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1646     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1647         ABORT;
1648
1649     /*
1650      * We have not performed precomputation so have_precompute mult should be
1651      * false
1652      */
1653     if (EC_GROUP_have_precompute_mult(NISTP))
1654         ABORT;
1655
1656     /* now repeat all tests with precomputation */
1657     if (!EC_GROUP_precompute_mult(NISTP, ctx))
1658         ABORT;
1659     if (!EC_GROUP_have_precompute_mult(NISTP))
1660         ABORT;
1661
1662     /* fixed point multiplication */
1663     EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1664     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1665         ABORT;
1666     /* random point multiplication */
1667     EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1668     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1669         ABORT;
1670
1671     /* reset generator */
1672     if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
1673         ABORT;
1674     /* fixed point multiplication */
1675     EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1676     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1677         ABORT;
1678     /* random point multiplication */
1679     EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1680     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1681         ABORT;
1682
1683     fprintf(stdout, "ok\n");
1684     group_order_tests(NISTP);
1685     EC_GROUP_free(NISTP);
1686     EC_POINT_free(G);
1687     EC_POINT_free(P);
1688     EC_POINT_free(Q);
1689     EC_POINT_free(Q_CHECK);
1690     BN_free(n);
1691     BN_free(m);
1692     BN_free(p);
1693     BN_free(a);
1694     BN_free(b);
1695     BN_free(x);
1696     BN_free(y);
1697     BN_free(order);
1698     BN_free(yplusone);
1699     BN_CTX_free(ctx);
1700 }
1701
1702 static void nistp_tests()
1703 {
1704     unsigned i;
1705
1706     for (i = 0; i < OSSL_NELEM(nistp_tests_params); i++) {
1707         nistp_single_test(&nistp_tests_params[i]);
1708     }
1709 }
1710 # endif
1711
1712 static void parameter_test(void)
1713 {
1714     EC_GROUP *group, *group2;
1715     ECPARAMETERS *ecparameters;
1716
1717     fprintf(stderr, "\ntesting ecparameters conversion ...");
1718
1719     group = EC_GROUP_new_by_curve_name(NID_secp112r1);
1720     if (!group)
1721         ABORT;
1722
1723     ecparameters = EC_GROUP_get_ecparameters(group, NULL);
1724     if (!ecparameters)
1725         ABORT;
1726     group2 = EC_GROUP_new_from_ecparameters(ecparameters);
1727     if (!group2)
1728         ABORT;
1729     if (EC_GROUP_cmp(group, group2, NULL))
1730         ABORT;
1731
1732     fprintf(stderr, " ok\n");
1733
1734     EC_GROUP_free(group);
1735     EC_GROUP_free(group2);
1736     ECPARAMETERS_free(ecparameters);
1737 }
1738
1739 static const char rnd_seed[] =
1740     "string to make the random number generator think it has entropy";
1741
1742 int main(int argc, char *argv[])
1743 {
1744     char *p;
1745
1746     p = getenv("OPENSSL_DEBUG_MEMORY");
1747     if (p != NULL && strcmp(p, "on") == 0)
1748         CRYPTO_set_mem_debug(1);
1749     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1750
1751     RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
1752
1753     prime_field_tests();
1754     puts("");
1755 # ifndef OPENSSL_NO_EC2M
1756     char2_field_tests();
1757 # endif
1758 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1759     nistp_tests();
1760 # endif
1761     /* test the internal curves */
1762     internal_curve_test();
1763
1764     parameter_test();
1765
1766 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
1767     if (CRYPTO_mem_leaks_fp(stderr) <= 0)
1768         return 1;
1769 #endif
1770
1771     return 0;
1772 }
1773 #endif