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