1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implementation of GOST R 34.10-2001 *
7 * Requires OpenSSL 0.9.9 for compilation *
8 **********************************************************************/
11 #include <openssl/rand.h>
12 #include <openssl/ecdsa.h>
13 #include <openssl/err.h>
14 #include "e_gost_err.h"
17 void dump_signature(const char *message, const unsigned char *buffer,
19 void dump_dsa_sig(const char *message, DSA_SIG *sig);
22 # define dump_signature(a,b,c)
23 # define dump_dsa_sig(a,b)
27 * Fills EC_KEY structure hidden in the app_data field of DSA structure
28 * with parameter information, extracted from parameter array in
31 * Also fils DSA->q field with copy of EC_GROUP order field to make
32 * DSA_size function work
34 int fill_GOST2001_params(EC_KEY *eckey, int nid)
36 R3410_2001_params *params = R3410_2001_paramset;
38 BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
40 BN_CTX *ctx = BN_CTX_new();
44 GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
55 if (!p || !a || !b || !x || !y || !q) {
56 GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
59 while (params->nid != NID_undef && params->nid != nid)
61 if (params->nid == NID_undef) {
62 GOSTerr(GOST_F_FILL_GOST2001_PARAMS,
63 GOST_R_UNSUPPORTED_PARAMETER_SET);
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);
74 grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
76 GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
80 P = EC_POINT_new(grp);
82 GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
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);
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");
100 if (!EC_GROUP_set_generator(grp, P, q, NULL)) {
101 GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_INTERNAL_ERROR);
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);
120 * Computes gost2001 signature as DSA_SIG structure
124 DSA_SIG *gost2001_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
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 =
134 BN_CTX *ctx = BN_CTX_new();
136 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
140 OPENSSL_assert(dlen == 32);
141 newsig = DSA_SIG_new();
143 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
146 group = EC_KEY_get0_group(eckey);
148 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
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);
156 priv_key = EC_KEY_get0_private_key(eckey);
158 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
162 if (!e || !BN_mod(e, md, order, ctx)) {
163 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
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");
177 C = EC_POINT_new(group);
179 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
184 if (!BN_rand_range(k, order)) {
185 GOSTerr(GOST_F_GOST2001_DO_SIGN,
186 GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
189 if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
190 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_EC_LIB);
198 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
201 if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
202 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_EC_LIB);
206 if (!BN_nnmod(r, X, order, ctx)) {
207 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
211 while (BN_is_zero(r));
212 /* s = (r*priv_key+k*e) mod order */
214 tmp = BN_CTX_get(ctx);
216 tmp2 = BN_CTX_get(ctx);
219 if (!tmp || !tmp2 || !s) {
220 GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
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);
231 while (BN_is_zero(s));
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);
248 DSA_SIG_free(newsig);
253 * Verifies gost 2001 signature
256 int gost2001_do_verify(const unsigned char *dgst, int dgst_len,
257 DSA_SIG *sig, EC_KEY *ec)
259 BN_CTX *ctx = BN_CTX_new();
260 const EC_GROUP *group = EC_KEY_get0_group(ec);
262 BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 =
264 BIGNUM *X = NULL, *tmp = NULL;
266 const EC_POINT *pub_key = NULL;
269 if (!ctx || !group) {
270 GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
275 order = BN_CTX_get(ctx);
277 z1 = BN_CTX_get(ctx);
278 z2 = BN_CTX_get(ctx);
279 tmp = 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);
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);
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);
301 md = hashsum2bn(dgst);
303 if (!md || !BN_mod(e, md, order, ctx)) {
304 GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
308 fprintf(stderr, "digest as bignum: ");
309 BN_print_fp(stderr, md);
310 fprintf(stderr, "\ndigest mod q: ");
311 BN_print_fp(stderr, e);
313 if (BN_is_zero(e) && !BN_one(e)) {
314 GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
317 v = BN_mod_inverse(v, e, order, ctx);
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);
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);
333 C = EC_POINT_new(group);
335 GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_MALLOC_FAILURE);
338 if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) {
339 GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_EC_LIB);
342 if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
343 GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_EC_LIB);
346 if (!BN_mod(R, X, order, ctx)) {
347 GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
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");
357 if (BN_cmp(R, sig->r) != 0) {
358 GOSTerr(GOST_F_GOST2001_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
372 * Computes GOST R 34.10-2001 public key
376 int gost2001_compute_public(EC_KEY *ec)
378 const EC_GROUP *group = EC_KEY_get0_group(ec);
379 EC_POINT *pub_key = NULL;
380 const BIGNUM *priv_key = NULL;
385 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,
386 GOST_R_KEY_IS_NOT_INITIALIZED);
391 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
395 if ((priv_key = EC_KEY_get0_private_key(ec)) == NULL) {
396 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
400 pub_key = EC_POINT_new(group);
402 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
405 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
406 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
409 if (!EC_KEY_set_public_key(ec, pub_key)) {
410 GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
415 EC_POINT_free(pub_key);
424 * Generates GOST R 34.10-2001 keypair
428 int gost2001_keygen(EC_KEY *ec)
430 BIGNUM *order = BN_new(), *d = BN_new();
431 const EC_GROUP *group = EC_KEY_get0_group(ec);
433 if (!group || !EC_GROUP_get_order(group, order, NULL)) {
434 GOSTerr(GOST_F_GOST2001_KEYGEN, ERR_R_INTERNAL_ERROR);
441 if (!BN_rand_range(d, order)) {
442 GOSTerr(GOST_F_GOST2001_KEYGEN,
443 GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
449 while (BN_is_zero(d));
451 if (!EC_KEY_set_private_key(ec, d)) {
452 GOSTerr(GOST_F_GOST2001_KEYGEN, ERR_R_INTERNAL_ERROR);
459 return gost2001_compute_public(ec);