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