Add missing prototype. Fix various warnings (C++ comments, ; outside function).
[openssl.git] / engines / ccgost / gost_sign.c
1 /**********************************************************************
2  *                          gost_sign.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-94 signature algoritgthm      *
7  *       for OpenSSL                                                  *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <string.h>
11 #include <openssl/rand.h>
12 #include <openssl/bn.h>
13 #include <openssl/dsa.h>
14 #include <openssl/evp.h>
15
16 #include "gost_params.h"
17 #include "gost_lcl.h"
18 #include "e_gost_err.h"
19
20 #ifdef DEBUG_SIGN
21 void dump_signature(const char *message,const unsigned char *buffer,size_t len)
22         {
23         size_t i;
24         fprintf(stderr,"signature %s Length=%d",message,len);
25         for (i=0; i<len; i++)
26                 {
27                 if (i% 16 ==0) fputc('\n',stderr);
28                 fprintf (stderr," %02x",buffer[i]);
29                 }
30         fprintf(stderr,"\nEnd of signature\n");
31         }
32
33 void dump_dsa_sig(const char *message, DSA_SIG *sig)
34         {
35         fprintf(stderr,"%s\nR=",message);
36         BN_print_fp(stderr,sig->r);
37         fprintf(stderr,"\nS=");
38         BN_print_fp(stderr,sig->s);
39         fprintf(stderr,"\n");
40         }
41
42 #else
43
44 #define dump_signature(a,b,c)
45 #define dump_dsa_sig(a,b)
46 #endif
47
48 /*
49  * Computes signature and returns it as DSA_SIG structure
50  */
51 DSA_SIG *gost_do_sign(const unsigned char *dgst,int dlen, DSA *dsa)
52         {
53         BIGNUM *k=NULL,*tmp=NULL,*tmp2=NULL;
54         DSA_SIG *newsig = DSA_SIG_new();
55         BIGNUM *md = hashsum2bn(dgst);
56         /* check if H(M) mod q is zero */
57         BN_CTX *ctx=BN_CTX_new();
58         BN_CTX_start(ctx);
59         if (!newsig)
60                 {
61                 GOSTerr(GOST_F_GOST_DO_SIGN,GOST_R_NO_MEMORY);
62                 goto err;
63                 }       
64         tmp=BN_CTX_get(ctx);
65         k = BN_CTX_get(ctx);
66         tmp2 = BN_CTX_get(ctx);
67         BN_mod(tmp,md,dsa->q,ctx);
68         if (BN_is_zero(tmp))
69                 {
70                 BN_one(md);
71                 }       
72         do
73                 {
74                 do
75                         {
76                         /*Generate random number k less than q*/
77                         BN_rand_range(k,dsa->q);
78                         /* generate r = (a^x mod p) mod q */
79                         BN_mod_exp(tmp,dsa->g, k, dsa->p,ctx);
80                         if (!(newsig->r)) newsig->r=BN_new();
81                         BN_mod(newsig->r,tmp,dsa->q,ctx);
82                         }
83                 while (BN_is_zero(newsig->r));
84                 /* generate s = (xr + k(Hm)) mod q */
85                 BN_mod_mul(tmp,dsa->priv_key,newsig->r,dsa->q,ctx);
86                 BN_mod_mul(tmp2,k,md,dsa->q,ctx);
87                 if (!newsig->s) newsig->s=BN_new();
88                 BN_mod_add(newsig->s,tmp,tmp2,dsa->q,ctx);
89                 }
90         while (BN_is_zero(newsig->s));          
91         err:
92         BN_free(md);
93         BN_CTX_end(ctx);
94         BN_CTX_free(ctx);
95         return newsig;
96         }       
97
98
99 /*
100  * Packs signature according to Cryptocom rules
101  * and frees up DSA_SIG structure
102  */
103
104 int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
105         {
106         *siglen = 2*order;
107         memset(sig,0,*siglen);
108         store_bignum(s->r, sig,order);
109         store_bignum(s->s, sig + order,order);
110         dump_signature("serialized",sig,*siglen);
111         DSA_SIG_free(s);
112         return 1;
113         }
114 /*
115  * Packs signature according to Cryptopro rules
116  * and frees up DSA_SIG structure
117  */
118 int pack_sign_cp(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
119         {
120         *siglen = 2*order;
121         memset(sig,0,*siglen);
122         store_bignum(s->s, sig, order);
123         store_bignum(s->r, sig+order,order);
124         dump_signature("serialized",sig,*siglen);
125         DSA_SIG_free(s);
126         return 1;
127         }
128
129 /*
130  * Verifies signature passed as DSA_SIG structure
131  *
132  */
133
134 int gost_do_verify(const unsigned char *dgst, int dgst_len,
135         DSA_SIG *sig, DSA *dsa)
136         {
137         BIGNUM *md, *tmp=NULL;
138         BIGNUM *q2=NULL;
139         BIGNUM *u=NULL,*v=NULL,*z1=NULL,*z2=NULL;
140         BIGNUM *tmp2=NULL,*tmp3=NULL;
141         int ok;
142         BN_CTX *ctx = BN_CTX_new();
143
144         BN_CTX_start(ctx);
145         if (BN_cmp(sig->s,dsa->q)>=1||
146                 BN_cmp(sig->r,dsa->q)>=1)
147                 {
148                 GOSTerr(GOST_F_GOST_DO_VERIFY,GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
149                 return 0;
150                 }
151         md=hashsum2bn(dgst);
152         
153         tmp=BN_CTX_get(ctx);
154         v=BN_CTX_get(ctx);
155         q2=BN_CTX_get(ctx);
156         z1=BN_CTX_get(ctx);
157         z2=BN_CTX_get(ctx);
158         tmp2=BN_CTX_get(ctx);
159         tmp3=BN_CTX_get(ctx);
160         u = BN_CTX_get(ctx);
161         
162         BN_mod(tmp,md,dsa->q,ctx);
163         if (BN_is_zero(tmp))
164                 {
165                 BN_one(md);
166                 }
167         BN_copy(q2,dsa->q);
168         BN_sub_word(q2,2);
169         BN_mod_exp(v,md,q2,dsa->q,ctx);
170         BN_mod_mul(z1,sig->s,v,dsa->q,ctx);
171         BN_sub(tmp,dsa->q,sig->r);
172         BN_mod_mul(z2,tmp,v,dsa->p,ctx);
173         BN_mod_exp(tmp,dsa->g,z1,dsa->p,ctx);
174         BN_mod_exp(tmp2,dsa->pub_key,z2,dsa->p,ctx);
175         BN_mod_mul(tmp3,tmp,tmp2,dsa->p,ctx);
176         BN_mod(u,tmp3,dsa->q,ctx);
177         ok= BN_cmp(u,sig->r);
178         
179         BN_free(md);
180         BN_CTX_end(ctx);
181         BN_CTX_free(ctx);
182         if (ok!=0)
183                 {
184                 GOSTerr(GOST_F_GOST_DO_VERIFY,GOST_R_SIGNATURE_MISMATCH);
185                 }       
186         return (ok==0);
187         }
188
189 /*
190  * Computes public keys for GOST R 34.10-94 algorithm
191  *
192  */
193 int gost94_compute_public(DSA *dsa)
194         {
195         /* Now fill algorithm parameters with correct values */
196         BN_CTX *ctx = BN_CTX_new();
197         if (!dsa->g)
198                 {
199                 GOSTerr(GOST_F_GOST_COMPUTE_PUBLIC,GOST_R_KEY_IS_NOT_INITALIZED);
200                 return 0;
201                 }       
202         /* Compute public key  y = a^x mod p */
203         dsa->pub_key=BN_new();
204         BN_mod_exp(dsa->pub_key, dsa->g,dsa->priv_key,dsa->p,ctx);
205         BN_CTX_free(ctx);
206         return 1;
207         }
208
209 /*
210  * Fill GOST 94 params, searching them in R3410_paramset array
211  * by nid of paramset
212  *
213  */
214 int fill_GOST94_params(DSA *dsa,int nid)
215         {
216         R3410_params *params=R3410_paramset;
217         while (params->nid!=NID_undef && params->nid !=nid) params++;
218         if (params->nid == NID_undef)
219                 {
220                 GOSTerr(GOST_F_FILL_GOST94_PARAMS,GOST_R_UNSUPPORTED_PARAMETER_SET);
221                 return 0;
222                 }       
223 #define dump_signature(a,b,c)
224         if (dsa->p) { BN_free(dsa->p); }
225         dsa->p=NULL;
226         BN_dec2bn(&(dsa->p),params->p);
227         if (dsa->q) { BN_free(dsa->q); }
228         dsa->q=NULL;
229         BN_dec2bn(&(dsa->q),params->q);
230         if (dsa->g) { BN_free(dsa->g); }
231         dsa->g=NULL;
232         BN_dec2bn(&(dsa->g),params->a);
233         return 1;
234         }       
235
236 /*
237  *  Generate GOST R 34.10-94 keypair
238  *
239  *
240  */
241 int gost_sign_keygen(DSA *dsa)
242         {
243         dsa->priv_key = BN_new();
244         BN_rand_range(dsa->priv_key,dsa->q);
245         return gost94_compute_public( dsa);
246         }
247
248 /* Unpack signature according to cryptocom rules  */
249
250 DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen)
251         {
252         DSA_SIG *s;
253         s = DSA_SIG_new();
254         if (s == NULL)
255                 {
256                 GOSTerr(GOST_F_UNPACK_CC_SIGNATURE,GOST_R_NO_MEMORY);
257                 return(NULL);
258                 }
259         s->r = getbnfrombuf(sig, siglen/2);
260         s->s = getbnfrombuf(sig + siglen/2, siglen/2);
261         return s;
262         }
263
264 /* Unpack signature according to cryptopro rules  */
265 DSA_SIG *unpack_cp_signature(const unsigned char *sig,size_t siglen)
266         {
267         DSA_SIG *s;
268
269         s = DSA_SIG_new();
270         if (s == NULL)
271                 {
272                 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE,GOST_R_NO_MEMORY);
273                 return NULL;
274                 }
275         s->s = getbnfrombuf(sig , siglen/2);
276         s->r = getbnfrombuf(sig + siglen/2, siglen/2);
277         return s;
278         }
279
280 /* Convert little-endian byte array into bignum */
281 BIGNUM *hashsum2bn(const unsigned char *dgst)
282         {
283         unsigned char buf[32];
284         int i;
285         for (i=0;i<32;i++)
286                 {
287                 buf[31-i]=dgst[i];
288                 }
289         return getbnfrombuf(buf,32);
290         }
291
292 /* Convert byte buffer to bignum, skipping leading zeros*/
293 BIGNUM *getbnfrombuf(const unsigned char *buf,size_t len)
294         {
295         while (*buf==0&&len>0)
296                 {
297                 buf++; len--;
298                 }
299         if (len)
300                 {
301                 return BN_bin2bn(buf,len,NULL);
302                 }
303         else
304                 {
305                 BIGNUM *b=BN_new();
306                 BN_zero(b);
307                 return b;
308                 }
309         }
310
311 /* Pack bignum into byte buffer of given size, filling all leading bytes
312  * by zeros */
313 int store_bignum(BIGNUM *bn, unsigned char *buf,int len)
314         {
315         int bytes = BN_num_bytes(bn);
316         if (bytes>len) return 0;
317         memset(buf,0,len);
318         BN_bn2bin(bn,buf+len-bytes);
319         return 1;
320         }