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