1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implementation of RFC 4490/4491 ASN1 method *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
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>
19 #include "gost_params.h"
21 #include "e_gost_err.h"
23 int gost94_nid_by_params(DSA *p)
25 R3410_params *gost_params;
27 for (gost_params = R3410_paramset; gost_params->q != NULL; gost_params++) {
28 BN_dec2bn(&q, gost_params->q);
29 if (!BN_cmp(q, p->q)) {
31 return gost_params->nid;
38 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
40 ASN1_STRING *params = ASN1_STRING_new();
41 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
42 int pkey_param_nid = NID_undef;
44 if (!params || !gkp) {
45 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
46 ASN1_STRING_free(params);
50 switch (EVP_PKEY_base_id(key)) {
51 case NID_id_GostR3410_2001:
53 EC_GROUP_get_curve_name(EC_KEY_get0_group
54 (EVP_PKEY_get0((EVP_PKEY *)key)));
56 case NID_id_GostR3410_94:
58 (int)gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)key));
59 if (pkey_param_nid == NID_undef) {
60 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
61 GOST_R_INVALID_GOST94_PARMSET);
62 ASN1_STRING_free(params);
68 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
69 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
71 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
73 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
74 if (params->length <= 0) {
75 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
76 ASN1_STRING_free(params);
80 params->type = V_ASN1_SEQUENCE;
82 GOST_KEY_PARAMS_free(gkp);
87 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
90 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
92 ASN1_OBJECT *palg_obj = NULL;
93 int ptype = V_ASN1_UNDEF;
94 int pkey_nid = NID_undef, param_nid = NID_undef;
96 ASN1_STRING *pval = NULL;
97 const unsigned char *p;
98 GOST_KEY_PARAMS *gkp = NULL;
100 X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
102 if (ptype != V_ASN1_SEQUENCE) {
103 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
104 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
108 pkey_nid = OBJ_obj2nid(palg_obj);
110 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
112 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
113 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
116 param_nid = OBJ_obj2nid(gkp->key_params);
117 GOST_KEY_PARAMS_free(gkp);
118 EVP_PKEY_set_type(pkey, pkey_nid);
120 case NID_id_GostR3410_94:
122 DSA *dsa = EVP_PKEY_get0(pkey);
125 if (!EVP_PKEY_assign(pkey, pkey_nid, dsa))
128 if (!fill_GOST94_params(dsa, param_nid))
132 case NID_id_GostR3410_2001:
134 EC_KEY *ec = EVP_PKEY_get0(pkey);
137 if (!EVP_PKEY_assign(pkey, pkey_nid, ec))
140 if (!fill_GOST2001_params(ec, param_nid))
148 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
150 switch (EVP_PKEY_base_id(pkey)) {
151 case NID_id_GostR3410_94:
153 DSA *dsa = EVP_PKEY_get0(pkey);
156 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), dsa);
158 dsa->priv_key = BN_dup(priv);
159 if (!EVP_PKEY_missing_parameters(pkey))
160 gost94_compute_public(dsa);
163 case NID_id_GostR3410_2001:
165 EC_KEY *ec = EVP_PKEY_get0(pkey);
168 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
170 if (!EC_KEY_set_private_key(ec, priv))
172 if (!EVP_PKEY_missing_parameters(pkey))
173 gost2001_compute_public(ec);
180 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
182 switch (EVP_PKEY_base_id(pkey)) {
183 case NID_id_GostR3410_94:
185 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
191 return dsa->priv_key;
194 case NID_id_GostR3410_2001:
196 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
201 if (!(priv = EC_KEY_get0_private_key(ec)))
203 return (BIGNUM *)priv;
210 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
213 case ASN1_PKEY_CTRL_PKCS7_SIGN:
215 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
216 int nid = EVP_PKEY_base_id(pkey);
217 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2,
219 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
221 if (nid == NID_undef) {
224 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
227 #ifndef OPENSSL_NO_CMS
228 case ASN1_PKEY_CTRL_CMS_SIGN:
230 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
231 int nid = EVP_PKEY_base_id(pkey);
232 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2,
233 NULL, NULL, &alg1, &alg2);
234 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
236 if (nid == NID_undef) {
239 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
243 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
246 ASN1_STRING *params = encode_gost_algor_params(pkey);
250 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg);
251 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
252 V_ASN1_SEQUENCE, params);
255 #ifndef OPENSSL_NO_CMS
256 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
258 X509_ALGOR *alg = NULL;
259 ASN1_STRING *params = encode_gost_algor_params(pkey);
263 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
265 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type), V_ASN1_SEQUENCE,
270 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
271 *(int *)arg2 = NID_id_GostR3411_94;
278 /* --------------------- free functions * ------------------------------*/
279 static void pkey_free_gost94(EVP_PKEY *key)
282 DSA_free(key->pkey.dsa);
286 static void pkey_free_gost01(EVP_PKEY *key)
289 EC_KEY_free(key->pkey.ec);
293 /* ------------------ private key functions -----------------------------*/
294 static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
296 const unsigned char *pkey_buf = NULL, *p = NULL;
298 BIGNUM *pk_num = NULL;
300 X509_ALGOR *palg = NULL;
301 ASN1_OBJECT *palg_obj = NULL;
302 ASN1_INTEGER *priv_key = NULL;
304 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
307 if (!decode_gost_algor_params(pk, palg)) {
310 if (V_ASN1_OCTET_STRING == *p) {
311 /* New format - Little endian octet string */
312 unsigned char rev_buf[32];
314 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
315 if (!s || s->length != 32) {
316 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
319 for (i = 0; i < 32; i++) {
320 rev_buf[31 - i] = s->data[i];
323 pk_num = getbnfrombuf(rev_buf, 32);
325 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
328 ret = ((pk_num = ASN1_INTEGER_to_BN(priv_key, NULL)) != NULL);
329 ASN1_INTEGER_free(priv_key);
331 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
336 ret = gost_set_priv_key(pk, pk_num);
341 /* ----------------------------------------------------------------------*/
342 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
344 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
345 ASN1_STRING *params = encode_gost_algor_params(pk);
346 unsigned char *priv_buf = NULL;
349 ASN1_INTEGER *asn1key = NULL;
353 asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk), NULL);
354 priv_len = i2d_ASN1_INTEGER(asn1key, &priv_buf);
355 ASN1_INTEGER_free(asn1key);
356 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
360 /* --------- printing keys --------------------------------*/
361 static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent,
362 ASN1_PCTX *pctx, int type)
364 int param_nid = NID_undef;
369 if (!BIO_indent(out, indent, 128))
371 BIO_printf(out, "Private key: ");
372 key = gost_get0_priv_key(pkey);
374 BIO_printf(out, "<undefined>");
377 BIO_printf(out, "\n");
382 pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
383 BIO_indent(out, indent, 128);
384 BIO_printf(out, "Public key: ");
385 BN_print(out, pubkey);
386 BIO_printf(out, "\n");
389 param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
390 BIO_indent(out, indent, 128);
391 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
395 static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
398 return print_gost_94(out, pkey, indent, pctx, 0);
401 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
404 return print_gost_94(out, pkey, indent, pctx, 1);
407 static int priv_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
410 return print_gost_94(out, pkey, indent, pctx, 2);
413 static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
414 ASN1_PCTX *pctx, int type)
416 int param_nid = NID_undef;
420 if (!BIO_indent(out, indent, 128))
422 BIO_printf(out, "Private key: ");
423 key = gost_get0_priv_key(pkey);
425 BIO_printf(out, "<undefined)");
428 BIO_printf(out, "\n");
431 BN_CTX *ctx = BN_CTX_new();
433 const EC_POINT *pubkey;
434 const EC_GROUP *group;
437 GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_MALLOC_FAILURE);
444 EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
445 group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
446 if (!EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y, ctx)) {
447 GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_EC_LIB);
451 if (!BIO_indent(out, indent, 128))
453 BIO_printf(out, "Public key:\n");
454 if (!BIO_indent(out, indent + 3, 128))
456 BIO_printf(out, "X:");
458 BIO_printf(out, "\n");
459 BIO_indent(out, indent + 3, 128);
460 BIO_printf(out, "Y:");
462 BIO_printf(out, "\n");
468 EC_GROUP_get_curve_name(EC_KEY_get0_group
469 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
470 if (!BIO_indent(out, indent, 128))
472 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
476 static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
479 return print_gost_01(out, pkey, indent, pctx, 0);
482 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
485 return print_gost_01(out, pkey, indent, pctx, 1);
488 static int priv_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
491 return print_gost_01(out, pkey, indent, pctx, 2);
494 /* ---------------------------------------------------------------------*/
495 static int param_missing_gost94(const EVP_PKEY *pk)
497 const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
505 static int param_missing_gost01(const EVP_PKEY *pk)
507 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
510 if (!EC_KEY_get0_group(ec))
515 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from)
517 const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
518 DSA *dto = EVP_PKEY_get0(to);
519 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
520 GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_INCOMPATIBLE_ALGORITHMS);
524 GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_KEY_PARAMETERS_MISSING);
529 EVP_PKEY_assign(to, EVP_PKEY_base_id(from), dto);
531 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);
532 COPYBIGNUM(dto, dfrom, p)
533 COPYBIGNUM(dto, dfrom, q)
534 COPYBIGNUM(dto, dfrom, g)
537 gost94_compute_public(dto);
541 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
543 EC_KEY *eto = EVP_PKEY_get0(to);
544 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
545 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
546 GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_INCOMPATIBLE_ALGORITHMS);
550 GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_KEY_PARAMETERS_MISSING);
555 EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto);
557 EC_KEY_set_group(eto, EC_KEY_get0_group(efrom));
558 if (EC_KEY_get0_private_key(eto)) {
559 gost2001_compute_public(eto);
564 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
566 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
567 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
568 if (!BN_cmp(da->q, db->q))
573 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
575 if (EC_GROUP_get_curve_name
576 (EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a))) ==
577 EC_GROUP_get_curve_name(EC_KEY_get0_group
578 (EVP_PKEY_get0((EVP_PKEY *)b)))) {
585 /* ---------- Public key functions * --------------------------------------*/
586 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
588 X509_ALGOR *palg = NULL;
589 const unsigned char *pubkey_buf = NULL;
590 unsigned char *databuf;
591 ASN1_OBJECT *palgobj = NULL;
594 ASN1_OCTET_STRING *octet = NULL;
596 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
598 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
599 if (!decode_gost_algor_params(pk, palg))
601 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
603 GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE);
606 databuf = OPENSSL_malloc(octet->length);
607 for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
608 databuf[j] = octet->data[i];
610 dsa = EVP_PKEY_get0(pk);
611 dsa->pub_key = BN_bin2bn(databuf, octet->length, NULL);
612 ASN1_OCTET_STRING_free(octet);
613 OPENSSL_free(databuf);
618 static int pub_encode_gost94(X509_PUBKEY *pub, const EVP_PKEY *pk)
620 ASN1_OBJECT *algobj = NULL;
621 ASN1_OCTET_STRING *octet = NULL;
623 unsigned char *buf = NULL, *databuf, *sptr;
624 int i, j, data_len, ret = 0;
626 int ptype = V_ASN1_UNDEF;
627 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
628 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
629 if (pk->save_parameters) {
630 ASN1_STRING *params = encode_gost_algor_params(pk);
632 ptype = V_ASN1_SEQUENCE;
634 data_len = BN_num_bytes(dsa->pub_key);
635 databuf = OPENSSL_malloc(data_len);
636 BN_bn2bin(dsa->pub_key, databuf);
637 octet = ASN1_OCTET_STRING_new();
638 ASN1_STRING_set(octet, NULL, data_len);
639 sptr = ASN1_STRING_data(octet);
640 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
641 sptr[i] = databuf[j];
643 OPENSSL_free(databuf);
644 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
645 ASN1_BIT_STRING_free(octet);
648 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
651 static int pub_decode_gost01(EVP_PKEY *pk, X509_PUBKEY *pub)
653 X509_ALGOR *palg = NULL;
654 const unsigned char *pubkey_buf = NULL;
655 unsigned char *databuf;
656 ASN1_OBJECT *palgobj = NULL;
660 ASN1_OCTET_STRING *octet = NULL;
662 const EC_GROUP *group;
664 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
666 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
667 if (!decode_gost_algor_params(pk, palg))
669 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
670 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
672 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
675 databuf = OPENSSL_malloc(octet->length);
676 for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
677 databuf[j] = octet->data[i];
679 len = octet->length / 2;
680 ASN1_OCTET_STRING_free(octet);
682 Y = getbnfrombuf(databuf, len);
683 X = getbnfrombuf(databuf + len, len);
684 OPENSSL_free(databuf);
685 pub_key = EC_POINT_new(group);
686 if (!EC_POINT_set_affine_coordinates_GFp(group, pub_key, X, Y, NULL)) {
687 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
688 EC_POINT_free(pub_key);
695 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
696 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
697 EC_POINT_free(pub_key);
700 EC_POINT_free(pub_key);
705 static int pub_encode_gost01(X509_PUBKEY *pub, const EVP_PKEY *pk)
707 ASN1_OBJECT *algobj = NULL;
708 ASN1_OCTET_STRING *octet = NULL;
710 unsigned char *buf = NULL, *databuf, *sptr;
711 int i, j, data_len, ret = 0;
712 const EC_POINT *pub_key;
713 BIGNUM *X, *Y, *order;
714 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
715 int ptype = V_ASN1_UNDEF;
717 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
718 if (pk->save_parameters) {
719 ASN1_STRING *params = encode_gost_algor_params(pk);
721 ptype = V_ASN1_SEQUENCE;
724 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
725 pub_key = EC_KEY_get0_public_key(ec);
727 GOSTerr(GOST_F_PUB_ENCODE_GOST01, GOST_R_PUBLIC_KEY_UNDEFINED);
732 EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
733 pub_key, X, Y, NULL);
734 data_len = 2 * BN_num_bytes(order);
736 databuf = OPENSSL_malloc(data_len);
737 memset(databuf, 0, data_len);
739 store_bignum(X, databuf + data_len / 2, data_len / 2);
740 store_bignum(Y, databuf, data_len / 2);
744 octet = ASN1_OCTET_STRING_new();
745 ASN1_STRING_set(octet, NULL, data_len);
746 sptr = ASN1_STRING_data(octet);
747 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
748 sptr[i] = databuf[j];
750 OPENSSL_free(databuf);
751 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
752 ASN1_BIT_STRING_free(octet);
755 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
758 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
760 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
761 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
762 if (da && db && da->pub_key && db->pub_key
763 && !BN_cmp(da->pub_key, db->pub_key)) {
769 static int pub_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
771 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
772 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
773 const EC_POINT *ka, *kb;
777 ka = EC_KEY_get0_public_key(ea);
778 kb = EC_KEY_get0_public_key(eb);
781 ret = (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
785 static int pkey_size_gost(const EVP_PKEY *pk)
790 static int pkey_bits_gost(const EVP_PKEY *pk)
795 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
796 static void mackey_free_gost(EVP_PKEY *pk)
799 OPENSSL_free(pk->pkey.ptr);
803 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
806 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
807 *(int *)arg2 = NID_id_Gost28147_89_MAC;
813 static int gost94_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
815 int nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
816 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
819 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
822 EC_GROUP_get_curve_name(EC_KEY_get0_group
823 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
824 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
827 static int gost94_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
830 ASN1_OBJECT *obj = NULL;
831 DSA *dsa = EVP_PKEY_get0(pkey);
833 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
836 nid = OBJ_obj2nid(obj);
837 ASN1_OBJECT_free(obj);
840 if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_94, dsa))
843 if (!fill_GOST94_params(dsa, nid))
848 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
851 ASN1_OBJECT *obj = NULL;
853 EC_KEY *ec = EVP_PKEY_get0(pkey);
854 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
857 nid = OBJ_obj2nid(obj);
858 ASN1_OBJECT_free(obj);
861 if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec))
864 if (!fill_GOST2001_params(ec, nid))
869 /* ----------------------------------------------------------------------*/
870 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
871 const char *pemstr, const char *info)
873 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
877 case NID_id_GostR3410_94:
878 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost94);
879 EVP_PKEY_asn1_set_private(*ameth,
880 priv_decode_gost, priv_encode_gost,
883 EVP_PKEY_asn1_set_param(*ameth,
884 gost94_param_decode, gost94_param_encode,
885 param_missing_gost94, param_copy_gost94,
886 param_cmp_gost94, param_print_gost94);
887 EVP_PKEY_asn1_set_public(*ameth,
888 pub_decode_gost94, pub_encode_gost94,
889 pub_cmp_gost94, pub_print_gost94,
890 pkey_size_gost, pkey_bits_gost);
892 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
894 case NID_id_GostR3410_2001:
895 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost01);
896 EVP_PKEY_asn1_set_private(*ameth,
897 priv_decode_gost, priv_encode_gost,
900 EVP_PKEY_asn1_set_param(*ameth,
901 gost2001_param_decode, gost2001_param_encode,
902 param_missing_gost01, param_copy_gost01,
903 param_cmp_gost01, param_print_gost01);
904 EVP_PKEY_asn1_set_public(*ameth,
905 pub_decode_gost01, pub_encode_gost01,
906 pub_cmp_gost01, pub_print_gost01,
907 pkey_size_gost, pkey_bits_gost);
909 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
911 case NID_id_Gost28147_89_MAC:
912 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
913 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);