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"
23 * Pack bignum into byte buffer of given size, filling all leading bytes by
26 int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
28 int bytes = BN_num_bytes(bn);
33 BN_bn2bin(bn, buf + len - bytes);
37 /* Convert byte buffer to bignum, skipping leading zeros*/
38 BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len)
42 while (*buf == 0 && len > 0) {
47 return BN_bin2bn(buf, len, NULL);
53 static int pkey_bits_gost(const EVP_PKEY *pk)
58 int id= (EVP_PKEY_base_id(pk));
59 if ( id == NID_id_GostR3410_2001 ||id == NID_gost2012_256)
61 if (id == NID_gost2012_512)
67 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
69 ASN1_STRING *params = ASN1_STRING_new();
70 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
71 int pkey_param_nid = NID_undef;
72 int base_id = EVP_PKEY_base_id(key);
73 void *key_ptr = EVP_PKEY_get0((EVP_PKEY *)key);
76 if (!params || !gkp) {
77 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
80 if (base_id == NID_gost2012_256) {
81 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
82 gkp->hash_params = OBJ_nid2obj(NID_md_gost12_256);
83 } else if (base_id == NID_gost2012_512) {
84 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
85 gkp->hash_params = OBJ_nid2obj(NID_md_gost12_512);
86 } else if (base_id == NID_id_GostR3410_2001) {
87 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
88 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
91 if (pkey_param_nid == NID_undef) {
92 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, GOST_R_INVALID_PARAMSET);
96 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
98 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
100 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
101 if (params->length <= 0) {
102 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
105 params->type = V_ASN1_SEQUENCE;
109 GOST_KEY_PARAMS_free(gkp);
110 if (result == 0) { /* if error */
112 ASN1_STRING_free(params);
118 static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid)
120 void *key_ptr = EVP_PKEY_get0(pkey);
122 if (pkey_nid == NID_gost2012_256 ||
123 pkey_nid == NID_gost2012_512 ||
124 pkey_nid == NID_id_GostR3410_2001) {
126 key_ptr = EC_KEY_new();
127 if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) {
128 EC_KEY_free(key_ptr);
132 return fill_GOST_EC_params(key_ptr, param_nid);
139 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
142 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
144 ASN1_OBJECT *palg_obj = NULL;
145 int ptype = V_ASN1_UNDEF;
146 int pkey_nid = NID_undef, param_nid = NID_undef;
147 ASN1_STRING *pval = NULL;
148 const unsigned char *p;
149 GOST_KEY_PARAMS *gkp = NULL;
153 X509_ALGOR_get0(&palg_obj, &ptype, (void **)&pval, palg);
154 if (ptype != V_ASN1_SEQUENCE) {
155 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
156 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
160 pkey_nid = OBJ_obj2nid(palg_obj);
162 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
164 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
165 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
168 param_nid = OBJ_obj2nid(gkp->key_params);
169 GOST_KEY_PARAMS_free(gkp);
170 if (!EVP_PKEY_set_type(pkey, pkey_nid)) {
171 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
174 return gost_decode_nid_params(pkey, pkey_nid, param_nid);
177 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
179 int id = EVP_PKEY_base_id(pkey);
181 if (id == NID_gost2012_512 ||
182 id == NID_gost2012_256 ||
183 id == NID_id_GostR3410_2001)
185 EC_KEY *ec = EVP_PKEY_get0(pkey);
188 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
190 if (!EC_KEY_set_private_key(ec, priv))
192 if (!EVP_PKEY_missing_parameters(pkey))
193 gost_ec_compute_public(ec);
199 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
201 int id =EVP_PKEY_base_id(pkey);
203 if (id == NID_gost2012_512 ||
204 id == NID_gost2012_256||
205 id == NID_id_GostR3410_2001)
207 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
209 return (BIGNUM *)EC_KEY_get0_private_key(ec);
217 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
219 int nid = EVP_PKEY_base_id(pkey), md_nid = NID_undef;
220 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
222 if (nid == NID_gost2012_512) {
223 md_nid = NID_md_gost12_512;
224 } else if (nid == NID_gost2012_256) {
225 md_nid = NID_md_gost12_256;
226 } else if (nid == NID_id_GostR3410_2001 ||
227 nid == NID_id_GostR3410_94) {
228 md_nid = NID_id_GostR3411_94;
234 case ASN1_PKEY_CTRL_PKCS7_SIGN:
236 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2, NULL,
238 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
239 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
242 #ifndef OPENSSL_NO_CMS
243 case ASN1_PKEY_CTRL_CMS_SIGN:
245 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2, NULL, NULL,
247 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
248 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
252 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
254 ASN1_STRING *params = encode_gost_algor_params(pkey);
258 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg1);
259 X509_ALGOR_set0(alg1, OBJ_nid2obj(pkey->type),
260 V_ASN1_SEQUENCE, params);
263 #ifndef OPENSSL_NO_CMS
264 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
266 ASN1_STRING *params = encode_gost_algor_params(pkey);
270 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
272 X509_ALGOR_set0(alg1, OBJ_nid2obj(pkey->type), V_ASN1_SEQUENCE,
277 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
278 *(int *)arg2 = md_nid;
285 /* --------------------- free functions * ------------------------------*/
286 static void pkey_free_gost_ec(EVP_PKEY *key)
288 EC_KEY_free(key->pkey.ec);
291 /* ------------------ private key functions -----------------------------*/
293 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
294 const unsigned char *buf, int len,
297 BIGNUM *pknum_masked = NULL, *q = NULL;
298 const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
299 const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
301 pknum_masked = hashsum2bn(buf, len);
307 * XXX Remove sign by gost94
309 const unsigned char *p = buf + num_masks * len;
312 if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
313 BN_free(pknum_masked);
318 for (; p != buf; p -= len) {
319 BIGNUM *mask = hashsum2bn(p, len);
320 BN_CTX *ctx = BN_CTX_new();
322 BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
335 static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
337 const unsigned char *pkey_buf = NULL, *p = NULL;
339 BIGNUM *pk_num = NULL;
341 X509_ALGOR *palg = NULL;
342 ASN1_OBJECT *palg_obj = NULL;
343 ASN1_INTEGER *priv_key = NULL;
344 int expected_key_len = 32;
346 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
349 if (!decode_gost_algor_params(pk, palg)) {
353 expected_key_len = pkey_bits_gost(pk) > 0 ? pkey_bits_gost(pk) / 8 : 0;
354 if (expected_key_len == 0) {
355 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
359 if (priv_len % expected_key_len == 0) {
360 /* Key is not wrapped but masked */
361 pk_num = unmask_priv_key(pk, pkey_buf, expected_key_len,
362 priv_len / expected_key_len - 1);
363 } else if (V_ASN1_OCTET_STRING == *p) {
364 /* New format - Little endian octet string */
365 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
366 if (!s || ((s->length != 32) && (s->length != 64))) {
368 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
371 pk_num = hashsum2bn(s->data, s->length);
373 } else if (V_ASN1_INTEGER == *p) {
374 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
376 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
379 pk_num = ASN1_INTEGER_to_BN(priv_key, NULL);
380 ASN1_INTEGER_free(priv_key);
381 } else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
382 MASKED_GOST_KEY *mgk = NULL;
383 mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
386 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
390 priv_len = mgk->masked_priv_key->length;
391 if (priv_len % expected_key_len) {
392 MASKED_GOST_KEY_free(mgk);
393 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
397 pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
399 priv_len / expected_key_len - 1);
400 MASKED_GOST_KEY_free(mgk);
402 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
406 if (pk_num == NULL) {
407 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
411 ret = gost_set_priv_key(pk, pk_num);
416 /* ----------------------------------------------------------------------*/
417 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
419 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
420 ASN1_STRING *params = encode_gost_algor_params(pk);
421 unsigned char *priv_buf = NULL, *buf = NULL;
422 int key_len = pkey_bits_gost(pk), priv_len = 0, i = 0;
424 ASN1_STRING *octet = NULL;
429 key_len = (key_len < 0) ? 0 : key_len / 8;
430 if (key_len == 0 || !(buf = OPENSSL_malloc(key_len))) {
434 if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
439 /* Convert buf to Little-endian */
440 for (i = 0; i < key_len / 2; i++) {
441 unsigned char tmp = buf[i];
442 buf[i] = buf[key_len - 1 - i];
443 buf[key_len - 1 - i] = tmp;
446 octet = ASN1_STRING_new();
447 ASN1_OCTET_STRING_set(octet, buf, key_len);
449 priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
450 ASN1_STRING_free(octet);
453 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
457 /* --------- printing keys --------------------------------*/
458 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
462 if (!BIO_indent(out, indent, 128))
464 BIO_printf(out, "Private key: ");
465 key = gost_get0_priv_key(pkey);
467 BIO_printf(out, "<undefined>");
470 BIO_printf(out, "\n");
475 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
479 const EC_POINT *pubkey;
480 const EC_GROUP *group;
481 EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
486 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
493 pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
494 group = (key) ? EC_KEY_get0_group(key) : NULL;
495 if (!pubkey || !group)
498 if (!EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y, ctx)) {
499 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
502 if (!BIO_indent(out, indent, 128))
504 BIO_printf(out, "Public key:\n");
505 if (!BIO_indent(out, indent + 3, 128))
507 BIO_printf(out, "X:");
509 BIO_printf(out, "\n");
510 if (!BIO_indent(out, indent + 3, 128))
512 BIO_printf(out, "Y:");
514 BIO_printf(out, "\n");
523 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
525 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
526 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
532 param_nid = EC_GROUP_get_curve_name(group);
533 if (!BIO_indent(out, indent, 128))
535 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
540 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
541 ASN1_PCTX *pctx, int type)
544 if (print_gost_priv(out, pkey, indent) == 0)
548 if (print_gost_ec_pub(out, pkey, indent) == 0)
552 return print_gost_ec_param(out, pkey, indent);
555 static int param_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
558 return print_gost_ec(out, pkey, indent, pctx, 0);
561 static int pub_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
564 return print_gost_ec(out, pkey, indent, pctx, 1);
567 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
570 return print_gost_ec(out, pkey, indent, pctx, 2);
573 /* ---------------------------------------------------------------------*/
574 static int param_missing_gost_ec(const EVP_PKEY *pk)
576 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
579 if (!EC_KEY_get0_group(ec))
585 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
587 EC_KEY *eto = EVP_PKEY_get0(to);
588 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
589 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
590 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_INCOMPATIBLE_ALGORITHMS);
594 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
600 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
603 if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
604 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
609 if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
610 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
613 if (EC_KEY_get0_private_key(eto)) {
614 return gost_ec_compute_public(eto);
619 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
621 const EC_GROUP *group_a, *group_b;
622 EC_KEY *ec_a = EVP_PKEY_get0((EVP_PKEY *)a);
623 EC_KEY *ec_b = EVP_PKEY_get0((EVP_PKEY *)b);
627 group_a = EC_KEY_get0_group(ec_a);
628 group_b = EC_KEY_get0_group(ec_b);
629 if (!group_a || !group_b)
632 if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
638 /* ---------- Public key functions * --------------------------------------*/
639 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub)
641 X509_ALGOR *palg = NULL;
642 const unsigned char *pubkey_buf = NULL;
643 unsigned char *databuf;
644 ASN1_OBJECT *palgobj = NULL;
648 ASN1_OCTET_STRING *octet = NULL;
650 const EC_GROUP *group;
652 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
654 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
655 if (!decode_gost_algor_params(pk, palg))
657 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
658 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
660 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
663 databuf = OPENSSL_malloc(octet->length);
664 if (databuf == NULL) {
665 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
666 ASN1_OCTET_STRING_free(octet);
669 for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
670 databuf[j] = octet->data[i];
672 len = octet->length / 2;
673 ASN1_OCTET_STRING_free(octet);
675 Y = getbnfrombuf(databuf, len);
676 X = getbnfrombuf(databuf + len, len);
677 OPENSSL_free(databuf);
678 pub_key = EC_POINT_new(group);
679 if (!EC_POINT_set_affine_coordinates_GFp(group, pub_key, X, Y, NULL)) {
680 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
681 EC_POINT_free(pub_key);
688 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
689 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
690 EC_POINT_free(pub_key);
693 EC_POINT_free(pub_key);
698 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
700 ASN1_OBJECT *algobj = NULL;
701 ASN1_OCTET_STRING *octet = NULL;
703 unsigned char *buf = NULL, *databuf = NULL, *sptr;
704 int i, j, data_len, ret = -1;
705 const EC_POINT *pub_key;
706 BIGNUM *X = NULL, *Y = NULL, *order = NULL;
707 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
708 int ptype = V_ASN1_UNDEF;
710 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
711 if (pk->save_parameters) {
712 ASN1_STRING *params = encode_gost_algor_params(pk);
714 ptype = V_ASN1_SEQUENCE;
718 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
721 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
722 pub_key = EC_KEY_get0_public_key(ec);
724 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
730 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
733 if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
734 pub_key, X, Y, NULL)) {
735 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
738 data_len = 2 * BN_num_bytes(order);
739 databuf = OPENSSL_malloc(data_len);
740 if (databuf == NULL) {
741 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
744 memset(databuf, 0, data_len);
746 store_bignum(X, databuf + data_len / 2, data_len / 2);
747 store_bignum(Y, databuf, data_len / 2);
749 octet = ASN1_OCTET_STRING_new();
751 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
754 ASN1_STRING_set(octet, NULL, data_len);
755 sptr = ASN1_STRING_data(octet);
756 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
757 sptr[i] = databuf[j];
760 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
761 ASN1_BIT_STRING_free(octet);
770 OPENSSL_free(databuf);
774 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
777 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
779 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
780 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
781 const EC_POINT *ka, *kb;
784 ka = EC_KEY_get0_public_key(ea);
785 kb = EC_KEY_get0_public_key(eb);
788 return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
791 static int pkey_size_gost(const EVP_PKEY *pk)
796 id = EVP_PKEY_base_id(pk);
797 if (id == NID_id_GostR3410_94 ||
798 id == NID_id_GostR3410_2001 ||
799 id ==NID_gost2012_256 )
802 } else if (id == NID_gost2012_512) {
809 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
810 static void mackey_free_gost(EVP_PKEY *pk)
812 OPENSSL_free(pk->pkey.ptr);
815 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
818 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
820 *(int *)arg2 = NID_id_Gost28147_89_MAC;
827 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
830 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
832 *(int *)arg2 = NID_gost_mac_12;
839 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
842 EC_GROUP_get_curve_name(EC_KEY_get0_group
843 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
844 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
847 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
850 ASN1_OBJECT *obj = NULL;
852 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
855 nid = OBJ_obj2nid(obj);
856 ASN1_OBJECT_free(obj);
858 return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
861 /* ----------------------------------------------------------------------*/
862 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
863 const char *pemstr, const char *info)
865 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
868 if (nid == NID_id_GostR3410_2001) {
869 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
870 EVP_PKEY_asn1_set_private(*ameth,
871 priv_decode_gost, priv_encode_gost,
874 EVP_PKEY_asn1_set_param(*ameth,
875 gost2001_param_decode, gost2001_param_encode,
876 param_missing_gost_ec, param_copy_gost_ec,
877 param_cmp_gost_ec, param_print_gost_ec);
878 EVP_PKEY_asn1_set_public(*ameth,
879 pub_decode_gost_ec, pub_encode_gost_ec,
880 pub_cmp_gost_ec, pub_print_gost_ec,
881 pkey_size_gost, pkey_bits_gost);
883 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
884 } else if (nid == NID_gost2012_256 || nid == NID_gost2012_512) {
885 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
886 EVP_PKEY_asn1_set_private(*ameth,
887 priv_decode_gost, priv_encode_gost,
890 EVP_PKEY_asn1_set_param(*ameth,
892 param_missing_gost_ec, param_copy_gost_ec,
893 param_cmp_gost_ec, NULL);
895 EVP_PKEY_asn1_set_public(*ameth,
896 pub_decode_gost_ec, pub_encode_gost_ec,
897 pub_cmp_gost_ec, pub_print_gost_ec,
898 pkey_size_gost, pkey_bits_gost);
900 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
901 } else if (nid == NID_id_Gost28147_89_MAC) {
902 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
903 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
904 } else if (nid == NID_gost_mac_12) {
905 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
906 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);