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