Sync usage with man page.
[netbsd-mini2440.git] / crypto / external / bsd / openssl / dist / engines / ccgost / gost_ameth.c
blob16a99ac2b2cf6ae16510dd8e80b1b672e8988f06
1 /**********************************************************************
2 * gost_ameth.c *
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
5 * *
6 * Implementation of RFC 4490/4491 ASN1 method *
7 * for OpenSSL *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
10 #include <string.h>
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/asn1.h>
16 #include "gost_params.h"
17 #include "gost_lcl.h"
18 #include "e_gost_err.h"
20 int gost94_nid_by_params(DSA *p)
22 R3410_params *gost_params;
23 BIGNUM *q=BN_new();
24 for (gost_params = R3410_paramset;gost_params->q!=NULL; gost_params++)
26 BN_dec2bn(&q,gost_params->q);
27 if (!BN_cmp(q,p->q))
29 BN_free(q);
30 return gost_params->nid;
33 BN_free(q);
34 return NID_undef;
37 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
39 ASN1_STRING *params = ASN1_STRING_new();
40 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
41 int pkey_param_nid = NID_undef;
42 int cipher_param_nid = NID_undef;
43 if (!params || !gkp)
45 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
46 ERR_R_MALLOC_FAILURE);
47 ASN1_STRING_free(params);
48 params = NULL;
49 goto err;
51 switch (EVP_PKEY_base_id(key))
53 case NID_id_GostR3410_2001:
54 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)key)));
55 cipher_param_nid = get_encryption_params(NULL)->nid;
56 break;
57 case NID_id_GostR3410_94:
58 pkey_param_nid = (int) gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)key));
59 if (pkey_param_nid == NID_undef)
61 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
62 GOST_R_INVALID_GOST94_PARMSET);
63 ASN1_STRING_free(params);
64 params=NULL;
65 goto err;
67 cipher_param_nid = get_encryption_params(NULL)->nid;
68 break;
70 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
71 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
72 /*gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);*/
73 params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
74 if (params->length <=0 )
76 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
77 ERR_R_MALLOC_FAILURE);
78 ASN1_STRING_free(params);
79 params = NULL;
80 goto err;
82 params ->type = V_ASN1_SEQUENCE;
83 err:
84 GOST_KEY_PARAMS_free(gkp);
85 return params;
88 /* Parses GOST algorithm parameters from X509_ALGOR and
89 * modifies pkey setting NID and parameters
91 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
93 ASN1_OBJECT *palg_obj =NULL;
94 int ptype = V_ASN1_UNDEF;
95 int pkey_nid = NID_undef,param_nid = NID_undef;
96 void *_pval;
97 ASN1_STRING *pval = NULL;
98 const unsigned char *p;
99 GOST_KEY_PARAMS *gkp = NULL;
101 X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
102 pval = _pval;
103 if (ptype != V_ASN1_SEQUENCE)
105 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
106 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
107 return 0;
109 p=pval->data;
110 pkey_nid = OBJ_obj2nid(palg_obj);
112 gkp = d2i_GOST_KEY_PARAMS(NULL,&p,pval->length);
113 if (!gkp)
115 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
116 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
117 return 0;
119 param_nid = OBJ_obj2nid(gkp->key_params);
120 GOST_KEY_PARAMS_free(gkp);
121 EVP_PKEY_set_type(pkey,pkey_nid);
122 switch (pkey_nid)
124 case NID_id_GostR3410_94:
126 DSA *dsa= EVP_PKEY_get0(pkey);
127 if (!dsa)
129 dsa = DSA_new();
130 if (!EVP_PKEY_assign(pkey,pkey_nid,dsa)) return 0;
132 if (!fill_GOST94_params(dsa,param_nid)) return 0;
133 break;
135 case NID_id_GostR3410_2001:
137 EC_KEY *ec = EVP_PKEY_get0(pkey);
138 if (!ec)
140 ec = EC_KEY_new();
141 if (!EVP_PKEY_assign(pkey,pkey_nid,ec)) return 0;
143 if (!fill_GOST2001_params(ec,param_nid)) return 0;
147 return 1;
150 static int gost_set_priv_key(EVP_PKEY *pkey,BIGNUM *priv)
152 switch (EVP_PKEY_base_id(pkey))
154 case NID_id_GostR3410_94:
156 DSA *dsa = EVP_PKEY_get0(pkey);
157 if (!dsa)
159 dsa = DSA_new();
160 EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),dsa);
162 dsa->priv_key = BN_dup(priv);
163 if (!EVP_PKEY_missing_parameters(pkey))
164 gost94_compute_public(dsa);
165 break;
167 case NID_id_GostR3410_2001:
169 EC_KEY *ec = EVP_PKEY_get0(pkey);
170 if (!ec)
172 ec = EC_KEY_new();
173 EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),ec);
175 if (!EC_KEY_set_private_key(ec,priv)) return 0;
176 if (!EVP_PKEY_missing_parameters(pkey))
177 gost2001_compute_public(ec);
178 break;
181 return 1;
183 BIGNUM* gost_get0_priv_key(const EVP_PKEY *pkey)
185 switch (EVP_PKEY_base_id(pkey))
187 case NID_id_GostR3410_94:
189 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
190 if (!dsa)
192 return NULL;
194 if (!dsa->priv_key) return NULL;
195 return dsa->priv_key;
196 break;
198 case NID_id_GostR3410_2001:
200 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
201 const BIGNUM* priv;
202 if (!ec)
204 return NULL;
206 if (!(priv=EC_KEY_get0_private_key(ec))) return NULL;
207 return (BIGNUM *)priv;
208 break;
211 return NULL;
214 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op,
215 long arg1, void *arg2)
217 switch (op)
219 case ASN1_PKEY_CTRL_PKCS7_SIGN:
220 if (arg1 == 0)
222 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
223 int nid = EVP_PKEY_base_id(pkey);
224 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO*)arg2,
225 NULL, &alg1, &alg2);
226 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
227 V_ASN1_NULL, 0);
228 if (nid == NID_undef)
230 return (-1);
232 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
234 return 1;
235 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
236 if (arg1 == 0)
238 X509_ALGOR *alg;
239 ASN1_STRING * params = encode_gost_algor_params(pkey);
240 if (!params)
242 return -1;
244 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO*)arg2, &alg);
245 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
246 V_ASN1_SEQUENCE, params);
248 return 1;
249 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
250 *(int *)arg2 = NID_id_GostR3411_94;
251 return 2;
254 return -2;
256 /*----------------------- free functions * ------------------------------*/
257 static void pkey_free_gost94(EVP_PKEY *key)
259 if (key->pkey.dsa)
261 DSA_free(key->pkey.dsa);
265 static void pkey_free_gost01(EVP_PKEY *key)
267 if (key->pkey.ec)
269 EC_KEY_free(key->pkey.ec);
273 /* ------------------ private key functions -----------------------------*/
274 static int priv_decode_gost( EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
276 const unsigned char *pkey_buf = NULL,*p=NULL;
277 int priv_len = 0;
278 BIGNUM *pk_num=NULL;
279 int ret =0;
280 X509_ALGOR *palg =NULL;
281 ASN1_OBJECT *palg_obj = NULL;
282 ASN1_INTEGER *priv_key=NULL;
284 if (!PKCS8_pkey_get0(&palg_obj,&pkey_buf,&priv_len,&palg,p8inf))
285 return 0;
286 p = pkey_buf;
287 if (!decode_gost_algor_params(pk,palg))
289 return 0;
291 if (V_ASN1_OCTET_STRING == *p)
293 /* New format - Little endian octet string */
294 unsigned char rev_buf[32];
295 int i;
296 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL,&p,priv_len);
297 if (!s||s->length !=32)
299 GOSTerr(GOST_F_PRIV_DECODE_GOST,
300 EVP_R_DECODE_ERROR);
301 return 0;
303 for (i=0;i<32;i++)
305 rev_buf[31-i]=s->data[i];
307 ASN1_STRING_free(s);
308 pk_num = getbnfrombuf(rev_buf,32);
310 else
312 priv_key=d2i_ASN1_INTEGER(NULL,&p,priv_len);
313 if (!priv_key) return 0;
314 ret= ((pk_num = ASN1_INTEGER_to_BN(priv_key, NULL))!=NULL) ;
315 ASN1_INTEGER_free(priv_key);
316 if (!ret)
318 GOSTerr(GOST_F_PRIV_DECODE_GOST,
319 EVP_R_DECODE_ERROR);
320 return 0;
324 ret= gost_set_priv_key(pk,pk_num);
325 BN_free(pk_num);
326 return ret;
329 /* ----------------------------------------------------------------------*/
330 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
332 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
333 ASN1_STRING *params = encode_gost_algor_params(pk);
334 unsigned char *priv_buf = NULL;
335 int priv_len;
337 ASN1_INTEGER *asn1key=NULL;
338 if (!params)
340 return 0;
342 asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk),NULL);
343 priv_len = i2d_ASN1_INTEGER(asn1key,&priv_buf);
344 ASN1_INTEGER_free(asn1key);
345 return PKCS8_pkey_set0(p8,algobj,0,V_ASN1_SEQUENCE,params,
346 priv_buf,priv_len);
348 /* --------- printing keys --------------------------------*/
349 static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent,
350 ASN1_PCTX *pctx, int type)
352 int param_nid = NID_undef;
354 if (type == 2)
356 BIGNUM *key;
358 if (!BIO_indent(out,indent,128)) return 0;
359 BIO_printf(out,"Private key: ");
360 key = gost_get0_priv_key(pkey);
361 if (!key)
362 BIO_printf(out,"<undefined>");
363 else
364 BN_print(out,key);
365 BIO_printf(out,"\n");
367 if (type >= 1)
369 BIGNUM *pubkey;
371 pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
372 BIO_indent(out,indent,128);
373 BIO_printf(out,"Public key: ");
374 BN_print(out,pubkey);
375 BIO_printf(out,"\n");
378 param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
379 BIO_indent(out,indent,128);
380 BIO_printf(out, "Parameter set: %s\n",OBJ_nid2ln(param_nid));
381 return 1;
384 static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
385 ASN1_PCTX *pctx)
387 return print_gost_94(out, pkey, indent, pctx,0);
390 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
391 ASN1_PCTX *pctx)
393 return print_gost_94(out,pkey, indent, pctx,1);
395 static int priv_print_gost94(BIO *out,const EVP_PKEY *pkey, int indent,
396 ASN1_PCTX *pctx)
398 return print_gost_94(out,pkey,indent,pctx,2);
401 static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
402 ASN1_PCTX *pctx, int type)
404 int param_nid = NID_undef;
405 if (type == 2)
407 BIGNUM *key;
409 if (!BIO_indent(out,indent,128)) return 0;
410 BIO_printf(out,"Private key: ");
411 key = gost_get0_priv_key(pkey);
412 if (!key)
413 BIO_printf(out,"<undefined)");
414 else
415 BN_print(out,key);
416 BIO_printf(out,"\n");
418 if (type >= 1)
420 BN_CTX *ctx = BN_CTX_new();
421 BIGNUM *X,*Y;
422 const EC_POINT *pubkey;
423 const EC_GROUP *group;
425 if (!ctx)
427 GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_MALLOC_FAILURE);
428 return 0;
430 BN_CTX_start(ctx);
431 X = BN_CTX_get(ctx);
432 Y = BN_CTX_get(ctx);
433 pubkey = EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
434 group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
435 if (!EC_POINT_get_affine_coordinates_GFp(group,pubkey,X,Y,ctx))
437 GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_EC_LIB);
438 BN_CTX_free(ctx);
439 return 0;
441 if (!BIO_indent(out,indent,128)) return 0;
442 BIO_printf(out,"Public key:\n");
443 if (!BIO_indent(out,indent+3,128)) return 0;
444 BIO_printf(out,"X:");
445 BN_print(out,X);
446 BIO_printf(out,"\n");
447 BIO_indent(out,indent+3,128);
448 BIO_printf(out,"Y:");
449 BN_print(out,Y);
450 BIO_printf(out,"\n");
451 BN_CTX_end(ctx);
452 BN_CTX_free(ctx);
455 param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
456 if (!BIO_indent(out,indent,128)) return 0;
457 BIO_printf(out,"Parameter set: %s\n",OBJ_nid2ln(param_nid));
458 return 1;
460 static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
461 ASN1_PCTX *pctx)
463 return print_gost_01(out,pkey,indent,pctx,0);
465 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
466 ASN1_PCTX *pctx)
468 return print_gost_01(out,pkey, indent, pctx,1);
470 static int priv_print_gost01(BIO *out,const EVP_PKEY *pkey, int indent,
471 ASN1_PCTX *pctx)
473 return print_gost_01(out,pkey,indent,pctx,2);
475 /* ---------------------------------------------------------------------*/
476 static int param_missing_gost94(const EVP_PKEY *pk)
478 const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
479 if (!dsa) return 1;
480 if (!dsa->q) return 1;
481 return 0;
484 static int param_missing_gost01(const EVP_PKEY *pk)
486 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
487 if (!ec) return 1;
488 if (!EC_KEY_get0_group(ec)) return 1;
489 return 0;
492 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from)
494 const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
495 DSA *dto = EVP_PKEY_get0(to);
496 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to))
498 GOSTerr(GOST_F_PARAM_COPY_GOST94,
499 GOST_R_INCOMPATIBLE_ALGORITHMS);
500 return 0;
502 if (!dfrom)
504 GOSTerr(GOST_F_PARAM_COPY_GOST94,
505 GOST_R_KEY_PARAMETERS_MISSING);
506 return 0;
508 if (!dto)
510 dto = DSA_new();
511 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),dto);
513 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);
514 COPYBIGNUM(dto,dfrom,p)
515 COPYBIGNUM(dto,dfrom,q)
516 COPYBIGNUM(dto,dfrom,g)
518 if (dto->priv_key)
519 gost94_compute_public(dto);
520 return 1;
522 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
524 EC_KEY *eto = EVP_PKEY_get0(to);
525 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
526 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to))
528 GOSTerr(GOST_F_PARAM_COPY_GOST01,
529 GOST_R_INCOMPATIBLE_ALGORITHMS);
530 return 0;
532 if (!efrom)
534 GOSTerr(GOST_F_PARAM_COPY_GOST01,
535 GOST_R_KEY_PARAMETERS_MISSING);
536 return 0;
538 if (!eto)
540 eto = EC_KEY_new();
541 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),eto);
543 EC_KEY_set_group(eto,EC_KEY_get0_group(efrom));
544 if (EC_KEY_get0_private_key(eto))
546 gost2001_compute_public(eto);
548 return 1;
551 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
553 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
554 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
555 if (!BN_cmp(da->q,db->q)) return 1;
556 return 0;
559 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
561 if (EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a)))==
562 EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)b))))
564 return 1;
566 return 0;
570 /* ---------- Public key functions * --------------------------------------*/
571 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
573 X509_ALGOR *palg = NULL;
574 const unsigned char *pubkey_buf = NULL;
575 unsigned char *databuf;
576 ASN1_OBJECT *palgobj = NULL;
577 int pub_len,i,j;
578 DSA *dsa;
579 ASN1_OCTET_STRING *octet= NULL;
581 if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
582 &palg, pub)) return 0;
583 EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);
584 if (!decode_gost_algor_params(pk,palg)) return 0;
585 octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
586 if (!octet)
588 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
589 return 0;
591 databuf = OPENSSL_malloc(octet->length);
592 for (i=0,j=octet->length-1;i<octet->length;i++,j--)
594 databuf[j]=octet->data[i];
596 dsa = EVP_PKEY_get0(pk);
597 dsa->pub_key=BN_bin2bn(databuf,octet->length,NULL);
598 ASN1_OCTET_STRING_free(octet);
599 OPENSSL_free(databuf);
600 return 1;
604 static int pub_encode_gost94(X509_PUBKEY *pub,const EVP_PKEY *pk)
606 ASN1_OBJECT *algobj = NULL;
607 ASN1_OCTET_STRING *octet = NULL;
608 void *pval = NULL;
609 unsigned char *buf=NULL,*databuf,*sptr;
610 int i,j,data_len,ret=0;
612 int ptype = V_ASN1_UNDEF;
613 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
614 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
615 if (pk->save_parameters)
617 ASN1_STRING *params = encode_gost_algor_params(pk);
618 pval = params;
619 ptype = V_ASN1_SEQUENCE;
621 data_len = BN_num_bytes(dsa->pub_key);
622 databuf = OPENSSL_malloc(data_len);
623 BN_bn2bin(dsa->pub_key,databuf);
624 octet = ASN1_OCTET_STRING_new();
625 ASN1_STRING_set(octet,NULL,data_len);
626 sptr = ASN1_STRING_data(octet);
627 for (i=0,j=data_len-1; i< data_len;i++,j--)
629 sptr[i]=databuf[j];
631 OPENSSL_free(databuf);
632 ret = i2d_ASN1_OCTET_STRING(octet,&buf);
633 ASN1_BIT_STRING_free(octet);
634 if (ret <0) return 0;
635 return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
638 static int pub_decode_gost01(EVP_PKEY *pk,X509_PUBKEY *pub)
640 X509_ALGOR *palg = NULL;
641 const unsigned char *pubkey_buf = NULL;
642 unsigned char *databuf;
643 ASN1_OBJECT *palgobj = NULL;
644 int pub_len,i,j;
645 EC_POINT *pub_key;
646 BIGNUM *X,*Y;
647 ASN1_OCTET_STRING *octet= NULL;
648 int len;
649 const EC_GROUP *group;
651 if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
652 &palg, pub)) return 0;
653 EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);
654 if (!decode_gost_algor_params(pk,palg)) return 0;
655 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
656 octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
657 if (!octet)
659 GOSTerr(GOST_F_PUB_DECODE_GOST01,ERR_R_MALLOC_FAILURE);
660 return 0;
662 databuf = OPENSSL_malloc(octet->length);
663 for (i=0,j=octet->length-1;i<octet->length;i++,j--)
665 databuf[j]=octet->data[i];
667 len=octet->length/2;
668 ASN1_OCTET_STRING_free(octet);
670 Y= getbnfrombuf(databuf,len);
671 X= getbnfrombuf(databuf+len,len);
672 OPENSSL_free(databuf);
673 pub_key = EC_POINT_new(group);
674 if (!EC_POINT_set_affine_coordinates_GFp(group
675 ,pub_key,X,Y,NULL))
677 GOSTerr(GOST_F_PUB_DECODE_GOST01,
678 ERR_R_EC_LIB);
679 EC_POINT_free(pub_key);
680 BN_free(X);
681 BN_free(Y);
682 return 0;
684 BN_free(X);
685 BN_free(Y);
686 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk),pub_key))
688 GOSTerr(GOST_F_PUB_DECODE_GOST01,
689 ERR_R_EC_LIB);
690 EC_POINT_free(pub_key);
691 return 0;
693 EC_POINT_free(pub_key);
694 return 1;
698 static int pub_encode_gost01(X509_PUBKEY *pub,const EVP_PKEY *pk)
700 ASN1_OBJECT *algobj = NULL;
701 ASN1_OCTET_STRING *octet = NULL;
702 void *pval = NULL;
703 unsigned char *buf=NULL,*databuf,*sptr;
704 int i,j,data_len,ret=0;
705 const EC_POINT *pub_key;
706 BIGNUM *X,*Y,*order;
707 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
708 int ptype = V_ASN1_UNDEF;
710 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
711 if (pk->save_parameters)
713 ASN1_STRING *params = encode_gost_algor_params(pk);
714 pval = params;
715 ptype = V_ASN1_SEQUENCE;
717 order = BN_new();
718 EC_GROUP_get_order(EC_KEY_get0_group(ec),order,NULL);
719 pub_key=EC_KEY_get0_public_key(ec);
720 if (!pub_key)
722 GOSTerr(GOST_F_PUB_ENCODE_GOST01,
723 GOST_R_PUBLIC_KEY_UNDEFINED);
724 return 0;
726 X=BN_new();
727 Y=BN_new();
728 EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
729 pub_key,X,Y,NULL);
730 data_len = 2*BN_num_bytes(order);
731 BN_free(order);
732 databuf = OPENSSL_malloc(data_len);
733 memset(databuf,0,data_len);
735 store_bignum(X,databuf+data_len/2,data_len/2);
736 store_bignum(Y,databuf,data_len/2);
738 BN_free(X);
739 BN_free(Y);
740 octet = ASN1_OCTET_STRING_new();
741 ASN1_STRING_set(octet,NULL,data_len);
742 sptr=ASN1_STRING_data(octet);
743 for (i=0,j=data_len-1;i<data_len;i++,j--)
745 sptr[i]=databuf[j];
747 OPENSSL_free(databuf);
748 ret = i2d_ASN1_OCTET_STRING(octet,&buf);
749 ASN1_BIT_STRING_free(octet);
750 if (ret <0) return 0;
751 return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
754 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
756 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
757 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
758 if (da && db && da->pub_key && db->pub_key
759 && !BN_cmp(da->pub_key,db->pub_key))
761 return 1;
763 return 0;
766 static int pub_cmp_gost01(const EVP_PKEY *a,const EVP_PKEY *b)
768 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
769 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
770 const EC_POINT *ka,*kb;
771 int ret=0;
772 if (!ea || !eb) return 0;
773 ka = EC_KEY_get0_public_key(ea);
774 kb = EC_KEY_get0_public_key(eb);
775 if (!ka || !kb) return 0;
776 ret = (0==EC_POINT_cmp(EC_KEY_get0_group(ea),ka,kb,NULL)) ;
777 return ret;
783 static int pkey_size_gost(const EVP_PKEY *pk)
785 return 64;
788 static int pkey_bits_gost(const EVP_PKEY *pk)
790 return 256;
792 /*------------------------ ASN1 METHOD for GOST MAC -------------------*/
793 static void mackey_free_gost(EVP_PKEY *pk)
795 if (pk->pkey.ptr) {
796 OPENSSL_free(pk->pkey.ptr);
799 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
801 switch (op)
803 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
804 *(int *)arg2 = NID_undef;
805 return 2;
807 return -2;
810 static int gost94_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
812 int nid=gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
813 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid),pder);
815 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
817 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
818 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid),pder);
821 static int gost94_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen)
823 ASN1_OBJECT *obj=NULL;
824 DSA *dsa = EVP_PKEY_get0(pkey);
825 int nid;
826 if (d2i_ASN1_OBJECT(&obj,pder,derlen)==NULL) {
827 return 0;
829 nid = OBJ_obj2nid(obj);
830 ASN1_OBJECT_free(obj);
831 if (!dsa)
833 dsa=DSA_new();
834 if (!EVP_PKEY_assign(pkey,NID_id_GostR3410_94,dsa)) return 0;
836 if (!fill_GOST94_params(dsa,nid)) return 0;
837 return 1;
840 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) {
841 ASN1_OBJECT *obj=NULL;
842 int nid;
843 EC_KEY *ec = EVP_PKEY_get0(pkey);
844 if (d2i_ASN1_OBJECT(&obj,pder,derlen)==NULL) {
845 return 0;
847 nid = OBJ_obj2nid(obj);
848 ASN1_OBJECT_free(obj);
849 if (!ec)
851 ec = EC_KEY_new();
852 if (!EVP_PKEY_assign(pkey,NID_id_GostR3410_2001,ec)) return 0;
854 if (!fill_GOST2001_params(ec, nid)) return 0;
855 return 1;
862 /* ----------------------------------------------------------------------*/
863 int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info)
865 *ameth = EVP_PKEY_asn1_new(nid,
866 ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
867 if (!*ameth) return 0;
868 switch (nid)
870 case NID_id_GostR3410_94:
871 EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost94);
872 EVP_PKEY_asn1_set_private (*ameth,
873 priv_decode_gost, priv_encode_gost,
874 priv_print_gost94);
876 EVP_PKEY_asn1_set_param (*ameth,
877 gost94_param_decode, gost94_param_encode,
878 param_missing_gost94, param_copy_gost94,
879 param_cmp_gost94,param_print_gost94 );
880 EVP_PKEY_asn1_set_public (*ameth,
881 pub_decode_gost94, pub_encode_gost94,
882 pub_cmp_gost94, pub_print_gost94,
883 pkey_size_gost, pkey_bits_gost);
885 EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
886 break;
887 case NID_id_GostR3410_2001:
888 EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost01);
889 EVP_PKEY_asn1_set_private (*ameth,
890 priv_decode_gost, priv_encode_gost,
891 priv_print_gost01);
893 EVP_PKEY_asn1_set_param (*ameth,
894 gost2001_param_decode, gost2001_param_encode,
895 param_missing_gost01, param_copy_gost01,
896 param_cmp_gost01, param_print_gost01);
897 EVP_PKEY_asn1_set_public (*ameth,
898 pub_decode_gost01, pub_encode_gost01,
899 pub_cmp_gost01, pub_print_gost01,
900 pkey_size_gost, pkey_bits_gost);
902 EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
903 break;
904 case NID_id_Gost28147_89_MAC:
905 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
906 EVP_PKEY_asn1_set_ctrl(*ameth,mac_ctrl_gost);
907 break;
909 return 1;