xref: /onnv-gate/usr/src/common/openssl/crypto/ec/ectest.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/ec/ectest.c */
2*2139Sjp161948 /*
3*2139Sjp161948  * Originally written by Bodo Moeller for the OpenSSL project.
4*2139Sjp161948  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    openssl-core@openssl.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
58*2139Sjp161948 /* ====================================================================
59*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60*2139Sjp161948  *
61*2139Sjp161948  * Portions of the attached software ("Contribution") are developed by
62*2139Sjp161948  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63*2139Sjp161948  *
64*2139Sjp161948  * The Contribution is licensed pursuant to the OpenSSL open source
65*2139Sjp161948  * license provided above.
66*2139Sjp161948  *
67*2139Sjp161948  * The elliptic curve binary polynomial software is originally written by
68*2139Sjp161948  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69*2139Sjp161948  *
70*2139Sjp161948  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <stdio.h>
730Sstevel@tonic-gate #include <stdlib.h>
740Sstevel@tonic-gate #ifdef FLAT_INC
750Sstevel@tonic-gate #include "e_os.h"
760Sstevel@tonic-gate #else
770Sstevel@tonic-gate #include "../e_os.h"
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate #include <string.h>
800Sstevel@tonic-gate #include <time.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #ifdef OPENSSL_NO_EC
main(int argc,char * argv[])840Sstevel@tonic-gate int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #include <openssl/ec.h>
890Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
900Sstevel@tonic-gate #include <openssl/engine.h>
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate #include <openssl/err.h>
93*2139Sjp161948 #include <openssl/obj_mac.h>
94*2139Sjp161948 #include <openssl/objects.h>
95*2139Sjp161948 #include <openssl/rand.h>
96*2139Sjp161948 #include <openssl/bn.h>
97*2139Sjp161948 
98*2139Sjp161948 #if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
99*2139Sjp161948 /* suppress "too big too optimize" warning */
100*2139Sjp161948 #pragma warning(disable:4959)
101*2139Sjp161948 #endif
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define ABORT do { \
1040Sstevel@tonic-gate 	fflush(stdout); \
1050Sstevel@tonic-gate 	fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
1060Sstevel@tonic-gate 	ERR_print_errors_fp(stderr); \
1070Sstevel@tonic-gate 	EXIT(1); \
1080Sstevel@tonic-gate } while (0)
1090Sstevel@tonic-gate 
110*2139Sjp161948 void prime_field_tests(void);
111*2139Sjp161948 void char2_field_tests(void);
112*2139Sjp161948 void internal_curve_test(void);
113*2139Sjp161948 
114*2139Sjp161948 #define TIMING_BASE_PT 0
115*2139Sjp161948 #define TIMING_RAND_PT 1
116*2139Sjp161948 #define TIMING_SIMUL 2
117*2139Sjp161948 
1180Sstevel@tonic-gate #if 0
119*2139Sjp161948 static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
1200Sstevel@tonic-gate 	{
1210Sstevel@tonic-gate 	clock_t clck;
1220Sstevel@tonic-gate 	int i, j;
123*2139Sjp161948 	BIGNUM *s;
124*2139Sjp161948 	BIGNUM *r[10], *r0[10];
1250Sstevel@tonic-gate 	EC_POINT *P;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	s = BN_new();
128*2139Sjp161948 	if (s == NULL) ABORT;
1290Sstevel@tonic-gate 
130*2139Sjp161948 	fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
1310Sstevel@tonic-gate 	if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
132*2139Sjp161948 	fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
1330Sstevel@tonic-gate 	fflush(stdout);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	P = EC_POINT_new(group);
1360Sstevel@tonic-gate 	if (P == NULL) ABORT;
1370Sstevel@tonic-gate 	EC_POINT_copy(P, EC_GROUP_get0_generator(group));
1380Sstevel@tonic-gate 
139*2139Sjp161948 	for (i = 0; i < 10; i++)
140*2139Sjp161948 		{
141*2139Sjp161948 		if ((r[i] = BN_new()) == NULL) ABORT;
142*2139Sjp161948 		if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0)) ABORT;
143*2139Sjp161948 		if (type != TIMING_BASE_PT)
144*2139Sjp161948 			{
145*2139Sjp161948 			if ((r0[i] = BN_new()) == NULL) ABORT;
146*2139Sjp161948 			if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0)) ABORT;
147*2139Sjp161948 			}
148*2139Sjp161948 		}
149*2139Sjp161948 
1500Sstevel@tonic-gate 	clck = clock();
1510Sstevel@tonic-gate 	for (i = 0; i < 10; i++)
1520Sstevel@tonic-gate 		{
1530Sstevel@tonic-gate 		for (j = 0; j < 10; j++)
1540Sstevel@tonic-gate 			{
155*2139Sjp161948 			if (!EC_POINT_mul(group, P, (type != TIMING_RAND_PT) ? r[i] : NULL,
156*2139Sjp161948 				(type != TIMING_BASE_PT) ? P : NULL, (type != TIMING_BASE_PT) ? r0[i] : NULL, ctx)) ABORT;
1570Sstevel@tonic-gate 			}
1580Sstevel@tonic-gate 		}
159*2139Sjp161948 	clck = clock() - clck;
160*2139Sjp161948 
1610Sstevel@tonic-gate 	fprintf(stdout, "\n");
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate #ifdef CLOCKS_PER_SEC
1640Sstevel@tonic-gate 	/* "To determine the time in seconds, the value returned
1650Sstevel@tonic-gate 	 * by the clock function should be divided by the value
1660Sstevel@tonic-gate 	 * of the macro CLOCKS_PER_SEC."
1670Sstevel@tonic-gate 	 *                                       -- ISO/IEC 9899 */
1680Sstevel@tonic-gate #	define UNIT "s"
1690Sstevel@tonic-gate #else
1700Sstevel@tonic-gate 	/* "`CLOCKS_PER_SEC' undeclared (first use this function)"
1710Sstevel@tonic-gate 	 *                            -- cc on NeXTstep/OpenStep */
1720Sstevel@tonic-gate #	define UNIT "units"
1730Sstevel@tonic-gate #	define CLOCKS_PER_SEC 1
1740Sstevel@tonic-gate #endif
1750Sstevel@tonic-gate 
176*2139Sjp161948 	if (type == TIMING_BASE_PT) {
177*2139Sjp161948 		fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
178*2139Sjp161948 			"base point multiplications", (double)clck/CLOCKS_PER_SEC);
179*2139Sjp161948 	} else if (type == TIMING_RAND_PT) {
180*2139Sjp161948 		fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
181*2139Sjp161948 			"random point multiplications", (double)clck/CLOCKS_PER_SEC);
182*2139Sjp161948 	} else if (type == TIMING_SIMUL) {
183*2139Sjp161948 		fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
184*2139Sjp161948 			"s*P+t*Q operations", (double)clck/CLOCKS_PER_SEC);
185*2139Sjp161948 	}
1860Sstevel@tonic-gate 	fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	EC_POINT_free(P);
1890Sstevel@tonic-gate 	BN_free(s);
190*2139Sjp161948 	for (i = 0; i < 10; i++)
191*2139Sjp161948 		{
192*2139Sjp161948 		BN_free(r[i]);
193*2139Sjp161948 		if (type != TIMING_BASE_PT) BN_free(r0[i]);
194*2139Sjp161948 		}
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate #endif
1970Sstevel@tonic-gate 
prime_field_tests()198*2139Sjp161948 void prime_field_tests()
1990Sstevel@tonic-gate 	{
2000Sstevel@tonic-gate 	BN_CTX *ctx = NULL;
2010Sstevel@tonic-gate 	BIGNUM *p, *a, *b;
2020Sstevel@tonic-gate 	EC_GROUP *group;
203*2139Sjp161948 	EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
2040Sstevel@tonic-gate 	EC_POINT *P, *Q, *R;
2050Sstevel@tonic-gate 	BIGNUM *x, *y, *z;
2060Sstevel@tonic-gate 	unsigned char buf[100];
2070Sstevel@tonic-gate 	size_t i, len;
2080Sstevel@tonic-gate 	int k;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate #if 1 /* optional */
2110Sstevel@tonic-gate 	ctx = BN_CTX_new();
2120Sstevel@tonic-gate 	if (!ctx) ABORT;
2130Sstevel@tonic-gate #endif
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	p = BN_new();
2160Sstevel@tonic-gate 	a = BN_new();
2170Sstevel@tonic-gate 	b = BN_new();
2180Sstevel@tonic-gate 	if (!p || !a || !b) ABORT;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (!BN_hex2bn(&p, "17")) ABORT;
2210Sstevel@tonic-gate 	if (!BN_hex2bn(&a, "1")) ABORT;
2220Sstevel@tonic-gate 	if (!BN_hex2bn(&b, "1")) ABORT;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
2250Sstevel@tonic-gate 	                                             * so that the library gets to choose the EC_METHOD */
2260Sstevel@tonic-gate 	if (!group) ABORT;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	{
2310Sstevel@tonic-gate 		EC_GROUP *tmp;
2320Sstevel@tonic-gate 		tmp = EC_GROUP_new(EC_GROUP_method_of(group));
2330Sstevel@tonic-gate 		if (!tmp) ABORT;
234*2139Sjp161948 		if (!EC_GROUP_copy(tmp, group)) ABORT;
2350Sstevel@tonic-gate 		EC_GROUP_free(group);
2360Sstevel@tonic-gate 		group = tmp;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	fprintf(stdout, "Curve defined by Weierstrass equation\n     y^2 = x^3 + a*x + b  (mod 0x");
2420Sstevel@tonic-gate 	BN_print_fp(stdout, p);
2430Sstevel@tonic-gate 	fprintf(stdout, ")\n     a = 0x");
2440Sstevel@tonic-gate 	BN_print_fp(stdout, a);
2450Sstevel@tonic-gate 	fprintf(stdout, "\n     b = 0x");
2460Sstevel@tonic-gate 	BN_print_fp(stdout, b);
2470Sstevel@tonic-gate 	fprintf(stdout, "\n");
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	P = EC_POINT_new(group);
2500Sstevel@tonic-gate 	Q = EC_POINT_new(group);
2510Sstevel@tonic-gate 	R = EC_POINT_new(group);
2520Sstevel@tonic-gate 	if (!P || !Q || !R) ABORT;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (!EC_POINT_set_to_infinity(group, P)) ABORT;
2550Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, P)) ABORT;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	buf[0] = 0;
2580Sstevel@tonic-gate 	if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
2610Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, P)) ABORT;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	x = BN_new();
2640Sstevel@tonic-gate 	y = BN_new();
2650Sstevel@tonic-gate 	z = BN_new();
2660Sstevel@tonic-gate 	if (!x || !y || !z) ABORT;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (!BN_hex2bn(&x, "D")) ABORT;
2690Sstevel@tonic-gate 	if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
2700Sstevel@tonic-gate 	if (!EC_POINT_is_on_curve(group, Q, ctx))
2710Sstevel@tonic-gate 		{
2720Sstevel@tonic-gate 		if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
2730Sstevel@tonic-gate 		fprintf(stderr, "Point is not on curve: x = 0x");
2740Sstevel@tonic-gate 		BN_print_fp(stderr, x);
2750Sstevel@tonic-gate 		fprintf(stderr, ", y = 0x");
2760Sstevel@tonic-gate 		BN_print_fp(stderr, y);
2770Sstevel@tonic-gate 		fprintf(stderr, "\n");
2780Sstevel@tonic-gate 		ABORT;
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	fprintf(stdout, "A cyclic subgroup:\n");
2820Sstevel@tonic-gate 	k = 100;
2830Sstevel@tonic-gate 	do
2840Sstevel@tonic-gate 		{
2850Sstevel@tonic-gate 		if (k-- == 0) ABORT;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		if (EC_POINT_is_at_infinity(group, P))
2880Sstevel@tonic-gate 			fprintf(stdout, "     point at infinity\n");
2890Sstevel@tonic-gate 		else
2900Sstevel@tonic-gate 			{
2910Sstevel@tonic-gate 			if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 			fprintf(stdout, "     x = 0x");
2940Sstevel@tonic-gate 			BN_print_fp(stdout, x);
2950Sstevel@tonic-gate 			fprintf(stdout, ", y = 0x");
2960Sstevel@tonic-gate 			BN_print_fp(stdout, y);
2970Sstevel@tonic-gate 			fprintf(stdout, "\n");
2980Sstevel@tonic-gate 			}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		if (!EC_POINT_copy(R, P)) ABORT;
3010Sstevel@tonic-gate 		if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate #if 0 /* optional */
3040Sstevel@tonic-gate 		{
3050Sstevel@tonic-gate 			EC_POINT *points[3];
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 			points[0] = R;
3080Sstevel@tonic-gate 			points[1] = Q;
3090Sstevel@tonic-gate 			points[2] = P;
3100Sstevel@tonic-gate 			if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate #endif
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 	while (!EC_POINT_is_at_infinity(group, P));
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
3180Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, P)) ABORT;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
3210Sstevel@tonic-gate 	if (len == 0) ABORT;
3220Sstevel@tonic-gate 	if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
3230Sstevel@tonic-gate 	if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
3240Sstevel@tonic-gate 	fprintf(stdout, "Generator as octect string, compressed form:\n     ");
3250Sstevel@tonic-gate 	for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
3280Sstevel@tonic-gate 	if (len == 0) ABORT;
3290Sstevel@tonic-gate 	if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
3300Sstevel@tonic-gate 	if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
3310Sstevel@tonic-gate 	fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n     ");
3320Sstevel@tonic-gate 	for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
3350Sstevel@tonic-gate 	if (len == 0) ABORT;
3360Sstevel@tonic-gate 	if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
3370Sstevel@tonic-gate 	if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
3380Sstevel@tonic-gate 	fprintf(stdout, "\nGenerator as octect string, hybrid form:\n     ");
3390Sstevel@tonic-gate 	for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
3420Sstevel@tonic-gate 	fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n     X = 0x");
3430Sstevel@tonic-gate 	BN_print_fp(stdout, x);
3440Sstevel@tonic-gate 	fprintf(stdout, ", Y = 0x");
3450Sstevel@tonic-gate 	BN_print_fp(stdout, y);
3460Sstevel@tonic-gate 	fprintf(stdout, ", Z = 0x");
3470Sstevel@tonic-gate 	BN_print_fp(stdout, z);
3480Sstevel@tonic-gate 	fprintf(stdout, "\n");
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (!EC_POINT_invert(group, P, ctx)) ABORT;
3510Sstevel@tonic-gate 	if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 
354*2139Sjp161948 	/* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000)
355*2139Sjp161948 	 * -- not a NIST curve, but commonly used */
356*2139Sjp161948 
357*2139Sjp161948 	if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) ABORT;
358*2139Sjp161948 	if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
359*2139Sjp161948 	if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) ABORT;
360*2139Sjp161948 	if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")) ABORT;
361*2139Sjp161948 	if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
362*2139Sjp161948 
363*2139Sjp161948 	if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82")) ABORT;
364*2139Sjp161948 	if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
365*2139Sjp161948 	if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
366*2139Sjp161948 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
367*2139Sjp161948 	if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257")) ABORT;
368*2139Sjp161948 	if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
369*2139Sjp161948 
370*2139Sjp161948 	if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
371*2139Sjp161948 	fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n     x = 0x");
372*2139Sjp161948 	BN_print_fp(stdout, x);
373*2139Sjp161948 	fprintf(stdout, "\n     y = 0x");
374*2139Sjp161948 	BN_print_fp(stdout, y);
375*2139Sjp161948 	fprintf(stdout, "\n");
376*2139Sjp161948 	/* G_y value taken from the standard: */
377*2139Sjp161948 	if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
378*2139Sjp161948 	if (0 != BN_cmp(y, z)) ABORT;
379*2139Sjp161948 
380*2139Sjp161948 	fprintf(stdout, "verify degree ...");
381*2139Sjp161948 	if (EC_GROUP_get_degree(group) != 160) ABORT;
382*2139Sjp161948 	fprintf(stdout, " ok\n");
383*2139Sjp161948 
384*2139Sjp161948 	fprintf(stdout, "verify group order ...");
385*2139Sjp161948 	fflush(stdout);
386*2139Sjp161948 	if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
387*2139Sjp161948 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
388*2139Sjp161948 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
389*2139Sjp161948 	fprintf(stdout, ".");
390*2139Sjp161948 	fflush(stdout);
391*2139Sjp161948 	if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
392*2139Sjp161948 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
393*2139Sjp161948 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
394*2139Sjp161948 	fprintf(stdout, " ok\n");
395*2139Sjp161948 
396*2139Sjp161948 	if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
397*2139Sjp161948 	if (!EC_GROUP_copy(P_160, group)) ABORT;
398*2139Sjp161948 
399*2139Sjp161948 
4000Sstevel@tonic-gate 	/* Curve P-192 (FIPS PUB 186-2, App. 6) */
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
403*2139Sjp161948 	if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
4040Sstevel@tonic-gate 	if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
4050Sstevel@tonic-gate 	if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
4060Sstevel@tonic-gate 	if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
4090Sstevel@tonic-gate 	if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
4100Sstevel@tonic-gate 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
4110Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
4120Sstevel@tonic-gate 	if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
4150Sstevel@tonic-gate 	fprintf(stdout, "\nNIST curve P-192 -- Generator:\n     x = 0x");
4160Sstevel@tonic-gate 	BN_print_fp(stdout, x);
4170Sstevel@tonic-gate 	fprintf(stdout, "\n     y = 0x");
4180Sstevel@tonic-gate 	BN_print_fp(stdout, y);
4190Sstevel@tonic-gate 	fprintf(stdout, "\n");
4200Sstevel@tonic-gate 	/* G_y value taken from the standard: */
4210Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
4220Sstevel@tonic-gate 	if (0 != BN_cmp(y, z)) ABORT;
423*2139Sjp161948 
424*2139Sjp161948 	fprintf(stdout, "verify degree ...");
425*2139Sjp161948 	if (EC_GROUP_get_degree(group) != 192) ABORT;
426*2139Sjp161948 	fprintf(stdout, " ok\n");
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	fprintf(stdout, "verify group order ...");
4290Sstevel@tonic-gate 	fflush(stdout);
4300Sstevel@tonic-gate 	if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
4310Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
4320Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
4330Sstevel@tonic-gate 	fprintf(stdout, ".");
4340Sstevel@tonic-gate 	fflush(stdout);
435*2139Sjp161948 #if 0
4360Sstevel@tonic-gate 	if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
437*2139Sjp161948 #endif
4380Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
4390Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
4400Sstevel@tonic-gate 	fprintf(stdout, " ok\n");
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
4430Sstevel@tonic-gate 	if (!EC_GROUP_copy(P_192, group)) ABORT;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/* Curve P-224 (FIPS PUB 186-2, App. 6) */
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
449*2139Sjp161948 	if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
4500Sstevel@tonic-gate 	if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
4510Sstevel@tonic-gate 	if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
4520Sstevel@tonic-gate 	if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
4550Sstevel@tonic-gate 	if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
4560Sstevel@tonic-gate 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
4570Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
4580Sstevel@tonic-gate 	if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
4610Sstevel@tonic-gate 	fprintf(stdout, "\nNIST curve P-224 -- Generator:\n     x = 0x");
4620Sstevel@tonic-gate 	BN_print_fp(stdout, x);
4630Sstevel@tonic-gate 	fprintf(stdout, "\n     y = 0x");
4640Sstevel@tonic-gate 	BN_print_fp(stdout, y);
4650Sstevel@tonic-gate 	fprintf(stdout, "\n");
4660Sstevel@tonic-gate 	/* G_y value taken from the standard: */
4670Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
4680Sstevel@tonic-gate 	if (0 != BN_cmp(y, z)) ABORT;
4690Sstevel@tonic-gate 
470*2139Sjp161948 	fprintf(stdout, "verify degree ...");
471*2139Sjp161948 	if (EC_GROUP_get_degree(group) != 224) ABORT;
472*2139Sjp161948 	fprintf(stdout, " ok\n");
473*2139Sjp161948 
4740Sstevel@tonic-gate 	fprintf(stdout, "verify group order ...");
4750Sstevel@tonic-gate 	fflush(stdout);
4760Sstevel@tonic-gate 	if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
4770Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
4780Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
4790Sstevel@tonic-gate 	fprintf(stdout, ".");
4800Sstevel@tonic-gate 	fflush(stdout);
481*2139Sjp161948 #if 0
4820Sstevel@tonic-gate 	if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
483*2139Sjp161948 #endif
4840Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
4850Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
4860Sstevel@tonic-gate 	fprintf(stdout, " ok\n");
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
4890Sstevel@tonic-gate 	if (!EC_GROUP_copy(P_224, group)) ABORT;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/* Curve P-256 (FIPS PUB 186-2, App. 6) */
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
495*2139Sjp161948 	if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
4960Sstevel@tonic-gate 	if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
4970Sstevel@tonic-gate 	if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
4980Sstevel@tonic-gate 	if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
5010Sstevel@tonic-gate 	if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
5020Sstevel@tonic-gate 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
5030Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
5040Sstevel@tonic-gate 		"84F3B9CAC2FC632551")) ABORT;
5050Sstevel@tonic-gate 	if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
5080Sstevel@tonic-gate 	fprintf(stdout, "\nNIST curve P-256 -- Generator:\n     x = 0x");
5090Sstevel@tonic-gate 	BN_print_fp(stdout, x);
5100Sstevel@tonic-gate 	fprintf(stdout, "\n     y = 0x");
5110Sstevel@tonic-gate 	BN_print_fp(stdout, y);
5120Sstevel@tonic-gate 	fprintf(stdout, "\n");
5130Sstevel@tonic-gate 	/* G_y value taken from the standard: */
5140Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
5150Sstevel@tonic-gate 	if (0 != BN_cmp(y, z)) ABORT;
5160Sstevel@tonic-gate 
517*2139Sjp161948 	fprintf(stdout, "verify degree ...");
518*2139Sjp161948 	if (EC_GROUP_get_degree(group) != 256) ABORT;
519*2139Sjp161948 	fprintf(stdout, " ok\n");
520*2139Sjp161948 
5210Sstevel@tonic-gate 	fprintf(stdout, "verify group order ...");
5220Sstevel@tonic-gate 	fflush(stdout);
5230Sstevel@tonic-gate 	if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
5240Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
5250Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
5260Sstevel@tonic-gate 	fprintf(stdout, ".");
5270Sstevel@tonic-gate 	fflush(stdout);
528*2139Sjp161948 #if 0
5290Sstevel@tonic-gate 	if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
530*2139Sjp161948 #endif
5310Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
5320Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
5330Sstevel@tonic-gate 	fprintf(stdout, " ok\n");
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
5360Sstevel@tonic-gate 	if (!EC_GROUP_copy(P_256, group)) ABORT;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/* Curve P-384 (FIPS PUB 186-2, App. 6) */
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
5420Sstevel@tonic-gate 		"FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
543*2139Sjp161948 	if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
5440Sstevel@tonic-gate 	if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
5450Sstevel@tonic-gate 		"FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
5460Sstevel@tonic-gate 	if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
5470Sstevel@tonic-gate 		"120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
5480Sstevel@tonic-gate 	if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
5510Sstevel@tonic-gate 		"9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
5520Sstevel@tonic-gate 	if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
5530Sstevel@tonic-gate 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
5540Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
5550Sstevel@tonic-gate 		"FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
5560Sstevel@tonic-gate 	if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
5590Sstevel@tonic-gate 	fprintf(stdout, "\nNIST curve P-384 -- Generator:\n     x = 0x");
5600Sstevel@tonic-gate 	BN_print_fp(stdout, x);
5610Sstevel@tonic-gate 	fprintf(stdout, "\n     y = 0x");
5620Sstevel@tonic-gate 	BN_print_fp(stdout, y);
5630Sstevel@tonic-gate 	fprintf(stdout, "\n");
5640Sstevel@tonic-gate 	/* G_y value taken from the standard: */
5650Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
5660Sstevel@tonic-gate 		"7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
5670Sstevel@tonic-gate 	if (0 != BN_cmp(y, z)) ABORT;
5680Sstevel@tonic-gate 
569*2139Sjp161948 	fprintf(stdout, "verify degree ...");
570*2139Sjp161948 	if (EC_GROUP_get_degree(group) != 384) ABORT;
571*2139Sjp161948 	fprintf(stdout, " ok\n");
572*2139Sjp161948 
5730Sstevel@tonic-gate 	fprintf(stdout, "verify group order ...");
5740Sstevel@tonic-gate 	fflush(stdout);
5750Sstevel@tonic-gate 	if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
5760Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
5770Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
5780Sstevel@tonic-gate 	fprintf(stdout, ".");
5790Sstevel@tonic-gate 	fflush(stdout);
580*2139Sjp161948 #if 0
5810Sstevel@tonic-gate 	if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
582*2139Sjp161948 #endif
5830Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
5840Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
5850Sstevel@tonic-gate 	fprintf(stdout, " ok\n");
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
5880Sstevel@tonic-gate 	if (!EC_GROUP_copy(P_384, group)) ABORT;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/* Curve P-521 (FIPS PUB 186-2, App. 6) */
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
5940Sstevel@tonic-gate 		"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
5950Sstevel@tonic-gate 		"FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
596*2139Sjp161948 	if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
5970Sstevel@tonic-gate 	if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
5980Sstevel@tonic-gate 		"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
5990Sstevel@tonic-gate 		"FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
6000Sstevel@tonic-gate 	if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
6010Sstevel@tonic-gate 		"315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
6020Sstevel@tonic-gate 		"DF883D2C34F1EF451FD46B503F00")) ABORT;
6030Sstevel@tonic-gate 	if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
6060Sstevel@tonic-gate 		"B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
6070Sstevel@tonic-gate 		"3C1856A429BF97E7E31C2E5BD66")) ABORT;
6080Sstevel@tonic-gate 	if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
6090Sstevel@tonic-gate 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
6100Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
6110Sstevel@tonic-gate 		"FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
6120Sstevel@tonic-gate 		"C9B8899C47AEBB6FB71E91386409")) ABORT;
6130Sstevel@tonic-gate 	if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
6160Sstevel@tonic-gate 	fprintf(stdout, "\nNIST curve P-521 -- Generator:\n     x = 0x");
6170Sstevel@tonic-gate 	BN_print_fp(stdout, x);
6180Sstevel@tonic-gate 	fprintf(stdout, "\n     y = 0x");
6190Sstevel@tonic-gate 	BN_print_fp(stdout, y);
6200Sstevel@tonic-gate 	fprintf(stdout, "\n");
6210Sstevel@tonic-gate 	/* G_y value taken from the standard: */
6220Sstevel@tonic-gate 	if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
6230Sstevel@tonic-gate 		"B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
6240Sstevel@tonic-gate 		"7086A272C24088BE94769FD16650")) ABORT;
6250Sstevel@tonic-gate 	if (0 != BN_cmp(y, z)) ABORT;
6260Sstevel@tonic-gate 
627*2139Sjp161948 	fprintf(stdout, "verify degree ...");
628*2139Sjp161948 	if (EC_GROUP_get_degree(group) != 521) ABORT;
629*2139Sjp161948 	fprintf(stdout, " ok\n");
630*2139Sjp161948 
6310Sstevel@tonic-gate 	fprintf(stdout, "verify group order ...");
6320Sstevel@tonic-gate 	fflush(stdout);
6330Sstevel@tonic-gate 	if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
6340Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
6350Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
6360Sstevel@tonic-gate 	fprintf(stdout, ".");
6370Sstevel@tonic-gate 	fflush(stdout);
638*2139Sjp161948 #if 0
6390Sstevel@tonic-gate 	if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
640*2139Sjp161948 #endif
6410Sstevel@tonic-gate 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
6420Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
6430Sstevel@tonic-gate 	fprintf(stdout, " ok\n");
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
6460Sstevel@tonic-gate 	if (!EC_GROUP_copy(P_521, group)) ABORT;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	/* more tests using the last curve */
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	if (!EC_POINT_copy(Q, P)) ABORT;
6520Sstevel@tonic-gate 	if (EC_POINT_is_at_infinity(group, Q)) ABORT;
6530Sstevel@tonic-gate 	if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
6540Sstevel@tonic-gate 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
6550Sstevel@tonic-gate 	if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
6580Sstevel@tonic-gate 	if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
6590Sstevel@tonic-gate 	if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	{
6620Sstevel@tonic-gate 		const EC_POINT *points[3];
6630Sstevel@tonic-gate 		const BIGNUM *scalars[3];
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 		if (EC_POINT_is_at_infinity(group, Q)) ABORT;
6660Sstevel@tonic-gate 		points[0] = Q;
6670Sstevel@tonic-gate 		points[1] = Q;
6680Sstevel@tonic-gate 		points[2] = Q;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 		if (!BN_add(y, z, BN_value_one())) ABORT;
6710Sstevel@tonic-gate 		if (BN_is_odd(y)) ABORT;
6720Sstevel@tonic-gate 		if (!BN_rshift1(y, y)) ABORT;
6730Sstevel@tonic-gate 		scalars[0] = y; /* (group order + 1)/2,  so  y*Q + y*Q = Q */
6740Sstevel@tonic-gate 		scalars[1] = y;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		fprintf(stdout, "combined multiplication ...");
6770Sstevel@tonic-gate 		fflush(stdout);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		/* z is still the group order */
6800Sstevel@tonic-gate 		if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
6810Sstevel@tonic-gate 		if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
6820Sstevel@tonic-gate 		if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
6830Sstevel@tonic-gate 		if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		fprintf(stdout, ".");
6860Sstevel@tonic-gate 		fflush(stdout);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
6890Sstevel@tonic-gate 		if (!BN_add(z, z, y)) ABORT;
690*2139Sjp161948 		BN_set_negative(z, 1);
6910Sstevel@tonic-gate 		scalars[0] = y;
6920Sstevel@tonic-gate 		scalars[1] = z; /* z = -(order + y) */
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
6950Sstevel@tonic-gate 		if (!EC_POINT_is_at_infinity(group, P)) ABORT;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		fprintf(stdout, ".");
6980Sstevel@tonic-gate 		fflush(stdout);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
7010Sstevel@tonic-gate 		if (!BN_add(z, x, y)) ABORT;
702*2139Sjp161948 		BN_set_negative(z, 1);
7030Sstevel@tonic-gate 		scalars[0] = x;
7040Sstevel@tonic-gate 		scalars[1] = y;
7050Sstevel@tonic-gate 		scalars[2] = z; /* z = -(x+y) */
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 		if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
7080Sstevel@tonic-gate 		if (!EC_POINT_is_at_infinity(group, P)) ABORT;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		fprintf(stdout, " ok\n\n");
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate #if 0
715*2139Sjp161948 	timings(P_160, TIMING_BASE_PT, ctx);
716*2139Sjp161948 	timings(P_160, TIMING_RAND_PT, ctx);
717*2139Sjp161948 	timings(P_160, TIMING_SIMUL, ctx);
718*2139Sjp161948 	timings(P_192, TIMING_BASE_PT, ctx);
719*2139Sjp161948 	timings(P_192, TIMING_RAND_PT, ctx);
720*2139Sjp161948 	timings(P_192, TIMING_SIMUL, ctx);
721*2139Sjp161948 	timings(P_224, TIMING_BASE_PT, ctx);
722*2139Sjp161948 	timings(P_224, TIMING_RAND_PT, ctx);
723*2139Sjp161948 	timings(P_224, TIMING_SIMUL, ctx);
724*2139Sjp161948 	timings(P_256, TIMING_BASE_PT, ctx);
725*2139Sjp161948 	timings(P_256, TIMING_RAND_PT, ctx);
726*2139Sjp161948 	timings(P_256, TIMING_SIMUL, ctx);
727*2139Sjp161948 	timings(P_384, TIMING_BASE_PT, ctx);
728*2139Sjp161948 	timings(P_384, TIMING_RAND_PT, ctx);
729*2139Sjp161948 	timings(P_384, TIMING_SIMUL, ctx);
730*2139Sjp161948 	timings(P_521, TIMING_BASE_PT, ctx);
731*2139Sjp161948 	timings(P_521, TIMING_RAND_PT, ctx);
732*2139Sjp161948 	timings(P_521, TIMING_SIMUL, ctx);
7330Sstevel@tonic-gate #endif
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (ctx)
7370Sstevel@tonic-gate 		BN_CTX_free(ctx);
7380Sstevel@tonic-gate 	BN_free(p); BN_free(a);	BN_free(b);
7390Sstevel@tonic-gate 	EC_GROUP_free(group);
7400Sstevel@tonic-gate 	EC_POINT_free(P);
7410Sstevel@tonic-gate 	EC_POINT_free(Q);
7420Sstevel@tonic-gate 	EC_POINT_free(R);
7430Sstevel@tonic-gate 	BN_free(x); BN_free(y); BN_free(z);
7440Sstevel@tonic-gate 
745*2139Sjp161948 	if (P_160) EC_GROUP_free(P_160);
7460Sstevel@tonic-gate 	if (P_192) EC_GROUP_free(P_192);
7470Sstevel@tonic-gate 	if (P_224) EC_GROUP_free(P_224);
7480Sstevel@tonic-gate 	if (P_256) EC_GROUP_free(P_256);
7490Sstevel@tonic-gate 	if (P_384) EC_GROUP_free(P_384);
7500Sstevel@tonic-gate 	if (P_521) EC_GROUP_free(P_521);
7510Sstevel@tonic-gate 
752*2139Sjp161948 	}
753*2139Sjp161948 
754*2139Sjp161948 /* Change test based on whether binary point compression is enabled or not. */
755*2139Sjp161948 #ifdef OPENSSL_EC_BIN_PT_COMP
756*2139Sjp161948 #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
757*2139Sjp161948 	if (!BN_hex2bn(&x, _x)) ABORT; \
758*2139Sjp161948 	if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
759*2139Sjp161948 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
760*2139Sjp161948 	if (!BN_hex2bn(&z, _order)) ABORT; \
761*2139Sjp161948 	if (!BN_hex2bn(&cof, _cof)) ABORT; \
762*2139Sjp161948 	if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
763*2139Sjp161948 	if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
764*2139Sjp161948 	fprintf(stdout, "\n%s -- Generator:\n     x = 0x", _name); \
765*2139Sjp161948 	BN_print_fp(stdout, x); \
766*2139Sjp161948 	fprintf(stdout, "\n     y = 0x"); \
767*2139Sjp161948 	BN_print_fp(stdout, y); \
768*2139Sjp161948 	fprintf(stdout, "\n"); \
769*2139Sjp161948 	/* G_y value taken from the standard: */ \
770*2139Sjp161948 	if (!BN_hex2bn(&z, _y)) ABORT; \
771*2139Sjp161948 	if (0 != BN_cmp(y, z)) ABORT;
772*2139Sjp161948 #else
773*2139Sjp161948 #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
774*2139Sjp161948 	if (!BN_hex2bn(&x, _x)) ABORT; \
775*2139Sjp161948 	if (!BN_hex2bn(&y, _y)) ABORT; \
776*2139Sjp161948 	if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
777*2139Sjp161948 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
778*2139Sjp161948 	if (!BN_hex2bn(&z, _order)) ABORT; \
779*2139Sjp161948 	if (!BN_hex2bn(&cof, _cof)) ABORT; \
780*2139Sjp161948 	if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
781*2139Sjp161948 	fprintf(stdout, "\n%s -- Generator:\n     x = 0x", _name); \
782*2139Sjp161948 	BN_print_fp(stdout, x); \
783*2139Sjp161948 	fprintf(stdout, "\n     y = 0x"); \
784*2139Sjp161948 	BN_print_fp(stdout, y); \
785*2139Sjp161948 	fprintf(stdout, "\n");
786*2139Sjp161948 #endif
787*2139Sjp161948 
788*2139Sjp161948 #define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
789*2139Sjp161948 	if (!BN_hex2bn(&p, _p)) ABORT; \
790*2139Sjp161948 	if (!BN_hex2bn(&a, _a)) ABORT; \
791*2139Sjp161948 	if (!BN_hex2bn(&b, _b)) ABORT; \
792*2139Sjp161948 	if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
793*2139Sjp161948 	CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
794*2139Sjp161948 	fprintf(stdout, "verify degree ..."); \
795*2139Sjp161948 	if (EC_GROUP_get_degree(group) != _degree) ABORT; \
796*2139Sjp161948 	fprintf(stdout, " ok\n"); \
797*2139Sjp161948 	fprintf(stdout, "verify group order ..."); \
798*2139Sjp161948 	fflush(stdout); \
799*2139Sjp161948 	if (!EC_GROUP_get_order(group, z, ctx)) ABORT; \
800*2139Sjp161948 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
801*2139Sjp161948 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
802*2139Sjp161948 	fprintf(stdout, "."); \
803*2139Sjp161948 	fflush(stdout); \
804*2139Sjp161948 	/* if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; */ \
805*2139Sjp161948 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
806*2139Sjp161948 	if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
807*2139Sjp161948 	fprintf(stdout, " ok\n"); \
808*2139Sjp161948 	if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
809*2139Sjp161948 	if (!EC_GROUP_copy(_variable, group)) ABORT;
810*2139Sjp161948 
char2_field_tests()811*2139Sjp161948 void char2_field_tests()
812*2139Sjp161948 	{
813*2139Sjp161948 	BN_CTX *ctx = NULL;
814*2139Sjp161948 	BIGNUM *p, *a, *b;
815*2139Sjp161948 	EC_GROUP *group;
816*2139Sjp161948 	EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 = NULL, *C2_K571 = NULL;
817*2139Sjp161948 	EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 = NULL, *C2_B571 = NULL;
818*2139Sjp161948 	EC_POINT *P, *Q, *R;
819*2139Sjp161948 	BIGNUM *x, *y, *z, *cof;
820*2139Sjp161948 	unsigned char buf[100];
821*2139Sjp161948 	size_t i, len;
822*2139Sjp161948 	int k;
823*2139Sjp161948 
824*2139Sjp161948 #if 1 /* optional */
825*2139Sjp161948 	ctx = BN_CTX_new();
826*2139Sjp161948 	if (!ctx) ABORT;
827*2139Sjp161948 #endif
828*2139Sjp161948 
829*2139Sjp161948 	p = BN_new();
830*2139Sjp161948 	a = BN_new();
831*2139Sjp161948 	b = BN_new();
832*2139Sjp161948 	if (!p || !a || !b) ABORT;
833*2139Sjp161948 
834*2139Sjp161948 	if (!BN_hex2bn(&p, "13")) ABORT;
835*2139Sjp161948 	if (!BN_hex2bn(&a, "3")) ABORT;
836*2139Sjp161948 	if (!BN_hex2bn(&b, "1")) ABORT;
837*2139Sjp161948 
838*2139Sjp161948 	group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
839*2139Sjp161948 	                                                * so that the library gets to choose the EC_METHOD */
840*2139Sjp161948 	if (!group) ABORT;
841*2139Sjp161948 	if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
842*2139Sjp161948 
843*2139Sjp161948 	{
844*2139Sjp161948 		EC_GROUP *tmp;
845*2139Sjp161948 		tmp = EC_GROUP_new(EC_GROUP_method_of(group));
846*2139Sjp161948 		if (!tmp) ABORT;
847*2139Sjp161948 		if (!EC_GROUP_copy(tmp, group)) ABORT;
848*2139Sjp161948 		EC_GROUP_free(group);
849*2139Sjp161948 		group = tmp;
850*2139Sjp161948 	}
851*2139Sjp161948 
852*2139Sjp161948 	if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)) ABORT;
853*2139Sjp161948 
854*2139Sjp161948 	fprintf(stdout, "Curve defined by Weierstrass equation\n     y^2 + x*y = x^3 + a*x^2 + b  (mod 0x");
855*2139Sjp161948 	BN_print_fp(stdout, p);
856*2139Sjp161948 	fprintf(stdout, ")\n     a = 0x");
857*2139Sjp161948 	BN_print_fp(stdout, a);
858*2139Sjp161948 	fprintf(stdout, "\n     b = 0x");
859*2139Sjp161948 	BN_print_fp(stdout, b);
860*2139Sjp161948 	fprintf(stdout, "\n(0x... means binary polynomial)\n");
861*2139Sjp161948 
862*2139Sjp161948 	P = EC_POINT_new(group);
863*2139Sjp161948 	Q = EC_POINT_new(group);
864*2139Sjp161948 	R = EC_POINT_new(group);
865*2139Sjp161948 	if (!P || !Q || !R) ABORT;
866*2139Sjp161948 
867*2139Sjp161948 	if (!EC_POINT_set_to_infinity(group, P)) ABORT;
868*2139Sjp161948 	if (!EC_POINT_is_at_infinity(group, P)) ABORT;
869*2139Sjp161948 
870*2139Sjp161948 	buf[0] = 0;
871*2139Sjp161948 	if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
872*2139Sjp161948 
873*2139Sjp161948 	if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
874*2139Sjp161948 	if (!EC_POINT_is_at_infinity(group, P)) ABORT;
875*2139Sjp161948 
876*2139Sjp161948 	x = BN_new();
877*2139Sjp161948 	y = BN_new();
878*2139Sjp161948 	z = BN_new();
879*2139Sjp161948 	cof = BN_new();
880*2139Sjp161948 	if (!x || !y || !z || !cof) ABORT;
881*2139Sjp161948 
882*2139Sjp161948 	if (!BN_hex2bn(&x, "6")) ABORT;
883*2139Sjp161948 /* Change test based on whether binary point compression is enabled or not. */
884*2139Sjp161948 #ifdef OPENSSL_EC_BIN_PT_COMP
885*2139Sjp161948 	if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx)) ABORT;
886*2139Sjp161948 #else
887*2139Sjp161948 	if (!BN_hex2bn(&y, "8")) ABORT;
888*2139Sjp161948 	if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
889*2139Sjp161948 #endif
890*2139Sjp161948 	if (!EC_POINT_is_on_curve(group, Q, ctx))
891*2139Sjp161948 		{
892*2139Sjp161948 /* Change test based on whether binary point compression is enabled or not. */
893*2139Sjp161948 #ifdef OPENSSL_EC_BIN_PT_COMP
894*2139Sjp161948 		if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
895*2139Sjp161948 #endif
896*2139Sjp161948 		fprintf(stderr, "Point is not on curve: x = 0x");
897*2139Sjp161948 		BN_print_fp(stderr, x);
898*2139Sjp161948 		fprintf(stderr, ", y = 0x");
899*2139Sjp161948 		BN_print_fp(stderr, y);
900*2139Sjp161948 		fprintf(stderr, "\n");
901*2139Sjp161948 		ABORT;
902*2139Sjp161948 		}
903*2139Sjp161948 
904*2139Sjp161948 	fprintf(stdout, "A cyclic subgroup:\n");
905*2139Sjp161948 	k = 100;
906*2139Sjp161948 	do
907*2139Sjp161948 		{
908*2139Sjp161948 		if (k-- == 0) ABORT;
909*2139Sjp161948 
910*2139Sjp161948 		if (EC_POINT_is_at_infinity(group, P))
911*2139Sjp161948 			fprintf(stdout, "     point at infinity\n");
912*2139Sjp161948 		else
913*2139Sjp161948 			{
914*2139Sjp161948 			if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT;
915*2139Sjp161948 
916*2139Sjp161948 			fprintf(stdout, "     x = 0x");
917*2139Sjp161948 			BN_print_fp(stdout, x);
918*2139Sjp161948 			fprintf(stdout, ", y = 0x");
919*2139Sjp161948 			BN_print_fp(stdout, y);
920*2139Sjp161948 			fprintf(stdout, "\n");
921*2139Sjp161948 			}
922*2139Sjp161948 
923*2139Sjp161948 		if (!EC_POINT_copy(R, P)) ABORT;
924*2139Sjp161948 		if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
925*2139Sjp161948 		}
926*2139Sjp161948 	while (!EC_POINT_is_at_infinity(group, P));
927*2139Sjp161948 
928*2139Sjp161948 	if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
929*2139Sjp161948 	if (!EC_POINT_is_at_infinity(group, P)) ABORT;
930*2139Sjp161948 
931*2139Sjp161948 /* Change test based on whether binary point compression is enabled or not. */
932*2139Sjp161948 #ifdef OPENSSL_EC_BIN_PT_COMP
933*2139Sjp161948 	len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
934*2139Sjp161948 	if (len == 0) ABORT;
935*2139Sjp161948 	if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
936*2139Sjp161948 	if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
937*2139Sjp161948 	fprintf(stdout, "Generator as octet string, compressed form:\n     ");
938*2139Sjp161948 	for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
939*2139Sjp161948 #endif
940*2139Sjp161948 
941*2139Sjp161948 	len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
942*2139Sjp161948 	if (len == 0) ABORT;
943*2139Sjp161948 	if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
944*2139Sjp161948 	if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
945*2139Sjp161948 	fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n     ");
946*2139Sjp161948 	for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
947*2139Sjp161948 
948*2139Sjp161948 /* Change test based on whether binary point compression is enabled or not. */
949*2139Sjp161948 #ifdef OPENSSL_EC_BIN_PT_COMP
950*2139Sjp161948 	len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
951*2139Sjp161948 	if (len == 0) ABORT;
952*2139Sjp161948 	if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
953*2139Sjp161948 	if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
954*2139Sjp161948 	fprintf(stdout, "\nGenerator as octet string, hybrid form:\n     ");
955*2139Sjp161948 	for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
956*2139Sjp161948 #endif
957*2139Sjp161948 
958*2139Sjp161948 	fprintf(stdout, "\n");
959*2139Sjp161948 
960*2139Sjp161948 	if (!EC_POINT_invert(group, P, ctx)) ABORT;
961*2139Sjp161948 	if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
962*2139Sjp161948 
963*2139Sjp161948 
964*2139Sjp161948 	/* Curve K-163 (FIPS PUB 186-2, App. 6) */
965*2139Sjp161948 	CHAR2_CURVE_TEST
966*2139Sjp161948 		(
967*2139Sjp161948 		"NIST curve K-163",
968*2139Sjp161948 		"0800000000000000000000000000000000000000C9",
969*2139Sjp161948 		"1",
970*2139Sjp161948 		"1",
971*2139Sjp161948 		"02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
972*2139Sjp161948 		"0289070FB05D38FF58321F2E800536D538CCDAA3D9",
973*2139Sjp161948 		1,
974*2139Sjp161948 		"04000000000000000000020108A2E0CC0D99F8A5EF",
975*2139Sjp161948 		"2",
976*2139Sjp161948 		163,
977*2139Sjp161948 		C2_K163
978*2139Sjp161948 		);
979*2139Sjp161948 
980*2139Sjp161948 	/* Curve B-163 (FIPS PUB 186-2, App. 6) */
981*2139Sjp161948 	CHAR2_CURVE_TEST
982*2139Sjp161948 		(
983*2139Sjp161948 		"NIST curve B-163",
984*2139Sjp161948 		"0800000000000000000000000000000000000000C9",
985*2139Sjp161948 		"1",
986*2139Sjp161948 		"020A601907B8C953CA1481EB10512F78744A3205FD",
987*2139Sjp161948 		"03F0EBA16286A2D57EA0991168D4994637E8343E36",
988*2139Sjp161948 		"00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
989*2139Sjp161948 		1,
990*2139Sjp161948 		"040000000000000000000292FE77E70C12A4234C33",
991*2139Sjp161948 		"2",
992*2139Sjp161948 		163,
993*2139Sjp161948 		C2_B163
994*2139Sjp161948 		);
995*2139Sjp161948 
996*2139Sjp161948 	/* Curve K-233 (FIPS PUB 186-2, App. 6) */
997*2139Sjp161948 	CHAR2_CURVE_TEST
998*2139Sjp161948 		(
999*2139Sjp161948 		"NIST curve K-233",
1000*2139Sjp161948 		"020000000000000000000000000000000000000004000000000000000001",
1001*2139Sjp161948 		"0",
1002*2139Sjp161948 		"1",
1003*2139Sjp161948 		"017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
1004*2139Sjp161948 		"01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
1005*2139Sjp161948 		0,
1006*2139Sjp161948 		"008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
1007*2139Sjp161948 		"4",
1008*2139Sjp161948 		233,
1009*2139Sjp161948 		C2_K233
1010*2139Sjp161948 		);
1011*2139Sjp161948 
1012*2139Sjp161948 	/* Curve B-233 (FIPS PUB 186-2, App. 6) */
1013*2139Sjp161948 	CHAR2_CURVE_TEST
1014*2139Sjp161948 		(
1015*2139Sjp161948 		"NIST curve B-233",
1016*2139Sjp161948 		"020000000000000000000000000000000000000004000000000000000001",
1017*2139Sjp161948 		"000000000000000000000000000000000000000000000000000000000001",
1018*2139Sjp161948 		"0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
1019*2139Sjp161948 		"00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
1020*2139Sjp161948 		"01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
1021*2139Sjp161948 		1,
1022*2139Sjp161948 		"01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
1023*2139Sjp161948 		"2",
1024*2139Sjp161948 		233,
1025*2139Sjp161948 		C2_B233
1026*2139Sjp161948 		);
1027*2139Sjp161948 
1028*2139Sjp161948 	/* Curve K-283 (FIPS PUB 186-2, App. 6) */
1029*2139Sjp161948 	CHAR2_CURVE_TEST
1030*2139Sjp161948 		(
1031*2139Sjp161948 		"NIST curve K-283",
1032*2139Sjp161948 		"0800000000000000000000000000000000000000000000000000000000000000000010A1",
1033*2139Sjp161948 		"0",
1034*2139Sjp161948 		"1",
1035*2139Sjp161948 		"0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
1036*2139Sjp161948 		"01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
1037*2139Sjp161948 		0,
1038*2139Sjp161948 		"01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
1039*2139Sjp161948 		"4",
1040*2139Sjp161948 		283,
1041*2139Sjp161948 		C2_K283
1042*2139Sjp161948 		);
1043*2139Sjp161948 
1044*2139Sjp161948 	/* Curve B-283 (FIPS PUB 186-2, App. 6) */
1045*2139Sjp161948 	CHAR2_CURVE_TEST
1046*2139Sjp161948 		(
1047*2139Sjp161948 		"NIST curve B-283",
1048*2139Sjp161948 		"0800000000000000000000000000000000000000000000000000000000000000000010A1",
1049*2139Sjp161948 		"000000000000000000000000000000000000000000000000000000000000000000000001",
1050*2139Sjp161948 		"027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
1051*2139Sjp161948 		"05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
1052*2139Sjp161948 		"03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
1053*2139Sjp161948 		1,
1054*2139Sjp161948 		"03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
1055*2139Sjp161948 		"2",
1056*2139Sjp161948 		283,
1057*2139Sjp161948 		C2_B283
1058*2139Sjp161948 		);
1059*2139Sjp161948 
1060*2139Sjp161948 	/* Curve K-409 (FIPS PUB 186-2, App. 6) */
1061*2139Sjp161948 	CHAR2_CURVE_TEST
1062*2139Sjp161948 		(
1063*2139Sjp161948 		"NIST curve K-409",
1064*2139Sjp161948 		"02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
1065*2139Sjp161948 		"0",
1066*2139Sjp161948 		"1",
1067*2139Sjp161948 		"0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
1068*2139Sjp161948 		"01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
1069*2139Sjp161948 		1,
1070*2139Sjp161948 		"007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
1071*2139Sjp161948 		"4",
1072*2139Sjp161948 		409,
1073*2139Sjp161948 		C2_K409
1074*2139Sjp161948 		);
1075*2139Sjp161948 
1076*2139Sjp161948 	/* Curve B-409 (FIPS PUB 186-2, App. 6) */
1077*2139Sjp161948 	CHAR2_CURVE_TEST
1078*2139Sjp161948 		(
1079*2139Sjp161948 		"NIST curve B-409",
1080*2139Sjp161948 		"02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
1081*2139Sjp161948 		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1082*2139Sjp161948 		"0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
1083*2139Sjp161948 		"015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
1084*2139Sjp161948 		"0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
1085*2139Sjp161948 		1,
1086*2139Sjp161948 		"010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
1087*2139Sjp161948 		"2",
1088*2139Sjp161948 		409,
1089*2139Sjp161948 		C2_B409
1090*2139Sjp161948 		);
1091*2139Sjp161948 
1092*2139Sjp161948 	/* Curve K-571 (FIPS PUB 186-2, App. 6) */
1093*2139Sjp161948 	CHAR2_CURVE_TEST
1094*2139Sjp161948 		(
1095*2139Sjp161948 		"NIST curve K-571",
1096*2139Sjp161948 		"80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1097*2139Sjp161948 		"0",
1098*2139Sjp161948 		"1",
1099*2139Sjp161948 		"026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
1100*2139Sjp161948 		"0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
1101*2139Sjp161948 		0,
1102*2139Sjp161948 		"020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
1103*2139Sjp161948 		"4",
1104*2139Sjp161948 		571,
1105*2139Sjp161948 		C2_K571
1106*2139Sjp161948 		);
1107*2139Sjp161948 
1108*2139Sjp161948 	/* Curve B-571 (FIPS PUB 186-2, App. 6) */
1109*2139Sjp161948 	CHAR2_CURVE_TEST
1110*2139Sjp161948 		(
1111*2139Sjp161948 		"NIST curve B-571",
1112*2139Sjp161948 		"80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1113*2139Sjp161948 		"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1114*2139Sjp161948 		"02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
1115*2139Sjp161948 		"0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
1116*2139Sjp161948 		"037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
1117*2139Sjp161948 		1,
1118*2139Sjp161948 		"03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
1119*2139Sjp161948 		"2",
1120*2139Sjp161948 		571,
1121*2139Sjp161948 		C2_B571
1122*2139Sjp161948 		);
1123*2139Sjp161948 
1124*2139Sjp161948 	/* more tests using the last curve */
1125*2139Sjp161948 
1126*2139Sjp161948 	if (!EC_POINT_copy(Q, P)) ABORT;
1127*2139Sjp161948 	if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1128*2139Sjp161948 	if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
1129*2139Sjp161948 	if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
1130*2139Sjp161948 	if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
1131*2139Sjp161948 
1132*2139Sjp161948 	if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
1133*2139Sjp161948 	if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
1134*2139Sjp161948 	if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
1135*2139Sjp161948 
1136*2139Sjp161948 	{
1137*2139Sjp161948 		const EC_POINT *points[3];
1138*2139Sjp161948 		const BIGNUM *scalars[3];
1139*2139Sjp161948 
1140*2139Sjp161948 		if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1141*2139Sjp161948 		points[0] = Q;
1142*2139Sjp161948 		points[1] = Q;
1143*2139Sjp161948 		points[2] = Q;
1144*2139Sjp161948 
1145*2139Sjp161948 		if (!BN_add(y, z, BN_value_one())) ABORT;
1146*2139Sjp161948 		if (BN_is_odd(y)) ABORT;
1147*2139Sjp161948 		if (!BN_rshift1(y, y)) ABORT;
1148*2139Sjp161948 		scalars[0] = y; /* (group order + 1)/2,  so  y*Q + y*Q = Q */
1149*2139Sjp161948 		scalars[1] = y;
1150*2139Sjp161948 
1151*2139Sjp161948 		fprintf(stdout, "combined multiplication ...");
1152*2139Sjp161948 		fflush(stdout);
1153*2139Sjp161948 
1154*2139Sjp161948 		/* z is still the group order */
1155*2139Sjp161948 		if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1156*2139Sjp161948 		if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
1157*2139Sjp161948 		if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
1158*2139Sjp161948 		if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
1159*2139Sjp161948 
1160*2139Sjp161948 		fprintf(stdout, ".");
1161*2139Sjp161948 		fflush(stdout);
1162*2139Sjp161948 
1163*2139Sjp161948 		if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
1164*2139Sjp161948 		if (!BN_add(z, z, y)) ABORT;
1165*2139Sjp161948 		BN_set_negative(z, 1);
1166*2139Sjp161948 		scalars[0] = y;
1167*2139Sjp161948 		scalars[1] = z; /* z = -(order + y) */
1168*2139Sjp161948 
1169*2139Sjp161948 		if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1170*2139Sjp161948 		if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1171*2139Sjp161948 
1172*2139Sjp161948 		fprintf(stdout, ".");
1173*2139Sjp161948 		fflush(stdout);
1174*2139Sjp161948 
1175*2139Sjp161948 		if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
1176*2139Sjp161948 		if (!BN_add(z, x, y)) ABORT;
1177*2139Sjp161948 		BN_set_negative(z, 1);
1178*2139Sjp161948 		scalars[0] = x;
1179*2139Sjp161948 		scalars[1] = y;
1180*2139Sjp161948 		scalars[2] = z; /* z = -(x+y) */
1181*2139Sjp161948 
1182*2139Sjp161948 		if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
1183*2139Sjp161948 		if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1184*2139Sjp161948 
1185*2139Sjp161948 		fprintf(stdout, " ok\n\n");
1186*2139Sjp161948 	}
1187*2139Sjp161948 
1188*2139Sjp161948 
1189*2139Sjp161948 #if 0
1190*2139Sjp161948 	timings(C2_K163, TIMING_BASE_PT, ctx);
1191*2139Sjp161948 	timings(C2_K163, TIMING_RAND_PT, ctx);
1192*2139Sjp161948 	timings(C2_K163, TIMING_SIMUL, ctx);
1193*2139Sjp161948 	timings(C2_B163, TIMING_BASE_PT, ctx);
1194*2139Sjp161948 	timings(C2_B163, TIMING_RAND_PT, ctx);
1195*2139Sjp161948 	timings(C2_B163, TIMING_SIMUL, ctx);
1196*2139Sjp161948 	timings(C2_K233, TIMING_BASE_PT, ctx);
1197*2139Sjp161948 	timings(C2_K233, TIMING_RAND_PT, ctx);
1198*2139Sjp161948 	timings(C2_K233, TIMING_SIMUL, ctx);
1199*2139Sjp161948 	timings(C2_B233, TIMING_BASE_PT, ctx);
1200*2139Sjp161948 	timings(C2_B233, TIMING_RAND_PT, ctx);
1201*2139Sjp161948 	timings(C2_B233, TIMING_SIMUL, ctx);
1202*2139Sjp161948 	timings(C2_K283, TIMING_BASE_PT, ctx);
1203*2139Sjp161948 	timings(C2_K283, TIMING_RAND_PT, ctx);
1204*2139Sjp161948 	timings(C2_K283, TIMING_SIMUL, ctx);
1205*2139Sjp161948 	timings(C2_B283, TIMING_BASE_PT, ctx);
1206*2139Sjp161948 	timings(C2_B283, TIMING_RAND_PT, ctx);
1207*2139Sjp161948 	timings(C2_B283, TIMING_SIMUL, ctx);
1208*2139Sjp161948 	timings(C2_K409, TIMING_BASE_PT, ctx);
1209*2139Sjp161948 	timings(C2_K409, TIMING_RAND_PT, ctx);
1210*2139Sjp161948 	timings(C2_K409, TIMING_SIMUL, ctx);
1211*2139Sjp161948 	timings(C2_B409, TIMING_BASE_PT, ctx);
1212*2139Sjp161948 	timings(C2_B409, TIMING_RAND_PT, ctx);
1213*2139Sjp161948 	timings(C2_B409, TIMING_SIMUL, ctx);
1214*2139Sjp161948 	timings(C2_K571, TIMING_BASE_PT, ctx);
1215*2139Sjp161948 	timings(C2_K571, TIMING_RAND_PT, ctx);
1216*2139Sjp161948 	timings(C2_K571, TIMING_SIMUL, ctx);
1217*2139Sjp161948 	timings(C2_B571, TIMING_BASE_PT, ctx);
1218*2139Sjp161948 	timings(C2_B571, TIMING_RAND_PT, ctx);
1219*2139Sjp161948 	timings(C2_B571, TIMING_SIMUL, ctx);
1220*2139Sjp161948 #endif
1221*2139Sjp161948 
1222*2139Sjp161948 
1223*2139Sjp161948 	if (ctx)
1224*2139Sjp161948 		BN_CTX_free(ctx);
1225*2139Sjp161948 	BN_free(p); BN_free(a);	BN_free(b);
1226*2139Sjp161948 	EC_GROUP_free(group);
1227*2139Sjp161948 	EC_POINT_free(P);
1228*2139Sjp161948 	EC_POINT_free(Q);
1229*2139Sjp161948 	EC_POINT_free(R);
1230*2139Sjp161948 	BN_free(x); BN_free(y); BN_free(z); BN_free(cof);
1231*2139Sjp161948 
1232*2139Sjp161948 	if (C2_K163) EC_GROUP_free(C2_K163);
1233*2139Sjp161948 	if (C2_B163) EC_GROUP_free(C2_B163);
1234*2139Sjp161948 	if (C2_K233) EC_GROUP_free(C2_K233);
1235*2139Sjp161948 	if (C2_B233) EC_GROUP_free(C2_B233);
1236*2139Sjp161948 	if (C2_K283) EC_GROUP_free(C2_K283);
1237*2139Sjp161948 	if (C2_B283) EC_GROUP_free(C2_B283);
1238*2139Sjp161948 	if (C2_K409) EC_GROUP_free(C2_K409);
1239*2139Sjp161948 	if (C2_B409) EC_GROUP_free(C2_B409);
1240*2139Sjp161948 	if (C2_K571) EC_GROUP_free(C2_K571);
1241*2139Sjp161948 	if (C2_B571) EC_GROUP_free(C2_B571);
1242*2139Sjp161948 
1243*2139Sjp161948 	}
1244*2139Sjp161948 
internal_curve_test(void)1245*2139Sjp161948 void internal_curve_test(void)
1246*2139Sjp161948 	{
1247*2139Sjp161948 	EC_builtin_curve *curves = NULL;
1248*2139Sjp161948 	size_t crv_len = 0, n = 0;
1249*2139Sjp161948 	int    ok = 1;
1250*2139Sjp161948 
1251*2139Sjp161948 	crv_len = EC_get_builtin_curves(NULL, 0);
1252*2139Sjp161948 
1253*2139Sjp161948 	curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
1254*2139Sjp161948 
1255*2139Sjp161948 	if (curves == NULL)
1256*2139Sjp161948 		return;
1257*2139Sjp161948 
1258*2139Sjp161948 	if (!EC_get_builtin_curves(curves, crv_len))
1259*2139Sjp161948 		{
1260*2139Sjp161948 		OPENSSL_free(curves);
1261*2139Sjp161948 		return;
1262*2139Sjp161948 		}
1263*2139Sjp161948 
1264*2139Sjp161948 	fprintf(stdout, "testing internal curves: ");
1265*2139Sjp161948 
1266*2139Sjp161948 	for (n = 0; n < crv_len; n++)
1267*2139Sjp161948 		{
1268*2139Sjp161948 		EC_GROUP *group = NULL;
1269*2139Sjp161948 		int nid = curves[n].nid;
1270*2139Sjp161948 		if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL)
1271*2139Sjp161948 			{
1272*2139Sjp161948 			ok = 0;
1273*2139Sjp161948 			fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
1274*2139Sjp161948 				" curve %s\n", OBJ_nid2sn(nid));
1275*2139Sjp161948 			/* try next curve */
1276*2139Sjp161948 			continue;
1277*2139Sjp161948 			}
1278*2139Sjp161948 		if (!EC_GROUP_check(group, NULL))
1279*2139Sjp161948 			{
1280*2139Sjp161948 			ok = 0;
1281*2139Sjp161948 			fprintf(stdout, "\nEC_GROUP_check() failed with"
1282*2139Sjp161948 				" curve %s\n", OBJ_nid2sn(nid));
1283*2139Sjp161948 			EC_GROUP_free(group);
1284*2139Sjp161948 			/* try the next curve */
1285*2139Sjp161948 			continue;
1286*2139Sjp161948 			}
1287*2139Sjp161948 		fprintf(stdout, ".");
1288*2139Sjp161948 		fflush(stdout);
1289*2139Sjp161948 		EC_GROUP_free(group);
1290*2139Sjp161948 		}
1291*2139Sjp161948 	if (ok)
1292*2139Sjp161948 		fprintf(stdout, " ok\n");
1293*2139Sjp161948 	else
1294*2139Sjp161948 		fprintf(stdout, " failed\n");
1295*2139Sjp161948 	OPENSSL_free(curves);
1296*2139Sjp161948 	return;
1297*2139Sjp161948 	}
1298*2139Sjp161948 
1299*2139Sjp161948 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1300*2139Sjp161948 
main(int argc,char * argv[])1301*2139Sjp161948 int main(int argc, char *argv[])
1302*2139Sjp161948 	{
1303*2139Sjp161948 
1304*2139Sjp161948 	/* enable memory leak checking unless explicitly disabled */
1305*2139Sjp161948 	if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
1306*2139Sjp161948 		{
1307*2139Sjp161948 		CRYPTO_malloc_debug_init();
1308*2139Sjp161948 		CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1309*2139Sjp161948 		}
1310*2139Sjp161948 	else
1311*2139Sjp161948 		{
1312*2139Sjp161948 		/* OPENSSL_DEBUG_MEMORY=off */
1313*2139Sjp161948 		CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
1314*2139Sjp161948 		}
1315*2139Sjp161948 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1316*2139Sjp161948 	ERR_load_crypto_strings();
1317*2139Sjp161948 
1318*2139Sjp161948 	RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
1319*2139Sjp161948 
1320*2139Sjp161948 	prime_field_tests();
1321*2139Sjp161948 	puts("");
1322*2139Sjp161948 	char2_field_tests();
1323*2139Sjp161948 	/* test the internal curves */
1324*2139Sjp161948 	internal_curve_test();
1325*2139Sjp161948 
13260Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
13270Sstevel@tonic-gate 	ENGINE_cleanup();
13280Sstevel@tonic-gate #endif
13290Sstevel@tonic-gate 	CRYPTO_cleanup_all_ex_data();
13300Sstevel@tonic-gate 	ERR_free_strings();
13310Sstevel@tonic-gate 	ERR_remove_state(0);
13320Sstevel@tonic-gate 	CRYPTO_mem_leaks_fp(stderr);
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	return 0;
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate #endif
1337