switch (EVP_PKEY_base_id(key)) {
case NID_id_GostR3410_2012_256:
pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
- gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_256);
+ switch (pkey_param_nid) {
+ case NID_id_GostR3410_2001_TestParamSet:
+ case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
+ case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
+ case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
+ case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
+ case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
+ gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_256);
+ }
break;
case NID_id_GostR3410_2012_512:
pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
- gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_512);
+ switch (pkey_param_nid) {
+ case NID_id_tc26_gost_3410_2012_512_paramSetTest:
+ case NID_id_tc26_gost_3410_2012_512_paramSetA:
+ case NID_id_tc26_gost_3410_2012_512_paramSetB:
+ gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_512);
+ }
break;
case NID_id_GostR3410_2001:
pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
* Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
* NID and parameters
*/
-static int decode_gost_algor_params(EVP_PKEY *pkey, const X509_ALGOR *palg)
+static int decode_gost_algor_params(EVP_PKEY *pkey,
+ const X509_ALGOR *palg)
{
const ASN1_OBJECT *palg_obj = NULL;
int ptype = V_ASN1_UNDEF;
if (!EC_KEY_set_private_key(ec, priv))
return 0;
if (!EVP_PKEY_missing_parameters(pkey))
- gost_ec_compute_public(ec);
+ return gost_ec_compute_public(ec);
break;
}
default:
/* ------------------ private key functions -----------------------------*/
static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
- const unsigned char *buf, int len,
- int num_masks)
+ const unsigned char *buf, int len, int num_masks)
{
BIGNUM *pknum_masked = NULL, *q = NULL;
const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
- pknum_masked = hashsum2bn(buf, len);
+ pknum_masked = BN_lebin2bn(buf, len, BN_secure_new());
if (!pknum_masked)
return NULL;
}
for (; p != buf; p -= len) {
- BIGNUM *mask = hashsum2bn(p, len);
- BN_CTX *ctx = BN_CTX_new();
+ BIGNUM *mask = BN_lebin2bn(p, len, BN_secure_new());
+ BN_CTX *ctx = BN_CTX_secure_new();
BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
return pknum_masked;
}
-static int priv_decode_gost(EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf)
+static int priv_decode_gost(EVP_PKEY *pk,
+ const PKCS8_PRIV_KEY_INFO *p8inf)
{
const unsigned char *pkey_buf = NULL, *p = NULL;
int priv_len = 0;
const X509_ALGOR *palg = NULL;
const ASN1_OBJECT *palg_obj = NULL;
ASN1_INTEGER *priv_key = NULL;
- int expected_key_len = 32;
+ int expected_key_len;
if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
return 0;
GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
return 0;
}
- pk_num = hashsum2bn(s->data, s->length);
+ pk_num = BN_lebin2bn(s->data, s->length, BN_secure_new());
ASN1_STRING_free(s);
} else if (V_ASN1_INTEGER == *p) {
priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
return 0;
}
- pk_num = ASN1_INTEGER_to_BN(priv_key, NULL);
+ pk_num = ASN1_INTEGER_to_BN(priv_key, BN_secure_new());
ASN1_INTEGER_free(priv_key);
} else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
- MASKED_GOST_KEY *mgk = NULL;
- mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
+ MASKED_GOST_KEY *mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
if (!mgk) {
GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
{
ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
- ASN1_STRING *params = encode_gost_algor_params(pk);
+ ASN1_STRING *params = NULL;
unsigned char *buf = NULL;
int key_len = pkey_bits_gost(pk), i = 0;
/* unmasked private key */
const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
- if (!params) {
+ key_len = (key_len < 0) ? 0 : key_len / 8;
+ if (key_len == 0 || !(buf = OPENSSL_secure_malloc(key_len))) {
return 0;
}
- key_len = (key_len < 0) ? 0 : key_len / 8;
- if (key_len == 0 || !(buf = OPENSSL_malloc(key_len))) {
+ if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
+ OPENSSL_secure_free(buf);
return 0;
}
- if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
- OPENSSL_free(buf);
+ params = encode_gost_algor_params(pk);
+ if (!params) {
+ OPENSSL_secure_free(buf);
return 0;
}
buf[key_len - 1 - i] = tmp;
}
- if(pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
- ASN1_STRING *octet = NULL;
+ if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
+ ASN1_STRING *octet = ASN1_STRING_new();
int priv_len = 0;
unsigned char *priv_buf = NULL;
-
- octet = ASN1_STRING_new();
- ASN1_OCTET_STRING_set(octet, buf, key_len);
+ if (!octet || !ASN1_OCTET_STRING_set(octet, buf, key_len)) {
+ ASN1_STRING_free(octet);
+ ASN1_STRING_free(params);
+ OPENSSL_secure_free(buf);
+ return 0;
+ }
priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
ASN1_STRING_free(octet);
- OPENSSL_free(buf);
+ OPENSSL_secure_free(buf);
return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
- priv_buf, priv_len);
+ priv_buf, priv_len);
}
return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
if (!pubkey || !group)
goto err;
- if (!EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y, ctx)) {
+ if (!EC_POINT_get_affine_coordinates(group, pubkey, X, Y, ctx)) {
GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
goto err;
}
return 0;
}
- BUF_reverse(databuf, octet->data, octet->length);
+ BUF_reverse(databuf, octet->data, octet->length);
len = octet->length / 2;
ASN1_OCTET_STRING_free(octet);
X = BN_bin2bn(databuf + len, len, NULL);
OPENSSL_free(databuf);
pub_key = EC_POINT_new(group);
- if (!EC_POINT_set_affine_coordinates_GFp(group, pub_key, X, Y, NULL)) {
+ if (!EC_POINT_set_affine_coordinates(group, pub_key, X, Y, NULL)) {
GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
EC_POINT_free(pub_key);
BN_free(X);
static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
{
- ASN1_OBJECT *algobj = NULL;
+ ASN1_OBJECT *algobj;
ASN1_OCTET_STRING *octet = NULL;
- void *pval = NULL;
+ void *pval;
unsigned char *buf = NULL, *databuf = NULL;
int data_len, ret = -1;
const EC_POINT *pub_key;
- BIGNUM *X = NULL, *Y = NULL, *order = NULL;
+ BIGNUM *X = NULL, *Y = NULL, *order;
const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
- int ptype = V_ASN1_UNDEF;
+ int ptype = V_ASN1_SEQUENCE;
+ ASN1_STRING *params;
algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
- ASN1_STRING *params = encode_gost_algor_params(pk);
- pval = params;
- ptype = V_ASN1_SEQUENCE;
+ params = encode_gost_algor_params(pk);
+ pval = params;
order = BN_new();
if (!order) {
GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
goto err;
}
- if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
+ if (!EC_POINT_get_affine_coordinates(EC_KEY_get0_group(ec),
pub_key, X, Y, NULL)) {
GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
goto err;
store_bignum(X, databuf + data_len / 2, data_len / 2);
store_bignum(Y, databuf, data_len / 2);
- BUF_reverse(databuf, NULL, data_len);
+ BUF_reverse(databuf, NULL, data_len);
octet = ASN1_OCTET_STRING_new();
if (octet == NULL) {
}
ret = i2d_ASN1_OCTET_STRING(octet, &buf);
- ASN1_BIT_STRING_free(octet);
err:
+ ASN1_BIT_STRING_free(octet);
if (X)
BN_free(X);
if (Y)
return -2;
}
+static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2)
+{
+ switch (op) {
+ case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
+ if (arg2) {
+ *(int *)arg2 = NID_magma_mac;
+ return 2;
+ }
+ }
+ return -2;
+}
+
+static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2)
+{
+ switch (op) {
+ case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
+ if (arg2) {
+ *(int *)arg2 = NID_grasshopper_mac;
+ return 2;
+ }
+ }
+ return -2;
+}
+
static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
{
int nid =
pkey_size_gost, pkey_bits_gost);
EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
-#endif
break;
case NID_id_GostR3410_2012_256:
case NID_id_GostR3410_2012_512:
pkey_size_gost, pkey_bits_gost);
EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
-#endif
break;
case NID_id_Gost28147_89_MAC:
EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
break;
-/* TODO
case NID_magma_mac:
EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
- EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
+ EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma);
break;
case NID_grasshopper_mac:
EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
- EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
+ EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper);
break;
-*/
}
return 1;
}