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