- if (C) {
- size_t buf_len = 0, tmp_len = 0;
- const EC_POINT *point;
- int is_prime, len = 0;
- const EC_METHOD *meth = EC_GROUP_method_of(group);
-
- if ((ec_p = BN_new()) == NULL
- || (ec_a = BN_new()) == NULL
- || (ec_b = BN_new()) == NULL
- || (ec_gen = BN_new()) == NULL
- || (ec_order = BN_new()) == NULL
- || (ec_cofactor = BN_new()) == NULL) {
- perror("Can't allocate BN");
- goto end;
- }
-
- is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
- if (!is_prime) {
- BIO_printf(bio_err, "Can only handle X9.62 prime fields\n");
- goto end;
- }
-
- if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL))
- goto end;
-
- if ((point = EC_GROUP_get0_generator(group)) == NULL)
- goto end;
- if (!EC_POINT_point2bn(group, point,
- EC_GROUP_get_point_conversion_form(group),
- ec_gen, NULL))
- goto end;
- if (!EC_GROUP_get_order(group, ec_order, NULL))
- goto end;
- if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
- goto end;
-
- if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
- goto end;
-
- len = BN_num_bits(ec_order);
-
- if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
- buf_len = tmp_len;
- if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
- buf_len = tmp_len;
- if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
- buf_len = tmp_len;
- if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
- buf_len = tmp_len;
- if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
- buf_len = tmp_len;
- if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
- buf_len = tmp_len;
-
- buffer = app_malloc(buf_len, "BN buffer");
-
- BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
- print_bignum_var(out, ec_p, "ec_p", len, buffer);
- print_bignum_var(out, ec_a, "ec_a", len, buffer);
- print_bignum_var(out, ec_b, "ec_b", len, buffer);
- print_bignum_var(out, ec_gen, "ec_gen", len, buffer);
- print_bignum_var(out, ec_order, "ec_order", len, buffer);
- print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer);
- BIO_printf(out, " int ok = 0;\n"
- " EC_GROUP *group = NULL;\n"
- " EC_POINT *point = NULL;\n"
- " BIGNUM *tmp_1 = NULL;\n"
- " BIGNUM *tmp_2 = NULL;\n"
- " BIGNUM *tmp_3 = NULL;\n"
- "\n");
-
- BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof (ec_p_%d), NULL)) == NULL)\n"
- " goto err;\n", len, len);
- BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof (ec_a_%d), NULL)) == NULL)\n"
- " goto err;\n", len, len);
- BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof (ec_b_%d), NULL)) == NULL)\n"
- " goto err;\n", len, len);
- BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n"
- " goto err;\n"
- "\n");
- BIO_printf(out, " /* build generator */\n");
- BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof (ec_gen_%d), tmp_1)) == NULL)\n"
- " goto err;\n", len, len);
- BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n");
- BIO_printf(out, " if (point == NULL)\n"
- " goto err;\n");
- BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof (ec_order_%d), tmp_2)) == NULL)\n"
- " goto err;\n", len, len);
- BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof (ec_cofactor_%d), tmp_3)) == NULL)\n"
- " goto err;\n", len, len);
- BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n"
- " goto err;\n"
- "ok = 1;"
- "\n");
- BIO_printf(out, "err:\n"
- " BN_free(tmp_1);\n"
- " BN_free(tmp_2);\n"
- " BN_free(tmp_3);\n"
- " EC_POINT_free(point);\n"
- " if (!ok) {\n"
- " EC_GROUP_free(group);\n"
- " return NULL;\n"
- " }\n"
- " return (group);\n"
- "}\n");
- }