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