Import OpenSSL-0.9.8i.
[dragonfly.git] / crypto / openssl-0.9.7d / test / ectest.c
1 /* crypto/ec/ectest.c */
2 /* ====================================================================
3  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55
56 #include <stdio.h>
57 #include <stdlib.h>
58 #ifdef FLAT_INC
59 #include "e_os.h"
60 #else
61 #include "../e_os.h"
62 #endif
63 #include <string.h>
64 #include <time.h>
65
66
67 #ifdef OPENSSL_NO_EC
68 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
69 #else
70
71
72 #include <openssl/ec.h>
73 #ifndef OPENSSL_NO_ENGINE
74 #include <openssl/engine.h>
75 #endif
76 #include <openssl/err.h>
77
78 #define ABORT do { \
79         fflush(stdout); \
80         fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
81         ERR_print_errors_fp(stderr); \
82         EXIT(1); \
83 } while (0)
84
85 #if 0
86 static void timings(EC_GROUP *group, int multi, BN_CTX *ctx)
87         {
88         clock_t clck;
89         int i, j;
90         BIGNUM *s, *s0;
91         EC_POINT *P;
92                 
93         s = BN_new();
94         s0 = BN_new();
95         if (s == NULL || s0 == NULL) ABORT;
96
97         if (!EC_GROUP_get_curve_GFp(group, s, NULL, NULL, ctx)) ABORT;
98         fprintf(stdout, "Timings for %d bit prime, ", (int)BN_num_bits(s));
99         if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
100         fprintf(stdout, "%d bit scalars ", (int)BN_num_bits(s));
101         fflush(stdout);
102
103         P = EC_POINT_new(group);
104         if (P == NULL) ABORT;
105         EC_POINT_copy(P, EC_GROUP_get0_generator(group));
106
107         clck = clock();
108         for (i = 0; i < 10; i++)
109                 {
110                 if (!BN_pseudo_rand(s, BN_num_bits(s), 0, 0)) ABORT;
111                 if (multi)
112                         {
113                         if (!BN_pseudo_rand(s0, BN_num_bits(s), 0, 0)) ABORT;
114                         }
115                 for (j = 0; j < 10; j++)
116                         {
117                         if (!EC_POINT_mul(group, P, s, multi ? P : NULL, multi ? s0 : NULL, ctx)) ABORT;
118                         }
119                 fprintf(stdout, ".");
120                 fflush(stdout);
121                 }
122         fprintf(stdout, "\n");
123         
124         clck = clock() - clck;
125
126 #ifdef CLOCKS_PER_SEC
127         /* "To determine the time in seconds, the value returned
128          * by the clock function should be divided by the value
129          * of the macro CLOCKS_PER_SEC."
130          *                                       -- ISO/IEC 9899 */
131 #       define UNIT "s"
132 #else
133         /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
134          *                            -- cc on NeXTstep/OpenStep */
135 #       define UNIT "units"
136 #       define CLOCKS_PER_SEC 1
137 #endif
138
139         fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
140                 multi ? "s*P+t*Q operations" : "point multiplications",
141                 (double)clck/CLOCKS_PER_SEC);
142         fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
143
144         EC_POINT_free(P);
145         BN_free(s);
146         BN_free(s0);
147         }
148 #endif
149
150 int main(int argc, char *argv[])
151         {       
152         BN_CTX *ctx = NULL;
153         BIGNUM *p, *a, *b;
154         EC_GROUP *group;
155         EC_GROUP *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
156         EC_POINT *P, *Q, *R;
157         BIGNUM *x, *y, *z;
158         unsigned char buf[100];
159         size_t i, len;
160         int k;
161         
162         /* enable memory leak checking unless explicitly disabled */
163         if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
164                 {
165                 CRYPTO_malloc_debug_init();
166                 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
167                 }
168         else
169                 {
170                 /* OPENSSL_DEBUG_MEMORY=off */
171                 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
172                 }
173         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
174         ERR_load_crypto_strings();
175
176 #if 1 /* optional */
177         ctx = BN_CTX_new();
178         if (!ctx) ABORT;
179 #endif
180
181         p = BN_new();
182         a = BN_new();
183         b = BN_new();
184         if (!p || !a || !b) ABORT;
185
186         if (!BN_hex2bn(&p, "17")) ABORT;
187         if (!BN_hex2bn(&a, "1")) ABORT;
188         if (!BN_hex2bn(&b, "1")) ABORT;
189         
190         group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
191                                                      * so that the library gets to choose the EC_METHOD */
192         if (!group) ABORT;
193
194         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
195
196         {
197                 EC_GROUP *tmp;
198                 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
199                 if (!tmp) ABORT;
200                 if (!EC_GROUP_copy(tmp, group));
201                 EC_GROUP_free(group);
202                 group = tmp;
203         }
204         
205         if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
206
207         fprintf(stdout, "Curve defined by Weierstrass equation\n     y^2 = x^3 + a*x + b  (mod 0x");
208         BN_print_fp(stdout, p);
209         fprintf(stdout, ")\n     a = 0x");
210         BN_print_fp(stdout, a);
211         fprintf(stdout, "\n     b = 0x");
212         BN_print_fp(stdout, b);
213         fprintf(stdout, "\n");
214
215         P = EC_POINT_new(group);
216         Q = EC_POINT_new(group);
217         R = EC_POINT_new(group);
218         if (!P || !Q || !R) ABORT;
219         
220         if (!EC_POINT_set_to_infinity(group, P)) ABORT;
221         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
222
223         buf[0] = 0;
224         if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
225
226         if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
227         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
228
229         x = BN_new();
230         y = BN_new();
231         z = BN_new();
232         if (!x || !y || !z) ABORT;
233
234         if (!BN_hex2bn(&x, "D")) ABORT;
235         if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
236         if (!EC_POINT_is_on_curve(group, Q, ctx))
237                 {
238                 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
239                 fprintf(stderr, "Point is not on curve: x = 0x");
240                 BN_print_fp(stderr, x);
241                 fprintf(stderr, ", y = 0x");
242                 BN_print_fp(stderr, y);
243                 fprintf(stderr, "\n");
244                 ABORT;
245                 }
246
247         fprintf(stdout, "A cyclic subgroup:\n");
248         k = 100;
249         do
250                 {
251                 if (k-- == 0) ABORT;
252
253                 if (EC_POINT_is_at_infinity(group, P))
254                         fprintf(stdout, "     point at infinity\n");
255                 else
256                         {
257                         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
258
259                         fprintf(stdout, "     x = 0x");
260                         BN_print_fp(stdout, x);
261                         fprintf(stdout, ", y = 0x");
262                         BN_print_fp(stdout, y);
263                         fprintf(stdout, "\n");
264                         }
265                 
266                 if (!EC_POINT_copy(R, P)) ABORT;
267                 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
268
269 #if 0 /* optional */
270                 {
271                         EC_POINT *points[3];
272                 
273                         points[0] = R;
274                         points[1] = Q;
275                         points[2] = P;
276                         if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
277                 }
278 #endif
279
280                 }
281         while (!EC_POINT_is_at_infinity(group, P));
282
283         if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
284         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
285
286         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
287         if (len == 0) ABORT;
288         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
289         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
290         fprintf(stdout, "Generator as octect string, compressed form:\n     ");
291         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
292         
293         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
294         if (len == 0) ABORT;
295         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
296         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
297         fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n     ");
298         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
299         
300         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
301         if (len == 0) ABORT;
302         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
303         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
304         fprintf(stdout, "\nGenerator as octect string, hybrid form:\n     ");
305         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
306         
307         if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
308         fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n     X = 0x");
309         BN_print_fp(stdout, x);
310         fprintf(stdout, ", Y = 0x");
311         BN_print_fp(stdout, y);
312         fprintf(stdout, ", Z = 0x");
313         BN_print_fp(stdout, z);
314         fprintf(stdout, "\n");
315
316         if (!EC_POINT_invert(group, P, ctx)) ABORT;
317         if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
318
319
320         /* Curve P-192 (FIPS PUB 186-2, App. 6) */
321         
322         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
323         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
324         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
325         if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
326         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
327
328         if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
329         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
330         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
331         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
332         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
333
334         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
335         fprintf(stdout, "\nNIST curve P-192 -- Generator:\n     x = 0x");
336         BN_print_fp(stdout, x);
337         fprintf(stdout, "\n     y = 0x");
338         BN_print_fp(stdout, y);
339         fprintf(stdout, "\n");
340         /* G_y value taken from the standard: */
341         if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
342         if (0 != BN_cmp(y, z)) ABORT;
343         
344         fprintf(stdout, "verify group order ...");
345         fflush(stdout);
346         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
347         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
348         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
349         fprintf(stdout, ".");
350         fflush(stdout);
351         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
352         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
353         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
354         fprintf(stdout, " ok\n");
355
356         if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
357         if (!EC_GROUP_copy(P_192, group)) ABORT;
358
359
360         /* Curve P-224 (FIPS PUB 186-2, App. 6) */
361         
362         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
363         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
364         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
365         if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
366         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
367
368         if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
369         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
370         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
371         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
372         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
373
374         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
375         fprintf(stdout, "\nNIST curve P-224 -- Generator:\n     x = 0x");
376         BN_print_fp(stdout, x);
377         fprintf(stdout, "\n     y = 0x");
378         BN_print_fp(stdout, y);
379         fprintf(stdout, "\n");
380         /* G_y value taken from the standard: */
381         if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
382         if (0 != BN_cmp(y, z)) ABORT;
383         
384         fprintf(stdout, "verify group order ...");
385         fflush(stdout);
386         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
387         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
388         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
389         fprintf(stdout, ".");
390         fflush(stdout);
391         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
392         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
393         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
394         fprintf(stdout, " ok\n");
395
396         if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
397         if (!EC_GROUP_copy(P_224, group)) ABORT;
398
399
400         /* Curve P-256 (FIPS PUB 186-2, App. 6) */
401         
402         if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
403         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
404         if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
405         if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
406         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
407
408         if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
409         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
410         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
411         if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
412                 "84F3B9CAC2FC632551")) ABORT;
413         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
414
415         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
416         fprintf(stdout, "\nNIST curve P-256 -- Generator:\n     x = 0x");
417         BN_print_fp(stdout, x);
418         fprintf(stdout, "\n     y = 0x");
419         BN_print_fp(stdout, y);
420         fprintf(stdout, "\n");
421         /* G_y value taken from the standard: */
422         if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
423         if (0 != BN_cmp(y, z)) ABORT;
424         
425         fprintf(stdout, "verify group order ...");
426         fflush(stdout);
427         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
428         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
429         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
430         fprintf(stdout, ".");
431         fflush(stdout);
432         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
433         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
434         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
435         fprintf(stdout, " ok\n");
436
437         if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
438         if (!EC_GROUP_copy(P_256, group)) ABORT;
439
440
441         /* Curve P-384 (FIPS PUB 186-2, App. 6) */
442         
443         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
444                 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
445         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
446         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
447                 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
448         if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
449                 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
450         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
451
452         if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
453                 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
454         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
455         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
456         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
457                 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
458         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
459
460         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
461         fprintf(stdout, "\nNIST curve P-384 -- Generator:\n     x = 0x");
462         BN_print_fp(stdout, x);
463         fprintf(stdout, "\n     y = 0x");
464         BN_print_fp(stdout, y);
465         fprintf(stdout, "\n");
466         /* G_y value taken from the standard: */
467         if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
468                 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
469         if (0 != BN_cmp(y, z)) ABORT;
470         
471         fprintf(stdout, "verify group order ...");
472         fflush(stdout);
473         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
474         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
475         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
476         fprintf(stdout, ".");
477         fflush(stdout);
478         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
479         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
480         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
481         fprintf(stdout, " ok\n");
482
483         if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
484         if (!EC_GROUP_copy(P_384, group)) ABORT;
485
486
487         /* Curve P-521 (FIPS PUB 186-2, App. 6) */
488         
489         if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
490                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
491                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
492         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
493         if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
494                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
495                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
496         if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
497                 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
498                 "DF883D2C34F1EF451FD46B503F00")) ABORT;
499         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
500
501         if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
502                 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
503                 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
504         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
505         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
506         if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
507                 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
508                 "C9B8899C47AEBB6FB71E91386409")) ABORT;
509         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
510
511         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
512         fprintf(stdout, "\nNIST curve P-521 -- Generator:\n     x = 0x");
513         BN_print_fp(stdout, x);
514         fprintf(stdout, "\n     y = 0x");
515         BN_print_fp(stdout, y);
516         fprintf(stdout, "\n");
517         /* G_y value taken from the standard: */
518         if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
519                 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
520                 "7086A272C24088BE94769FD16650")) ABORT;
521         if (0 != BN_cmp(y, z)) ABORT;
522         
523         fprintf(stdout, "verify group order ...");
524         fflush(stdout);
525         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
526         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
527         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
528         fprintf(stdout, ".");
529         fflush(stdout);
530         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
531         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
532         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
533         fprintf(stdout, " ok\n");
534
535         if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
536         if (!EC_GROUP_copy(P_521, group)) ABORT;
537
538
539         /* more tests using the last curve */
540
541         if (!EC_POINT_copy(Q, P)) ABORT;
542         if (EC_POINT_is_at_infinity(group, Q)) ABORT;
543         if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
544         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
545         if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
546
547         if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
548         if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
549         if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
550
551         {
552                 const EC_POINT *points[3];
553                 const BIGNUM *scalars[3];
554         
555                 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
556                 points[0] = Q;
557                 points[1] = Q;
558                 points[2] = Q;
559
560                 if (!BN_add(y, z, BN_value_one())) ABORT;
561                 if (BN_is_odd(y)) ABORT;
562                 if (!BN_rshift1(y, y)) ABORT;
563                 scalars[0] = y; /* (group order + 1)/2,  so  y*Q + y*Q = Q */
564                 scalars[1] = y;
565
566                 fprintf(stdout, "combined multiplication ...");
567                 fflush(stdout);
568
569                 /* z is still the group order */
570                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
571                 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
572                 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
573                 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
574
575                 fprintf(stdout, ".");
576                 fflush(stdout);
577
578                 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
579                 if (!BN_add(z, z, y)) ABORT;
580                 z->neg = 1;
581                 scalars[0] = y;
582                 scalars[1] = z; /* z = -(order + y) */
583
584                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
585                 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
586
587                 fprintf(stdout, ".");
588                 fflush(stdout);
589
590                 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
591                 if (!BN_add(z, x, y)) ABORT;
592                 z->neg = 1;
593                 scalars[0] = x;
594                 scalars[1] = y;
595                 scalars[2] = z; /* z = -(x+y) */
596
597                 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
598                 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
599
600                 fprintf(stdout, " ok\n\n");
601         }
602
603
604 #if 0
605         timings(P_192, 0, ctx);
606         timings(P_192, 1, ctx);
607         timings(P_224, 0, ctx);
608         timings(P_224, 1, ctx);
609         timings(P_256, 0, ctx);
610         timings(P_256, 1, ctx);
611         timings(P_384, 0, ctx);
612         timings(P_384, 1, ctx);
613         timings(P_521, 0, ctx);
614         timings(P_521, 1, ctx);
615 #endif
616
617
618         if (ctx)
619                 BN_CTX_free(ctx);
620         BN_free(p); BN_free(a); BN_free(b);
621         EC_GROUP_free(group);
622         EC_POINT_free(P);
623         EC_POINT_free(Q);
624         EC_POINT_free(R);
625         BN_free(x); BN_free(y); BN_free(z);
626
627         if (P_192) EC_GROUP_free(P_192);
628         if (P_224) EC_GROUP_free(P_224);
629         if (P_256) EC_GROUP_free(P_256);
630         if (P_384) EC_GROUP_free(P_384);
631         if (P_521) EC_GROUP_free(P_521);
632
633 #ifndef OPENSSL_NO_ENGINE
634         ENGINE_cleanup();
635 #endif
636         CRYPTO_cleanup_all_ex_data();
637         ERR_free_strings();
638         ERR_remove_state(0);
639         CRYPTO_mem_leaks_fp(stderr);
640         
641         return 0;
642         }
643 #endif