1 /**********************************************************************
3 * Copyright (c) 2005-2013 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implementation of GOST R 34.10-2001 *
7 * Requires OpenSSL 1.0.0+ 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)
26 /* Convert little-endian byte array into bignum */
27 BIGNUM *hashsum2bn(const unsigned char *dgst, int len)
29 unsigned char buf[64];
32 if (len > sizeof(buf))
35 for (i = 0; i < len; i++) {
36 buf[len - i - 1] = dgst[i];
38 return BN_bin2bn(buf, len, NULL);
41 static R3410_ec_params *gost_nid2params(int nid)
43 R3410_ec_params *params;
45 /* Search nid in 2012 paramset */
46 params = R3410_2012_512_paramset;
47 while (params->nid != NID_undef) {
48 if (params->nid == nid)
53 /* Search nid in 2001 paramset */
54 params = R3410_2001_paramset;
55 while (params->nid != NID_undef) {
56 if (params->nid == nid)
65 * Fills EC_KEY structure hidden in the app_data field of DSA structure
66 * with parameter information, extracted from parameter array in
69 * Also fils DSA->q field with copy of EC_GROUP order field to make
70 * DSA_size function work
72 int fill_GOST_EC_params(EC_KEY *eckey, int nid)
74 R3410_ec_params *params = gost_nid2params(nid);
77 BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL, *cofactor = NULL;
81 if (!eckey || !params) {
82 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
86 if (!(ctx = BN_CTX_new())) {
87 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
98 cofactor = BN_CTX_get(ctx);
99 if (!p || !a || !b || !x || !y || !q || !cofactor) {
100 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
104 if (!BN_hex2bn(&p, params->p)
105 || !BN_hex2bn(&a, params->a)
106 || !BN_hex2bn(&b, params->b)
107 || !BN_hex2bn(&cofactor, params->cofactor) ) {
108 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
112 grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
114 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
118 P = EC_POINT_new(grp);
120 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
124 if (!BN_hex2bn(&x, params->x)
125 || !BN_hex2bn(&y, params->y)
126 || !EC_POINT_set_affine_coordinates_GFp(grp, P, x, y, ctx)
127 || !BN_hex2bn(&q, params->q)) {
128 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
132 if (!EC_GROUP_set_generator(grp, P, q, cofactor)) {
133 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
136 EC_GROUP_set_curve_name(grp, params->nid);
137 if (!EC_KEY_set_group(eckey, grp)) {
138 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
153 * Computes gost_ec signature as DSA_SIG structure
156 DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
158 DSA_SIG *newsig = NULL, *ret = NULL;
160 BIGNUM *order = NULL;
161 const EC_GROUP *group;
162 const BIGNUM *priv_key;
163 BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL,
164 *k = NULL, *e = NULL;
166 BIGNUM *new_r = NULL, *new_s = NULL;
171 OPENSSL_assert(dgst != NULL && eckey != NULL);
173 if (!(ctx = BN_CTX_new())) {
174 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
179 OPENSSL_assert(dlen == 32 || dlen == 64);
180 md = hashsum2bn(dgst, dlen);
181 newsig = DSA_SIG_new();
182 if (!newsig || !md) {
183 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
186 group = EC_KEY_get0_group(eckey);
188 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
191 order = BN_CTX_get(ctx);
192 if (!order || !EC_GROUP_get_order(group, order, ctx)) {
193 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
196 priv_key = EC_KEY_get0_private_key(eckey);
198 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
202 if (!e || !BN_mod(e, md, order, ctx)) {
203 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
207 fprintf(stderr, "digest as bignum=");
208 BN_print_fp(stderr, md);
209 fprintf(stderr, "\ndigest mod q=");
210 BN_print_fp(stderr, e);
211 fprintf(stderr, "\n");
217 C = EC_POINT_new(group);
219 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
225 if (!BN_rand_range(k, order)) {
226 GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_RNG_ERROR);
230 * To avoid timing information leaking the length of k,
231 * compute C*k using an equivalent scalar of fixed bit-length */
232 if (!BN_add(k, k, order)
233 || (BN_num_bits(k) <= BN_num_bits(order)
234 && !BN_add(k, k, order))) {
237 if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
238 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
246 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
249 if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
250 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
254 if (!BN_nnmod(r, X, order, ctx)) {
255 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
259 while (BN_is_zero(r));
260 /* s = (r*priv_key+k*e) mod order */
262 tmp = BN_CTX_get(ctx);
264 tmp2 = BN_CTX_get(ctx);
267 if (!tmp || !tmp2 || !s) {
268 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
272 if (!BN_mod_mul(tmp, priv_key, r, order, ctx)
273 || !BN_mod_mul(tmp2, k, e, order, ctx)
274 || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
275 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
279 while (BN_is_zero(s));
283 if (!new_s || !new_r) {
284 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
287 DSA_SIG_set0(newsig, new_r, new_s);
297 if (!ret && newsig) {
298 DSA_SIG_free(newsig);
304 * Verifies gost ec signature
307 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
308 DSA_SIG *sig, EC_KEY *ec)
311 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
313 BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL,
314 *z1 = NULL, *z2 = NULL;
315 const BIGNUM *sig_s = NULL, *sig_r = NULL;
316 BIGNUM *X = NULL, *tmp = NULL;
318 const EC_POINT *pub_key = NULL;
321 OPENSSL_assert(dgst != NULL && sig != NULL && group != NULL);
323 if (!(ctx = BN_CTX_new())) {
324 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
329 order = BN_CTX_get(ctx);
331 z1 = BN_CTX_get(ctx);
332 z2 = BN_CTX_get(ctx);
333 tmp = BN_CTX_get(ctx);
337 if (!order || !e || !z1 || !z2 || !tmp || !X || !R || !v) {
338 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
342 pub_key = EC_KEY_get0_public_key(ec);
343 if (!pub_key || !EC_GROUP_get_order(group, order, ctx)) {
344 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
348 DSA_SIG_get0(sig, &sig_r, &sig_s);
350 if (BN_is_zero(sig_s) || BN_is_zero(sig_r) ||
351 (BN_cmp(sig_s, order) >= 1) || (BN_cmp(sig_r, order) >= 1)) {
352 GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
357 OPENSSL_assert(dgst_len == 32 || dgst_len == 64);
358 md = hashsum2bn(dgst, dgst_len);
359 if (!md || !BN_mod(e, md, order, ctx)) {
360 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
364 fprintf(stderr, "digest as bignum: ");
365 BN_print_fp(stderr, md);
366 fprintf(stderr, "\ndigest mod q: ");
367 BN_print_fp(stderr, e);
369 if (BN_is_zero(e) && !BN_one(e)) {
370 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
373 v = BN_mod_inverse(v, e, order, ctx);
374 if (!v || !BN_mod_mul(z1, sig_s, v, order, ctx)
375 || !BN_sub(tmp, order, sig_r)
376 || !BN_mod_mul(z2, tmp, v, order, ctx)) {
377 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
381 fprintf(stderr, "\nInverted digest value: ");
382 BN_print_fp(stderr, v);
383 fprintf(stderr, "\nz1: ");
384 BN_print_fp(stderr, z1);
385 fprintf(stderr, "\nz2: ");
386 BN_print_fp(stderr, z2);
388 C = EC_POINT_new(group);
390 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
393 if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) {
394 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
397 if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
398 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
401 if (!BN_mod(R, X, order, ctx)) {
402 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
406 fprintf(stderr, "\nX=");
407 BN_print_fp(stderr, X);
408 fprintf(stderr, "\nX mod q=");
409 BN_print_fp(stderr, R);
410 fprintf(stderr, "\n");
412 if (BN_cmp(R, sig_r) != 0) {
413 GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_MISMATCH);
428 * Computes GOST R 34.10-2001 public key
429 * or GOST R 34.10-2012 public key
432 int gost_ec_compute_public(EC_KEY *ec)
434 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
435 EC_POINT *pub_key = NULL;
436 const BIGNUM *priv_key = NULL;
441 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITIALIZED);
447 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
452 priv_key = EC_KEY_get0_private_key(ec);
454 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
458 pub_key = EC_POINT_new(group);
460 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
464 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
465 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
468 if (!EC_KEY_set_public_key(ec, pub_key)) {
469 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
475 EC_POINT_free(pub_key);
483 * Generates GOST R 34.10-2001
484 * or GOST R 34.10-2012 keypair
487 int gost_ec_keygen(EC_KEY *ec)
489 BIGNUM *order = NULL, *d = NULL;
490 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
494 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
501 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_MALLOC_FAILURE);
505 if (!EC_GROUP_get_order(group, order, NULL)) {
506 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
511 if (!BN_rand_range(d, order)) {
512 GOSTerr(GOST_F_GOST_EC_KEYGEN, GOST_R_RNG_ERROR);
516 while (BN_is_zero(d));
518 if (!EC_KEY_set_private_key(ec, d)) {
519 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
530 return (ok) ? gost_ec_compute_public(ec) : 0;