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 /* 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 =
78 NULL, *cofactor = NULL;
82 if (!eckey || !params) {
83 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
87 if (!(ctx = BN_CTX_new())) {
88 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
99 cofactor = BN_CTX_get(ctx);
100 if (!p || !a || !b || !x || !y || !q || !cofactor) {
101 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
105 if (!BN_hex2bn(&p, params->p)
106 || !BN_hex2bn(&a, params->a)
107 || !BN_hex2bn(&b, params->b)
108 || !BN_hex2bn(&cofactor, params->cofactor)) {
109 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
113 grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
115 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
119 P = EC_POINT_new(grp);
121 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
125 if (!BN_hex2bn(&x, params->x)
126 || !BN_hex2bn(&y, params->y)
127 || !EC_POINT_set_affine_coordinates_GFp(grp, P, x, y, ctx)
128 || !BN_hex2bn(&q, params->q)) {
129 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
133 if (!EC_GROUP_set_generator(grp, P, q, cofactor)) {
134 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
137 EC_GROUP_set_curve_name(grp, params->nid);
138 if (!EC_KEY_set_group(eckey, grp)) {
139 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
154 * Computes gost_ec signature as ECDSA_SIG structure
157 ECDSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
159 ECDSA_SIG *newsig = NULL, *ret = NULL;
161 BIGNUM *order = NULL;
162 const EC_GROUP *group;
163 const BIGNUM *priv_key;
164 BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL,
165 *k = NULL, *e = NULL;
167 BIGNUM *new_r = NULL, *new_s = NULL;
172 OPENSSL_assert(dgst != NULL && eckey != NULL);
174 if (!(ctx = BN_CTX_new())) {
175 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
180 OPENSSL_assert(dlen == 32 || dlen == 64);
181 md = hashsum2bn(dgst, dlen);
182 newsig = ECDSA_SIG_new();
183 if (!newsig || !md) {
184 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
187 group = EC_KEY_get0_group(eckey);
189 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
192 order = BN_CTX_get(ctx);
193 if (!order || !EC_GROUP_get_order(group, order, ctx)) {
194 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
197 priv_key = EC_KEY_get0_private_key(eckey);
199 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
203 if (!e || !BN_mod(e, md, order, ctx)) {
204 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
208 fprintf(stderr, "digest as bignum=");
209 BN_print_fp(stderr, md);
210 fprintf(stderr, "\ndigest mod q=");
211 BN_print_fp(stderr, e);
212 fprintf(stderr, "\n");
218 C = EC_POINT_new(group);
220 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
226 if (!BN_rand_range(k, order)) {
227 GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_RNG_ERROR);
231 * To avoid timing information leaking the length of k,
232 * compute C*k using an equivalent scalar of fixed bit-length */
233 if (!BN_add(k, k, order)
234 || (BN_num_bits(k) <= BN_num_bits(order)
235 && !BN_add(k, k, order))) {
238 if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
239 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
247 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
250 if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
251 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
255 if (!BN_nnmod(r, X, order, ctx)) {
256 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
260 while (BN_is_zero(r));
261 /* s = (r*priv_key+k*e) mod order */
263 tmp = BN_CTX_get(ctx);
265 tmp2 = BN_CTX_get(ctx);
268 if (!tmp || !tmp2 || !s) {
269 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
273 if (!BN_mod_mul(tmp, priv_key, r, order, ctx)
274 || !BN_mod_mul(tmp2, k, e, order, ctx)
275 || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
276 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
280 while (BN_is_zero(s));
284 if (!new_s || !new_r) {
285 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
288 ECDSA_SIG_set0(newsig, new_r, new_s);
298 if (!ret && newsig) {
299 ECDSA_SIG_free(newsig);
305 * Verifies gost ec signature
308 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
309 ECDSA_SIG *sig, EC_KEY *ec)
312 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
314 BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *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 ECDSA_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;