static int
param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
{
	if (EC_GROUP_get_curve_name(GOST_KEY_get0_group(a->pkey.gost)) !=
	    EC_GROUP_get_curve_name(GOST_KEY_get0_group(b->pkey.gost)))
		return 0;

	if (GOST_KEY_get_digest(a->pkey.gost) !=
	    GOST_KEY_get_digest(b->pkey.gost))
		return 0;

	return 1;
}
int
gost2001_keygen(GOST_KEY *ec)
{
	BIGNUM *order = BN_new(), *d = BN_new();
	const EC_GROUP *group = GOST_KEY_get0_group(ec);
	int rc = 0;

	if (order == NULL || d == NULL)
		goto err;
	if (EC_GROUP_get_order(group, order, NULL) == 0)
		goto err;

	do {
		if (BN_rand_range(d, order) == 0) {
			GOSTerr(GOST_F_GOST2001_KEYGEN,
				GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
			goto err;
		}
	} while (BN_is_zero(d));

	if (GOST_KEY_set_private_key(ec, d) == 0)
		goto err;
	rc = gost2001_compute_public(ec);

err:
	BN_free(d);
	BN_free(order);
	return rc;
}
static ASN1_STRING *
encode_gost01_algor_params(const EVP_PKEY *key)
{
	ASN1_STRING *params = ASN1_STRING_new();
	GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
	int pkey_param_nid = NID_undef;

	if (params == NULL || gkp == NULL) {
		GOSTerr(GOST_F_ENCODE_GOST01_ALGOR_PARAMS,
		    ERR_R_MALLOC_FAILURE);
		ASN1_STRING_free(params);
		params = NULL;
		goto err;
	}

	pkey_param_nid =
	    EC_GROUP_get_curve_name(GOST_KEY_get0_group(key->pkey.gost));
	gkp->key_params = OBJ_nid2obj(pkey_param_nid);
	gkp->hash_params = OBJ_nid2obj(GOST_KEY_get_digest(key->pkey.gost));
	/*gkp->cipher_params = OBJ_nid2obj(cipher_param_nid); */
	params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
	if (params->length <= 0) {
		GOSTerr(GOST_F_ENCODE_GOST01_ALGOR_PARAMS,
		    ERR_R_MALLOC_FAILURE);
		ASN1_STRING_free(params);
		params = NULL;
		goto err;
	}
	params->type = V_ASN1_SEQUENCE;
err:
	GOST_KEY_PARAMS_free(gkp);
	return params;
}
static int
param_missing_gost01(const EVP_PKEY *pk)
{
	const GOST_KEY *ec = pk->pkey.gost;

	if (ec == NULL)
		return 1;
	if (GOST_KEY_get0_group(ec) == NULL)
		return 1;
	if (GOST_KEY_get_digest(ec) == NID_undef)
		return 1;
	return 0;
}
static int
param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx)
{
	int param_nid =
	    EC_GROUP_get_curve_name(GOST_KEY_get0_group(pkey->pkey.gost));

	if (BIO_indent(out, indent, 128) == 0)
		return 0;
	BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
	if (BIO_indent(out, indent, 128) == 0)
		return 0;
	BIO_printf(out, "Digest Algorithm: %s\n",
	    OBJ_nid2ln(GOST_KEY_get_digest(pkey->pkey.gost)));
	return 1;
}
static int
pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx)
{
	BN_CTX *ctx = BN_CTX_new();
	BIGNUM *X, *Y;
	const EC_POINT *pubkey;
	const EC_GROUP *group;

	if (ctx == NULL) {
		GOSTerr(GOST_F_PUB_PRINT_GOST01, ERR_R_MALLOC_FAILURE);
		return 0;
	}
	BN_CTX_start(ctx);
	if ((X = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((Y = BN_CTX_get(ctx)) == NULL)
		goto err;
	pubkey = GOST_KEY_get0_public_key(pkey->pkey.gost);
	group = GOST_KEY_get0_group(pkey->pkey.gost);
	if (EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y,
	    ctx) == 0) {
		GOSTerr(GOST_F_PUB_PRINT_GOST01, ERR_R_EC_LIB);
		goto err;
	}
	if (BIO_indent(out, indent, 128) == 0)
		goto err;
	BIO_printf(out, "Public key:\n");
	if (BIO_indent(out, indent + 3, 128) == 0)
		goto err;
	BIO_printf(out, "X:");
	BN_print(out, X);
	BIO_printf(out, "\n");
	BIO_indent(out, indent + 3, 128);
	BIO_printf(out, "Y:");
	BN_print(out, Y);
	BIO_printf(out, "\n");

	BN_CTX_end(ctx);
	BN_CTX_free(ctx);

	return param_print_gost01(out, pkey, indent, pctx);

err:
	BN_CTX_end(ctx);
	BN_CTX_free(ctx);
	return 0;
}
static int
pub_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
{
	const GOST_KEY *ea = a->pkey.gost;
	const GOST_KEY *eb = b->pkey.gost;
	const EC_POINT *ka, *kb;
	int ret = 0;

	if (ea == NULL || eb == NULL)
		return 0;
	ka = GOST_KEY_get0_public_key(ea);
	kb = GOST_KEY_get0_public_key(eb);
	if (ka == NULL || kb == NULL)
		return 0;
	ret = (0 == EC_POINT_cmp(GOST_KEY_get0_group(ea), ka, kb, NULL));
	return ret;
}
int
gost2001_compute_public(GOST_KEY *ec)
{
	const EC_GROUP *group = GOST_KEY_get0_group(ec);
	EC_POINT *pub_key = NULL;
	const BIGNUM *priv_key = NULL;
	BN_CTX *ctx = NULL;
	int ok = 0;

	if (group == NULL) {
		GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,
		    GOST_R_KEY_IS_NOT_INITIALIZED);
		return 0;
	}
	ctx = BN_CTX_new();
	if (ctx == NULL) {
		GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,
		    ERR_R_MALLOC_FAILURE);
		return 0;
	}
	BN_CTX_start(ctx);
	if ((priv_key = GOST_KEY_get0_private_key(ec)) == NULL)
		goto err;

	pub_key = EC_POINT_new(group);
	if (pub_key == NULL)
		goto err;
	if (EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx) == 0)
		goto err;
	if (GOST_KEY_set_public_key(ec, pub_key) == 0)
		goto err;
	ok = 1;

	if (ok == 0) {
err:
		GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
	}
	EC_POINT_free(pub_key);
	if (ctx != NULL) {
		BN_CTX_end(ctx);
		BN_CTX_free(ctx);
	}
	return ok;
}
/* Implementation of CryptoPro VKO 34.10-2001 algorithm */
int
VKO_compute_key(BIGNUM *X, BIGNUM *Y, const GOST_KEY *pkey, GOST_KEY *priv_key,
    const BIGNUM *ukm)
{
	BIGNUM *p = NULL, *order = NULL;
	const BIGNUM *key = GOST_KEY_get0_private_key(priv_key);
	const EC_GROUP *group = GOST_KEY_get0_group(priv_key);
	const EC_POINT *pub_key = GOST_KEY_get0_public_key(pkey);
	EC_POINT *pnt;
	BN_CTX *ctx = NULL;
	int ok = 0;

	pnt = EC_POINT_new(group);
	if (pnt == NULL)
		goto err;
	ctx = BN_CTX_new();
	if (ctx == NULL)
		goto err;
	BN_CTX_start(ctx);
	if ((p = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((order = BN_CTX_get(ctx)) == NULL)
		goto err;
	if (EC_GROUP_get_order(group, order, ctx) == 0)
		goto err;
	if (BN_mod_mul(p, key, ukm, order, ctx) == 0)
		goto err;
	if (EC_POINT_mul(group, pnt, NULL, pub_key, p, ctx) == 0)
		goto err;
	if (EC_POINT_get_affine_coordinates_GFp(group, pnt, X, Y, ctx) == 0)
		goto err;
	ok = 1;

err:
	if (ctx != NULL) {
		BN_CTX_end(ctx);
		BN_CTX_free(ctx);
	}
	EC_POINT_free(pnt);
	return ok;
}
Exemple #10
0
static int
param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
{
	GOST_KEY *eto = to->pkey.gost;
	const GOST_KEY *efrom = from->pkey.gost;
	int ret = 1;

	if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
		GOSTerr(GOST_F_PARAM_COPY_GOST01,
		    GOST_R_INCOMPATIBLE_ALGORITHMS);
		return 0;
	}
	if (efrom == NULL) {
		GOSTerr(GOST_F_PARAM_COPY_GOST01,
		    GOST_R_KEY_PARAMETERS_MISSING);
		return 0;
	}
	if (eto == NULL) {
		eto = GOST_KEY_new();
		if (eto == NULL) {
			GOSTerr(GOST_F_PARAM_COPY_GOST01,
			    ERR_R_MALLOC_FAILURE);
			return 0;
		}
		if (EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto) == 0) {
			GOST_KEY_free(eto);
			return 0;
		}
	}
	GOST_KEY_set_group(eto, GOST_KEY_get0_group(efrom));
	GOST_KEY_set_digest(eto, GOST_KEY_get_digest(efrom));
	if (GOST_KEY_get0_private_key(eto) != NULL)
		ret = gost2001_compute_public(eto);

	return ret;
}
Exemple #11
0
size_t GOST_KEY_get_size(const GOST_KEY *r)
{
    int i;
    BIGNUM *order = NULL;
    const EC_GROUP *group;

    if (r == NULL)
        return 0;
    group = GOST_KEY_get0_group(r);
    if (group == NULL)
        return 0;

    if ((order = BN_new()) == NULL)
        return 0;

    if (!EC_GROUP_get_order(group, order, NULL)) {
        BN_clear_free(order);
        return 0;
    }

    i = BN_num_bytes(order);
    BN_clear_free(order);
    return (i);
}
Exemple #12
0
static int
pub_encode_gost01(X509_PUBKEY *pub, const EVP_PKEY *pk)
{
	ASN1_OBJECT *algobj = NULL;
	ASN1_OCTET_STRING *octet = NULL;
	ASN1_STRING *params = NULL;
	void *pval = NULL;
	unsigned char *buf = NULL, *sptr;
	int key_size, ret = 0;
	const EC_POINT *pub_key;
	BIGNUM *X = NULL, *Y = NULL;
	const GOST_KEY *ec = pk->pkey.gost;
	int ptype = V_ASN1_UNDEF;

	algobj = OBJ_nid2obj(GostR3410_get_pk_digest(GOST_KEY_get_digest(ec)));
	if (pk->save_parameters) {
		params = encode_gost01_algor_params(pk);
		if (params == NULL)
			return 0;
		pval = params;
		ptype = V_ASN1_SEQUENCE;
	}

	key_size = GOST_KEY_get_size(ec);

	pub_key = GOST_KEY_get0_public_key(ec);
	if (pub_key == NULL) {
		GOSTerr(GOST_F_PUB_ENCODE_GOST01, GOST_R_PUBLIC_KEY_UNDEFINED);
		goto err;
	}

	octet = ASN1_OCTET_STRING_new();
	if (octet == NULL) {
		GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
		goto err;
	}

	ret = ASN1_STRING_set(octet, NULL, 2 * key_size);
	if (ret == 0) {
		GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_INTERNAL_ERROR);
		goto err;
	}

	sptr = ASN1_STRING_data(octet);

	X = BN_new();
	Y = BN_new();
	if (X == NULL || Y == NULL) {
		GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
		goto err;
	}

	if (EC_POINT_get_affine_coordinates_GFp(GOST_KEY_get0_group(ec),
	    pub_key, X, Y, NULL) == 0) {
		GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_EC_LIB);
		goto err;
	}

	GOST_bn2le(X, sptr, key_size);
	GOST_bn2le(Y, sptr + key_size, key_size);

	BN_free(Y);
	BN_free(X);

	ret = i2d_ASN1_OCTET_STRING(octet, &buf);
	ASN1_BIT_STRING_free(octet);
	if (ret < 0)
		return 0;

	return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);

err:
	BN_free(Y);
	BN_free(X);
	ASN1_BIT_STRING_free(octet);
	ASN1_STRING_free(params);
	return 0;
}
Exemple #13
0
int
gost2001_do_verify(BIGNUM *md, ECDSA_SIG *sig, GOST_KEY *ec)
{
	BN_CTX *ctx = BN_CTX_new();
	const EC_GROUP *group = GOST_KEY_get0_group(ec);
	BIGNUM *order;
	BIGNUM *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
	BIGNUM *X = NULL, *tmp = NULL;
	EC_POINT *C = NULL;
	const EC_POINT *pub_key = NULL;
	int ok = 0;

	if (ctx == NULL)
		goto err;
	BN_CTX_start(ctx);
	if ((order = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((e = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((z1 = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((z2 = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((tmp = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((X = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((R = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((v = BN_CTX_get(ctx)) == NULL)
		goto err;

	if (EC_GROUP_get_order(group, order, ctx) == 0)
		goto err;
	pub_key = GOST_KEY_get0_public_key(ec);
	if (BN_is_zero(sig->s) || BN_is_zero(sig->r) ||
	    BN_cmp(sig->s, order) >= 1 || BN_cmp(sig->r, order) >= 1) {
		GOSTerr(GOST_F_GOST2001_DO_VERIFY,
		    GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
		goto err;
	}

	if (BN_mod(e, md, order, ctx) == 0)
		goto err;
	if (BN_is_zero(e))
		BN_one(e);
	if ((v = BN_mod_inverse(v, e, order, ctx)) == NULL)
		goto err;
	if (BN_mod_mul(z1, sig->s, v, order, ctx) == 0)
		goto err;
	if (BN_sub(tmp, order, sig->r) == 0)
		goto err;
	if (BN_mod_mul(z2, tmp, v, order, ctx) == 0)
		goto err;
	if ((C = EC_POINT_new(group)) == NULL)
		goto err;
	if (EC_POINT_mul(group, C, z1, pub_key, z2, ctx) == 0) {
		GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_EC_LIB);
		goto err;
	}
	if (EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx) == 0) {
		GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_EC_LIB);
		goto err;
	}
	if (BN_mod(R, X, order, ctx) == 0)
		goto err;
	if (BN_cmp(R, sig->r) != 0) {
		GOSTerr(GOST_F_GOST2001_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
	} else {
		ok = 1;
	}
err:
	EC_POINT_free(C);
	if (ctx != NULL) {
		BN_CTX_end(ctx);
		BN_CTX_free(ctx);
	}
	return ok;
}
Exemple #14
0
ECDSA_SIG *
gost2001_do_sign(BIGNUM *md, GOST_KEY *eckey)
{
	ECDSA_SIG *newsig = NULL;
	BIGNUM *order = NULL;
	const EC_GROUP *group;
	const BIGNUM *priv_key;
	BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL, *k =
	    NULL, *e = NULL;
	EC_POINT *C = NULL;
	BN_CTX *ctx = BN_CTX_new();
	int ok = 0;

	if (ctx == NULL) {
		GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
		return NULL;
	}
	BN_CTX_start(ctx);
	newsig = ECDSA_SIG_new();
	if (newsig == NULL) {
		GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
		goto err;
	}
	s = newsig->s;
	r = newsig->r;
	group = GOST_KEY_get0_group(eckey);
	if ((order = BN_CTX_get(ctx)) == NULL)
		goto err;
	if (EC_GROUP_get_order(group, order, ctx) == 0)
		goto err;
	priv_key = GOST_KEY_get0_private_key(eckey);
	if ((e = BN_CTX_get(ctx)) == NULL)
		goto err;
	if (BN_mod(e, md, order, ctx) == 0)
		goto err;
	if (BN_is_zero(e))
		BN_one(e);
	if ((k = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((X = BN_CTX_get(ctx)) == NULL)
		goto err;
	if ((C = EC_POINT_new(group)) == NULL)
		goto err;
	do {
		do {
			if (!BN_rand_range(k, order)) {
				GOSTerr(GOST_F_GOST2001_DO_SIGN,
					GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
				goto err;
			}
			/*
			 * We do not want timing information to leak the length
			 * of k, so we compute G*k using an equivalent scalar
			 * of fixed bit-length.
			 */
			if (BN_add(k, k, order) == 0)
				goto err;
			if (BN_num_bits(k) <= BN_num_bits(order))
				if (BN_add(k, k, order) == 0)
					goto err;

			if (EC_POINT_mul(group, C, k, NULL, NULL, ctx) == 0) {
				GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_EC_LIB);
				goto err;
			}
			if (EC_POINT_get_affine_coordinates_GFp(group, C, X,
			    NULL, ctx) == 0) {
				GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_EC_LIB);
				goto err;
			}
			if (BN_nnmod(r, X, order, ctx) == 0)
				goto err;
		} while (BN_is_zero(r));
		/* s = (r*priv_key+k*e) mod order */
		if (tmp == NULL) {
			if ((tmp = BN_CTX_get(ctx)) == NULL)
				goto err;
		}
		if (BN_mod_mul(tmp, priv_key, r, order, ctx) == 0)
			goto err;
		if (tmp2 == NULL) {
			if ((tmp2 = BN_CTX_get(ctx)) == NULL)
				goto err;
		}
		if (BN_mod_mul(tmp2, k, e, order, ctx) == 0)
			goto err;
		if (BN_mod_add(s, tmp, tmp2, order, ctx) == 0)
			goto err;
	} while (BN_is_zero(s));
	ok = 1;

err:
	EC_POINT_free(C);
	if (ctx != NULL) {
		BN_CTX_end(ctx);
		BN_CTX_free(ctx);
	}
	if (ok == 0) {
		ECDSA_SIG_free(newsig);
		newsig = NULL;
	}
	return newsig;
}