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