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 getbnfrombuf(buf, len);
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;
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 if (!p || !a || !b || !x || !y || !q) {
99 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
103 if (!BN_hex2bn(&p, params->p)
104 || !BN_hex2bn(&a, params->a)
105 || !BN_hex2bn(&b, params->b)) {
106 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
110 grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
112 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
116 P = EC_POINT_new(grp);
118 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_MALLOC_FAILURE);
122 if (!BN_hex2bn(&x, params->x)
123 || !BN_hex2bn(&y, params->y)
124 || !EC_POINT_set_affine_coordinates_GFp(grp, P, x, y, ctx)
125 || !BN_hex2bn(&q, params->q)) {
126 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
130 if (!EC_GROUP_set_generator(grp, P, q, NULL)) {
131 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
134 EC_GROUP_set_curve_name(grp, params->nid);
135 if (!EC_KEY_set_group(eckey, grp)) {
136 GOSTerr(GOST_F_FILL_GOST_EC_PARAMS, ERR_R_INTERNAL_ERROR);
151 * Computes gost_ec signature as DSA_SIG structure
154 DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
156 DSA_SIG *newsig = NULL, *ret = NULL;
158 BIGNUM *order = NULL;
159 const EC_GROUP *group;
160 const BIGNUM *priv_key;
161 BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL,
162 *k = NULL, *e = NULL;
166 OPENSSL_assert(dgst != NULL && eckey != NULL);
168 if (!(ctx = BN_CTX_new())) {
169 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
174 OPENSSL_assert(dlen == 32 || dlen == 64);
175 md = hashsum2bn(dgst, dlen);
176 newsig = DSA_SIG_new();
177 if (!newsig || !md) {
178 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
181 group = EC_KEY_get0_group(eckey);
183 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
186 order = BN_CTX_get(ctx);
187 if (!order || !EC_GROUP_get_order(group, order, ctx)) {
188 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
191 priv_key = EC_KEY_get0_private_key(eckey);
193 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
197 if (!e || !BN_mod(e, md, order, ctx)) {
198 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
202 fprintf(stderr, "digest as bignum=");
203 BN_print_fp(stderr, md);
204 fprintf(stderr, "\ndigest mod q=");
205 BN_print_fp(stderr, e);
206 fprintf(stderr, "\n");
212 C = EC_POINT_new(group);
214 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
220 if (!BN_rand_range(k, order)) {
221 GOSTerr(GOST_F_GOST_EC_SIGN, GOST_R_RNG_ERROR);
225 * To avoid timing information leaking the length of k,
226 * compute C*k using an equivalent scalar of fixed bit-length */
227 if (!BN_add(k, k, order)
228 || (BN_num_bits(k) <= BN_num_bits(order)
229 && !BN_add(k, k, order))) {
232 if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
233 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
241 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
244 if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
245 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_EC_LIB);
249 if (!BN_nnmod(r, X, order, ctx)) {
250 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
254 while (BN_is_zero(r));
255 /* s = (r*priv_key+k*e) mod order */
257 tmp = BN_CTX_get(ctx);
259 tmp2 = BN_CTX_get(ctx);
262 if (!tmp || !tmp2 || !s) {
263 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
267 if (!BN_mod_mul(tmp, priv_key, r, order, ctx)
268 || !BN_mod_mul(tmp2, k, e, order, ctx)
269 || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
270 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_INTERNAL_ERROR);
274 while (BN_is_zero(s));
276 newsig->s = BN_dup(s);
277 newsig->r = BN_dup(r);
278 if (!newsig->s || !newsig->r) {
279 GOSTerr(GOST_F_GOST_EC_SIGN, ERR_R_MALLOC_FAILURE);
291 if (!ret && newsig) {
292 DSA_SIG_free(newsig);
298 * Verifies gost ec signature
301 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
302 DSA_SIG *sig, EC_KEY *ec)
305 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
307 BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL,
308 *z1 = NULL, *z2 = NULL;
309 BIGNUM *X = NULL, *tmp = NULL;
311 const EC_POINT *pub_key = NULL;
314 OPENSSL_assert(dgst != NULL && sig != NULL && group != NULL);
316 if (!(ctx = BN_CTX_new())) {
317 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
322 order = BN_CTX_get(ctx);
324 z1 = BN_CTX_get(ctx);
325 z2 = BN_CTX_get(ctx);
326 tmp = BN_CTX_get(ctx);
330 if (!order || !e || !z1 || !z2 || !tmp || !X || !R || !v) {
331 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
335 pub_key = EC_KEY_get0_public_key(ec);
336 if (!pub_key || !EC_GROUP_get_order(group, order, ctx)) {
337 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
341 if (BN_is_zero(sig->s) || BN_is_zero(sig->r) ||
342 (BN_cmp(sig->s, order) >= 1) || (BN_cmp(sig->r, order) >= 1)) {
343 GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
348 OPENSSL_assert(dgst_len == 32 || dgst_len == 64);
349 md = hashsum2bn(dgst, dgst_len);
350 if (!md || !BN_mod(e, md, order, ctx)) {
351 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
355 fprintf(stderr, "digest as bignum: ");
356 BN_print_fp(stderr, md);
357 fprintf(stderr, "\ndigest mod q: ");
358 BN_print_fp(stderr, e);
360 if (BN_is_zero(e) && !BN_one(e)) {
361 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
364 v = BN_mod_inverse(v, e, order, ctx);
365 if (!v || !BN_mod_mul(z1, sig->s, v, order, ctx)
366 || !BN_sub(tmp, order, sig->r)
367 || !BN_mod_mul(z2, tmp, v, order, ctx)) {
368 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
372 fprintf(stderr, "\nInverted digest value: ");
373 BN_print_fp(stderr, v);
374 fprintf(stderr, "\nz1: ");
375 BN_print_fp(stderr, z1);
376 fprintf(stderr, "\nz2: ");
377 BN_print_fp(stderr, z2);
379 C = EC_POINT_new(group);
381 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_MALLOC_FAILURE);
384 if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) {
385 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
388 if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
389 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_EC_LIB);
392 if (!BN_mod(R, X, order, ctx)) {
393 GOSTerr(GOST_F_GOST_EC_VERIFY, ERR_R_INTERNAL_ERROR);
397 fprintf(stderr, "\nX=");
398 BN_print_fp(stderr, X);
399 fprintf(stderr, "\nX mod q=");
400 BN_print_fp(stderr, R);
401 fprintf(stderr, "\n");
403 if (BN_cmp(R, sig->r) != 0) {
404 GOSTerr(GOST_F_GOST_EC_VERIFY, GOST_R_SIGNATURE_MISMATCH);
419 * Computes GOST R 34.10-2001 public key
420 * or GOST R 34.10-2012 public key
423 int gost_ec_compute_public(EC_KEY *ec)
425 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
426 EC_POINT *pub_key = NULL;
427 const BIGNUM *priv_key = NULL;
432 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITIALIZED);
438 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
443 priv_key = EC_KEY_get0_private_key(ec);
445 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
449 pub_key = EC_POINT_new(group);
451 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
455 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
456 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
459 if (!EC_KEY_set_public_key(ec, pub_key)) {
460 GOSTerr(GOST_F_GOST_EC_COMPUTE_PUBLIC, ERR_R_EC_LIB);
466 EC_POINT_free(pub_key);
474 * Generates GOST R 34.10-2001
475 * or GOST R 34.10-2012 keypair
478 int gost_ec_keygen(EC_KEY *ec)
480 BIGNUM *order = NULL, *d = NULL;
481 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
485 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
492 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_MALLOC_FAILURE);
496 if (!EC_GROUP_get_order(group, order, NULL)) {
497 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
502 if (!BN_rand_range(d, order)) {
503 GOSTerr(GOST_F_GOST_EC_KEYGEN, GOST_R_RNG_ERROR);
507 while (BN_is_zero(d));
509 if (!EC_KEY_set_private_key(ec, d)) {
510 GOSTerr(GOST_F_GOST_EC_KEYGEN, ERR_R_INTERNAL_ERROR);
521 return (ok) ? gost_ec_compute_public(ec) : 0;