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>
20 #include "e_gost_err.h"
22 #define PK_WRAP_PARAM "LEGACY_PK_WRAP"
25 * Pack bignum into byte buffer of given size, filling all leading bytes by
28 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len)
30 int bytes = BN_num_bytes(bn);
35 BN_bn2bin(bn, buf + len - bytes);
39 static int pkey_bits_gost(const EVP_PKEY *pk)
44 switch (EVP_PKEY_base_id(pk)) {
45 case NID_id_GostR3410_2001:
46 case NID_id_GostR3410_2012_256:
48 case NID_id_GostR3410_2012_512:
55 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
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);
63 if (!params || !gkp) {
64 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
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);
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);
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);
82 if (pkey_param_nid == NID_undef) {
83 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, GOST_R_INVALID_PARAMSET);
87 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
89 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
91 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
92 if (params->length <= 0) {
93 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
96 params->type = V_ASN1_SEQUENCE;
100 GOST_KEY_PARAMS_free(gkp);
101 if (result == 0) { /* if error */
103 ASN1_STRING_free(params);
109 static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid)
111 void *key_ptr = EVP_PKEY_get0(pkey);
114 case NID_id_GostR3410_2012_256:
115 case NID_id_GostR3410_2012_512:
116 case NID_id_GostR3410_2001:
118 key_ptr = EC_KEY_new();
119 if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) {
120 EC_KEY_free(key_ptr);
124 return fill_GOST_EC_params(key_ptr, param_nid);
131 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
134 static int decode_gost_algor_params(EVP_PKEY *pkey, const X509_ALGOR *palg)
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;
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);
152 pkey_nid = OBJ_obj2nid(palg_obj);
154 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
156 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
157 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
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);
166 return gost_decode_nid_params(pkey, pkey_nid, param_nid);
169 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
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:
176 EC_KEY *ec = EVP_PKEY_get0(pkey);
179 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
181 if (!EC_KEY_set_private_key(ec, priv))
183 if (!EVP_PKEY_missing_parameters(pkey))
184 gost_ec_compute_public(ec);
193 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
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:
200 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
202 return (BIGNUM *)EC_KEY_get0_private_key(ec);
212 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
214 int nid = EVP_PKEY_base_id(pkey), md_nid = NID_undef;
215 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
218 case NID_id_GostR3410_2012_512:
219 md_nid = NID_id_GostR3411_2012_512;
221 case NID_id_GostR3410_2012_256:
222 md_nid = NID_id_GostR3411_2012_256;
224 case NID_id_GostR3410_2001:
225 case NID_id_GostR3410_94:
226 md_nid = NID_id_GostR3411_94;
233 case ASN1_PKEY_CTRL_PKCS7_SIGN:
235 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2, NULL,
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);
241 #ifndef OPENSSL_NO_CMS
242 case ASN1_PKEY_CTRL_CMS_SIGN:
244 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2, NULL, NULL,
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);
251 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
253 ASN1_STRING *params = encode_gost_algor_params(pkey);
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);
262 #ifndef OPENSSL_NO_CMS
263 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
265 ASN1_STRING *params = encode_gost_algor_params(pkey);
269 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
271 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
272 V_ASN1_SEQUENCE, params);
276 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
277 *(int *)arg2 = md_nid;
284 /* --------------------- free functions * ------------------------------*/
285 static void pkey_free_gost_ec(EVP_PKEY *key)
287 EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key));
290 /* ------------------ private key functions -----------------------------*/
292 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
293 const unsigned char *buf, int len,
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;
300 pknum_masked = hashsum2bn(buf, len);
306 * XXX Remove sign by gost94
308 const unsigned char *p = buf + num_masks * len;
311 if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
312 BN_free(pknum_masked);
317 for (; p != buf; p -= len) {
318 BIGNUM *mask = hashsum2bn(p, len);
319 BN_CTX *ctx = BN_CTX_new();
321 BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
334 static int priv_decode_gost(EVP_PKEY *pk, const PKCS8_PRIV_KEY_INFO *p8inf)
336 const unsigned char *pkey_buf = NULL, *p = NULL;
338 BIGNUM *pk_num = NULL;
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;
345 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
348 if (!decode_gost_algor_params(pk, palg)) {
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);
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))) {
367 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
370 pk_num = hashsum2bn(s->data, s->length);
372 } else if (V_ASN1_INTEGER == *p) {
373 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
375 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
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);
385 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
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);
396 pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
398 priv_len / expected_key_len - 1);
399 MASKED_GOST_KEY_free(mgk);
401 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
405 if (pk_num == NULL) {
406 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
410 ret = gost_set_priv_key(pk, pk_num);
415 /* ----------------------------------------------------------------------*/
416 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
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;
427 key_len = (key_len < 0) ? 0 : key_len / 8;
428 if (key_len == 0 || !(buf = OPENSSL_malloc(key_len))) {
432 if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
437 /* Convert buf to Little-endian */
438 for (i = 0; i < key_len / 2; i++) {
439 unsigned char tmp = buf[i];
440 buf[i] = buf[key_len - 1 - i];
441 buf[key_len - 1 - i] = tmp;
444 /* unmasked private key */
445 const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
446 if(pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
447 ASN1_STRING *octet = NULL;
449 unsigned char *priv_buf = NULL;
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);
457 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
461 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
465 /* --------- printing keys --------------------------------*/
466 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
470 if (!BIO_indent(out, indent, 128))
472 BIO_printf(out, "Private key: ");
473 key = gost_get0_priv_key(pkey);
475 BIO_printf(out, "<undefined>");
478 BIO_printf(out, "\n");
483 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
487 const EC_POINT *pubkey;
488 const EC_GROUP *group;
489 EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
494 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
501 pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
502 group = (key) ? EC_KEY_get0_group(key) : NULL;
503 if (!pubkey || !group)
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);
510 if (!BIO_indent(out, indent, 128))
512 BIO_printf(out, "Public key:\n");
513 if (!BIO_indent(out, indent + 3, 128))
515 BIO_printf(out, "X:");
517 BIO_printf(out, "\n");
518 if (!BIO_indent(out, indent + 3, 128))
520 BIO_printf(out, "Y:");
522 BIO_printf(out, "\n");
531 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
533 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
534 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
540 param_nid = EC_GROUP_get_curve_name(group);
541 if (!BIO_indent(out, indent, 128))
543 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
548 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
549 ASN1_PCTX *pctx, int type)
552 if (print_gost_priv(out, pkey, indent) == 0)
556 if (print_gost_ec_pub(out, pkey, indent) == 0)
560 return print_gost_ec_param(out, pkey, indent);
563 static int param_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
566 return print_gost_ec(out, pkey, indent, pctx, 0);
569 static int pub_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
572 return print_gost_ec(out, pkey, indent, pctx, 1);
575 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
578 return print_gost_ec(out, pkey, indent, pctx, 2);
581 /* ---------------------------------------------------------------------*/
582 static int param_missing_gost_ec(const EVP_PKEY *pk)
584 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
587 if (!EC_KEY_get0_group(ec))
592 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
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);
601 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
607 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
610 if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
611 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
616 if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
617 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
620 if (EC_KEY_get0_private_key(eto)) {
621 return gost_ec_compute_public(eto);
626 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
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);
634 group_a = EC_KEY_get0_group(ec_a);
635 group_b = EC_KEY_get0_group(ec_b);
636 if (!group_a || !group_b)
639 if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
645 /* ---------- Public key functions * --------------------------------------*/
646 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub)
648 X509_ALGOR *palg = NULL;
649 const unsigned char *pubkey_buf = NULL;
650 unsigned char *databuf;
651 ASN1_OBJECT *palgobj = NULL;
655 ASN1_OCTET_STRING *octet = NULL;
657 const EC_GROUP *group;
659 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
661 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
662 if (!decode_gost_algor_params(pk, palg))
664 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
665 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
667 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
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);
677 BUF_reverse(databuf, octet->data, octet->length);
678 len = octet->length / 2;
679 ASN1_OCTET_STRING_free(octet);
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);
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);
699 EC_POINT_free(pub_key);
704 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
706 ASN1_OBJECT *algobj = NULL;
707 ASN1_OCTET_STRING *octet = 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;
716 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
718 ASN1_STRING *params = encode_gost_algor_params(pk);
720 ptype = V_ASN1_SEQUENCE;
724 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
727 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
728 pub_key = EC_KEY_get0_public_key(ec);
730 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
736 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
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);
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);
751 store_bignum(X, databuf + data_len / 2, data_len / 2);
752 store_bignum(Y, databuf, data_len / 2);
754 BUF_reverse(databuf, NULL, data_len);
756 octet = ASN1_OCTET_STRING_new();
758 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
762 if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
763 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
767 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
768 ASN1_BIT_STRING_free(octet);
777 OPENSSL_free(databuf);
781 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
784 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
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;
791 ka = EC_KEY_get0_public_key(ea);
792 kb = EC_KEY_get0_public_key(eb);
795 return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
798 static int pkey_size_gost(const EVP_PKEY *pk)
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:
808 case NID_id_GostR3410_2012_512:
815 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
816 static void mackey_free_gost(EVP_PKEY *pk)
818 OPENSSL_free(EVP_PKEY_get0(pk));
821 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
824 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
826 *(int *)arg2 = NID_id_Gost28147_89_MAC;
833 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
836 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
838 *(int *)arg2 = NID_gost_mac_12;
845 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
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);
853 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
856 ASN1_OBJECT *obj = NULL;
858 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
861 nid = OBJ_obj2nid(obj);
862 ASN1_OBJECT_free(obj);
864 return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
867 /* ----------------------------------------------------------------------*/
868 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
869 const char *pemstr, const char *info)
871 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
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,
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);
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);
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,
902 EVP_PKEY_asn1_set_param(*ameth,
904 param_missing_gost_ec, param_copy_gost_ec,
905 param_cmp_gost_ec, NULL);
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);
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);
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);
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);