]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_pmeth.c
Don't forget to remove some temporary files
[openssl-gost/engine.git] / gost_pmeth.c
1 /**********************************************************************
2  *                          gost_pmeth.c                              *
3  *             Copyright (c) 2005-2013 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 1.0.0+ 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_lcl.h"
19 #include "e_gost_err.h"
20
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
28     data = OPENSSL_malloc(sizeof(*data));
29     if (!data)
30         return 0;
31     memset(data, 0, sizeof(*data));
32     if (pkey && EVP_PKEY_get0(pkey)) {
33                 int id =  (EVP_PKEY_base_id(pkey));
34         if (id == NID_id_GostR3410_2001 ||
35             id ==  NID_gost2012_256 ||
36             id == NID_gost2012_512)
37             {
38                 const EC_GROUP *group =
39                     EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey));
40                 if (group != NULL) {
41                     data->sign_param_nid = EC_GROUP_get_curve_name(group);
42                 }
43             }
44         else 
45                         {
46             OPENSSL_free(data);
47             return 0;
48         }
49     }
50     EVP_PKEY_CTX_set_data(ctx, data);
51     return 1;
52 }
53
54 /* Copies contents of gost_pmeth_data structure */
55 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
56 {
57     struct gost_pmeth_data *dst_data, *src_data;
58     if (!pkey_gost_init(dst)) {
59         return 0;
60     }
61     src_data = EVP_PKEY_CTX_get_data(src);
62     dst_data = EVP_PKEY_CTX_get_data(dst);
63     if (!src_data || !dst_data)
64         return 0;
65
66     *dst_data = *src_data;
67     if (src_data->shared_ukm) {
68         dst_data->shared_ukm = NULL;
69     }
70     return 1;
71 }
72
73 /* Frees up gost_pmeth_data structure */
74 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
75 {
76     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
77     if (!data)
78         return;
79     OPENSSL_free(data->shared_ukm);
80     OPENSSL_free(data);
81 }
82
83 /* --------------------- control functions  ------------------------------*/
84 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
85 {
86     struct gost_pmeth_data *pctx =
87         (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
88     if (pctx == NULL)
89         return 0;
90
91     switch (type) {
92     case EVP_PKEY_CTRL_MD:
93         {
94             EVP_PKEY *key = EVP_PKEY_CTX_get0_pkey(ctx);
95             int pkey_nid = (key == NULL) ? NID_undef : EVP_PKEY_base_id(key);
96
97             OPENSSL_assert(p2 != NULL);
98
99             int md_type = EVP_MD_type((const EVP_MD *)p2);
100             if (md_type == NID_id_GostR3411_94) {
101                 if (pkey_nid == NID_id_GostR3410_2001
102                     || pkey_nid == NID_id_GostR3410_94) {
103                     pctx->md = (EVP_MD *)p2;
104                     return 1;
105                 }
106                         } else if (md_type == NID_md_gost12_256) {
107                 if (pkey_nid == NID_gost2012_256) {
108                     pctx->md = (EVP_MD *)p2;
109                     return 1;
110                 }
111             } else if ( md_type == NID_md_gost12_512) {
112                 if (pkey_nid == NID_gost2012_512) {
113                     pctx->md = (EVP_MD *)p2;
114                     return 1;
115                 }
116             }
117
118             GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
119             return 0;
120         }
121 #ifdef EVP_PKEY_CTRL_GET_MD
122     case EVP_PKEY_CTRL_GET_MD:
123         *(const EVP_MD **)p2 = pctx->md;
124         return 1;
125 #endif
126     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
127     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
128     case EVP_PKEY_CTRL_PKCS7_SIGN:
129     case EVP_PKEY_CTRL_DIGESTINIT:
130 #ifndef OPENSSL_NO_CMS
131     case EVP_PKEY_CTRL_CMS_ENCRYPT:
132     case EVP_PKEY_CTRL_CMS_DECRYPT:
133     case EVP_PKEY_CTRL_CMS_SIGN:
134 #endif
135         return 1;
136
137     case EVP_PKEY_CTRL_GOST_PARAMSET:
138         pctx->sign_param_nid = (int)p1;
139         return 1;
140     case EVP_PKEY_CTRL_SET_IV:
141         OPENSSL_assert(p2 != NULL);
142         pctx->shared_ukm = OPENSSL_malloc((int)p1);
143         if (!pctx->shared_ukm)
144             return 0;
145         memcpy(pctx->shared_ukm, p2, (int)p1);
146         return 1;
147     case EVP_PKEY_CTRL_PEER_KEY:
148         if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
149             return 1;
150         if (p1 == 2)            /* TLS: peer key used? */
151             return pctx->peer_key_used;
152         if (p1 == 3)            /* TLS: peer key used! */
153             return (pctx->peer_key_used = 1);
154         break;
155     }
156
157     GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_CTRL_CALL_FAILED);
158     return -2;
159 }
160
161 static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx,
162                                      const char *type, const char *value)
163 {
164     int param_nid = 0;
165
166     if (strcmp(type, param_ctrl_string) == 0) {
167         if (!value) {
168             return 0;
169         }
170         if (strlen(value) == 1) {
171             switch (toupper((unsigned char)value[0])) {
172             case 'A':
173                 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
174                 break;
175             case 'B':
176                 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
177                 break;
178             case 'C':
179                 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
180                 break;
181             case '0':
182                 param_nid = NID_id_GostR3410_2001_TestParamSet;
183                 break;
184             default:
185                 return 0;
186             }
187         } else if ((strlen(value) == 2)
188                    && (toupper((unsigned char)value[0]) == 'X')) {
189             switch (toupper((unsigned char)value[1])) {
190             case 'A':
191                 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
192                 break;
193             case 'B':
194                 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
195                 break;
196             default:
197                 return 0;
198             }
199         } else {
200             R3410_ec_params *p = R3410_2001_paramset;
201             param_nid = OBJ_txt2nid(value);
202             if (param_nid == NID_undef) {
203                 return 0;
204             }
205             for (; p->nid != NID_undef; p++) {
206                 if (p->nid == param_nid)
207                     break;
208             }
209             if (p->nid == NID_undef) {
210                 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_256,
211                         GOST_R_INVALID_PARAMSET);
212                 return 0;
213             }
214         }
215
216         return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
217                               param_nid, NULL);
218     }
219     return -2;
220 }
221
222 static int pkey_gost_ec_ctrl_str_512(EVP_PKEY_CTX *ctx,
223                                      const char *type, const char *value)
224 {
225     int param_nid = NID_undef;
226
227     if (strcmp(type, param_ctrl_string))
228         return -2;
229
230     if (!value)
231         return 0;
232
233     if (strlen(value) == 1) {
234         switch (toupper((unsigned char)value[0])) {
235         case 'A':
236             param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
237             break;
238
239         case 'B':
240             param_nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
241             break;
242
243         default:
244             return 0;
245         }
246     } else {
247         R3410_ec_params *p = R3410_2012_512_paramset;
248         param_nid = OBJ_txt2nid(value);
249         if (param_nid == NID_undef)
250             return 0;
251
252         while (p->nid != NID_undef && p->nid != param_nid)
253             p++;
254
255         if (p->nid == NID_undef) {
256             GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_512,
257                     GOST_R_INVALID_PARAMSET);
258             return 0;
259         }
260     }
261
262     return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, param_nid, NULL);
263 }
264
265 /* --------------------- key generation  --------------------------------*/
266
267 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
268 {
269     return 1;
270 }
271
272 static int pkey_gost2001_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
273 {
274     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
275     EC_KEY *ec = NULL;
276
277     if (!data || data->sign_param_nid == NID_undef) {
278         GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
279         return 0;
280     }
281
282     ec = EC_KEY_new();
283     if (!fill_GOST_EC_params(ec, data->sign_param_nid)
284         || !EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec)) {
285         EC_KEY_free(ec);
286         return 0;
287     }
288     return 1;
289 }
290
291 static int pkey_gost2012_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
292 {
293     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
294     EC_KEY *ec;
295     int result = 0;
296
297     if (!data || data->sign_param_nid == NID_undef) {
298         GOSTerr(GOST_F_PKEY_GOST12_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
299         return 0;
300     }
301
302     ec = EC_KEY_new();
303     if (!fill_GOST_EC_params(ec, data->sign_param_nid)) {
304         EC_KEY_free(ec);
305         return 0;
306     }
307
308     if (data->sign_param_nid == NID_id_tc26_gost_3410_2012_512_paramSetA ||
309         data->sign_param_nid == NID_id_tc26_gost_3410_2012_512_paramSetB) {
310         result =
311             (EVP_PKEY_assign(pkey, NID_gost2012_512, ec)) ? 1 : 0;
312     } else 
313         if (data->sign_param_nid == NID_id_GostR3410_2001_CryptoPro_A_ParamSet ||
314     data->sign_param_nid == NID_id_GostR3410_2001_CryptoPro_B_ParamSet ||
315     data->sign_param_nid == NID_id_GostR3410_2001_CryptoPro_C_ParamSet ||
316     data->sign_param_nid == NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet ||
317     data->sign_param_nid == NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet ||
318     data->sign_param_nid == NID_id_GostR3410_2001_TestParamSet) {
319         result =
320             (EVP_PKEY_assign(pkey, NID_gost2012_256, ec)) ? 1 : 0;
321     }
322
323     if (result == 0)
324         EC_KEY_free(ec);
325
326     return result;
327 }
328
329 /* ----------- keygen callbacks --------------------------------------*/
330 /* Generates GOST_R3410 2001 key and assigns it using specified type */
331 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
332 {
333     EC_KEY *ec;
334     if (!pkey_gost2001_paramgen(ctx, pkey))
335         return 0;
336     ec = EVP_PKEY_get0(pkey);
337     gost_ec_keygen(ec);
338     return 1;
339 }
340
341 /* Generates GOST_R3410 2012 key and assigns it using specified type */
342 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
343 {
344     if (!pkey_gost2012_paramgen(ctx, pkey))
345         return 0;
346
347     gost_ec_keygen(EVP_PKEY_get0(pkey));
348     return 1;
349 }
350
351 /* ----------- sign callbacks --------------------------------------*/
352 /*
353  * Packs signature according to Cryptopro rules
354  * and frees up DSA_SIG structure
355  */
356 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
357 {
358     *siglen = 2 * order;
359     memset(sig, 0, *siglen);
360     store_bignum(s->s, sig, order);
361     store_bignum(s->r, sig + order, order);
362     DSA_SIG_free(s);
363     return 1;
364 }
365
366 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
367                                 size_t *siglen, const unsigned char *tbs,
368                                 size_t tbs_len)
369 {
370     DSA_SIG *unpacked_sig = NULL;
371     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
372     int order = 0;
373         int id = EVP_PKEY_base_id(pkey);
374     if (!siglen)
375         return 0;
376     if (!pkey)
377         return 0;
378
379     if (id == NID_id_GostR3410_2001 ||
380         id == NID_gost2012_256) {
381         order = 64;
382     } else if (id == NID_gost2012_512) {
383         order = 128;
384     } else {
385         return 0;
386     }
387
388     if (!sig) {
389         *siglen = order;
390         return 1;
391     }
392     unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
393     if (!unpacked_sig) {
394         return 0;
395     }
396     return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
397 }
398
399 /* ------------------- verify callbacks ---------------------------*/
400 /* Unpack signature according to cryptopro rules  */
401 DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
402 {
403     DSA_SIG *s;
404
405     s = DSA_SIG_new();
406     if (s == NULL) {
407         GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
408         return NULL;
409     }
410     s->s = BN_bin2bn(sig, siglen / 2, NULL);
411     s->r = BN_bin2bn(sig + siglen / 2, siglen / 2, NULL);
412     return s;
413 }
414
415 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
416                                   size_t siglen, const unsigned char *tbs,
417                                   size_t tbs_len)
418 {
419     int ok = 0;
420     EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
421     DSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
422     if (!s)
423         return 0;
424 #ifdef DEBUG_SIGN
425     fprintf(stderr, "R=");
426     BN_print_fp(stderr, s->r);
427     fprintf(stderr, "\nS=");
428     BN_print_fp(stderr, s->s);
429     fprintf(stderr, "\n");
430 #endif
431     if (pub_key)
432         ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
433     DSA_SIG_free(s);
434     return ok;
435 }
436
437 /* ------------- encrypt init -------------------------------------*/
438 /* Generates ephermeral key */
439 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
440 {
441     return 1;
442 }
443
444 /* --------------- Derive init ------------------------------------*/
445 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
446 {
447     return 1;
448 }
449
450 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
451 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
452 {
453     struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
454
455     if (!data)
456         return 0;
457     memset(data, 0, sizeof(*data));
458         data->mac_size = 4;
459     EVP_PKEY_CTX_set_data(ctx, data);
460     return 1;
461 }
462
463 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
464 {
465     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
466     if (data)
467         OPENSSL_free(data);
468 }
469
470 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
471 {
472     struct gost_mac_pmeth_data *dst_data, *src_data;
473     if (!pkey_gost_mac_init(dst)) {
474         return 0;
475     }
476     src_data = EVP_PKEY_CTX_get_data(src);
477     dst_data = EVP_PKEY_CTX_get_data(dst);
478     if (!src_data || !dst_data)
479         return 0;
480
481     *dst_data = *src_data;
482     return 1;
483 }
484
485 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
486 {
487     struct gost_mac_pmeth_data *data =
488         (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
489
490     switch (type) {
491     case EVP_PKEY_CTRL_MD:
492         {
493             int nid = EVP_MD_type((const EVP_MD *)p2);
494             if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
495                 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
496                         GOST_R_INVALID_DIGEST_TYPE);
497                 return 0;
498             }
499             data->md = (EVP_MD *)p2;
500             return 1;
501         }
502 #ifdef EVP_PKEY_CTRL_GET_MD
503     case EVP_PKEY_CTRL_GET_MD:
504         *(const EVP_MD **)p2 = data->md;
505         return 1;
506 #endif
507     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
508     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
509     case EVP_PKEY_CTRL_PKCS7_SIGN:
510         return 1;
511     case EVP_PKEY_CTRL_SET_MAC_KEY:
512         if (p1 != 32) {
513             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
514             return 0;
515         }
516
517         memcpy(data->key, p2, 32);
518         data->key_set = 1;
519         return 1;
520     case EVP_PKEY_CTRL_DIGESTINIT:
521         {
522             EVP_MD_CTX *mctx = p2;
523             void *key;
524             if (!data->key_set) {
525                 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
526                 if (!pkey) {
527                     GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
528                             GOST_R_MAC_KEY_NOT_SET);
529                     return 0;
530                 }
531                 key = EVP_PKEY_get0(pkey);
532                 if (!key) {
533                     GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
534                             GOST_R_MAC_KEY_NOT_SET);
535                     return 0;
536                 }
537             } else {
538                 key = &(data->key);
539             }
540             return mctx->digest->md_ctrl(mctx, EVP_MD_CTRL_SET_KEY, 32, key);
541         }
542         case EVP_PKEY_CTRL_MAC_LEN:     
543                 {
544                         if (p1<1 || p1>8)
545                                 {
546                                         
547                                         GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_INVALID_MAC_SIZE);
548                                         return 0;
549                                 }
550                                 data->mac_size = p1;
551                                 return 1;
552                 }
553     }
554     return -2;
555 }
556
557 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
558                                   const char *type, const char *value)
559 {
560     if (strcmp(type, key_ctrl_string) == 0) {
561         if (strlen(value) != 32) {
562             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
563                     GOST_R_INVALID_MAC_KEY_LENGTH);
564             return 0;
565         }
566         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
567                                   32, (char *)value);
568     }
569     if (strcmp(type, hexkey_ctrl_string) == 0) {
570         long keylen;
571         int ret;
572         unsigned char *keybuf = string_to_hex(value, &keylen);
573         if (!keybuf || keylen != 32) {
574             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
575                     GOST_R_INVALID_MAC_KEY_LENGTH);
576             OPENSSL_free(keybuf);
577             return 0;
578         }
579         ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
580         OPENSSL_free(keybuf);
581         return ret;
582
583     }
584         if (!strcmp(type,maclen_ctrl_string)) {
585                 char *endptr;
586                 long size=strtol(value,&endptr,10);
587                 if (*endptr!='\0') {
588                         GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
589                                    GOST_R_INVALID_MAC_SIZE);
590                         return 0;
591                 }
592                 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN,size,NULL);
593         }
594     return -2;
595 }
596
597 static int pkey_gost_mac_keygen_base(EVP_PKEY_CTX *ctx,
598                                      EVP_PKEY *pkey, int mac_nid)
599 {
600     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
601     unsigned char *keydata;
602     if (!data || !data->key_set) {
603         GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN, GOST_R_MAC_KEY_NOT_SET);
604         return 0;
605     }
606     keydata = OPENSSL_malloc(32);
607     if (keydata == NULL)
608         return 0;
609     memcpy(keydata, data->key, 32);
610     EVP_PKEY_assign(pkey, mac_nid, keydata);
611     return 1;
612 }
613
614 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
615 {
616     return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
617 }
618
619 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
620 {
621     return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
622 }
623
624 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
625 {
626     return 1;
627 }
628
629 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
630                                  size_t *siglen, EVP_MD_CTX *mctx)
631 {
632     unsigned int tmpsiglen;
633     int ret;
634         struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
635
636     if (!siglen)
637         return 0;
638     tmpsiglen = *siglen;        /* for platforms where sizeof(int) !=
639                                  * sizeof(size_t) */
640
641     if (!sig) {
642         *siglen = data->mac_size;
643         return 1;
644     }
645
646         mctx->digest->md_ctrl(mctx, EVP_MD_CTRL_MAC_LEN, data->mac_size, NULL);
647     ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
648     *siglen = data->mac_size;
649     return ret;
650 }
651
652 /* ----------------------------------------------------------------*/
653 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
654 {
655     *pmeth = EVP_PKEY_meth_new(id, flags);
656     if (!*pmeth)
657         return 0;
658
659     if (id == NID_id_GostR3410_2001) {
660         EVP_PKEY_meth_set_ctrl(*pmeth,
661                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
662         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
663         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
664
665         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
666
667         EVP_PKEY_meth_set_encrypt(*pmeth,
668                                   pkey_gost_encrypt_init,
669                                   pkey_GOST_ECcp_encrypt);
670         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST_ECcp_decrypt);
671         EVP_PKEY_meth_set_derive(*pmeth,
672                                  pkey_gost_derive_init, pkey_gost_ec_derive);
673         EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
674                                    pkey_gost2001_paramgen);
675     } else if (id == NID_gost2012_256) {
676         EVP_PKEY_meth_set_ctrl(*pmeth,
677                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
678         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
679         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
680
681         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
682
683         EVP_PKEY_meth_set_encrypt(*pmeth,
684                                   pkey_gost_encrypt_init,
685                                   pkey_GOST_ECcp_encrypt);
686         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST_ECcp_decrypt);
687         EVP_PKEY_meth_set_derive(*pmeth,
688                                  pkey_gost_derive_init, pkey_gost_ec_derive);
689         EVP_PKEY_meth_set_paramgen(*pmeth,
690                                    pkey_gost_paramgen_init,
691                                    pkey_gost2012_paramgen);
692     } else if (id == NID_gost2012_512 ) {
693         EVP_PKEY_meth_set_ctrl(*pmeth,
694                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
695         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
696         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
697
698         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
699
700         EVP_PKEY_meth_set_encrypt(*pmeth,
701                                   pkey_gost_encrypt_init,
702                                   pkey_GOST_ECcp_encrypt);
703         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST_ECcp_decrypt);
704         EVP_PKEY_meth_set_derive(*pmeth,
705                                  pkey_gost_derive_init, pkey_gost_ec_derive);
706         EVP_PKEY_meth_set_paramgen(*pmeth,
707                                    pkey_gost_paramgen_init,
708                                    pkey_gost2012_paramgen);
709     } else if (id == NID_id_Gost28147_89_MAC) {
710         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
711                                pkey_gost_mac_ctrl_str);
712         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
713                                   pkey_gost_mac_signctx);
714         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
715         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
716         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
717         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
718                 return 1;
719     } else if (id == NID_gost_mac_12) {
720         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
721                                pkey_gost_mac_ctrl_str);
722         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
723                                   pkey_gost_mac_signctx);
724         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
725         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
726         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
727         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
728                 return 1;
729     } else {
730        /* Unsupported method */
731         return 0;
732     }
733     EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
734     EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
735
736     EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
737     /*
738      * FIXME derive etc...
739      */
740
741     return 1;
742 }