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