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, ECDSA_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 /* Map tc26-2012 256-bit parameters to cp-2001 parameters */
47 case NID_id_tc26_gost_3410_2012_256_paramSetB:
48 nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
50 case NID_id_tc26_gost_3410_2012_256_paramSetC:
51 nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
53 case NID_id_tc26_gost_3410_2012_256_paramSetD:
54 nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
57 /* Search nid in 2012 paramset */
58 params = R3410_2012_512_paramset;
59 while (params->nid != NID_undef) {
60 if (params->nid == nid)
65 /* Search nid in 2001 paramset */
66 params = R3410_2001_paramset;
67 while (params->nid != NID_undef) {
68 if (params->nid == nid)
77 * Fills EC_KEY structure hidden in the app_data field of DSA structure
78 * with parameter information, extracted from parameter array in
81 * Also fils DSA->q field with copy of EC_GROUP order field to make
82 * DSA_size function work
84 int fill_GOST_EC_params(EC_KEY *eckey, int nid)
86 R3410_ec_params *params = gost_nid2params(nid);
89 BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y =
90 NULL, *cofactor = NULL;
94 if (!eckey || !params) {
95 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
99 if (!(ctx = BN_CTX_new())) {
100 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
111 cofactor = BN_CTX_get(ctx);
112 if (!p || !a || !b || !x || !y || !q || !cofactor) {
113 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
117 if (!BN_hex2bn(&p, params->p)
118 || !BN_hex2bn(&a, params->a)
119 || !BN_hex2bn(&b, params->b)
120 || !BN_hex2bn(&cofactor, params->cofactor)) {
121 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
125 grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
127 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
131 P = EC_POINT_new(grp);
133 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
137 if (!BN_hex2bn(&x, params->x)
138 || !BN_hex2bn(&y, params->y)
139 || !EC_POINT_set_affine_coordinates(grp, P, x, y, ctx)
140 || !BN_hex2bn(&q, params->q)) {
141 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
145 if (!EC_GROUP_set_generator(grp, P, q, cofactor)) {
146 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
149 EC_GROUP_set_curve_name(grp, nid);
150 if (!EC_KEY_set_group(eckey, grp)) {
151 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
166 * Computes gost_ec signature as ECDSA_SIG structure
169 ECDSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
171 ECDSA_SIG *newsig = NULL, *ret = NULL;
173 BIGNUM *order = NULL;
174 const EC_GROUP *group;
175 const BIGNUM *priv_key;
176 BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL,
177 *k = NULL, *e = NULL;
179 BIGNUM *new_r = NULL, *new_s = NULL;
184 OPENSSL_assert(dgst != NULL && eckey != NULL);
186 if (!(ctx = BN_CTX_new())) {
187 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
192 OPENSSL_assert(dlen == 32 || dlen == 64);
193 md = hashsum2bn(dgst, dlen);
194 newsig = ECDSA_SIG_new();
195 if (!newsig || !md) {
196 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
199 group = EC_KEY_get0_group(eckey);
201 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
204 order = BN_CTX_get(ctx);
205 if (!order || !EC_GROUP_get_order(group, order, ctx)) {
206 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
209 priv_key = EC_KEY_get0_private_key(eckey);
211 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
215 if (!e || !BN_mod(e, md, order, ctx)) {
216 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
220 fprintf(stderr, "digest as bignum=");
221 BN_print_fp(stderr, md);
222 fprintf(stderr, "\ndigest mod q=");
223 BN_print_fp(stderr, e);
224 fprintf(stderr, "\n");
230 C = EC_POINT_new(group);
232 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
238 if (!BN_rand_range(k, order)) {
239 GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_RNG_ERROR);
243 * To avoid timing information leaking the length of k,
244 * compute C*k using an equivalent scalar of fixed bit-length */
245 if (!BN_add(k, k, order)
246 || (BN_num_bits(k) <= BN_num_bits(order)
247 && !BN_add(k, k, order))) {
250 if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
251 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
259 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
262 if (!EC_POINT_get_affine_coordinates(group, C, X, NULL, ctx)) {
263 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
267 if (!BN_nnmod(r, X, order, ctx)) {
268 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
272 while (BN_is_zero(r));
273 /* s = (r*priv_key+k*e) mod order */
275 tmp = BN_CTX_get(ctx);
277 tmp2 = BN_CTX_get(ctx);
280 if (!tmp || !tmp2 || !s) {
281 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
285 if (!BN_mod_mul(tmp, priv_key, r, order, ctx)
286 || !BN_mod_mul(tmp2, k, e, order, ctx)
287 || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
288 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
292 while (BN_is_zero(s));
296 if (!new_s || !new_r) {
297 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
300 ECDSA_SIG_set0(newsig, new_r, new_s);
310 if (!ret && newsig) {
311 ECDSA_SIG_free(newsig);
317 * Verifies gost ec signature
320 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
321 ECDSA_SIG *sig, EC_KEY *ec)
324 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
326 BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
327 const BIGNUM *sig_s = NULL, *sig_r = NULL;
328 BIGNUM *X = NULL, *tmp = NULL;
330 const EC_POINT *pub_key = NULL;
333 OPENSSL_assert(dgst != NULL && sig != NULL && group != NULL);
335 if (!(ctx = BN_CTX_new())) {
336 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
341 order = BN_CTX_get(ctx);
343 z1 = BN_CTX_get(ctx);
344 z2 = BN_CTX_get(ctx);
345 tmp = BN_CTX_get(ctx);
349 if (!order || !e || !z1 || !z2 || !tmp || !X || !R || !v) {
350 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
354 pub_key = EC_KEY_get0_public_key(ec);
355 if (!pub_key || !EC_GROUP_get_order(group, order, ctx)) {
356 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
360 ECDSA_SIG_get0(sig, &sig_r, &sig_s);
362 if (BN_is_zero(sig_s) || BN_is_zero(sig_r) ||
363 (BN_cmp(sig_s, order) >= 1) || (BN_cmp(sig_r, order) >= 1)) {
364 GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
369 OPENSSL_assert(dgst_len == 32 || dgst_len == 64);
370 md = hashsum2bn(dgst, dgst_len);
371 if (!md || !BN_mod(e, md, order, ctx)) {
372 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
376 fprintf(stderr, "digest as bignum: ");
377 BN_print_fp(stderr, md);
378 fprintf(stderr, "\ndigest mod q: ");
379 BN_print_fp(stderr, e);
381 if (BN_is_zero(e) && !BN_one(e)) {
382 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
385 v = BN_mod_inverse(v, e, order, ctx);
386 if (!v || !BN_mod_mul(z1, sig_s, v, order, ctx)
387 || !BN_sub(tmp, order, sig_r)
388 || !BN_mod_mul(z2, tmp, v, order, ctx)) {
389 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
393 fprintf(stderr, "\nInverted digest value: ");
394 BN_print_fp(stderr, v);
395 fprintf(stderr, "\nz1: ");
396 BN_print_fp(stderr, z1);
397 fprintf(stderr, "\nz2: ");
398 BN_print_fp(stderr, z2);
400 C = EC_POINT_new(group);
402 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
405 if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) {
406 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
409 if (!EC_POINT_get_affine_coordinates(group, C, X, NULL, ctx)) {
410 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
413 if (!BN_mod(R, X, order, ctx)) {
414 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
418 fprintf(stderr, "\nX=");
419 BN_print_fp(stderr, X);
420 fprintf(stderr, "\nX mod q=");
421 BN_print_fp(stderr, R);
422 fprintf(stderr, "\n");
424 if (BN_cmp(R, sig_r) != 0) {
425 GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_MISMATCH);
440 * Computes GOST R 34.10-2001 public key
441 * or GOST R 34.10-2012 public key
444 int gost_ec_compute_public(EC_KEY *ec)
446 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
447 EC_POINT *pub_key = NULL;
448 const BIGNUM *priv_key = NULL;
453 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITIALIZED);
459 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
464 priv_key = EC_KEY_get0_private_key(ec);
466 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
470 pub_key = EC_POINT_new(group);
472 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
476 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
477 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
480 if (!EC_KEY_set_public_key(ec, pub_key)) {
481 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
487 EC_POINT_free(pub_key);
495 * Generates GOST R 34.10-2001
496 * or GOST R 34.10-2012 keypair
499 int gost_ec_keygen(EC_KEY *ec)
501 BIGNUM *order = NULL, *d = NULL;
502 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
506 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
513 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_MALLOC_FAILURE);
517 if (!EC_GROUP_get_order(group, order, NULL)) {
518 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
523 if (!BN_rand_range(d, order)) {
524 GOSTerr(GOST_F_GOST_EC_KEYGEN, GOST_R_RNG_ERROR);
528 while (BN_is_zero(d));
530 if (!EC_KEY_set_private_key(ec, d)) {
531 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
542 return (ok) ? gost_ec_compute_public(ec) : 0;