]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ameth.c
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / gost_ameth.c
1 /**********************************************************************
2  *                          gost_ameth.c                              *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *       Implementation of RFC 4490/4491 ASN1 method                  *
7  *       for OpenSSL                                                  *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <string.h>
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/asn1.h>
16 #ifndef OPENSSL_NO_CMS
17 # include <openssl/cms.h>
18 #endif
19 #include "gost_lcl.h"
20 #include "e_gost_err.h"
21
22 #define PK_WRAP_PARAM "LEGACY_PK_WRAP"
23
24 /*
25  * Pack bignum into byte buffer of given size, filling all leading bytes by
26  * zeros
27  */
28 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len)
29 {
30     int bytes = BN_num_bytes(bn);
31
32     if (bytes > len)
33         return 0;
34     memset(buf, 0, len);
35     BN_bn2bin(bn, buf + len - bytes);
36     return 1;
37 }
38
39 static int pkey_bits_gost(const EVP_PKEY *pk)
40 {
41     if (!pk)
42         return -1;
43
44     switch (EVP_PKEY_base_id(pk)) {
45     case NID_id_GostR3410_2001:
46     case NID_id_GostR3410_2001DH:
47     case NID_id_GostR3410_2012_256:
48         return 256;
49     case NID_id_GostR3410_2012_512:
50         return 512;
51     }
52
53     return -1;
54 }
55
56 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
57 {
58     ASN1_STRING *params = ASN1_STRING_new();
59     GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
60     int pkey_param_nid = NID_undef;
61     void *key_ptr = EVP_PKEY_get0((EVP_PKEY *)key);
62     int result = 0;
63
64     if (!params || !gkp) {
65         GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
66         goto err;
67     }
68     switch (EVP_PKEY_base_id(key)) {
69     case NID_id_GostR3410_2012_256:
70         pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
71         switch (pkey_param_nid) {
72             case NID_id_GostR3410_2001_TestParamSet:
73             case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
74             case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
75             case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
76             case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
77             case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
78                 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_256);
79         }
80         break;
81     case NID_id_GostR3410_2012_512:
82         pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
83         switch (pkey_param_nid) {
84             case NID_id_tc26_gost_3410_2012_512_paramSetTest:
85             case NID_id_tc26_gost_3410_2012_512_paramSetA:
86             case NID_id_tc26_gost_3410_2012_512_paramSetB:
87                 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_512);
88         }
89         break;
90     case NID_id_GostR3410_2001:
91     case NID_id_GostR3410_2001DH:
92         pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
93         gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
94         break;
95     }
96
97     if (pkey_param_nid == NID_undef) {
98         GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, GOST_R_INVALID_PARAMSET);
99         goto err;
100     }
101
102     gkp->key_params = OBJ_nid2obj(pkey_param_nid);
103     /*
104      * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
105      */
106     params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
107     if (params->length <= 0) {
108         GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
109         goto err;
110     }
111     params->type = V_ASN1_SEQUENCE;
112     result = 1;
113  err:
114     if (gkp)
115         GOST_KEY_PARAMS_free(gkp);
116     if (result == 0) {          /* if error */
117         if (params)
118             ASN1_STRING_free(params);
119         return NULL;
120     }
121     return params;
122 }
123
124 static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid)
125 {
126     void *key_ptr = EVP_PKEY_get0(pkey);
127
128     switch (pkey_nid) {
129     case NID_id_GostR3410_2012_256:
130     case NID_id_GostR3410_2012_512:
131     case NID_id_GostR3410_2001:
132     case NID_id_GostR3410_2001DH:
133         if (!key_ptr) {
134             key_ptr = EC_KEY_new();
135             if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) {
136                 EC_KEY_free(key_ptr);
137                 break;
138             }
139         }
140         return fill_GOST_EC_params(key_ptr, param_nid);
141     }
142
143     return 0;
144 }
145
146 /*
147  * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
148  * NID and parameters
149  */
150 static int decode_gost_algor_params(EVP_PKEY *pkey,
151                                     const X509_ALGOR *palg)
152 {
153     const ASN1_OBJECT *palg_obj = NULL;
154     int ptype = V_ASN1_UNDEF;
155     int pkey_nid = NID_undef, param_nid = NID_undef;
156     ASN1_STRING *pval = NULL;
157     const unsigned char *p;
158     GOST_KEY_PARAMS *gkp = NULL;
159
160     if (!pkey || !palg)
161         return 0;
162     X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg);
163     if (ptype != V_ASN1_SEQUENCE) {
164         GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
165                 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
166         return 0;
167     }
168     p = pval->data;
169     pkey_nid = OBJ_obj2nid(palg_obj);
170
171     gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
172     if (!gkp) {
173         GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
174                 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
175         return 0;
176     }
177     param_nid = OBJ_obj2nid(gkp->key_params);
178     GOST_KEY_PARAMS_free(gkp);
179     if (!EVP_PKEY_set_type(pkey, pkey_nid)) {
180         GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
181         return 0;
182     }
183     return gost_decode_nid_params(pkey, pkey_nid, param_nid);
184 }
185
186 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
187 {
188     switch (EVP_PKEY_base_id(pkey)) {
189     case NID_id_GostR3410_2012_512:
190     case NID_id_GostR3410_2012_256:
191     case NID_id_GostR3410_2001:
192     case NID_id_GostR3410_2001DH:
193         {
194             EC_KEY *ec = EVP_PKEY_get0(pkey);
195             if (!ec) {
196                 ec = EC_KEY_new();
197                 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
198             }
199             if (!EC_KEY_set_private_key(ec, priv))
200                 return 0;
201             if (!EVP_PKEY_missing_parameters(pkey))
202                 return gost_ec_compute_public(ec);
203             break;
204         }
205     default:
206         return 0;
207     }
208     return 1;
209 }
210
211 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
212 {
213     switch (EVP_PKEY_base_id(pkey)) {
214     case NID_id_GostR3410_2012_512:
215     case NID_id_GostR3410_2012_256:
216     case NID_id_GostR3410_2001:
217     case NID_id_GostR3410_2001DH:
218         {
219             EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
220             if (ec)
221                 return (BIGNUM *)EC_KEY_get0_private_key(ec);
222             break;
223         }
224     }
225     return NULL;
226 }
227
228 /*
229  * GOST CMS processing functions
230  */
231 /* FIXME reaarange declarations */
232 static int pub_decode_gost_ec(EVP_PKEY *pk, const X509_PUBKEY *pub);
233
234 static int gost_cms_set_kari_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
235 {
236         int ret = 0;
237         X509_ALGOR *alg;
238         ASN1_OCTET_STRING *ukm;
239
240         /* Deal with originator */
241         X509_ALGOR *pubalg = NULL;
242         ASN1_BIT_STRING *pubkey = NULL;
243
244         EVP_PKEY *peer_key = NULL;
245         X509_PUBKEY *tmp   = NULL;
246
247         int nid;
248         unsigned char shared_key[64];
249         size_t shared_key_size = 64;
250         const EVP_CIPHER *cipher = NULL;
251
252         if (CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm) == 0)
253                 goto err;
254
255         if (CMS_RecipientInfo_kari_get0_orig_id(ri, &pubalg, &pubkey, NULL, NULL, NULL) == 0)
256                   goto err;
257
258         nid = OBJ_obj2nid(alg->algorithm);
259         if (alg->parameter->type != V_ASN1_SEQUENCE)
260                   goto err;
261
262         switch (nid) {
263                 case NID_kuznyechik_kexp15:
264                 case NID_magma_kexp15:
265                         cipher = EVP_get_cipherbynid(nid);
266                         break;
267         }
268
269         if (cipher == NULL) {
270                         GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_CIPHER_NOT_FOUND);
271                   goto err;
272   }
273
274         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_SET_IV,
275                 ASN1_STRING_length(ukm), (void *)ASN1_STRING_get0_data(ukm)) <= 0)
276                         goto err;
277
278         if (pubkey != NULL && pubalg != NULL) {
279                 const ASN1_OBJECT *paobj = NULL;
280                 int ptype = 0;
281                 const void *param = NULL;
282
283                 peer_key = EVP_PKEY_new();
284                 tmp = X509_PUBKEY_new();
285
286                 if ((peer_key == NULL) || (tmp == NULL)) {
287                         GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, ERR_R_MALLOC_FAILURE);
288                         goto err;
289                 }
290
291                 X509_ALGOR_get0(&paobj, &ptype, &param, pubalg);
292
293                 if (X509_PUBKEY_set0_param(tmp, (ASN1_OBJECT *)paobj,
294                         ptype, (void *)param,
295                         (unsigned char *)ASN1_STRING_get0_data(pubkey),
296                         ASN1_STRING_length(pubkey) ) == 0) {
297                                 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_PUBLIC_KEY_UNDEFINED);
298                                 goto err;
299                 }
300
301                 if (pub_decode_gost_ec(peer_key, tmp) <= 0) {
302                                 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_DECODING_PUBLIC_KEY);
303                                 goto err;
304                 }
305
306                 if (EVP_PKEY_derive_set_peer(pctx, peer_key) <= 0) {
307                                 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_SETTING_PEER_KEY);
308                                 goto err;
309                 }
310         }
311
312         if (EVP_PKEY_derive(pctx, shared_key, &shared_key_size) <= 0) {
313                 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_COMPUTING_SHARED_KEY);
314                 goto err;
315         }
316
317         EVP_CIPHER_CTX_set_flags(CMS_RecipientInfo_kari_get0_ctx(ri), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
318         if (EVP_DecryptInit_ex(CMS_RecipientInfo_kari_get0_ctx(ri), cipher, NULL,
319                 shared_key, ukm->data+24) == 0)
320                         goto err;
321
322         ret = 1;
323 err:
324         EVP_PKEY_free(peer_key);
325         if (ret == 0) {
326                 X509_PUBKEY_free(tmp);
327         }
328
329         return ret;
330 }
331
332 static int gost_cms_set_ktri_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
333 {
334         X509_ALGOR *alg;
335         struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
336
337         CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg);
338
339         switch (OBJ_obj2nid(alg->algorithm)) {
340                 case NID_kuznyechik_kexp15:
341                         gctx->cipher_nid = NID_kuznyechik_ctr;
342                         break;
343
344                 case NID_magma_kexp15:
345                         gctx->cipher_nid = NID_magma_ctr;
346                         break;
347
348                 case NID_id_GostR3410_2001:
349                 case NID_id_GostR3410_2001DH:
350                 case NID_id_GostR3410_2012_256:
351                 case NID_id_GostR3410_2012_512:
352                         gctx->cipher_nid = NID_id_Gost28147_89;
353                         break;
354
355                 default:
356                         GOSTerr(GOST_F_GOST_CMS_SET_KTRI_SHARED_INFO, GOST_R_UNSUPPORTED_RECIPIENT_INFO);
357                         return 0;
358         }
359
360         return 1;
361 }
362
363 static int gost_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
364 {
365         switch(CMS_RecipientInfo_type(ri)) {
366                 case CMS_RECIPINFO_AGREE:
367                         return gost_cms_set_kari_shared_info(pctx, ri);
368                 break;
369                 case CMS_RECIPINFO_TRANS:
370                         return gost_cms_set_ktri_shared_info(pctx, ri);
371                 break;
372         }
373
374         GOSTerr(GOST_F_GOST_CMS_SET_SHARED_INFO, GOST_R_UNSUPPORTED_RECIPIENT_INFO);
375         return 0;
376 }
377
378 static ASN1_STRING *gost_encode_cms_params(int ka_nid)
379 {
380         ASN1_STRING *ret = NULL;
381         ASN1_STRING *params = ASN1_STRING_new();
382
383         /* It's a hack. We have only one OID here, so we can use
384          * GOST_KEY_PARAMS which is a sequence of 3 OIDs,
385          * the 1st one is mandatory and the rest are optional */
386         GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
387
388         if (params == NULL || gkp == NULL) {
389                   GOSTerr(GOST_F_GOST_ENCODE_CMS_PARAMS, ERR_R_MALLOC_FAILURE);
390                         goto end;
391         }
392
393         gkp->key_params = OBJ_nid2obj(ka_nid);
394         params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
395
396         if (params->length < 0) {
397                   GOSTerr(GOST_F_GOST_ENCODE_CMS_PARAMS, ERR_R_MALLOC_FAILURE);
398                         goto end;
399         }
400
401         params->type = V_ASN1_SEQUENCE;
402         ret = params;
403
404 end:
405         GOST_KEY_PARAMS_free(gkp);
406
407         if (ret == NULL)
408                 ASN1_STRING_free(params);
409
410         return ret;
411 }
412
413 /*
414  * Control function
415  */
416 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
417 {
418     int nid = EVP_PKEY_base_id(pkey), md_nid = NID_undef;
419     X509_ALGOR *alg1 = NULL, *alg2 = NULL;
420
421     switch (nid) {
422     case NID_id_GostR3410_2012_512:
423         md_nid = NID_id_GostR3411_2012_512;
424         break;
425     case NID_id_GostR3410_2012_256:
426         md_nid = NID_id_GostR3411_2012_256;
427         break;
428     case NID_id_GostR3410_2001:
429     case NID_id_GostR3410_2001DH:
430     case NID_id_GostR3410_94:
431         md_nid = NID_id_GostR3411_94;
432         break;
433     default:
434         return -1;
435     }
436
437     switch (op) {
438     case ASN1_PKEY_CTRL_PKCS7_SIGN:
439         if (arg1 == 0) {
440             PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2, NULL,
441                                         &alg1, &alg2);
442             X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
443             X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
444         }
445         return 1;
446 #ifndef OPENSSL_NO_CMS
447     case ASN1_PKEY_CTRL_CMS_SIGN:
448         if (arg1 == 0) {
449             CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2, NULL, NULL,
450                                      &alg1, &alg2);
451             X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
452             X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
453         }
454         return 1;
455 #endif
456     case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
457         if (arg1 == 0) { /* Encryption */
458             ASN1_STRING *params = encode_gost_algor_params(pkey);
459             if (!params) {
460                 return -1;
461             }
462             PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg1);
463             X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
464                             V_ASN1_SEQUENCE, params);
465                                 }
466         return 1;
467 #ifndef OPENSSL_NO_CMS
468     case ASN1_PKEY_CTRL_CMS_ENVELOPE:
469         if (arg1 == 0) {
470           EVP_PKEY_CTX *pctx;
471           CMS_RecipientInfo *ri = arg2;
472
473           struct gost_pmeth_data *gctx = NULL;
474           ASN1_STRING *params = NULL;
475
476           pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
477           if (!pctx)
478             return 0;
479
480           gctx = EVP_PKEY_CTX_get_data(pctx);
481
482           switch (gctx->cipher_nid) {
483             case NID_magma_ctr:
484             case NID_kuznyechik_ctr:
485               {
486                 int ka_nid;
487
488                 nid = (gctx->cipher_nid == NID_magma_ctr) ? NID_magma_kexp15 :
489                   NID_kuznyechik_kexp15;
490
491                 ka_nid = (EVP_PKEY_base_id(pkey) == NID_id_GostR3410_2012_256) ?
492                   NID_id_tc26_agreement_gost_3410_2012_256 : NID_id_tc26_agreement_gost_3410_2012_512;
493
494                 params = gost_encode_cms_params(ka_nid);
495               }
496               break;
497             default:
498                 params = encode_gost_algor_params(pkey);
499               break;
500           }
501
502           if (params == NULL)
503               return -1;
504
505           CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
506               NULL, &alg1);
507           X509_ALGOR_set0(alg1, OBJ_nid2obj(nid), V_ASN1_SEQUENCE, params);
508         } else {
509           EVP_PKEY_CTX *pctx;
510           CMS_RecipientInfo *ri = arg2;
511           pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
512           if (!pctx)
513               return 0;
514           return gost_cms_set_shared_info(pctx, ri);
515         }
516         return 1;
517 #ifdef ASN1_PKEY_CTRL_CMS_RI_TYPE
518   case ASN1_PKEY_CTRL_CMS_RI_TYPE:
519         *(int *)arg2 = CMS_RECIPINFO_TRANS;
520         return 1;
521         case ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED:
522                         if (arg1 == CMS_RECIPINFO_AGREE || arg1 == CMS_RECIPINFO_TRANS) {
523           *(int *)arg2 = 1;
524                                   return 1;
525       }
526                         else
527                                   return 0;
528                         break;
529 #endif
530 #endif
531     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
532         *(int *)arg2 = md_nid;
533         return 2;
534     }
535
536     return -2;
537 }
538
539 /* --------------------- free functions * ------------------------------*/
540 static void pkey_free_gost_ec(EVP_PKEY *key)
541 {
542     EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key));
543 }
544
545 /* ------------------ private key functions  -----------------------------*/
546
547 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
548                                const unsigned char *buf, int len, int num_masks)
549 {
550     BIGNUM *pknum_masked = NULL, *q = NULL;
551     const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
552     const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
553
554     pknum_masked = BN_lebin2bn(buf, len, BN_secure_new());
555     if (!pknum_masked)
556         return NULL;
557
558     if (num_masks > 0) {
559         /*
560          * XXX Remove sign by gost94
561          */
562         const unsigned char *p = buf + num_masks * len;
563
564         q = BN_new();
565         if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
566             BN_free(pknum_masked);
567             pknum_masked = NULL;
568             goto end;
569         }
570
571         for (; p != buf; p -= len) {
572             BIGNUM *mask = BN_lebin2bn(p, len, BN_secure_new());
573             BN_CTX *ctx = BN_CTX_secure_new();
574
575             BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
576
577             BN_CTX_free(ctx);
578             BN_free(mask);
579         }
580     }
581
582  end:
583     if (q)
584         BN_free(q);
585     return pknum_masked;
586 }
587
588 static int priv_decode_gost(EVP_PKEY *pk,
589                             const PKCS8_PRIV_KEY_INFO *p8inf)
590 {
591     const unsigned char *pkey_buf = NULL, *p = NULL;
592     int priv_len = 0;
593     BIGNUM *pk_num = NULL;
594     int ret = 0;
595     const X509_ALGOR *palg = NULL;
596     const ASN1_OBJECT *palg_obj = NULL;
597     ASN1_INTEGER *priv_key = NULL;
598     int expected_key_len;
599
600     if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
601         return 0;
602     p = pkey_buf;
603     if (!decode_gost_algor_params(pk, palg)) {
604         return 0;
605     }
606
607     expected_key_len = pkey_bits_gost(pk) > 0 ? pkey_bits_gost(pk) / 8 : 0;
608     if (expected_key_len == 0) {
609         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
610         return 0;
611     }
612
613     if (priv_len % expected_key_len == 0) {
614         /* Key is not wrapped but masked */
615         pk_num = unmask_priv_key(pk, pkey_buf, expected_key_len,
616                                  priv_len / expected_key_len - 1);
617     } else if (V_ASN1_OCTET_STRING == *p) {
618         /* New format - Little endian octet string */
619         ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
620         if (!s || ((s->length != 32) && (s->length != 64))) {
621             ASN1_STRING_free(s);
622             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
623             return 0;
624         }
625         pk_num = BN_lebin2bn(s->data, s->length, BN_secure_new());
626         ASN1_STRING_free(s);
627     } else if (V_ASN1_INTEGER == *p) {
628         priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
629         if (!priv_key) {
630             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
631             return 0;
632         }
633         pk_num = ASN1_INTEGER_to_BN(priv_key, BN_secure_new());
634         ASN1_INTEGER_free(priv_key);
635     } else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
636         MASKED_GOST_KEY *mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
637
638         if (!mgk) {
639             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
640             return 0;
641         }
642
643         priv_len = mgk->masked_priv_key->length;
644         if (priv_len % expected_key_len) {
645             MASKED_GOST_KEY_free(mgk);
646             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
647             return 0;
648         }
649
650         pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
651                                  expected_key_len,
652                                  priv_len / expected_key_len - 1);
653         MASKED_GOST_KEY_free(mgk);
654     } else {
655         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
656         return 0;
657     }
658
659     if (pk_num == NULL) {
660         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
661         return 0;
662     }
663
664     ret = gost_set_priv_key(pk, pk_num);
665     BN_free(pk_num);
666     return ret;
667 }
668
669 /* ----------------------------------------------------------------------*/
670 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
671 {
672     ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
673     ASN1_STRING *params = NULL;
674     unsigned char *buf = NULL;
675     int key_len = pkey_bits_gost(pk), i = 0;
676     /* unmasked private key */
677     const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
678
679     key_len = (key_len < 0) ? 0 : key_len / 8;
680     if (key_len == 0 || !(buf = OPENSSL_secure_malloc(key_len))) {
681         return 0;
682     }
683
684     if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
685         OPENSSL_secure_free(buf);
686         return 0;
687     }
688
689     params = encode_gost_algor_params(pk);
690     if (!params) {
691         OPENSSL_secure_free(buf);
692         return 0;
693     }
694
695     /* Convert buf to Little-endian */
696     for (i = 0; i < key_len / 2; i++) {
697         unsigned char tmp = buf[i];
698         buf[i] = buf[key_len - 1 - i];
699         buf[key_len - 1 - i] = tmp;
700     }
701
702     if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
703         ASN1_STRING *octet = ASN1_STRING_new();
704         int priv_len = 0;
705         unsigned char *priv_buf = NULL;
706         if (!octet || !ASN1_OCTET_STRING_set(octet, buf, key_len)) {
707             ASN1_STRING_free(octet);
708             ASN1_STRING_free(params);
709             OPENSSL_secure_free(buf);
710             return 0;
711         }
712         priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
713         ASN1_STRING_free(octet);
714         OPENSSL_secure_free(buf);
715
716         return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
717                                priv_buf, priv_len);
718     }
719
720     return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
721                            buf, key_len);
722 }
723
724 /* --------- printing keys --------------------------------*/
725 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
726 {
727     BIGNUM *key;
728
729     if (!BIO_indent(out, indent, 128))
730         return 0;
731     BIO_printf(out, "Private key: ");
732     key = gost_get0_priv_key(pkey);
733     if (!key)
734         BIO_printf(out, "<undefined>");
735     else
736         BN_print(out, key);
737     BIO_printf(out, "\n");
738
739     return 1;
740 }
741
742 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
743 {
744     BN_CTX *ctx;
745     BIGNUM *X, *Y;
746     const EC_POINT *pubkey;
747     const EC_GROUP *group;
748     EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
749     int ok = 0;
750
751     ctx = BN_CTX_new();
752     if (!ctx) {
753         GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
754         return 0;
755     }
756
757     BN_CTX_start(ctx);
758     X = BN_CTX_get(ctx);
759     Y = BN_CTX_get(ctx);
760     pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
761     group = (key) ? EC_KEY_get0_group(key) : NULL;
762     if (!pubkey || !group)
763         goto err;
764
765     if (!EC_POINT_get_affine_coordinates(group, pubkey, X, Y, ctx)) {
766         GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
767         goto err;
768     }
769     if (!BIO_indent(out, indent, 128))
770         goto err;
771     BIO_printf(out, "Public key:\n");
772     if (!BIO_indent(out, indent + 3, 128))
773         goto err;
774     BIO_printf(out, "X:");
775     BN_print(out, X);
776     BIO_printf(out, "\n");
777     if (!BIO_indent(out, indent + 3, 128))
778         goto err;
779     BIO_printf(out, "Y:");
780     BN_print(out, Y);
781     BIO_printf(out, "\n");
782     ok = 1;
783  err:
784     BN_CTX_end(ctx);
785     BN_CTX_free(ctx);
786
787     return ok;
788 }
789
790 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
791 {
792     EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
793     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
794     int param_nid;
795
796     if (!group)
797         return 0;
798
799     param_nid = EC_GROUP_get_curve_name(group);
800     if (!BIO_indent(out, indent, 128))
801         return 0;
802     BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
803
804     return 1;
805 }
806
807 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
808                          ASN1_PCTX *pctx, int type)
809 {
810     if (type == 2) {
811         if (print_gost_priv(out, pkey, indent) == 0)
812             return 0;
813     }
814     if (type >= 1) {
815         if (print_gost_ec_pub(out, pkey, indent) == 0)
816             return 0;
817     }
818
819     return print_gost_ec_param(out, pkey, indent);
820 }
821
822 static int param_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
823                                ASN1_PCTX *pctx)
824 {
825     return print_gost_ec(out, pkey, indent, pctx, 0);
826 }
827
828 static int pub_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
829                              ASN1_PCTX *pctx)
830 {
831     return print_gost_ec(out, pkey, indent, pctx, 1);
832 }
833
834 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
835                               ASN1_PCTX *pctx)
836 {
837     return print_gost_ec(out, pkey, indent, pctx, 2);
838 }
839
840 /* ---------------------------------------------------------------------*/
841 static int param_missing_gost_ec(const EVP_PKEY *pk)
842 {
843     const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
844     if (!ec)
845         return 1;
846     if (!EC_KEY_get0_group(ec))
847         return 1;
848     return 0;
849 }
850
851 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
852 {
853     EC_KEY *eto = EVP_PKEY_get0(to);
854     const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
855     if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
856         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_INCOMPATIBLE_ALGORITHMS);
857         return 0;
858     }
859     if (!efrom) {
860         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
861         return 0;
862     }
863     if (!eto) {
864         eto = EC_KEY_new();
865         if (!eto) {
866             GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
867             return 0;
868         }
869         if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
870             GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
871             EC_KEY_free(eto);
872             return 0;
873         }
874     }
875     if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
876         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
877         return 0;
878     }
879     if (EC_KEY_get0_private_key(eto)) {
880         return gost_ec_compute_public(eto);
881     }
882     return 1;
883 }
884
885 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
886 {
887     const EC_GROUP *group_a, *group_b;
888     EC_KEY *ec_a = EVP_PKEY_get0((EVP_PKEY *)a);
889     EC_KEY *ec_b = EVP_PKEY_get0((EVP_PKEY *)b);
890     if (!ec_a || !ec_b)
891         return 0;
892
893     group_a = EC_KEY_get0_group(ec_a);
894     group_b = EC_KEY_get0_group(ec_b);
895     if (!group_a || !group_b)
896         return 0;
897
898     if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
899         return 1;
900     }
901     return 0;
902 }
903
904 /* ---------- Public key functions * --------------------------------------*/
905 static int pub_decode_gost_ec(EVP_PKEY *pk, const X509_PUBKEY *pub)
906 {
907     X509_ALGOR *palg = NULL;
908     const unsigned char *pubkey_buf = NULL;
909     unsigned char *databuf = NULL;
910     ASN1_OBJECT *palgobj = NULL;
911     int pub_len;
912     EC_POINT *pub_key = NULL;
913     BIGNUM *X = NULL, *Y = NULL;
914     ASN1_OCTET_STRING *octet = NULL;
915     size_t len;
916     const EC_GROUP *group;
917     int retval = 0;
918
919     if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
920         goto ret;
921     EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
922     if (!decode_gost_algor_params(pk, palg))
923         goto ret;
924     group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
925     octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
926     if (!octet) {
927         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
928         goto ret;
929     }
930     databuf = OPENSSL_malloc(octet->length);
931     if (!databuf) {
932         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
933         goto ret;
934     }
935
936     BUF_reverse(databuf, octet->data, octet->length);
937     len = octet->length / 2;
938
939     Y = BN_bin2bn(databuf, len, NULL);
940     X = BN_bin2bn(databuf + len, len, NULL);
941     if (!X || !Y) {
942         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_BN_LIB);
943         goto ret;
944     }
945     pub_key = EC_POINT_new(group);
946     if (!EC_POINT_set_affine_coordinates(group, pub_key, X, Y, NULL)) {
947         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
948         goto ret;
949     }
950
951     retval = EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key);
952     if (!retval)
953         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
954
955 ret:
956     EC_POINT_free(pub_key);
957     BN_free(X);
958     BN_free(Y);
959     OPENSSL_free(databuf);
960     ASN1_OCTET_STRING_free(octet);
961     return retval;
962 }
963
964 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
965 {
966     ASN1_OBJECT *algobj;
967     ASN1_OCTET_STRING *octet = NULL;
968     void *pval;
969     unsigned char *buf = NULL, *databuf = NULL;
970     int data_len, ret = -1;
971     const EC_POINT *pub_key;
972     BIGNUM *X = NULL, *Y = NULL, *order;
973     const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
974     int ptype = V_ASN1_SEQUENCE;
975     ASN1_STRING *params;
976
977     algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
978
979     params = encode_gost_algor_params(pk);
980     pval = params;
981
982     order = BN_new();
983     if (order == NULL || EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL) == 0) {
984         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
985         goto err;
986     }
987     if (EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL) == 0) {
988         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
989         goto err;
990     }
991     pub_key = EC_KEY_get0_public_key(ec);
992     if (!pub_key) {
993         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
994         goto err;
995     }
996     X = BN_new();
997     Y = BN_new();
998     if (!X || !Y) {
999         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1000         goto err;
1001     }
1002     if (!EC_POINT_get_affine_coordinates(EC_KEY_get0_group(ec),
1003                                              pub_key, X, Y, NULL)) {
1004         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
1005         goto err;
1006     }
1007     data_len = 2 * BN_num_bytes(order);
1008     databuf = OPENSSL_zalloc(data_len);
1009     if (databuf == NULL) {
1010         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1011         goto err;
1012     }
1013
1014     store_bignum(X, databuf + data_len / 2, data_len / 2);
1015     store_bignum(Y, databuf, data_len / 2);
1016
1017     BUF_reverse(databuf, NULL, data_len);
1018
1019     octet = ASN1_OCTET_STRING_new();
1020     if (octet == NULL) {
1021         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1022         goto err;
1023     }
1024
1025     if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
1026         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1027         goto err;
1028     }
1029
1030     ret = i2d_ASN1_OCTET_STRING(octet, &buf);
1031  err:
1032     ASN1_BIT_STRING_free(octet);
1033     if (X)
1034         BN_free(X);
1035     if (Y)
1036         BN_free(Y);
1037     if (order)
1038         BN_free(order);
1039     if (databuf)
1040         OPENSSL_free(databuf);
1041
1042     if (ret < 0)
1043         return 0;
1044     return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
1045 }
1046
1047 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
1048 {
1049     const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
1050     const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
1051     const EC_POINT *ka, *kb;
1052     if (!ea || !eb)
1053         return 0;
1054     ka = EC_KEY_get0_public_key(ea);
1055     kb = EC_KEY_get0_public_key(eb);
1056     if (!ka || !kb)
1057         return 0;
1058     return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
1059 }
1060
1061 static int pkey_size_gost(const EVP_PKEY *pk)
1062 {
1063     if (!pk)
1064         return -1;
1065
1066     switch (EVP_PKEY_base_id(pk)) {
1067     case NID_id_GostR3410_94:
1068     case NID_id_GostR3410_2001:
1069     case NID_id_GostR3410_2001DH:
1070     case NID_id_GostR3410_2012_256:
1071         return 64;
1072     case NID_id_GostR3410_2012_512:
1073         return 128;
1074     }
1075
1076     return -1;
1077 }
1078
1079 /* ---------------------- ASN1 METHOD for GOST MAC  -------------------*/
1080 static void mackey_free_gost(EVP_PKEY *pk)
1081 {
1082     OPENSSL_free(EVP_PKEY_get0(pk));
1083 }
1084
1085 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1086 {
1087     switch (op) {
1088     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1089         if (arg2) {
1090             *(int *)arg2 = NID_id_Gost28147_89_MAC;
1091             return 2;
1092         }
1093     }
1094     return -2;
1095 }
1096
1097 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1098 {
1099     switch (op) {
1100     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1101         if (arg2) {
1102             *(int *)arg2 = NID_gost_mac_12;
1103             return 2;
1104         }
1105     }
1106     return -2;
1107 }
1108
1109 static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1110 {
1111     switch (op) {
1112     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1113         if (arg2) {
1114             *(int *)arg2 = NID_magma_mac;
1115             return 2;
1116         }
1117     }
1118     return -2;
1119 }
1120
1121 static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1122 {
1123     switch (op) {
1124     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1125         if (arg2) {
1126             *(int *)arg2 = NID_grasshopper_mac;
1127             return 2;
1128         }
1129     }
1130     return -2;
1131 }
1132
1133 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
1134 {
1135     int nid =
1136         EC_GROUP_get_curve_name(EC_KEY_get0_group
1137                                 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
1138     return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
1139 }
1140
1141 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
1142                                  int derlen)
1143 {
1144     ASN1_OBJECT *obj = NULL;
1145     int nid;
1146     if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
1147         return 0;
1148     }
1149     nid = OBJ_obj2nid(obj);
1150     ASN1_OBJECT_free(obj);
1151
1152     return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
1153 }
1154
1155 /* ----------------------------------------------------------------------*/
1156 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
1157                         const char *pemstr, const char *info)
1158 {
1159     *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
1160     if (!*ameth)
1161         return 0;
1162     switch (nid) {
1163     case NID_id_GostR3410_2001:
1164     case NID_id_GostR3410_2001DH:
1165         EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1166         EVP_PKEY_asn1_set_private(*ameth,
1167                                   priv_decode_gost, priv_encode_gost,
1168                                   priv_print_gost_ec);
1169
1170         EVP_PKEY_asn1_set_param(*ameth,
1171                                 gost2001_param_decode, gost2001_param_encode,
1172                                 param_missing_gost_ec, param_copy_gost_ec,
1173                                 param_cmp_gost_ec, param_print_gost_ec);
1174         EVP_PKEY_asn1_set_public(*ameth,
1175                                  pub_decode_gost_ec, pub_encode_gost_ec,
1176                                  pub_cmp_gost_ec, pub_print_gost_ec,
1177                                  pkey_size_gost, pkey_bits_gost);
1178
1179         EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1180         EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1181         break;
1182     case NID_id_GostR3410_2012_256:
1183     case NID_id_GostR3410_2012_512:
1184         EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1185         EVP_PKEY_asn1_set_private(*ameth,
1186                                   priv_decode_gost, priv_encode_gost,
1187                                   priv_print_gost_ec);
1188
1189         EVP_PKEY_asn1_set_param(*ameth,
1190                                 NULL, NULL,
1191                                 param_missing_gost_ec, param_copy_gost_ec,
1192                                 param_cmp_gost_ec, NULL);
1193
1194         EVP_PKEY_asn1_set_public(*ameth,
1195                                  pub_decode_gost_ec, pub_encode_gost_ec,
1196                                  pub_cmp_gost_ec, pub_print_gost_ec,
1197                                  pkey_size_gost, pkey_bits_gost);
1198
1199         EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1200         EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1201         break;
1202     case NID_id_Gost28147_89_MAC:
1203         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1204         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
1205         break;
1206     case NID_gost_mac_12:
1207         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1208         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
1209         break;
1210     case NID_magma_mac:
1211         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1212         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma);
1213         break;
1214     case NID_grasshopper_mac:
1215         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1216         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper);
1217         break;
1218     }
1219     return 1;
1220 }