]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_pmeth.c
Merge branch 'master' of https://github.com/gost-engine/engine
[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 <openssl/opensslv.h>   /* For OPENSSL_VERSION_MAJOR */
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include "gost_lcl.h"
20 #include "e_gost_err.h"
21
22 #define ossl3_const
23 #ifdef OPENSSL_VERSION_MAJOR
24 #undef ossl3_const
25 #define ossl3_const const
26 #endif
27
28 /* -----init, cleanup, copy - uniform for all algs  --------------*/
29 /* Allocates new gost_pmeth_data structure and assigns it as data */
30 static int pkey_gost_init(EVP_PKEY_CTX *ctx)
31 {
32     struct gost_pmeth_data *data;
33     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
34
35     data = OPENSSL_malloc(sizeof(*data));
36     if (!data)
37         return 0;
38     memset(data, 0, sizeof(*data));
39     if (pkey && EVP_PKEY_get0(pkey)) {
40         switch (EVP_PKEY_base_id(pkey)) {
41         case NID_id_GostR3410_2001:
42         case NID_id_GostR3410_2012_256:
43         case NID_id_GostR3410_2012_512:
44             {
45                 const EC_GROUP *group =
46                     EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey));
47                 if (group != NULL) {
48                     data->sign_param_nid = EC_GROUP_get_curve_name(group);
49                     break;
50                 }
51                 /* else */
52             }
53         default:
54             OPENSSL_free(data);
55             return 0;
56         }
57     }
58     EVP_PKEY_CTX_set_data(ctx, data);
59     return 1;
60 }
61
62 /* Copies contents of gost_pmeth_data structure */
63 static int pkey_gost_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
64 {
65     struct gost_pmeth_data *dst_data, *src_data;
66     if (!pkey_gost_init(dst)) {
67         return 0;
68     }
69     src_data = EVP_PKEY_CTX_get_data(src);
70     dst_data = EVP_PKEY_CTX_get_data(dst);
71     if (!src_data || !dst_data)
72         return 0;
73
74     *dst_data = *src_data;
75     if (src_data->shared_ukm) {
76         dst_data->shared_ukm = NULL;
77     }
78     return 1;
79 }
80
81 /* Frees up gost_pmeth_data structure */
82 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
83 {
84     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
85     if (!data)
86         return;
87     OPENSSL_free(data->shared_ukm);
88     OPENSSL_free(data);
89 }
90
91 /* --------------------- control functions  ------------------------------*/
92 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
93 {
94     struct gost_pmeth_data *pctx =
95         (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
96     if (pctx == NULL)
97         return 0;
98
99     switch (type) {
100     case EVP_PKEY_CTRL_MD:
101         {
102             EVP_PKEY *key = EVP_PKEY_CTX_get0_pkey(ctx);
103             int pkey_nid = (key == NULL) ? NID_undef : EVP_PKEY_base_id(key);
104
105             OPENSSL_assert(p2 != NULL);
106
107             switch (EVP_MD_type((const EVP_MD *)p2)) {
108             case NID_id_GostR3411_94:
109                 if (pkey_nid == NID_id_GostR3410_2001
110                     || pkey_nid == NID_id_GostR3410_94) {
111                     pctx->md = (EVP_MD *)p2;
112                     return 1;
113                 }
114                 break;
115
116             case NID_id_GostR3411_2012_256:
117                 if (pkey_nid == NID_id_GostR3410_2012_256) {
118                     pctx->md = (EVP_MD *)p2;
119                     return 1;
120                 }
121                 break;
122
123             case NID_id_GostR3411_2012_512:
124                 if (pkey_nid == NID_id_GostR3410_2012_512) {
125                     pctx->md = (EVP_MD *)p2;
126                     return 1;
127                 }
128                 break;
129             }
130
131             GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
132             return 0;
133         }
134
135     case EVP_PKEY_CTRL_GET_MD:
136         *(const EVP_MD **)p2 = pctx->md;
137         return 1;
138
139     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
140     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
141     case EVP_PKEY_CTRL_PKCS7_SIGN:
142     case EVP_PKEY_CTRL_DIGESTINIT:
143 #ifndef OPENSSL_NO_CMS
144     case EVP_PKEY_CTRL_CMS_ENCRYPT:
145     case EVP_PKEY_CTRL_CMS_DECRYPT:
146     case EVP_PKEY_CTRL_CMS_SIGN:
147 #endif
148         return 1;
149
150     case EVP_PKEY_CTRL_GOST_PARAMSET:
151         pctx->sign_param_nid = (int)p1;
152         return 1;
153     case EVP_PKEY_CTRL_SET_IV:
154         OPENSSL_assert(p2 != NULL);
155         pctx->shared_ukm = OPENSSL_malloc((int)p1);
156         if (pctx->shared_ukm == NULL) {
157             GOSTerr(GOST_F_PKEY_GOST_CTRL, ERR_R_MALLOC_FAILURE);
158             return 0;
159         }
160         memcpy(pctx->shared_ukm, p2, (int)p1);
161         pctx->shared_ukm_size = p1;
162         return 1;
163     case EVP_PKEY_CTRL_CIPHER:
164         pctx->cipher_nid = p1;
165         return 1;
166     case EVP_PKEY_CTRL_PEER_KEY:
167         if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
168             return 1;
169         if (p1 == 2)            /* TLS: peer key used? */
170             return pctx->peer_key_used;
171         if (p1 == 3)            /* TLS: peer key used! */
172             return (pctx->peer_key_used = 1);
173         break;
174     }
175
176     GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_CTRL_CALL_FAILED);
177     return -2;
178 }
179
180 static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx,
181                                      const char *type, const char *value)
182 {
183     int param_nid = 0;
184
185     if (strcmp(type, param_ctrl_string) == 0) {
186         if (!value) {
187             return 0;
188         }
189         if (strlen(value) == 1) {
190             switch (toupper((unsigned char)value[0])) {
191             case 'A':
192                 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
193                 break;
194             case 'B':
195                 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
196                 break;
197             case 'C':
198                 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
199                 break;
200             case '0':
201                 param_nid = NID_id_GostR3410_2001_TestParamSet;
202                 break;
203             default:
204                 return 0;
205             }
206         } else if ((strlen(value) == 2)
207                    && (toupper((unsigned char)value[0]) == 'X')) {
208             switch (toupper((unsigned char)value[1])) {
209             case 'A':
210                 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
211                 break;
212             case 'B':
213                 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
214                 break;
215             default:
216                 return 0;
217             }
218         } else if ((strlen(value) == 3)
219             && (toupper((unsigned char)value[0]) == 'T')
220             && (toupper((unsigned char)value[1]) == 'C')) {
221             switch (toupper((unsigned char)value[2])) {
222             case 'A':
223                 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetA;
224                 break;
225             case 'B':
226                 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetB;
227                 break;
228             case 'C':
229                 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetC;
230                 break;
231             case 'D':
232                 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetD;
233                 break;
234             default:
235                 return 0;
236             }
237         } else {
238             R3410_ec_params *p = R3410_2001_paramset;
239             param_nid = OBJ_txt2nid(value);
240             if (param_nid == NID_undef) {
241                 return 0;
242             }
243             for (; p->nid != NID_undef; p++) {
244                 if (p->nid == param_nid)
245                     break;
246             }
247             if (p->nid == NID_undef) {
248                 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_256,
249                         GOST_R_INVALID_PARAMSET);
250                 return 0;
251             }
252         }
253
254         return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
255                               param_nid, NULL);
256     }
257     return -2;
258 }
259
260 static int pkey_gost_ec_ctrl_str_512(EVP_PKEY_CTX *ctx,
261                                      const char *type, const char *value)
262 {
263     int param_nid = NID_undef;
264
265     if (strcmp(type, param_ctrl_string))
266         return -2;
267
268     if (!value)
269         return 0;
270
271     if (strlen(value) == 1) {
272         switch (toupper((unsigned char)value[0])) {
273         case 'A':
274             param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
275             break;
276
277         case 'B':
278             param_nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
279             break;
280
281         case 'C':
282             param_nid = NID_id_tc26_gost_3410_2012_512_paramSetC;
283             break;
284
285         default:
286             return 0;
287         }
288     } else {
289         R3410_ec_params *p = R3410_2012_512_paramset;
290         param_nid = OBJ_txt2nid(value);
291         if (param_nid == NID_undef)
292             return 0;
293
294         while (p->nid != NID_undef && p->nid != param_nid)
295             p++;
296
297         if (p->nid == NID_undef) {
298             GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_512,
299                     GOST_R_INVALID_PARAMSET);
300             return 0;
301         }
302     }
303
304     return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, param_nid, NULL);
305 }
306
307 /* --------------------- key generation  --------------------------------*/
308
309 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
310 {
311     return 1;
312 }
313
314 static int pkey_gost2001_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
315 {
316     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
317     EC_KEY *ec = NULL;
318
319     if (!data || data->sign_param_nid == NID_undef) {
320         GOSTerr(GOST_F_PKEY_GOST2001_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
321         return 0;
322     }
323
324     ec = EC_KEY_new();
325     if (!fill_GOST_EC_params(ec, data->sign_param_nid)
326         || !EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec)) {
327         EC_KEY_free(ec);
328         return 0;
329     }
330     return 1;
331 }
332
333 static int pkey_gost2012_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
334 {
335     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
336     EC_KEY *ec;
337     int result = 0;
338
339     if (!data || data->sign_param_nid == NID_undef) {
340         GOSTerr(GOST_F_PKEY_GOST2012_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
341         return 0;
342     }
343
344     ec = EC_KEY_new();
345     if (!fill_GOST_EC_params(ec, data->sign_param_nid)) {
346         EC_KEY_free(ec);
347         return 0;
348     }
349
350     switch (data->sign_param_nid) {
351     case NID_id_tc26_gost_3410_2012_512_paramSetA:
352     case NID_id_tc26_gost_3410_2012_512_paramSetB:
353     case NID_id_tc26_gost_3410_2012_512_paramSetC:
354     case NID_id_tc26_gost_3410_2012_512_paramSetTest:
355         result =
356             (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_512, ec)) ? 1 : 0;
357         break;
358
359     case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
360     case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
361     case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
362     case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
363     case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
364     case NID_id_GostR3410_2001_TestParamSet:
365     case NID_id_tc26_gost_3410_2012_256_paramSetA:
366     case NID_id_tc26_gost_3410_2012_256_paramSetB:
367     case NID_id_tc26_gost_3410_2012_256_paramSetC:
368     case NID_id_tc26_gost_3410_2012_256_paramSetD:
369         result =
370             (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_256, ec)) ? 1 : 0;
371         break;
372     default:
373         result = 0;
374         break;
375     }
376
377     if (result == 0)
378         EC_KEY_free(ec);
379
380     return result;
381 }
382
383 /* ----------- keygen callbacks --------------------------------------*/
384 /* Generates GOST_R3410 2001 key and assigns it using specified type */
385 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
386 {
387     EC_KEY *ec;
388     if (!pkey_gost2001_paramgen(ctx, pkey))
389         return 0;
390     ec = EVP_PKEY_get0(pkey);
391     gost_ec_keygen(ec);
392     return 1;
393 }
394
395 /* Generates GOST_R3410 2012 key and assigns it using specified type */
396 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
397 {
398     if (!pkey_gost2012_paramgen(ctx, pkey))
399         return 0;
400
401     gost_ec_keygen(EVP_PKEY_get0(pkey));
402     return 1;
403 }
404
405 /* ----------- sign callbacks --------------------------------------*/
406 /*
407  * Packs signature according to Cryptopro rules
408  * and frees up ECDSA_SIG structure
409  */
410 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
411 {
412     const BIGNUM *sig_r = NULL, *sig_s = NULL;
413     ECDSA_SIG_get0(s, &sig_r, &sig_s);
414     *siglen = 2 * order;
415     memset(sig, 0, *siglen);
416     store_bignum(sig_s, sig, order);
417     store_bignum(sig_r, sig + order, order);
418     ECDSA_SIG_free(s);
419     return 1;
420 }
421
422 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
423                                 size_t *siglen, const unsigned char *tbs,
424                                 size_t tbs_len)
425 {
426     ECDSA_SIG *unpacked_sig = NULL;
427     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
428     int order = 0;
429
430     if (!siglen)
431         return 0;
432     if (!pkey)
433         return 0;
434
435     switch (EVP_PKEY_base_id(pkey)) {
436     case NID_id_GostR3410_2001:
437     case NID_id_GostR3410_2012_256:
438         order = 64;
439         break;
440     case NID_id_GostR3410_2012_512:
441         order = 128;
442         break;
443     default:
444         return 0;
445     }
446
447     if (!sig) {
448         *siglen = order;
449         return 1;
450     }
451     unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
452     if (!unpacked_sig) {
453         return 0;
454     }
455     return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
456 }
457
458 /* ------------------- verify callbacks ---------------------------*/
459 /* Unpack signature according to cryptopro rules  */
460 ECDSA_SIG *unpack_cp_signature(const unsigned char *sigbuf, size_t siglen)
461 {
462     ECDSA_SIG *sig;
463     BIGNUM *r = NULL, *s = NULL;
464
465     sig = ECDSA_SIG_new();
466     if (sig == NULL) {
467         GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
468         return NULL;
469     }
470     s = BN_bin2bn(sigbuf, siglen / 2, NULL);
471     r = BN_bin2bn(sigbuf + siglen / 2, siglen / 2, NULL);
472                 ECDSA_SIG_set0(sig, r, s);
473     return sig;
474 }
475
476 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
477                                   size_t siglen, const unsigned char *tbs,
478                                   size_t tbs_len)
479 {
480     int ok = 0;
481     EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
482     ECDSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
483     if (!s)
484         return 0;
485 #ifdef DEBUG_SIGN
486     fprintf(stderr, "R=");
487     BN_print_fp(stderr, ECDSA_SIG_get0_r(s));
488     fprintf(stderr, "\nS=");
489     BN_print_fp(stderr, ECDSA_SIG_get0_s(s));
490     fprintf(stderr, "\n");
491 #endif
492     if (pub_key)
493         ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
494     ECDSA_SIG_free(s);
495     return ok;
496 }
497
498 /* ------------- encrypt init -------------------------------------*/
499 /* Generates ephermeral key */
500 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
501 {
502     return 1;
503 }
504
505 /* --------------- Derive init ------------------------------------*/
506 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
507 {
508     return 1;
509 }
510
511 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
512 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
513 {
514     struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
515     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
516
517     if (!data)
518         return 0;
519     memset(data, 0, sizeof(*data));
520     data->mac_size = 4;
521     data->mac_param_nid = NID_undef;
522
523     if (pkey) {
524         struct gost_mac_key *key = EVP_PKEY_get0(pkey);
525         if (key) {
526             data->mac_param_nid = key->mac_param_nid;
527             data->mac_size = key->mac_size;
528         }
529     }
530
531     EVP_PKEY_CTX_set_data(ctx, data);
532     return 1;
533 }
534
535 static int pkey_gost_omac_init(EVP_PKEY_CTX *ctx, size_t mac_size)
536 {
537     struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
538     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
539
540     if (!data)
541         return 0;
542     memset(data, 0, sizeof(*data));
543     data->mac_size = mac_size;
544     data->mac_param_nid = NID_undef;
545
546     if (pkey) {
547         struct gost_mac_key *key = EVP_PKEY_get0(pkey);
548         if (key) {
549             data->mac_param_nid = key->mac_param_nid;
550             data->mac_size = key->mac_size;
551         }
552     }
553
554     EVP_PKEY_CTX_set_data(ctx, data);
555     return 1;
556 }
557
558 static int pkey_gost_magma_mac_init(EVP_PKEY_CTX *ctx)
559 {
560         return pkey_gost_omac_init(ctx, 8);
561 }
562
563 static int pkey_gost_grasshopper_mac_init(EVP_PKEY_CTX *ctx)
564 {
565         return pkey_gost_omac_init(ctx, 16);
566 }
567
568 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
569 {
570     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
571     if (data)
572         OPENSSL_free(data);
573 }
574
575 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
576 {
577     struct gost_mac_pmeth_data *dst_data, *src_data;
578     if (!pkey_gost_mac_init(dst)) {
579         return 0;
580     }
581     src_data = EVP_PKEY_CTX_get_data(src);
582     dst_data = EVP_PKEY_CTX_get_data(dst);
583     if (!src_data || !dst_data)
584         return 0;
585
586     *dst_data = *src_data;
587     return 1;
588 }
589
590 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
591 {
592     struct gost_mac_pmeth_data *data =
593         (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
594
595     switch (type) {
596     case EVP_PKEY_CTRL_MD:
597         {
598             int nid = EVP_MD_type((const EVP_MD *)p2);
599             if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
600                 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
601                         GOST_R_INVALID_DIGEST_TYPE);
602                 return 0;
603             }
604             data->md = (EVP_MD *)p2;
605             return 1;
606         }
607
608     case EVP_PKEY_CTRL_GET_MD:
609         *(const EVP_MD **)p2 = data->md;
610         return 1;
611
612     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
613     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
614     case EVP_PKEY_CTRL_PKCS7_SIGN:
615         return 1;
616     case EVP_PKEY_CTRL_SET_MAC_KEY:
617         if (p1 != 32) {
618             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
619             return 0;
620         }
621
622         memcpy(data->key, p2, 32);
623         data->key_set = 1;
624         return 1;
625     case EVP_PKEY_CTRL_GOST_PARAMSET:
626         {
627             struct gost_cipher_info *param = p2;
628             data->mac_param_nid = param->nid;
629             return 1;
630         }
631     case EVP_PKEY_CTRL_DIGESTINIT:
632         {
633             EVP_MD_CTX *mctx = p2;
634             struct gost_mac_key *key;
635             if (!data->key_set) {
636                 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
637                 if (!pkey) {
638                     GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
639                             GOST_R_MAC_KEY_NOT_SET);
640                     return 0;
641                 }
642                 key = EVP_PKEY_get0(pkey);
643                 if (!key) {
644                     GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
645                             GOST_R_MAC_KEY_NOT_SET);
646                     return 0;
647                 }
648                 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
649                     (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
650             } else {
651                 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
652                     (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
653             }
654         }
655     case EVP_PKEY_CTRL_MAC_LEN:
656         {
657             if (p1 < 1 || p1 > 8) {
658
659                 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_SIZE);
660                 return 0;
661             }
662             data->mac_size = p1;
663             return 1;
664         }
665     }
666     return -2;
667 }
668
669 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
670                                   const char *type, const char *value)
671 {
672     if (strcmp(type, key_ctrl_string) == 0) {
673         if (strlen(value) != 32) {
674             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
675                     GOST_R_INVALID_MAC_KEY_LENGTH);
676             return 0;
677         }
678         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
679                                   32, (char *)value);
680     }
681     if (strcmp(type, hexkey_ctrl_string) == 0) {
682         long keylen;
683         int ret;
684         unsigned char *keybuf = string_to_hex(value, &keylen);
685         if (!keybuf || keylen != 32) {
686             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
687                     GOST_R_INVALID_MAC_KEY_LENGTH);
688             OPENSSL_free(keybuf);
689             return 0;
690         }
691         ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
692         OPENSSL_free(keybuf);
693         return ret;
694
695     }
696     if (!strcmp(type, maclen_ctrl_string)) {
697         char *endptr;
698         long size = strtol(value, &endptr, 10);
699         if (*endptr != '\0') {
700             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
701             return 0;
702         }
703         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL);
704     }
705     if (strcmp(type, param_ctrl_string) == 0) {
706         ASN1_OBJECT *obj = OBJ_txt2obj(value, 0);
707         const struct gost_cipher_info *param = NULL;
708         if (obj == NULL) {
709             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
710             return 0;
711         }
712
713         param = get_encryption_params(obj);
714                                 ASN1_OBJECT_free(obj);
715         if (param == NULL) {
716             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
717             return 0;
718         }
719
720
721         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 0,
722                                   (void *)param);
723     }
724     return -2;
725 }
726
727 static int pkey_gost_omac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2, size_t max_size)
728 {
729     struct gost_mac_pmeth_data *data =
730         (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
731
732     switch (type) {
733     case EVP_PKEY_CTRL_MD:
734         {
735             int nid = EVP_MD_type((const EVP_MD *)p2);
736             if (nid != NID_magma_mac && nid != NID_grasshopper_mac
737                 && nid != NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac /* FIXME beldmit */
738                 && nid != NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac) {
739                 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
740                         GOST_R_INVALID_DIGEST_TYPE);
741                 return 0;
742             }
743             data->md = (EVP_MD *)p2;
744             return 1;
745         }
746
747     case EVP_PKEY_CTRL_GET_MD:
748         *(const EVP_MD **)p2 = data->md;
749         return 1;
750
751     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
752     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
753     case EVP_PKEY_CTRL_PKCS7_SIGN:
754         return 1;
755     case EVP_PKEY_CTRL_SET_MAC_KEY:
756         if (p1 != 32) {
757             GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
758             return 0;
759         }
760
761         memcpy(data->key, p2, 32);
762         data->key_set = 1;
763         return 1;
764     case EVP_PKEY_CTRL_DIGESTINIT:
765         {
766             EVP_MD_CTX *mctx = p2;
767             struct gost_mac_key *key;
768             if (!data->key_set) {
769                 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
770                 if (!pkey) {
771                     GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
772                             GOST_R_MAC_KEY_NOT_SET);
773                     return 0;
774                 }
775                 key = EVP_PKEY_get0(pkey);
776                 if (!key) {
777                     GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
778                             GOST_R_MAC_KEY_NOT_SET);
779                     return 0;
780                 }
781                 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
782                     (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
783             } else {
784                 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
785                     (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
786             }
787         }
788     case EVP_PKEY_CTRL_MAC_LEN:
789         {
790             if (p1 < 1 || p1 > max_size) {
791
792                 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_SIZE);
793                 return 0;
794             }
795             data->mac_size = p1;
796             return 1;
797         }
798     }
799     return -2;
800 }
801
802 static int pkey_gost_magma_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
803 {
804         return pkey_gost_omac_ctrl(ctx, type, p1, p2, 8);
805 }
806
807 static int pkey_gost_grasshopper_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
808 {
809         return pkey_gost_omac_ctrl(ctx, type, p1, p2, 16);
810 }
811
812 static int pkey_gost_omac_ctrl_str(EVP_PKEY_CTX *ctx,
813                                   const char *type, const char *value, size_t max_size)
814 {
815     if (strcmp(type, key_ctrl_string) == 0) {
816         if (strlen(value) != 32) {
817             GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
818                     GOST_R_INVALID_MAC_KEY_LENGTH);
819             return 0;
820         }
821         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
822                                   32, (char *)value);
823     }
824     if (strcmp(type, hexkey_ctrl_string) == 0) {
825         long keylen;
826         int ret;
827         unsigned char *keybuf = string_to_hex(value, &keylen);
828         if (!keybuf || keylen != 32) {
829             GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
830                     GOST_R_INVALID_MAC_KEY_LENGTH);
831             OPENSSL_free(keybuf);
832             return 0;
833         }
834         ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
835         OPENSSL_free(keybuf);
836         return ret;
837
838     }
839     if (!strcmp(type, maclen_ctrl_string)) {
840         char *endptr;
841         long size = strtol(value, &endptr, 10);
842         if (*endptr != '\0') {
843             GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
844             return 0;
845         }
846         return pkey_gost_omac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL, max_size);
847     }
848     return -2;
849 }
850
851 static int pkey_gost_magma_mac_ctrl_str(EVP_PKEY_CTX *ctx,
852                                   const char *type, const char *value)
853 {
854         return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
855 }
856
857 static int pkey_gost_grasshopper_mac_ctrl_str(EVP_PKEY_CTX *ctx,
858                                   const char *type, const char *value)
859 {
860         return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
861 }
862
863 static int pkey_gost_mac_keygen_base(EVP_PKEY_CTX *ctx,
864                                      EVP_PKEY *pkey, int mac_nid)
865 {
866     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
867     struct gost_mac_key *keydata;
868     if (!data || !data->key_set) {
869         GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN_BASE, GOST_R_MAC_KEY_NOT_SET);
870         return 0;
871     }
872     keydata = OPENSSL_malloc(sizeof(struct gost_mac_key));
873     if (keydata == NULL)
874         return 0;
875     memcpy(keydata->key, data->key, 32);
876     keydata->mac_param_nid = data->mac_param_nid;
877     keydata->mac_size = data->mac_size;
878     EVP_PKEY_assign(pkey, mac_nid, keydata);
879     return 1;
880 }
881
882 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
883 {
884     return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
885 }
886
887 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
888 {
889     return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
890 }
891
892 static int pkey_gost_magma_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
893 {
894     return pkey_gost_mac_keygen_base(ctx, pkey, NID_magma_mac);
895 }
896
897 static int pkey_gost_grasshopper_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
898 {
899     return pkey_gost_mac_keygen_base(ctx, pkey, NID_grasshopper_mac);
900 }
901
902 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
903 {
904     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
905
906     if (data == NULL) {
907         pkey_gost_mac_init(ctx);
908     }
909
910     data = EVP_PKEY_CTX_get_data(ctx);
911     if (!data) {
912         GOSTerr(GOST_F_PKEY_GOST_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
913         return 0;
914     }
915
916     return 1;
917 }
918
919 static int pkey_gost_magma_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
920 {
921     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
922
923     if (data == NULL) {
924         pkey_gost_omac_init(ctx, 4);
925     }
926
927     data = EVP_PKEY_CTX_get_data(ctx);
928     if (!data) {
929         GOSTerr(GOST_F_PKEY_GOST_MAGMA_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
930         return 0;
931     }
932
933     return 1;
934 }
935
936 static int pkey_gost_grasshopper_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
937 {
938     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
939
940     if (data == NULL) {
941         pkey_gost_omac_init(ctx, 8);
942     }
943
944     data = EVP_PKEY_CTX_get_data(ctx);
945     if (!data) {
946         GOSTerr(GOST_F_PKEY_GOST_GRASSHOPPER_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
947         return 0;
948     }
949
950     return 1;
951 }
952
953 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
954                                  size_t *siglen, EVP_MD_CTX *mctx)
955 {
956     unsigned int tmpsiglen;
957     int ret;
958     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
959
960     if (!siglen)
961         return 0;
962     tmpsiglen = *siglen;        /* for platforms where sizeof(int) !=
963                                  * sizeof(size_t) */
964
965     if (!sig) {
966         *siglen = data->mac_size;
967         return 1;
968     }
969
970     EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
971         (mctx, EVP_MD_CTRL_XOF_LEN, data->mac_size, NULL);
972     ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
973     *siglen = data->mac_size;
974     return ret;
975 }
976
977 /* ----------- misc callbacks -------------------------------------*/
978
979 /* Callback for both EVP_PKEY_check() and EVP_PKEY_public_check. */
980 static int pkey_gost_check(EVP_PKEY *pkey)
981 {
982     return EC_KEY_check_key(EVP_PKEY_get0(pkey));
983 }
984
985 /* ----------------------------------------------------------------*/
986 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
987 {
988     *pmeth = EVP_PKEY_meth_new(id, flags);
989     if (!*pmeth)
990         return 0;
991
992     switch (id) {
993     case NID_id_GostR3410_2001:
994         EVP_PKEY_meth_set_ctrl(*pmeth,
995                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
996         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
997         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
998
999         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
1000
1001         EVP_PKEY_meth_set_encrypt(*pmeth,
1002                                   pkey_gost_encrypt_init,
1003                                   pkey_gost_encrypt);
1004         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1005         EVP_PKEY_meth_set_derive(*pmeth,
1006                                  pkey_gost_derive_init, pkey_gost_ec_derive);
1007         EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
1008                                    pkey_gost2001_paramgen);
1009         EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1010         EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1011         break;
1012     case NID_id_GostR3410_2012_256:
1013         EVP_PKEY_meth_set_ctrl(*pmeth,
1014                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1015         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1016         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1017
1018         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1019
1020         EVP_PKEY_meth_set_encrypt(*pmeth,
1021                                   pkey_gost_encrypt_init,
1022                                   pkey_gost_encrypt);
1023         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1024         EVP_PKEY_meth_set_derive(*pmeth,
1025                                  pkey_gost_derive_init, pkey_gost_ec_derive);
1026         EVP_PKEY_meth_set_paramgen(*pmeth,
1027                                    pkey_gost_paramgen_init,
1028                                    pkey_gost2012_paramgen);
1029         EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1030         EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1031         break;
1032     case NID_id_GostR3410_2012_512:
1033         EVP_PKEY_meth_set_ctrl(*pmeth,
1034                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
1035         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1036         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1037
1038         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1039
1040         EVP_PKEY_meth_set_encrypt(*pmeth,
1041                                   pkey_gost_encrypt_init,
1042                                   pkey_gost_encrypt);
1043         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1044         EVP_PKEY_meth_set_derive(*pmeth,
1045                                  pkey_gost_derive_init, pkey_gost_ec_derive);
1046         EVP_PKEY_meth_set_paramgen(*pmeth,
1047                                    pkey_gost_paramgen_init,
1048                                    pkey_gost2012_paramgen);
1049         EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1050         EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1051         break;
1052     case NID_id_Gost28147_89_MAC:
1053         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1054                                pkey_gost_mac_ctrl_str);
1055         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1056                                   pkey_gost_mac_signctx);
1057         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
1058         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1059         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1060         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1061         return 1;
1062     case NID_gost_mac_12:
1063         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1064                                pkey_gost_mac_ctrl_str);
1065         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1066                                   pkey_gost_mac_signctx);
1067         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
1068         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1069         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1070         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1071         return 1;
1072     case NID_magma_mac:
1073         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_magma_mac_ctrl,
1074                                pkey_gost_magma_mac_ctrl_str);
1075         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_magma_mac_signctx_init,
1076                                   pkey_gost_mac_signctx);
1077         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_magma_mac_keygen);
1078         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_magma_mac_init);
1079         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1080         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1081         return 1;
1082     case NID_grasshopper_mac:
1083     case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac: /* FIXME beldmit */
1084         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_grasshopper_mac_ctrl,
1085                                pkey_gost_grasshopper_mac_ctrl_str);
1086         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_grasshopper_mac_signctx_init,
1087                                   pkey_gost_mac_signctx);
1088         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_grasshopper_mac_keygen);
1089         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_grasshopper_mac_init);
1090         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1091         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1092         return 1;
1093     default:                   /* Unsupported method */
1094         return 0;
1095     }
1096     EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
1097     EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
1098
1099     EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
1100     /*
1101      * FIXME derive etc...
1102      */
1103
1104     return 1;
1105 }