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