]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_pmeth.c
Get rid of EVP_MD_CTRL_MAC_LEN
[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         result =
355             (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_512, ec)) ? 1 : 0;
356         break;
357
358     case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
359     case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
360     case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
361     case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
362     case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
363     case NID_id_GostR3410_2001_TestParamSet:
364     case NID_id_tc26_gost_3410_2012_256_paramSetA:
365     case NID_id_tc26_gost_3410_2012_256_paramSetB:
366     case NID_id_tc26_gost_3410_2012_256_paramSetC:
367     case NID_id_tc26_gost_3410_2012_256_paramSetD:
368         result =
369             (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_256, ec)) ? 1 : 0;
370         break;
371     default:
372         result = 0;
373         break;
374     }
375
376     if (result == 0)
377         EC_KEY_free(ec);
378
379     return result;
380 }
381
382 /* ----------- keygen callbacks --------------------------------------*/
383 /* Generates GOST_R3410 2001 key and assigns it using specified type */
384 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
385 {
386     EC_KEY *ec;
387     if (!pkey_gost2001_paramgen(ctx, pkey))
388         return 0;
389     ec = EVP_PKEY_get0(pkey);
390     gost_ec_keygen(ec);
391     return 1;
392 }
393
394 /* Generates GOST_R3410 2012 key and assigns it using specified type */
395 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
396 {
397     if (!pkey_gost2012_paramgen(ctx, pkey))
398         return 0;
399
400     gost_ec_keygen(EVP_PKEY_get0(pkey));
401     return 1;
402 }
403
404 /* ----------- sign callbacks --------------------------------------*/
405 /*
406  * Packs signature according to Cryptopro rules
407  * and frees up ECDSA_SIG structure
408  */
409 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
410 {
411     const BIGNUM *sig_r = NULL, *sig_s = NULL;
412     ECDSA_SIG_get0(s, &sig_r, &sig_s);
413     *siglen = 2 * order;
414     memset(sig, 0, *siglen);
415     store_bignum(sig_s, sig, order);
416     store_bignum(sig_r, sig + order, order);
417     ECDSA_SIG_free(s);
418     return 1;
419 }
420
421 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
422                                 size_t *siglen, const unsigned char *tbs,
423                                 size_t tbs_len)
424 {
425     ECDSA_SIG *unpacked_sig = NULL;
426     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
427     int order = 0;
428
429     if (!siglen)
430         return 0;
431     if (!pkey)
432         return 0;
433
434     switch (EVP_PKEY_base_id(pkey)) {
435     case NID_id_GostR3410_2001:
436     case NID_id_GostR3410_2012_256:
437         order = 64;
438         break;
439     case NID_id_GostR3410_2012_512:
440         order = 128;
441         break;
442     default:
443         return 0;
444     }
445
446     if (!sig) {
447         *siglen = order;
448         return 1;
449     }
450     unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
451     if (!unpacked_sig) {
452         return 0;
453     }
454     return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
455 }
456
457 /* ------------------- verify callbacks ---------------------------*/
458 /* Unpack signature according to cryptopro rules  */
459 ECDSA_SIG *unpack_cp_signature(const unsigned char *sigbuf, size_t siglen)
460 {
461     ECDSA_SIG *sig;
462     BIGNUM *r = NULL, *s = NULL;
463
464     sig = ECDSA_SIG_new();
465     if (sig == NULL) {
466         GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
467         return NULL;
468     }
469     s = BN_bin2bn(sigbuf, siglen / 2, NULL);
470     r = BN_bin2bn(sigbuf + siglen / 2, siglen / 2, NULL);
471                 ECDSA_SIG_set0(sig, r, s);
472     return sig;
473 }
474
475 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
476                                   size_t siglen, const unsigned char *tbs,
477                                   size_t tbs_len)
478 {
479     int ok = 0;
480     EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
481     ECDSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
482     if (!s)
483         return 0;
484 #ifdef DEBUG_SIGN
485     fprintf(stderr, "R=");
486     BN_print_fp(stderr, ECDSA_SIG_get0_r(s));
487     fprintf(stderr, "\nS=");
488     BN_print_fp(stderr, ECDSA_SIG_get0_s(s));
489     fprintf(stderr, "\n");
490 #endif
491     if (pub_key)
492         ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
493     ECDSA_SIG_free(s);
494     return ok;
495 }
496
497 /* ------------- encrypt init -------------------------------------*/
498 /* Generates ephermeral key */
499 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
500 {
501     return 1;
502 }
503
504 /* --------------- Derive init ------------------------------------*/
505 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
506 {
507     return 1;
508 }
509
510 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
511 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
512 {
513     struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
514     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
515
516     if (!data)
517         return 0;
518     memset(data, 0, sizeof(*data));
519     data->mac_size = 4;
520     data->mac_param_nid = NID_undef;
521
522     if (pkey) {
523         struct gost_mac_key *key = EVP_PKEY_get0(pkey);
524         if (key) {
525             data->mac_param_nid = key->mac_param_nid;
526             data->mac_size = key->mac_size;
527         }
528     }
529
530     EVP_PKEY_CTX_set_data(ctx, data);
531     return 1;
532 }
533
534 static int pkey_gost_omac_init(EVP_PKEY_CTX *ctx, size_t mac_size)
535 {
536     struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
537     EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
538
539     if (!data)
540         return 0;
541     memset(data, 0, sizeof(*data));
542     data->mac_size = mac_size;
543     data->mac_param_nid = NID_undef;
544
545     if (pkey) {
546         struct gost_mac_key *key = EVP_PKEY_get0(pkey);
547         if (key) {
548             data->mac_param_nid = key->mac_param_nid;
549             data->mac_size = key->mac_size;
550         }
551     }
552
553     EVP_PKEY_CTX_set_data(ctx, data);
554     return 1;
555 }
556
557 static int pkey_gost_magma_mac_init(EVP_PKEY_CTX *ctx)
558 {
559         return pkey_gost_omac_init(ctx, 8);
560 }
561
562 static int pkey_gost_grasshopper_mac_init(EVP_PKEY_CTX *ctx)
563 {
564         return pkey_gost_omac_init(ctx, 16);
565 }
566
567 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
568 {
569     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
570     if (data)
571         OPENSSL_free(data);
572 }
573
574 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
575 {
576     struct gost_mac_pmeth_data *dst_data, *src_data;
577     if (!pkey_gost_mac_init(dst)) {
578         return 0;
579     }
580     src_data = EVP_PKEY_CTX_get_data(src);
581     dst_data = EVP_PKEY_CTX_get_data(dst);
582     if (!src_data || !dst_data)
583         return 0;
584
585     *dst_data = *src_data;
586     return 1;
587 }
588
589 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
590 {
591     struct gost_mac_pmeth_data *data =
592         (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
593
594     switch (type) {
595     case EVP_PKEY_CTRL_MD:
596         {
597             int nid = EVP_MD_type((const EVP_MD *)p2);
598             if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
599                 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
600                         GOST_R_INVALID_DIGEST_TYPE);
601                 return 0;
602             }
603             data->md = (EVP_MD *)p2;
604             return 1;
605         }
606
607     case EVP_PKEY_CTRL_GET_MD:
608         *(const EVP_MD **)p2 = data->md;
609         return 1;
610
611     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
612     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
613     case EVP_PKEY_CTRL_PKCS7_SIGN:
614         return 1;
615     case EVP_PKEY_CTRL_SET_MAC_KEY:
616         if (p1 != 32) {
617             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
618             return 0;
619         }
620
621         memcpy(data->key, p2, 32);
622         data->key_set = 1;
623         return 1;
624     case EVP_PKEY_CTRL_GOST_PARAMSET:
625         {
626             struct gost_cipher_info *param = p2;
627             data->mac_param_nid = param->nid;
628             return 1;
629         }
630     case EVP_PKEY_CTRL_DIGESTINIT:
631         {
632             EVP_MD_CTX *mctx = p2;
633             struct gost_mac_key *key;
634             if (!data->key_set) {
635                 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
636                 if (!pkey) {
637                     GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
638                             GOST_R_MAC_KEY_NOT_SET);
639                     return 0;
640                 }
641                 key = EVP_PKEY_get0(pkey);
642                 if (!key) {
643                     GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
644                             GOST_R_MAC_KEY_NOT_SET);
645                     return 0;
646                 }
647                 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
648                     (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
649             } else {
650                 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
651                     (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
652             }
653         }
654     case EVP_PKEY_CTRL_MAC_LEN:
655         {
656             if (p1 < 1 || p1 > 8) {
657
658                 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_SIZE);
659                 return 0;
660             }
661             data->mac_size = p1;
662             return 1;
663         }
664     }
665     return -2;
666 }
667
668 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
669                                   const char *type, const char *value)
670 {
671     if (strcmp(type, key_ctrl_string) == 0) {
672         if (strlen(value) != 32) {
673             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
674                     GOST_R_INVALID_MAC_KEY_LENGTH);
675             return 0;
676         }
677         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
678                                   32, (char *)value);
679     }
680     if (strcmp(type, hexkey_ctrl_string) == 0) {
681         long keylen;
682         int ret;
683         unsigned char *keybuf = string_to_hex(value, &keylen);
684         if (!keybuf || keylen != 32) {
685             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
686                     GOST_R_INVALID_MAC_KEY_LENGTH);
687             OPENSSL_free(keybuf);
688             return 0;
689         }
690         ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
691         OPENSSL_free(keybuf);
692         return ret;
693
694     }
695     if (!strcmp(type, maclen_ctrl_string)) {
696         char *endptr;
697         long size = strtol(value, &endptr, 10);
698         if (*endptr != '\0') {
699             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
700             return 0;
701         }
702         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL);
703     }
704     if (strcmp(type, param_ctrl_string) == 0) {
705         ASN1_OBJECT *obj = OBJ_txt2obj(value, 0);
706         const struct gost_cipher_info *param = NULL;
707         if (obj == NULL) {
708             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
709             return 0;
710         }
711
712         param = get_encryption_params(obj);
713                                 ASN1_OBJECT_free(obj);
714         if (param == NULL) {
715             GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
716             return 0;
717         }
718
719
720         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 0,
721                                   (void *)param);
722     }
723     return -2;
724 }
725
726 static int pkey_gost_omac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2, size_t max_size)
727 {
728     struct gost_mac_pmeth_data *data =
729         (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
730
731     switch (type) {
732     case EVP_PKEY_CTRL_MD:
733         {
734             int nid = EVP_MD_type((const EVP_MD *)p2);
735             if (nid != NID_magma_mac && nid != NID_grasshopper_mac
736                 && nid != NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac /* FIXME beldmit */
737                 && nid != NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac) {
738                 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
739                         GOST_R_INVALID_DIGEST_TYPE);
740                 return 0;
741             }
742             data->md = (EVP_MD *)p2;
743             return 1;
744         }
745
746     case EVP_PKEY_CTRL_GET_MD:
747         *(const EVP_MD **)p2 = data->md;
748         return 1;
749
750     case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
751     case EVP_PKEY_CTRL_PKCS7_DECRYPT:
752     case EVP_PKEY_CTRL_PKCS7_SIGN:
753         return 1;
754     case EVP_PKEY_CTRL_SET_MAC_KEY:
755         if (p1 != 32) {
756             GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
757             return 0;
758         }
759
760         memcpy(data->key, p2, 32);
761         data->key_set = 1;
762         return 1;
763     case EVP_PKEY_CTRL_DIGESTINIT:
764         {
765             EVP_MD_CTX *mctx = p2;
766             struct gost_mac_key *key;
767             if (!data->key_set) {
768                 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
769                 if (!pkey) {
770                     GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
771                             GOST_R_MAC_KEY_NOT_SET);
772                     return 0;
773                 }
774                 key = EVP_PKEY_get0(pkey);
775                 if (!key) {
776                     GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
777                             GOST_R_MAC_KEY_NOT_SET);
778                     return 0;
779                 }
780                 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
781                     (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
782             } else {
783                 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
784                     (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
785             }
786         }
787     case EVP_PKEY_CTRL_MAC_LEN:
788         {
789             if (p1 < 1 || p1 > max_size) {
790
791                 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_SIZE);
792                 return 0;
793             }
794             data->mac_size = p1;
795             return 1;
796         }
797     }
798     return -2;
799 }
800
801 static int pkey_gost_magma_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
802 {
803         return pkey_gost_omac_ctrl(ctx, type, p1, p2, 8);
804 }
805
806 static int pkey_gost_grasshopper_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
807 {
808         return pkey_gost_omac_ctrl(ctx, type, p1, p2, 16);
809 }
810
811 static int pkey_gost_omac_ctrl_str(EVP_PKEY_CTX *ctx,
812                                   const char *type, const char *value, size_t max_size)
813 {
814     if (strcmp(type, key_ctrl_string) == 0) {
815         if (strlen(value) != 32) {
816             GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
817                     GOST_R_INVALID_MAC_KEY_LENGTH);
818             return 0;
819         }
820         return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
821                                   32, (char *)value);
822     }
823     if (strcmp(type, hexkey_ctrl_string) == 0) {
824         long keylen;
825         int ret;
826         unsigned char *keybuf = string_to_hex(value, &keylen);
827         if (!keybuf || keylen != 32) {
828             GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
829                     GOST_R_INVALID_MAC_KEY_LENGTH);
830             OPENSSL_free(keybuf);
831             return 0;
832         }
833         ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
834         OPENSSL_free(keybuf);
835         return ret;
836
837     }
838     if (!strcmp(type, maclen_ctrl_string)) {
839         char *endptr;
840         long size = strtol(value, &endptr, 10);
841         if (*endptr != '\0') {
842             GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
843             return 0;
844         }
845         return pkey_gost_omac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL, max_size);
846     }
847     return -2;
848 }
849
850 static int pkey_gost_magma_mac_ctrl_str(EVP_PKEY_CTX *ctx,
851                                   const char *type, const char *value)
852 {
853         return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
854 }
855
856 static int pkey_gost_grasshopper_mac_ctrl_str(EVP_PKEY_CTX *ctx,
857                                   const char *type, const char *value)
858 {
859         return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
860 }
861
862 static int pkey_gost_mac_keygen_base(EVP_PKEY_CTX *ctx,
863                                      EVP_PKEY *pkey, int mac_nid)
864 {
865     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
866     struct gost_mac_key *keydata;
867     if (!data || !data->key_set) {
868         GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN_BASE, GOST_R_MAC_KEY_NOT_SET);
869         return 0;
870     }
871     keydata = OPENSSL_malloc(sizeof(struct gost_mac_key));
872     if (keydata == NULL)
873         return 0;
874     memcpy(keydata->key, data->key, 32);
875     keydata->mac_param_nid = data->mac_param_nid;
876     keydata->mac_size = data->mac_size;
877     EVP_PKEY_assign(pkey, mac_nid, keydata);
878     return 1;
879 }
880
881 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
882 {
883     return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
884 }
885
886 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
887 {
888     return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
889 }
890
891 static int pkey_gost_magma_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
892 {
893     return pkey_gost_mac_keygen_base(ctx, pkey, NID_magma_mac);
894 }
895
896 static int pkey_gost_grasshopper_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
897 {
898     return pkey_gost_mac_keygen_base(ctx, pkey, NID_grasshopper_mac);
899 }
900
901 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
902 {
903     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
904
905     if (data == NULL) {
906         pkey_gost_mac_init(ctx);
907     }
908
909     data = EVP_PKEY_CTX_get_data(ctx);
910     if (!data) {
911         GOSTerr(GOST_F_PKEY_GOST_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
912         return 0;
913     }
914
915     return 1;
916 }
917
918 static int pkey_gost_magma_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
919 {
920     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
921
922     if (data == NULL) {
923         pkey_gost_omac_init(ctx, 4);
924     }
925
926     data = EVP_PKEY_CTX_get_data(ctx);
927     if (!data) {
928         GOSTerr(GOST_F_PKEY_GOST_MAGMA_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
929         return 0;
930     }
931
932     return 1;
933 }
934
935 static int pkey_gost_grasshopper_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
936 {
937     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
938
939     if (data == NULL) {
940         pkey_gost_omac_init(ctx, 8);
941     }
942
943     data = EVP_PKEY_CTX_get_data(ctx);
944     if (!data) {
945         GOSTerr(GOST_F_PKEY_GOST_GRASSHOPPER_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
946         return 0;
947     }
948
949     return 1;
950 }
951
952 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
953                                  size_t *siglen, EVP_MD_CTX *mctx)
954 {
955     unsigned int tmpsiglen;
956     int ret;
957     struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
958
959     if (!siglen)
960         return 0;
961     tmpsiglen = *siglen;        /* for platforms where sizeof(int) !=
962                                  * sizeof(size_t) */
963
964     if (!sig) {
965         *siglen = data->mac_size;
966         return 1;
967     }
968
969     EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
970         (mctx, EVP_MD_CTRL_XOF_LEN, data->mac_size, NULL);
971     ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
972     *siglen = data->mac_size;
973     return ret;
974 }
975
976 /* ----------- misc callbacks -------------------------------------*/
977
978 /* Callback for both EVP_PKEY_check() and EVP_PKEY_public_check. */
979 static int pkey_gost_check(EVP_PKEY *pkey)
980 {
981     return EC_KEY_check_key(EVP_PKEY_get0(pkey));
982 }
983
984 /* ----------------------------------------------------------------*/
985 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
986 {
987     *pmeth = EVP_PKEY_meth_new(id, flags);
988     if (!*pmeth)
989         return 0;
990
991     switch (id) {
992     case NID_id_GostR3410_2001:
993         EVP_PKEY_meth_set_ctrl(*pmeth,
994                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
995         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
996         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
997
998         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
999
1000         EVP_PKEY_meth_set_encrypt(*pmeth,
1001                                   pkey_gost_encrypt_init,
1002                                   pkey_gost_encrypt);
1003         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1004         EVP_PKEY_meth_set_derive(*pmeth,
1005                                  pkey_gost_derive_init, pkey_gost_ec_derive);
1006         EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
1007                                    pkey_gost2001_paramgen);
1008         EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1009         EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1010         break;
1011     case NID_id_GostR3410_2012_256:
1012         EVP_PKEY_meth_set_ctrl(*pmeth,
1013                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1014         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1015         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1016
1017         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1018
1019         EVP_PKEY_meth_set_encrypt(*pmeth,
1020                                   pkey_gost_encrypt_init,
1021                                   pkey_gost_encrypt);
1022         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1023         EVP_PKEY_meth_set_derive(*pmeth,
1024                                  pkey_gost_derive_init, pkey_gost_ec_derive);
1025         EVP_PKEY_meth_set_paramgen(*pmeth,
1026                                    pkey_gost_paramgen_init,
1027                                    pkey_gost2012_paramgen);
1028         EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1029         EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1030         break;
1031     case NID_id_GostR3410_2012_512:
1032         EVP_PKEY_meth_set_ctrl(*pmeth,
1033                                pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
1034         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1035         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1036
1037         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1038
1039         EVP_PKEY_meth_set_encrypt(*pmeth,
1040                                   pkey_gost_encrypt_init,
1041                                   pkey_gost_encrypt);
1042         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1043         EVP_PKEY_meth_set_derive(*pmeth,
1044                                  pkey_gost_derive_init, pkey_gost_ec_derive);
1045         EVP_PKEY_meth_set_paramgen(*pmeth,
1046                                    pkey_gost_paramgen_init,
1047                                    pkey_gost2012_paramgen);
1048         EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1049         EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1050         break;
1051     case NID_id_Gost28147_89_MAC:
1052         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1053                                pkey_gost_mac_ctrl_str);
1054         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1055                                   pkey_gost_mac_signctx);
1056         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
1057         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1058         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1059         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1060         return 1;
1061     case NID_gost_mac_12:
1062         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1063                                pkey_gost_mac_ctrl_str);
1064         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1065                                   pkey_gost_mac_signctx);
1066         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
1067         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1068         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1069         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1070         return 1;
1071     case NID_magma_mac:
1072         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_magma_mac_ctrl,
1073                                pkey_gost_magma_mac_ctrl_str);
1074         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_magma_mac_signctx_init,
1075                                   pkey_gost_mac_signctx);
1076         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_magma_mac_keygen);
1077         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_magma_mac_init);
1078         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1079         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1080         return 1;
1081     case NID_grasshopper_mac:
1082     case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac: /* FIXME beldmit */
1083         EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_grasshopper_mac_ctrl,
1084                                pkey_gost_grasshopper_mac_ctrl_str);
1085         EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_grasshopper_mac_signctx_init,
1086                                   pkey_gost_mac_signctx);
1087         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_grasshopper_mac_keygen);
1088         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_grasshopper_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     default:                   /* Unsupported method */
1093         return 0;
1094     }
1095     EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
1096     EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
1097
1098     EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
1099     /*
1100      * FIXME derive etc...
1101      */
1102
1103     return 1;
1104 }