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