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