Remove Gost94 signature algorithm.
[openssl.git] / engines / ccgost / gost2001.c
1 /**********************************************************************
2  *                          gost2001.c                                *
3  *             Copyright (c) 2005-2006 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 0.9.9 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 /*
27  * Fills EC_KEY structure hidden in the app_data field of DSA structure
28  * with parameter information, extracted from parameter array in
29  * params.c file.
30  *
31  * Also fils DSA->q field with copy of EC_GROUP order field to make
32  * DSA_size function work
33  */
34 int fill_GOST2001_params(EC_KEY *eckey, int nid)
35 {
36     R3410_2001_params *params = R3410_2001_paramset;
37     EC_GROUP *grp = NULL;
38     BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
39     EC_POINT *P = NULL;
40     BN_CTX *ctx = BN_CTX_new();
41     int ok = 0;
42
43     if (!ctx) {
44         GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
45         goto err;
46     }
47
48     BN_CTX_start(ctx);
49     p = BN_CTX_get(ctx);
50     a = BN_CTX_get(ctx);
51     b = BN_CTX_get(ctx);
52     x = BN_CTX_get(ctx);
53     y = BN_CTX_get(ctx);
54     q = BN_CTX_get(ctx);
55     if (!p || !a || !b || !x || !y || !q) {
56         GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
57         goto err;
58     }
59     while (params->nid != NID_undef && params->nid != nid)
60         params++;
61     if (params->nid == NID_undef) {
62         GOSTerr(GOST_F_FILL_GOST2001_PARAMS,
63                 GOST_R_UNSUPPORTED_PARAMETER_SET);
64         goto err;
65     }
66     if (!BN_hex2bn(&p, params->p)
67         || !BN_hex2bn(&a, params->a)
68         || !BN_hex2bn(&b, params->b)) {
69         GOSTerr(GOST_F_FILL_GOST2001_PARAMS,
70                 ERR_R_INTERNAL_ERROR);
71         goto err;
72     }
73
74     grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
75     if (!grp)  {
76         GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
77         goto err;
78     }
79
80     P = EC_POINT_new(grp);
81     if (!P)  {
82         GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
83         goto err;
84     }
85
86     if (!BN_hex2bn(&x, params->x)
87         || !BN_hex2bn(&y, params->y)
88         || !EC_POINT_set_affine_coordinates_GFp(grp, P, x, y, ctx)
89         || !BN_hex2bn(&q, params->q))  {
90         GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_INTERNAL_ERROR);
91         goto err;
92     }
93 #ifdef DEBUG_KEYS
94     fprintf(stderr, "Set params index %d oid %s\nq=",
95             (params - R3410_2001_paramset), OBJ_nid2sn(params->nid));
96     BN_print_fp(stderr, q);
97     fprintf(stderr, "\n");
98 #endif
99
100     if (!EC_GROUP_set_generator(grp, P, q, NULL)) {
101         GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_INTERNAL_ERROR);
102         goto err;
103     }
104     EC_GROUP_set_curve_name(grp, params->nid);
105     if (!EC_KEY_set_group(eckey, grp)) {
106         GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_INTERNAL_ERROR);
107         goto err;
108     }
109     ok = 1;
110  err:
111     EC_POINT_free(P);
112     EC_GROUP_free(grp);
113     if (ctx)
114         BN_CTX_end(ctx);
115     BN_CTX_free(ctx);
116     return ok;
117 }
118
119 /*
120  * Computes gost2001 signature as DSA_SIG structure
121  *
122  *
123  */
124 DSA_SIG *gost2001_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
125 {
126     DSA_SIG *newsig = NULL, *ret = NULL;
127     BIGNUM *md = hashsum2bn(dgst);
128     BIGNUM *order = NULL;
129     const EC_GROUP *group;
130     const BIGNUM *priv_key;
131     BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL, *k =
132         NULL, *e = NULL;
133     EC_POINT *C = NULL;
134     BN_CTX *ctx = BN_CTX_new();
135     if (!ctx || !md) {
136         GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
137         goto err;
138     }
139     BN_CTX_start(ctx);
140     OPENSSL_assert(dlen == 32);
141     newsig = DSA_SIG_new();
142     if (!newsig) {
143         GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
144         goto err;
145     }
146     group = EC_KEY_get0_group(eckey);
147     if (!group) {
148         GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
149         goto err;
150     }
151     order = BN_CTX_get(ctx);
152     if (!order || !EC_GROUP_get_order(group, order, ctx)) {
153         GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
154         goto err;
155     }
156     priv_key = EC_KEY_get0_private_key(eckey);
157     if (!priv_key) {
158         GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
159         goto err;
160     }
161     e = BN_CTX_get(ctx);
162     if (!e || !BN_mod(e, md, order, ctx)) {
163         GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
164         goto err;
165     }
166 #ifdef DEBUG_SIGN
167     fprintf(stderr, "digest as bignum=");
168     BN_print_fp(stderr, md);
169     fprintf(stderr, "\ndigest mod q=");
170     BN_print_fp(stderr, e);
171     fprintf(stderr, "\n");
172 #endif
173     if (BN_is_zero(e)) {
174         BN_one(e);
175     }
176     k = BN_CTX_get(ctx);
177     C = EC_POINT_new(group);
178     if (!k || !C) {
179         GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
180         goto err;
181     }
182     do {
183         do {
184             if (!BN_rand_range(k, order)) {
185                 GOSTerr(GOST_F_GOST2001_DO_SIGN,
186                         GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
187                 goto err;
188             }
189             if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
190                 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_EC_LIB);
191                 goto err;
192             }
193             if (!X)
194                 X = BN_CTX_get(ctx);
195             if (!r)
196                 r = BN_CTX_get(ctx);
197             if (!X || !r) {
198                 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
199                 goto err;
200             }
201             if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
202                 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_EC_LIB);
203                 goto err;
204             }
205
206             if (!BN_nnmod(r, X, order, ctx)) {
207                 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
208                 goto err;
209             }
210         }
211         while (BN_is_zero(r));
212         /* s =  (r*priv_key+k*e) mod order */
213         if (!tmp)
214             tmp = BN_CTX_get(ctx);
215         if (!tmp2)
216             tmp2 = BN_CTX_get(ctx);
217         if (!s)
218             s = BN_CTX_get(ctx);
219         if (!tmp || !tmp2 || !s) {
220             GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
221             goto err;
222         }
223
224         if (!BN_mod_mul(tmp, priv_key, r, order, ctx)
225             || !BN_mod_mul(tmp2, k, e, order, ctx)
226             || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
227             GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
228             goto err;
229         }
230     }
231     while (BN_is_zero(s));
232
233     newsig->s = BN_dup(s);
234     newsig->r = BN_dup(r);
235     if (!newsig->s || !newsig->r) {
236         GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
237         goto err;
238     }
239
240     ret = newsig;
241  err:
242     if (ctx)
243         BN_CTX_end(ctx);
244     BN_CTX_free(ctx);
245     EC_POINT_free(C);
246     BN_free(md);
247     if (!ret)
248         DSA_SIG_free(newsig);
249     return ret;
250 }
251
252 /*
253  * Verifies gost 2001 signature
254  *
255  */
256 int gost2001_do_verify(const unsigned char *dgst, int dgst_len,
257                        DSA_SIG *sig, EC_KEY *ec)
258 {
259     BN_CTX *ctx = BN_CTX_new();
260     const EC_GROUP *group = EC_KEY_get0_group(ec);
261     BIGNUM *order;
262     BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 =
263         NULL;
264     BIGNUM *X = NULL, *tmp = NULL;
265     EC_POINT *C = NULL;
266     const EC_POINT *pub_key = NULL;
267     int ok = 0;
268
269     if (!ctx || !group) {
270         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
271         goto err;
272     }
273
274     BN_CTX_start(ctx);
275     order = BN_CTX_get(ctx);
276     e = BN_CTX_get(ctx);
277     z1 = BN_CTX_get(ctx);
278     z2 = BN_CTX_get(ctx);
279     tmp = BN_CTX_get(ctx);
280     X = BN_CTX_get(ctx);
281     R = BN_CTX_get(ctx);
282     v = BN_CTX_get(ctx);
283     if (!order || !e || !z1 || !z2 || !tmp || !X || !R || !v) {
284         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_MALLOC_FAILURE);
285         goto err;
286     }
287
288     pub_key = EC_KEY_get0_public_key(ec);
289     if (!pub_key || !EC_GROUP_get_order(group, order, ctx)) {
290         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
291         goto err;
292     }
293
294     if (BN_is_zero(sig->s) || BN_is_zero(sig->r) ||
295         (BN_cmp(sig->s, order) >= 1) || (BN_cmp(sig->r, order) >= 1)) {
296         GOSTerr(GOST_F_GOST2001_DO_VERIFY,
297                 GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
298         goto err;
299
300     }
301     md = hashsum2bn(dgst);
302
303     if (!md || !BN_mod(e, md, order, ctx)) {
304         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
305         goto err;
306     }
307 #ifdef DEBUG_SIGN
308     fprintf(stderr, "digest as bignum: ");
309     BN_print_fp(stderr, md);
310     fprintf(stderr, "\ndigest mod q: ");
311     BN_print_fp(stderr, e);
312 #endif
313     if (BN_is_zero(e) && !BN_one(e)) {
314         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
315         goto err;
316     }
317     v = BN_mod_inverse(v, e, order, ctx);
318     if (!v
319         || !BN_mod_mul(z1, sig->s, v, order, ctx)
320         || !BN_sub(tmp, order, sig->r)
321         || !BN_mod_mul(z2, tmp, v, order, ctx)) {
322         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
323         goto err;
324     }
325 #ifdef DEBUG_SIGN
326     fprintf(stderr, "\nInverted digest value: ");
327     BN_print_fp(stderr, v);
328     fprintf(stderr, "\nz1: ");
329     BN_print_fp(stderr, z1);
330     fprintf(stderr, "\nz2: ");
331     BN_print_fp(stderr, z2);
332 #endif
333     C = EC_POINT_new(group);
334     if (!C) {
335         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_MALLOC_FAILURE);
336         goto err;
337     }
338     if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) {
339         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_EC_LIB);
340         goto err;
341     }
342     if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
343         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_EC_LIB);
344         goto err;
345     }
346     if (!BN_mod(R, X, order, ctx)) {
347         GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
348         goto err;
349     }
350 #ifdef DEBUG_SIGN
351     fprintf(stderr, "\nX=");
352     BN_print_fp(stderr, X);
353     fprintf(stderr, "\nX mod q=");
354     BN_print_fp(stderr, R);
355     fprintf(stderr, "\n");
356 #endif
357     if (BN_cmp(R, sig->r) != 0) {
358         GOSTerr(GOST_F_GOST2001_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
359     } else {
360         ok = 1;
361     }
362  err:
363     EC_POINT_free(C);
364     if (ctx)
365         BN_CTX_end(ctx);
366     BN_CTX_free(ctx);
367     BN_free(md);
368     return ok;
369 }
370
371 /*
372  * Computes GOST R 34.10-2001 public key
373  *
374  *
375  */
376 int gost2001_compute_public(EC_KEY *ec)
377 {
378     const EC_GROUP *group = EC_KEY_get0_group(ec);
379     EC_POINT *pub_key = NULL;
380     const BIGNUM *priv_key = NULL;
381     BN_CTX *ctx = NULL;
382     int ok = 0;
383
384     if (!group) {
385         GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,
386                 GOST_R_KEY_IS_NOT_INITIALIZED);
387         return 0;
388     }
389     ctx = BN_CTX_new();
390     if (!ctx) {
391         GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
392         goto err;
393     }
394     BN_CTX_start(ctx);
395     if ((priv_key = EC_KEY_get0_private_key(ec)) == NULL) {
396         GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
397         goto err;
398     }
399
400     pub_key = EC_POINT_new(group);
401     if (!pub_key) {
402         GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
403         goto err;
404     }
405     if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
406         GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
407         goto err;
408     }
409     if (!EC_KEY_set_public_key(ec, pub_key)) {
410         GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
411         goto err;
412     }
413     ok = 256;
414  err:
415     EC_POINT_free(pub_key);
416     if (ctx)
417         BN_CTX_end(ctx);
418     BN_CTX_free(ctx);
419     return ok;
420 }
421
422 /*
423  *
424  * Generates GOST R 34.10-2001 keypair
425  *
426  *
427  */
428 int gost2001_keygen(EC_KEY *ec)
429 {
430     BIGNUM *order = BN_new(), *d = BN_new();
431     const EC_GROUP *group = EC_KEY_get0_group(ec);
432
433     if (!group || !EC_GROUP_get_order(group, order, NULL)) {
434         GOSTerr(GOST_F_GOST2001_KEYGEN, ERR_R_INTERNAL_ERROR);
435         BN_free(d);
436         BN_free(order);
437         return 0;
438     }
439
440     do {
441         if (!BN_rand_range(d, order)) {
442             GOSTerr(GOST_F_GOST2001_KEYGEN,
443                     GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
444             BN_free(d);
445             BN_free(order);
446             return 0;
447         }
448     }
449     while (BN_is_zero(d));
450
451     if (!EC_KEY_set_private_key(ec, d)) {
452         GOSTerr(GOST_F_GOST2001_KEYGEN, ERR_R_INTERNAL_ERROR);
453         BN_free(d);
454         BN_free(order);
455         return 0;
456     }
457     BN_free(d);
458     BN_free(order);
459     return gost2001_compute_public(ec);
460 }