GOST public key algorithm ENGINE donated to the OpenSSL by Cryptocom.
[openssl.git] / engines / ccgost / gost2001.c
1 /**********************************************************************
2  *                          gost2001.c                                *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *          Implementation of GOST R 34.10-2001                                   *
7  *          Requires OpenSSL 0.9.9 for compilation                    *
8  **********************************************************************/
9 #include "tools.h"
10 #include "sign.h"
11 #include "paramset.h"
12 #include <string.h>
13 #include <openssl/rand.h>
14 #include <openssl/ecdsa.h>
15 #include <openssl/err.h>
16 #include "e_gost_err.h"
17 #ifdef DEBUG_SIGN
18 extern 
19 void dump_signature(const char *message,const unsigned char *buffer,size_t len);
20 void dump_dsa_sig(const char *message, DSA_SIG *sig);
21 #else
22
23 #define dump_signature(a,b,c)
24 #define dump_dsa_sig(a,b)
25 #endif
26
27 /*
28  * Fills EC_KEY structure hidden in the app_data field of DSA structure
29  * with parameter information, extracted from parameter array in
30  * params.c file.
31  *
32  * Also fils DSA->q field with copy of EC_GROUP order field to make
33  * DSA_size function work
34  */ 
35 int fill_GOST2001_params(EC_KEY *eckey, int nid) {
36         R3410_2001_params *params = R3410_2001_paramset;
37         EC_GROUP *grp;
38         BIGNUM *p=NULL,*q=NULL,*a=NULL,*b=NULL,*x=NULL,*y=NULL;
39         EC_POINT *P=NULL;
40         BN_CTX *ctx=BN_CTX_new();
41         int ok=0;
42         
43         BN_CTX_start(ctx);
44         p=BN_CTX_get(ctx);
45         a=BN_CTX_get(ctx);
46         b=BN_CTX_get(ctx);
47         x=BN_CTX_get(ctx);
48         y=BN_CTX_get(ctx);
49         q=BN_CTX_get(ctx);
50         while (params->nid!=NID_undef && params->nid != nid) params++;
51         if (params->nid == NID_undef) {
52                 GOSTerr(GOST_F_FILL_GOST2001_PARAMS,GOST_R_UNSUPPORTED_PARAMETER_SET);
53                 goto err;
54         }       
55         BN_hex2bn(&p,params->p);
56         BN_hex2bn(&a,params->a);
57         BN_hex2bn(&b,params->b);
58         
59         grp = EC_GROUP_new_curve_GFp(p,a,b,ctx);
60
61         P = EC_POINT_new(grp);
62
63         BN_hex2bn(&x,params->x);
64         BN_hex2bn(&y,params->y);
65         EC_POINT_set_affine_coordinates_GFp(grp,P,x,y,ctx);
66         BN_hex2bn(&q,params->q);
67 #ifdef DEBUG_KEYS
68         fprintf(stderr,"Set params index %d oid %s\nq=",
69                         (params-R3410_2001_paramset),OBJ_nid2sn(params->nid));
70         BN_print_fp(stderr,q);
71         fprintf(stderr,"\n");
72 #endif  
73
74         EC_GROUP_set_generator(grp,P,q,NULL);
75         EC_GROUP_set_curve_name(grp,params->nid);
76
77         EC_KEY_set_group(eckey,grp);
78         ok=1;
79 err:
80         EC_POINT_free(P);
81         EC_GROUP_free(grp);
82         BN_CTX_end(ctx);
83         BN_CTX_free(ctx);
84         return ok;
85                         
86 }       
87
88
89 /*
90  * Computes gost2001 signature as DSA_SIG structure 
91  *
92  *
93  */ 
94 DSA_SIG *gost2001_do_sign(const unsigned char *dgst,int dlen, EC_KEY *eckey) {
95                 DSA_SIG *newsig = NULL;
96                 BIGNUM *md = hashsum2bn(dgst);
97                 BIGNUM *order = NULL;
98                 const EC_GROUP *group;
99                 const BIGNUM *priv_key;
100                 BIGNUM *r=NULL,*s=NULL,*X=NULL,*tmp=NULL,*tmp2=NULL, *k=NULL,*e=NULL;
101                 EC_POINT *C=NULL;
102                 BN_CTX *ctx = BN_CTX_new();     
103                 BN_CTX_start(ctx);
104                 OPENSSL_assert(dlen==32);
105                 newsig=DSA_SIG_new();
106                 if (!newsig) 
107                 {
108                         GOSTerr(GOST_F_GOST2001_DO_SIGN,GOST_R_NO_MEMORY);
109                         goto err;
110                 }       
111                 group = EC_KEY_get0_group(eckey);
112                 order=BN_CTX_get(ctx);
113                 EC_GROUP_get_order(group,order,ctx);
114                 priv_key = EC_KEY_get0_private_key(eckey);
115                 e = BN_CTX_get(ctx);
116                 BN_mod(e,md,order,ctx);
117 #ifdef DEBUG_SIGN
118                 fprintf(stderr,"digest as bignum=");
119                 BN_print_fp(stderr,md);
120                 fprintf(stderr,"\ndigest mod q=");
121                 BN_print_fp(stderr,e);
122                 fprintf(stderr,"\n");
123 #endif          
124                 if (BN_is_zero(e))
125                 {
126                    BN_one(e);
127                 }   
128                 k =BN_CTX_get(ctx);
129                 C=EC_POINT_new(group);
130                 do {
131                         do {
132                                 if (!BN_rand_range(k,order)) 
133                                 {
134                                         GOSTerr(GOST_F_GOST2001_DO_SIGN,GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
135                                         DSA_SIG_free(newsig);
136                                         goto err;
137                                 }       
138                                 if (!EC_POINT_mul(group,C,k,NULL,NULL,ctx)) {
139                                         GOSTerr(GOST_F_GOST2001_DO_SIGN,ERR_R_EC_LIB);
140                                         DSA_SIG_free(newsig);
141                                         goto err;
142                                 }       
143                                 if (!X) X=BN_CTX_get(ctx);
144                                 if (!EC_POINT_get_affine_coordinates_GFp(group,C,X,NULL,ctx)) {
145                                         GOSTerr(GOST_F_GOST2001_DO_SIGN,ERR_R_EC_LIB);
146                                         DSA_SIG_free(newsig);
147                                         goto err;
148                                 }       
149                                 if (!r) r=BN_CTX_get(ctx);
150                                 BN_nnmod(r,X,order,ctx);
151                         } while (BN_is_zero(r));
152                         /* s =  (r*priv_key+k*e) mod order */
153                         if (!tmp) tmp = BN_CTX_get(ctx);
154                         BN_mod_mul(tmp,priv_key,r,order,ctx);
155                         if (!tmp2) tmp2 = BN_CTX_get(ctx);
156                         BN_mod_mul(tmp2,k,e,order,ctx);
157                         if (!s) s=BN_CTX_get(ctx);
158                         BN_mod_add(s,tmp,tmp2,order,ctx);
159                 } while (BN_is_zero(s));        
160
161                 newsig->s=BN_dup(s);
162                 newsig->r=BN_dup(r);
163 err:                    
164                 BN_CTX_end(ctx);
165                 BN_CTX_free(ctx);
166                 EC_POINT_free(C);
167                 BN_free(md);
168                 return newsig;
169 }
170 /*
171  * Verifies gost 2001 signature
172  *
173  */ 
174 int gost2001_do_verify(const unsigned char *dgst,int dgst_len,
175                         DSA_SIG *sig, EC_KEY *ec) {
176         BN_CTX *ctx=BN_CTX_new();
177         const EC_GROUP *group = EC_KEY_get0_group(ec);
178         BIGNUM *order;
179         BIGNUM *md = NULL,*e=NULL,*R=NULL,*v=NULL,*z1=NULL,*z2=NULL;
180         BIGNUM *X=NULL,*tmp=NULL;
181         EC_POINT *C = NULL;
182         const EC_POINT *pub_key=NULL;
183         int ok=0;
184
185         BN_CTX_start(ctx);
186         order = BN_CTX_get(ctx);
187         e = BN_CTX_get(ctx);
188         z1 = BN_CTX_get(ctx);
189         z2 = BN_CTX_get(ctx);
190         tmp = BN_CTX_get(ctx);
191         X= BN_CTX_get(ctx);     
192         R=BN_CTX_get(ctx);
193         v=BN_CTX_get(ctx);
194         
195         EC_GROUP_get_order(group,order,ctx);
196         pub_key = EC_KEY_get0_public_key(ec);
197         if (BN_is_zero(sig->s) || BN_is_zero(sig->r) ||
198                 (BN_cmp(sig->s,order)>=1) || (BN_cmp(sig->r,order)>=1)) 
199         {
200           GOSTerr(GOST_F_GOST_DO_VERIFY,GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
201           goto err;
202
203         }
204         md = hashsum2bn(dgst);
205
206         BN_mod(e,md,order,ctx);
207 #ifdef DEBUG_SIGN
208         fprintf(stderr,"digest as bignum: ");
209         BN_print_fp(stderr,md);
210         fprintf(stderr,"\ndigest mod q: ");
211         BN_print_fp(stderr,e);
212 #endif  
213         if (BN_is_zero(e)) BN_one(e);
214         v=BN_mod_inverse(v,e,order,ctx);
215         BN_mod_mul(z1,sig->s,v,order,ctx);
216         BN_sub(tmp,order,sig->r);
217         BN_mod_mul(z2,tmp,v,order,ctx);
218 #ifdef DEBUG_SIGN
219         fprintf(stderr,"\nInverted digest value: ");
220         BN_print_fp(stderr,v);
221         fprintf(stderr,"\nz1: ");
222         BN_print_fp(stderr,z1);
223         fprintf(stderr,"\nz2: ");
224         BN_print_fp(stderr,z2);
225 #endif  
226         C = EC_POINT_new(group);
227         if (!EC_POINT_mul(group,C,z1,pub_key,z2,ctx)) 
228         {       
229                 GOSTerr(GOST_F_GOST2001_DO_VERIFY,ERR_R_EC_LIB);
230                 goto err;
231         }       
232         if (!EC_POINT_get_affine_coordinates_GFp(group,C,X,NULL,ctx)) 
233         {
234                 GOSTerr(GOST_F_GOST2001_DO_VERIFY,ERR_R_EC_LIB);
235                 goto err;
236         }
237         BN_mod(R,X,order,ctx);
238 #ifdef DEBUG_SIGN
239         fprintf(stderr,"\nX=");
240         BN_print_fp(stderr,X);
241         fprintf(stderr,"\nX mod q=");
242         BN_print_fp(stderr,R);
243         fprintf(stderr,"\n");
244 #endif  
245         if (BN_cmp(R,sig->r)!=0) {
246                 GOSTerr(GOST_F_GOST2001_DO_VERIFY,GOST_R_SIGNATURE_MISMATCH);
247         } else {
248                 ok = 1;
249         }
250 err:
251         EC_POINT_free(C);
252         BN_CTX_end(ctx);
253         BN_CTX_free(ctx);
254         BN_free(md);
255         return ok;
256 }
257 /*
258  * Computes GOST R 34.10-2001 public key
259  *
260  *
261  */ 
262 int gost2001_compute_public(EC_KEY *ec) 
263 {
264         const EC_GROUP *group = EC_KEY_get0_group(ec);
265         EC_POINT *pub_key=NULL;
266         const BIGNUM *priv_key=NULL;
267         BN_CTX *ctx=NULL;
268         int ok=0;
269
270         if (!group) {
271                 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,GOST_R_KEY_IS_NOT_INITIALIZED);
272                 return 0;
273         }       
274         ctx=BN_CTX_new();
275         BN_CTX_start(ctx);
276         if (!(priv_key=EC_KEY_get0_private_key(ec))) 
277         {
278                 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,ERR_R_EC_LIB);
279                 goto err;
280         }       
281
282         pub_key = EC_POINT_new(group);
283         if (!EC_POINT_mul(group,pub_key,priv_key,NULL,NULL,ctx)) 
284         {
285                 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,ERR_R_EC_LIB);
286                 goto err;
287         }       
288         if (!EC_KEY_set_public_key(ec,pub_key)) {
289                 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,ERR_R_EC_LIB);
290                 goto err;
291         }       
292         ok = 256;
293 err:
294         BN_CTX_end(ctx);
295         EC_POINT_free(pub_key);
296         BN_CTX_free(ctx);
297         return ok;
298 }
299 /*
300  * 
301  * Generates GOST R 34.10-2001 keypair
302  *
303  *
304  */ 
305 int gost2001_keygen(EC_KEY *ec) {
306         BIGNUM *order = BN_new(),*d=BN_new();
307         const EC_GROUP *group = EC_KEY_get0_group(ec);
308         EC_GROUP_get_order(group,order,NULL);
309         
310         do {
311                 if (!BN_rand_range(d,order)) 
312                 {
313                         GOSTerr(GOST_F_GOST2001_DO_SIGN,GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
314                         BN_free(d);
315                         BN_free(order);
316                         return 0;
317                 }       
318         } while (BN_is_zero(d));
319         EC_KEY_set_private_key(ec,d);
320         BN_free(d);
321         BN_free(order);
322         return gost2001_compute_public(ec);
323 }
324