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>
19 #include "gost_params.h"
21 #include "e_gost_err.h"
23 int gost94_nid_by_params(DSA *p)
25 R3410_params *gost_params;
27 for (gost_params = R3410_paramset;gost_params->q!=NULL; gost_params++)
29 BN_dec2bn(&q,gost_params->q);
33 return gost_params->nid;
40 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
42 ASN1_STRING *params = ASN1_STRING_new();
43 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
44 int pkey_param_nid = NID_undef;
48 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
49 ERR_R_MALLOC_FAILURE);
50 ASN1_STRING_free(params);
54 switch (EVP_PKEY_base_id(key))
56 case NID_id_GostR3410_2001:
57 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)key)));
59 case NID_id_GostR3410_94:
60 pkey_param_nid = (int) gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)key));
61 if (pkey_param_nid == NID_undef)
63 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
64 GOST_R_INVALID_GOST94_PARMSET);
65 ASN1_STRING_free(params);
71 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
72 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
73 /*gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);*/
74 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
75 if (params->length <=0 )
77 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
78 ERR_R_MALLOC_FAILURE);
79 ASN1_STRING_free(params);
83 params ->type = V_ASN1_SEQUENCE;
85 GOST_KEY_PARAMS_free(gkp);
89 /* Parses GOST algorithm parameters from X509_ALGOR and
90 * modifies pkey setting NID and parameters
92 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
94 ASN1_OBJECT *palg_obj =NULL;
95 int ptype = V_ASN1_UNDEF;
96 int pkey_nid = NID_undef,param_nid = NID_undef;
98 ASN1_STRING *pval = NULL;
99 const unsigned char *p;
100 GOST_KEY_PARAMS *gkp = NULL;
102 X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
104 if (ptype != V_ASN1_SEQUENCE)
106 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
107 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
111 pkey_nid = OBJ_obj2nid(palg_obj);
113 gkp = d2i_GOST_KEY_PARAMS(NULL,&p,pval->length);
116 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
117 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
120 param_nid = OBJ_obj2nid(gkp->key_params);
121 GOST_KEY_PARAMS_free(gkp);
122 EVP_PKEY_set_type(pkey,pkey_nid);
125 case NID_id_GostR3410_94:
127 DSA *dsa= EVP_PKEY_get0(pkey);
131 if (!EVP_PKEY_assign(pkey,pkey_nid,dsa)) return 0;
133 if (!fill_GOST94_params(dsa,param_nid)) return 0;
136 case NID_id_GostR3410_2001:
138 EC_KEY *ec = EVP_PKEY_get0(pkey);
142 if (!EVP_PKEY_assign(pkey,pkey_nid,ec)) return 0;
144 if (!fill_GOST2001_params(ec,param_nid)) return 0;
151 static int gost_set_priv_key(EVP_PKEY *pkey,BIGNUM *priv)
153 switch (EVP_PKEY_base_id(pkey))
155 case NID_id_GostR3410_94:
157 DSA *dsa = EVP_PKEY_get0(pkey);
161 EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),dsa);
163 dsa->priv_key = BN_dup(priv);
164 if (!EVP_PKEY_missing_parameters(pkey))
165 gost94_compute_public(dsa);
168 case NID_id_GostR3410_2001:
170 EC_KEY *ec = EVP_PKEY_get0(pkey);
174 EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),ec);
176 if (!EC_KEY_set_private_key(ec,priv)) return 0;
177 if (!EVP_PKEY_missing_parameters(pkey))
178 gost2001_compute_public(ec);
184 BIGNUM* gost_get0_priv_key(const EVP_PKEY *pkey)
186 switch (EVP_PKEY_base_id(pkey))
188 case NID_id_GostR3410_94:
190 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
195 if (!dsa->priv_key) return NULL;
196 return dsa->priv_key;
199 case NID_id_GostR3410_2001:
201 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
207 if (!(priv=EC_KEY_get0_private_key(ec))) return NULL;
208 return (BIGNUM *)priv;
215 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op,
216 long arg1, void *arg2)
220 case ASN1_PKEY_CTRL_PKCS7_SIGN:
223 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
224 int nid = EVP_PKEY_base_id(pkey);
225 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO*)arg2,
227 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
229 if (nid == NID_undef)
233 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
236 #ifndef OPENSSL_NO_CMS
237 case ASN1_PKEY_CTRL_CMS_SIGN:
240 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
241 int nid = EVP_PKEY_base_id(pkey);
242 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2,
243 NULL, NULL, &alg1, &alg2);
244 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
246 if (nid == NID_undef)
250 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
254 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
258 ASN1_STRING * params = encode_gost_algor_params(pkey);
263 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO*)arg2, &alg);
264 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
265 V_ASN1_SEQUENCE, params);
268 #ifndef OPENSSL_NO_CMS
269 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
273 ASN1_STRING * params = encode_gost_algor_params(pkey);
278 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL, NULL, &alg);
279 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
280 V_ASN1_SEQUENCE, params);
284 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
285 *(int *)arg2 = NID_id_GostR3411_94;
291 /*----------------------- free functions * ------------------------------*/
292 static void pkey_free_gost94(EVP_PKEY *key)
296 DSA_free(key->pkey.dsa);
300 static void pkey_free_gost01(EVP_PKEY *key)
304 EC_KEY_free(key->pkey.ec);
308 /* ------------------ private key functions -----------------------------*/
309 static int priv_decode_gost( EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
311 const unsigned char *pkey_buf = NULL,*p=NULL;
315 X509_ALGOR *palg =NULL;
316 ASN1_OBJECT *palg_obj = NULL;
317 ASN1_INTEGER *priv_key=NULL;
319 if (!PKCS8_pkey_get0(&palg_obj,&pkey_buf,&priv_len,&palg,p8inf))
322 if (!decode_gost_algor_params(pk,palg))
326 if (V_ASN1_OCTET_STRING == *p)
328 /* New format - Little endian octet string */
329 unsigned char rev_buf[32];
331 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL,&p,priv_len);
332 if (!s||s->length !=32)
334 GOSTerr(GOST_F_PRIV_DECODE_GOST,
340 rev_buf[31-i]=s->data[i];
343 pk_num = getbnfrombuf(rev_buf,32);
347 priv_key=d2i_ASN1_INTEGER(NULL,&p,priv_len);
348 if (!priv_key) return 0;
349 ret= ((pk_num = ASN1_INTEGER_to_BN(priv_key, NULL))!=NULL) ;
350 ASN1_INTEGER_free(priv_key);
353 GOSTerr(GOST_F_PRIV_DECODE_GOST,
359 ret= gost_set_priv_key(pk,pk_num);
364 /* ----------------------------------------------------------------------*/
365 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
367 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
368 ASN1_STRING *params = encode_gost_algor_params(pk);
369 unsigned char *priv_buf = NULL;
372 ASN1_INTEGER *asn1key=NULL;
377 asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk),NULL);
378 priv_len = i2d_ASN1_INTEGER(asn1key,&priv_buf);
379 ASN1_INTEGER_free(asn1key);
380 return PKCS8_pkey_set0(p8,algobj,0,V_ASN1_SEQUENCE,params,
383 /* --------- printing keys --------------------------------*/
384 static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent,
385 ASN1_PCTX *pctx, int type)
387 int param_nid = NID_undef;
393 if (!BIO_indent(out,indent,128)) return 0;
394 BIO_printf(out,"Private key: ");
395 key = gost_get0_priv_key(pkey);
397 BIO_printf(out,"<undefined>");
400 BIO_printf(out,"\n");
406 pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
407 BIO_indent(out,indent,128);
408 BIO_printf(out,"Public key: ");
409 BN_print(out,pubkey);
410 BIO_printf(out,"\n");
413 param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
414 BIO_indent(out,indent,128);
415 BIO_printf(out, "Parameter set: %s\n",OBJ_nid2ln(param_nid));
419 static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
422 return print_gost_94(out, pkey, indent, pctx,0);
425 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
428 return print_gost_94(out,pkey, indent, pctx,1);
430 static int priv_print_gost94(BIO *out,const EVP_PKEY *pkey, int indent,
433 return print_gost_94(out,pkey,indent,pctx,2);
436 static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
437 ASN1_PCTX *pctx, int type)
439 int param_nid = NID_undef;
444 if (!BIO_indent(out,indent,128)) return 0;
445 BIO_printf(out,"Private key: ");
446 key = gost_get0_priv_key(pkey);
448 BIO_printf(out,"<undefined)");
451 BIO_printf(out,"\n");
455 BN_CTX *ctx = BN_CTX_new();
457 const EC_POINT *pubkey;
458 const EC_GROUP *group;
462 GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_MALLOC_FAILURE);
468 pubkey = EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
469 group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
470 if (!EC_POINT_get_affine_coordinates_GFp(group,pubkey,X,Y,ctx))
472 GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_EC_LIB);
476 if (!BIO_indent(out,indent,128)) return 0;
477 BIO_printf(out,"Public key:\n");
478 if (!BIO_indent(out,indent+3,128)) return 0;
479 BIO_printf(out,"X:");
481 BIO_printf(out,"\n");
482 BIO_indent(out,indent+3,128);
483 BIO_printf(out,"Y:");
485 BIO_printf(out,"\n");
490 param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
491 if (!BIO_indent(out,indent,128)) return 0;
492 BIO_printf(out,"Parameter set: %s\n",OBJ_nid2ln(param_nid));
495 static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
498 return print_gost_01(out,pkey,indent,pctx,0);
500 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
503 return print_gost_01(out,pkey, indent, pctx,1);
505 static int priv_print_gost01(BIO *out,const EVP_PKEY *pkey, int indent,
508 return print_gost_01(out,pkey,indent,pctx,2);
510 /* ---------------------------------------------------------------------*/
511 static int param_missing_gost94(const EVP_PKEY *pk)
513 const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
515 if (!dsa->q) return 1;
519 static int param_missing_gost01(const EVP_PKEY *pk)
521 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
523 if (!EC_KEY_get0_group(ec)) return 1;
527 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from)
529 const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
530 DSA *dto = EVP_PKEY_get0(to);
531 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to))
533 GOSTerr(GOST_F_PARAM_COPY_GOST94,
534 GOST_R_INCOMPATIBLE_ALGORITHMS);
539 GOSTerr(GOST_F_PARAM_COPY_GOST94,
540 GOST_R_KEY_PARAMETERS_MISSING);
546 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),dto);
548 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);
549 COPYBIGNUM(dto,dfrom,p)
550 COPYBIGNUM(dto,dfrom,q)
551 COPYBIGNUM(dto,dfrom,g)
554 gost94_compute_public(dto);
557 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
559 EC_KEY *eto = EVP_PKEY_get0(to);
560 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
561 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to))
563 GOSTerr(GOST_F_PARAM_COPY_GOST01,
564 GOST_R_INCOMPATIBLE_ALGORITHMS);
569 GOSTerr(GOST_F_PARAM_COPY_GOST01,
570 GOST_R_KEY_PARAMETERS_MISSING);
576 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),eto);
578 EC_KEY_set_group(eto,EC_KEY_get0_group(efrom));
579 if (EC_KEY_get0_private_key(eto))
581 gost2001_compute_public(eto);
586 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
588 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
589 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
590 if (!BN_cmp(da->q,db->q)) return 1;
594 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
596 if (EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a)))==
597 EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)b))))
605 /* ---------- Public key functions * --------------------------------------*/
606 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
608 X509_ALGOR *palg = NULL;
609 const unsigned char *pubkey_buf = NULL;
610 unsigned char *databuf;
611 ASN1_OBJECT *palgobj = NULL;
614 ASN1_OCTET_STRING *octet= NULL;
616 if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
617 &palg, pub)) return 0;
618 EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);
619 if (!decode_gost_algor_params(pk,palg)) return 0;
620 octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
623 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
626 databuf = OPENSSL_malloc(octet->length);
627 for (i=0,j=octet->length-1;i<octet->length;i++,j--)
629 databuf[j]=octet->data[i];
631 dsa = EVP_PKEY_get0(pk);
632 dsa->pub_key=BN_bin2bn(databuf,octet->length,NULL);
633 ASN1_OCTET_STRING_free(octet);
634 OPENSSL_free(databuf);
639 static int pub_encode_gost94(X509_PUBKEY *pub,const EVP_PKEY *pk)
641 ASN1_OBJECT *algobj = NULL;
642 ASN1_OCTET_STRING *octet = NULL;
644 unsigned char *buf=NULL,*databuf,*sptr;
645 int i,j,data_len,ret=0;
647 int ptype = V_ASN1_UNDEF;
648 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
649 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
650 if (pk->save_parameters)
652 ASN1_STRING *params = encode_gost_algor_params(pk);
654 ptype = V_ASN1_SEQUENCE;
656 data_len = BN_num_bytes(dsa->pub_key);
657 databuf = OPENSSL_malloc(data_len);
658 BN_bn2bin(dsa->pub_key,databuf);
659 octet = ASN1_OCTET_STRING_new();
660 ASN1_STRING_set(octet,NULL,data_len);
661 sptr = ASN1_STRING_data(octet);
662 for (i=0,j=data_len-1; i< data_len;i++,j--)
666 OPENSSL_free(databuf);
667 ret = i2d_ASN1_OCTET_STRING(octet,&buf);
668 ASN1_BIT_STRING_free(octet);
669 if (ret <0) return 0;
670 return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
673 static int pub_decode_gost01(EVP_PKEY *pk,X509_PUBKEY *pub)
675 X509_ALGOR *palg = NULL;
676 const unsigned char *pubkey_buf = NULL;
677 unsigned char *databuf;
678 ASN1_OBJECT *palgobj = NULL;
682 ASN1_OCTET_STRING *octet= NULL;
684 const EC_GROUP *group;
686 if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
687 &palg, pub)) return 0;
688 EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);
689 if (!decode_gost_algor_params(pk,palg)) return 0;
690 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
691 octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
694 GOSTerr(GOST_F_PUB_DECODE_GOST01,ERR_R_MALLOC_FAILURE);
697 databuf = OPENSSL_malloc(octet->length);
698 for (i=0,j=octet->length-1;i<octet->length;i++,j--)
700 databuf[j]=octet->data[i];
703 ASN1_OCTET_STRING_free(octet);
705 Y= getbnfrombuf(databuf,len);
706 X= getbnfrombuf(databuf+len,len);
707 OPENSSL_free(databuf);
708 pub_key = EC_POINT_new(group);
709 if (!EC_POINT_set_affine_coordinates_GFp(group
712 GOSTerr(GOST_F_PUB_DECODE_GOST01,
714 EC_POINT_free(pub_key);
721 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk),pub_key))
723 GOSTerr(GOST_F_PUB_DECODE_GOST01,
725 EC_POINT_free(pub_key);
728 EC_POINT_free(pub_key);
733 static int pub_encode_gost01(X509_PUBKEY *pub,const EVP_PKEY *pk)
735 ASN1_OBJECT *algobj = NULL;
736 ASN1_OCTET_STRING *octet = NULL;
738 unsigned char *buf=NULL,*databuf,*sptr;
739 int i,j,data_len,ret=0;
740 const EC_POINT *pub_key;
742 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
743 int ptype = V_ASN1_UNDEF;
745 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
746 if (pk->save_parameters)
748 ASN1_STRING *params = encode_gost_algor_params(pk);
750 ptype = V_ASN1_SEQUENCE;
753 EC_GROUP_get_order(EC_KEY_get0_group(ec),order,NULL);
754 pub_key=EC_KEY_get0_public_key(ec);
757 GOSTerr(GOST_F_PUB_ENCODE_GOST01,
758 GOST_R_PUBLIC_KEY_UNDEFINED);
763 EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
765 data_len = 2*BN_num_bytes(order);
767 databuf = OPENSSL_malloc(data_len);
768 memset(databuf,0,data_len);
770 store_bignum(X,databuf+data_len/2,data_len/2);
771 store_bignum(Y,databuf,data_len/2);
775 octet = ASN1_OCTET_STRING_new();
776 ASN1_STRING_set(octet,NULL,data_len);
777 sptr=ASN1_STRING_data(octet);
778 for (i=0,j=data_len-1;i<data_len;i++,j--)
782 OPENSSL_free(databuf);
783 ret = i2d_ASN1_OCTET_STRING(octet,&buf);
784 ASN1_BIT_STRING_free(octet);
785 if (ret <0) return 0;
786 return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
789 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
791 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
792 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
793 if (da && db && da->pub_key && db->pub_key
794 && !BN_cmp(da->pub_key,db->pub_key))
801 static int pub_cmp_gost01(const EVP_PKEY *a,const EVP_PKEY *b)
803 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
804 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
805 const EC_POINT *ka,*kb;
807 if (!ea || !eb) return 0;
808 ka = EC_KEY_get0_public_key(ea);
809 kb = EC_KEY_get0_public_key(eb);
810 if (!ka || !kb) return 0;
811 ret = (0==EC_POINT_cmp(EC_KEY_get0_group(ea),ka,kb,NULL)) ;
818 static int pkey_size_gost(const EVP_PKEY *pk)
823 static int pkey_bits_gost(const EVP_PKEY *pk)
827 /*------------------------ ASN1 METHOD for GOST MAC -------------------*/
828 static void mackey_free_gost(EVP_PKEY *pk)
831 OPENSSL_free(pk->pkey.ptr);
834 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
838 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
839 *(int *)arg2 = NID_id_Gost28147_89_MAC;
845 static int gost94_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
847 int nid=gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
848 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid),pder);
850 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
852 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
853 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid),pder);
856 static int gost94_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen)
858 ASN1_OBJECT *obj=NULL;
859 DSA *dsa = EVP_PKEY_get0(pkey);
861 if (d2i_ASN1_OBJECT(&obj,pder,derlen)==NULL) {
864 nid = OBJ_obj2nid(obj);
865 ASN1_OBJECT_free(obj);
869 if (!EVP_PKEY_assign(pkey,NID_id_GostR3410_94,dsa)) return 0;
871 if (!fill_GOST94_params(dsa,nid)) return 0;
875 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) {
876 ASN1_OBJECT *obj=NULL;
878 EC_KEY *ec = EVP_PKEY_get0(pkey);
879 if (d2i_ASN1_OBJECT(&obj,pder,derlen)==NULL) {
882 nid = OBJ_obj2nid(obj);
883 ASN1_OBJECT_free(obj);
887 if (!EVP_PKEY_assign(pkey,NID_id_GostR3410_2001,ec)) return 0;
889 if (!fill_GOST2001_params(ec, nid)) return 0;
897 /* ----------------------------------------------------------------------*/
898 int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info)
900 *ameth = EVP_PKEY_asn1_new(nid,
901 ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
902 if (!*ameth) return 0;
905 case NID_id_GostR3410_94:
906 EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost94);
907 EVP_PKEY_asn1_set_private (*ameth,
908 priv_decode_gost, priv_encode_gost,
911 EVP_PKEY_asn1_set_param (*ameth,
912 gost94_param_decode, gost94_param_encode,
913 param_missing_gost94, param_copy_gost94,
914 param_cmp_gost94,param_print_gost94 );
915 EVP_PKEY_asn1_set_public (*ameth,
916 pub_decode_gost94, pub_encode_gost94,
917 pub_cmp_gost94, pub_print_gost94,
918 pkey_size_gost, pkey_bits_gost);
920 EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
922 case NID_id_GostR3410_2001:
923 EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost01);
924 EVP_PKEY_asn1_set_private (*ameth,
925 priv_decode_gost, priv_encode_gost,
928 EVP_PKEY_asn1_set_param (*ameth,
929 gost2001_param_decode, gost2001_param_encode,
930 param_missing_gost01, param_copy_gost01,
931 param_cmp_gost01, param_print_gost01);
932 EVP_PKEY_asn1_set_public (*ameth,
933 pub_decode_gost01, pub_encode_gost01,
934 pub_cmp_gost01, pub_print_gost01,
935 pkey_size_gost, pkey_bits_gost);
937 EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
939 case NID_id_Gost28147_89_MAC:
940 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
941 EVP_PKEY_asn1_set_ctrl(*ameth,mac_ctrl_gost);