Commit 4882171d authored by Bodo Möller's avatar Bodo Möller
Browse files

EC curve stuff

Submitted by: Nils Larsch
parent 931627e6
Loading
Loading
Loading
Loading
+75 −37
Original line number Diff line number Diff line
@@ -149,6 +149,21 @@
 * -X9_62_239v2		- use the X9_62 239v2 example curve over a 239 bit prime field
 * -X9_62_239v3		- use the X9_62 239v3 example curve over a 239 bit prime field
 * -X9_62_256v1		- use the X9_62 239v1 example curve over a 256 bit prime field
 * -SECG_PRIME_112R1    - use the SECG 112r1 recommeded curve over a 112 bit prime field
 * -SECG_PRIME_112R2    - use the SECG 112r2 recommeded curve over a 112 bit prime field
 * -SECG_PRIME_128R1    - use the SECG 128r1 recommeded curve over a 128 bit prime field
 * -SECG_PRIME_128R2    - use the SECG 128r2 recommeded curve over a 128 bit prime field
 * -SECG_PRIME_160K1    - use the SECG 160k1 recommeded curve over a 160 bit prime field
 * -SECG_PRIME_160R1    - use the SECG 160r1 recommeded curve over a 160 bit prime field
 * -SECG_PRIME_160R2    - use the SECG 160r2 recommeded curve over a 160 bit prime field
 * -SECG_PRIME_192K1    - use the SECG 192k1 recommeded curve over a 192 bit prime field
 * -SECG_PRIME_192R1    - use the SECG 192r1 recommeded curve over a 192 bit prime field
 * -SECG_PRIME_224K1    - use the SECG 224k1 recommeded curve over a 224 bit prime field
 * -SECG_PRIME_224R1    - use the SECG 224r1 recommeded curve over a 224 bit prime field
 * -SECG_PRIME_256K1    - use the SECG 256k1 recommeded curve over a 256 bit prime field
 * -SECG_PRIME_256R1    - use the SECG 256r1 recommeded curve over a 256 bit prime field
 * -SECG_PRIME_384R1    - use the SECG 384r1 recommeded curve over a 384 bit prime field
 * -SECG_PRIME_521R1    - use the SECG 521r1 recommeded curve over a 521 bit prime field
 */

int MAIN(int, char **);
@@ -326,6 +341,21 @@ bad:
		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");
		BIO_printf(bio_err," -SECG_PRIME_112R1  use the SECG 112r1 recommeded curve over a 112 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_112R2  use the SECG 112r2 recommeded curve over a 112 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_128R1  use the SECG 128r1 recommeded curve over a 128 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_128R2  use the SECG 128r2 recommeded curve over a 128 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_160K1  use the SECG 160k1 recommeded curve over a 160 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_160R1  use the SECG 160r1 recommeded curve over a 160 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_160R2  use the SECG 160r2 recommeded curve over a 160 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_192K1  use the SECG 192k1 recommeded curve over a 192 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_192R1  use the SECG 192r1 recommeded curve over a 192 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_224K1  use the SECG 224k1 recommeded curve over a 224 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_224R1  use the SECG 224r1 recommeded curve over a 224 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_256K1  use the SECG 256k1 recommeded curve over a 256 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_256R1  use the SECG 256r1 recommeded curve over a 256 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_384R1  use the SECG 384r1 recommeded curve over a 384 bit prime field\n");
		BIO_printf(bio_err," -SECG_PRIME_521R1  use the SECG 521r1 recommeded curve over a 521 bit prime field\n");
		goto end;
	}

@@ -505,29 +535,37 @@ bad:
		 */

		printf("ECDSA *get_ecdsa%d(void)\n\t{\n",bits_p);
		printf("\tint ok=0;\n");
		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\n");
		printf("\t/* 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\n");
		printf("\tif ((tmp_1 = BN_bin2bn(ecdsa%d_p, sizeof(ecdsa%d_p), NULL)) == NULL) goto err;\n", bits_p, bits_p);
		printf("\tif ((tmp_2 = BN_bin2bn(ecdsa%d_a, sizeof(ecdsa%d_a), NULL)) == NULL) goto err;\n", bits_p, bits_p);
		printf("\tif ((tmp_3 = BN_bin2bn(ecdsa%d_b, sizeof(ecdsa%d_b), NULL)) == NULL) goto err;\n", bits_p, bits_p);
		printf("\tif ((ecdsa->group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL) goto err;\n\n");
		printf("\t/* build generator */\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\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\t}\n");
		printf("\tEC_POINT_set_affine_coordinates_GFp(ecdsa->group, point, tmp_1, tmp_2, NULL);\n");
		printf("\tif (!BN_bin2bn(ecdsa%d_x, sizeof(ecdsa%d_x), tmp_1)) goto err;\n", bits_p, bits_p);
		printf("\tif (!BN_bin2bn(ecdsa%d_y, sizeof(ecdsa%d_y), tmp_2)) goto err;\n", bits_p, bits_p);
		printf("\tif ((point = EC_POINT_new(ecdsa->group)) == NULL) goto err;\n");
		printf("\tif (!EC_POINT_set_affine_coordinates_GFp(ecdsa->group, point, tmp_1, tmp_2, NULL)) goto err;\n");
		printf("\t/* set generator, order and cofactor */\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\n");
		printf("\tif (!BN_bin2bn(ecdsa%d_o, sizeof(ecdsa%d_o), tmp_1)) goto err;\n", bits_p, bits_p);
		printf("\tif (!BN_bin2bn(ecdsa%d_c, sizeof(ecdsa%d_c), tmp_2)) goto err;\n", bits_p, bits_p);
		printf("\tif (!EC_GROUP_set_generator(ecdsa->group, point, tmp_1, tmp_2)) goto err;\n");
		printf("\n\tok=1;\n");
		printf("err:\n");
		printf("\tif (tmp_1) BN_free(tmp_1);\n");
		printf("\tif (tmp_2) BN_free(tmp_2);\n");
		printf("\tif (tmp_3) BN_free(tmp_3);\n");
		printf("\tif (point) EC_POINT_free(point);\n");
		printf("\tif (!ok)\n");
		printf("\t\t{\n");
		printf("\t\tECDSA_free(ecdsa);\n");
		printf("\t\tecdsa = NULL;\n");
		printf("\t\t}\n");
		printf("\treturn(ecdsa);\n\t}\n");
	}

+11 −3
Original line number Diff line number Diff line
@@ -519,7 +519,8 @@ int ECDSAParameters_print(BIO *bp, const ECDSA *x)
       unsigned char *buffer=NULL;
       int     buf_len;
       int     reason=ERR_R_EC_LIB, i, ret=0;
       BIGNUM  *tmp_1=NULL, *tmp_2=NULL, *tmp_3=NULL, *tmp_4=NULL;
       BIGNUM  *tmp_1=NULL, *tmp_2=NULL, *tmp_3=NULL, *tmp_4=NULL,
               *tmp_5=NULL, *tmp_6=NULL;
       BN_CTX  *ctx=NULL;
       EC_POINT *point=NULL;
 
@@ -530,13 +531,16 @@ int ECDSAParameters_print(BIO *bp, const ECDSA *x)
		goto err;
       }
       if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL ||
	   (tmp_3 = BN_new()) == NULL || (ctx = BN_CTX_new()) == NULL)
	   (tmp_3 = BN_new()) == NULL || (tmp_5 = BN_new()) == NULL ||
           (tmp_6 = BN_new()) == NULL || (ctx = BN_CTX_new()) == NULL)
       {
		reason = ERR_R_MALLOC_FAILURE;
		goto err;
	}
	if (!EC_GROUP_get_curve_GFp(x->group, tmp_1, tmp_2, tmp_3, ctx)) goto err;
	if ((point = EC_GROUP_get0_generator(x->group)) == NULL) goto err;
	if (!EC_GROUP_get_order(x->group, tmp_5, ctx)) goto err;
	if (!EC_GROUP_get_cofactor(x->group, tmp_6, ctx)) goto err;	
	buf_len = EC_POINT_point2oct(x->group, point, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
	if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL)
	{
@@ -566,12 +570,16 @@ int ECDSAParameters_print(BIO *bp, const ECDSA *x)
	if (!print(bp, "Curve a:", tmp_2, buffer, 4)) goto err;
	if (!print(bp, "Curve b:", tmp_3, buffer, 4)) goto err;
	if (!print(bp, "Generator (compressed):", tmp_4, buffer, 4)) goto err; 
	if (!print(bp, "Order:", tmp_5, buffer, 4)) goto err;
	if (!print(bp, "Cofactor:", tmp_6, buffer, 4)) goto err;
	ret=1;
err:
	if (tmp_1)  BN_free(tmp_1);
	if (tmp_2)  BN_free(tmp_2);
	if (tmp_3)  BN_free(tmp_3);
	if (tmp_4)  BN_free(tmp_4);
	if (tmp_5)  BN_free(tmp_5);
	if (tmp_6)  BN_free(tmp_6);
	if (ctx)    BN_CTX_free(ctx);
	if (buffer) OPENSSL_free(buffer);
	ECDSAerr(ECDSA_F_ECDSAPARAMETERS_PRINT, reason);
+1 −1

File changed.

Contains only whitespace changes.