]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_sign.c
Indentation
[openssl-gost/engine.git] / gost_ec_sign.c
1 /**********************************************************************
2  *                        gost_ec_sign.c                              *
3  *             Copyright (c) 2005-2013 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 1.0.0+ for compilation                   *
8  **********************************************************************/
9 #include "gost_lcl.h"
10 #include <string.h>
11 #include <openssl/rand.h>
12 #include <openssl/ecdsa.h>
13 #include <openssl/err.h>
14 #include "e_gost_err.h"
15 #ifdef DEBUG_SIGN
16 extern
17 void dump_signature(const char *message, const unsigned char *buffer,
18                     size_t len);
19 void dump_dsa_sig(const char *message, DSA_SIG *sig);
20 #else
21
22 # define dump_signature(a,b,c)
23 # define dump_dsa_sig(a,b)
24 #endif
25
26 /* Convert little-endian byte array into bignum */
27 BIGNUM *hashsum2bn(const unsigned char *dgst, int len)
28 {
29     unsigned char buf[64];
30     int i;
31
32     if (len > sizeof(buf))
33         return NULL;
34
35     for (i = 0; i < len; i++) {
36         buf[len - i - 1] = dgst[i];
37     }
38     return BN_bin2bn(buf, len, NULL);
39 }
40
41 static R3410_ec_params *gost_nid2params(int nid)
42 {
43     R3410_ec_params *params;
44
45     /* Search nid in 2012 paramset */
46     params = R3410_2012_512_paramset;
47     while (params->nid != NID_undef) {
48         if (params->nid == nid)
49             return params;
50         params++;
51     }
52
53     /* Search nid in 2001 paramset */
54     params = R3410_2001_paramset;
55     while (params->nid != NID_undef) {
56         if (params->nid == nid)
57             return params;
58         params++;
59     }
60
61     return NULL;
62 }
63
64 /*
65  * Fills EC_KEY structure hidden in the app_data field of DSA structure
66  * with parameter information, extracted from parameter array in
67  * params.c file.
68  *
69  * Also fils DSA->q field with copy of EC_GROUP order field to make
70  * DSA_size function work
71  */
72 int fill_GOST_EC_params(EC_KEY *eckey, int nid)
73 {
74     R3410_ec_params *params = gost_nid2params(nid);
75     EC_GROUP *grp = NULL;
76     EC_POINT *P = NULL;
77     BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y =
78         NULL, *cofactor = NULL;
79     BN_CTX *ctx;
80     int ok = 0;
81
82     if (!eckey || !params) {
83         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
84         return 0;
85     }
86
87     if (!(ctx = BN_CTX_new())) {
88         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
89         return 0;
90     }
91
92     BN_CTX_start(ctx);
93     p = BN_CTX_get(ctx);
94     a = BN_CTX_get(ctx);
95     b = BN_CTX_get(ctx);
96     x = BN_CTX_get(ctx);
97     y = BN_CTX_get(ctx);
98     q = BN_CTX_get(ctx);
99     cofactor = BN_CTX_get(ctx);
100     if (!p || !a || !b || !x || !y || !q || !cofactor) {
101         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
102         goto end;
103     }
104
105     if (!BN_hex2bn(&p, params->p)
106         || !BN_hex2bn(&a, params->a)
107         || !BN_hex2bn(&b, params->b)
108         || !BN_hex2bn(&cofactor, params->cofactor)) {
109         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
110         goto end;
111     }
112
113     grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
114     if (!grp) {
115         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
116         goto end;
117     }
118
119     P = EC_POINT_new(grp);
120     if (!P) {
121         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
122         goto end;
123     }
124
125     if (!BN_hex2bn(&x, params->x)
126         || !BN_hex2bn(&y, params->y)
127         || !EC_POINT_set_affine_coordinates_GFp(grp, P, x, y, ctx)
128         || !BN_hex2bn(&q, params->q)) {
129         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
130         goto end;
131     }
132
133     if (!EC_GROUP_set_generator(grp, P, q, cofactor)) {
134         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
135         goto end;
136     }
137     EC_GROUP_set_curve_name(grp, params->nid);
138     if (!EC_KEY_set_group(eckey, grp)) {
139         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
140         goto end;
141     }
142     ok = 1;
143  end:
144     if (P)
145         EC_POINT_free(P);
146     if (grp)
147         EC_GROUP_free(grp);
148     BN_CTX_end(ctx);
149     BN_CTX_free(ctx);
150     return ok;
151 }
152
153 /*
154  * Computes gost_ec signature as DSA_SIG structure
155  *
156  */
157 DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
158 {
159     DSA_SIG *newsig = NULL, *ret = NULL;
160     BIGNUM *md = NULL;
161     BIGNUM *order = NULL;
162     const EC_GROUP *group;
163     const BIGNUM *priv_key;
164     BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL,
165         *k = NULL, *e = NULL;
166
167     BIGNUM *new_r = NULL, *new_s = NULL;
168
169     EC_POINT *C = NULL;
170     BN_CTX *ctx;
171
172     OPENSSL_assert(dgst != NULL && eckey != NULL);
173
174     if (!(ctx = BN_CTX_new())) {
175         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
176         return NULL;
177     }
178
179     BN_CTX_start(ctx);
180     OPENSSL_assert(dlen == 32 || dlen == 64);
181     md = hashsum2bn(dgst, dlen);
182     newsig = DSA_SIG_new();
183     if (!newsig || !md) {
184         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
185         goto err;
186     }
187     group = EC_KEY_get0_group(eckey);
188     if (!group) {
189         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
190         goto err;
191     }
192     order = BN_CTX_get(ctx);
193     if (!order || !EC_GROUP_get_order(group, order, ctx)) {
194         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
195         goto err;
196     }
197     priv_key = EC_KEY_get0_private_key(eckey);
198     if (!priv_key) {
199         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
200         goto err;
201     }
202     e = BN_CTX_get(ctx);
203     if (!e || !BN_mod(e, md, order, ctx)) {
204         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
205         goto err;
206     }
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     fprintf(stderr, "\n");
213 #endif
214     if (BN_is_zero(e)) {
215         BN_one(e);
216     }
217     k = BN_CTX_get(ctx);
218     C = EC_POINT_new(group);
219     if (!k || !C) {
220         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
221         goto err;
222     }
223
224     do {
225         do {
226             if (!BN_rand_range(k, order)) {
227                 GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_RNG_ERROR);
228                 goto err;
229             }
230             /*
231              * To avoid timing information leaking the length of k,
232              * compute C*k using an equivalent scalar of fixed bit-length */
233             if (!BN_add(k, k, order)
234                 || (BN_num_bits(k) <= BN_num_bits(order)
235                     && !BN_add(k, k, order))) {
236                 goto err;
237             }
238             if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
239                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
240                 goto err;
241             }
242             if (!X)
243                 X = BN_CTX_get(ctx);
244             if (!r)
245                 r = BN_CTX_get(ctx);
246             if (!X || !r) {
247                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
248                 goto err;
249             }
250             if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
251                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
252                 goto err;
253             }
254
255             if (!BN_nnmod(r, X, order, ctx)) {
256                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
257                 goto err;
258             }
259         }
260         while (BN_is_zero(r));
261         /* s =  (r*priv_key+k*e) mod order */
262         if (!tmp)
263             tmp = BN_CTX_get(ctx);
264         if (!tmp2)
265             tmp2 = BN_CTX_get(ctx);
266         if (!s)
267             s = BN_CTX_get(ctx);
268         if (!tmp || !tmp2 || !s) {
269             GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
270             goto err;
271         }
272
273         if (!BN_mod_mul(tmp, priv_key, r, order, ctx)
274             || !BN_mod_mul(tmp2, k, e, order, ctx)
275             || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
276             GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
277             goto err;
278         }
279     }
280     while (BN_is_zero(s));
281
282     new_s = BN_dup(s);
283     new_r = BN_dup(r);
284     if (!new_s || !new_r) {
285         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
286         goto err;
287     }
288     DSA_SIG_set0(newsig, new_r, new_s);
289
290     ret = newsig;
291  err:
292     BN_CTX_end(ctx);
293     BN_CTX_free(ctx);
294     if (C)
295         EC_POINT_free(C);
296     if (md)
297         BN_free(md);
298     if (!ret && newsig) {
299         DSA_SIG_free(newsig);
300     }
301     return ret;
302 }
303
304 /*
305  * Verifies gost ec signature
306  *
307  */
308 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
309                    DSA_SIG *sig, EC_KEY *ec)
310 {
311     BN_CTX *ctx;
312     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
313     BIGNUM *order;
314     BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
315     const BIGNUM *sig_s = NULL, *sig_r = NULL;
316     BIGNUM *X = NULL, *tmp = NULL;
317     EC_POINT *C = NULL;
318     const EC_POINT *pub_key = NULL;
319     int ok = 0;
320
321     OPENSSL_assert(dgst != NULL && sig != NULL && group != NULL);
322
323     if (!(ctx = BN_CTX_new())) {
324         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
325         return 0;
326     }
327
328     BN_CTX_start(ctx);
329     order = BN_CTX_get(ctx);
330     e = BN_CTX_get(ctx);
331     z1 = BN_CTX_get(ctx);
332     z2 = BN_CTX_get(ctx);
333     tmp = BN_CTX_get(ctx);
334     X = BN_CTX_get(ctx);
335     R = BN_CTX_get(ctx);
336     v = BN_CTX_get(ctx);
337     if (!order || !e || !z1 || !z2 || !tmp || !X || !R || !v) {
338         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
339         goto err;
340     }
341
342     pub_key = EC_KEY_get0_public_key(ec);
343     if (!pub_key || !EC_GROUP_get_order(group, order, ctx)) {
344         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
345         goto err;
346     }
347
348     DSA_SIG_get0(sig, &sig_r, &sig_s);
349
350     if (BN_is_zero(sig_s) || BN_is_zero(sig_r) ||
351         (BN_cmp(sig_s, order) >= 1) || (BN_cmp(sig_r, order) >= 1)) {
352         GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
353         goto err;
354
355     }
356
357     OPENSSL_assert(dgst_len == 32 || dgst_len == 64);
358     md = hashsum2bn(dgst, dgst_len);
359     if (!md || !BN_mod(e, md, order, ctx)) {
360         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
361         goto err;
362     }
363 #ifdef DEBUG_SIGN
364     fprintf(stderr, "digest as bignum: ");
365     BN_print_fp(stderr, md);
366     fprintf(stderr, "\ndigest mod q: ");
367     BN_print_fp(stderr, e);
368 #endif
369     if (BN_is_zero(e) && !BN_one(e)) {
370         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
371         goto err;
372     }
373     v = BN_mod_inverse(v, e, order, ctx);
374     if (!v || !BN_mod_mul(z1, sig_s, v, order, ctx)
375         || !BN_sub(tmp, order, sig_r)
376         || !BN_mod_mul(z2, tmp, v, order, ctx)) {
377         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
378         goto err;
379     }
380 #ifdef DEBUG_SIGN
381     fprintf(stderr, "\nInverted digest value: ");
382     BN_print_fp(stderr, v);
383     fprintf(stderr, "\nz1: ");
384     BN_print_fp(stderr, z1);
385     fprintf(stderr, "\nz2: ");
386     BN_print_fp(stderr, z2);
387 #endif
388     C = EC_POINT_new(group);
389     if (!C) {
390         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
391         goto err;
392     }
393     if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) {
394         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
395         goto err;
396     }
397     if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
398         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
399         goto err;
400     }
401     if (!BN_mod(R, X, order, ctx)) {
402         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
403         goto err;
404     }
405 #ifdef DEBUG_SIGN
406     fprintf(stderr, "\nX=");
407     BN_print_fp(stderr, X);
408     fprintf(stderr, "\nX mod q=");
409     BN_print_fp(stderr, R);
410     fprintf(stderr, "\n");
411 #endif
412     if (BN_cmp(R, sig_r) != 0) {
413         GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_MISMATCH);
414     } else {
415         ok = 1;
416     }
417  err:
418     if (C)
419         EC_POINT_free(C);
420     BN_CTX_end(ctx);
421     BN_CTX_free(ctx);
422     if (md)
423         BN_free(md);
424     return ok;
425 }
426
427 /*
428  * Computes GOST R 34.10-2001 public key
429  * or GOST R 34.10-2012 public key
430  *
431  */
432 int gost_ec_compute_public(EC_KEY *ec)
433 {
434     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
435     EC_POINT *pub_key = NULL;
436     const BIGNUM *priv_key = NULL;
437     BN_CTX *ctx = NULL;
438     int ok = 0;
439
440     if (!group) {
441         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITIALIZED);
442         return 0;
443     }
444
445     ctx = BN_CTX_new();
446     if (!ctx) {
447         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
448         return 0;
449     }
450
451     BN_CTX_start(ctx);
452     priv_key = EC_KEY_get0_private_key(ec);
453     if (!priv_key) {
454         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
455         goto err;
456     }
457
458     pub_key = EC_POINT_new(group);
459     if (!pub_key) {
460         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
461         goto err;
462     }
463
464     if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
465         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
466         goto err;
467     }
468     if (!EC_KEY_set_public_key(ec, pub_key)) {
469         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
470         goto err;
471     }
472     ok = 1;
473  err:
474     if (pub_key)
475         EC_POINT_free(pub_key);
476     BN_CTX_end(ctx);
477     BN_CTX_free(ctx);
478     return ok;
479 }
480
481 /*
482  *
483  * Generates GOST R 34.10-2001
484  * or GOST R 34.10-2012 keypair
485  *
486  */
487 int gost_ec_keygen(EC_KEY *ec)
488 {
489     BIGNUM *order = NULL, *d = NULL;
490     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
491     int ok = 0;
492
493     if (!group) {
494         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
495         return 0;
496     }
497
498     order = BN_new();
499     d = BN_new();
500     if (!order || !d) {
501         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_MALLOC_FAILURE);
502         goto end;
503     }
504
505     if (!EC_GROUP_get_order(group, order, NULL)) {
506         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
507         goto end;
508     }
509
510     do {
511         if (!BN_rand_range(d, order)) {
512             GOSTerr(GOST_F_GOST_EC_KEYGEN, GOST_R_RNG_ERROR);
513             goto end;
514         }
515     }
516     while (BN_is_zero(d));
517
518     if (!EC_KEY_set_private_key(ec, d)) {
519         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
520         goto end;
521     }
522
523     ok = 1;
524  end:
525     if (d)
526         BN_free(d);
527     if (order)
528         BN_free(order);
529
530     return (ok) ? gost_ec_compute_public(ec) : 0;
531 }