]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_sign.c
GOST ECC optimizations (#263)
[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             if (!gost_ec_point_mul(group, C, k, NULL, NULL, ctx)) {
228                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
229                 goto err;
230             }
231             if (!X)
232                 X = BN_CTX_get(ctx);
233             if (!r)
234                 r = BN_CTX_get(ctx);
235             if (!X || !r) {
236                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
237                 goto err;
238             }
239             if (!EC_POINT_get_affine_coordinates(group, C, X, NULL, ctx)) {
240                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
241                 goto err;
242             }
243
244             if (!BN_nnmod(r, X, order, ctx)) {
245                 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
246                 goto err;
247             }
248         }
249         while (BN_is_zero(r));
250         /* s =  (r*priv_key+k*e) mod order */
251         if (!tmp)
252             tmp = BN_CTX_get(ctx);
253         if (!tmp2)
254             tmp2 = BN_CTX_get(ctx);
255         if (!s)
256             s = BN_CTX_get(ctx);
257         if (!tmp || !tmp2 || !s) {
258             GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
259             goto err;
260         }
261
262         if (!BN_mod_mul(tmp, priv_key, r, order, ctx)
263             || !BN_mod_mul(tmp2, k, e, order, ctx)
264             || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
265             GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
266             goto err;
267         }
268     }
269     while (BN_is_zero(s));
270
271     new_s = BN_dup(s);
272     new_r = BN_dup(r);
273     if (!new_s || !new_r) {
274         GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
275         goto err;
276     }
277     ECDSA_SIG_set0(newsig, new_r, new_s);
278
279     ret = newsig;
280  err:
281     BN_CTX_end(ctx);
282     BN_CTX_free(ctx);
283     if (C)
284         EC_POINT_free(C);
285     if (md)
286         BN_free(md);
287     if (!ret && newsig) {
288         ECDSA_SIG_free(newsig);
289     }
290     return ret;
291 }
292
293 /*
294  * Verifies gost ec signature
295  *
296  */
297 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
298                    ECDSA_SIG *sig, EC_KEY *ec)
299 {
300     BN_CTX *ctx;
301     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
302     BIGNUM *order;
303     BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
304     const BIGNUM *sig_s = NULL, *sig_r = NULL;
305     BIGNUM *X = NULL, *tmp = NULL;
306     EC_POINT *C = NULL;
307     const EC_POINT *pub_key = NULL;
308     int ok = 0;
309
310     OPENSSL_assert(dgst != NULL && sig != NULL && group != NULL);
311
312     if (!(ctx = BN_CTX_new())) {
313         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
314         return 0;
315     }
316
317     BN_CTX_start(ctx);
318     order = BN_CTX_get(ctx);
319     e = BN_CTX_get(ctx);
320     z1 = BN_CTX_get(ctx);
321     z2 = BN_CTX_get(ctx);
322     tmp = BN_CTX_get(ctx);
323     X = BN_CTX_get(ctx);
324     R = BN_CTX_get(ctx);
325     v = BN_CTX_get(ctx);
326     if (!order || !e || !z1 || !z2 || !tmp || !X || !R || !v) {
327         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
328         goto err;
329     }
330
331     pub_key = EC_KEY_get0_public_key(ec);
332     if (!pub_key || !EC_GROUP_get_order(group, order, ctx)) {
333         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
334         goto err;
335     }
336
337     ECDSA_SIG_get0(sig, &sig_r, &sig_s);
338
339     if (BN_is_zero(sig_s) || BN_is_zero(sig_r) ||
340         (BN_cmp(sig_s, order) >= 1) || (BN_cmp(sig_r, order) >= 1)) {
341         GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
342         goto err;
343
344     }
345
346     OPENSSL_assert(dgst_len == 32 || dgst_len == 64);
347     md = BN_lebin2bn(dgst, dgst_len, NULL);
348     if (!md || !BN_mod(e, md, order, ctx)) {
349         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
350         goto err;
351     }
352 #ifdef DEBUG_SIGN
353     fprintf(stderr, "digest as bignum: ");
354     BN_print_fp(stderr, md);
355     fprintf(stderr, "\ndigest mod q: ");
356     BN_print_fp(stderr, e);
357 #endif
358     if (BN_is_zero(e) && !BN_one(e)) {
359         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
360         goto err;
361     }
362     v = BN_mod_inverse(v, e, order, ctx);
363     if (!v || !BN_mod_mul(z1, sig_s, v, order, ctx)
364         || !BN_sub(tmp, order, sig_r)
365         || !BN_mod_mul(z2, tmp, v, order, ctx)) {
366         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
367         goto err;
368     }
369 #ifdef DEBUG_SIGN
370     fprintf(stderr, "\nInverted digest value: ");
371     BN_print_fp(stderr, v);
372     fprintf(stderr, "\nz1: ");
373     BN_print_fp(stderr, z1);
374     fprintf(stderr, "\nz2: ");
375     BN_print_fp(stderr, z2);
376 #endif
377     C = EC_POINT_new(group);
378     if (!C) {
379         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
380         goto err;
381     }
382     if (!gost_ec_point_mul(group, C, z1, pub_key, z2, ctx)) {
383         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
384         goto err;
385     }
386     if (!EC_POINT_get_affine_coordinates(group, C, X, NULL, ctx)) {
387         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
388         goto err;
389     }
390     if (!BN_mod(R, X, order, ctx)) {
391         GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
392         goto err;
393     }
394 #ifdef DEBUG_SIGN
395     fprintf(stderr, "\nX=");
396     BN_print_fp(stderr, X);
397     fprintf(stderr, "\nX mod q=");
398     BN_print_fp(stderr, R);
399     fprintf(stderr, "\n");
400 #endif
401     if (BN_cmp(R, sig_r) != 0) {
402         GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_MISMATCH);
403     } else {
404         ok = 1;
405     }
406  err:
407     if (C)
408         EC_POINT_free(C);
409     BN_CTX_end(ctx);
410     BN_CTX_free(ctx);
411     if (md)
412         BN_free(md);
413     return ok;
414 }
415
416 /*
417  * Computes GOST R 34.10-2001 public key
418  * or GOST R 34.10-2012 public key
419  *
420  */
421 int gost_ec_compute_public(EC_KEY *ec)
422 {
423     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
424     EC_POINT *pub_key = NULL;
425     const BIGNUM *priv_key = NULL;
426     BN_CTX *ctx = NULL;
427     int ok = 0;
428
429     if (!group) {
430         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITIALIZED);
431         return 0;
432     }
433
434     ctx = BN_CTX_secure_new();
435     if (!ctx) {
436         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
437         return 0;
438     }
439
440     BN_CTX_start(ctx);
441     priv_key = EC_KEY_get0_private_key(ec);
442     if (!priv_key) {
443         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
444         goto err;
445     }
446
447     pub_key = EC_POINT_new(group);
448     if (!pub_key) {
449         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
450         goto err;
451     }
452
453     if (!gost_ec_point_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
454         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
455         goto err;
456     }
457     if (!EC_KEY_set_public_key(ec, pub_key)) {
458         GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
459         goto err;
460     }
461     ok = 1;
462  err:
463     if (pub_key)
464         EC_POINT_free(pub_key);
465     BN_CTX_end(ctx);
466     BN_CTX_free(ctx);
467     return ok;
468 }
469
470 int gost_ec_point_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
471                       const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx)
472 {
473     if (group == NULL || r == NULL || ctx == NULL)
474         return 0;
475
476     if (m != NULL && n != NULL) {
477         /* verification */
478         if (q == NULL)
479             return 0;
480         switch(EC_GROUP_get_curve_name(group)) {
481             case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
482             case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
483             case NID_id_tc26_gost_3410_2012_256_paramSetB:
484                 return point_mul_two_id_GostR3410_2001_CryptoPro_A_ParamSet(group, r, n, q, m, ctx);
485             case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
486             case NID_id_tc26_gost_3410_2012_256_paramSetC:
487                 return point_mul_two_id_GostR3410_2001_CryptoPro_B_ParamSet(group, r, n, q, m, ctx);
488             case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
489             case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
490             case NID_id_tc26_gost_3410_2012_256_paramSetD:
491                 return point_mul_two_id_GostR3410_2001_CryptoPro_C_ParamSet(group, r, n, q, m, ctx);
492             case NID_id_GostR3410_2001_TestParamSet:
493                 return point_mul_two_id_GostR3410_2001_TestParamSet(group, r, n, q, m, ctx);
494             case NID_id_tc26_gost_3410_2012_256_paramSetA:
495                 return point_mul_two_id_tc26_gost_3410_2012_256_paramSetA(group, r, n, q, m, ctx);
496             case NID_id_tc26_gost_3410_2012_512_paramSetA:
497                 return point_mul_two_id_tc26_gost_3410_2012_512_paramSetA(group, r, n, q, m, ctx);
498             case NID_id_tc26_gost_3410_2012_512_paramSetB:
499                 return point_mul_two_id_tc26_gost_3410_2012_512_paramSetB(group, r, n, q, m, ctx);
500             case NID_id_tc26_gost_3410_2012_512_paramSetC:
501                 return point_mul_two_id_tc26_gost_3410_2012_512_paramSetC(group, r, n, q, m, ctx);
502             default:
503                 return EC_POINT_mul(group, r, n, q, m, ctx);
504         }
505     } else if (n != NULL) {
506         /* mul g */
507         switch(EC_GROUP_get_curve_name(group)) {
508             case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
509             case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
510             case NID_id_tc26_gost_3410_2012_256_paramSetB:
511                 return point_mul_g_id_GostR3410_2001_CryptoPro_A_ParamSet(group, r, n, ctx);
512             case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
513             case NID_id_tc26_gost_3410_2012_256_paramSetC:
514                 return point_mul_g_id_GostR3410_2001_CryptoPro_B_ParamSet(group, r, n, ctx);
515             case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
516             case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
517             case NID_id_tc26_gost_3410_2012_256_paramSetD:
518                 return point_mul_g_id_GostR3410_2001_CryptoPro_C_ParamSet(group, r, n, ctx);
519             case NID_id_GostR3410_2001_TestParamSet:
520                 return point_mul_g_id_GostR3410_2001_TestParamSet(group, r, n, ctx);
521             case NID_id_tc26_gost_3410_2012_256_paramSetA:
522                 return point_mul_g_id_tc26_gost_3410_2012_256_paramSetA(group, r, n, ctx);
523             case NID_id_tc26_gost_3410_2012_512_paramSetA:
524                 return point_mul_g_id_tc26_gost_3410_2012_512_paramSetA(group, r, n, ctx);
525             case NID_id_tc26_gost_3410_2012_512_paramSetB:
526                 return point_mul_g_id_tc26_gost_3410_2012_512_paramSetB(group, r, n, ctx);
527             case NID_id_tc26_gost_3410_2012_512_paramSetC:
528                 return point_mul_g_id_tc26_gost_3410_2012_512_paramSetC(group, r, n, ctx);
529             default:
530                 return EC_POINT_mul(group, r, n, q, m, ctx);
531         }
532     } else if (m != NULL) {
533         if (q == NULL)
534             return 0;
535         /* mul */
536         switch(EC_GROUP_get_curve_name(group)) {
537             case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
538             case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
539             case NID_id_tc26_gost_3410_2012_256_paramSetB:
540                 return point_mul_id_GostR3410_2001_CryptoPro_A_ParamSet(group, r, q, m, ctx);
541             case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
542             case NID_id_tc26_gost_3410_2012_256_paramSetC:
543                 return point_mul_id_GostR3410_2001_CryptoPro_B_ParamSet(group, r, q, m, ctx);
544             case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
545             case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
546             case NID_id_tc26_gost_3410_2012_256_paramSetD:
547                 return point_mul_id_GostR3410_2001_CryptoPro_C_ParamSet(group, r, q, m, ctx);
548             case NID_id_GostR3410_2001_TestParamSet:
549                 return point_mul_id_GostR3410_2001_TestParamSet(group, r, q, m, ctx);
550             case NID_id_tc26_gost_3410_2012_256_paramSetA:
551                 return point_mul_id_tc26_gost_3410_2012_256_paramSetA(group, r, q, m, ctx);
552             case NID_id_tc26_gost_3410_2012_512_paramSetA:
553                 return point_mul_id_tc26_gost_3410_2012_512_paramSetA(group, r, q, m, ctx);
554             case NID_id_tc26_gost_3410_2012_512_paramSetB:
555                 return point_mul_id_tc26_gost_3410_2012_512_paramSetB(group, r, q, m, ctx);
556             case NID_id_tc26_gost_3410_2012_512_paramSetC:
557                 return point_mul_id_tc26_gost_3410_2012_512_paramSetC(group, r, q, m, ctx);
558             default:
559                 return EC_POINT_mul(group, r, n, q, m, ctx);
560         }
561     }
562     return 0;
563 }
564
565 /*
566  *
567  * Generates GOST R 34.10-2001
568  * or GOST R 34.10-2012 keypair
569  *
570  */
571 int gost_ec_keygen(EC_KEY *ec)
572 {
573     BIGNUM *order = NULL, *d = NULL;
574     const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
575     int ok = 0;
576
577     if (!group) {
578         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
579         return 0;
580     }
581
582     order = BN_new();
583     d = BN_secure_new();
584     if (!order || !d) {
585         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_MALLOC_FAILURE);
586         goto end;
587     }
588
589     if (!EC_GROUP_get_order(group, order, NULL)) {
590         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
591         goto end;
592     }
593
594     do {
595         if (!BN_rand_range(d, order)) {
596             GOSTerr(GOST_F_GOST_EC_KEYGEN, GOST_R_RNG_ERROR);
597             goto end;
598         }
599     }
600     while (BN_is_zero(d));
601
602     if (!EC_KEY_set_private_key(ec, d)) {
603         GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
604         goto end;
605     }
606
607     ok = 1;
608  end:
609     if (d)
610         BN_free(d);
611     if (order)
612         BN_free(order);
613
614     return (ok) ? gost_ec_compute_public(ec) : 0;
615 }