l = BN_bn2bin(tmp_3, data); printf("static unsigned char ecdsa%d_y[]={", bits_p); for (i=0; i<l; i++) { if ((i%12) == 0) printf("\n\t"); printf("0x%02X,",data[i]); } printf("\n\t};\n\n");  l = BN_bn2bin(tmp_3, data); printf("static unsigned char ecdsa%d_o[]={", bits_p); for (i=0; i<l; i++) { if ((i%12) == 0) printf("\n\t"); printf("0x%02X,",data[i]); } printf("\n\t};\n\n");  l = BN_bn2bin(tmp_3, data); printf("static unsigned char ecdsa%d_c[]={", bits_p); for (i=0; i<l; i++) { if ((i%12) == 0) printf("\n\t"); printf("0x%02X,",data[i]); } printf("\n\t};\n\n");
int MAIN(int argc, char **argv){	ENGINE 	*e = NULL;	ECDSA 	*ecdsa = NULL;	int 	i, badops = 0, text = 0;	BIO 	*in = NULL, *out = NULL;	int 	informat, outformat, noout = 0, C = 0, ret = 1;	char 	*infile, *outfile, *prog, *inrand = NULL;	int 	genkey = 0;	int 	need_rand = 0;	char 	*engine=NULL;	int	curve_type = EC_GROUP_NO_CURVE;	int	named_curve = 0;	BIGNUM	*tmp_1 = NULL, *tmp_2 = NULL, *tmp_3 = NULL, *tmp_4 = NULL, *tmp_5 = NULL,		*tmp_6 = NULL, *tmp_7 = NULL;	BN_CTX	*ctx = NULL;	EC_POINT *point = NULL;	unsigned char *data = NULL;	apps_startup();	if (bio_err == NULL)		if ((bio_err=BIO_new(BIO_s_file())) != NULL)			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);	if (!load_config(bio_err, NULL))		goto end;	infile=NULL;	outfile=NULL;	informat=FORMAT_PEM;	outformat=FORMAT_PEM;	prog=argv[0];	argc--;	argv++;	while (argc >= 1)		{		if 	(strcmp(*argv,"-inform") == 0)		{			if (--argc < 1) goto bad;			informat=str2fmt(*(++argv));		}		else if (strcmp(*argv,"-outform") == 0)		{			if (--argc < 1) goto bad;			outformat=str2fmt(*(++argv));		}		else if (strcmp(*argv,"-in") == 0)		{			if (--argc < 1) goto bad;			infile= *(++argv);		}		else if (strcmp(*argv,"-out") == 0)		{			if (--argc < 1) goto bad;			outfile= *(++argv);		}		else if(strcmp(*argv, "-engine") == 0)		{			if (--argc < 1) goto bad;			engine = *(++argv);		}		else if (strcmp(*argv,"-text") == 0)			text = 1;		else if (strcmp(*argv,"-C") == 0)			C = 1;		else if (strcmp(*argv,"-genkey") == 0)		{			genkey = 1;			need_rand = 1;		}		else if (strcmp(*argv,"-rand") == 0)		{			if (--argc < 1) goto bad;			inrand= *(++argv);			need_rand=1;		}		else if (strcmp(*argv, "-named_curve") == 0)			named_curve = 1;		else if (strcmp(*argv, "-NIST_192") == 0)			curve_type = EC_GROUP_NIST_PRIME_192;		else if (strcmp(*argv, "-NIST_224") == 0)			curve_type = EC_GROUP_NIST_PRIME_224;		else if (strcmp(*argv, "-NIST_256") == 0)			curve_type = EC_GROUP_NIST_PRIME_256;		else if (strcmp(*argv, "-NIST_384") == 0)			curve_type = EC_GROUP_NIST_PRIME_384;		else if (strcmp(*argv, "-NIST_521") == 0)			curve_type = EC_GROUP_NIST_PRIME_521;		else if (strcmp(*argv, "-X9_62_192v1") == 0)			curve_type = EC_GROUP_X9_62_PRIME_192V1;		else if (strcmp(*argv, "-X9_62_192v2") == 0)			curve_type = EC_GROUP_X9_62_PRIME_192V2;		else if (strcmp(*argv, "-X9_62_192v3") == 0)			curve_type = EC_GROUP_X9_62_PRIME_192V3;		else if (strcmp(*argv, "-X9_62_239v1") == 0)			curve_type = EC_GROUP_X9_62_PRIME_239V1;		else if (strcmp(*argv, "-X9_62_239v2") == 0)			curve_type = EC_GROUP_X9_62_PRIME_239V2;		else if (strcmp(*argv, "-X9_62_239v3") == 0)			curve_type = EC_GROUP_X9_62_PRIME_239V3;		else if (strcmp(*argv, "-X9_62_256v1") == 0)			curve_type = EC_GROUP_X9_62_PRIME_256V1;		else if (strcmp(*argv, "-noout") == 0)			noout=1;		else		{			BIO_printf(bio_err,"unknown option %s\n",*argv);			badops=1;			break;		}		argc--;		argv++;	}	if (badops)	{bad:		BIO_printf(bio_err,"%s [options] [bits] <infile >outfile\n",prog);		BIO_printf(bio_err,"where options are\n");		BIO_printf(bio_err," -inform arg   input format - DER or PEM\n");		BIO_printf(bio_err," -outform arg  output format - DER or PEM\n");		BIO_printf(bio_err," -in arg       input file\n");		BIO_printf(bio_err," -out arg      output file\n");		BIO_printf(bio_err," -text         print the key in text\n");		BIO_printf(bio_err," -C            Output C code\n");		BIO_printf(bio_err," -noout        no output\n");		BIO_printf(bio_err," -rand         files to use for random number input\n");		BIO_printf(bio_err," -engine e     use engine e, possibly a hardware device.\n");		BIO_printf(bio_err," -named_curve  use the curve oid instead of the parameters\n");		BIO_printf(bio_err," -NIST_192     use the NIST recommeded curve parameters over a 192 bit prime field\n");		BIO_printf(bio_err," -NIST_224     use the NIST recommeded curve parameters over a 224 bit prime field\n");		BIO_printf(bio_err," -NIST_256     use the NIST recommeded curve parameters over a 256 bit prime field\n");		BIO_printf(bio_err," -NIST_384     use the NIST recommeded curve parameters over a 384 bit prime field\n");		BIO_printf(bio_err," -NIST_521     use the NIST recommeded curve parameters over a 521 bit prime field\n");		BIO_printf(bio_err," -X9_62_192v1  use the X9_62 192v1 example curve over a 192 bit prime field\n");		BIO_printf(bio_err," -X9_62_192v2  use the X9_62 192v2 example curve over a 192 bit prime field\n");		BIO_printf(bio_err," -X9_62_192v3  use the X9_62 192v3 example curve over a 192 bit prime field\n");		BIO_printf(bio_err," -X9_62_239v1  use the X9_62 239v1 example curve over a 239 bit prime field\n");		BIO_printf(bio_err," -X9_62_239v2  use the X9_62 239v2 example curve over a 239 bit prime field\n");		BIO_printf(bio_err," -X9_62_239v3  use the X9_62 239v3 example curve over a 239 bit prime field\n");		BIO_printf(bio_err," -X9_62_256v1  use the X9_62 239v1 example curve over a 256 bit prime field\n");		goto end;	}	ERR_load_crypto_strings();	in=BIO_new(BIO_s_file());	out=BIO_new(BIO_s_file());	if ((in == NULL) || (out == NULL))	{		ERR_print_errors(bio_err);		goto end;	}	if (infile == NULL)		BIO_set_fp(in,stdin,BIO_NOCLOSE);	else	{		if (BIO_read_filename(in,infile) <= 0)		{			perror(infile);			goto end;		}	}	if (outfile == NULL)	{		BIO_set_fp(out,stdout,BIO_NOCLOSE);#ifdef OPENSSL_SYS_VMS		{		BIO *tmpbio = BIO_new(BIO_f_linebuffer());		out = BIO_push(tmpbio, out);		}#endif	}	else	{		if (BIO_write_filename(out,outfile) <= 0)		{			perror(outfile);			goto end;		}	}        e = setup_engine(bio_err, engine, 0);	if (need_rand)	{		app_RAND_load_file(NULL, bio_err, (inrand != NULL));		if (inrand != NULL)			BIO_printf(bio_err,"%ld semi-random bytes loaded\n",				app_RAND_load_files(inrand));	}	if (curve_type != EC_GROUP_NO_CURVE)	{		if ((ecdsa = ECDSA_new()) == NULL)			goto end;		ecdsa->group = EC_GROUP_new_by_name(curve_type);		if (named_curve)			ECDSA_set_parameter_flags(ecdsa, ECDSA_FLAG_NAMED_CURVE);	}	else if (informat == FORMAT_ASN1)		ecdsa = d2i_ECDSAParameters_bio(in,NULL);	else if (informat == FORMAT_PEM)		ecdsa = PEM_read_bio_ECDSAParameters(in, NULL, NULL, NULL);	else	{		BIO_printf(bio_err, "bad input format specified\n");		goto end;	}	if (ecdsa == NULL)	{		BIO_printf(bio_err, "unable to load ECDSA parameters\n");		ERR_print_errors(bio_err);		goto end;	}	if (text)	{		ECDSAParameters_print(out, ecdsa);	}		if (C)	  {	/* TODO : characteristic two */		int 	l, len, bits_p;		if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL ||		    (tmp_3 = BN_new()) == NULL || (tmp_4 = BN_new()) == NULL ||		    (tmp_5 = BN_new()) == NULL || (tmp_6 = BN_new()) == NULL ||                    (tmp_7 = BN_new()) == NULL || (ctx = BN_CTX_new()) == NULL)		{			perror("OPENSSL_malloc");			goto end;		}		if (!EC_GROUP_get_curve_GFp(ecdsa->group, tmp_1, tmp_2, tmp_3, ctx))			goto end;		if ((point = EC_GROUP_get0_generator(ecdsa->group)) == NULL)			goto end;		if (!EC_POINT_get_affine_coordinates_GFp(ecdsa->group, point, tmp_4, tmp_5, ctx))			goto end;		if (!EC_GROUP_get_order(ecdsa->group, tmp_6, ctx))			goto end;		if (!EC_GROUP_get_cofactor(ecdsa->group, tmp_7, ctx))			goto end;				len    = BN_num_bytes(tmp_1);		bits_p = BN_num_bits(tmp_1);		data=(unsigned char *)OPENSSL_malloc(len+20);		if (data == NULL)		{			perror("OPENSSL_malloc");			goto end;		}		l = BN_bn2bin(tmp_1, data);		printf("static unsigned char ecdsa%d_p[]={", bits_p);		for (i=0; i<l; i++)		{			if ((i%12) == 0) printf("\n\t");			printf("0x%02X,",data[i]);		}		printf("\n\t};\n");		l = BN_bn2bin(tmp_2, data);		printf("static unsigned char ecdsa%d_a[]={",bits_p);		for (i=0; i<l; i++)		{			if ((i%12) == 0) printf("\n\t");			printf("0x%02X,",data[i]);		}		printf("\n\t};\n");		l = BN_bn2bin(tmp_3, data);		printf("static unsigned char ecdsa%d_b[]={", bits_p);		for (i=0; i<l; i++)		{			if ((i%12) == 0) printf("\n\t");			printf("0x%02X,",data[i]);		}		printf("\n\t};\n\n");		l = BN_bn2bin(tmp_3, data);		printf("static unsigned char ecdsa%d_x[]={", bits_p);		for (i=0; i<l; i++)		{			if ((i%12) == 0) printf("\n\t");			printf("0x%02X,",data[i]);		}		printf("\n\t};\n\n");		l = BN_bn2bin(tmp_3, data);		printf("static unsigned char ecdsa%d_y[]={", bits_p);		for (i=0; i<l; i++)		{			if ((i%12) == 0) printf("\n\t");			printf("0x%02X,",data[i]);		}		printf("\n\t};\n\n");		l = BN_bn2bin(tmp_3, data);		printf("static unsigned char ecdsa%d_o[]={", bits_p);		for (i=0; i<l; i++)		{			if ((i%12) == 0) printf("\n\t");			printf("0x%02X,",data[i]);		}		printf("\n\t};\n\n");		l = BN_bn2bin(tmp_3, data);		printf("static unsigned char ecdsa%d_c[]={", bits_p);		for (i=0; i<l; i++)		{			if ((i%12) == 0) printf("\n\t");			printf("0x%02X,",data[i]);		}		printf("\n\t};\n\n");		printf("ECDSA *get_ecdsa%d(void)\n\t{\n",bits_p);		printf("\tECDSA    *ecdsa=NULL;\n");		printf("\tEC_POINT *point=NULL;\n");		printf("\tBIGNUM   *tmp_1=NULL,*tmp_2=NULL,*tmp_3=NULL;\n\n");		printf("\tif ((ecdsa=ECDSA_new()) == NULL)\n");		printf("\t\treturn(NULL);\n");		printf("\t/* first : generate EC_GROUP-structure */\n");		printf("\ttmp_1 = BN_bin2bn(ecdsa%d_p, sizeof(ecdsa%d_p), NULL);\n", bits_p, bits_p);		printf("\ttmp_2 = BN_bin2bn(ecdsa%d_a, sizeof(ecdsa%d_a), NULL);\n", bits_p, bits_p);		printf("\ttmp_3 = BN_bin2bn(ecdsa%d_b, sizeof(ecdsa%d_b), NULL);\n", bits_p, bits_p);		printf("\tecdsa->group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL);\n");		printf("\t/* second : set coordinates of the generating point */\n");		printf("\tBN_bin2bn(ecdsa%d_x, sizeof(ecdsa%d_x), tmp_1);\n", bits_p, bits_p);		printf("\tBN_bin2bn(ecdsa%d_y, sizeof(ecdsa%d_y), tmp_2);\n", bits_p, bits_p);		printf("\tif ((point = EC_POINT_new(ecdsa->group)) == NULL)\n");		printf("\t{\n\t\tECDSA_free(ecdsa); BN_free(tmp_1); BN_free(tmp_2); BN_free(tmp_3);\n");		printf("\t\treturn(NULL);\n\t}\n");		printf("\tEC_POINT_set_affine_coordinates_GFp(ecdsa->group, point, tmp_1, tmp_2, NULL);\n");		printf("\t/* and finally : insert the generating point and its order in the EC_GROUP-structure */\n");		printf("\tBN_bin2bn(ecdsa%d_o, sizeof(ecdsa%d_o), tmp_1);\n", bits_p, bits_p);		printf("\tBN_bin2bn(ecdsa%d_c, sizeof(ecdsa%d_c), tmp_2);\n", bits_p, bits_p);		printf("\tEC_GROUP_set_generator(ecdsa->group, point, tmp_1, tmp_2);\n");		printf("\tBN_free(tmp_1); BN_free(tmp_2); BN_free(tmp_3);\n");		printf("\tEC_POINT_free(point);\n");		printf("\treturn(ecdsa);\n\t}\n");	}	if (!noout)	{		if (outformat == FORMAT_ASN1)			i = i2d_ECDSAParameters_bio(out, ecdsa);		else if (outformat == FORMAT_PEM)			i = PEM_write_bio_ECDSAParameters(out, ecdsa);		else			{			BIO_printf(bio_err,"bad output format specified for outfile\n");			goto end;		}		if (!i)		{			BIO_printf(bio_err, "unable to write ECDSA parameters\n");			ERR_print_errors(bio_err);			goto end;		}	}	if (genkey)	{		ECDSA *ecdsakey;		assert(need_rand);		if ((ecdsakey = ECDSAParameters_dup(ecdsa)) == NULL) goto end;		if (!ECDSA_generate_key(ecdsakey)) goto end;		if (outformat == FORMAT_ASN1)			i = i2d_ECDSAPrivateKey_bio(out, ecdsakey);		else if (outformat == FORMAT_PEM)			i = PEM_write_bio_ECDSAPrivateKey(out, ecdsakey, NULL, NULL, 0, NULL, NULL);		else			{			BIO_printf(bio_err, "bad output format specified for outfile\n");			goto end;		}		ECDSA_free(ecdsakey);	}	if (need_rand)		app_RAND_write_file(NULL, bio_err);	ret=0;end:	if (in != NULL) 	BIO_free(in);	if (out != NULL) 	BIO_free_all(out);	if (ecdsa != NULL) 	ECDSA_free(ecdsa);	if (tmp_1)		BN_free(tmp_1);	if (tmp_2)		BN_free(tmp_2);	if (tmp_3)		BN_free(tmp_3);	if (tmp_3)		BN_free(tmp_4);	if (tmp_3)		BN_free(tmp_5);	if (tmp_3)		BN_free(tmp_6);	if (tmp_3)		BN_free(tmp_7);	if (ctx)		BN_CTX_free(ctx);	if (data)		OPENSSL_free(data);	apps_shutdown();	EXIT(ret);}