]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ameth.c
OMACs implementation. Unfinished.
[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_2012_256:
47         return 256;
48     case NID_id_GostR3410_2012_512:
49         return 512;
50     }
51
52     return -1;
53 }
54
55 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
56 {
57     ASN1_STRING *params = ASN1_STRING_new();
58     GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
59     int pkey_param_nid = NID_undef;
60     void *key_ptr = EVP_PKEY_get0((EVP_PKEY *)key);
61     int result = 0;
62
63     if (!params || !gkp) {
64         GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
65         goto err;
66     }
67     switch (EVP_PKEY_base_id(key)) {
68     case NID_id_GostR3410_2012_256:
69         pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
70         gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_256);
71         break;
72     case NID_id_GostR3410_2012_512:
73         pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
74         gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_512);
75         break;
76     case NID_id_GostR3410_2001:
77         pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
78         gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
79         break;
80     }
81
82     if (pkey_param_nid == NID_undef) {
83         GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, GOST_R_INVALID_PARAMSET);
84         goto err;
85     }
86
87     gkp->key_params = OBJ_nid2obj(pkey_param_nid);
88     /*
89      * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
90      */
91     params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
92     if (params->length <= 0) {
93         GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
94         goto err;
95     }
96     params->type = V_ASN1_SEQUENCE;
97     result = 1;
98  err:
99     if (gkp)
100         GOST_KEY_PARAMS_free(gkp);
101     if (result == 0) {          /* if error */
102         if (params)
103             ASN1_STRING_free(params);
104         return NULL;
105     }
106     return params;
107 }
108
109 static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid)
110 {
111     void *key_ptr = EVP_PKEY_get0(pkey);
112
113     switch (pkey_nid) {
114     case NID_id_GostR3410_2012_256:
115     case NID_id_GostR3410_2012_512:
116     case NID_id_GostR3410_2001:
117         if (!key_ptr) {
118             key_ptr = EC_KEY_new();
119             if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) {
120                 EC_KEY_free(key_ptr);
121                 break;
122             }
123         }
124         return fill_GOST_EC_params(key_ptr, param_nid);
125     }
126
127     return 0;
128 }
129
130 /*
131  * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
132  * NID and parameters
133  */
134 static int decode_gost_algor_params(EVP_PKEY *pkey, const X509_ALGOR *palg)
135 {
136     const ASN1_OBJECT *palg_obj = NULL;
137     int ptype = V_ASN1_UNDEF;
138     int pkey_nid = NID_undef, param_nid = NID_undef;
139     ASN1_STRING *pval = NULL;
140     const unsigned char *p;
141     GOST_KEY_PARAMS *gkp = NULL;
142
143     if (!pkey || !palg)
144         return 0;
145     X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg);
146     if (ptype != V_ASN1_SEQUENCE) {
147         GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
148                 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
149         return 0;
150     }
151     p = pval->data;
152     pkey_nid = OBJ_obj2nid(palg_obj);
153
154     gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
155     if (!gkp) {
156         GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
157                 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
158         return 0;
159     }
160     param_nid = OBJ_obj2nid(gkp->key_params);
161     GOST_KEY_PARAMS_free(gkp);
162     if (!EVP_PKEY_set_type(pkey, pkey_nid)) {
163         GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
164         return 0;
165     }
166     return gost_decode_nid_params(pkey, pkey_nid, param_nid);
167 }
168
169 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
170 {
171     switch (EVP_PKEY_base_id(pkey)) {
172     case NID_id_GostR3410_2012_512:
173     case NID_id_GostR3410_2012_256:
174     case NID_id_GostR3410_2001:
175         {
176             EC_KEY *ec = EVP_PKEY_get0(pkey);
177             if (!ec) {
178                 ec = EC_KEY_new();
179                 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
180             }
181             if (!EC_KEY_set_private_key(ec, priv))
182                 return 0;
183             if (!EVP_PKEY_missing_parameters(pkey))
184                 gost_ec_compute_public(ec);
185             break;
186         }
187     default:
188         return 0;
189     }
190     return 1;
191 }
192
193 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
194 {
195     switch (EVP_PKEY_base_id(pkey)) {
196     case NID_id_GostR3410_2012_512:
197     case NID_id_GostR3410_2012_256:
198     case NID_id_GostR3410_2001:
199         {
200             EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
201             if (ec)
202                 return (BIGNUM *)EC_KEY_get0_private_key(ec);
203             break;
204         }
205     }
206     return NULL;
207 }
208
209 /*
210  * Control function
211  */
212 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
213 {
214     int nid = EVP_PKEY_base_id(pkey), md_nid = NID_undef;
215     X509_ALGOR *alg1 = NULL, *alg2 = NULL;
216
217     switch (nid) {
218     case NID_id_GostR3410_2012_512:
219         md_nid = NID_id_GostR3411_2012_512;
220         break;
221     case NID_id_GostR3410_2012_256:
222         md_nid = NID_id_GostR3411_2012_256;
223         break;
224     case NID_id_GostR3410_2001:
225     case NID_id_GostR3410_94:
226         md_nid = NID_id_GostR3411_94;
227         break;
228     default:
229         return -1;
230     }
231
232     switch (op) {
233     case ASN1_PKEY_CTRL_PKCS7_SIGN:
234         if (arg1 == 0) {
235             PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2, NULL,
236                                         &alg1, &alg2);
237             X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
238             X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
239         }
240         return 1;
241 #ifndef OPENSSL_NO_CMS
242     case ASN1_PKEY_CTRL_CMS_SIGN:
243         if (arg1 == 0) {
244             CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2, NULL, NULL,
245                                      &alg1, &alg2);
246             X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
247             X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
248         }
249         return 1;
250 #endif
251     case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
252         if (arg1 == 0) {
253             ASN1_STRING *params = encode_gost_algor_params(pkey);
254             if (!params) {
255                 return -1;
256             }
257             PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg1);
258             X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
259                             V_ASN1_SEQUENCE, params);
260         }
261         return 1;
262 #ifndef OPENSSL_NO_CMS
263     case ASN1_PKEY_CTRL_CMS_ENVELOPE:
264         if (arg1 == 0) {
265             ASN1_STRING *params = encode_gost_algor_params(pkey);
266             if (!params) {
267                 return -1;
268             }
269             CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
270                                              NULL, &alg1);
271             X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
272                             V_ASN1_SEQUENCE, params);
273         }
274         return 1;
275 #endif
276     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
277         *(int *)arg2 = md_nid;
278         return 2;
279     }
280
281     return -2;
282 }
283
284 /* --------------------- free functions * ------------------------------*/
285 static void pkey_free_gost_ec(EVP_PKEY *key)
286 {
287     EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key));
288 }
289
290 /* ------------------ private key functions  -----------------------------*/
291
292 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
293                                const unsigned char *buf, int len,
294                                int num_masks)
295 {
296     BIGNUM *pknum_masked = NULL, *q = NULL;
297     const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
298     const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
299
300     pknum_masked = hashsum2bn(buf, len);
301     if (!pknum_masked)
302         return NULL;
303
304     if (num_masks > 0) {
305         /*
306          * XXX Remove sign by gost94
307          */
308         const unsigned char *p = buf + num_masks * len;
309
310         q = BN_new();
311         if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
312             BN_free(pknum_masked);
313             pknum_masked = NULL;
314             goto end;
315         }
316
317         for (; p != buf; p -= len) {
318             BIGNUM *mask = hashsum2bn(p, len);
319             BN_CTX *ctx = BN_CTX_new();
320
321             BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
322
323             BN_CTX_free(ctx);
324             BN_free(mask);
325         }
326     }
327
328  end:
329     if (q)
330         BN_free(q);
331     return pknum_masked;
332 }
333
334 static int priv_decode_gost(EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf)
335 {
336     const unsigned char *pkey_buf = NULL, *p = NULL;
337     int priv_len = 0;
338     BIGNUM *pk_num = NULL;
339     int ret = 0;
340     const X509_ALGOR *palg = NULL;
341     const ASN1_OBJECT *palg_obj = NULL;
342     ASN1_INTEGER *priv_key = NULL;
343     int expected_key_len = 32;
344
345     if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
346         return 0;
347     p = pkey_buf;
348     if (!decode_gost_algor_params(pk, palg)) {
349         return 0;
350     }
351
352     expected_key_len = pkey_bits_gost(pk) > 0 ? pkey_bits_gost(pk) / 8 : 0;
353     if (expected_key_len == 0) {
354         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
355         return 0;
356     }
357
358     if (priv_len % expected_key_len == 0) {
359         /* Key is not wrapped but masked */
360         pk_num = unmask_priv_key(pk, pkey_buf, expected_key_len,
361                                  priv_len / expected_key_len - 1);
362     } else if (V_ASN1_OCTET_STRING == *p) {
363         /* New format - Little endian octet string */
364         ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
365         if (!s || ((s->length != 32) && (s->length != 64))) {
366             ASN1_STRING_free(s);
367             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
368             return 0;
369         }
370         pk_num = hashsum2bn(s->data, s->length);
371         ASN1_STRING_free(s);
372     } else if (V_ASN1_INTEGER == *p) {
373         priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
374         if (!priv_key) {
375             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
376             return 0;
377         }
378         pk_num = ASN1_INTEGER_to_BN(priv_key, NULL);
379         ASN1_INTEGER_free(priv_key);
380     } else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
381         MASKED_GOST_KEY *mgk = NULL;
382         mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
383
384         if (!mgk) {
385             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
386             return 0;
387         }
388
389         priv_len = mgk->masked_priv_key->length;
390         if (priv_len % expected_key_len) {
391             MASKED_GOST_KEY_free(mgk);
392             GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
393             return 0;
394         }
395
396         pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
397                                  expected_key_len,
398                                  priv_len / expected_key_len - 1);
399         MASKED_GOST_KEY_free(mgk);
400     } else {
401         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
402         return 0;
403     }
404
405     if (pk_num == NULL) {
406         GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
407         return 0;
408     }
409
410     ret = gost_set_priv_key(pk, pk_num);
411     BN_free(pk_num);
412     return ret;
413 }
414
415 /* ----------------------------------------------------------------------*/
416 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
417 {
418     ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
419     ASN1_STRING *params = encode_gost_algor_params(pk);
420     unsigned char *buf = NULL;
421     int key_len = pkey_bits_gost(pk), i = 0;
422     /* unmasked private key */
423     const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
424
425     if (!params) {
426         return 0;
427     }
428
429     key_len = (key_len < 0) ? 0 : key_len / 8;
430     if (key_len == 0 || !(buf = OPENSSL_malloc(key_len))) {
431         return 0;
432     }
433
434     if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
435         OPENSSL_free(buf);
436         return 0;
437     }
438
439     /* Convert buf to Little-endian */
440     for (i = 0; i < key_len / 2; i++) {
441         unsigned char tmp = buf[i];
442         buf[i] = buf[key_len - 1 - i];
443         buf[key_len - 1 - i] = tmp;
444     }
445
446     if(pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
447         ASN1_STRING *octet = NULL;
448         int priv_len = 0;
449         unsigned char *priv_buf = NULL;
450
451         octet = ASN1_STRING_new();
452         ASN1_OCTET_STRING_set(octet, buf, key_len);
453         priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
454         ASN1_STRING_free(octet);
455         OPENSSL_free(buf);
456
457         return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
458                            priv_buf, priv_len); 
459     }
460
461     return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
462                            buf, key_len);
463 }
464
465 /* --------- printing keys --------------------------------*/
466 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
467 {
468     BIGNUM *key;
469
470     if (!BIO_indent(out, indent, 128))
471         return 0;
472     BIO_printf(out, "Private key: ");
473     key = gost_get0_priv_key(pkey);
474     if (!key)
475         BIO_printf(out, "<undefined>");
476     else
477         BN_print(out, key);
478     BIO_printf(out, "\n");
479
480     return 1;
481 }
482
483 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
484 {
485     BN_CTX *ctx;
486     BIGNUM *X, *Y;
487     const EC_POINT *pubkey;
488     const EC_GROUP *group;
489     EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
490     int ok = 0;
491
492     ctx = BN_CTX_new();
493     if (!ctx) {
494         GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
495         return 0;
496     }
497
498     BN_CTX_start(ctx);
499     X = BN_CTX_get(ctx);
500     Y = BN_CTX_get(ctx);
501     pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
502     group = (key) ? EC_KEY_get0_group(key) : NULL;
503     if (!pubkey || !group)
504         goto err;
505
506     if (!EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y, ctx)) {
507         GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
508         goto err;
509     }
510     if (!BIO_indent(out, indent, 128))
511         goto err;
512     BIO_printf(out, "Public key:\n");
513     if (!BIO_indent(out, indent + 3, 128))
514         goto err;
515     BIO_printf(out, "X:");
516     BN_print(out, X);
517     BIO_printf(out, "\n");
518     if (!BIO_indent(out, indent + 3, 128))
519         goto err;
520     BIO_printf(out, "Y:");
521     BN_print(out, Y);
522     BIO_printf(out, "\n");
523     ok = 1;
524  err:
525     BN_CTX_end(ctx);
526     BN_CTX_free(ctx);
527
528     return ok;
529 }
530
531 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
532 {
533     EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
534     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
535     int param_nid;
536
537     if (!group)
538         return 0;
539
540     param_nid = EC_GROUP_get_curve_name(group);
541     if (!BIO_indent(out, indent, 128))
542         return 0;
543     BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
544
545     return 1;
546 }
547
548 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
549                          ASN1_PCTX *pctx, int type)
550 {
551     if (type == 2) {
552         if (print_gost_priv(out, pkey, indent) == 0)
553             return 0;
554     }
555     if (type >= 1) {
556         if (print_gost_ec_pub(out, pkey, indent) == 0)
557             return 0;
558     }
559
560     return print_gost_ec_param(out, pkey, indent);
561 }
562
563 static int param_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
564                                ASN1_PCTX *pctx)
565 {
566     return print_gost_ec(out, pkey, indent, pctx, 0);
567 }
568
569 static int pub_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
570                              ASN1_PCTX *pctx)
571 {
572     return print_gost_ec(out, pkey, indent, pctx, 1);
573 }
574
575 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
576                               ASN1_PCTX *pctx)
577 {
578     return print_gost_ec(out, pkey, indent, pctx, 2);
579 }
580
581 /* ---------------------------------------------------------------------*/
582 static int param_missing_gost_ec(const EVP_PKEY *pk)
583 {
584     const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
585     if (!ec)
586         return 1;
587     if (!EC_KEY_get0_group(ec))
588         return 1;
589     return 0;
590 }
591
592 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
593 {
594     EC_KEY *eto = EVP_PKEY_get0(to);
595     const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
596     if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
597         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_INCOMPATIBLE_ALGORITHMS);
598         return 0;
599     }
600     if (!efrom) {
601         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
602         return 0;
603     }
604     if (!eto) {
605         eto = EC_KEY_new();
606         if (!eto) {
607             GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
608             return 0;
609         }
610         if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
611             GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
612             EC_KEY_free(eto);
613             return 0;
614         }
615     }
616     if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
617         GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
618         return 0;
619     }
620     if (EC_KEY_get0_private_key(eto)) {
621         return gost_ec_compute_public(eto);
622     }
623     return 1;
624 }
625
626 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
627 {
628     const EC_GROUP *group_a, *group_b;
629     EC_KEY *ec_a = EVP_PKEY_get0((EVP_PKEY *)a);
630     EC_KEY *ec_b = EVP_PKEY_get0((EVP_PKEY *)b);
631     if (!ec_a || !ec_b)
632         return 0;
633
634     group_a = EC_KEY_get0_group(ec_a);
635     group_b = EC_KEY_get0_group(ec_b);
636     if (!group_a || !group_b)
637         return 0;
638
639     if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
640         return 1;
641     }
642     return 0;
643 }
644
645 /* ---------- Public key functions * --------------------------------------*/
646 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub)
647 {
648     X509_ALGOR *palg = NULL;
649     const unsigned char *pubkey_buf = NULL;
650     unsigned char *databuf;
651     ASN1_OBJECT *palgobj = NULL;
652     int pub_len;
653     EC_POINT *pub_key;
654     BIGNUM *X, *Y;
655     ASN1_OCTET_STRING *octet = NULL;
656     size_t len;
657     const EC_GROUP *group;
658
659     if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
660         return 0;
661     EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
662     if (!decode_gost_algor_params(pk, palg))
663         return 0;
664     group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
665     octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
666     if (!octet) {
667         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
668         return 0;
669     }
670     databuf = OPENSSL_malloc(octet->length);
671     if (databuf == NULL) {
672         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
673         ASN1_OCTET_STRING_free(octet);
674         return 0;
675     }
676
677                 BUF_reverse(databuf, octet->data, octet->length);
678     len = octet->length / 2;
679     ASN1_OCTET_STRING_free(octet);
680
681     Y = BN_bin2bn(databuf, len, NULL);
682     X = BN_bin2bn(databuf + len, len, NULL);
683     OPENSSL_free(databuf);
684     pub_key = EC_POINT_new(group);
685     if (!EC_POINT_set_affine_coordinates_GFp(group, pub_key, X, Y, NULL)) {
686         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
687         EC_POINT_free(pub_key);
688         BN_free(X);
689         BN_free(Y);
690         return 0;
691     }
692     BN_free(X);
693     BN_free(Y);
694     if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
695         GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
696         EC_POINT_free(pub_key);
697         return 0;
698     }
699     EC_POINT_free(pub_key);
700     return 1;
701
702 }
703
704 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
705 {
706     ASN1_OBJECT *algobj = NULL;
707     ASN1_OCTET_STRING *octet = NULL;
708     void *pval = NULL;
709     unsigned char *buf = NULL, *databuf = NULL;
710     int data_len, ret = -1;
711     const EC_POINT *pub_key;
712     BIGNUM *X = NULL, *Y = NULL, *order = NULL;
713     const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
714     int ptype = V_ASN1_UNDEF;
715
716     algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
717
718                 ASN1_STRING *params = encode_gost_algor_params(pk);
719                 pval = params;
720                 ptype = V_ASN1_SEQUENCE;
721
722     order = BN_new();
723     if (!order) {
724         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
725         goto err;
726     }
727     EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
728     pub_key = EC_KEY_get0_public_key(ec);
729     if (!pub_key) {
730         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
731         goto err;
732     }
733     X = BN_new();
734     Y = BN_new();
735     if (!X || !Y) {
736         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
737         goto err;
738     }
739     if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
740                                              pub_key, X, Y, NULL)) {
741         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
742         goto err;
743     }
744     data_len = 2 * BN_num_bytes(order);
745     databuf = OPENSSL_zalloc(data_len);
746     if (databuf == NULL) {
747         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
748         goto err;
749     }
750
751     store_bignum(X, databuf + data_len / 2, data_len / 2);
752     store_bignum(Y, databuf, data_len / 2);
753
754                 BUF_reverse(databuf, NULL, data_len);
755
756     octet = ASN1_OCTET_STRING_new();
757     if (octet == NULL) {
758         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
759         goto err;
760     }
761
762     if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
763         GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
764         goto err;
765     }
766
767     ret = i2d_ASN1_OCTET_STRING(octet, &buf);
768     ASN1_BIT_STRING_free(octet);
769  err:
770     if (X)
771         BN_free(X);
772     if (Y)
773         BN_free(Y);
774     if (order)
775         BN_free(order);
776     if (databuf)
777         OPENSSL_free(databuf);
778
779     if (ret < 0)
780         return 0;
781     return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
782 }
783
784 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
785 {
786     const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
787     const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
788     const EC_POINT *ka, *kb;
789     if (!ea || !eb)
790         return 0;
791     ka = EC_KEY_get0_public_key(ea);
792     kb = EC_KEY_get0_public_key(eb);
793     if (!ka || !kb)
794         return 0;
795     return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
796 }
797
798 static int pkey_size_gost(const EVP_PKEY *pk)
799 {
800     if (!pk)
801         return -1;
802
803     switch (EVP_PKEY_base_id(pk)) {
804     case NID_id_GostR3410_94:
805     case NID_id_GostR3410_2001:
806     case NID_id_GostR3410_2012_256:
807         return 64;
808     case NID_id_GostR3410_2012_512:
809         return 128;
810     }
811
812     return -1;
813 }
814
815 /* ---------------------- ASN1 METHOD for GOST MAC  -------------------*/
816 static void mackey_free_gost(EVP_PKEY *pk)
817 {
818     OPENSSL_free(EVP_PKEY_get0(pk));
819 }
820
821 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
822 {
823     switch (op) {
824     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
825         if (arg2) {
826             *(int *)arg2 = NID_id_Gost28147_89_MAC;
827             return 2;
828         }
829     }
830     return -2;
831 }
832
833 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
834 {
835     switch (op) {
836     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
837         if (arg2) {
838             *(int *)arg2 = NID_gost_mac_12;
839             return 2;
840         }
841     }
842     return -2;
843 }
844
845 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
846 {
847     int nid =
848         EC_GROUP_get_curve_name(EC_KEY_get0_group
849                                 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
850     return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
851 }
852
853 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
854                                  int derlen)
855 {
856     ASN1_OBJECT *obj = NULL;
857     int nid;
858     if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
859         return 0;
860     }
861     nid = OBJ_obj2nid(obj);
862     ASN1_OBJECT_free(obj);
863
864     return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
865 }
866
867 /* ----------------------------------------------------------------------*/
868 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
869                         const char *pemstr, const char *info)
870 {
871     *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
872     if (!*ameth)
873         return 0;
874     switch (nid) {
875     case NID_id_GostR3410_2001:
876         EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
877         EVP_PKEY_asn1_set_private(*ameth,
878                                   priv_decode_gost, priv_encode_gost,
879                                   priv_print_gost_ec);
880
881         EVP_PKEY_asn1_set_param(*ameth,
882                                 gost2001_param_decode, gost2001_param_encode,
883                                 param_missing_gost_ec, param_copy_gost_ec,
884                                 param_cmp_gost_ec, param_print_gost_ec);
885         EVP_PKEY_asn1_set_public(*ameth,
886                                  pub_decode_gost_ec, pub_encode_gost_ec,
887                                  pub_cmp_gost_ec, pub_print_gost_ec,
888                                  pkey_size_gost, pkey_bits_gost);
889
890         EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
891 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
892         EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
893 #endif
894         break;
895     case NID_id_GostR3410_2012_256:
896     case NID_id_GostR3410_2012_512:
897         EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
898         EVP_PKEY_asn1_set_private(*ameth,
899                                   priv_decode_gost, priv_encode_gost,
900                                   priv_print_gost_ec);
901
902         EVP_PKEY_asn1_set_param(*ameth,
903                                 NULL, NULL,
904                                 param_missing_gost_ec, param_copy_gost_ec,
905                                 param_cmp_gost_ec, NULL);
906
907         EVP_PKEY_asn1_set_public(*ameth,
908                                  pub_decode_gost_ec, pub_encode_gost_ec,
909                                  pub_cmp_gost_ec, pub_print_gost_ec,
910                                  pkey_size_gost, pkey_bits_gost);
911
912         EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
913 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
914         EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
915 #endif
916         break;
917     case NID_id_Gost28147_89_MAC:
918         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
919         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
920         break;
921     case NID_gost_mac_12:
922         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
923         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
924         break;
925 /* TODO                         
926     case NID_magma_mac:
927         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
928         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
929         break;
930     case NID_grasshopper_mac:
931         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
932         EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
933         break;
934 */                              
935     }
936     return 1;
937 }