1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implementation of RFC 4490/4491 ASN1 method *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/asn1.h>
16 #ifndef OPENSSL_NO_CMS
17 # include <openssl/cms.h>
20 #include "e_gost_err.h"
22 #define PK_WRAP_PARAM "LEGACY_PK_WRAP"
25 * Pack bignum into byte buffer of given size, filling all leading bytes by
28 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len)
30 int bytes = BN_num_bytes(bn);
35 BN_bn2bin(bn, buf + len - bytes);
39 static int pkey_bits_gost(const EVP_PKEY *pk)
44 switch (EVP_PKEY_base_id(pk)) {
45 case NID_id_GostR3410_2001:
46 case NID_id_GostR3410_2001DH:
47 case NID_id_GostR3410_2012_256:
49 case NID_id_GostR3410_2012_512:
56 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
58 ASN1_STRING *params = ASN1_STRING_new();
59 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
60 int pkey_param_nid = NID_undef;
61 void *key_ptr = EVP_PKEY_get0((EVP_PKEY *)key);
64 if (!params || !gkp) {
65 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
68 switch (EVP_PKEY_base_id(key)) {
69 case NID_id_GostR3410_2012_256:
70 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
71 switch (pkey_param_nid) {
72 case NID_id_GostR3410_2001_TestParamSet:
73 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
74 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
75 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
76 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
77 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
78 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_256);
81 case NID_id_GostR3410_2012_512:
82 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
83 switch (pkey_param_nid) {
84 case NID_id_tc26_gost_3410_2012_512_paramSetTest:
85 case NID_id_tc26_gost_3410_2012_512_paramSetA:
86 case NID_id_tc26_gost_3410_2012_512_paramSetB:
87 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_512);
90 case NID_id_GostR3410_2001:
91 case NID_id_GostR3410_2001DH:
92 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
93 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
97 if (pkey_param_nid == NID_undef) {
98 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, GOST_R_INVALID_PARAMSET);
102 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
104 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
106 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
107 if (params->length <= 0) {
108 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
111 params->type = V_ASN1_SEQUENCE;
115 GOST_KEY_PARAMS_free(gkp);
116 if (result == 0) { /* if error */
118 ASN1_STRING_free(params);
124 static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid)
126 void *key_ptr = EVP_PKEY_get0(pkey);
129 case NID_id_GostR3410_2012_256:
130 case NID_id_GostR3410_2012_512:
131 case NID_id_GostR3410_2001:
132 case NID_id_GostR3410_2001DH:
134 key_ptr = EC_KEY_new();
135 if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) {
136 EC_KEY_free(key_ptr);
140 return fill_GOST_EC_params(key_ptr, param_nid);
147 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
150 static int decode_gost_algor_params(EVP_PKEY *pkey,
151 const X509_ALGOR *palg)
153 const ASN1_OBJECT *palg_obj = NULL;
154 int ptype = V_ASN1_UNDEF;
155 int pkey_nid = NID_undef, param_nid = NID_undef;
156 ASN1_STRING *pval = NULL;
157 const unsigned char *p;
158 GOST_KEY_PARAMS *gkp = NULL;
162 X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg);
163 if (ptype != V_ASN1_SEQUENCE) {
164 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
165 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
169 pkey_nid = OBJ_obj2nid(palg_obj);
171 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
173 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
174 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
177 param_nid = OBJ_obj2nid(gkp->key_params);
178 GOST_KEY_PARAMS_free(gkp);
179 if (!EVP_PKEY_set_type(pkey, pkey_nid)) {
180 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
183 return gost_decode_nid_params(pkey, pkey_nid, param_nid);
186 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
188 switch (EVP_PKEY_base_id(pkey)) {
189 case NID_id_GostR3410_2012_512:
190 case NID_id_GostR3410_2012_256:
191 case NID_id_GostR3410_2001:
192 case NID_id_GostR3410_2001DH:
194 EC_KEY *ec = EVP_PKEY_get0(pkey);
197 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
199 if (!EC_KEY_set_private_key(ec, priv))
201 if (!EVP_PKEY_missing_parameters(pkey))
202 return gost_ec_compute_public(ec);
211 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
213 switch (EVP_PKEY_base_id(pkey)) {
214 case NID_id_GostR3410_2012_512:
215 case NID_id_GostR3410_2012_256:
216 case NID_id_GostR3410_2001:
217 case NID_id_GostR3410_2001DH:
219 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
221 return (BIGNUM *)EC_KEY_get0_private_key(ec);
231 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
233 int nid = EVP_PKEY_base_id(pkey), md_nid = NID_undef;
234 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
237 case NID_id_GostR3410_2012_512:
238 md_nid = NID_id_GostR3411_2012_512;
240 case NID_id_GostR3410_2012_256:
241 md_nid = NID_id_GostR3411_2012_256;
243 case NID_id_GostR3410_2001:
244 case NID_id_GostR3410_2001DH:
245 case NID_id_GostR3410_94:
246 md_nid = NID_id_GostR3411_94;
253 case ASN1_PKEY_CTRL_PKCS7_SIGN:
255 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2, NULL,
257 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
258 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
261 #ifndef OPENSSL_NO_CMS
262 case ASN1_PKEY_CTRL_CMS_SIGN:
264 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2, NULL, NULL,
266 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
267 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
271 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
273 ASN1_STRING *params = encode_gost_algor_params(pkey);
277 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg1);
278 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
279 V_ASN1_SEQUENCE, params);
282 #ifndef OPENSSL_NO_CMS
283 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
285 ASN1_STRING *params = encode_gost_algor_params(pkey);
289 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
291 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
292 V_ASN1_SEQUENCE, params);
296 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
297 *(int *)arg2 = md_nid;
304 /* --------------------- free functions * ------------------------------*/
305 static void pkey_free_gost_ec(EVP_PKEY *key)
307 EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key));
310 /* ------------------ private key functions -----------------------------*/
312 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
313 const unsigned char *buf, int len, int num_masks)
315 BIGNUM *pknum_masked = NULL, *q = NULL;
316 const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
317 const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
319 pknum_masked = BN_lebin2bn(buf, len, BN_secure_new());
325 * XXX Remove sign by gost94
327 const unsigned char *p = buf + num_masks * len;
330 if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
331 BN_free(pknum_masked);
336 for (; p != buf; p -= len) {
337 BIGNUM *mask = BN_lebin2bn(p, len, BN_secure_new());
338 BN_CTX *ctx = BN_CTX_secure_new();
340 BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
353 static int priv_decode_gost(EVP_PKEY *pk,
354 const PKCS8_PRIV_KEY_INFO *p8inf)
356 const unsigned char *pkey_buf = NULL, *p = NULL;
358 BIGNUM *pk_num = NULL;
360 const X509_ALGOR *palg = NULL;
361 const ASN1_OBJECT *palg_obj = NULL;
362 ASN1_INTEGER *priv_key = NULL;
363 int expected_key_len;
365 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
368 if (!decode_gost_algor_params(pk, palg)) {
372 expected_key_len = pkey_bits_gost(pk) > 0 ? pkey_bits_gost(pk) / 8 : 0;
373 if (expected_key_len == 0) {
374 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
378 if (priv_len % expected_key_len == 0) {
379 /* Key is not wrapped but masked */
380 pk_num = unmask_priv_key(pk, pkey_buf, expected_key_len,
381 priv_len / expected_key_len - 1);
382 } else if (V_ASN1_OCTET_STRING == *p) {
383 /* New format - Little endian octet string */
384 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
385 if (!s || ((s->length != 32) && (s->length != 64))) {
387 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
390 pk_num = BN_lebin2bn(s->data, s->length, BN_secure_new());
392 } else if (V_ASN1_INTEGER == *p) {
393 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
395 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
398 pk_num = ASN1_INTEGER_to_BN(priv_key, BN_secure_new());
399 ASN1_INTEGER_free(priv_key);
400 } else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
401 MASKED_GOST_KEY *mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
404 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
408 priv_len = mgk->masked_priv_key->length;
409 if (priv_len % expected_key_len) {
410 MASKED_GOST_KEY_free(mgk);
411 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
415 pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
417 priv_len / expected_key_len - 1);
418 MASKED_GOST_KEY_free(mgk);
420 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
424 if (pk_num == NULL) {
425 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
429 ret = gost_set_priv_key(pk, pk_num);
434 /* ----------------------------------------------------------------------*/
435 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
437 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
438 ASN1_STRING *params = NULL;
439 unsigned char *buf = NULL;
440 int key_len = pkey_bits_gost(pk), i = 0;
441 /* unmasked private key */
442 const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
444 key_len = (key_len < 0) ? 0 : key_len / 8;
445 if (key_len == 0 || !(buf = OPENSSL_secure_malloc(key_len))) {
449 if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
450 OPENSSL_secure_free(buf);
454 params = encode_gost_algor_params(pk);
456 OPENSSL_secure_free(buf);
460 /* Convert buf to Little-endian */
461 for (i = 0; i < key_len / 2; i++) {
462 unsigned char tmp = buf[i];
463 buf[i] = buf[key_len - 1 - i];
464 buf[key_len - 1 - i] = tmp;
467 if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
468 ASN1_STRING *octet = ASN1_STRING_new();
470 unsigned char *priv_buf = NULL;
471 if (!octet || !ASN1_OCTET_STRING_set(octet, buf, key_len)) {
472 ASN1_STRING_free(octet);
473 ASN1_STRING_free(params);
474 OPENSSL_secure_free(buf);
477 priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
478 ASN1_STRING_free(octet);
479 OPENSSL_secure_free(buf);
481 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
485 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
489 /* --------- printing keys --------------------------------*/
490 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
494 if (!BIO_indent(out, indent, 128))
496 BIO_printf(out, "Private key: ");
497 key = gost_get0_priv_key(pkey);
499 BIO_printf(out, "<undefined>");
502 BIO_printf(out, "\n");
507 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
511 const EC_POINT *pubkey;
512 const EC_GROUP *group;
513 EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
518 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
525 pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
526 group = (key) ? EC_KEY_get0_group(key) : NULL;
527 if (!pubkey || !group)
530 if (!EC_POINT_get_affine_coordinates(group, pubkey, X, Y, ctx)) {
531 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
534 if (!BIO_indent(out, indent, 128))
536 BIO_printf(out, "Public key:\n");
537 if (!BIO_indent(out, indent + 3, 128))
539 BIO_printf(out, "X:");
541 BIO_printf(out, "\n");
542 if (!BIO_indent(out, indent + 3, 128))
544 BIO_printf(out, "Y:");
546 BIO_printf(out, "\n");
555 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
557 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
558 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
564 param_nid = EC_GROUP_get_curve_name(group);
565 if (!BIO_indent(out, indent, 128))
567 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
572 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
573 ASN1_PCTX *pctx, int type)
576 if (print_gost_priv(out, pkey, indent) == 0)
580 if (print_gost_ec_pub(out, pkey, indent) == 0)
584 return print_gost_ec_param(out, pkey, indent);
587 static int param_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
590 return print_gost_ec(out, pkey, indent, pctx, 0);
593 static int pub_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
596 return print_gost_ec(out, pkey, indent, pctx, 1);
599 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
602 return print_gost_ec(out, pkey, indent, pctx, 2);
605 /* ---------------------------------------------------------------------*/
606 static int param_missing_gost_ec(const EVP_PKEY *pk)
608 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
611 if (!EC_KEY_get0_group(ec))
616 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
618 EC_KEY *eto = EVP_PKEY_get0(to);
619 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
620 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
621 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_INCOMPATIBLE_ALGORITHMS);
625 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
631 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
634 if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
635 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
640 if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
641 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
644 if (EC_KEY_get0_private_key(eto)) {
645 return gost_ec_compute_public(eto);
650 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
652 const EC_GROUP *group_a, *group_b;
653 EC_KEY *ec_a = EVP_PKEY_get0((EVP_PKEY *)a);
654 EC_KEY *ec_b = EVP_PKEY_get0((EVP_PKEY *)b);
658 group_a = EC_KEY_get0_group(ec_a);
659 group_b = EC_KEY_get0_group(ec_b);
660 if (!group_a || !group_b)
663 if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
669 /* ---------- Public key functions * --------------------------------------*/
670 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub)
672 X509_ALGOR *palg = NULL;
673 const unsigned char *pubkey_buf = NULL;
674 unsigned char *databuf;
675 ASN1_OBJECT *palgobj = NULL;
679 ASN1_OCTET_STRING *octet = NULL;
681 const EC_GROUP *group;
683 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
685 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
686 if (!decode_gost_algor_params(pk, palg))
688 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
689 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
691 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
694 databuf = OPENSSL_malloc(octet->length);
695 if (databuf == NULL) {
696 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
697 ASN1_OCTET_STRING_free(octet);
701 BUF_reverse(databuf, octet->data, octet->length);
702 len = octet->length / 2;
703 ASN1_OCTET_STRING_free(octet);
705 Y = BN_bin2bn(databuf, len, NULL);
706 X = BN_bin2bn(databuf + len, len, NULL);
707 OPENSSL_free(databuf);
708 pub_key = EC_POINT_new(group);
709 if (!EC_POINT_set_affine_coordinates(group, pub_key, X, Y, NULL)) {
710 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
711 EC_POINT_free(pub_key);
718 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
719 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
720 EC_POINT_free(pub_key);
723 EC_POINT_free(pub_key);
728 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
731 ASN1_OCTET_STRING *octet = NULL;
733 unsigned char *buf = NULL, *databuf = NULL;
734 int data_len, ret = -1;
735 const EC_POINT *pub_key;
736 BIGNUM *X = NULL, *Y = NULL, *order;
737 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
738 int ptype = V_ASN1_SEQUENCE;
741 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
743 params = encode_gost_algor_params(pk);
748 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
751 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
752 pub_key = EC_KEY_get0_public_key(ec);
754 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
760 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
763 if (!EC_POINT_get_affine_coordinates(EC_KEY_get0_group(ec),
764 pub_key, X, Y, NULL)) {
765 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
768 data_len = 2 * BN_num_bytes(order);
769 databuf = OPENSSL_zalloc(data_len);
770 if (databuf == NULL) {
771 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
775 store_bignum(X, databuf + data_len / 2, data_len / 2);
776 store_bignum(Y, databuf, data_len / 2);
778 BUF_reverse(databuf, NULL, data_len);
780 octet = ASN1_OCTET_STRING_new();
782 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
786 if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
787 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
791 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
793 ASN1_BIT_STRING_free(octet);
801 OPENSSL_free(databuf);
805 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
808 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
810 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
811 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
812 const EC_POINT *ka, *kb;
815 ka = EC_KEY_get0_public_key(ea);
816 kb = EC_KEY_get0_public_key(eb);
819 return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
822 static int pkey_size_gost(const EVP_PKEY *pk)
827 switch (EVP_PKEY_base_id(pk)) {
828 case NID_id_GostR3410_94:
829 case NID_id_GostR3410_2001:
830 case NID_id_GostR3410_2001DH:
831 case NID_id_GostR3410_2012_256:
833 case NID_id_GostR3410_2012_512:
840 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
841 static void mackey_free_gost(EVP_PKEY *pk)
843 OPENSSL_free(EVP_PKEY_get0(pk));
846 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
849 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
851 *(int *)arg2 = NID_id_Gost28147_89_MAC;
858 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
861 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
863 *(int *)arg2 = NID_gost_mac_12;
870 static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2)
873 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
875 *(int *)arg2 = NID_magma_mac;
882 static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2)
885 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
887 *(int *)arg2 = NID_grasshopper_mac;
894 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
897 EC_GROUP_get_curve_name(EC_KEY_get0_group
898 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
899 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
902 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
905 ASN1_OBJECT *obj = NULL;
907 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
910 nid = OBJ_obj2nid(obj);
911 ASN1_OBJECT_free(obj);
913 return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
916 /* ----------------------------------------------------------------------*/
917 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
918 const char *pemstr, const char *info)
920 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
924 case NID_id_GostR3410_2001:
925 case NID_id_GostR3410_2001DH:
926 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
927 EVP_PKEY_asn1_set_private(*ameth,
928 priv_decode_gost, priv_encode_gost,
931 EVP_PKEY_asn1_set_param(*ameth,
932 gost2001_param_decode, gost2001_param_encode,
933 param_missing_gost_ec, param_copy_gost_ec,
934 param_cmp_gost_ec, param_print_gost_ec);
935 EVP_PKEY_asn1_set_public(*ameth,
936 pub_decode_gost_ec, pub_encode_gost_ec,
937 pub_cmp_gost_ec, pub_print_gost_ec,
938 pkey_size_gost, pkey_bits_gost);
940 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
941 EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
943 case NID_id_GostR3410_2012_256:
944 case NID_id_GostR3410_2012_512:
945 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
946 EVP_PKEY_asn1_set_private(*ameth,
947 priv_decode_gost, priv_encode_gost,
950 EVP_PKEY_asn1_set_param(*ameth,
952 param_missing_gost_ec, param_copy_gost_ec,
953 param_cmp_gost_ec, NULL);
955 EVP_PKEY_asn1_set_public(*ameth,
956 pub_decode_gost_ec, pub_encode_gost_ec,
957 pub_cmp_gost_ec, pub_print_gost_ec,
958 pkey_size_gost, pkey_bits_gost);
960 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
961 EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
963 case NID_id_Gost28147_89_MAC:
964 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
965 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
967 case NID_gost_mac_12:
968 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
969 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
972 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
973 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma);
975 case NID_grasshopper_mac:
976 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
977 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper);