X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=gost_ameth.c;h=199aef271207bc2eb55ad8d6b4a54ab8e62fce48;hb=20f99cd4df48ed150937a82cc57f233cadcc7c7e;hp=24984c71eda01004efeaaebdc37e136a461e3c29;hpb=cba16944bff9d8c5dcf37be641822cd3de6d2ec1;p=openssl-gost%2Fengine.git diff --git a/gost_ameth.c b/gost_ameth.c index 24984c7..199aef2 100644 --- a/gost_ameth.c +++ b/gost_ameth.c @@ -16,24 +16,40 @@ #ifndef OPENSSL_NO_CMS # include #endif -#include "gost_params.h" #include "gost_lcl.h" #include "e_gost_err.h" -static int pkey_bits_gost(const EVP_PKEY *pk); +#define PK_WRAP_PARAM "LEGACY_PK_WRAP" -int gost94_nid_by_params(DSA *p) +/* + * Pack bignum into byte buffer of given size, filling all leading bytes by + * zeros + */ +int store_bignum(const BIGNUM *bn, unsigned char *buf, int len) { - R3410_params *gost_params; - BIGNUM *q = BN_new(); - for (gost_params = R3410_paramset; gost_params->nid != NID_undef; - gost_params++) { - BN_dec2bn(&q, gost_params->q); - if (!BN_cmp(q, p->q)) - break; + int bytes = BN_num_bytes(bn); + + if (bytes > len) + return 0; + memset(buf, 0, len); + BN_bn2bin(bn, buf + len - bytes); + return 1; +} + +static int pkey_bits_gost(const EVP_PKEY *pk) +{ + if (!pk) + return -1; + + switch (EVP_PKEY_base_id(pk)) { + case NID_id_GostR3410_2001: + case NID_id_GostR3410_2012_256: + return 256; + case NID_id_GostR3410_2012_512: + return 512; } - BN_free(q); - return gost_params->nid; + + return -1; } static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key) @@ -51,20 +67,23 @@ static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key) 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); break; case NID_id_GostR3410_2001: pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr)); gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet); break; - case NID_id_GostR3410_94: - pkey_param_nid = (int)gost94_nid_by_params(key_ptr); - gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet); - break; } if (pkey_param_nid == NID_undef) { @@ -110,16 +129,6 @@ static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid) } } return fill_GOST_EC_params(key_ptr, param_nid); - - case NID_id_GostR3410_94: - if (!key_ptr) { - key_ptr = DSA_new(); - if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) { - DSA_free(key_ptr); - break; - } - } - return fill_GOST94_params(key_ptr, param_nid); } return 0; @@ -129,9 +138,10 @@ static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid) * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting * NID and parameters */ -static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg) +static int decode_gost_algor_params(EVP_PKEY *pkey, + const X509_ALGOR *palg) { - ASN1_OBJECT *palg_obj = NULL; + const ASN1_OBJECT *palg_obj = NULL; int ptype = V_ASN1_UNDEF; int pkey_nid = NID_undef, param_nid = NID_undef; ASN1_STRING *pval = NULL; @@ -140,7 +150,7 @@ static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg) if (!pkey || !palg) return 0; - X509_ALGOR_get0(&palg_obj, &ptype, (void **)&pval, palg); + X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg); if (ptype != V_ASN1_SEQUENCE) { GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, GOST_R_BAD_KEY_PARAMETERS_FORMAT); @@ -161,25 +171,12 @@ static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg) GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR); return 0; } - return gost_decode_nid_params(pkey, pkey_nid, param_nid); } static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv) { switch (EVP_PKEY_base_id(pkey)) { - case NID_id_GostR3410_94: - { - DSA *dsa = EVP_PKEY_get0(pkey); - if (!dsa) { - dsa = DSA_new(); - EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), dsa); - } - dsa->priv_key = BN_dup(priv); - if (!EVP_PKEY_missing_parameters(pkey)) - gost94_compute_public(dsa); - break; - } case NID_id_GostR3410_2012_512: case NID_id_GostR3410_2012_256: case NID_id_GostR3410_2001: @@ -192,7 +189,7 @@ static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv) 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: @@ -204,13 +201,6 @@ static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv) BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey) { switch (EVP_PKEY_base_id(pkey)) { - case NID_id_GostR3410_94: - { - DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey); - if (dsa) - return dsa->priv_key; - break; - } case NID_id_GostR3410_2012_512: case NID_id_GostR3410_2012_256: case NID_id_GostR3410_2001: @@ -273,7 +263,7 @@ static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2) return -1; } PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg1); - X509_ALGOR_set0(alg1, OBJ_nid2obj(pkey->type), + X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)), V_ASN1_SEQUENCE, params); } return 1; @@ -286,8 +276,8 @@ static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2) } CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL, NULL, &alg1); - X509_ALGOR_set0(alg1, OBJ_nid2obj(pkey->type), V_ASN1_SEQUENCE, - params); + X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)), + V_ASN1_SEQUENCE, params); } return 1; #endif @@ -300,25 +290,15 @@ static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2) } /* --------------------- free functions * ------------------------------*/ -static void pkey_free_gost94(EVP_PKEY *key) -{ - if (key->pkey.dsa) { - DSA_free(key->pkey.dsa); - } -} - static void pkey_free_gost_ec(EVP_PKEY *key) { - if (key->pkey.ec) { - EC_KEY_free(key->pkey.ec); - } + EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key)); } /* ------------------ 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; @@ -358,14 +338,15 @@ static BIGNUM *unmask_priv_key(EVP_PKEY *pk, return pknum_masked; } -static int priv_decode_gost(EVP_PKEY *pk, 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; BIGNUM *pk_num = NULL; int ret = 0; - X509_ALGOR *palg = NULL; - ASN1_OBJECT *palg_obj = NULL; + const X509_ALGOR *palg = NULL; + const ASN1_OBJECT *palg_obj = NULL; ASN1_INTEGER *priv_key = NULL; int expected_key_len = 32; @@ -444,10 +425,11 @@ 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); - unsigned char *priv_buf = NULL, *buf = NULL; - int key_len = pkey_bits_gost(pk), priv_len = 0, i = 0; + 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); - ASN1_STRING *octet = NULL; if (!params) { return 0; } @@ -469,15 +451,23 @@ static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk) buf[key_len - 1 - i] = tmp; } - octet = ASN1_STRING_new(); - M_ASN1_OCTET_STRING_set(octet, buf, key_len); + if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) { + ASN1_STRING *octet = NULL; + int priv_len = 0; + unsigned char *priv_buf = NULL; + + octet = ASN1_STRING_new(); + ASN1_OCTET_STRING_set(octet, buf, key_len); + priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf); + ASN1_STRING_free(octet); + OPENSSL_free(buf); - priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf); - ASN1_STRING_free(octet); - OPENSSL_free(buf); + return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params, + priv_buf, priv_len); + } return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params, - priv_buf, priv_len); + buf, key_len); } /* --------- printing keys --------------------------------*/ @@ -521,7 +511,7 @@ static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent) 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; } @@ -563,49 +553,6 @@ static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent) return 1; } -static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *pctx, int type) -{ - int param_nid = NID_undef; - - if (type == 2) { - if (print_gost_priv(out, pkey, indent) == 0) - return 0; - } - if (type >= 1) { - BIGNUM *pubkey; - - pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key; - BIO_indent(out, indent, 128); - BIO_printf(out, "Public key: "); - BN_print(out, pubkey); - BIO_printf(out, "\n"); - } - - param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey)); - BIO_indent(out, indent, 128); - BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid)); - return 1; -} - -static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *pctx) -{ - return print_gost_94(out, pkey, indent, pctx, 0); -} - -static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *pctx) -{ - return print_gost_94(out, pkey, indent, pctx, 1); -} - -static int priv_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *pctx) -{ - return print_gost_94(out, pkey, indent, pctx, 2); -} - static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx, int type) { @@ -640,16 +587,6 @@ static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent, } /* ---------------------------------------------------------------------*/ -static int param_missing_gost94(const EVP_PKEY *pk) -{ - const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk); - if (!dsa) - return 1; - if (!dsa->q) - return 1; - return 0; -} - static int param_missing_gost_ec(const EVP_PKEY *pk) { const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk); @@ -660,32 +597,6 @@ static int param_missing_gost_ec(const EVP_PKEY *pk) return 0; } -static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from) -{ - const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from); - DSA *dto = EVP_PKEY_get0(to); - if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) { - GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_INCOMPATIBLE_ALGORITHMS); - return 0; - } - if (!dfrom) { - GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_KEY_PARAMETERS_MISSING); - return 0; - } - if (!dto) { - dto = DSA_new(); - EVP_PKEY_assign(to, EVP_PKEY_base_id(from), dto); - } -#define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x); - COPYBIGNUM(dto, dfrom, p) - COPYBIGNUM(dto, dfrom, q) - COPYBIGNUM(dto, dfrom, g) - - if (dto->priv_key) - gost94_compute_public(dto); - return 1; -} - static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from) { EC_KEY *eto = EVP_PKEY_get0(to); @@ -720,15 +631,6 @@ static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from) return 1; } -static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b) -{ - const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a); - const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b); - if (!da || !db) - return 0; - return (BN_cmp(da->q, db->q) == 0) ? 1 : 0; -} - static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b) { const EC_GROUP *group_a, *group_b; @@ -749,96 +651,17 @@ static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b) } /* ---------- Public key functions * --------------------------------------*/ -static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub) -{ - X509_ALGOR *palg = NULL; - const unsigned char *pubkey_buf = NULL; - unsigned char *databuf; - ASN1_OBJECT *palgobj = NULL; - int pub_len, i, j; - DSA *dsa; - ASN1_OCTET_STRING *octet = NULL; - - if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub)) - return 0; - EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL); - if (!decode_gost_algor_params(pk, palg)) - return 0; - octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len); - if (!octet) { - GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE); - return 0; - } - databuf = OPENSSL_malloc(octet->length); - if (databuf == NULL) { - GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE); - ASN1_OCTET_STRING_free(octet); - return 0; - } - for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) { - databuf[j] = octet->data[i]; - } - dsa = EVP_PKEY_get0(pk); - dsa->pub_key = BN_bin2bn(databuf, octet->length, NULL); - ASN1_OCTET_STRING_free(octet); - OPENSSL_free(databuf); - return 1; - -} - -static int pub_encode_gost94(X509_PUBKEY *pub, const EVP_PKEY *pk) -{ - ASN1_OBJECT *algobj = NULL; - ASN1_OCTET_STRING *octet = NULL; - void *pval = NULL; - unsigned char *buf = NULL, *databuf, *sptr; - int i, j, data_len, ret = 0; - - int ptype = V_ASN1_UNDEF; - DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk); - algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk)); - if (pk->save_parameters) { - ASN1_STRING *params = encode_gost_algor_params(pk); - pval = params; - ptype = V_ASN1_SEQUENCE; - } - data_len = BN_num_bytes(dsa->pub_key); - databuf = OPENSSL_malloc(data_len); - if (databuf == NULL) { - GOSTerr(GOST_F_PUB_ENCODE_GOST94, ERR_R_MALLOC_FAILURE); - return 0; - } - BN_bn2bin(dsa->pub_key, databuf); - octet = ASN1_OCTET_STRING_new(); - if (octet == NULL) { - GOSTerr(GOST_F_PUB_ENCODE_GOST94, ERR_R_MALLOC_FAILURE); - OPENSSL_free(databuf); - return 0; - } - ASN1_STRING_set(octet, NULL, data_len); - sptr = ASN1_STRING_data(octet); - for (i = 0, j = data_len - 1; i < data_len; i++, j--) { - sptr[i] = databuf[j]; - } - OPENSSL_free(databuf); - 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); -} - static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub) { X509_ALGOR *palg = NULL; const unsigned char *pubkey_buf = NULL; unsigned char *databuf; ASN1_OBJECT *palgobj = NULL; - int pub_len, i, j; + int pub_len; EC_POINT *pub_key; BIGNUM *X, *Y; ASN1_OCTET_STRING *octet = NULL; - int len; + size_t len; const EC_GROUP *group; if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub)) @@ -858,17 +681,16 @@ static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub) ASN1_OCTET_STRING_free(octet); return 0; } - for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) { - databuf[j] = octet->data[i]; - } + + BUF_reverse(databuf, octet->data, octet->length); len = octet->length / 2; ASN1_OCTET_STRING_free(octet); - Y = getbnfrombuf(databuf, len); - X = getbnfrombuf(databuf + len, len); + Y = BN_bin2bn(databuf, len, NULL); + 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); @@ -892,19 +714,20 @@ static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk) ASN1_OBJECT *algobj = NULL; ASN1_OCTET_STRING *octet = NULL; void *pval = NULL; - unsigned char *buf = NULL, *databuf = NULL, *sptr; - int i, j, data_len, ret = -1; + unsigned char *buf = NULL, *databuf = NULL; + int data_len, ret = -1; const EC_POINT *pub_key; BIGNUM *X = NULL, *Y = NULL, *order = NULL; const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk); int ptype = V_ASN1_UNDEF; + ASN1_STRING *params; algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk)); - if (pk->save_parameters) { - ASN1_STRING *params = encode_gost_algor_params(pk); - pval = params; - ptype = V_ASN1_SEQUENCE; - } + + params = encode_gost_algor_params(pk); + pval = params; + ptype = V_ASN1_SEQUENCE; + order = BN_new(); if (!order) { GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE); @@ -922,36 +745,37 @@ static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk) 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; } data_len = 2 * BN_num_bytes(order); - databuf = OPENSSL_malloc(data_len); + databuf = OPENSSL_zalloc(data_len); if (databuf == NULL) { GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE); goto err; } - memset(databuf, 0, data_len); store_bignum(X, databuf + data_len / 2, data_len / 2); store_bignum(Y, databuf, data_len / 2); + BUF_reverse(databuf, NULL, data_len); + octet = ASN1_OCTET_STRING_new(); if (octet == NULL) { GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE); goto err; } - ASN1_STRING_set(octet, NULL, data_len); - sptr = ASN1_STRING_data(octet); - for (i = 0, j = data_len - 1; i < data_len; i++, j--) { - sptr[i] = databuf[j]; + + if (0 == ASN1_STRING_set(octet, databuf, data_len)) { + GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE); + goto err; } ret = i2d_ASN1_OCTET_STRING(octet, &buf); + err: ASN1_BIT_STRING_free(octet); -err: if (X) BN_free(X); if (Y) @@ -966,19 +790,6 @@ err: return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret); } -static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b) -{ - const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a); - const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b); - if (!da || !db) - return 0; - - if (!da->pub_key || !db->pub_key) - return 0; - - return (BN_cmp(da->pub_key, db->pub_key) == 0) ? 1 : 0; -} - static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b) { const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a); @@ -1010,29 +821,10 @@ static int pkey_size_gost(const EVP_PKEY *pk) return -1; } -static int pkey_bits_gost(const EVP_PKEY *pk) -{ - if (!pk) - return -1; - - switch (EVP_PKEY_base_id(pk)) { - case NID_id_GostR3410_94: - case NID_id_GostR3410_2001: - case NID_id_GostR3410_2012_256: - return 256; - case NID_id_GostR3410_2012_512: - return 512; - } - - return -1; -} - /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/ static void mackey_free_gost(EVP_PKEY *pk) { - if (pk->pkey.ptr) { - OPENSSL_free(pk->pkey.ptr); - } + OPENSSL_free(EVP_PKEY_get0(pk)); } static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2) @@ -1059,10 +851,28 @@ static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2) return -2; } -static int gost94_param_encode(const EVP_PKEY *pkey, unsigned char **pder) +static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2) { - int nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey)); - return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder); + 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) @@ -1073,20 +883,6 @@ static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder) return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder); } -static int gost94_param_decode(EVP_PKEY *pkey, const unsigned char **pder, - int derlen) -{ - ASN1_OBJECT *obj = NULL; - int nid; - if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) { - return 0; - } - nid = OBJ_obj2nid(obj); - ASN1_OBJECT_free(obj); - - return gost_decode_nid_params(pkey, NID_id_GostR3410_94, nid); -} - static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) { @@ -1109,23 +905,6 @@ int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth, if (!*ameth) return 0; switch (nid) { - case NID_id_GostR3410_94: - EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost94); - EVP_PKEY_asn1_set_private(*ameth, - priv_decode_gost, priv_encode_gost, - priv_print_gost94); - - EVP_PKEY_asn1_set_param(*ameth, - gost94_param_decode, gost94_param_encode, - param_missing_gost94, param_copy_gost94, - param_cmp_gost94, param_print_gost94); - EVP_PKEY_asn1_set_public(*ameth, - pub_decode_gost94, pub_encode_gost94, - pub_cmp_gost94, pub_print_gost94, - pkey_size_gost, pkey_bits_gost); - - EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost); - break; case NID_id_GostR3410_2001: EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec); EVP_PKEY_asn1_set_private(*ameth, @@ -1142,6 +921,7 @@ int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth, pkey_size_gost, pkey_bits_gost); EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost); + EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost); break; case NID_id_GostR3410_2012_256: case NID_id_GostR3410_2012_512: @@ -1161,6 +941,7 @@ int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth, pkey_size_gost, pkey_bits_gost); EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost); + EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost); break; case NID_id_Gost28147_89_MAC: EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost); @@ -1170,6 +951,14 @@ int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth, EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost); EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12); break; + case NID_magma_mac: + EVP_PKEY_asn1_set_free(*ameth, mackey_free_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_grasshopper); + break; } return 1; }