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_2001DH:
47 case NID_id_GostR3410_2012_256:
49 case NID_id_GostR3410_2012_512:
56 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
58 ASN1_STRING *params = ASN1_STRING_new();
59 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
60 int pkey_param_nid = NID_undef;
61 void *key_ptr = EVP_PKEY_get0((EVP_PKEY *)key);
64 if (!params || !gkp) {
65 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
68 switch (EVP_PKEY_base_id(key)) {
69 case NID_id_GostR3410_2012_256:
70 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
71 switch (pkey_param_nid) {
72 case NID_id_GostR3410_2001_TestParamSet:
73 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
74 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
75 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
76 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
77 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
78 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_256);
81 case NID_id_GostR3410_2012_512:
82 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
83 switch (pkey_param_nid) {
84 case NID_id_tc26_gost_3410_2012_512_paramSetTest:
85 case NID_id_tc26_gost_3410_2012_512_paramSetA:
86 case NID_id_tc26_gost_3410_2012_512_paramSetB:
87 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_512);
90 case NID_id_GostR3410_2001:
91 case NID_id_GostR3410_2001DH:
92 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
93 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
97 if (pkey_param_nid == NID_undef) {
98 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, GOST_R_INVALID_PARAMSET);
102 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
104 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
106 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
107 if (params->length <= 0) {
108 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
111 params->type = V_ASN1_SEQUENCE;
115 GOST_KEY_PARAMS_free(gkp);
116 if (result == 0) { /* if error */
118 ASN1_STRING_free(params);
124 static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid)
126 void *key_ptr = EVP_PKEY_get0(pkey);
129 case NID_id_GostR3410_2012_256:
130 case NID_id_GostR3410_2012_512:
131 case NID_id_GostR3410_2001:
132 case NID_id_GostR3410_2001DH:
134 key_ptr = EC_KEY_new();
135 if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) {
136 EC_KEY_free(key_ptr);
140 return fill_GOST_EC_params(key_ptr, param_nid);
147 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
150 static int decode_gost_algor_params(EVP_PKEY *pkey,
151 const X509_ALGOR *palg)
153 const ASN1_OBJECT *palg_obj = NULL;
154 int ptype = V_ASN1_UNDEF;
155 int pkey_nid = NID_undef, param_nid = NID_undef;
156 ASN1_STRING *pval = NULL;
157 const unsigned char *p;
158 GOST_KEY_PARAMS *gkp = NULL;
162 X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg);
163 if (ptype != V_ASN1_SEQUENCE) {
164 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
165 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
169 pkey_nid = OBJ_obj2nid(palg_obj);
171 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
173 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
174 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
177 param_nid = OBJ_obj2nid(gkp->key_params);
178 GOST_KEY_PARAMS_free(gkp);
179 if (!EVP_PKEY_set_type(pkey, pkey_nid)) {
180 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
183 return gost_decode_nid_params(pkey, pkey_nid, param_nid);
186 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
188 switch (EVP_PKEY_base_id(pkey)) {
189 case NID_id_GostR3410_2012_512:
190 case NID_id_GostR3410_2012_256:
191 case NID_id_GostR3410_2001:
192 case NID_id_GostR3410_2001DH:
194 EC_KEY *ec = EVP_PKEY_get0(pkey);
197 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
199 if (!EC_KEY_set_private_key(ec, priv))
201 if (!EVP_PKEY_missing_parameters(pkey))
202 return gost_ec_compute_public(ec);
211 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
213 switch (EVP_PKEY_base_id(pkey)) {
214 case NID_id_GostR3410_2012_512:
215 case NID_id_GostR3410_2012_256:
216 case NID_id_GostR3410_2001:
217 case NID_id_GostR3410_2001DH:
219 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
221 return (BIGNUM *)EC_KEY_get0_private_key(ec);
229 * GOST CMS processing functions
231 /* FIXME reaarange declarations */
232 static int pub_decode_gost_ec(EVP_PKEY *pk, const X509_PUBKEY *pub);
234 static int gost_cms_set_kari_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
238 ASN1_OCTET_STRING *ukm;
240 /* Deal with originator */
241 X509_ALGOR *pubalg = NULL;
242 ASN1_BIT_STRING *pubkey = NULL;
244 EVP_PKEY *peer_key = NULL;
245 X509_PUBKEY *tmp = NULL;
248 unsigned char shared_key[64];
249 size_t shared_key_size = 64;
250 const EVP_CIPHER *cipher = NULL;
252 if (CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm) == 0)
255 if (CMS_RecipientInfo_kari_get0_orig_id(ri, &pubalg, &pubkey, NULL, NULL, NULL) == 0)
258 nid = OBJ_obj2nid(alg->algorithm);
259 if (alg->parameter->type != V_ASN1_SEQUENCE)
263 case NID_kuznyechik_kexp15:
264 case NID_magma_kexp15:
265 cipher = EVP_get_cipherbynid(nid);
269 if (cipher == NULL) {
270 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_CIPHER_NOT_FOUND);
274 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_SET_IV,
275 ASN1_STRING_length(ukm), (void *)ASN1_STRING_get0_data(ukm)) <= 0)
278 if (pubkey != NULL && pubalg != NULL) {
279 const ASN1_OBJECT *paobj = NULL;
281 const void *param = NULL;
283 peer_key = EVP_PKEY_new();
284 tmp = X509_PUBKEY_new();
286 if ((peer_key == NULL) || (tmp == NULL)) {
287 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, ERR_R_MALLOC_FAILURE);
291 X509_ALGOR_get0(&paobj, &ptype, ¶m, pubalg);
293 if (X509_PUBKEY_set0_param(tmp, (ASN1_OBJECT *)paobj,
294 ptype, (void *)param,
295 (unsigned char *)ASN1_STRING_get0_data(pubkey),
296 ASN1_STRING_length(pubkey) ) == 0) {
297 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_PUBLIC_KEY_UNDEFINED);
301 if (pub_decode_gost_ec(peer_key, tmp) <= 0) {
302 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_DECODING_PUBLIC_KEY);
306 if (EVP_PKEY_derive_set_peer(pctx, peer_key) <= 0) {
307 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_SETTING_PEER_KEY);
312 if (EVP_PKEY_derive(pctx, shared_key, &shared_key_size) <= 0) {
313 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_COMPUTING_SHARED_KEY);
317 EVP_CIPHER_CTX_set_flags(CMS_RecipientInfo_kari_get0_ctx(ri), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
318 if (EVP_DecryptInit_ex(CMS_RecipientInfo_kari_get0_ctx(ri), cipher, NULL,
319 shared_key, ukm->data+24) == 0)
324 EVP_PKEY_free(peer_key);
326 X509_PUBKEY_free(tmp);
332 static int gost_cms_set_ktri_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
335 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
337 CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg);
339 switch (OBJ_obj2nid(alg->algorithm)) {
340 case NID_kuznyechik_kexp15:
341 gctx->cipher_nid = NID_kuznyechik_ctr;
344 case NID_magma_kexp15:
345 gctx->cipher_nid = NID_magma_ctr;
348 case NID_id_GostR3410_2001:
349 case NID_id_GostR3410_2001DH:
350 case NID_id_GostR3410_2012_256:
351 case NID_id_GostR3410_2012_512:
352 gctx->cipher_nid = NID_id_Gost28147_89;
356 GOSTerr(GOST_F_GOST_CMS_SET_KTRI_SHARED_INFO, GOST_R_UNSUPPORTED_RECIPIENT_INFO);
363 static int gost_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
365 switch(CMS_RecipientInfo_type(ri)) {
366 case CMS_RECIPINFO_AGREE:
367 return gost_cms_set_kari_shared_info(pctx, ri);
369 case CMS_RECIPINFO_TRANS:
370 return gost_cms_set_ktri_shared_info(pctx, ri);
374 GOSTerr(GOST_F_GOST_CMS_SET_SHARED_INFO, GOST_R_UNSUPPORTED_RECIPIENT_INFO);
378 static ASN1_STRING *gost_encode_cms_params(int ka_nid)
380 ASN1_STRING *ret = NULL;
381 ASN1_STRING *params = ASN1_STRING_new();
383 /* It's a hack. We have only one OID here, so we can use
384 * GOST_KEY_PARAMS which is a sequence of 3 OIDs,
385 * the 1st one is mandatory and the rest are optional */
386 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
388 if (params == NULL || gkp == NULL) {
389 GOSTerr(GOST_F_GOST_ENCODE_CMS_PARAMS, ERR_R_MALLOC_FAILURE);
393 gkp->key_params = OBJ_nid2obj(ka_nid);
394 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
396 if (params->length < 0) {
397 GOSTerr(GOST_F_GOST_ENCODE_CMS_PARAMS, ERR_R_MALLOC_FAILURE);
401 params->type = V_ASN1_SEQUENCE;
405 GOST_KEY_PARAMS_free(gkp);
408 ASN1_STRING_free(params);
416 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
418 int nid = EVP_PKEY_base_id(pkey), md_nid = NID_undef;
419 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
422 case NID_id_GostR3410_2012_512:
423 md_nid = NID_id_GostR3411_2012_512;
425 case NID_id_GostR3410_2012_256:
426 md_nid = NID_id_GostR3411_2012_256;
428 case NID_id_GostR3410_2001:
429 case NID_id_GostR3410_2001DH:
430 case NID_id_GostR3410_94:
431 md_nid = NID_id_GostR3411_94;
438 case ASN1_PKEY_CTRL_PKCS7_SIGN:
440 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2, NULL,
442 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
443 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
446 #ifndef OPENSSL_NO_CMS
447 case ASN1_PKEY_CTRL_CMS_SIGN:
449 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2, NULL, NULL,
451 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
452 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
456 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
457 if (arg1 == 0) { /* Encryption */
458 ASN1_STRING *params = encode_gost_algor_params(pkey);
462 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg1);
463 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
464 V_ASN1_SEQUENCE, params);
467 #ifndef OPENSSL_NO_CMS
468 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
471 CMS_RecipientInfo *ri = arg2;
473 struct gost_pmeth_data *gctx = NULL;
474 ASN1_STRING *params = NULL;
476 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
480 gctx = EVP_PKEY_CTX_get_data(pctx);
482 switch (gctx->cipher_nid) {
484 case NID_kuznyechik_ctr:
488 nid = (gctx->cipher_nid == NID_magma_ctr) ? NID_magma_kexp15 :
489 NID_kuznyechik_kexp15;
491 ka_nid = (EVP_PKEY_base_id(pkey) == NID_id_GostR3410_2012_256) ?
492 NID_id_tc26_agreement_gost_3410_2012_256 : NID_id_tc26_agreement_gost_3410_2012_512;
494 params = gost_encode_cms_params(ka_nid);
498 params = encode_gost_algor_params(pkey);
505 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
507 X509_ALGOR_set0(alg1, OBJ_nid2obj(nid), V_ASN1_SEQUENCE, params);
510 CMS_RecipientInfo *ri = arg2;
511 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
514 return gost_cms_set_shared_info(pctx, ri);
517 #ifdef ASN1_PKEY_CTRL_CMS_RI_TYPE
518 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
519 *(int *)arg2 = CMS_RECIPINFO_TRANS;
521 case ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED:
522 if (arg1 == CMS_RECIPINFO_AGREE || arg1 == CMS_RECIPINFO_TRANS) {
531 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
532 *(int *)arg2 = md_nid;
539 /* --------------------- free functions * ------------------------------*/
540 static void pkey_free_gost_ec(EVP_PKEY *key)
542 EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key));
545 /* ------------------ private key functions -----------------------------*/
547 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
548 const unsigned char *buf, int len, int num_masks)
550 BIGNUM *pknum_masked = NULL, *q = NULL;
551 const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
552 const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
554 pknum_masked = BN_lebin2bn(buf, len, BN_secure_new());
560 * XXX Remove sign by gost94
562 const unsigned char *p = buf + num_masks * len;
565 if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
566 BN_free(pknum_masked);
571 for (; p != buf; p -= len) {
572 BIGNUM *mask = BN_lebin2bn(p, len, BN_secure_new());
573 BN_CTX *ctx = BN_CTX_secure_new();
575 BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
588 static int priv_decode_gost(EVP_PKEY *pk,
589 const PKCS8_PRIV_KEY_INFO *p8inf)
591 const unsigned char *pkey_buf = NULL, *p = NULL;
593 BIGNUM *pk_num = NULL;
595 const X509_ALGOR *palg = NULL;
596 const ASN1_OBJECT *palg_obj = NULL;
597 ASN1_INTEGER *priv_key = NULL;
598 int expected_key_len;
600 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
603 if (!decode_gost_algor_params(pk, palg)) {
607 expected_key_len = pkey_bits_gost(pk) > 0 ? pkey_bits_gost(pk) / 8 : 0;
608 if (expected_key_len == 0) {
609 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
613 if (priv_len % expected_key_len == 0) {
614 /* Key is not wrapped but masked */
615 pk_num = unmask_priv_key(pk, pkey_buf, expected_key_len,
616 priv_len / expected_key_len - 1);
617 } else if (V_ASN1_OCTET_STRING == *p) {
618 /* New format - Little endian octet string */
619 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
620 if (!s || ((s->length != 32) && (s->length != 64))) {
622 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
625 pk_num = BN_lebin2bn(s->data, s->length, BN_secure_new());
627 } else if (V_ASN1_INTEGER == *p) {
628 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
630 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
633 pk_num = ASN1_INTEGER_to_BN(priv_key, BN_secure_new());
634 ASN1_INTEGER_free(priv_key);
635 } else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
636 MASKED_GOST_KEY *mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
639 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
643 priv_len = mgk->masked_priv_key->length;
644 if (priv_len % expected_key_len) {
645 MASKED_GOST_KEY_free(mgk);
646 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
650 pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
652 priv_len / expected_key_len - 1);
653 MASKED_GOST_KEY_free(mgk);
655 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
659 if (pk_num == NULL) {
660 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
664 ret = gost_set_priv_key(pk, pk_num);
669 /* ----------------------------------------------------------------------*/
670 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
672 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
673 ASN1_STRING *params = NULL;
674 unsigned char *buf = NULL;
675 int key_len = pkey_bits_gost(pk), i = 0;
676 /* unmasked private key */
677 const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
679 key_len = (key_len < 0) ? 0 : key_len / 8;
680 if (key_len == 0 || !(buf = OPENSSL_secure_malloc(key_len))) {
684 if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
685 OPENSSL_secure_free(buf);
689 params = encode_gost_algor_params(pk);
691 OPENSSL_secure_free(buf);
695 /* Convert buf to Little-endian */
696 for (i = 0; i < key_len / 2; i++) {
697 unsigned char tmp = buf[i];
698 buf[i] = buf[key_len - 1 - i];
699 buf[key_len - 1 - i] = tmp;
702 if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
703 ASN1_STRING *octet = ASN1_STRING_new();
705 unsigned char *priv_buf = NULL;
706 if (!octet || !ASN1_OCTET_STRING_set(octet, buf, key_len)) {
707 ASN1_STRING_free(octet);
708 ASN1_STRING_free(params);
709 OPENSSL_secure_free(buf);
712 priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
713 ASN1_STRING_free(octet);
714 OPENSSL_secure_free(buf);
716 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
720 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
724 /* --------- printing keys --------------------------------*/
725 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
729 if (!BIO_indent(out, indent, 128))
731 BIO_printf(out, "Private key: ");
732 key = gost_get0_priv_key(pkey);
734 BIO_printf(out, "<undefined>");
737 BIO_printf(out, "\n");
742 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
746 const EC_POINT *pubkey;
747 const EC_GROUP *group;
748 EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
753 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
760 pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
761 group = (key) ? EC_KEY_get0_group(key) : NULL;
762 if (!pubkey || !group)
765 if (!EC_POINT_get_affine_coordinates(group, pubkey, X, Y, ctx)) {
766 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
769 if (!BIO_indent(out, indent, 128))
771 BIO_printf(out, "Public key:\n");
772 if (!BIO_indent(out, indent + 3, 128))
774 BIO_printf(out, "X:");
776 BIO_printf(out, "\n");
777 if (!BIO_indent(out, indent + 3, 128))
779 BIO_printf(out, "Y:");
781 BIO_printf(out, "\n");
790 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
792 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
793 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
799 param_nid = EC_GROUP_get_curve_name(group);
800 if (!BIO_indent(out, indent, 128))
802 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
807 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
808 ASN1_PCTX *pctx, int type)
811 if (print_gost_priv(out, pkey, indent) == 0)
815 if (print_gost_ec_pub(out, pkey, indent) == 0)
819 return print_gost_ec_param(out, pkey, indent);
822 static int param_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
825 return print_gost_ec(out, pkey, indent, pctx, 0);
828 static int pub_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
831 return print_gost_ec(out, pkey, indent, pctx, 1);
834 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
837 return print_gost_ec(out, pkey, indent, pctx, 2);
840 /* ---------------------------------------------------------------------*/
841 static int param_missing_gost_ec(const EVP_PKEY *pk)
843 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
846 if (!EC_KEY_get0_group(ec))
851 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
853 EC_KEY *eto = EVP_PKEY_get0(to);
854 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
855 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
856 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_INCOMPATIBLE_ALGORITHMS);
860 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
866 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
869 if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
870 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
875 if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
876 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
879 if (EC_KEY_get0_private_key(eto)) {
880 return gost_ec_compute_public(eto);
885 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
887 const EC_GROUP *group_a, *group_b;
888 EC_KEY *ec_a = EVP_PKEY_get0((EVP_PKEY *)a);
889 EC_KEY *ec_b = EVP_PKEY_get0((EVP_PKEY *)b);
893 group_a = EC_KEY_get0_group(ec_a);
894 group_b = EC_KEY_get0_group(ec_b);
895 if (!group_a || !group_b)
898 if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
904 /* ---------- Public key functions * --------------------------------------*/
905 static int pub_decode_gost_ec(EVP_PKEY *pk, const X509_PUBKEY *pub)
907 X509_ALGOR *palg = NULL;
908 const unsigned char *pubkey_buf = NULL;
909 unsigned char *databuf = NULL;
910 ASN1_OBJECT *palgobj = NULL;
912 EC_POINT *pub_key = NULL;
913 BIGNUM *X = NULL, *Y = NULL;
914 ASN1_OCTET_STRING *octet = NULL;
916 const EC_GROUP *group;
919 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
921 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
922 if (!decode_gost_algor_params(pk, palg))
924 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
925 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
927 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
930 databuf = OPENSSL_malloc(octet->length);
932 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
936 BUF_reverse(databuf, octet->data, octet->length);
937 len = octet->length / 2;
939 Y = BN_bin2bn(databuf, len, NULL);
940 X = BN_bin2bn(databuf + len, len, NULL);
942 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_BN_LIB);
945 pub_key = EC_POINT_new(group);
946 if (!EC_POINT_set_affine_coordinates(group, pub_key, X, Y, NULL)) {
947 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
951 retval = EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key);
953 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
956 EC_POINT_free(pub_key);
959 OPENSSL_free(databuf);
960 ASN1_OCTET_STRING_free(octet);
964 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
967 ASN1_OCTET_STRING *octet = NULL;
969 unsigned char *buf = NULL, *databuf = NULL;
970 int data_len, ret = -1;
971 const EC_POINT *pub_key;
972 BIGNUM *X = NULL, *Y = NULL, *order;
973 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
974 int ptype = V_ASN1_SEQUENCE;
977 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
979 params = encode_gost_algor_params(pk);
983 if (order == NULL || EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL) == 0) {
984 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
987 if (EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL) == 0) {
988 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
991 pub_key = EC_KEY_get0_public_key(ec);
993 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
999 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1002 if (!EC_POINT_get_affine_coordinates(EC_KEY_get0_group(ec),
1003 pub_key, X, Y, NULL)) {
1004 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
1007 data_len = 2 * BN_num_bytes(order);
1008 databuf = OPENSSL_zalloc(data_len);
1009 if (databuf == NULL) {
1010 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1014 store_bignum(X, databuf + data_len / 2, data_len / 2);
1015 store_bignum(Y, databuf, data_len / 2);
1017 BUF_reverse(databuf, NULL, data_len);
1019 octet = ASN1_OCTET_STRING_new();
1020 if (octet == NULL) {
1021 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1025 if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
1026 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1030 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
1032 ASN1_BIT_STRING_free(octet);
1040 OPENSSL_free(databuf);
1044 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
1047 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
1049 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
1050 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
1051 const EC_POINT *ka, *kb;
1054 ka = EC_KEY_get0_public_key(ea);
1055 kb = EC_KEY_get0_public_key(eb);
1058 return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
1061 static int pkey_size_gost(const EVP_PKEY *pk)
1066 switch (EVP_PKEY_base_id(pk)) {
1067 case NID_id_GostR3410_94:
1068 case NID_id_GostR3410_2001:
1069 case NID_id_GostR3410_2001DH:
1070 case NID_id_GostR3410_2012_256:
1072 case NID_id_GostR3410_2012_512:
1079 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
1080 static void mackey_free_gost(EVP_PKEY *pk)
1082 OPENSSL_free(EVP_PKEY_get0(pk));
1085 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1088 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1090 *(int *)arg2 = NID_id_Gost28147_89_MAC;
1097 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1100 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1102 *(int *)arg2 = NID_gost_mac_12;
1109 static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1112 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1114 *(int *)arg2 = NID_magma_mac;
1121 static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1124 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1126 *(int *)arg2 = NID_grasshopper_mac;
1133 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
1136 EC_GROUP_get_curve_name(EC_KEY_get0_group
1137 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
1138 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
1141 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
1144 ASN1_OBJECT *obj = NULL;
1146 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
1149 nid = OBJ_obj2nid(obj);
1150 ASN1_OBJECT_free(obj);
1152 return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
1155 /* ----------------------------------------------------------------------*/
1156 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
1157 const char *pemstr, const char *info)
1159 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
1163 case NID_id_GostR3410_2001:
1164 case NID_id_GostR3410_2001DH:
1165 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1166 EVP_PKEY_asn1_set_private(*ameth,
1167 priv_decode_gost, priv_encode_gost,
1168 priv_print_gost_ec);
1170 EVP_PKEY_asn1_set_param(*ameth,
1171 gost2001_param_decode, gost2001_param_encode,
1172 param_missing_gost_ec, param_copy_gost_ec,
1173 param_cmp_gost_ec, param_print_gost_ec);
1174 EVP_PKEY_asn1_set_public(*ameth,
1175 pub_decode_gost_ec, pub_encode_gost_ec,
1176 pub_cmp_gost_ec, pub_print_gost_ec,
1177 pkey_size_gost, pkey_bits_gost);
1179 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1180 EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1182 case NID_id_GostR3410_2012_256:
1183 case NID_id_GostR3410_2012_512:
1184 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1185 EVP_PKEY_asn1_set_private(*ameth,
1186 priv_decode_gost, priv_encode_gost,
1187 priv_print_gost_ec);
1189 EVP_PKEY_asn1_set_param(*ameth,
1191 param_missing_gost_ec, param_copy_gost_ec,
1192 param_cmp_gost_ec, NULL);
1194 EVP_PKEY_asn1_set_public(*ameth,
1195 pub_decode_gost_ec, pub_encode_gost_ec,
1196 pub_cmp_gost_ec, pub_print_gost_ec,
1197 pkey_size_gost, pkey_bits_gost);
1199 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1200 EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1202 case NID_id_Gost28147_89_MAC:
1203 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1204 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
1206 case NID_gost_mac_12:
1207 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1208 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
1211 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1212 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma);
1214 case NID_grasshopper_mac:
1215 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1216 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper);