f3866481be81d2f931901bb0526a2ceb3b9b8dc7
[openssl.git] / crypto / ec / ectest.c
1 /* crypto/ec/ectest.c */
2 /*
3  * Originally written by Bodo Moeller for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@openssl.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  *
61  * Portions of the attached software ("Contribution") are developed by 
62  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63  *
64  * The Contribution is licensed pursuant to the OpenSSL open source
65  * license provided above.
66  *
67  * The elliptic curve binary polynomial software is originally written by 
68  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69  *
70  */
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #ifdef FLAT_INC
75 #include "e_os.h"
76 #else
77 #include "../../e_os.h"
78 #endif
79 #include <string.h>
80 #include <time.h>
81
82
83 #ifdef OPENSSL_NO_EC
84 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
85 #else
86
87
88 #include <openssl/ec.h>
89 #include <openssl/engine.h>
90 #include <openssl/err.h>
91 #include <openssl/obj_mac.h>
92 #include <openssl/objects.h>
93
94 #define ABORT do { \
95         fflush(stdout); \
96         fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
97         ERR_print_errors_fp(stderr); \
98         exit(1); \
99 } while (0)
100
101 void prime_field_tests(void);
102 void char2_field_tests(void);
103 void internal_curve_test(void);
104
105 #if 0
106 static void timings(EC_GROUP *group, int multi, BN_CTX *ctx)
107         {
108         clock_t clck;
109         int i, j;
110         BIGNUM *s;
111         BIGNUM *r[10], *r0[10];
112         EC_POINT *P;
113                 
114         s = BN_new();
115         if (s == NULL) ABORT;
116
117         fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
118         if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
119         fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
120         fflush(stdout);
121
122         P = EC_POINT_new(group);
123         if (P == NULL) ABORT;
124         EC_POINT_copy(P, EC_GROUP_get0_generator(group));
125
126         for (i = 0; i < 10; i++)
127                 {
128                 if ((r[i] = BN_new()) == NULL) ABORT;
129                 if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0)) ABORT;
130                 if (multi)
131                         {
132                         if ((r0[i] = BN_new()) == NULL) ABORT;
133                         if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0)) ABORT;
134                         }
135                 }
136
137         clck = clock();
138         for (i = 0; i < 10; i++)
139                 {
140                 for (j = 0; j < 10; j++)
141                         {
142                         if (!EC_POINT_mul(group, P, r[i], multi ? P : NULL, multi ? r0[i] : NULL, ctx)) ABORT;
143                         }
144                 }
145         fprintf(stdout, "\n");
146         
147         clck = clock() - clck;
148
149 #ifdef CLOCKS_PER_SEC
150         /* "To determine the time in seconds, the value returned
151          * by the clock function should be divided by the value
152          * of the macro CLOCKS_PER_SEC."
153          *                                       -- ISO/IEC 9899 */
154 #       define UNIT "s"
155 #else
156         /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
157          *                            -- cc on NeXTstep/OpenStep */
158 #       define UNIT "units"
159 #       define CLOCKS_PER_SEC 1
160 #endif
161
162         fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
163                 multi ? "s*P+t*Q operations" : "point multiplications",
164                 (double)clck/CLOCKS_PER_SEC);
165         fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
166
167         EC_POINT_free(P);
168         BN_free(s);
169         for (i = 0; i < 10; i++)
170                 {
171                 BN_free(r[i]);
172                 if (multi) BN_free(r0[i]);
173                 }
174         }
175 #endif
176
177 void prime_field_tests()
178         {       
179         BN_CTX *ctx = NULL;
180         BIGNUM *p, *a, *b;
181         EC_GROUP *group;
182         EC_GROUP *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
183         EC_POINT *P, *Q, *R;
184         BIGNUM *x, *y, *z;
185         unsigned char buf[100];
186         size_t i, len;
187         int k;
188         
189 #if 1 /* optional */
190         ctx = BN_CTX_new();
191         if (!ctx) ABORT;
192 #endif
193
194         p = BN_new();
195         a = BN_new();
196         b = BN_new();
197         if (!p || !a || !b) ABORT;
198
199         if (!BN_hex2bn(&p, "17")) ABORT;
200         if (!BN_hex2bn(&a, "1")) ABORT;
201         if (!BN_hex2bn(&b, "1")) ABORT;
202         
203         group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
204                                                      * so that the library gets to choose the EC_METHOD */
205         if (!group) ABORT;
206
207         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
208
209         {
210                 EC_GROUP *tmp;
211                 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
212                 if (!tmp) ABORT;
213                 if (!EC_GROUP_copy(tmp, group));
214                 EC_GROUP_free(group);
215                 group = tmp;
216         }
217         
218         if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
219
220         fprintf(stdout, "Curve defined by Weierstrass equation\n     y^2 = x^3 + a*x + b  (mod 0x");
221         BN_print_fp(stdout, p);
222         fprintf(stdout, ")\n     a = 0x");
223         BN_print_fp(stdout, a);
224         fprintf(stdout, "\n     b = 0x");
225         BN_print_fp(stdout, b);
226         fprintf(stdout, "\n");
227
228         P = EC_POINT_new(group);
229         Q = EC_POINT_new(group);
230         R = EC_POINT_new(group);
231         if (!P || !Q || !R) ABORT;
232         
233         if (!EC_POINT_set_to_infinity(group, P)) ABORT;
234         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
235
236         buf[0] = 0;
237         if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
238
239         if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
240         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
241
242         x = BN_new();
243         y = BN_new();
244         z = BN_new();
245         if (!x || !y || !z) ABORT;
246
247         if (!BN_hex2bn(&x, "D")) ABORT;
248         if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
249         if (!EC_POINT_is_on_curve(group, Q, ctx))
250                 {
251                 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
252                 fprintf(stderr, "Point is not on curve: x = 0x");
253                 BN_print_fp(stderr, x);
254                 fprintf(stderr, ", y = 0x");
255                 BN_print_fp(stderr, y);
256                 fprintf(stderr, "\n");
257                 ABORT;
258                 }
259
260         fprintf(stdout, "A cyclic subgroup:\n");
261         k = 100;
262         do
263                 {
264                 if (k-- == 0) ABORT;
265
266                 if (EC_POINT_is_at_infinity(group, P))
267                         fprintf(stdout, "     point at infinity\n");
268                 else
269                         {
270                         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
271
272                         fprintf(stdout, "     x = 0x");
273                         BN_print_fp(stdout, x);
274                         fprintf(stdout, ", y = 0x");
275                         BN_print_fp(stdout, y);
276                         fprintf(stdout, "\n");
277                         }
278                 
279                 if (!EC_POINT_copy(R, P)) ABORT;
280                 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
281
282 #if 0 /* optional */
283                 {
284                         EC_POINT *points[3];
285                 
286                         points[0] = R;
287                         points[1] = Q;
288                         points[2] = P;
289                         if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
290                 }
291 #endif
292
293                 }
294         while (!EC_POINT_is_at_infinity(group, P));
295
296         if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
297         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
298
299         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
300         if (len == 0) ABORT;
301         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
302         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
303         fprintf(stdout, "Generator as octect string, compressed form:\n     ");
304         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
305         
306         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
307         if (len == 0) ABORT;
308         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
309         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
310         fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n     ");
311         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
312         
313         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
314         if (len == 0) ABORT;
315         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
316         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
317         fprintf(stdout, "\nGenerator as octect string, hybrid form:\n     ");
318         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
319         
320         if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
321         fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n     X = 0x");
322         BN_print_fp(stdout, x);
323         fprintf(stdout, ", Y = 0x");
324         BN_print_fp(stdout, y);
325         fprintf(stdout, ", Z = 0x");
326         BN_print_fp(stdout, z);
327         fprintf(stdout, "\n");
328
329         if (!EC_POINT_invert(group, P, ctx)) ABORT;
330         if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
331
332
333         /* Curve P-192 (FIPS PUB 186-2, App. 6) */
334         
335         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
336         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
337         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
338         if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
339         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
340
341         if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
342         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
343         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
344         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
345         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
346
347         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
348         fprintf(stdout, "\nNIST curve P-192 -- Generator:\n     x = 0x");
349         BN_print_fp(stdout, x);
350         fprintf(stdout, "\n     y = 0x");
351         BN_print_fp(stdout, y);
352         fprintf(stdout, "\n");
353         /* G_y value taken from the standard: */
354         if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
355         if (0 != BN_cmp(y, z)) ABORT;
356
357         fprintf(stdout, "verify degree ...");
358         if (EC_GROUP_get_degree(group) != 192) ABORT;
359         fprintf(stdout, " ok\n");
360         
361         fprintf(stdout, "verify group order ...");
362         fflush(stdout);
363         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
364         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
365         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
366         fprintf(stdout, ".");
367         fflush(stdout);
368         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
369         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
370         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
371         fprintf(stdout, " ok\n");
372
373         if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
374         if (!EC_GROUP_copy(P_192, group)) ABORT;
375
376
377         /* Curve P-224 (FIPS PUB 186-2, App. 6) */
378         
379         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
380         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
381         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
382         if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
383         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
384
385         if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
386         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
387         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
388         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
389         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
390
391         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
392         fprintf(stdout, "\nNIST curve P-224 -- Generator:\n     x = 0x");
393         BN_print_fp(stdout, x);
394         fprintf(stdout, "\n     y = 0x");
395         BN_print_fp(stdout, y);
396         fprintf(stdout, "\n");
397         /* G_y value taken from the standard: */
398         if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
399         if (0 != BN_cmp(y, z)) ABORT;
400         
401         fprintf(stdout, "verify degree ...");
402         if (EC_GROUP_get_degree(group) != 224) ABORT;
403         fprintf(stdout, " ok\n");
404         
405         fprintf(stdout, "verify group order ...");
406         fflush(stdout);
407         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
408         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
409         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
410         fprintf(stdout, ".");
411         fflush(stdout);
412         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
413         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
414         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
415         fprintf(stdout, " ok\n");
416
417         if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
418         if (!EC_GROUP_copy(P_224, group)) ABORT;
419
420
421         /* Curve P-256 (FIPS PUB 186-2, App. 6) */
422         
423         if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
424         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
425         if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
426         if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
427         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
428
429         if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
430         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
431         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
432         if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
433                 "84F3B9CAC2FC632551")) ABORT;
434         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
435
436         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
437         fprintf(stdout, "\nNIST curve P-256 -- Generator:\n     x = 0x");
438         BN_print_fp(stdout, x);
439         fprintf(stdout, "\n     y = 0x");
440         BN_print_fp(stdout, y);
441         fprintf(stdout, "\n");
442         /* G_y value taken from the standard: */
443         if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
444         if (0 != BN_cmp(y, z)) ABORT;
445         
446         fprintf(stdout, "verify degree ...");
447         if (EC_GROUP_get_degree(group) != 256) ABORT;
448         fprintf(stdout, " ok\n");
449         
450         fprintf(stdout, "verify group order ...");
451         fflush(stdout);
452         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
453         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
454         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
455         fprintf(stdout, ".");
456         fflush(stdout);
457         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
458         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
459         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
460         fprintf(stdout, " ok\n");
461
462         if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
463         if (!EC_GROUP_copy(P_256, group)) ABORT;
464
465
466         /* Curve P-384 (FIPS PUB 186-2, App. 6) */
467         
468         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
469                 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
470         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
471         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
472                 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
473         if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
474                 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
475         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
476
477         if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
478                 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
479         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
480         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
481         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
482                 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
483         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
484
485         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
486         fprintf(stdout, "\nNIST curve P-384 -- Generator:\n     x = 0x");
487         BN_print_fp(stdout, x);
488         fprintf(stdout, "\n     y = 0x");
489         BN_print_fp(stdout, y);
490         fprintf(stdout, "\n");
491         /* G_y value taken from the standard: */
492         if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
493                 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
494         if (0 != BN_cmp(y, z)) ABORT;
495         
496         fprintf(stdout, "verify degree ...");
497         if (EC_GROUP_get_degree(group) != 384) ABORT;
498         fprintf(stdout, " ok\n");
499         
500         fprintf(stdout, "verify group order ...");
501         fflush(stdout);
502         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
503         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
504         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
505         fprintf(stdout, ".");
506         fflush(stdout);
507         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
508         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
509         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
510         fprintf(stdout, " ok\n");
511
512         if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
513         if (!EC_GROUP_copy(P_384, group)) ABORT;
514
515
516         /* Curve P-521 (FIPS PUB 186-2, App. 6) */
517         
518         if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
519                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
520                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
521         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
522         if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
523                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
524                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
525         if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
526                 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
527                 "DF883D2C34F1EF451FD46B503F00")) ABORT;
528         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
529
530         if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
531                 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
532                 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
533         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
534         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
535         if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
536                 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
537                 "C9B8899C47AEBB6FB71E91386409")) ABORT;
538         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
539
540         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
541         fprintf(stdout, "\nNIST curve P-521 -- Generator:\n     x = 0x");
542         BN_print_fp(stdout, x);
543         fprintf(stdout, "\n     y = 0x");
544         BN_print_fp(stdout, y);
545         fprintf(stdout, "\n");
546         /* G_y value taken from the standard: */
547         if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
548                 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
549                 "7086A272C24088BE94769FD16650")) ABORT;
550         if (0 != BN_cmp(y, z)) ABORT;
551         
552         fprintf(stdout, "verify degree ...");
553         if (EC_GROUP_get_degree(group) != 521) ABORT;
554         fprintf(stdout, " ok\n");
555         
556         fprintf(stdout, "verify group order ...");
557         fflush(stdout);
558         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
559         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
560         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
561         fprintf(stdout, ".");
562         fflush(stdout);
563         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
564         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
565         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
566         fprintf(stdout, " ok\n");
567
568         if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
569         if (!EC_GROUP_copy(P_521, group)) ABORT;
570
571
572         /* more tests using the last curve */
573
574         if (!EC_POINT_copy(Q, P)) ABORT;
575         if (EC_POINT_is_at_infinity(group, Q)) ABORT;
576         if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
577         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
578         if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
579
580         if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
581         if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
582         if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
583
584         {
585                 const EC_POINT *points[3];
586                 const BIGNUM *scalars[3];
587         
588                 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
589                 points[0] = Q;
590                 points[1] = Q;
591                 points[2] = Q;
592
593                 if (!BN_add(y, z, BN_value_one())) ABORT;
594                 if (BN_is_odd(y)) ABORT;
595                 if (!BN_rshift1(y, y)) ABORT;
596                 scalars[0] = y; /* (group order + 1)/2,  so  y*Q + y*Q = Q */
597                 scalars[1] = y;
598
599                 fprintf(stdout, "combined multiplication ...");
600                 fflush(stdout);
601
602                 /* z is still the group order */
603                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
604                 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
605                 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
606                 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
607
608                 fprintf(stdout, ".");
609                 fflush(stdout);
610
611                 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
612                 if (!BN_add(z, z, y)) ABORT;
613                 BN_set_sign(z, 1);
614                 scalars[0] = y;
615                 scalars[1] = z; /* z = -(order + y) */
616
617                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
618                 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
619
620                 fprintf(stdout, ".");
621                 fflush(stdout);
622
623                 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
624                 if (!BN_add(z, x, y)) ABORT;
625                 BN_set_sign(z, 1);
626                 scalars[0] = x;
627                 scalars[1] = y;
628                 scalars[2] = z; /* z = -(x+y) */
629
630                 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
631                 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
632
633                 fprintf(stdout, " ok\n\n");
634         }
635
636
637 #if 0
638         timings(P_192, 0, ctx);
639         timings(P_192, 1, ctx);
640         timings(P_224, 0, ctx);
641         timings(P_224, 1, ctx);
642         timings(P_256, 0, ctx);
643         timings(P_256, 1, ctx);
644         timings(P_384, 0, ctx);
645         timings(P_384, 1, ctx);
646         timings(P_521, 0, ctx);
647         timings(P_521, 1, ctx);
648 #endif
649
650
651         if (ctx)
652                 BN_CTX_free(ctx);
653         BN_free(p); BN_free(a); BN_free(b);
654         EC_GROUP_free(group);
655         EC_POINT_free(P);
656         EC_POINT_free(Q);
657         EC_POINT_free(R);
658         BN_free(x); BN_free(y); BN_free(z);
659
660         if (P_192) EC_GROUP_free(P_192);
661         if (P_224) EC_GROUP_free(P_224);
662         if (P_256) EC_GROUP_free(P_256);
663         if (P_384) EC_GROUP_free(P_384);
664         if (P_521) EC_GROUP_free(P_521);
665
666         }
667
668 /* Change test based on whether binary point compression is enabled or not. */
669 #ifdef OPENSSL_EC_BIN_PT_COMP
670 #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
671         if (!BN_hex2bn(&x, _x)) ABORT; \
672         if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
673         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
674         if (!BN_hex2bn(&z, _order)) ABORT; \
675         if (!BN_hex2bn(&cof, _cof)) ABORT; \
676         if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
677         if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
678         fprintf(stdout, "\n%s -- Generator:\n     x = 0x", _name); \
679         BN_print_fp(stdout, x); \
680         fprintf(stdout, "\n     y = 0x"); \
681         BN_print_fp(stdout, y); \
682         fprintf(stdout, "\n"); \
683         /* G_y value taken from the standard: */ \
684         if (!BN_hex2bn(&z, _y)) ABORT; \
685         if (0 != BN_cmp(y, z)) ABORT;
686 #else 
687 #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
688         if (!BN_hex2bn(&x, _x)) ABORT; \
689         if (!BN_hex2bn(&y, _y)) ABORT; \
690         if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
691         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
692         if (!BN_hex2bn(&z, _order)) ABORT; \
693         if (!BN_hex2bn(&cof, _cof)) ABORT; \
694         if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
695         fprintf(stdout, "\n%s -- Generator:\n     x = 0x", _name); \
696         BN_print_fp(stdout, x); \
697         fprintf(stdout, "\n     y = 0x"); \
698         BN_print_fp(stdout, y); \
699         fprintf(stdout, "\n");
700 #endif
701
702 #define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
703         if (!BN_hex2bn(&p, _p)) ABORT; \
704         if (!BN_hex2bn(&a, _a)) ABORT; \
705         if (!BN_hex2bn(&b, _b)) ABORT; \
706         if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
707         CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
708         fprintf(stdout, "verify degree ..."); \
709         if (EC_GROUP_get_degree(group) != _degree) ABORT; \
710         fprintf(stdout, " ok\n"); \
711         fprintf(stdout, "verify group order ..."); \
712         fflush(stdout); \
713         if (!EC_GROUP_get_order(group, z, ctx)) ABORT; \
714         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
715         if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
716         fprintf(stdout, "."); \
717         fflush(stdout); \
718         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; \
719         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
720         if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
721         fprintf(stdout, " ok\n"); \
722         if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
723         if (!EC_GROUP_copy(_variable, group)) ABORT;
724
725 void char2_field_tests()
726         {       
727         BN_CTX *ctx = NULL;
728         BIGNUM *p, *a, *b;
729         EC_GROUP *group;
730         EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 = NULL, *C2_K571 = NULL;
731         EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 = NULL, *C2_B571 = NULL;
732         EC_POINT *P, *Q, *R;
733         BIGNUM *x, *y, *z, *cof;
734         unsigned char buf[100];
735         size_t i, len;
736         int k;
737         
738 #if 1 /* optional */
739         ctx = BN_CTX_new();
740         if (!ctx) ABORT;
741 #endif
742
743         p = BN_new();
744         a = BN_new();
745         b = BN_new();
746         if (!p || !a || !b) ABORT;
747
748         if (!BN_hex2bn(&p, "13")) ABORT;
749         if (!BN_hex2bn(&a, "3")) ABORT;
750         if (!BN_hex2bn(&b, "1")) ABORT;
751         
752         group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
753                                                         * so that the library gets to choose the EC_METHOD */
754         if (!group) ABORT;
755         if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
756
757         {
758                 EC_GROUP *tmp;
759                 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
760                 if (!tmp) ABORT;
761                 if (!EC_GROUP_copy(tmp, group));
762                 EC_GROUP_free(group);
763                 group = tmp;
764         }
765         
766         if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)) ABORT;
767
768         fprintf(stdout, "Curve defined by Weierstrass equation\n     y^2 + x*y = x^3 + a*x^2 + b  (mod 0x");
769         BN_print_fp(stdout, p);
770         fprintf(stdout, ")\n     a = 0x");
771         BN_print_fp(stdout, a);
772         fprintf(stdout, "\n     b = 0x");
773         BN_print_fp(stdout, b);
774         fprintf(stdout, "\n(0x... means binary polynomial)\n");
775
776         P = EC_POINT_new(group);
777         Q = EC_POINT_new(group);
778         R = EC_POINT_new(group);
779         if (!P || !Q || !R) ABORT;
780         
781         if (!EC_POINT_set_to_infinity(group, P)) ABORT;
782         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
783
784         buf[0] = 0;
785         if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
786
787         if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
788         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
789
790         x = BN_new();
791         y = BN_new();
792         z = BN_new();
793         cof = BN_new();
794         if (!x || !y || !z || !cof) ABORT;
795
796         if (!BN_hex2bn(&x, "6")) ABORT;
797 /* Change test based on whether binary point compression is enabled or not. */
798 #ifdef OPENSSL_EC_BIN_PT_COMP
799         if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx)) ABORT;
800 #else
801         if (!BN_hex2bn(&y, "8")) ABORT;
802         if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
803 #endif
804         if (!EC_POINT_is_on_curve(group, Q, ctx))
805                 {
806 /* Change test based on whether binary point compression is enabled or not. */
807 #ifdef OPENSSL_EC_BIN_PT_COMP
808                 if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
809 #endif
810                 fprintf(stderr, "Point is not on curve: x = 0x");
811                 BN_print_fp(stderr, x);
812                 fprintf(stderr, ", y = 0x");
813                 BN_print_fp(stderr, y);
814                 fprintf(stderr, "\n");
815                 ABORT;
816                 }
817
818         fprintf(stdout, "A cyclic subgroup:\n");
819         k = 100;
820         do
821                 {
822                 if (k-- == 0) ABORT;
823
824                 if (EC_POINT_is_at_infinity(group, P))
825                         fprintf(stdout, "     point at infinity\n");
826                 else
827                         {
828                         if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT;
829
830                         fprintf(stdout, "     x = 0x");
831                         BN_print_fp(stdout, x);
832                         fprintf(stdout, ", y = 0x");
833                         BN_print_fp(stdout, y);
834                         fprintf(stdout, "\n");
835                         }
836                 
837                 if (!EC_POINT_copy(R, P)) ABORT;
838                 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
839                 }
840         while (!EC_POINT_is_at_infinity(group, P));
841
842         if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
843         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
844
845 /* Change test based on whether binary point compression is enabled or not. */
846 #ifdef OPENSSL_EC_BIN_PT_COMP
847         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
848         if (len == 0) ABORT;
849         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
850         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
851         fprintf(stdout, "Generator as octet string, compressed form:\n     ");
852         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
853 #endif
854         
855         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
856         if (len == 0) ABORT;
857         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
858         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
859         fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n     ");
860         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
861         
862 /* Change test based on whether binary point compression is enabled or not. */
863 #ifdef OPENSSL_EC_BIN_PT_COMP
864         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
865         if (len == 0) ABORT;
866         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
867         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
868         fprintf(stdout, "\nGenerator as octet string, hybrid form:\n     ");
869         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
870 #endif
871
872         fprintf(stdout, "\n");
873         
874         if (!EC_POINT_invert(group, P, ctx)) ABORT;
875         if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
876
877
878         /* Curve K-163 (FIPS PUB 186-2, App. 6) */
879         CHAR2_CURVE_TEST
880                 (
881                 "NIST curve K-163",
882                 "0800000000000000000000000000000000000000C9",
883                 "1",
884                 "1",
885                 "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
886                 "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
887                 1,
888                 "04000000000000000000020108A2E0CC0D99F8A5EF",
889                 "2",
890                 163,
891                 C2_K163
892                 );
893
894         /* Curve B-163 (FIPS PUB 186-2, App. 6) */
895         CHAR2_CURVE_TEST
896                 (
897                 "NIST curve B-163",
898                 "0800000000000000000000000000000000000000C9",
899                 "1",
900                 "020A601907B8C953CA1481EB10512F78744A3205FD",
901                 "03F0EBA16286A2D57EA0991168D4994637E8343E36",
902                 "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
903                 1,
904                 "040000000000000000000292FE77E70C12A4234C33",
905                 "2",
906                 163,
907                 C2_B163
908                 );
909
910         /* Curve K-233 (FIPS PUB 186-2, App. 6) */
911         CHAR2_CURVE_TEST
912                 (
913                 "NIST curve K-233",
914                 "020000000000000000000000000000000000000004000000000000000001",
915                 "0",
916                 "1",
917                 "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
918                 "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
919                 0,
920                 "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
921                 "4",
922                 233,
923                 C2_K233
924                 );
925
926         /* Curve B-233 (FIPS PUB 186-2, App. 6) */
927         CHAR2_CURVE_TEST
928                 (
929                 "NIST curve B-233",
930                 "020000000000000000000000000000000000000004000000000000000001",
931                 "000000000000000000000000000000000000000000000000000000000001",
932                 "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
933                 "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
934                 "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
935                 1,
936                 "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
937                 "2",
938                 233,
939                 C2_B233
940                 );
941
942         /* Curve K-283 (FIPS PUB 186-2, App. 6) */
943         CHAR2_CURVE_TEST
944                 (
945                 "NIST curve K-283",
946                 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
947                 "0",
948                 "1",
949                 "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
950                 "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
951                 0,
952                 "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
953                 "4",
954                 283,
955                 C2_K283
956                 );
957
958         /* Curve B-283 (FIPS PUB 186-2, App. 6) */
959         CHAR2_CURVE_TEST
960                 (
961                 "NIST curve B-283",
962                 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
963                 "000000000000000000000000000000000000000000000000000000000000000000000001",
964                 "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
965                 "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
966                 "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
967                 1,
968                 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
969                 "2",
970                 283,
971                 C2_B283
972                 );
973
974         /* Curve K-409 (FIPS PUB 186-2, App. 6) */
975         CHAR2_CURVE_TEST
976                 (
977                 "NIST curve K-409",
978                 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
979                 "0",
980                 "1",
981                 "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
982                 "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
983                 1,
984                 "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
985                 "4",
986                 409,
987                 C2_K409
988                 );
989
990         /* Curve B-409 (FIPS PUB 186-2, App. 6) */
991         CHAR2_CURVE_TEST
992                 (
993                 "NIST curve B-409",
994                 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
995                 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
996                 "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
997                 "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
998                 "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
999                 1,
1000                 "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
1001                 "2",
1002                 409,
1003                 C2_B409
1004                 );
1005
1006         /* Curve K-571 (FIPS PUB 186-2, App. 6) */
1007         CHAR2_CURVE_TEST
1008                 (
1009                 "NIST curve K-571",
1010                 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1011                 "0",
1012                 "1",
1013                 "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
1014                 "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
1015                 0,
1016                 "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
1017                 "4",
1018                 571,
1019                 C2_K571
1020                 );
1021
1022         /* Curve B-571 (FIPS PUB 186-2, App. 6) */
1023         CHAR2_CURVE_TEST
1024                 (
1025                 "NIST curve B-571",
1026                 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1027                 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1028                 "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
1029                 "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
1030                 "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
1031                 1,
1032                 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
1033                 "2",
1034                 571,
1035                 C2_B571
1036                 );
1037
1038         /* more tests using the last curve */
1039
1040         if (!EC_POINT_copy(Q, P)) ABORT;
1041         if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1042         if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
1043         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
1044         if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
1045
1046         if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
1047         if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
1048         if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
1049
1050         {
1051                 const EC_POINT *points[3];
1052                 const BIGNUM *scalars[3];
1053         
1054                 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1055                 points[0] = Q;
1056                 points[1] = Q;
1057                 points[2] = Q;
1058
1059                 if (!BN_add(y, z, BN_value_one())) ABORT;
1060                 if (BN_is_odd(y)) ABORT;
1061                 if (!BN_rshift1(y, y)) ABORT;
1062                 scalars[0] = y; /* (group order + 1)/2,  so  y*Q + y*Q = Q */
1063                 scalars[1] = y;
1064
1065                 fprintf(stdout, "combined multiplication ...");
1066                 fflush(stdout);
1067
1068                 /* z is still the group order */
1069                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1070                 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
1071                 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
1072                 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
1073
1074                 fprintf(stdout, ".");
1075                 fflush(stdout);
1076
1077                 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
1078                 if (!BN_add(z, z, y)) ABORT;
1079                 BN_set_sign(z, 1);
1080                 scalars[0] = y;
1081                 scalars[1] = z; /* z = -(order + y) */
1082
1083                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1084                 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1085
1086                 fprintf(stdout, ".");
1087                 fflush(stdout);
1088
1089                 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
1090                 if (!BN_add(z, x, y)) ABORT;
1091                 BN_set_sign(z, 1);
1092                 scalars[0] = x;
1093                 scalars[1] = y;
1094                 scalars[2] = z; /* z = -(x+y) */
1095
1096                 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
1097                 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1098
1099                 fprintf(stdout, " ok\n\n");
1100         }
1101
1102
1103 #if 0
1104         timings(C2_K163, 0, ctx);
1105         timings(C2_K163, 1, ctx);
1106         timings(C2_B163, 0, ctx);
1107         timings(C2_B163, 1, ctx);
1108         timings(C2_K233, 0, ctx);
1109         timings(C2_K233, 1, ctx);
1110         timings(C2_B233, 0, ctx);
1111         timings(C2_B233, 1, ctx);
1112         timings(C2_K283, 0, ctx);
1113         timings(C2_K283, 1, ctx);
1114         timings(C2_B283, 0, ctx);
1115         timings(C2_B283, 1, ctx);
1116         timings(C2_K409, 0, ctx);
1117         timings(C2_K409, 1, ctx);
1118         timings(C2_B409, 0, ctx);
1119         timings(C2_B409, 1, ctx);
1120         timings(C2_K571, 0, ctx);
1121         timings(C2_K571, 1, ctx);
1122         timings(C2_B571, 0, ctx);
1123         timings(C2_B571, 1, ctx);
1124 #endif
1125
1126
1127         if (ctx)
1128                 BN_CTX_free(ctx);
1129         BN_free(p); BN_free(a); BN_free(b);
1130         EC_GROUP_free(group);
1131         EC_POINT_free(P);
1132         EC_POINT_free(Q);
1133         EC_POINT_free(R);
1134         BN_free(x); BN_free(y); BN_free(z); BN_free(cof);
1135
1136         if (C2_K163) EC_GROUP_free(C2_K163);
1137         if (C2_B163) EC_GROUP_free(C2_B163);
1138         if (C2_K233) EC_GROUP_free(C2_K233);
1139         if (C2_B233) EC_GROUP_free(C2_B233);
1140         if (C2_K283) EC_GROUP_free(C2_K283);
1141         if (C2_B283) EC_GROUP_free(C2_B283);
1142         if (C2_K409) EC_GROUP_free(C2_K409);
1143         if (C2_B409) EC_GROUP_free(C2_B409);
1144         if (C2_K571) EC_GROUP_free(C2_K571);
1145         if (C2_B571) EC_GROUP_free(C2_B571);
1146
1147         }
1148
1149 void internal_curve_test(void)
1150         {
1151         EC_builtin_curve *curves = NULL;
1152         size_t crv_len = 0, n = 0;
1153         int    ok = 1;
1154
1155         crv_len = EC_get_builtin_curves(NULL, 0);
1156
1157         curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
1158
1159         if (curves == NULL)
1160                 return;
1161
1162         if (!EC_get_builtin_curves(curves, crv_len))
1163                 {
1164                 OPENSSL_free(curves);
1165                 return;
1166                 }
1167
1168         fprintf(stdout, "testing internal curves: ");
1169                 
1170         for (n = 0; n < crv_len; n++)
1171                 {
1172                 EC_GROUP *group = NULL;
1173                 int nid = curves[n].nid;
1174                 if ((group = EC_GROUP_new_by_nid(nid)) == NULL)
1175                         {
1176                         ok = 0;
1177                         fprintf(stdout, "\nEC_GROUP_new_by_nid() failed with"
1178                                 " curve %s\n", OBJ_nid2sn(nid));
1179                         /* try next curve */
1180                         continue;
1181                         }
1182                 if (!EC_GROUP_check(group, NULL))
1183                         {
1184                         ok = 0;
1185                         fprintf(stdout, "\nEC_GROUP_check() failed with"
1186                                 " curve %s\n", OBJ_nid2sn(nid));
1187                         EC_GROUP_free(group);
1188                         /* try the next curve */
1189                         continue;
1190                         }
1191                 fprintf(stdout, ".");
1192                 fflush(stdout);
1193                 EC_GROUP_free(group);
1194                 }
1195         if (ok)
1196                 fprintf(stdout, " ok\n");
1197         else
1198                 fprintf(stdout, " failed\n");
1199         OPENSSL_free(curves);
1200         return;
1201         }
1202
1203 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1204
1205 int main(int argc, char *argv[])
1206         {       
1207         
1208         /* enable memory leak checking unless explicitly disabled */
1209         if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
1210                 {
1211                 CRYPTO_malloc_debug_init();
1212                 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1213                 }
1214         else
1215                 {
1216                 /* OPENSSL_DEBUG_MEMORY=off */
1217                 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
1218                 }
1219         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1220         ERR_load_crypto_strings();
1221
1222         RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
1223
1224         prime_field_tests();
1225         puts("");
1226         char2_field_tests();
1227         /* test the internal curves */
1228         internal_curve_test();
1229
1230         ENGINE_cleanup();
1231         CRYPTO_cleanup_all_ex_data();
1232         ERR_free_strings();
1233         ERR_remove_state(0);
1234         CRYPTO_mem_leaks_fp(stderr);
1235         
1236         return 0;
1237         }
1238 #endif