]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ameth.c
Delete .travis.yml
[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, 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     case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
536         return gost_ec_oct2key((EC_KEY *)EVP_PKEY_get0(pkey), arg2, arg1);
537
538     case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
539         return gost_ec_key2buf((EC_KEY *)EVP_PKEY_get0(pkey), arg2);
540     }
541
542     return -2;
543 }
544
545 /* --------------------- free functions * ------------------------------*/
546 static void pkey_free_gost_ec(EVP_PKEY *key)
547 {
548     EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key));
549 }
550
551 /* ------------------ private key functions  -----------------------------*/
552
553 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
554                                const unsigned char *buf, int len, int num_masks)
555 {
556     BIGNUM *pknum_masked = NULL, *q = NULL;
557     const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
558     const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
559
560     pknum_masked = BN_lebin2bn(buf, len, BN_secure_new());
561     if (!pknum_masked)
562         return NULL;
563
564     if (num_masks > 0) {
565         /*
566          * XXX Remove sign by gost94
567          */
568         const unsigned char *p = buf + num_masks * len;
569
570         q = BN_new();
571         if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
572             BN_free(pknum_masked);
573             pknum_masked = NULL;
574             goto end;
575         }
576
577         for (; p != buf; p -= len) {
578             BIGNUM *mask = BN_lebin2bn(p, len, BN_secure_new());
579             BN_CTX *ctx = BN_CTX_secure_new();
580
581             BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
582
583             BN_CTX_free(ctx);
584             BN_free(mask);
585         }
586     }
587
588  end:
589     if (q)
590         BN_free(q);
591     return pknum_masked;
592 }
593
594 static int priv_decode_gost(EVP_PKEY *pk,
595                             const PKCS8_PRIV_KEY_INFO *p8inf)
596 {
597     const unsigned char *pkey_buf = NULL, *p = NULL;
598     int priv_len = 0;
599     BIGNUM *pk_num = NULL;
600     int ret = 0;
601     const X509_ALGOR *palg = NULL;
602     const ASN1_OBJECT *palg_obj = NULL;
603     ASN1_INTEGER *priv_key = NULL;
604     int expected_key_len;
605
606     if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
607         return 0;
608     p = pkey_buf;
609     if (!decode_gost_algor_params(pk, palg)) {
610         return 0;
611     }
612
613     expected_key_len = pkey_bits_gost(pk) > 0 ? pkey_bits_gost(pk) / 8 : 0;
614     if (expected_key_len == 0) {
615         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
616         return 0;
617     }
618
619     if (priv_len % expected_key_len == 0) {
620         /* Key is not wrapped but masked */
621         pk_num = unmask_priv_key(pk, pkey_buf, expected_key_len,
622                                  priv_len / expected_key_len - 1);
623     } else if (V_ASN1_OCTET_STRING == *p) {
624         /* New format - Little endian octet string */
625         ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
626         if (!s || ((s->length != 32) && (s->length != 64))) {
627             ASN1_STRING_free(s);
628             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
629             return 0;
630         }
631         pk_num = BN_lebin2bn(s->data, s->length, BN_secure_new());
632         ASN1_STRING_free(s);
633     } else if (V_ASN1_INTEGER == *p) {
634         priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
635         if (!priv_key) {
636             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
637             return 0;
638         }
639         pk_num = ASN1_INTEGER_to_BN(priv_key, BN_secure_new());
640         ASN1_INTEGER_free(priv_key);
641     } else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
642         MASKED_GOST_KEY *mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
643
644         if (!mgk) {
645             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
646             return 0;
647         }
648
649         priv_len = mgk->masked_priv_key->length;
650         if (priv_len % expected_key_len) {
651             MASKED_GOST_KEY_free(mgk);
652             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
653             return 0;
654         }
655
656         pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
657                                  expected_key_len,
658                                  priv_len / expected_key_len - 1);
659         MASKED_GOST_KEY_free(mgk);
660     } else {
661         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
662         return 0;
663     }
664
665     if (pk_num == NULL) {
666         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
667         return 0;
668     }
669
670     ret = gost_set_priv_key(pk, pk_num);
671     BN_free(pk_num);
672     return ret;
673 }
674
675 /* ----------------------------------------------------------------------*/
676 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
677 {
678     ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
679     ASN1_STRING *params = NULL;
680     unsigned char *buf = NULL;
681     int key_len = pkey_bits_gost(pk), i = 0;
682     /* unmasked private key */
683     const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
684
685     key_len = (key_len < 0) ? 0 : key_len / 8;
686     if (key_len == 0 || !(buf = OPENSSL_secure_malloc(key_len))) {
687         return 0;
688     }
689
690     if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
691         OPENSSL_secure_free(buf);
692         return 0;
693     }
694
695     params = encode_gost_algor_params(pk);
696     if (!params) {
697         OPENSSL_secure_free(buf);
698         return 0;
699     }
700
701     /* Convert buf to Little-endian */
702     for (i = 0; i < key_len / 2; i++) {
703         unsigned char tmp = buf[i];
704         buf[i] = buf[key_len - 1 - i];
705         buf[key_len - 1 - i] = tmp;
706     }
707
708     if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
709         ASN1_STRING *octet = ASN1_STRING_new();
710         int priv_len = 0;
711         unsigned char *priv_buf = NULL;
712         if (!octet || !ASN1_OCTET_STRING_set(octet, buf, key_len)) {
713             ASN1_STRING_free(octet);
714             ASN1_STRING_free(params);
715             OPENSSL_secure_free(buf);
716             return 0;
717         }
718         priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
719         ASN1_STRING_free(octet);
720         OPENSSL_secure_free(buf);
721
722         return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
723                                priv_buf, priv_len);
724     }
725
726     return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
727                            buf, key_len);
728 }
729
730 /* --------- printing keys --------------------------------*/
731 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
732 {
733     BIGNUM *key;
734
735     if (!BIO_indent(out, indent, 128))
736         return 0;
737     BIO_printf(out, "Private key: ");
738     key = gost_get0_priv_key(pkey);
739     if (!key)
740         BIO_printf(out, "<undefined>");
741     else
742         BN_print(out, key);
743     BIO_printf(out, "\n");
744
745     return 1;
746 }
747
748 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
749 {
750     BN_CTX *ctx;
751     BIGNUM *X, *Y;
752     const EC_POINT *pubkey;
753     const EC_GROUP *group;
754     EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
755     int ok = 0;
756
757     ctx = BN_CTX_new();
758     if (!ctx) {
759         GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
760         return 0;
761     }
762
763     BN_CTX_start(ctx);
764     X = BN_CTX_get(ctx);
765     Y = BN_CTX_get(ctx);
766     pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
767     group = (key) ? EC_KEY_get0_group(key) : NULL;
768     if (!pubkey || !group)
769         goto err;
770
771     if (!EC_POINT_get_affine_coordinates(group, pubkey, X, Y, ctx)) {
772         GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
773         goto err;
774     }
775     if (!BIO_indent(out, indent, 128))
776         goto err;
777     BIO_printf(out, "Public key:\n");
778     if (!BIO_indent(out, indent + 3, 128))
779         goto err;
780     BIO_printf(out, "X:");
781     BN_print(out, X);
782     BIO_printf(out, "\n");
783     if (!BIO_indent(out, indent + 3, 128))
784         goto err;
785     BIO_printf(out, "Y:");
786     BN_print(out, Y);
787     BIO_printf(out, "\n");
788     ok = 1;
789  err:
790     BN_CTX_end(ctx);
791     BN_CTX_free(ctx);
792
793     return ok;
794 }
795
796 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
797 {
798     EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
799     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
800     int param_nid;
801
802     if (!group)
803         return 0;
804
805     param_nid = EC_GROUP_get_curve_name(group);
806     if (!BIO_indent(out, indent, 128))
807         return 0;
808     BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
809
810     return 1;
811 }
812
813 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
814                          ASN1_PCTX *pctx, int type)
815 {
816     if (type == 2) {
817         if (print_gost_priv(out, pkey, indent) == 0)
818             return 0;
819     }
820     if (type >= 1) {
821         if (print_gost_ec_pub(out, pkey, indent) == 0)
822             return 0;
823     }
824
825     return print_gost_ec_param(out, pkey, indent);
826 }
827
828 static int param_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, 0);
832 }
833
834 static int pub_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, 1);
838 }
839
840 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
841                               ASN1_PCTX *pctx)
842 {
843     return print_gost_ec(out, pkey, indent, pctx, 2);
844 }
845
846 /* ---------------------------------------------------------------------*/
847 static int param_missing_gost_ec(const EVP_PKEY *pk)
848 {
849     const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
850     if (!ec)
851         return 1;
852     if (!EC_KEY_get0_group(ec))
853         return 1;
854     return 0;
855 }
856
857 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
858 {
859     EC_KEY *eto = EVP_PKEY_get0(to);
860     const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
861     if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
862         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_INCOMPATIBLE_ALGORITHMS);
863         return 0;
864     }
865     if (!efrom) {
866         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
867         return 0;
868     }
869     if (!eto) {
870         eto = EC_KEY_new();
871         if (!eto) {
872             GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
873             return 0;
874         }
875         if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
876             GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
877             EC_KEY_free(eto);
878             return 0;
879         }
880     }
881     if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
882         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
883         return 0;
884     }
885     if (EC_KEY_get0_private_key(eto)) {
886         return gost_ec_compute_public(eto);
887     }
888     return 1;
889 }
890
891 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
892 {
893     const EC_GROUP *group_a, *group_b;
894     EC_KEY *ec_a = EVP_PKEY_get0((EVP_PKEY *)a);
895     EC_KEY *ec_b = EVP_PKEY_get0((EVP_PKEY *)b);
896     if (!ec_a || !ec_b)
897         return 0;
898
899     group_a = EC_KEY_get0_group(ec_a);
900     group_b = EC_KEY_get0_group(ec_b);
901     if (!group_a || !group_b)
902         return 0;
903
904     if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
905         return 1;
906     }
907     return 0;
908 }
909
910 /* ---------- Public key functions * --------------------------------------*/
911 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub)
912 {
913     X509_ALGOR *palg = NULL;
914     const unsigned char *pubkey_buf = NULL;
915     unsigned char *databuf;
916     ASN1_OBJECT *palgobj = NULL;
917     int pub_len;
918     EC_POINT *pub_key;
919     BIGNUM *X, *Y;
920     ASN1_OCTET_STRING *octet = NULL;
921     size_t len;
922     const EC_GROUP *group;
923
924     if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
925         return 0;
926     EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
927     if (!decode_gost_algor_params(pk, palg))
928         return 0;
929     group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
930     octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
931     if (!octet) {
932         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
933         return 0;
934     }
935     databuf = OPENSSL_malloc(octet->length);
936     if (databuf == NULL) {
937         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
938         ASN1_OCTET_STRING_free(octet);
939         return 0;
940     }
941
942     BUF_reverse(databuf, octet->data, octet->length);
943     len = octet->length / 2;
944     ASN1_OCTET_STRING_free(octet);
945
946     Y = BN_bin2bn(databuf, len, NULL);
947     X = BN_bin2bn(databuf + len, len, NULL);
948     OPENSSL_free(databuf);
949     pub_key = EC_POINT_new(group);
950     if (!EC_POINT_set_affine_coordinates(group, pub_key, X, Y, NULL)) {
951         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
952         EC_POINT_free(pub_key);
953         BN_free(X);
954         BN_free(Y);
955         return 0;
956     }
957     BN_free(X);
958     BN_free(Y);
959     if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
960         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
961         EC_POINT_free(pub_key);
962         return 0;
963     }
964     EC_POINT_free(pub_key);
965     return 1;
966
967 }
968
969 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
970 {
971     ASN1_OBJECT *algobj;
972     ASN1_OCTET_STRING *octet = NULL;
973     void *pval;
974     unsigned char *buf = NULL, *databuf = NULL;
975     int data_len, ret = -1;
976     const EC_POINT *pub_key;
977     BIGNUM *X = NULL, *Y = NULL, *order;
978     const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
979     int ptype = V_ASN1_SEQUENCE;
980     ASN1_STRING *params;
981
982     algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
983
984     params = encode_gost_algor_params(pk);
985     pval = params;
986
987     order = BN_new();
988     if (order == NULL || EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL) == 0) {
989         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
990         goto err;
991     }
992     EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
993     pub_key = EC_KEY_get0_public_key(ec);
994     if (!pub_key) {
995         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
996         goto err;
997     }
998     X = BN_new();
999     Y = BN_new();
1000     if (!X || !Y) {
1001         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1002         goto err;
1003     }
1004     if (!EC_POINT_get_affine_coordinates(EC_KEY_get0_group(ec),
1005                                              pub_key, X, Y, NULL)) {
1006         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
1007         goto err;
1008     }
1009     data_len = 2 * BN_num_bytes(order);
1010     databuf = OPENSSL_zalloc(data_len);
1011     if (databuf == NULL) {
1012         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1013         goto err;
1014     }
1015
1016     store_bignum(X, databuf + data_len / 2, data_len / 2);
1017     store_bignum(Y, databuf, data_len / 2);
1018
1019     BUF_reverse(databuf, NULL, data_len);
1020
1021     octet = ASN1_OCTET_STRING_new();
1022     if (octet == NULL) {
1023         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1024         goto err;
1025     }
1026
1027     if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
1028         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1029         goto err;
1030     }
1031
1032     ret = i2d_ASN1_OCTET_STRING(octet, &buf);
1033  err:
1034     ASN1_BIT_STRING_free(octet);
1035     if (X)
1036         BN_free(X);
1037     if (Y)
1038         BN_free(Y);
1039     if (order)
1040         BN_free(order);
1041     if (databuf)
1042         OPENSSL_free(databuf);
1043
1044     if (ret < 0)
1045         return 0;
1046     return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
1047 }
1048
1049 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
1050 {
1051     const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
1052     const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
1053     const EC_POINT *ka, *kb;
1054     if (!ea || !eb)
1055         return 0;
1056     ka = EC_KEY_get0_public_key(ea);
1057     kb = EC_KEY_get0_public_key(eb);
1058     if (!ka || !kb)
1059         return 0;
1060     return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
1061 }
1062
1063 static int pkey_size_gost(const EVP_PKEY *pk)
1064 {
1065     if (!pk)
1066         return -1;
1067
1068     switch (EVP_PKEY_base_id(pk)) {
1069     case NID_id_GostR3410_94:
1070     case NID_id_GostR3410_2001:
1071     case NID_id_GostR3410_2001DH:
1072     case NID_id_GostR3410_2012_256:
1073         return 64;
1074     case NID_id_GostR3410_2012_512:
1075         return 128;
1076     }
1077
1078     return -1;
1079 }
1080
1081 /* ---------------------- ASN1 METHOD for GOST MAC  -------------------*/
1082 static void mackey_free_gost(EVP_PKEY *pk)
1083 {
1084     OPENSSL_free(EVP_PKEY_get0(pk));
1085 }
1086
1087 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1088 {
1089     switch (op) {
1090     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1091         if (arg2) {
1092             *(int *)arg2 = NID_id_Gost28147_89_MAC;
1093             return 2;
1094         }
1095     }
1096     return -2;
1097 }
1098
1099 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1100 {
1101     switch (op) {
1102     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1103         if (arg2) {
1104             *(int *)arg2 = NID_gost_mac_12;
1105             return 2;
1106         }
1107     }
1108     return -2;
1109 }
1110
1111 static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1112 {
1113     switch (op) {
1114     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1115         if (arg2) {
1116             *(int *)arg2 = NID_magma_mac;
1117             return 2;
1118         }
1119     }
1120     return -2;
1121 }
1122
1123 static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1124 {
1125     switch (op) {
1126     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1127         if (arg2) {
1128             *(int *)arg2 = NID_grasshopper_mac;
1129             return 2;
1130         }
1131     }
1132     return -2;
1133 }
1134
1135 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
1136 {
1137     int nid =
1138         EC_GROUP_get_curve_name(EC_KEY_get0_group
1139                                 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
1140     return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
1141 }
1142
1143 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
1144                                  int derlen)
1145 {
1146     ASN1_OBJECT *obj = NULL;
1147     int nid;
1148     if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
1149         return 0;
1150     }
1151     nid = OBJ_obj2nid(obj);
1152     ASN1_OBJECT_free(obj);
1153
1154     return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
1155 }
1156
1157 /* ----------------------------------------------------------------------*/
1158 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
1159                         const char *pemstr, const char *info)
1160 {
1161     *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
1162     if (!*ameth)
1163         return 0;
1164     switch (nid) {
1165     case NID_id_GostR3410_2001:
1166     case NID_id_GostR3410_2001DH:
1167         EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1168         EVP_PKEY_asn1_set_private(*ameth,
1169                                   priv_decode_gost, priv_encode_gost,
1170                                   priv_print_gost_ec);
1171
1172         EVP_PKEY_asn1_set_param(*ameth,
1173                                 gost2001_param_decode, gost2001_param_encode,
1174                                 param_missing_gost_ec, param_copy_gost_ec,
1175                                 param_cmp_gost_ec, param_print_gost_ec);
1176         EVP_PKEY_asn1_set_public(*ameth,
1177                                  pub_decode_gost_ec, pub_encode_gost_ec,
1178                                  pub_cmp_gost_ec, pub_print_gost_ec,
1179                                  pkey_size_gost, pkey_bits_gost);
1180
1181         EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1182         EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1183         break;
1184     case NID_id_GostR3410_2012_256:
1185     case NID_id_GostR3410_2012_512:
1186         EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1187         EVP_PKEY_asn1_set_private(*ameth,
1188                                   priv_decode_gost, priv_encode_gost,
1189                                   priv_print_gost_ec);
1190
1191         EVP_PKEY_asn1_set_param(*ameth,
1192                                 NULL, NULL,
1193                                 param_missing_gost_ec, param_copy_gost_ec,
1194                                 param_cmp_gost_ec, NULL);
1195
1196         EVP_PKEY_asn1_set_public(*ameth,
1197                                  pub_decode_gost_ec, pub_encode_gost_ec,
1198                                  pub_cmp_gost_ec, pub_print_gost_ec,
1199                                  pkey_size_gost, pkey_bits_gost);
1200
1201         EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1202         EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1203         break;
1204     case NID_id_Gost28147_89_MAC:
1205         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1206         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
1207         break;
1208     case NID_gost_mac_12:
1209         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1210         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
1211         break;
1212     case NID_magma_mac:
1213         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1214         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma);
1215         break;
1216     case NID_grasshopper_mac:
1217         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1218         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper);
1219         break;
1220     }
1221     return 1;
1222 }