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 switch (pkey_param_nid) {
71 case NID_id_GostR3410_2001_TestParamSet:
72 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
73 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
74 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
75 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
76 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
77 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_256);
80 case NID_id_GostR3410_2012_512:
81 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
82 switch (pkey_param_nid) {
83 case NID_id_tc26_gost_3410_2012_512_paramSetTest:
84 case NID_id_tc26_gost_3410_2012_512_paramSetA:
85 case NID_id_tc26_gost_3410_2012_512_paramSetB:
86 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_512);
89 case NID_id_GostR3410_2001:
90 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
91 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
95 if (pkey_param_nid == NID_undef) {
96 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, GOST_R_INVALID_PARAMSET);
100 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
102 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
104 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
105 if (params->length <= 0) {
106 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
109 params->type = V_ASN1_SEQUENCE;
113 GOST_KEY_PARAMS_free(gkp);
114 if (result == 0) { /* if error */
116 ASN1_STRING_free(params);
122 static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid)
124 void *key_ptr = EVP_PKEY_get0(pkey);
127 case NID_id_GostR3410_2012_256:
128 case NID_id_GostR3410_2012_512:
129 case NID_id_GostR3410_2001:
131 key_ptr = EC_KEY_new();
132 if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) {
133 EC_KEY_free(key_ptr);
137 return fill_GOST_EC_params(key_ptr, param_nid);
144 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
147 static int decode_gost_algor_params(EVP_PKEY *pkey,
148 const X509_ALGOR *palg)
150 const ASN1_OBJECT *palg_obj = NULL;
151 int ptype = V_ASN1_UNDEF;
152 int pkey_nid = NID_undef, param_nid = NID_undef;
153 ASN1_STRING *pval = NULL;
154 const unsigned char *p;
155 GOST_KEY_PARAMS *gkp = NULL;
159 X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg);
160 if (ptype != V_ASN1_SEQUENCE) {
161 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
162 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
166 pkey_nid = OBJ_obj2nid(palg_obj);
168 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
170 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
171 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
174 param_nid = OBJ_obj2nid(gkp->key_params);
175 GOST_KEY_PARAMS_free(gkp);
176 if (!EVP_PKEY_set_type(pkey, pkey_nid)) {
177 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
180 return gost_decode_nid_params(pkey, pkey_nid, param_nid);
183 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
185 switch (EVP_PKEY_base_id(pkey)) {
186 case NID_id_GostR3410_2012_512:
187 case NID_id_GostR3410_2012_256:
188 case NID_id_GostR3410_2001:
190 EC_KEY *ec = EVP_PKEY_get0(pkey);
193 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
195 if (!EC_KEY_set_private_key(ec, priv))
197 if (!EVP_PKEY_missing_parameters(pkey))
198 return gost_ec_compute_public(ec);
207 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
209 switch (EVP_PKEY_base_id(pkey)) {
210 case NID_id_GostR3410_2012_512:
211 case NID_id_GostR3410_2012_256:
212 case NID_id_GostR3410_2001:
214 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
216 return (BIGNUM *)EC_KEY_get0_private_key(ec);
224 * GOST CMS processing functions
226 /* FIXME reaarange declarations */
227 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub);
229 static int gost_cms_set_kari_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
233 ASN1_OCTET_STRING *ukm;
235 /* Deal with originator */
236 X509_ALGOR *pubalg = NULL;
237 ASN1_BIT_STRING *pubkey = NULL;
239 EVP_PKEY *peer_key = NULL;
240 X509_PUBKEY *tmp = NULL;
243 unsigned char shared_key[64];
244 size_t shared_key_size = 64;
245 const EVP_CIPHER *cipher = NULL;
247 if (CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm) == 0)
250 if (CMS_RecipientInfo_kari_get0_orig_id(ri, &pubalg, &pubkey, NULL, NULL, NULL) == 0)
253 nid = OBJ_obj2nid(alg->algorithm);
254 if (alg->parameter->type != V_ASN1_SEQUENCE)
258 case NID_kuznyechik_kexp15:
259 case NID_magma_kexp15:
260 cipher = EVP_get_cipherbynid(nid);
264 if (cipher == NULL) {
265 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_CIPHER_NOT_FOUND);
269 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_SET_IV,
270 ASN1_STRING_length(ukm), (void *)ASN1_STRING_get0_data(ukm)) <= 0)
273 if (pubkey != NULL && pubalg != NULL) {
274 const ASN1_OBJECT *paobj = NULL;
276 const void *param = NULL;
278 peer_key = EVP_PKEY_new();
279 tmp = X509_PUBKEY_new();
281 if ((peer_key == NULL) || (tmp == NULL)) {
282 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, ERR_R_MALLOC_FAILURE);
286 X509_ALGOR_get0(&paobj, &ptype, ¶m, pubalg);
288 if (X509_PUBKEY_set0_param(tmp, (ASN1_OBJECT *)paobj,
289 ptype, (void *)param,
290 (unsigned char *)ASN1_STRING_get0_data(pubkey),
291 ASN1_STRING_length(pubkey) ) == 0) {
292 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_PUBLIC_KEY_UNDEFINED);
296 if (pub_decode_gost_ec(peer_key, tmp) <= 0) {
297 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_DECODING_PUBLIC_KEY);
301 if (EVP_PKEY_derive_set_peer(pctx, peer_key) <= 0) {
302 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_SETTING_PEER_KEY);
307 if (EVP_PKEY_derive(pctx, shared_key, &shared_key_size) <= 0) {
308 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_COMPUTING_SHARED_KEY);
312 EVP_CIPHER_CTX_set_flags(CMS_RecipientInfo_kari_get0_ctx(ri), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
313 if (EVP_DecryptInit_ex(CMS_RecipientInfo_kari_get0_ctx(ri), cipher, NULL,
314 shared_key, ukm->data+24) == 0)
319 EVP_PKEY_free(peer_key);
321 X509_PUBKEY_free(tmp);
327 static int gost_cms_set_ktri_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
330 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
332 CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg);
334 switch (OBJ_obj2nid(alg->algorithm)) {
335 case NID_kuznyechik_kexp15:
336 gctx->cipher_nid = NID_kuznyechik_ctr;
339 case NID_magma_kexp15:
340 gctx->cipher_nid = NID_magma_ctr;
343 case NID_id_GostR3410_2001:
344 case NID_id_GostR3410_2001DH:
345 case NID_id_GostR3410_2012_256:
346 case NID_id_GostR3410_2012_512:
347 gctx->cipher_nid = NID_id_Gost28147_89;
351 GOSTerr(GOST_F_GOST_CMS_SET_KTRI_SHARED_INFO, GOST_R_UNSUPPORTED_RECIPIENT_INFO);
358 static int gost_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
360 switch(CMS_RecipientInfo_type(ri)) {
361 case CMS_RECIPINFO_AGREE:
362 return gost_cms_set_kari_shared_info(pctx, ri);
364 case CMS_RECIPINFO_TRANS:
365 return gost_cms_set_ktri_shared_info(pctx, ri);
369 GOSTerr(GOST_F_GOST_CMS_SET_SHARED_INFO, GOST_R_UNSUPPORTED_RECIPIENT_INFO);
375 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
377 int nid = EVP_PKEY_base_id(pkey), md_nid = NID_undef;
378 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
381 case NID_id_GostR3410_2012_512:
382 md_nid = NID_id_GostR3411_2012_512;
384 case NID_id_GostR3410_2012_256:
385 md_nid = NID_id_GostR3411_2012_256;
387 case NID_id_GostR3410_2001:
388 case NID_id_GostR3410_94:
389 md_nid = NID_id_GostR3411_94;
396 case ASN1_PKEY_CTRL_PKCS7_SIGN:
398 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2, NULL,
400 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
401 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
404 #ifndef OPENSSL_NO_CMS
405 case ASN1_PKEY_CTRL_CMS_SIGN:
407 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2, NULL, NULL,
409 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
410 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
414 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
415 if (arg1 == 0) { /* Encryption */
416 ASN1_STRING *params = encode_gost_algor_params(pkey);
420 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg1);
421 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
422 V_ASN1_SEQUENCE, params);
425 #ifndef OPENSSL_NO_CMS
426 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
428 ASN1_STRING *params = encode_gost_algor_params(pkey);
432 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
434 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
435 V_ASN1_SEQUENCE, params);
438 CMS_RecipientInfo *ri = arg2;
439 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
442 return gost_cms_set_shared_info(pctx, ri);
445 #ifdef ASN1_PKEY_CTRL_CMS_RI_TYPE
446 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
447 *(int *)arg2 = CMS_RECIPINFO_TRANS;
449 case ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED:
450 if (arg1 == CMS_RECIPINFO_AGREE || arg1 == CMS_RECIPINFO_TRANS) {
459 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
460 *(int *)arg2 = md_nid;
467 /* --------------------- free functions * ------------------------------*/
468 static void pkey_free_gost_ec(EVP_PKEY *key)
470 EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key));
473 /* ------------------ private key functions -----------------------------*/
475 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
476 const unsigned char *buf, int len, int num_masks)
478 BIGNUM *pknum_masked = NULL, *q = NULL;
479 const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
480 const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
482 pknum_masked = BN_lebin2bn(buf, len, BN_secure_new());
488 * XXX Remove sign by gost94
490 const unsigned char *p = buf + num_masks * len;
493 if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
494 BN_free(pknum_masked);
499 for (; p != buf; p -= len) {
500 BIGNUM *mask = BN_lebin2bn(p, len, BN_secure_new());
501 BN_CTX *ctx = BN_CTX_secure_new();
503 BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
516 static int priv_decode_gost(EVP_PKEY *pk,
517 const PKCS8_PRIV_KEY_INFO *p8inf)
519 const unsigned char *pkey_buf = NULL, *p = NULL;
521 BIGNUM *pk_num = NULL;
523 const X509_ALGOR *palg = NULL;
524 const ASN1_OBJECT *palg_obj = NULL;
525 ASN1_INTEGER *priv_key = NULL;
526 int expected_key_len;
528 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
531 if (!decode_gost_algor_params(pk, palg)) {
535 expected_key_len = pkey_bits_gost(pk) > 0 ? pkey_bits_gost(pk) / 8 : 0;
536 if (expected_key_len == 0) {
537 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
541 if (priv_len % expected_key_len == 0) {
542 /* Key is not wrapped but masked */
543 pk_num = unmask_priv_key(pk, pkey_buf, expected_key_len,
544 priv_len / expected_key_len - 1);
545 } else if (V_ASN1_OCTET_STRING == *p) {
546 /* New format - Little endian octet string */
547 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
548 if (!s || ((s->length != 32) && (s->length != 64))) {
550 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
553 pk_num = BN_lebin2bn(s->data, s->length, BN_secure_new());
555 } else if (V_ASN1_INTEGER == *p) {
556 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
558 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
561 pk_num = ASN1_INTEGER_to_BN(priv_key, BN_secure_new());
562 ASN1_INTEGER_free(priv_key);
563 } else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
564 MASKED_GOST_KEY *mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
567 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
571 priv_len = mgk->masked_priv_key->length;
572 if (priv_len % expected_key_len) {
573 MASKED_GOST_KEY_free(mgk);
574 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
578 pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
580 priv_len / expected_key_len - 1);
581 MASKED_GOST_KEY_free(mgk);
583 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
587 if (pk_num == NULL) {
588 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
592 ret = gost_set_priv_key(pk, pk_num);
597 /* ----------------------------------------------------------------------*/
598 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
600 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
601 ASN1_STRING *params = NULL;
602 unsigned char *buf = NULL;
603 int key_len = pkey_bits_gost(pk), i = 0;
604 /* unmasked private key */
605 const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
607 key_len = (key_len < 0) ? 0 : key_len / 8;
608 if (key_len == 0 || !(buf = OPENSSL_secure_malloc(key_len))) {
612 if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
613 OPENSSL_secure_free(buf);
617 params = encode_gost_algor_params(pk);
619 OPENSSL_secure_free(buf);
623 /* Convert buf to Little-endian */
624 for (i = 0; i < key_len / 2; i++) {
625 unsigned char tmp = buf[i];
626 buf[i] = buf[key_len - 1 - i];
627 buf[key_len - 1 - i] = tmp;
630 if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
631 ASN1_STRING *octet = ASN1_STRING_new();
633 unsigned char *priv_buf = NULL;
634 if (!octet || !ASN1_OCTET_STRING_set(octet, buf, key_len)) {
635 ASN1_STRING_free(octet);
636 ASN1_STRING_free(params);
637 OPENSSL_secure_free(buf);
640 priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
641 ASN1_STRING_free(octet);
642 OPENSSL_secure_free(buf);
644 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
648 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
652 /* --------- printing keys --------------------------------*/
653 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
657 if (!BIO_indent(out, indent, 128))
659 BIO_printf(out, "Private key: ");
660 key = gost_get0_priv_key(pkey);
662 BIO_printf(out, "<undefined>");
665 BIO_printf(out, "\n");
670 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
674 const EC_POINT *pubkey;
675 const EC_GROUP *group;
676 EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
681 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
688 pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
689 group = (key) ? EC_KEY_get0_group(key) : NULL;
690 if (!pubkey || !group)
693 if (!EC_POINT_get_affine_coordinates(group, pubkey, X, Y, ctx)) {
694 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
697 if (!BIO_indent(out, indent, 128))
699 BIO_printf(out, "Public key:\n");
700 if (!BIO_indent(out, indent + 3, 128))
702 BIO_printf(out, "X:");
704 BIO_printf(out, "\n");
705 if (!BIO_indent(out, indent + 3, 128))
707 BIO_printf(out, "Y:");
709 BIO_printf(out, "\n");
718 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
720 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
721 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
727 param_nid = EC_GROUP_get_curve_name(group);
728 if (!BIO_indent(out, indent, 128))
730 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
735 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
736 ASN1_PCTX *pctx, int type)
739 if (print_gost_priv(out, pkey, indent) == 0)
743 if (print_gost_ec_pub(out, pkey, indent) == 0)
747 return print_gost_ec_param(out, pkey, indent);
750 static int param_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
753 return print_gost_ec(out, pkey, indent, pctx, 0);
756 static int pub_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
759 return print_gost_ec(out, pkey, indent, pctx, 1);
762 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
765 return print_gost_ec(out, pkey, indent, pctx, 2);
768 /* ---------------------------------------------------------------------*/
769 static int param_missing_gost_ec(const EVP_PKEY *pk)
771 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
774 if (!EC_KEY_get0_group(ec))
779 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
781 EC_KEY *eto = EVP_PKEY_get0(to);
782 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
783 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
784 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_INCOMPATIBLE_ALGORITHMS);
788 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
794 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
797 if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
798 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
803 if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
804 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
807 if (EC_KEY_get0_private_key(eto)) {
808 return gost_ec_compute_public(eto);
813 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
815 const EC_GROUP *group_a, *group_b;
816 EC_KEY *ec_a = EVP_PKEY_get0((EVP_PKEY *)a);
817 EC_KEY *ec_b = EVP_PKEY_get0((EVP_PKEY *)b);
821 group_a = EC_KEY_get0_group(ec_a);
822 group_b = EC_KEY_get0_group(ec_b);
823 if (!group_a || !group_b)
826 if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
832 /* ---------- Public key functions * --------------------------------------*/
833 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub)
835 X509_ALGOR *palg = NULL;
836 const unsigned char *pubkey_buf = NULL;
837 unsigned char *databuf;
838 ASN1_OBJECT *palgobj = NULL;
842 ASN1_OCTET_STRING *octet = NULL;
844 const EC_GROUP *group;
846 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
848 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
849 if (!decode_gost_algor_params(pk, palg))
851 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
852 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
854 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
857 databuf = OPENSSL_malloc(octet->length);
858 if (databuf == NULL) {
859 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
860 ASN1_OCTET_STRING_free(octet);
864 BUF_reverse(databuf, octet->data, octet->length);
865 len = octet->length / 2;
866 ASN1_OCTET_STRING_free(octet);
868 Y = BN_bin2bn(databuf, len, NULL);
869 X = BN_bin2bn(databuf + len, len, NULL);
870 OPENSSL_free(databuf);
871 pub_key = EC_POINT_new(group);
872 if (!EC_POINT_set_affine_coordinates(group, pub_key, X, Y, NULL)) {
873 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
874 EC_POINT_free(pub_key);
881 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
882 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
883 EC_POINT_free(pub_key);
886 EC_POINT_free(pub_key);
891 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
894 ASN1_OCTET_STRING *octet = NULL;
896 unsigned char *buf = NULL, *databuf = NULL;
897 int data_len, ret = -1;
898 const EC_POINT *pub_key;
899 BIGNUM *X = NULL, *Y = NULL, *order;
900 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
901 int ptype = V_ASN1_SEQUENCE;
904 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
906 params = encode_gost_algor_params(pk);
911 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
914 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
915 pub_key = EC_KEY_get0_public_key(ec);
917 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
923 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
926 if (!EC_POINT_get_affine_coordinates(EC_KEY_get0_group(ec),
927 pub_key, X, Y, NULL)) {
928 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
931 data_len = 2 * BN_num_bytes(order);
932 databuf = OPENSSL_zalloc(data_len);
933 if (databuf == NULL) {
934 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
938 store_bignum(X, databuf + data_len / 2, data_len / 2);
939 store_bignum(Y, databuf, data_len / 2);
941 BUF_reverse(databuf, NULL, data_len);
943 octet = ASN1_OCTET_STRING_new();
945 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
949 if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
950 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
954 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
956 ASN1_BIT_STRING_free(octet);
964 OPENSSL_free(databuf);
968 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
971 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
973 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
974 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
975 const EC_POINT *ka, *kb;
978 ka = EC_KEY_get0_public_key(ea);
979 kb = EC_KEY_get0_public_key(eb);
982 return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
985 static int pkey_size_gost(const EVP_PKEY *pk)
990 switch (EVP_PKEY_base_id(pk)) {
991 case NID_id_GostR3410_94:
992 case NID_id_GostR3410_2001:
993 case NID_id_GostR3410_2012_256:
995 case NID_id_GostR3410_2012_512:
1002 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
1003 static void mackey_free_gost(EVP_PKEY *pk)
1005 OPENSSL_free(EVP_PKEY_get0(pk));
1008 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1011 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1013 *(int *)arg2 = NID_id_Gost28147_89_MAC;
1020 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1023 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1025 *(int *)arg2 = NID_gost_mac_12;
1032 static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1035 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1037 *(int *)arg2 = NID_magma_mac;
1044 static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1047 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1049 *(int *)arg2 = NID_grasshopper_mac;
1056 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
1059 EC_GROUP_get_curve_name(EC_KEY_get0_group
1060 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
1061 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
1064 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
1067 ASN1_OBJECT *obj = NULL;
1069 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
1072 nid = OBJ_obj2nid(obj);
1073 ASN1_OBJECT_free(obj);
1075 return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
1078 /* ----------------------------------------------------------------------*/
1079 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
1080 const char *pemstr, const char *info)
1082 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
1086 case NID_id_GostR3410_2001:
1087 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1088 EVP_PKEY_asn1_set_private(*ameth,
1089 priv_decode_gost, priv_encode_gost,
1090 priv_print_gost_ec);
1092 EVP_PKEY_asn1_set_param(*ameth,
1093 gost2001_param_decode, gost2001_param_encode,
1094 param_missing_gost_ec, param_copy_gost_ec,
1095 param_cmp_gost_ec, param_print_gost_ec);
1096 EVP_PKEY_asn1_set_public(*ameth,
1097 pub_decode_gost_ec, pub_encode_gost_ec,
1098 pub_cmp_gost_ec, pub_print_gost_ec,
1099 pkey_size_gost, pkey_bits_gost);
1101 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1102 EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1104 case NID_id_GostR3410_2012_256:
1105 case NID_id_GostR3410_2012_512:
1106 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1107 EVP_PKEY_asn1_set_private(*ameth,
1108 priv_decode_gost, priv_encode_gost,
1109 priv_print_gost_ec);
1111 EVP_PKEY_asn1_set_param(*ameth,
1113 param_missing_gost_ec, param_copy_gost_ec,
1114 param_cmp_gost_ec, NULL);
1116 EVP_PKEY_asn1_set_public(*ameth,
1117 pub_decode_gost_ec, pub_encode_gost_ec,
1118 pub_cmp_gost_ec, pub_print_gost_ec,
1119 pkey_size_gost, pkey_bits_gost);
1121 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1122 EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1124 case NID_id_Gost28147_89_MAC:
1125 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1126 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
1128 case NID_gost_mac_12:
1129 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1130 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
1133 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1134 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma);
1136 case NID_grasshopper_mac:
1137 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1138 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper);