]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_sign.c
ec: Use BN_{CTX_,}secure_new memory API for priv keys
[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 static R3410_ec_params *gost_nid2params(int nid)
27 {
28     R3410_ec_params *params;
29
30     /* Map tc26-2012 256-bit parameters to cp-2001 parameters */
31     switch (nid) {
32         case NID_id_tc26_gost_3410_2012_256_paramSetB:
33             nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
34             break;
35         case NID_id_tc26_gost_3410_2012_256_paramSetC:
36             nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
37             break;
38         case NID_id_tc26_gost_3410_2012_256_paramSetD:
39             nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
40     }
41
42     /* Search nid in 2012 paramset */
43     params = R3410_2012_512_paramset;
44     while (params->nid != NID_undef) {
45         if (params->nid == nid)
46             return params;
47         params++;
48     }
49
50     /* Search nid in 2001 paramset */
51     params = R3410_2001_paramset;
52     while (params->nid != NID_undef) {
53         if (params->nid == nid)
54             return params;
55         params++;
56     }
57
58     return NULL;
59 }
60
61 /*
62  * Fills EC_KEY structure hidden in the app_data field of DSA structure
63  * with parameter information, extracted from parameter array in
64  * params.c file.
65  *
66  * Also fils DSA->q field with copy of EC_GROUP order field to make
67  * DSA_size function work
68  */
69 int fill_GOST_EC_params(EC_KEY *eckey, int nid)
70 {
71     R3410_ec_params *params = gost_nid2params(nid);
72     EC_GROUP *grp = NULL;
73     EC_POINT *P = NULL;
74     BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y =
75         NULL, *cofactor = NULL;
76     BN_CTX *ctx;
77     int ok = 0;
78
79     if (!eckey || !params) {
80         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
81         return 0;
82     }
83
84     if (!(ctx = BN_CTX_new())) {
85         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
86         return 0;
87     }
88
89     BN_CTX_start(ctx);
90     p = BN_CTX_get(ctx);
91     a = BN_CTX_get(ctx);
92     b = BN_CTX_get(ctx);
93     x = BN_CTX_get(ctx);
94     y = BN_CTX_get(ctx);
95     q = BN_CTX_get(ctx);
96     cofactor = BN_CTX_get(ctx);
97     if (!p || !a || !b || !x || !y || !q || !cofactor) {
98         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
99         goto end;
100     }
101
102     if (!BN_hex2bn(&p, params->p)
103         || !BN_hex2bn(&a, params->a)
104         || !BN_hex2bn(&b, params->b)
105         || !BN_hex2bn(&cofactor, params->cofactor)) {
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(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, cofactor)) {
131         GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
132         goto end;
133     }
134     EC_GROUP_set_curve_name(grp, 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 ECDSA_SIG structure
152  *
153  */
154 ECDSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
155 {
156     ECDSA_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_secure_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 = BN_lebin2bn(dgst, dlen, NULL);
179     newsig = ECDSA_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(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     new_s = BN_dup(s);
280     new_r = BN_dup(r);
281     if (!new_s || !new_r) {
282         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
283         goto err;
284     }
285     ECDSA_SIG_set0(newsig, new_r, new_s);
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         ECDSA_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                    ECDSA_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, *z1 = NULL, *z2 = NULL;
312     const BIGNUM *sig_s = NULL, *sig_r = NULL;
313     BIGNUM *X = NULL, *tmp = NULL;
314     EC_POINT *C = NULL;
315     const EC_POINT *pub_key = NULL;
316     int ok = 0;
317
318     OPENSSL_assert(dgst != NULL && sig != NULL && group != NULL);
319
320     if (!(ctx = BN_CTX_new())) {
321         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
322         return 0;
323     }
324
325     BN_CTX_start(ctx);
326     order = BN_CTX_get(ctx);
327     e = BN_CTX_get(ctx);
328     z1 = BN_CTX_get(ctx);
329     z2 = BN_CTX_get(ctx);
330     tmp = BN_CTX_get(ctx);
331     X = BN_CTX_get(ctx);
332     R = BN_CTX_get(ctx);
333     v = BN_CTX_get(ctx);
334     if (!order || !e || !z1 || !z2 || !tmp || !X || !R || !v) {
335         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
336         goto err;
337     }
338
339     pub_key = EC_KEY_get0_public_key(ec);
340     if (!pub_key || !EC_GROUP_get_order(group, order, ctx)) {
341         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
342         goto err;
343     }
344
345     ECDSA_SIG_get0(sig, &sig_r, &sig_s);
346
347     if (BN_is_zero(sig_s) || BN_is_zero(sig_r) ||
348         (BN_cmp(sig_s, order) >= 1) || (BN_cmp(sig_r, order) >= 1)) {
349         GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
350         goto err;
351
352     }
353
354     OPENSSL_assert(dgst_len == 32 || dgst_len == 64);
355     md = BN_lebin2bn(dgst, dgst_len, NULL);
356     if (!md || !BN_mod(e, md, order, ctx)) {
357         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
358         goto err;
359     }
360 #ifdef DEBUG_SIGN
361     fprintf(stderr, "digest as bignum: ");
362     BN_print_fp(stderr, md);
363     fprintf(stderr, "\ndigest mod q: ");
364     BN_print_fp(stderr, e);
365 #endif
366     if (BN_is_zero(e) && !BN_one(e)) {
367         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
368         goto err;
369     }
370     v = BN_mod_inverse(v, e, order, ctx);
371     if (!v || !BN_mod_mul(z1, sig_s, v, order, ctx)
372         || !BN_sub(tmp, order, sig_r)
373         || !BN_mod_mul(z2, tmp, v, order, ctx)) {
374         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
375         goto err;
376     }
377 #ifdef DEBUG_SIGN
378     fprintf(stderr, "\nInverted digest value: ");
379     BN_print_fp(stderr, v);
380     fprintf(stderr, "\nz1: ");
381     BN_print_fp(stderr, z1);
382     fprintf(stderr, "\nz2: ");
383     BN_print_fp(stderr, z2);
384 #endif
385     C = EC_POINT_new(group);
386     if (!C) {
387         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
388         goto err;
389     }
390     if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) {
391         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
392         goto err;
393     }
394     if (!EC_POINT_get_affine_coordinates(group, C, X, NULL, ctx)) {
395         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
396         goto err;
397     }
398     if (!BN_mod(R, X, order, ctx)) {
399         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
400         goto err;
401     }
402 #ifdef DEBUG_SIGN
403     fprintf(stderr, "\nX=");
404     BN_print_fp(stderr, X);
405     fprintf(stderr, "\nX mod q=");
406     BN_print_fp(stderr, R);
407     fprintf(stderr, "\n");
408 #endif
409     if (BN_cmp(R, sig_r) != 0) {
410         GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_MISMATCH);
411     } else {
412         ok = 1;
413     }
414  err:
415     if (C)
416         EC_POINT_free(C);
417     BN_CTX_end(ctx);
418     BN_CTX_free(ctx);
419     if (md)
420         BN_free(md);
421     return ok;
422 }
423
424 /*
425  * Computes GOST R 34.10-2001 public key
426  * or GOST R 34.10-2012 public key
427  *
428  */
429 int gost_ec_compute_public(EC_KEY *ec)
430 {
431     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
432     EC_POINT *pub_key = NULL;
433     const BIGNUM *priv_key = NULL;
434     BN_CTX *ctx = NULL;
435     int ok = 0;
436
437     if (!group) {
438         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITIALIZED);
439         return 0;
440     }
441
442     ctx = BN_CTX_secure_new();
443     if (!ctx) {
444         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
445         return 0;
446     }
447
448     BN_CTX_start(ctx);
449     priv_key = EC_KEY_get0_private_key(ec);
450     if (!priv_key) {
451         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
452         goto err;
453     }
454
455     pub_key = EC_POINT_new(group);
456     if (!pub_key) {
457         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
458         goto err;
459     }
460
461     if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
462         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
463         goto err;
464     }
465     if (!EC_KEY_set_public_key(ec, pub_key)) {
466         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
467         goto err;
468     }
469     ok = 1;
470  err:
471     if (pub_key)
472         EC_POINT_free(pub_key);
473     BN_CTX_end(ctx);
474     BN_CTX_free(ctx);
475     return ok;
476 }
477
478 /*
479  *
480  * Generates GOST R 34.10-2001
481  * or GOST R 34.10-2012 keypair
482  *
483  */
484 int gost_ec_keygen(EC_KEY *ec)
485 {
486     BIGNUM *order = NULL, *d = NULL;
487     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
488     int ok = 0;
489
490     if (!group) {
491         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
492         return 0;
493     }
494
495     order = BN_new();
496     d = BN_secure_new();
497     if (!order || !d) {
498         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_MALLOC_FAILURE);
499         goto end;
500     }
501
502     if (!EC_GROUP_get_order(group, order, NULL)) {
503         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
504         goto end;
505     }
506
507     do {
508         if (!BN_rand_range(d, order)) {
509             GOSTerr(GOST_F_GOST_EC_KEYGEN, GOST_R_RNG_ERROR);
510             goto end;
511         }
512     }
513     while (BN_is_zero(d));
514
515     if (!EC_KEY_set_private_key(ec, d)) {
516         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
517         goto end;
518     }
519
520     ok = 1;
521  end:
522     if (d)
523         BN_free(d);
524     if (order)
525         BN_free(order);
526
527     return (ok) ? gost_ec_compute_public(ec) : 0;
528 }