RT3963: Allow OCSP stapling with -rev and -www
[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 = NULL, *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     if (!ret)
121         DSA_SIG_free(newsig);
122     return ret;
123 }
124
125 /*
126  * Packs signature according to Cryptocom rules
127  * and frees up DSA_SIG structure
128  */
129 /*-
130 int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
131         {
132         *siglen = 2*order;
133         memset(sig,0,*siglen);
134         store_bignum(s->r, sig,order);
135         store_bignum(s->s, sig + order,order);
136         dump_signature("serialized",sig,*siglen);
137         DSA_SIG_free(s);
138         return 1;
139         }
140 */
141 /*
142  * Packs signature according to Cryptopro rules
143  * and frees up DSA_SIG structure
144  */
145 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
146 {
147     *siglen = 2 * order;
148     memset(sig, 0, *siglen);
149     store_bignum(s->s, sig, order);
150     store_bignum(s->r, sig + order, order);
151     dump_signature("serialized", sig, *siglen);
152     DSA_SIG_free(s);
153     return 1;
154 }
155
156 /*
157  * Verifies signature passed as DSA_SIG structure
158  *
159  */
160
161 int gost_do_verify(const unsigned char *dgst, int dgst_len,
162                    DSA_SIG *sig, DSA *dsa)
163 {
164     BIGNUM *md = NULL, *tmp = NULL;
165     BIGNUM *q2 = NULL;
166     BIGNUM *u = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
167     BIGNUM *tmp2 = NULL, *tmp3 = NULL;
168     int ok = 0;
169     BN_CTX *ctx = BN_CTX_new();
170     if (!ctx) {
171         GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
172         goto err;
173     }
174
175     BN_CTX_start(ctx);
176     if (BN_cmp(sig->s, dsa->q) >= 1 || BN_cmp(sig->r, dsa->q) >= 1) {
177         GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
178         goto err;
179     }
180     md = hashsum2bn(dgst);
181
182     tmp = BN_CTX_get(ctx);
183     v = BN_CTX_get(ctx);
184     q2 = BN_CTX_get(ctx);
185     z1 = BN_CTX_get(ctx);
186     z2 = BN_CTX_get(ctx);
187     tmp2 = BN_CTX_get(ctx);
188     tmp3 = BN_CTX_get(ctx);
189     u = BN_CTX_get(ctx);
190     if (!tmp || !v || !q2 || !z1 || !z2 || !tmp2 || !tmp3 || !u) {
191         GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
192         goto err;
193     }
194
195     BN_mod(tmp, md, dsa->q, ctx);
196     if (BN_is_zero(tmp)) {
197         BN_one(md);
198     }
199     BN_copy(q2, dsa->q);
200     BN_sub_word(q2, 2);
201     BN_mod_exp(v, md, q2, dsa->q, ctx);
202     BN_mod_mul(z1, sig->s, v, dsa->q, ctx);
203     BN_sub(tmp, dsa->q, sig->r);
204     BN_mod_mul(z2, tmp, v, dsa->p, ctx);
205     BN_mod_exp(tmp, dsa->g, z1, dsa->p, ctx);
206     BN_mod_exp(tmp2, dsa->pub_key, z2, dsa->p, ctx);
207     BN_mod_mul(tmp3, tmp, tmp2, dsa->p, ctx);
208     BN_mod(u, tmp3, dsa->q, ctx);
209     ok = (BN_cmp(u, sig->r) == 0);
210
211     if (!ok) {
212         GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
213     }
214 err:
215     BN_free(md);
216     if (ctx)
217         BN_CTX_end(ctx);
218     BN_CTX_free(ctx);
219     return (ok == 0);
220 }
221
222 /*
223  * Computes public keys for GOST R 34.10-94 algorithm
224  *
225  */
226 int gost94_compute_public(DSA *dsa)
227 {
228     /* Now fill algorithm parameters with correct values */
229     BN_CTX *ctx;
230     if (!dsa->g) {
231         GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITALIZED);
232         return 0;
233     }
234     ctx = BN_CTX_new();
235     if (!ctx) {
236         GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
237         return 0;
238     }
239
240     dsa->pub_key = BN_new();
241     if (!dsa->pub_key) {
242         GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
243         BN_CTX_free(ctx);
244         return 0;
245     }
246     /* Compute public key  y = a^x mod p */
247     BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx);
248     BN_CTX_free(ctx);
249     return 1;
250 }
251
252 /*
253  * Fill GOST 94 params, searching them in R3410_paramset array
254  * by nid of paramset
255  *
256  */
257 int fill_GOST94_params(DSA *dsa, int nid)
258 {
259     R3410_params *params = R3410_paramset;
260     while (params->nid != NID_undef && params->nid != nid)
261         params++;
262     if (params->nid == NID_undef) {
263         GOSTerr(GOST_F_FILL_GOST94_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
264         return 0;
265     }
266 #define dump_signature(a,b,c)
267     BN_free(dsa->p);
268     dsa->p = NULL;
269     BN_dec2bn(&(dsa->p), params->p);
270     BN_free(dsa->q);
271     dsa->q = NULL;
272     BN_dec2bn(&(dsa->q), params->q);
273     BN_free(dsa->g);
274     dsa->g = NULL;
275     BN_dec2bn(&(dsa->g), params->a);
276     return 1;
277 }
278
279 /*
280  *  Generate GOST R 34.10-94 keypair
281  *
282  *
283  */
284 int gost_sign_keygen(DSA *dsa)
285 {
286     dsa->priv_key = BN_new();
287     if (!dsa->priv_key) {
288         GOSTerr(GOST_F_GOST_SIGN_KEYGEN, ERR_R_MALLOC_FAILURE);
289         return 0;
290     }
291     BN_rand_range(dsa->priv_key, dsa->q);
292     return gost94_compute_public(dsa);
293 }
294
295 /* Unpack signature according to cryptocom rules  */
296 /*-
297 DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen)
298         {
299         DSA_SIG *s;
300         s = DSA_SIG_new();
301         if (s == NULL)
302                 {
303                 GOSTerr(GOST_F_UNPACK_CC_SIGNATURE,ERR_R_MALLOC_FAILURE);
304                 return(NULL);
305                 }
306         s->r = getbnfrombuf(sig, siglen/2);
307         s->s = getbnfrombuf(sig + siglen/2, siglen/2);
308         return s;
309         }
310 */
311 /* Unpack signature according to cryptopro rules  */
312 DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
313 {
314     DSA_SIG *s;
315
316     s = DSA_SIG_new();
317     if (s == NULL) {
318         GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
319         return NULL;
320     }
321     s->s = getbnfrombuf(sig, siglen / 2);
322     s->r = getbnfrombuf(sig + siglen / 2, siglen / 2);
323     return s;
324 }
325
326 /* Convert little-endian byte array into bignum */
327 BIGNUM *hashsum2bn(const unsigned char *dgst)
328 {
329     unsigned char buf[32];
330     int i;
331     for (i = 0; i < 32; i++) {
332         buf[31 - i] = dgst[i];
333     }
334     return getbnfrombuf(buf, 32);
335 }
336
337 /* Convert byte buffer to bignum, skipping leading zeros*/
338 BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len)
339 {
340     while (*buf == 0 && len > 0) {
341         buf++;
342         len--;
343     }
344     if (len) {
345         return BN_bin2bn(buf, len, NULL);
346     } else {
347         BIGNUM *b = BN_new();
348         BN_zero(b);
349         return b;
350     }
351 }
352
353 /*
354  * Pack bignum into byte buffer of given size, filling all leading bytes by
355  * zeros
356  */
357 int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
358 {
359     int bytes = BN_num_bytes(bn);
360     if (bytes > len)
361         return 0;
362     memset(buf, 0, len);
363     BN_bn2bin(bn, buf + len - bytes);
364     return 1;
365 }