gost2001.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /**********************************************************************
  2. * gost2001.c *
  3. * Copyright (c) 2005-2006 Cryptocom LTD *
  4. * This file is distributed under the same license as OpenSSL *
  5. * *
  6. * Implementation of GOST R 34.10-2001 *
  7. * Requires OpenSSL 0.9.9 for compilation *
  8. **********************************************************************/
  9. #include "gost_lcl.h"
  10. #include "gost_params.h"
  11. #include <string.h>
  12. #include <openssl/rand.h>
  13. #include <openssl/ecdsa.h>
  14. #include <openssl/err.h>
  15. #include "e_gost_err.h"
  16. #ifdef DEBUG_SIGN
  17. extern
  18. void dump_signature(const char *message, const unsigned char *buffer,
  19. size_t len);
  20. void dump_dsa_sig(const char *message, DSA_SIG *sig);
  21. #else
  22. # define dump_signature(a,b,c)
  23. # define dump_dsa_sig(a,b)
  24. #endif
  25. /*
  26. * Fills EC_KEY structure hidden in the app_data field of DSA structure
  27. * with parameter information, extracted from parameter array in
  28. * params.c file.
  29. *
  30. * Also fils DSA->q field with copy of EC_GROUP order field to make
  31. * DSA_size function work
  32. */
  33. int fill_GOST2001_params(EC_KEY *eckey, int nid)
  34. {
  35. R3410_2001_params *params = R3410_2001_paramset;
  36. EC_GROUP *grp = NULL;
  37. BIGNUM *p = NULL, *q = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
  38. EC_POINT *P = NULL;
  39. BN_CTX *ctx = BN_CTX_new();
  40. int ok = 0;
  41. if(!ctx) {
  42. GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
  43. goto err;
  44. }
  45. BN_CTX_start(ctx);
  46. p = BN_CTX_get(ctx);
  47. a = BN_CTX_get(ctx);
  48. b = BN_CTX_get(ctx);
  49. x = BN_CTX_get(ctx);
  50. y = BN_CTX_get(ctx);
  51. q = BN_CTX_get(ctx);
  52. if(!p || !a || !b || !x || !y || !q) {
  53. GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
  54. goto err;
  55. }
  56. while (params->nid != NID_undef && params->nid != nid)
  57. params++;
  58. if (params->nid == NID_undef) {
  59. GOSTerr(GOST_F_FILL_GOST2001_PARAMS,
  60. GOST_R_UNSUPPORTED_PARAMETER_SET);
  61. goto err;
  62. }
  63. if(!BN_hex2bn(&p, params->p)
  64. || !BN_hex2bn(&a, params->a)
  65. || !BN_hex2bn(&b, params->b)) {
  66. GOSTerr(GOST_F_FILL_GOST2001_PARAMS,
  67. ERR_R_INTERNAL_ERROR);
  68. goto err;
  69. }
  70. grp = EC_GROUP_new_curve_GFp(p, a, b, ctx);
  71. if(!grp) {
  72. GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
  73. goto err;
  74. }
  75. P = EC_POINT_new(grp);
  76. if(!P) {
  77. GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_MALLOC_FAILURE);
  78. goto err;
  79. }
  80. if(!BN_hex2bn(&x, params->x)
  81. || !BN_hex2bn(&y, params->y)
  82. || !EC_POINT_set_affine_coordinates_GFp(grp, P, x, y, ctx)
  83. || !BN_hex2bn(&q, params->q)) {
  84. GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_INTERNAL_ERROR);
  85. goto err;
  86. }
  87. #ifdef DEBUG_KEYS
  88. fprintf(stderr, "Set params index %d oid %s\nq=",
  89. (params - R3410_2001_paramset), OBJ_nid2sn(params->nid));
  90. BN_print_fp(stderr, q);
  91. fprintf(stderr, "\n");
  92. #endif
  93. if(!EC_GROUP_set_generator(grp, P, q, NULL)) {
  94. GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_INTERNAL_ERROR);
  95. goto err;
  96. }
  97. EC_GROUP_set_curve_name(grp, params->nid);
  98. if(!EC_KEY_set_group(eckey, grp)) {
  99. GOSTerr(GOST_F_FILL_GOST2001_PARAMS, ERR_R_INTERNAL_ERROR);
  100. goto err;
  101. }
  102. ok = 1;
  103. err:
  104. if (P) EC_POINT_free(P);
  105. if (grp) EC_GROUP_free(grp);
  106. if (ctx) {
  107. BN_CTX_end(ctx);
  108. BN_CTX_free(ctx);
  109. }
  110. return ok;
  111. }
  112. /*
  113. * Computes gost2001 signature as DSA_SIG structure
  114. *
  115. *
  116. */
  117. DSA_SIG *gost2001_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
  118. {
  119. DSA_SIG *newsig = NULL, *ret = NULL;
  120. BIGNUM *md = hashsum2bn(dgst);
  121. BIGNUM *order = NULL;
  122. const EC_GROUP *group;
  123. const BIGNUM *priv_key;
  124. BIGNUM *r = NULL, *s = NULL, *X = NULL, *tmp = NULL, *tmp2 = NULL, *k =
  125. NULL, *e = NULL;
  126. EC_POINT *C = NULL;
  127. BN_CTX *ctx = BN_CTX_new();
  128. if(!ctx || !md) {
  129. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
  130. goto err;
  131. }
  132. BN_CTX_start(ctx);
  133. OPENSSL_assert(dlen == 32);
  134. newsig = DSA_SIG_new();
  135. if (!newsig) {
  136. GOSTerr(GOST_F_GOST2001_DO_SIGN, GOST_R_NO_MEMORY);
  137. goto err;
  138. }
  139. group = EC_KEY_get0_group(eckey);
  140. if(!group) {
  141. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
  142. goto err;
  143. }
  144. order = BN_CTX_get(ctx);
  145. if(!order || !EC_GROUP_get_order(group, order, ctx)) {
  146. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
  147. goto err;
  148. }
  149. priv_key = EC_KEY_get0_private_key(eckey);
  150. if(!priv_key) {
  151. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
  152. goto err;
  153. }
  154. e = BN_CTX_get(ctx);
  155. if(!e || !BN_mod(e, md, order, ctx)) {
  156. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
  157. goto err;
  158. }
  159. #ifdef DEBUG_SIGN
  160. fprintf(stderr, "digest as bignum=");
  161. BN_print_fp(stderr, md);
  162. fprintf(stderr, "\ndigest mod q=");
  163. BN_print_fp(stderr, e);
  164. fprintf(stderr, "\n");
  165. #endif
  166. if (BN_is_zero(e)) {
  167. BN_one(e);
  168. }
  169. k = BN_CTX_get(ctx);
  170. C = EC_POINT_new(group);
  171. if(!k || !C) {
  172. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
  173. goto err;
  174. }
  175. do {
  176. do {
  177. if (!BN_rand_range(k, order)) {
  178. GOSTerr(GOST_F_GOST2001_DO_SIGN,
  179. GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
  180. goto err;
  181. }
  182. if (!EC_POINT_mul(group, C, k, NULL, NULL, ctx)) {
  183. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_EC_LIB);
  184. goto err;
  185. }
  186. if (!X)
  187. X = BN_CTX_get(ctx);
  188. if (!r)
  189. r = BN_CTX_get(ctx);
  190. if (!X || !r) {
  191. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
  192. goto err;
  193. }
  194. if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
  195. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_EC_LIB);
  196. goto err;
  197. }
  198. if(!BN_nnmod(r, X, order, ctx)) {
  199. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
  200. goto err;
  201. }
  202. }
  203. while (BN_is_zero(r));
  204. /* s = (r*priv_key+k*e) mod order */
  205. if (!tmp)
  206. tmp = BN_CTX_get(ctx);
  207. if (!tmp2)
  208. tmp2 = BN_CTX_get(ctx);
  209. if (!s)
  210. s = BN_CTX_get(ctx);
  211. if (!tmp || !tmp2 || !s) {
  212. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
  213. goto err;
  214. }
  215. if(!BN_mod_mul(tmp, priv_key, r, order, ctx)
  216. || !BN_mod_mul(tmp2, k, e, order, ctx)
  217. || !BN_mod_add(s, tmp, tmp2, order, ctx)) {
  218. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_INTERNAL_ERROR);
  219. goto err;
  220. }
  221. }
  222. while (BN_is_zero(s));
  223. newsig->s = BN_dup(s);
  224. newsig->r = BN_dup(r);
  225. if(!newsig->s || !newsig->r) {
  226. GOSTerr(GOST_F_GOST2001_DO_SIGN, ERR_R_MALLOC_FAILURE);
  227. goto err;
  228. }
  229. ret = newsig;
  230. err:
  231. if(ctx) {
  232. BN_CTX_end(ctx);
  233. BN_CTX_free(ctx);
  234. }
  235. if (C) EC_POINT_free(C);
  236. if (md) BN_free(md);
  237. if (!ret && newsig) {
  238. DSA_SIG_free(newsig);
  239. }
  240. return ret;
  241. }
  242. /*
  243. * Verifies gost 2001 signature
  244. *
  245. */
  246. int gost2001_do_verify(const unsigned char *dgst, int dgst_len,
  247. DSA_SIG *sig, EC_KEY *ec)
  248. {
  249. BN_CTX *ctx = BN_CTX_new();
  250. const EC_GROUP *group = EC_KEY_get0_group(ec);
  251. BIGNUM *order;
  252. BIGNUM *md = NULL, *e = NULL, *R = NULL, *v = NULL, *z1 = NULL, *z2 =
  253. NULL;
  254. BIGNUM *X = NULL, *tmp = NULL;
  255. EC_POINT *C = NULL;
  256. const EC_POINT *pub_key = NULL;
  257. int ok = 0;
  258. if(!ctx || !group) {
  259. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
  260. goto err;
  261. }
  262. BN_CTX_start(ctx);
  263. order = BN_CTX_get(ctx);
  264. e = BN_CTX_get(ctx);
  265. z1 = BN_CTX_get(ctx);
  266. z2 = BN_CTX_get(ctx);
  267. tmp = BN_CTX_get(ctx);
  268. X = BN_CTX_get(ctx);
  269. R = BN_CTX_get(ctx);
  270. v = BN_CTX_get(ctx);
  271. if(!order || !e || !z1 || !z2 || !tmp || !X || !R || !v) {
  272. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  273. goto err;
  274. }
  275. pub_key = EC_KEY_get0_public_key(ec);
  276. if(!pub_key || !EC_GROUP_get_order(group, order, ctx)) {
  277. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
  278. goto err;
  279. }
  280. if (BN_is_zero(sig->s) || BN_is_zero(sig->r) ||
  281. (BN_cmp(sig->s, order) >= 1) || (BN_cmp(sig->r, order) >= 1)) {
  282. GOSTerr(GOST_F_GOST2001_DO_VERIFY,
  283. GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
  284. goto err;
  285. }
  286. md = hashsum2bn(dgst);
  287. if(!md || !BN_mod(e, md, order, ctx)) {
  288. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
  289. goto err;
  290. }
  291. #ifdef DEBUG_SIGN
  292. fprintf(stderr, "digest as bignum: ");
  293. BN_print_fp(stderr, md);
  294. fprintf(stderr, "\ndigest mod q: ");
  295. BN_print_fp(stderr, e);
  296. #endif
  297. if (BN_is_zero(e) && !BN_one(e)) {
  298. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
  299. goto err;
  300. }
  301. v = BN_mod_inverse(v, e, order, ctx);
  302. if(!v
  303. || !BN_mod_mul(z1, sig->s, v, order, ctx)
  304. || !BN_sub(tmp, order, sig->r)
  305. || !BN_mod_mul(z2, tmp, v, order, ctx)) {
  306. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
  307. goto err;
  308. }
  309. #ifdef DEBUG_SIGN
  310. fprintf(stderr, "\nInverted digest value: ");
  311. BN_print_fp(stderr, v);
  312. fprintf(stderr, "\nz1: ");
  313. BN_print_fp(stderr, z1);
  314. fprintf(stderr, "\nz2: ");
  315. BN_print_fp(stderr, z2);
  316. #endif
  317. C = EC_POINT_new(group);
  318. if (!C) {
  319. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  320. goto err;
  321. }
  322. if (!EC_POINT_mul(group, C, z1, pub_key, z2, ctx)) {
  323. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_EC_LIB);
  324. goto err;
  325. }
  326. if (!EC_POINT_get_affine_coordinates_GFp(group, C, X, NULL, ctx)) {
  327. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_EC_LIB);
  328. goto err;
  329. }
  330. if(!BN_mod(R, X, order, ctx)) {
  331. GOSTerr(GOST_F_GOST2001_DO_VERIFY, ERR_R_INTERNAL_ERROR);
  332. goto err;
  333. }
  334. #ifdef DEBUG_SIGN
  335. fprintf(stderr, "\nX=");
  336. BN_print_fp(stderr, X);
  337. fprintf(stderr, "\nX mod q=");
  338. BN_print_fp(stderr, R);
  339. fprintf(stderr, "\n");
  340. #endif
  341. if (BN_cmp(R, sig->r) != 0) {
  342. GOSTerr(GOST_F_GOST2001_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
  343. } else {
  344. ok = 1;
  345. }
  346. err:
  347. if (C) EC_POINT_free(C);
  348. if (ctx) {
  349. BN_CTX_end(ctx);
  350. BN_CTX_free(ctx);
  351. }
  352. if (md) BN_free(md);
  353. return ok;
  354. }
  355. /*
  356. * Computes GOST R 34.10-2001 public key
  357. *
  358. *
  359. */
  360. int gost2001_compute_public(EC_KEY *ec)
  361. {
  362. const EC_GROUP *group = EC_KEY_get0_group(ec);
  363. EC_POINT *pub_key = NULL;
  364. const BIGNUM *priv_key = NULL;
  365. BN_CTX *ctx = NULL;
  366. int ok = 0;
  367. if (!group) {
  368. GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,
  369. GOST_R_KEY_IS_NOT_INITIALIZED);
  370. return 0;
  371. }
  372. ctx = BN_CTX_new();
  373. if(!ctx) {
  374. GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
  375. goto err;
  376. }
  377. BN_CTX_start(ctx);
  378. if (!(priv_key = EC_KEY_get0_private_key(ec))) {
  379. GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
  380. goto err;
  381. }
  382. pub_key = EC_POINT_new(group);
  383. if(!pub_key) {
  384. GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
  385. goto err;
  386. }
  387. if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
  388. GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
  389. goto err;
  390. }
  391. if (!EC_KEY_set_public_key(ec, pub_key)) {
  392. GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);
  393. goto err;
  394. }
  395. ok = 256;
  396. err:
  397. if (pub_key) EC_POINT_free(pub_key);
  398. if (ctx) {
  399. BN_CTX_end(ctx);
  400. BN_CTX_free(ctx);
  401. }
  402. return ok;
  403. }
  404. /*
  405. *
  406. * Generates GOST R 34.10-2001 keypair
  407. *
  408. *
  409. */
  410. int gost2001_keygen(EC_KEY *ec)
  411. {
  412. BIGNUM *order = BN_new(), *d = BN_new();
  413. const EC_GROUP *group = EC_KEY_get0_group(ec);
  414. if(!group || !EC_GROUP_get_order(group, order, NULL)) {
  415. GOSTerr(GOST_F_GOST2001_KEYGEN, ERR_R_INTERNAL_ERROR);
  416. BN_free(d);
  417. BN_free(order);
  418. return 0;
  419. }
  420. do {
  421. if (!BN_rand_range(d, order)) {
  422. GOSTerr(GOST_F_GOST2001_KEYGEN,
  423. GOST_R_RANDOM_NUMBER_GENERATOR_FAILED);
  424. BN_free(d);
  425. BN_free(order);
  426. return 0;
  427. }
  428. }
  429. while (BN_is_zero(d));
  430. if(!EC_KEY_set_private_key(ec, d)) {
  431. GOSTerr(GOST_F_GOST2001_KEYGEN, ERR_R_INTERNAL_ERROR);
  432. BN_free(d);
  433. BN_free(order);
  434. return 0;
  435. }
  436. BN_free(d);
  437. BN_free(order);
  438. return gost2001_compute_public(ec);
  439. }