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