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