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