Fix warnings.
[openssl.git] / crypto / ec / 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 #include <string.h>
59 #include <time.h>
60
61
62 #ifdef OPENSSL_NO_EC
63 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
64 #else
65
66
67 #include <openssl/ec.h>
68 #include <openssl/err.h>
69
70 #define ABORT do { \
71         fflush(stdout); \
72         fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
73         ERR_print_errors_fp(stderr); \
74         exit(1); \
75 } while (0)
76
77
78 void timings(EC_GROUP *group, int simult, BN_CTX *ctx)
79         {
80         clock_t clck;
81         int i, j;
82         BIGNUM *s, *s0;
83         EC_POINT *P;
84                 
85         s = BN_new();
86         s0 = BN_new();
87         if (s == NULL || s0 == NULL) ABORT;
88
89         if (!EC_GROUP_get_curve_GFp(group, s, NULL, NULL, ctx)) ABORT;
90         fprintf(stdout, "Timings for %d bit prime, ", (int)BN_num_bits(s));
91         if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
92         fprintf(stdout, "%d bit scalars ", (int)BN_num_bits(s));
93         fflush(stdout);
94
95         P = EC_POINT_new(group);
96         if (P == NULL) ABORT;
97         EC_POINT_copy(P, EC_GROUP_get0_generator(group));
98
99         clck = clock();
100         for (i = 0; i < 10; i++)
101                 {
102                 if (!BN_pseudo_rand(s, BN_num_bits(s), 0, 0)) ABORT;
103                 if (simult)
104                         {
105                         if (!BN_pseudo_rand(s0, BN_num_bits(s), 0, 0)) ABORT;
106                         }
107                 for (j = 0; j < 10; j++)
108                         {
109                         if (!EC_POINT_mul(group, P, s, simult ? P : NULL, simult ? s0 : NULL, ctx)) ABORT;
110                         }
111                 fprintf(stdout, ".");
112                 fflush(stdout);
113                 }
114         fprintf(stdout, "\n");
115         
116         clck = clock() - clck;
117
118 #ifdef CLOCKS_PER_SEC
119         /* "To determine the time in seconds, the value returned
120          * by the clock function should be divided by the value
121          * of the macro CLOCKS_PER_SEC."
122          *                                       -- ISO/IEC 9899 */
123 #       define UNIT "s"
124 #else
125         /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
126          *                            -- cc on NeXTstep/OpenStep */
127 #       define UNIT "units"
128 #       define CLOCKS_PER_SEC 1
129 #endif
130
131         fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
132                 simult ? "s*P+t*Q operations" : "point multiplications",
133                 (double)clck/CLOCKS_PER_SEC);
134         fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
135
136         EC_POINT_free(P);
137         BN_free(s);
138         BN_free(s0);
139         }
140
141
142 int main(int argc, char *argv[])
143         {       
144         BN_CTX *ctx = NULL;
145         BIGNUM *p, *a, *b;
146         EC_GROUP *group;
147         EC_GROUP *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
148         EC_POINT *P, *Q, *R;
149         BIGNUM *x, *y, *z;
150         unsigned char buf[100];
151         size_t i, len;
152         int k;
153         
154         /* enable memory leak checking unless explicitly disabled */
155         if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
156                 {
157                 CRYPTO_malloc_debug_init();
158                 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
159                 }
160         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
161         ERR_load_crypto_strings();
162
163 #if 1 /* optional */
164         ctx = BN_CTX_new();
165         if (!ctx) ABORT;
166 #endif
167
168         p = BN_new();
169         a = BN_new();
170         b = BN_new();
171         if (!p || !a || !b) ABORT;
172
173         if (!BN_hex2bn(&p, "17")) ABORT;
174         if (!BN_hex2bn(&a, "1")) ABORT;
175         if (!BN_hex2bn(&b, "1")) ABORT;
176         
177         group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
178                                                      * so that the library gets to choose the EC_METHOD */
179         if (!group) ABORT;
180
181         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
182
183         {
184                 EC_GROUP *tmp;
185                 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
186                 if (!tmp) ABORT;
187                 if (!EC_GROUP_copy(tmp, group));
188                 EC_GROUP_free(group);
189                 group = tmp;
190         }
191         
192         if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
193
194         fprintf(stdout, "Curve defined by Weierstrass equation\n     y^2 = x^3 + a*x + b  (mod 0x");
195         BN_print_fp(stdout, p);
196         fprintf(stdout, ")\n     a = 0x");
197         BN_print_fp(stdout, a);
198         fprintf(stdout, "\n     b = 0x");
199         BN_print_fp(stdout, b);
200         fprintf(stdout, "\n");
201
202         P = EC_POINT_new(group);
203         Q = EC_POINT_new(group);
204         R = EC_POINT_new(group);
205         if (!P || !Q || !R) ABORT;
206         
207         if (!EC_POINT_set_to_infinity(group, P)) ABORT;
208         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
209
210         buf[0] = 0;
211         if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
212
213         if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
214         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
215
216         x = BN_new();
217         y = BN_new();
218         z = BN_new();
219         if (!x || !y || !z) ABORT;
220
221         if (!BN_hex2bn(&x, "D")) ABORT;
222         if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
223         if (!EC_POINT_is_on_curve(group, Q, ctx))
224                 {
225                 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
226                 fprintf(stderr, "Point is not on curve: x = 0x");
227                 BN_print_fp(stderr, x);
228                 fprintf(stderr, ", y = 0x");
229                 BN_print_fp(stderr, y);
230                 fprintf(stderr, "\n");
231                 ABORT;
232                 }
233
234         fprintf(stdout, "A cyclic subgroup:\n");
235         k = 100;
236         do
237                 {
238                 if (k-- == 0) ABORT;
239
240                 if (EC_POINT_is_at_infinity(group, P))
241                         fprintf(stdout, "     point at infinity\n");
242                 else
243                         {
244                         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
245
246                         fprintf(stdout, "     x = 0x");
247                         BN_print_fp(stdout, x);
248                         fprintf(stdout, ", y = 0x");
249                         BN_print_fp(stdout, y);
250                         fprintf(stdout, "\n");
251                         }
252                 
253                 if (!EC_POINT_copy(R, P)) ABORT;
254                 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
255
256 #if 0 /* optional */
257                 {
258                         EC_POINT *points[3];
259                 
260                         points[0] = R;
261                         points[1] = Q;
262                         points[2] = P;
263                         if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
264                 }
265 #endif
266
267                 }
268         while (!EC_POINT_is_at_infinity(group, P));
269
270         if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
271         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
272
273         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
274         if (len == 0) ABORT;
275         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
276         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
277         fprintf(stdout, "Generator as octect string, compressed form:\n     ");
278         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
279         
280         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
281         if (len == 0) ABORT;
282         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
283         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
284         fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n     ");
285         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
286         
287         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
288         if (len == 0) ABORT;
289         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
290         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
291         fprintf(stdout, "\nGenerator as octect string, hybrid form:\n     ");
292         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
293         
294         if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
295         fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n     X = 0x");
296         BN_print_fp(stdout, x);
297         fprintf(stdout, ", Y = 0x");
298         BN_print_fp(stdout, y);
299         fprintf(stdout, ", Z = 0x");
300         BN_print_fp(stdout, z);
301         fprintf(stdout, "\n");
302
303         if (!EC_POINT_invert(group, P, ctx)) ABORT;
304         if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
305
306
307         /* Curve P-192 (FIPS PUB 186-2, App. 6) */
308         
309         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
310         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
311         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
312         if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
313         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
314
315         if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
316         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
317         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
318         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
319         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
320
321         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
322         fprintf(stdout, "\nNIST curve P-192 -- Generator:\n     x = 0x");
323         BN_print_fp(stdout, x);
324         fprintf(stdout, "\n     y = 0x");
325         BN_print_fp(stdout, y);
326         fprintf(stdout, "\n");
327         /* G_y value taken from the standard: */
328         if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
329         if (0 != BN_cmp(y, z)) ABORT;
330         
331         fprintf(stdout, "verify group order ...");
332         fflush(stdout);
333         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
334         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
335         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
336         fprintf(stdout, ".");
337         fflush(stdout);
338         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
339         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
340         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
341         fprintf(stdout, " ok\n");
342
343         if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
344         if (!EC_GROUP_copy(P_192, group)) ABORT;
345
346
347         /* Curve P-224 (FIPS PUB 186-2, App. 6) */
348         
349         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
350         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
351         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
352         if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
353         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
354
355         if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
356         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
357         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
358         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
359         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
360
361         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
362         fprintf(stdout, "\nNIST curve P-224 -- Generator:\n     x = 0x");
363         BN_print_fp(stdout, x);
364         fprintf(stdout, "\n     y = 0x");
365         BN_print_fp(stdout, y);
366         fprintf(stdout, "\n");
367         /* G_y value taken from the standard: */
368         if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
369         if (0 != BN_cmp(y, z)) ABORT;
370         
371         fprintf(stdout, "verify group order ...");
372         fflush(stdout);
373         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
374         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
375         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
376         fprintf(stdout, ".");
377         fflush(stdout);
378         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
379         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
380         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
381         fprintf(stdout, " ok\n");
382
383         if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
384         if (!EC_GROUP_copy(P_224, group)) ABORT;
385
386
387         /* Curve P-256 (FIPS PUB 186-2, App. 6) */
388         
389         if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
390         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
391         if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
392         if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
393         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
394
395         if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
396         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
397         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
398         if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
399                 "84F3B9CAC2FC632551")) ABORT;
400         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
401
402         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
403         fprintf(stdout, "\nNIST curve P-256 -- Generator:\n     x = 0x");
404         BN_print_fp(stdout, x);
405         fprintf(stdout, "\n     y = 0x");
406         BN_print_fp(stdout, y);
407         fprintf(stdout, "\n");
408         /* G_y value taken from the standard: */
409         if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
410         if (0 != BN_cmp(y, z)) ABORT;
411         
412         fprintf(stdout, "verify group order ...");
413         fflush(stdout);
414         if (!EC_GROUP_get_order(group, z, 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, ".");
418         fflush(stdout);
419         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
420         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
421         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
422         fprintf(stdout, " ok\n");
423
424         if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
425         if (!EC_GROUP_copy(P_256, group)) ABORT;
426
427
428         /* Curve P-384 (FIPS PUB 186-2, App. 6) */
429         
430         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
431                 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
432         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
433         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
434                 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
435         if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
436                 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
437         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
438
439         if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
440                 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
441         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
442         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
443         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
444                 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
445         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
446
447         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
448         fprintf(stdout, "\nNIST curve P-384 -- Generator:\n     x = 0x");
449         BN_print_fp(stdout, x);
450         fprintf(stdout, "\n     y = 0x");
451         BN_print_fp(stdout, y);
452         fprintf(stdout, "\n");
453         /* G_y value taken from the standard: */
454         if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
455                 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
456         if (0 != BN_cmp(y, z)) ABORT;
457         
458         fprintf(stdout, "verify group order ...");
459         fflush(stdout);
460         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
461         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
462         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
463         fprintf(stdout, ".");
464         fflush(stdout);
465         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
466         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
467         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
468         fprintf(stdout, " ok\n");
469
470         if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
471         if (!EC_GROUP_copy(P_384, group)) ABORT;
472
473
474         /* Curve P-521 (FIPS PUB 186-2, App. 6) */
475         
476         if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
477                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
478                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
479         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
480         if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
481                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
482                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
483         if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
484                 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
485                 "DF883D2C34F1EF451FD46B503F00")) ABORT;
486         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
487
488         if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
489                 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
490                 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
491         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
492         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
493         if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
494                 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
495                 "C9B8899C47AEBB6FB71E91386409")) ABORT;
496         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
497
498         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
499         fprintf(stdout, "\nNIST curve P-521 -- Generator:\n     x = 0x");
500         BN_print_fp(stdout, x);
501         fprintf(stdout, "\n     y = 0x");
502         BN_print_fp(stdout, y);
503         fprintf(stdout, "\n");
504         /* G_y value taken from the standard: */
505         if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
506                 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
507                 "7086A272C24088BE94769FD16650")) ABORT;
508         if (0 != BN_cmp(y, z)) ABORT;
509         
510         fprintf(stdout, "verify group order ...");
511         fflush(stdout);
512         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
513         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
514         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
515         fprintf(stdout, ".");
516         fflush(stdout);
517         if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
518         if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
519         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
520         fprintf(stdout, " ok\n");
521
522         if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
523         if (!EC_GROUP_copy(P_521, group)) ABORT;
524
525
526         /* more tests using the last curve */
527
528         if (!EC_POINT_copy(Q, P)) ABORT;
529         if (EC_POINT_is_at_infinity(group, Q)) ABORT;
530         if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
531         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
532         if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
533
534         if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
535         if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
536         if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
537
538         {
539                 const EC_POINT *points[3];
540                 const BIGNUM *scalars[3];
541         
542                 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
543                 points[0] = Q;
544                 points[1] = Q;
545                 points[2] = Q;
546
547                 if (!BN_add(y, z, BN_value_one())) ABORT;
548                 if (BN_is_odd(y)) ABORT;
549                 if (!BN_rshift1(y, y)) ABORT;
550                 scalars[0] = y; /* (group order + 1)/2,  so  y*Q + y*Q = Q */
551                 scalars[1] = y;
552
553                 fprintf(stdout, "simultaneous multiplication ...");
554                 fflush(stdout);
555
556                 /* z is still the group order */
557                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
558                 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
559                 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
560                 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
561
562                 fprintf(stdout, ".");
563                 fflush(stdout);
564
565                 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
566                 if (!BN_copy(z, y)) ABORT;
567                 z->neg = 1;
568                 scalars[0] = y;
569                 scalars[1] = z; /* z = -y */
570
571                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
572                 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
573
574                 fprintf(stdout, ".");
575                 fflush(stdout);
576
577                 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
578                 if (!BN_add(z, x, y)) ABORT;
579                 z->neg = 1;
580                 scalars[0] = x;
581                 scalars[1] = y;
582                 scalars[2] = z; /* z = -(x+y) */
583
584                 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
585                 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
586
587                 fprintf(stdout, " ok\n\n");
588         }
589
590
591 #if 0
592         timings(P_192, 0, ctx);
593         timings(P_192, 1, ctx);
594         timings(P_224, 0, ctx);
595         timings(P_224, 1, ctx);
596         timings(P_256, 0, ctx);
597         timings(P_256, 1, ctx);
598         timings(P_384, 0, ctx);
599         timings(P_384, 1, ctx);
600         timings(P_521, 0, ctx);
601         timings(P_521, 1, ctx);
602 #endif
603
604
605         if (ctx)
606                 BN_CTX_free(ctx);
607         BN_free(p); BN_free(a); BN_free(b);
608         EC_GROUP_free(group);
609         EC_POINT_free(P);
610         EC_POINT_free(Q);
611         EC_POINT_free(R);
612         BN_free(x); BN_free(y); BN_free(z);
613
614         if (P_192) EC_GROUP_free(P_192);
615         if (P_224) EC_GROUP_free(P_224);
616         if (P_256) EC_GROUP_free(P_256);
617         if (P_384) EC_GROUP_free(P_384);
618         if (P_521) EC_GROUP_free(P_521);
619
620         ERR_free_strings();
621         ERR_remove_state(0);
622         CRYPTO_mem_leaks_fp(stderr);
623         
624         return 0;
625         }
626 #endif