Updated GOST MAC support.
[openssl.git] / engines / ccgost / gost_ameth.c
1 /**********************************************************************
2  *                          gost_ameth.c                              *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *       Implementation of RFC 4490/4491 ASN1 method                  *
7  *       for OpenSSL                                                  *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <openssl/engine.h>
11 #include <openssl/evp.h>
12 #include <openssl/asn1.h>
13 #include <string.h>
14 #include "gost_params.h"
15 #include "gost_lcl.h"
16 #include "e_gost_err.h"
17
18 int gost94_nid_by_params(DSA *p) 
19         {
20         R3410_params *gost_params;
21         BIGNUM *q=BN_new();
22         for (gost_params = R3410_paramset;gost_params->q!=NULL; gost_params++) 
23                 {
24                 BN_dec2bn(&q,gost_params->q);
25                 if (!BN_cmp(q,p->q)) 
26                         {
27                         BN_free(q);
28                         return gost_params->nid;
29                         }
30                 }       
31         BN_free(q);
32         return NID_undef;
33         }
34
35 static ASN1_STRING  *encode_gost_algor_params(const EVP_PKEY *key)
36         {
37         ASN1_STRING *params = ASN1_STRING_new();
38         GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
39         int pkey_param_nid = NID_undef;
40         int cipher_param_nid = NID_undef;
41         if (!params || !gkp) 
42                 {
43                 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
44                         ERR_R_MALLOC_FAILURE);
45                 ASN1_STRING_free(params);
46                 params = NULL;
47                 goto err;
48                 }       
49         switch (EVP_PKEY_base_id(key)) 
50                 {
51                 case NID_id_GostR3410_2001_cc:
52                         pkey_param_nid = NID_id_GostR3410_2001_ParamSet_cc;
53                         cipher_param_nid = NID_id_Gost28147_89_cc;
54                         break;
55                 case NID_id_GostR3410_94_cc:
56                         pkey_param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
57                         cipher_param_nid = NID_id_Gost28147_89_cc;
58                         break;
59                 case NID_id_GostR3410_2001:
60                         pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)key)));
61                         cipher_param_nid = get_encryption_params(NULL)->nid;
62                         break;
63                 case NID_id_GostR3410_94:
64                         pkey_param_nid = (int) gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)key));
65                         if (pkey_param_nid == NID_undef) 
66                                 {
67                                 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
68                                         GOST_R_INVALID_GOST94_PARMSET);
69                                 ASN1_STRING_free(params);
70                                 params=NULL;
71                                 goto err;
72                                 }       
73                         cipher_param_nid = get_encryption_params(NULL)->nid;
74                         break;
75                 }       
76         gkp->key_params = OBJ_nid2obj(pkey_param_nid);
77         gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
78         /*gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);*/
79         params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
80         if (params->length <=0 ) 
81                 {
82                 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
83                         ERR_R_MALLOC_FAILURE);
84                 ASN1_STRING_free(params);
85                 params = NULL;
86                 goto err;
87                 }
88         params ->type = V_ASN1_SEQUENCE;
89         err:
90         GOST_KEY_PARAMS_free(gkp);
91         return params;
92         }
93
94 /* Parses GOST algorithm parameters from X509_ALGOR and
95  * modifies pkey setting NID and parameters
96  */
97 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg) 
98         {
99         ASN1_OBJECT *palg_obj =NULL;
100         int ptype = V_ASN1_UNDEF;
101         int pkey_nid = NID_undef,param_nid = NID_undef;
102         void *_pval;
103         ASN1_STRING *pval = NULL;
104         const unsigned char  *p;
105         GOST_KEY_PARAMS *gkp = NULL;
106
107         X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
108         pval = _pval;
109         if (ptype != V_ASN1_SEQUENCE) 
110                 {
111                 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
112                         GOST_R_BAD_KEY_PARAMETERS_FORMAT);
113                 return 0;
114                 }       
115         p=pval->data;
116         pkey_nid = OBJ_obj2nid(palg_obj);
117
118         gkp = d2i_GOST_KEY_PARAMS(NULL,&p,pval->length);
119         if (!gkp) 
120                 {
121                 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
122                         GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
123                 return 0;
124                 }       
125         param_nid = OBJ_obj2nid(gkp->key_params);
126         GOST_KEY_PARAMS_free(gkp);
127         EVP_PKEY_set_type(pkey,pkey_nid);
128         switch (pkey_nid) 
129                 {
130                 case NID_id_GostR3410_94:
131                 case NID_id_GostR3410_94_cc:
132                 {
133                 DSA *dsa= EVP_PKEY_get0(pkey);
134                 if (!dsa) 
135                         {
136                         dsa = DSA_new();
137                         if (!EVP_PKEY_assign(pkey,pkey_nid,dsa)) return 0;
138                         }
139                 if (!fill_GOST94_params(dsa,param_nid)) return 0;
140                 break;
141                 }
142                 case NID_id_GostR3410_2001:
143                 case NID_id_GostR3410_2001_cc:
144                 {
145                 EC_KEY *ec = EVP_PKEY_get0(pkey);
146                 if (!ec) 
147                         {
148                         ec = EC_KEY_new();
149                         if (!EVP_PKEY_assign(pkey,pkey_nid,ec)) return 0;
150                         }
151                 if (!fill_GOST2001_params(ec,param_nid)) return 0;
152                 }
153                 }
154
155         return 1;
156         }
157
158 static int gost_set_priv_key(EVP_PKEY *pkey,BIGNUM *priv) 
159         {
160         switch (EVP_PKEY_base_id(pkey)) 
161                 {
162                 case NID_id_GostR3410_94:
163                 case NID_id_GostR3410_94_cc:
164                 {
165                 DSA *dsa = EVP_PKEY_get0(pkey);
166                 if (!dsa) 
167                         {
168                         dsa = DSA_new();
169                         EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),dsa);
170                         }       
171                 dsa->priv_key = BN_dup(priv);
172                 if (!EVP_PKEY_missing_parameters(pkey)) 
173                         gost94_compute_public(dsa);
174                 break;
175                 }       
176                 case NID_id_GostR3410_2001:
177                 case NID_id_GostR3410_2001_cc:
178                 {
179                 EC_KEY *ec = EVP_PKEY_get0(pkey);
180                 if (!ec) 
181                         {
182                         ec = EC_KEY_new();
183                         EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),ec);
184                         }       
185                 if (!EC_KEY_set_private_key(ec,priv)) return 0;
186                 if (!EVP_PKEY_missing_parameters(pkey)) 
187                         gost2001_compute_public(ec);
188                 break;
189                 }
190                 }
191         return 1;               
192         }
193 BIGNUM* gost_get_priv_key(const EVP_PKEY *pkey) 
194         {
195         switch (EVP_PKEY_base_id(pkey)) 
196                 {
197                 case NID_id_GostR3410_94:
198                 case NID_id_GostR3410_94_cc:
199                 {
200                 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
201                 if (!dsa) 
202                         {
203                         return NULL;
204                         }       
205                 if (!dsa->priv_key) return NULL;
206                 return BN_dup(dsa->priv_key);
207                 break;
208                 }       
209                 case NID_id_GostR3410_2001:
210                 case NID_id_GostR3410_2001_cc:
211                 {
212                 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
213                 const BIGNUM* priv;
214                 if (!ec) 
215                         {
216                         return NULL;
217                         }       
218                 if (!(priv=EC_KEY_get0_private_key(ec))) return NULL;
219                 return BN_dup(priv);
220                 break;
221                 }
222                 }
223         return NULL;            
224         }
225
226 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op,
227         long arg1, void *arg2)
228         {
229         switch (op)
230                 {
231                 case ASN1_PKEY_CTRL_PKCS7_SIGN:
232                         if (arg1 == 0) 
233                                 {
234                                 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
235                                 int nid = EVP_PKEY_base_id(pkey);
236                                 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO*)arg2, 
237                                         NULL, &alg1, &alg2);
238                                 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
239                                         V_ASN1_NULL, 0);
240                                 if (nid == NID_undef) 
241                                         {
242                                         return (-1);
243                                         }
244                                 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
245                                 }
246                         return 1;
247                 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
248                         if (arg1 == 0)
249                                 {
250                                 X509_ALGOR *alg;
251                                 ASN1_STRING * params = encode_gost_algor_params(pkey);
252                                 if (!params) 
253                                         {
254                                         return -1;
255                                         }
256                                 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO*)arg2, &alg);
257                                 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
258                                         V_ASN1_SEQUENCE, params);
259                                 }
260                         return 1;
261                 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
262                         *(int *)arg2 = NID_id_GostR3411_94;
263                         return 2;
264                 }
265         
266         return -2;
267         }
268 /*----------------------- free functions * ------------------------------*/
269 static void pkey_free_gost94(EVP_PKEY *key) 
270         {
271         if (key->pkey.dsa) 
272                 {
273                 DSA_free(key->pkey.dsa);
274                 }
275         }
276
277 static void pkey_free_gost01(EVP_PKEY *key) 
278         {
279         if (key->pkey.ec) 
280                 {
281                 EC_KEY_free(key->pkey.ec);
282                 }
283         }       
284
285 /* ------------------ private key functions  -----------------------------*/
286 static int priv_decode_gost( EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf) 
287         {
288         const unsigned char *pkey_buf = NULL,*p=NULL;
289         int priv_len = 0;
290         BIGNUM *pk_num=NULL;
291         int ret =0;
292         X509_ALGOR *palg =NULL;
293         ASN1_OBJECT *palg_obj = NULL;
294         ASN1_INTEGER *priv_key=NULL;
295
296         if (!PKCS8_pkey_get0(&palg_obj,&pkey_buf,&priv_len,&palg,p8inf)) 
297                 return 0;
298         p = pkey_buf;
299         if (!decode_gost_algor_params(pk,palg)) 
300                 {
301                 return 0;
302                 }
303         if (V_ASN1_OCTET_STRING == *p) 
304                 {
305                 /* New format - Little endian octet string */
306                 unsigned char rev_buf[32];
307                 int i;
308                 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL,&p,priv_len);
309                 if (!s||s->length !=32) 
310                         {
311                         GOSTerr(GOST_F_PRIV_DECODE_GOST_94,
312                                 EVP_R_DECODE_ERROR);
313                         return 0;       
314                         }
315                 for (i=0;i<32;i++)
316                         {
317                         rev_buf[31-i]=s->data[i];
318                         }
319                 ASN1_STRING_free(s);
320                 pk_num = getbnfrombuf(rev_buf,32);
321                 } 
322         else
323                 {
324                 priv_key=d2i_ASN1_INTEGER(NULL,&p,priv_len);
325                 if (!priv_key || !(pk_num =  ASN1_INTEGER_to_BN(priv_key, NULL))) 
326                         {
327                         GOSTerr(GOST_F_PRIV_DECODE_GOST_94,
328                                 EVP_R_DECODE_ERROR);
329                         return 0;       
330                         }
331                 }
332
333         ret= gost_set_priv_key(pk,pk_num);
334         BN_free(pk_num);
335         return ret;
336         }
337
338 /* ----------------------------------------------------------------------*/
339 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
340         {
341         ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
342         ASN1_STRING *params = encode_gost_algor_params(pk);
343         unsigned char *priv_buf = NULL;
344         int priv_len;
345         BIGNUM *key;
346
347         ASN1_INTEGER *asn1key=NULL;
348         if (!params) 
349                 {
350                 return 0;
351                 }
352         key = gost_get_priv_key(pk);
353         asn1key = BN_to_ASN1_INTEGER(key,NULL);
354         BN_free(key);
355         priv_len = i2d_ASN1_INTEGER(asn1key,&priv_buf);
356         ASN1_INTEGER_free(asn1key);
357         return PKCS8_pkey_set0(p8,algobj,0,V_ASN1_SEQUENCE,params,
358                 priv_buf,priv_len);
359         }
360
361 static int priv_print_gost (BIO *out,const EVP_PKEY *pkey, int indent,
362         ASN1_PCTX *pctx) 
363         {
364         BIGNUM *key;
365         if (!BIO_indent(out,indent,128)) return 0;
366         key = gost_get_priv_key(pkey);
367         if (!key) return 0;
368         BN_print(out,key);
369         BN_free(key);
370         return 1;
371         }
372
373 /* ---------------------------------------------------------------------*/
374 static int param_missing_gost94(const EVP_PKEY *pk) 
375         {
376         const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
377         if (!dsa) return 1;
378         if (!dsa->q) return 1;
379         return 0;
380         }
381
382 static int param_missing_gost01(const EVP_PKEY *pk) 
383         {
384         const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
385         if (!ec) return 1;
386         if (!EC_KEY_get0_group(ec)) return 1;
387         return 0;
388         }
389
390 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from) 
391         {
392         const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
393         DSA *dto = EVP_PKEY_get0(to);
394         if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) 
395                 {
396                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
397                         GOST_R_INCOMPATIBLE_ALGORITHMS);
398                 return 0;
399                 }       
400         if (!dfrom) 
401                 {
402                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
403                         GOST_R_KEY_PARAMETERS_MISSING);
404                 return 0;
405                 }       
406         if (!dto) 
407                 {
408                 dto = DSA_new();
409                 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),dto);
410                 }       
411 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);   
412         COPYBIGNUM(dto,dfrom,p)
413                 COPYBIGNUM(dto,dfrom,q)
414                 COPYBIGNUM(dto,dfrom,g)
415
416                 if (dto->priv_key) 
417                         gost94_compute_public(dto);
418         return 1;       
419         }
420 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from) 
421         {
422         EC_KEY *eto = EVP_PKEY_get0(to);
423         const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
424         if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) 
425                 {
426                 GOSTerr(GOST_F_PARAM_COPY_GOST01,
427                         GOST_R_INCOMPATIBLE_ALGORITHMS);
428                 return 0;
429                 }       
430         if (!efrom) 
431                 {
432                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
433                         GOST_R_KEY_PARAMETERS_MISSING);
434                 return 0;
435                 }       
436         if (!eto) 
437                 {
438                 eto = EC_KEY_new();
439                 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),eto);
440                 }       
441         EC_KEY_set_group(eto,EC_GROUP_dup(EC_KEY_get0_group(efrom)));
442         if (EC_KEY_get0_private_key(eto)) 
443                 {
444                 gost2001_compute_public(eto);
445                 }
446         return 1;
447         }
448
449 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b) 
450         {
451         const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
452         const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
453         if (!BN_cmp(da->q,db->q)) return 1;
454         return 0;
455         }
456
457 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b) 
458         {
459         if (EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a)))==
460                 EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)b)))) 
461                 {
462                 return 1;
463                 }
464         return 0;
465
466         }
467
468 /* ---------- Public key functions * --------------------------------------*/
469 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
470         {
471         X509_ALGOR *palg = NULL;
472         const unsigned char *pubkey_buf = NULL;
473         unsigned char *databuf;
474         ASN1_OBJECT *palgobj = NULL;
475         int pub_len,i,j;
476         DSA *dsa;
477         ASN1_OCTET_STRING *octet= NULL;
478
479         if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
480                         &palg, pub)) return 0;
481         EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);  
482         if (!decode_gost_algor_params(pk,palg)) return 0;
483         octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
484         if (!octet) 
485                 {
486                 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
487                 return 0;
488                 }       
489         databuf = OPENSSL_malloc(octet->length);
490         for (i=0,j=octet->length-1;i<octet->length;i++,j--)
491                 {
492                 databuf[j]=octet->data[i];
493                 }       
494         dsa = EVP_PKEY_get0(pk);
495         dsa->pub_key=BN_bin2bn(databuf,octet->length,NULL);
496         ASN1_OCTET_STRING_free(octet);
497         OPENSSL_free(databuf);
498         return 1;
499
500         }
501
502 static int pub_encode_gost94(X509_PUBKEY *pub,const EVP_PKEY *pk)
503         {
504         ASN1_OBJECT *algobj = NULL;
505         ASN1_OCTET_STRING *octet = NULL;
506         void *pval = NULL;
507         unsigned char *buf=NULL,*databuf,*sptr;
508         int i,j,data_len,ret=0;
509
510         int ptype = V_ASN1_UNDEF;
511         DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
512         algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
513         if (pk->save_parameters) 
514                 {
515                 ASN1_STRING *params = encode_gost_algor_params(pk);
516                 pval = params;
517                 ptype = V_ASN1_SEQUENCE;
518                 }       
519         data_len = BN_num_bytes(dsa->pub_key);
520         databuf = OPENSSL_malloc(data_len);
521         BN_bn2bin(dsa->pub_key,databuf);
522         octet = ASN1_OCTET_STRING_new();
523         ASN1_STRING_set(octet,NULL,data_len);
524         sptr = ASN1_STRING_data(octet);
525         for (i=0,j=data_len-1; i< data_len;i++,j--)
526                 {
527                 sptr[i]=databuf[j];
528                 }
529         OPENSSL_free(databuf);
530         ret = i2d_ASN1_OCTET_STRING(octet,&buf);
531         ASN1_BIT_STRING_free(octet);
532         if (ret <0)  return 0;
533         return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
534         }
535
536 static int pub_decode_gost01(EVP_PKEY *pk,X509_PUBKEY *pub)
537         {
538         X509_ALGOR *palg = NULL;
539         const unsigned char *pubkey_buf = NULL;
540         unsigned char *databuf;
541         ASN1_OBJECT *palgobj = NULL;
542         int pub_len,i,j;
543         EC_POINT *pub_key;
544         BIGNUM *X,*Y;
545         ASN1_OCTET_STRING *octet= NULL;
546         const EC_GROUP *group;
547
548         if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
549                         &palg, pub)) return 0;
550         EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);  
551         if (!decode_gost_algor_params(pk,palg)) return 0;
552         group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
553         octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
554         if (!octet) 
555                 {
556                 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
557                 return 0;
558                 }       
559         databuf = OPENSSL_malloc(octet->length);
560         for (i=0,j=octet->length-1;i<octet->length;i++,j--)
561                 {
562                 databuf[j]=octet->data[i];
563                 }
564         if (EVP_PKEY_base_id(pk) == NID_id_GostR3410_2001_cc) 
565                 {
566                 X= getbnfrombuf(databuf,octet->length/2);
567                 Y= getbnfrombuf(databuf+(octet->length/2),octet->length/2);
568                 }
569         else 
570                 {
571                 Y= getbnfrombuf(databuf,octet->length/2);
572                 X= getbnfrombuf(databuf+(octet->length/2),octet->length/2);
573                 }
574         OPENSSL_free(databuf);
575         pub_key = EC_POINT_new(group);
576         if (!EC_POINT_set_affine_coordinates_GFp(group
577                         ,pub_key,X,Y,NULL))
578                 {
579                 GOSTerr(GOST_F_PUB_DECODE_GOST01,
580                         ERR_R_EC_LIB);
581                 return 0;
582                 }       
583         BN_free(X);
584         BN_free(Y);
585         if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk),pub_key))
586                 {
587                 GOSTerr(GOST_F_PUB_DECODE_GOST01,
588                         ERR_R_EC_LIB);
589                 return 0;
590                 }       
591         /*EC_POINT_free(pub_key);*/
592         return 1;
593
594         }
595
596 static int pub_encode_gost01(X509_PUBKEY *pub,const EVP_PKEY *pk)
597         {
598         ASN1_OBJECT *algobj = NULL;
599         ASN1_OCTET_STRING *octet = NULL;
600         void *pval = NULL;
601         unsigned char *buf=NULL,*databuf,*sptr;
602         int i,j,data_len,ret=0;
603         const EC_POINT *pub_key;
604         BIGNUM *X,*Y,*order;
605         const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
606         int ptype = V_ASN1_UNDEF;
607
608         algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
609         if (pk->save_parameters) 
610                 {
611                 ASN1_STRING *params = encode_gost_algor_params(pk);
612                 pval = params;
613                 ptype = V_ASN1_SEQUENCE;
614                 }
615         order = BN_new();
616         EC_GROUP_get_order(EC_KEY_get0_group(ec),order,NULL);
617         pub_key=EC_KEY_get0_public_key(ec);
618         if (!pub_key) 
619                 {
620                 GOSTerr(GOST_F_PUB_ENCODE_GOST01,
621                         GOST_R_PUBLIC_KEY_UNDEFINED);
622                 return 0;
623                 }       
624         X=BN_new();
625         Y=BN_new();
626         EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
627                 pub_key,X,Y,NULL);
628         data_len = 2*BN_num_bytes(order);
629         BN_free(order);
630         databuf = OPENSSL_malloc(data_len);
631         memset(databuf,0,data_len);
632         if (EVP_PKEY_base_id(pk) == NID_id_GostR3410_2001_cc) 
633                 {
634                 store_bignum(X,databuf,data_len/2);
635                 store_bignum(Y,databuf+data_len/2,data_len/2);
636                 }
637         else 
638                 {
639                 store_bignum(X,databuf+data_len/2,data_len/2);
640                 store_bignum(Y,databuf,data_len/2);
641                 }
642         BN_free(X);
643         BN_free(Y);
644         octet = ASN1_OCTET_STRING_new();
645         ASN1_STRING_set(octet,NULL,data_len);
646         sptr=ASN1_STRING_data(octet);
647     for (i=0,j=data_len-1;i<data_len;i++,j--) 
648                 {
649         sptr[i]=databuf[j];
650                 }
651     OPENSSL_free(databuf);
652         ret = i2d_ASN1_OCTET_STRING(octet,&buf);
653         ASN1_BIT_STRING_free(octet);
654         if (ret <0)  return 0;
655         return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
656         }
657
658 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
659         {
660         const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
661         const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
662         if (da && db && da->pub_key && db->pub_key
663                 && !BN_cmp(da->pub_key,db->pub_key)) 
664                 {
665                 return 1;
666                 }               
667         return 0;
668         }
669
670 static int pub_cmp_gost01(const EVP_PKEY *a,const EVP_PKEY *b)
671         {
672         const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
673         const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
674         const EC_POINT *ka,*kb;
675         int ret=0;
676         if (!ea || !eb) return 0;
677         ka = EC_KEY_get0_public_key(ea);
678         kb = EC_KEY_get0_public_key(eb);
679         if (!ka || !kb) return 0;
680         ret = (0==EC_POINT_cmp(EC_KEY_get0_group(ea),ka,kb,NULL)) ;
681         return ret;
682         }
683
684 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
685         ASN1_PCTX *pctx)
686         {
687         const BIGNUM *key;
688         if (!BIO_indent(out,indent,128)) return 0;
689         key = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
690         if (!key) return 0;
691         BN_print(out,key);
692         return 1;
693         }
694
695 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
696         ASN1_PCTX *pctx)
697         {
698         return 0;
699         }
700
701 static int pkey_size_gost(const EVP_PKEY *pk)
702         {
703         return 64;
704         }
705
706 static int pkey_bits_gost(const EVP_PKEY *pk)
707         {
708         return 256;
709         }
710 /*------------------------ ASN1 METHOD for GOST MAC  -------------------*/
711 static void  mackey_free_gost(EVP_PKEY *pk)
712         {
713                 if (pk->pkey.ptr) {
714                         OPENSSL_free(pk->pkey.ptr);
715                 }       
716         }
717 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
718 {
719         switch (op)
720                 {
721                 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
722                         *(int *)arg2 = NID_undef;
723                         return 2;
724                 }
725         return -2;
726 }       
727 /* ----------------------------------------------------------------------*/
728 int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info) 
729         {
730         *ameth =        EVP_PKEY_asn1_new(nid, 
731                 ASN1_PKEY_SIGPARAM_NULL, pemstr, info); 
732         if (!*ameth) return 0;
733         switch (nid) 
734                 {
735                 case NID_id_GostR3410_94_cc:
736                 case NID_id_GostR3410_94:
737                         EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost94);
738                         EVP_PKEY_asn1_set_private (*ameth, 
739                                 priv_decode_gost, priv_encode_gost, 
740                                 priv_print_gost);
741
742                         EVP_PKEY_asn1_set_param (*ameth, 0, 0,
743                                 param_missing_gost94, param_copy_gost94, 
744                                 param_cmp_gost94,0 );
745                         EVP_PKEY_asn1_set_public (*ameth,
746                                 pub_decode_gost94, pub_encode_gost94,
747                                 pub_cmp_gost94, pub_print_gost94,
748                                 pkey_size_gost, pkey_bits_gost);
749         
750                         EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
751                         break;
752                 case NID_id_GostR3410_2001_cc:
753                 case NID_id_GostR3410_2001:
754                         EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost01);
755                         EVP_PKEY_asn1_set_private (*ameth, 
756                                 priv_decode_gost, priv_encode_gost, 
757                                 priv_print_gost);
758
759                         EVP_PKEY_asn1_set_param (*ameth, 0, 0,
760                                 param_missing_gost01, param_copy_gost01, 
761                                 param_cmp_gost01, 0);
762                         EVP_PKEY_asn1_set_public (*ameth,
763                                 pub_decode_gost01, pub_encode_gost01,
764                                 pub_cmp_gost01, pub_print_gost01,
765                                 pkey_size_gost, pkey_bits_gost);
766         
767                         EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
768                         break;
769                 case NID_id_Gost28147_89_MAC:
770                         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
771                         EVP_PKEY_asn1_set_ctrl(*ameth,mac_ctrl_gost);   
772                         break;
773                 }               
774         return 1;
775         }