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