]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_sign.c
Merge branch 'master' of https://github.com/gost-engine/engine
[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 = NULL, *cofactor = NULL;
78     BN_CTX *ctx;
79     int ok = 0;
80
81     if (!eckey || !params) {
82         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
83         return 0;
84     }
85
86     if (!(ctx = BN_CTX_new())) {
87         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
88         return 0;
89     }
90
91     BN_CTX_start(ctx);
92     p = BN_CTX_get(ctx);
93     a = BN_CTX_get(ctx);
94     b = BN_CTX_get(ctx);
95     x = BN_CTX_get(ctx);
96     y = BN_CTX_get(ctx);
97     q = BN_CTX_get(ctx);
98                 cofactor = BN_CTX_get(ctx);
99     if (!p || !a || !b || !x || !y || !q || !cofactor) {
100         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
101         goto end;
102     }
103
104     if (!BN_hex2bn(&p, params->p)
105         || !BN_hex2bn(&a, params->a)
106         || !BN_hex2bn(&b, params->b)
107                                 || !BN_hex2bn(&cofactor, params->cofactor) ) {
108         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
109         goto end;
110     }
111
112     grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
113     if (!grp) {
114         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
115         goto end;
116     }
117
118     P = EC_POINT_new(grp);
119     if (!P) {
120         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
121         goto end;
122     }
123
124     if (!BN_hex2bn(&x, params->x)
125         || !BN_hex2bn(&y, params->y)
126         || !EC_POINT_set_affine_coordinates_GFp(grp, P, x, y, ctx)
127         || !BN_hex2bn(&q, params->q)) {
128         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
129         goto end;
130     }
131
132     if (!EC_GROUP_set_generator(grp, P, q, cofactor)) {
133         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
134         goto end;
135     }
136     EC_GROUP_set_curve_name(grp, params->nid);
137     if (!EC_KEY_set_group(eckey, grp)) {
138         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
139         goto end;
140     }
141     ok = 1;
142  end:
143     if (P)
144         EC_POINT_free(P);
145     if (grp)
146         EC_GROUP_free(grp);
147     BN_CTX_end(ctx);
148     BN_CTX_free(ctx);
149     return ok;
150 }
151
152 /*
153  * Computes gost_ec signature as DSA_SIG structure
154  *
155  */
156 DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
157 {
158     DSA_SIG *newsig = NULL, *ret = NULL;
159     BIGNUM *md = NULL;
160     BIGNUM *order = NULL;
161     const EC_GROUP *group;
162     const BIGNUM *priv_key;
163     BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL,
164         *k = NULL, *e = NULL;
165
166     BIGNUM *new_r = NULL, *new_s = NULL;
167
168     EC_POINT *C = NULL;
169     BN_CTX *ctx;
170
171     OPENSSL_assert(dgst != NULL && eckey != NULL);
172
173     if (!(ctx = BN_CTX_new())) {
174         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
175         return NULL;
176     }
177
178     BN_CTX_start(ctx);
179     OPENSSL_assert(dlen == 32 || dlen == 64);
180     md = hashsum2bn(dgst, dlen);
181     newsig = DSA_SIG_new();
182     if (!newsig || !md) {
183         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
184         goto err;
185     }
186     group = EC_KEY_get0_group(eckey);
187     if (!group) {
188         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
189         goto err;
190     }
191     order = BN_CTX_get(ctx);
192     if (!order || !EC_GROUP_get_order(group, order, ctx)) {
193         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
194         goto err;
195     }
196     priv_key = EC_KEY_get0_private_key(eckey);
197     if (!priv_key) {
198         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
199         goto err;
200     }
201     e = BN_CTX_get(ctx);
202     if (!e || !BN_mod(e, md, order, ctx)) {
203         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
204         goto err;
205     }
206 #ifdef DEBUG_SIGN
207     fprintf(stderr, "digest as bignum=");
208     BN_print_fp(stderr, md);
209     fprintf(stderr, "\ndigest mod q=");
210     BN_print_fp(stderr, e);
211     fprintf(stderr, "\n");
212 #endif
213     if (BN_is_zero(e)) {
214         BN_one(e);
215     }
216     k = BN_CTX_get(ctx);
217     C = EC_POINT_new(group);
218     if (!k || !C) {
219         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
220         goto err;
221     }
222
223     do {
224         do {
225             if (!BN_rand_range(k, order)) {
226                 GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_RNG_ERROR);
227                 goto err;
228             }
229             /*
230              * To avoid timing information leaking the length of k,
231              * compute C*k using an equivalent scalar of fixed bit-length */
232             if (!BN_add(k, k, order)
233                 || (BN_num_bits(k) <= BN_num_bits(order)
234                     && !BN_add(k, k, order))) {
235                 goto err;
236             }
237             if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
238                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
239                 goto err;
240             }
241             if (!X)
242                 X = BN_CTX_get(ctx);
243             if (!r)
244                 r = BN_CTX_get(ctx);
245             if (!X || !r) {
246                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
247                 goto err;
248             }
249             if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
250                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
251                 goto err;
252             }
253
254             if (!BN_nnmod(r, X, order, ctx)) {
255                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
256                 goto err;
257             }
258         }
259         while (BN_is_zero(r));
260         /* s =  (r*priv_key+k*e) mod order */
261         if (!tmp)
262             tmp = BN_CTX_get(ctx);
263         if (!tmp2)
264             tmp2 = BN_CTX_get(ctx);
265         if (!s)
266             s = BN_CTX_get(ctx);
267         if (!tmp || !tmp2 || !s) {
268             GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
269             goto err;
270         }
271
272         if (!BN_mod_mul(tmp, priv_key, r, order, ctx)
273             || !BN_mod_mul(tmp2, k, e, order, ctx)
274             || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
275             GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
276             goto err;
277         }
278     }
279     while (BN_is_zero(s));
280
281     new_s = BN_dup(s);
282     new_r = BN_dup(r);
283     if (!new_s || !new_r) {
284         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
285         goto err;
286     }
287     DSA_SIG_set0(newsig, new_r, new_s);
288
289     ret = newsig;
290  err:
291     BN_CTX_end(ctx);
292     BN_CTX_free(ctx);
293     if (C)
294         EC_POINT_free(C);
295     if (md)
296         BN_free(md);
297     if (!ret && newsig) {
298         DSA_SIG_free(newsig);
299     }
300     return ret;
301 }
302
303 /*
304  * Verifies gost ec signature
305  *
306  */
307 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
308                    DSA_SIG *sig, EC_KEY *ec)
309 {
310     BN_CTX *ctx;
311     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
312     BIGNUM *order;
313     BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL,
314         *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 }