gost_sign.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**********************************************************************
  2. * gost_sign.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-94 signature algorithm *
  7. * for OpenSSL *
  8. * Requires OpenSSL 0.9.9 for compilation *
  9. **********************************************************************/
  10. #include <string.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/bn.h>
  13. #include <openssl/dsa.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/err.h>
  16. #include "gost_params.h"
  17. #include "gost_lcl.h"
  18. #include "e_gost_err.h"
  19. #ifdef DEBUG_SIGN
  20. void dump_signature(const char *message, const unsigned char *buffer,
  21. size_t len)
  22. {
  23. size_t i;
  24. fprintf(stderr, "signature %s Length=%d", message, len);
  25. for (i = 0; i < len; i++) {
  26. if (i % 16 == 0)
  27. fputc('\n', stderr);
  28. fprintf(stderr, " %02x", buffer[i]);
  29. }
  30. fprintf(stderr, "\nEnd of signature\n");
  31. }
  32. void dump_dsa_sig(const char *message, DSA_SIG *sig)
  33. {
  34. fprintf(stderr, "%s\nR=", message);
  35. BN_print_fp(stderr, sig->r);
  36. fprintf(stderr, "\nS=");
  37. BN_print_fp(stderr, sig->s);
  38. fprintf(stderr, "\n");
  39. }
  40. #else
  41. # define dump_signature(a,b,c)
  42. # define dump_dsa_sig(a,b)
  43. #endif
  44. /*
  45. * Computes signature and returns it as DSA_SIG structure
  46. */
  47. DSA_SIG *gost_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
  48. {
  49. BIGNUM *k = NULL, *tmp = NULL, *tmp2 = NULL;
  50. DSA_SIG *newsig = NULL, *ret = NULL;
  51. BIGNUM *md = hashsum2bn(dgst);
  52. /* check if H(M) mod q is zero */
  53. BN_CTX *ctx = BN_CTX_new();
  54. if(!ctx) {
  55. GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
  56. goto err;
  57. }
  58. BN_CTX_start(ctx);
  59. newsig = DSA_SIG_new();
  60. if (!newsig) {
  61. GOSTerr(GOST_F_GOST_DO_SIGN, GOST_R_NO_MEMORY);
  62. goto err;
  63. }
  64. tmp = BN_CTX_get(ctx);
  65. k = BN_CTX_get(ctx);
  66. tmp2 = BN_CTX_get(ctx);
  67. if(!tmp || !k || !tmp2) {
  68. GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
  69. goto err;
  70. }
  71. BN_mod(tmp, md, dsa->q, ctx);
  72. if (BN_is_zero(tmp)) {
  73. BN_one(md);
  74. }
  75. do {
  76. do {
  77. /*
  78. * Generate random number k less than q
  79. */
  80. BN_rand_range(k, dsa->q);
  81. /* generate r = (a^x mod p) mod q */
  82. BN_mod_exp(tmp, dsa->g, k, dsa->p, ctx);
  83. if (!(newsig->r)) {
  84. newsig->r = BN_new();
  85. if(!newsig->r) {
  86. GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
  87. goto err;
  88. }
  89. }
  90. BN_mod(newsig->r, tmp, dsa->q, ctx);
  91. }
  92. while (BN_is_zero(newsig->r));
  93. /* generate s = (xr + k(Hm)) mod q */
  94. BN_mod_mul(tmp, dsa->priv_key, newsig->r, dsa->q, ctx);
  95. BN_mod_mul(tmp2, k, md, dsa->q, ctx);
  96. if (!newsig->s) {
  97. newsig->s = BN_new();
  98. if(!newsig->s) {
  99. GOSTerr(GOST_F_GOST_DO_SIGN, ERR_R_MALLOC_FAILURE);
  100. goto err;
  101. }
  102. }
  103. BN_mod_add(newsig->s, tmp, tmp2, dsa->q, ctx);
  104. }
  105. while (BN_is_zero(newsig->s));
  106. ret = newsig;
  107. err:
  108. BN_free(md);
  109. if(ctx) {
  110. BN_CTX_end(ctx);
  111. BN_CTX_free(ctx);
  112. }
  113. if(!ret && newsig) {
  114. DSA_SIG_free(newsig);
  115. }
  116. return ret;
  117. }
  118. /*
  119. * Packs signature according to Cryptocom rules
  120. * and frees up DSA_SIG structure
  121. */
  122. /*-
  123. int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen)
  124. {
  125. *siglen = 2*order;
  126. memset(sig,0,*siglen);
  127. store_bignum(s->r, sig,order);
  128. store_bignum(s->s, sig + order,order);
  129. dump_signature("serialized",sig,*siglen);
  130. DSA_SIG_free(s);
  131. return 1;
  132. }
  133. */
  134. /*
  135. * Packs signature according to Cryptopro rules
  136. * and frees up DSA_SIG structure
  137. */
  138. int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
  139. {
  140. *siglen = 2 * order;
  141. memset(sig, 0, *siglen);
  142. store_bignum(s->s, sig, order);
  143. store_bignum(s->r, sig + order, order);
  144. dump_signature("serialized", sig, *siglen);
  145. DSA_SIG_free(s);
  146. return 1;
  147. }
  148. /*
  149. * Verifies signature passed as DSA_SIG structure
  150. *
  151. */
  152. int gost_do_verify(const unsigned char *dgst, int dgst_len,
  153. DSA_SIG *sig, DSA *dsa)
  154. {
  155. BIGNUM *md = NULL, *tmp = NULL;
  156. BIGNUM *q2 = NULL;
  157. BIGNUM *u = NULL, *v = NULL, *z1 = NULL, *z2 = NULL;
  158. BIGNUM *tmp2 = NULL, *tmp3 = NULL;
  159. int ok = 0;
  160. BN_CTX *ctx = BN_CTX_new();
  161. if(!ctx) {
  162. GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  163. goto err;
  164. }
  165. BN_CTX_start(ctx);
  166. if (BN_cmp(sig->s, dsa->q) >= 1 || BN_cmp(sig->r, dsa->q) >= 1) {
  167. GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);
  168. goto err;
  169. }
  170. md = hashsum2bn(dgst);
  171. tmp = BN_CTX_get(ctx);
  172. v = BN_CTX_get(ctx);
  173. q2 = BN_CTX_get(ctx);
  174. z1 = BN_CTX_get(ctx);
  175. z2 = BN_CTX_get(ctx);
  176. tmp2 = BN_CTX_get(ctx);
  177. tmp3 = BN_CTX_get(ctx);
  178. u = BN_CTX_get(ctx);
  179. if(!tmp || !v || !q2 || !z1 || !z2 || !tmp2 || !tmp3 || !u) {
  180. GOSTerr(GOST_F_GOST_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  181. goto err;
  182. }
  183. BN_mod(tmp, md, dsa->q, ctx);
  184. if (BN_is_zero(tmp)) {
  185. BN_one(md);
  186. }
  187. BN_copy(q2, dsa->q);
  188. BN_sub_word(q2, 2);
  189. BN_mod_exp(v, md, q2, dsa->q, ctx);
  190. BN_mod_mul(z1, sig->s, v, dsa->q, ctx);
  191. BN_sub(tmp, dsa->q, sig->r);
  192. BN_mod_mul(z2, tmp, v, dsa->p, ctx);
  193. BN_mod_exp(tmp, dsa->g, z1, dsa->p, ctx);
  194. BN_mod_exp(tmp2, dsa->pub_key, z2, dsa->p, ctx);
  195. BN_mod_mul(tmp3, tmp, tmp2, dsa->p, ctx);
  196. BN_mod(u, tmp3, dsa->q, ctx);
  197. ok = (BN_cmp(u, sig->r) == 0);
  198. if (!ok) {
  199. GOSTerr(GOST_F_GOST_DO_VERIFY, GOST_R_SIGNATURE_MISMATCH);
  200. }
  201. err:
  202. if(md) BN_free(md);
  203. if(ctx) {
  204. BN_CTX_end(ctx);
  205. BN_CTX_free(ctx);
  206. }
  207. return ok;
  208. }
  209. /*
  210. * Computes public keys for GOST R 34.10-94 algorithm
  211. *
  212. */
  213. int gost94_compute_public(DSA *dsa)
  214. {
  215. /* Now fill algorithm parameters with correct values */
  216. BN_CTX *ctx;
  217. if (!dsa->g) {
  218. GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, GOST_R_KEY_IS_NOT_INITALIZED);
  219. return 0;
  220. }
  221. ctx = BN_CTX_new();
  222. if(!ctx) {
  223. GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
  224. return 0;
  225. }
  226. dsa->pub_key = BN_new();
  227. if(!dsa->pub_key) {
  228. GOSTerr(GOST_F_GOST94_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);
  229. BN_CTX_free(ctx);
  230. return 0;
  231. }
  232. /* Compute public key y = a^x mod p */
  233. BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx);
  234. BN_CTX_free(ctx);
  235. return 1;
  236. }
  237. /*
  238. * Fill GOST 94 params, searching them in R3410_paramset array
  239. * by nid of paramset
  240. *
  241. */
  242. int fill_GOST94_params(DSA *dsa, int nid)
  243. {
  244. R3410_params *params = R3410_paramset;
  245. while (params->nid != NID_undef && params->nid != nid)
  246. params++;
  247. if (params->nid == NID_undef) {
  248. GOSTerr(GOST_F_FILL_GOST94_PARAMS, GOST_R_UNSUPPORTED_PARAMETER_SET);
  249. return 0;
  250. }
  251. #define dump_signature(a,b,c)
  252. if (dsa->p) {
  253. BN_free(dsa->p);
  254. }
  255. dsa->p = NULL;
  256. BN_dec2bn(&(dsa->p), params->p);
  257. if (dsa->q) {
  258. BN_free(dsa->q);
  259. }
  260. dsa->q = NULL;
  261. BN_dec2bn(&(dsa->q), params->q);
  262. if (dsa->g) {
  263. BN_free(dsa->g);
  264. }
  265. dsa->g = NULL;
  266. BN_dec2bn(&(dsa->g), params->a);
  267. return 1;
  268. }
  269. /*
  270. * Generate GOST R 34.10-94 keypair
  271. *
  272. *
  273. */
  274. int gost_sign_keygen(DSA *dsa)
  275. {
  276. dsa->priv_key = BN_new();
  277. if(!dsa->priv_key) {
  278. GOSTerr(GOST_F_GOST_SIGN_KEYGEN, ERR_R_MALLOC_FAILURE);
  279. return 0;
  280. }
  281. BN_rand_range(dsa->priv_key, dsa->q);
  282. return gost94_compute_public(dsa);
  283. }
  284. /* Unpack signature according to cryptocom rules */
  285. /*-
  286. DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen)
  287. {
  288. DSA_SIG *s;
  289. s = DSA_SIG_new();
  290. if (s == NULL)
  291. {
  292. GOSTerr(GOST_F_UNPACK_CC_SIGNATURE,GOST_R_NO_MEMORY);
  293. return(NULL);
  294. }
  295. s->r = getbnfrombuf(sig, siglen/2);
  296. s->s = getbnfrombuf(sig + siglen/2, siglen/2);
  297. return s;
  298. }
  299. */
  300. /* Unpack signature according to cryptopro rules */
  301. DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
  302. {
  303. DSA_SIG *s;
  304. s = DSA_SIG_new();
  305. if (s == NULL) {
  306. GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, GOST_R_NO_MEMORY);
  307. return NULL;
  308. }
  309. s->s = getbnfrombuf(sig, siglen / 2);
  310. s->r = getbnfrombuf(sig + siglen / 2, siglen / 2);
  311. return s;
  312. }
  313. /* Convert little-endian byte array into bignum */
  314. BIGNUM *hashsum2bn(const unsigned char *dgst)
  315. {
  316. unsigned char buf[32];
  317. int i;
  318. for (i = 0; i < 32; i++) {
  319. buf[31 - i] = dgst[i];
  320. }
  321. return getbnfrombuf(buf, 32);
  322. }
  323. /* Convert byte buffer to bignum, skipping leading zeros*/
  324. BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len)
  325. {
  326. while (*buf == 0 && len > 0) {
  327. buf++;
  328. len--;
  329. }
  330. if (len) {
  331. return BN_bin2bn(buf, len, NULL);
  332. } else {
  333. BIGNUM *b = BN_new();
  334. BN_zero(b);
  335. return b;
  336. }
  337. }
  338. /*
  339. * Pack bignum into byte buffer of given size, filling all leading bytes by
  340. * zeros
  341. */
  342. int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
  343. {
  344. int bytes = BN_num_bytes(bn);
  345. if (bytes > len)
  346. return 0;
  347. memset(buf, 0, len);
  348. BN_bn2bin(bn, buf + len - bytes);
  349. return 1;
  350. }