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