pkey_gost_mac_keygen: check for NULL after allocating keydata
[openssl.git] / engines / ccgost / gost_pmeth.c
1 /**********************************************************************
2  *                          gost_pmeth.c                              *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *   Implementation of RFC 4357 (GOST R 34.10) Publick key method     *
7  *       for OpenSSL                                                  *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <openssl/evp.h>
11 #include <openssl/objects.h>
12 #include <openssl/ec.h>
13 #include <openssl/x509v3.h> /*For string_to_hex */
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include "gost_params.h"
18 #include "gost_lcl.h"
19 #include "e_gost_err.h"
20 /*-------init, cleanup, copy - uniform for all algs  ---------------*/
21 /* Allocates new gost_pmeth_data structure and assigns it as data */
22 static int pkey_gost_init(EVP_PKEY_CTX *ctx)
23         {
24         struct gost_pmeth_data *data;
25         EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
26         data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
27         if (!data) return 0;
28         memset(data,0,sizeof(struct gost_pmeth_data));
29         if (pkey && EVP_PKEY_get0(pkey)) 
30                 {
31                 switch (EVP_PKEY_base_id(pkey)) {
32                 case NID_id_GostR3410_94:
33                   data->sign_param_nid = gost94_nid_by_params(EVP_PKEY_get0(pkey));
34                   break;
35                 case NID_id_GostR3410_2001:
36                    data->sign_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
37                 break;
38                 default:
39                         return 0;
40                 }         
41                 }
42         EVP_PKEY_CTX_set_data(ctx,data);
43         return 1;
44         }
45
46 /* Copies contents of gost_pmeth_data structure */
47 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
48         {
49         struct gost_pmeth_data *dst_data,*src_data;
50         if (!pkey_gost_init(dst))
51                 {
52                 return 0;
53                 }
54         src_data = EVP_PKEY_CTX_get_data(src);
55         dst_data = EVP_PKEY_CTX_get_data(dst);
56         *dst_data = *src_data;
57         if (src_data -> shared_ukm) {
58                 dst_data->shared_ukm=NULL;
59         }       
60         return 1;
61         }
62
63 /* Frees up gost_pmeth_data structure */
64 static void pkey_gost_cleanup (EVP_PKEY_CTX *ctx)
65         {
66         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
67         if (data->shared_ukm) OPENSSL_free(data->shared_ukm);
68         OPENSSL_free(data);
69         }       
70
71 /* --------------------- control functions  ------------------------------*/
72 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
73         {
74         struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
75         switch (type)
76                 {
77                 case EVP_PKEY_CTRL_MD:
78                 {
79                 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94)
80                         {
81                         GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
82                         return 0;
83                         }
84                 pctx->md = (EVP_MD *)p2;
85                 return 1;
86                 }
87                 break;
88
89                 case EVP_PKEY_CTRL_GET_MD:
90                 *(const EVP_MD **)p2 = pctx->md;
91                 return 1;
92
93                 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
94                 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
95                 case EVP_PKEY_CTRL_PKCS7_SIGN:
96                 case EVP_PKEY_CTRL_DIGESTINIT:
97 #ifndef OPENSSL_NO_CMS          
98                 case EVP_PKEY_CTRL_CMS_ENCRYPT:
99                 case EVP_PKEY_CTRL_CMS_DECRYPT:
100                 case EVP_PKEY_CTRL_CMS_SIGN:
101 #endif          
102                         return 1;
103
104                 case EVP_PKEY_CTRL_GOST_PARAMSET:
105                         pctx->sign_param_nid = (int)p1;
106                         return 1;
107                 case EVP_PKEY_CTRL_SET_IV:
108                         pctx->shared_ukm=OPENSSL_malloc((int)p1);
109                         if (pctx->shared_ukm == NULL)
110                                 {
111                                 GOSTerr(GOST_F_PKEY_GOST_CTRL, ERR_R_MALLOC_FAILURE);
112                                 return 0;
113                                 }
114                         memcpy(pctx->shared_ukm,p2,(int) p1);
115                         return 1;
116                 case EVP_PKEY_CTRL_PEER_KEY:
117                         if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
118                                 return 1;
119                         if (p1 == 2)            /* TLS: peer key used? */
120                                 return pctx->peer_key_used;
121                         if (p1 == 3)            /* TLS: peer key used! */
122                                 return (pctx->peer_key_used = 1);
123                         return -2;
124                 }
125         return -2;
126         }
127
128
129 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
130         const char *type, const char *value)
131         {
132         int param_nid=0;
133         if(!strcmp(type, param_ctrl_string))
134                 {
135                 if (!value)
136                         {
137                         return 0;
138                         }
139                 if (strlen(value) == 1)
140                         {
141                         switch(toupper((unsigned char)value[0]))
142                                 {
143                                 case 'A':
144                                         param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
145                                         break;
146                                 case 'B':
147                                         param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
148                                         break;
149                                 case 'C':
150                                         param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
151                                         break;
152                                 case 'D':
153                                         param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
154                                         break;
155                                 default:
156                                         return 0;
157                                         break;
158                                 }
159                         }
160                 else if ((strlen(value) == 2) && (toupper((unsigned char)value[0]) == 'X'))
161                         {
162                         switch (toupper((unsigned char)value[1]))
163                                 {
164                                 case 'A':
165                                         param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
166                                         break;
167                                 case 'B':
168                                         param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
169                                         break;
170                                 case 'C':
171                                         param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
172                                         break;
173                                 default:
174                                         return 0;
175                                         break;
176                                 }
177                         }
178                 else
179                         {
180                         R3410_params *p = R3410_paramset;
181                         param_nid = OBJ_txt2nid(value);
182                         if (param_nid == NID_undef)
183                                 {
184                                 return 0;
185                                 }
186                         for (;p->nid != NID_undef;p++)
187                                 {
188                                 if (p->nid == param_nid) break;
189                                 }
190                         if (p->nid == NID_undef)
191                                 {
192                                 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR,
193                                         GOST_R_INVALID_PARAMSET);
194                                 return 0;
195                                 }
196                         }
197
198                 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
199                         param_nid, NULL);
200                 }
201         return -2;
202         }
203
204 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
205         const char *type, const char *value)
206         {
207         int param_nid=0;
208         if(!strcmp(type, param_ctrl_string))
209                 {
210                 if (!value)
211                         {
212                         return 0;
213                         }
214                 if (strlen(value) == 1)
215                         {
216                         switch(toupper((unsigned char)value[0]))
217                                 {
218                                 case 'A':
219                                         param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
220                                         break;  
221                                 case 'B':
222                                         param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
223                                         break;
224                                 case 'C':
225                                         param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
226                                         break;
227                                 case '0':
228                                         param_nid = NID_id_GostR3410_2001_TestParamSet;
229                                         break;
230                                 default:
231                                         return 0;
232                                         break;
233                                 }
234                         }
235                 else if ((strlen(value) == 2) && (toupper((unsigned char)value[0]) == 'X'))
236                         {
237                         switch (toupper((unsigned char)value[1]))
238                                 {
239                                 case 'A':
240                                         param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
241                                         break;
242                                 case 'B':
243                                         param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
244                                         break;
245                                 default:
246                                         return 0;
247                                         break;
248                                 }
249                         }
250                 else
251                         {
252                         R3410_2001_params *p = R3410_2001_paramset;
253                         param_nid = OBJ_txt2nid(value);
254                         if (param_nid == NID_undef)
255                                 {
256                                 return 0;
257                                 }
258                         for (;p->nid != NID_undef;p++)
259                                 {
260                                 if (p->nid == param_nid) break;
261                                 }
262                         if (p->nid == NID_undef)
263                                 {
264                                 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR,
265                                         GOST_R_INVALID_PARAMSET);
266                                 return 0;
267                                 }
268                         }
269
270                 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
271                         param_nid, NULL);
272                 }
273         return -2;
274         }
275
276 /* --------------------- key generation  --------------------------------*/
277
278 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx) {
279         return 1;
280 }       
281 static int pkey_gost94_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) 
282         {
283         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
284         DSA *dsa=NULL;
285         if (data->sign_param_nid == NID_undef)
286                 {
287                         GOSTerr(GOST_F_PKEY_GOST94_PARAMGEN,
288                                 GOST_R_NO_PARAMETERS_SET);
289                         return 0;
290                 }
291         dsa = DSA_new();
292         if (!fill_GOST94_params(dsa,data->sign_param_nid))
293                 {
294                 DSA_free(dsa);
295                 return 0;
296                 }
297         EVP_PKEY_assign(pkey,NID_id_GostR3410_94,dsa);
298         return 1;
299         }
300 static int pkey_gost01_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
301         {
302         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
303         EC_KEY *ec=NULL;
304
305         if (data->sign_param_nid == NID_undef)
306                 {
307                         GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN,
308                                 GOST_R_NO_PARAMETERS_SET);
309                         return 0;
310                 }
311         if (!ec)        
312                 ec = EC_KEY_new();
313         if (!fill_GOST2001_params(ec,data->sign_param_nid))
314                 {
315                 EC_KEY_free(ec);
316                 return 0;
317                 }
318         EVP_PKEY_assign(pkey,NID_id_GostR3410_2001,ec);
319         return 1;
320         }
321
322 /* Generates Gost_R3410_94_cp key */
323 static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
324         {
325         DSA *dsa;
326         if (!pkey_gost94_paramgen(ctx,pkey)) return 0;
327         dsa = EVP_PKEY_get0(pkey);
328         gost_sign_keygen(dsa);
329         return 1;
330         }
331
332 /* Generates GOST_R3410 2001 key and assigns it using specified type */
333 static int pkey_gost01cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
334         {
335         EC_KEY *ec;
336     if (!pkey_gost01_paramgen(ctx,pkey)) return 0;
337         ec = EVP_PKEY_get0(pkey);
338         gost2001_keygen(ec);
339         return 1;
340         }
341
342
343
344 /* ----------- sign callbacks --------------------------------------*/
345
346 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
347         const unsigned char *tbs, size_t tbs_len)
348         {
349         DSA_SIG *unpacked_sig=NULL;
350         EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
351         if (!siglen) return 0;
352         if (!sig)
353                 {
354                 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
355                 return 1;
356                 }       
357         unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
358         if (!unpacked_sig)
359                 {
360                 return 0;
361                 }
362         return pack_sign_cp(unpacked_sig,32,sig,siglen);
363         }
364
365 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
366         const unsigned char *tbs, size_t tbs_len)
367         {
368         DSA_SIG *unpacked_sig=NULL;
369         EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
370         if (!siglen) return 0;
371         if (!sig)
372                 {
373                 *siglen= 64; /* better to check size of curve order*/
374                 return 1;
375                 }       
376         unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
377         if (!unpacked_sig)
378                 {
379                 return 0;
380                 }
381         return pack_sign_cp(unpacked_sig,32,sig,siglen);
382         }
383
384 /* ------------------- verify callbacks ---------------------------*/
385
386 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
387         size_t siglen, const unsigned char *tbs, size_t tbs_len)
388         {
389         int ok = 0;
390         EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
391         DSA_SIG *s=unpack_cp_signature(sig,siglen);
392         if (!s) return 0;
393         if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
394         DSA_SIG_free(s);
395         return ok;
396         }
397
398
399 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
400         size_t siglen, const unsigned char *tbs, size_t tbs_len)
401         {
402         int ok = 0;
403         EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
404         DSA_SIG *s=unpack_cp_signature(sig,siglen);
405         if (!s) return 0;
406 #ifdef DEBUG_SIGN       
407         fprintf(stderr,"R=");
408         BN_print_fp(stderr,s->r);
409         fprintf(stderr,"\nS=");
410         BN_print_fp(stderr,s->s);
411         fprintf(stderr,"\n");
412 #endif  
413         if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
414         DSA_SIG_free(s);
415         return ok;
416         }
417
418 /* ------------- encrypt init -------------------------------------*/
419 /* Generates ephermeral key */
420 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
421         {
422         return 1;
423         }
424 /* --------------- Derive init ------------------------------------*/
425 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
426 {
427         return 1;
428 }
429 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
430 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
431         {
432         struct gost_mac_pmeth_data *data;
433         data = OPENSSL_malloc(sizeof(struct gost_mac_pmeth_data));
434         if (!data) return 0;
435         memset(data,0,sizeof(struct gost_mac_pmeth_data));
436         EVP_PKEY_CTX_set_data(ctx,data);
437         return 1;
438         }       
439 static void pkey_gost_mac_cleanup (EVP_PKEY_CTX *ctx)
440         {
441         struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
442         OPENSSL_free(data);
443         }       
444 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
445         {
446         struct gost_mac_pmeth_data *dst_data,*src_data;
447         if (!pkey_gost_mac_init(dst))
448                 {
449                 return 0;
450                 }
451         src_data = EVP_PKEY_CTX_get_data(src);
452         dst_data = EVP_PKEY_CTX_get_data(dst);
453         *dst_data = *src_data;
454         return 1;
455         }
456         
457 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
458         {
459         struct gost_mac_pmeth_data *data =
460 (struct gost_mac_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
461
462         switch (type)
463                 {
464                 case EVP_PKEY_CTRL_MD:
465                 {
466                 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC)
467                         {
468                         GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_DIGEST_TYPE);
469                         return 0;
470                         }
471                 data->md = (EVP_MD *)p2;
472                 return 1;
473                 }
474                 break;
475
476                 case EVP_PKEY_CTRL_GET_MD:
477                 *(const EVP_MD **)p2 = data->md;
478                 return 1;
479
480                 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
481                 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
482                 case EVP_PKEY_CTRL_PKCS7_SIGN:
483                         return 1;
484                 case EVP_PKEY_CTRL_SET_MAC_KEY:
485                         if (p1 != 32) 
486                                 {
487                                 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
488                                         GOST_R_INVALID_MAC_KEY_LENGTH);
489                                 return 0;
490                                 }
491
492                         memcpy(data->key,p2,32);
493                         data->key_set = 1;
494                         return 1;
495                 case EVP_PKEY_CTRL_DIGESTINIT:
496                         { 
497                         EVP_MD_CTX *mctx = p2;
498                         void *key;
499                         if (!data->key_set)
500                                 { 
501                                 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
502                                 if (!pkey) 
503                                         {
504                                         GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
505                                         return 0;
506                                         }
507                                 key = EVP_PKEY_get0(pkey);
508                                 if (!key) 
509                                         {
510                                         GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
511                                         return 0;
512                                         }
513                                 } else {
514                                 key = &(data->key);
515                                 }
516                         return mctx->digest->md_ctrl(mctx,EVP_MD_CTRL_SET_KEY,32,key);
517                         }  
518                 }       
519         return -2;
520         }
521 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
522         const char *type, const char *value)
523         {
524         if (!strcmp(type, key_ctrl_string)) 
525                 {
526                 if (strlen(value)!=32) 
527                         {
528                         GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
529                                 GOST_R_INVALID_MAC_KEY_LENGTH);
530                         return 0;       
531                         }
532                 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
533                         32,(char *)value);
534                 }
535         if (!strcmp(type, hexkey_ctrl_string)) 
536                 {
537                         long keylen; int ret;
538                         unsigned char *keybuf=string_to_hex(value,&keylen);
539                         if (keylen != 32) 
540                                 {
541                                 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
542                                         GOST_R_INVALID_MAC_KEY_LENGTH);
543                                 OPENSSL_free(keybuf);
544                                 return 0;       
545                                 }
546                         ret= pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
547                                 32,keybuf);
548                         OPENSSL_free(keybuf);
549                         return ret;
550         
551                 }
552         return -2;
553         }       
554
555 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
556         {
557                 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
558                 unsigned char *keydata;
559                 if (!data->key_set) 
560                 {
561                         GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN,GOST_R_MAC_KEY_NOT_SET);
562                         return 0;
563                 }
564                 keydata = OPENSSL_malloc(32);
565                 if (keydata == NULL)
566                         return 0;
567                 memcpy(keydata,data->key,32);
568                 EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata);
569                 return 1;
570         }
571
572 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
573         {
574         return 1;
575 }
576
577 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, EVP_MD_CTX *mctx)
578         {
579                 unsigned int tmpsiglen=*siglen; /* for platforms where sizeof(int)!=sizeof(size_t)*/
580                 int ret;
581                 if (!sig) 
582                         {
583                         *siglen = 4;
584                         return 1;
585                         }
586                 ret=EVP_DigestFinal_ex(mctx,sig,&tmpsiglen);
587                 *siglen = tmpsiglen;
588                 return ret;
589         }
590 /* ----------------------------------------------------------------*/
591 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags)
592         {
593         *pmeth = EVP_PKEY_meth_new(id, flags);
594         if (!*pmeth) return 0;
595
596         switch (id)
597                 {
598                 case NID_id_GostR3410_94:
599                         EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str);
600                         EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen);
601                         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
602                         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
603                         EVP_PKEY_meth_set_encrypt(*pmeth,
604                                 pkey_gost_encrypt_init, pkey_GOST94cp_encrypt);
605                         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
606                         EVP_PKEY_meth_set_derive(*pmeth,
607                                 pkey_gost_derive_init, pkey_gost94_derive);
608                         EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,pkey_gost94_paramgen);       
609                         break;
610                 case NID_id_GostR3410_2001:
611                         EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str);
612                         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
613                         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
614
615                         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
616
617                         EVP_PKEY_meth_set_encrypt(*pmeth,
618                                 pkey_gost_encrypt_init, pkey_GOST01cp_encrypt);
619                         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
620                         EVP_PKEY_meth_set_derive(*pmeth,
621                                 pkey_gost_derive_init, pkey_gost2001_derive);
622                         EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,pkey_gost01_paramgen);       
623                         break;
624                 case NID_id_Gost28147_89_MAC:
625                         EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_mac_ctrl, pkey_gost_mac_ctrl_str);
626                         EVP_PKEY_meth_set_signctx(*pmeth,pkey_gost_mac_signctx_init, pkey_gost_mac_signctx);
627                         EVP_PKEY_meth_set_keygen(*pmeth,NULL, pkey_gost_mac_keygen);
628                         EVP_PKEY_meth_set_init(*pmeth,pkey_gost_mac_init);
629                         EVP_PKEY_meth_set_cleanup(*pmeth,pkey_gost_mac_cleanup);
630                         EVP_PKEY_meth_set_copy(*pmeth,pkey_gost_mac_copy);
631                         return 1;
632                 default: /*Unsupported method*/
633                         return 0;
634                 }
635         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
636         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
637
638         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
639         /*FIXME derive etc...*/
640         
641         return 1;
642         }
643