dsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. *
  57. * The DSS routines are based on patches supplied by
  58. * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
  59. #include <openssl/dsa.h>
  60. #include <string.h>
  61. #include <openssl/bn.h>
  62. #include <openssl/dh.h>
  63. #include <openssl/digest.h>
  64. #include <openssl/engine.h>
  65. #include <openssl/err.h>
  66. #include <openssl/ex_data.h>
  67. #include <openssl/mem.h>
  68. #include <openssl/rand.h>
  69. #include <openssl/sha.h>
  70. #include <openssl/thread.h>
  71. #include "../internal.h"
  72. #define OPENSSL_DSA_MAX_MODULUS_BITS 10000
  73. /* Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of
  74. * Rabin-Miller */
  75. #define DSS_prime_checks 50
  76. static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  77. DSA *DSA_new(void) {
  78. DSA *dsa = OPENSSL_malloc(sizeof(DSA));
  79. if (dsa == NULL) {
  80. OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
  81. return NULL;
  82. }
  83. memset(dsa, 0, sizeof(DSA));
  84. dsa->references = 1;
  85. CRYPTO_MUTEX_init(&dsa->method_mont_p_lock);
  86. CRYPTO_new_ex_data(&dsa->ex_data);
  87. return dsa;
  88. }
  89. void DSA_free(DSA *dsa) {
  90. if (dsa == NULL) {
  91. return;
  92. }
  93. if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
  94. return;
  95. }
  96. CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
  97. BN_clear_free(dsa->p);
  98. BN_clear_free(dsa->q);
  99. BN_clear_free(dsa->g);
  100. BN_clear_free(dsa->pub_key);
  101. BN_clear_free(dsa->priv_key);
  102. BN_clear_free(dsa->kinv);
  103. BN_clear_free(dsa->r);
  104. BN_MONT_CTX_free(dsa->method_mont_p);
  105. CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock);
  106. OPENSSL_free(dsa);
  107. }
  108. int DSA_up_ref(DSA *dsa) {
  109. CRYPTO_refcount_inc(&dsa->references);
  110. return 1;
  111. }
  112. int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
  113. size_t seed_len, int *out_counter,
  114. unsigned long *out_h, BN_GENCB *cb) {
  115. int ok = 0;
  116. unsigned char seed[SHA256_DIGEST_LENGTH];
  117. unsigned char md[SHA256_DIGEST_LENGTH];
  118. unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
  119. BIGNUM *r0, *W, *X, *c, *test;
  120. BIGNUM *g = NULL, *q = NULL, *p = NULL;
  121. BN_MONT_CTX *mont = NULL;
  122. int k, n = 0, m = 0;
  123. unsigned i;
  124. int counter = 0;
  125. int r = 0;
  126. BN_CTX *ctx = NULL;
  127. unsigned int h = 2;
  128. unsigned qsize;
  129. const EVP_MD *evpmd;
  130. evpmd = (bits >= 2048) ? EVP_sha256() : EVP_sha1();
  131. qsize = EVP_MD_size(evpmd);
  132. if (bits < 512) {
  133. bits = 512;
  134. }
  135. bits = (bits + 63) / 64 * 64;
  136. if (seed_in != NULL) {
  137. if (seed_len < (size_t)qsize) {
  138. return 0;
  139. }
  140. if (seed_len > (size_t)qsize) {
  141. /* Only consume as much seed as is expected. */
  142. seed_len = qsize;
  143. }
  144. memcpy(seed, seed_in, seed_len);
  145. }
  146. ctx = BN_CTX_new();
  147. if (ctx == NULL) {
  148. goto err;
  149. }
  150. BN_CTX_start(ctx);
  151. mont = BN_MONT_CTX_new();
  152. if (mont == NULL) {
  153. goto err;
  154. }
  155. r0 = BN_CTX_get(ctx);
  156. g = BN_CTX_get(ctx);
  157. W = BN_CTX_get(ctx);
  158. q = BN_CTX_get(ctx);
  159. X = BN_CTX_get(ctx);
  160. c = BN_CTX_get(ctx);
  161. p = BN_CTX_get(ctx);
  162. test = BN_CTX_get(ctx);
  163. if (test == NULL || !BN_lshift(test, BN_value_one(), bits - 1)) {
  164. goto err;
  165. }
  166. for (;;) {
  167. /* Find q. */
  168. for (;;) {
  169. /* step 1 */
  170. if (!BN_GENCB_call(cb, 0, m++)) {
  171. goto err;
  172. }
  173. int use_random_seed = (seed_in == NULL);
  174. if (use_random_seed) {
  175. if (!RAND_bytes(seed, qsize)) {
  176. goto err;
  177. }
  178. } else {
  179. /* If we come back through, use random seed next time. */
  180. seed_in = NULL;
  181. }
  182. memcpy(buf, seed, qsize);
  183. memcpy(buf2, seed, qsize);
  184. /* precompute "SEED + 1" for step 7: */
  185. for (i = qsize - 1; i < qsize; i--) {
  186. buf[i]++;
  187. if (buf[i] != 0) {
  188. break;
  189. }
  190. }
  191. /* step 2 */
  192. if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL) ||
  193. !EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) {
  194. goto err;
  195. }
  196. for (i = 0; i < qsize; i++) {
  197. md[i] ^= buf2[i];
  198. }
  199. /* step 3 */
  200. md[0] |= 0x80;
  201. md[qsize - 1] |= 0x01;
  202. if (!BN_bin2bn(md, qsize, q)) {
  203. goto err;
  204. }
  205. /* step 4 */
  206. r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, use_random_seed, cb);
  207. if (r > 0) {
  208. break;
  209. }
  210. if (r != 0) {
  211. goto err;
  212. }
  213. /* do a callback call */
  214. /* step 5 */
  215. }
  216. if (!BN_GENCB_call(cb, 2, 0) || !BN_GENCB_call(cb, 3, 0)) {
  217. goto err;
  218. }
  219. /* step 6 */
  220. counter = 0;
  221. /* "offset = 2" */
  222. n = (bits - 1) / 160;
  223. for (;;) {
  224. if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) {
  225. goto err;
  226. }
  227. /* step 7 */
  228. BN_zero(W);
  229. /* now 'buf' contains "SEED + offset - 1" */
  230. for (k = 0; k <= n; k++) {
  231. /* obtain "SEED + offset + k" by incrementing: */
  232. for (i = qsize - 1; i < qsize; i--) {
  233. buf[i]++;
  234. if (buf[i] != 0) {
  235. break;
  236. }
  237. }
  238. if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) {
  239. goto err;
  240. }
  241. /* step 8 */
  242. if (!BN_bin2bn(md, qsize, r0) ||
  243. !BN_lshift(r0, r0, (qsize << 3) * k) ||
  244. !BN_add(W, W, r0)) {
  245. goto err;
  246. }
  247. }
  248. /* more of step 8 */
  249. if (!BN_mask_bits(W, bits - 1) ||
  250. !BN_copy(X, W) ||
  251. !BN_add(X, X, test)) {
  252. goto err;
  253. }
  254. /* step 9 */
  255. if (!BN_lshift1(r0, q) ||
  256. !BN_mod(c, X, r0, ctx) ||
  257. !BN_sub(r0, c, BN_value_one()) ||
  258. !BN_sub(p, X, r0)) {
  259. goto err;
  260. }
  261. /* step 10 */
  262. if (BN_cmp(p, test) >= 0) {
  263. /* step 11 */
  264. r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
  265. if (r > 0) {
  266. goto end; /* found it */
  267. }
  268. if (r != 0) {
  269. goto err;
  270. }
  271. }
  272. /* step 13 */
  273. counter++;
  274. /* "offset = offset + n + 1" */
  275. /* step 14 */
  276. if (counter >= 4096) {
  277. break;
  278. }
  279. }
  280. }
  281. end:
  282. if (!BN_GENCB_call(cb, 2, 1)) {
  283. goto err;
  284. }
  285. /* We now need to generate g */
  286. /* Set r0=(p-1)/q */
  287. if (!BN_sub(test, p, BN_value_one()) ||
  288. !BN_div(r0, NULL, test, q, ctx)) {
  289. goto err;
  290. }
  291. if (!BN_set_word(test, h) ||
  292. !BN_MONT_CTX_set(mont, p, ctx)) {
  293. goto err;
  294. }
  295. for (;;) {
  296. /* g=test^r0%p */
  297. if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) {
  298. goto err;
  299. }
  300. if (!BN_is_one(g)) {
  301. break;
  302. }
  303. if (!BN_add(test, test, BN_value_one())) {
  304. goto err;
  305. }
  306. h++;
  307. }
  308. if (!BN_GENCB_call(cb, 3, 1)) {
  309. goto err;
  310. }
  311. ok = 1;
  312. err:
  313. if (ok) {
  314. BN_free(dsa->p);
  315. BN_free(dsa->q);
  316. BN_free(dsa->g);
  317. dsa->p = BN_dup(p);
  318. dsa->q = BN_dup(q);
  319. dsa->g = BN_dup(g);
  320. if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
  321. ok = 0;
  322. goto err;
  323. }
  324. if (out_counter != NULL) {
  325. *out_counter = counter;
  326. }
  327. if (out_h != NULL) {
  328. *out_h = h;
  329. }
  330. }
  331. if (ctx) {
  332. BN_CTX_end(ctx);
  333. BN_CTX_free(ctx);
  334. }
  335. BN_MONT_CTX_free(mont);
  336. return ok;
  337. }
  338. DSA *DSAparams_dup(const DSA *dsa) {
  339. DSA *ret = DSA_new();
  340. if (ret == NULL) {
  341. return NULL;
  342. }
  343. ret->p = BN_dup(dsa->p);
  344. ret->q = BN_dup(dsa->q);
  345. ret->g = BN_dup(dsa->g);
  346. if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
  347. DSA_free(ret);
  348. return NULL;
  349. }
  350. return ret;
  351. }
  352. int DSA_generate_key(DSA *dsa) {
  353. int ok = 0;
  354. BN_CTX *ctx = NULL;
  355. BIGNUM *pub_key = NULL, *priv_key = NULL;
  356. BIGNUM prk;
  357. ctx = BN_CTX_new();
  358. if (ctx == NULL) {
  359. goto err;
  360. }
  361. priv_key = dsa->priv_key;
  362. if (priv_key == NULL) {
  363. priv_key = BN_new();
  364. if (priv_key == NULL) {
  365. goto err;
  366. }
  367. }
  368. do {
  369. if (!BN_rand_range(priv_key, dsa->q)) {
  370. goto err;
  371. }
  372. } while (BN_is_zero(priv_key));
  373. pub_key = dsa->pub_key;
  374. if (pub_key == NULL) {
  375. pub_key = BN_new();
  376. if (pub_key == NULL) {
  377. goto err;
  378. }
  379. }
  380. BN_init(&prk);
  381. BN_with_flags(&prk, priv_key, BN_FLG_CONSTTIME);
  382. if (!BN_mod_exp(pub_key, dsa->g, &prk, dsa->p, ctx)) {
  383. goto err;
  384. }
  385. dsa->priv_key = priv_key;
  386. dsa->pub_key = pub_key;
  387. ok = 1;
  388. err:
  389. if (dsa->pub_key == NULL) {
  390. BN_free(pub_key);
  391. }
  392. if (dsa->priv_key == NULL) {
  393. BN_free(priv_key);
  394. }
  395. BN_CTX_free(ctx);
  396. return ok;
  397. }
  398. DSA_SIG *DSA_SIG_new(void) {
  399. DSA_SIG *sig;
  400. sig = OPENSSL_malloc(sizeof(DSA_SIG));
  401. if (!sig) {
  402. return NULL;
  403. }
  404. sig->r = NULL;
  405. sig->s = NULL;
  406. return sig;
  407. }
  408. void DSA_SIG_free(DSA_SIG *sig) {
  409. if (!sig) {
  410. return;
  411. }
  412. BN_free(sig->r);
  413. BN_free(sig->s);
  414. OPENSSL_free(sig);
  415. }
  416. DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
  417. BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
  418. BIGNUM m;
  419. BIGNUM xr;
  420. BN_CTX *ctx = NULL;
  421. int reason = ERR_R_BN_LIB;
  422. DSA_SIG *ret = NULL;
  423. int noredo = 0;
  424. BN_init(&m);
  425. BN_init(&xr);
  426. if (!dsa->p || !dsa->q || !dsa->g) {
  427. reason = DSA_R_MISSING_PARAMETERS;
  428. goto err;
  429. }
  430. s = BN_new();
  431. if (s == NULL) {
  432. goto err;
  433. }
  434. ctx = BN_CTX_new();
  435. if (ctx == NULL) {
  436. goto err;
  437. }
  438. redo:
  439. if (dsa->kinv == NULL || dsa->r == NULL) {
  440. if (!DSA_sign_setup(dsa, ctx, &kinv, &r)) {
  441. goto err;
  442. }
  443. } else {
  444. kinv = dsa->kinv;
  445. dsa->kinv = NULL;
  446. r = dsa->r;
  447. dsa->r = NULL;
  448. noredo = 1;
  449. }
  450. if (digest_len > BN_num_bytes(dsa->q)) {
  451. /* if the digest length is greater than the size of q use the
  452. * BN_num_bits(dsa->q) leftmost bits of the digest, see
  453. * fips 186-3, 4.2 */
  454. digest_len = BN_num_bytes(dsa->q);
  455. }
  456. if (BN_bin2bn(digest, digest_len, &m) == NULL) {
  457. goto err;
  458. }
  459. /* Compute s = inv(k) (m + xr) mod q */
  460. if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx)) {
  461. goto err; /* s = xr */
  462. }
  463. if (!BN_add(s, &xr, &m)) {
  464. goto err; /* s = m + xr */
  465. }
  466. if (BN_cmp(s, dsa->q) > 0) {
  467. if (!BN_sub(s, s, dsa->q)) {
  468. goto err;
  469. }
  470. }
  471. if (!BN_mod_mul(s, s, kinv, dsa->q, ctx)) {
  472. goto err;
  473. }
  474. /* Redo if r or s is zero as required by FIPS 186-3: this is
  475. * very unlikely. */
  476. if (BN_is_zero(r) || BN_is_zero(s)) {
  477. if (noredo) {
  478. reason = DSA_R_NEED_NEW_SETUP_VALUES;
  479. goto err;
  480. }
  481. goto redo;
  482. }
  483. ret = DSA_SIG_new();
  484. if (ret == NULL) {
  485. goto err;
  486. }
  487. ret->r = r;
  488. ret->s = s;
  489. err:
  490. if (ret == NULL) {
  491. OPENSSL_PUT_ERROR(DSA, reason);
  492. BN_free(r);
  493. BN_free(s);
  494. }
  495. BN_CTX_free(ctx);
  496. BN_clear_free(&m);
  497. BN_clear_free(&xr);
  498. BN_clear_free(kinv);
  499. return ret;
  500. }
  501. int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
  502. const DSA *dsa) {
  503. int valid;
  504. if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
  505. return -1;
  506. }
  507. return valid;
  508. }
  509. int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  510. size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
  511. BN_CTX *ctx;
  512. BIGNUM u1, u2, t1;
  513. BN_MONT_CTX *mont = NULL;
  514. int ret = 0;
  515. unsigned i;
  516. *out_valid = 0;
  517. if (!dsa->p || !dsa->q || !dsa->g) {
  518. OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
  519. return 0;
  520. }
  521. i = BN_num_bits(dsa->q);
  522. /* fips 186-3 allows only different sizes for q */
  523. if (i != 160 && i != 224 && i != 256) {
  524. OPENSSL_PUT_ERROR(DSA, DSA_R_BAD_Q_VALUE);
  525. return 0;
  526. }
  527. if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
  528. OPENSSL_PUT_ERROR(DSA, DSA_R_MODULUS_TOO_LARGE);
  529. return 0;
  530. }
  531. BN_init(&u1);
  532. BN_init(&u2);
  533. BN_init(&t1);
  534. ctx = BN_CTX_new();
  535. if (ctx == NULL) {
  536. goto err;
  537. }
  538. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  539. BN_ucmp(sig->r, dsa->q) >= 0) {
  540. ret = 1;
  541. goto err;
  542. }
  543. if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||
  544. BN_ucmp(sig->s, dsa->q) >= 0) {
  545. ret = 1;
  546. goto err;
  547. }
  548. /* Calculate W = inv(S) mod Q
  549. * save W in u2 */
  550. if (BN_mod_inverse(&u2, sig->s, dsa->q, ctx) == NULL) {
  551. goto err;
  552. }
  553. /* save M in u1 */
  554. if (digest_len > (i >> 3)) {
  555. /* if the digest length is greater than the size of q use the
  556. * BN_num_bits(dsa->q) leftmost bits of the digest, see
  557. * fips 186-3, 4.2 */
  558. digest_len = (i >> 3);
  559. }
  560. if (BN_bin2bn(digest, digest_len, &u1) == NULL) {
  561. goto err;
  562. }
  563. /* u1 = M * w mod q */
  564. if (!BN_mod_mul(&u1, &u1, &u2, dsa->q, ctx)) {
  565. goto err;
  566. }
  567. /* u2 = r * w mod q */
  568. if (!BN_mod_mul(&u2, sig->r, &u2, dsa->q, ctx)) {
  569. goto err;
  570. }
  571. mont = BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  572. (CRYPTO_MUTEX *)&dsa->method_mont_p_lock,
  573. dsa->p, ctx);
  574. if (!mont) {
  575. goto err;
  576. }
  577. if (!BN_mod_exp2_mont(&t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx,
  578. mont)) {
  579. goto err;
  580. }
  581. /* BN_copy(&u1,&t1); */
  582. /* let u1 = u1 mod q */
  583. if (!BN_mod(&u1, &t1, dsa->q, ctx)) {
  584. goto err;
  585. }
  586. /* V is now in u1. If the signature is correct, it will be
  587. * equal to R. */
  588. *out_valid = BN_ucmp(&u1, sig->r) == 0;
  589. ret = 1;
  590. err:
  591. if (ret != 1) {
  592. OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
  593. }
  594. BN_CTX_free(ctx);
  595. BN_free(&u1);
  596. BN_free(&u2);
  597. BN_free(&t1);
  598. return ret;
  599. }
  600. int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  601. uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
  602. DSA_SIG *s;
  603. s = DSA_do_sign(digest, digest_len, dsa);
  604. if (s == NULL) {
  605. *out_siglen = 0;
  606. return 0;
  607. }
  608. *out_siglen = i2d_DSA_SIG(s, &out_sig);
  609. DSA_SIG_free(s);
  610. return 1;
  611. }
  612. int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
  613. const uint8_t *sig, size_t sig_len, const DSA *dsa) {
  614. int valid;
  615. if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
  616. return -1;
  617. }
  618. return valid;
  619. }
  620. int DSA_check_signature(int *out_valid, const uint8_t *digest,
  621. size_t digest_len, const uint8_t *sig, size_t sig_len,
  622. const DSA *dsa) {
  623. DSA_SIG *s = NULL;
  624. int ret = 0;
  625. uint8_t *der = NULL;
  626. s = DSA_SIG_new();
  627. if (s == NULL) {
  628. goto err;
  629. }
  630. const uint8_t *sigp = sig;
  631. if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
  632. goto err;
  633. }
  634. /* Ensure that the signature uses DER and doesn't have trailing garbage. */
  635. int der_len = i2d_DSA_SIG(s, &der);
  636. if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) {
  637. goto err;
  638. }
  639. ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
  640. err:
  641. OPENSSL_free(der);
  642. DSA_SIG_free(s);
  643. return ret;
  644. }
  645. /* der_len_len returns the number of bytes needed to represent a length of |len|
  646. * in DER. */
  647. static size_t der_len_len(size_t len) {
  648. if (len < 0x80) {
  649. return 1;
  650. }
  651. size_t ret = 1;
  652. while (len > 0) {
  653. ret++;
  654. len >>= 8;
  655. }
  656. return ret;
  657. }
  658. int DSA_size(const DSA *dsa) {
  659. size_t order_len = BN_num_bytes(dsa->q);
  660. /* Compute the maximum length of an |order_len| byte integer. Defensively
  661. * assume that the leading 0x00 is included. */
  662. size_t integer_len = 1 /* tag */ + der_len_len(order_len + 1) + 1 + order_len;
  663. if (integer_len < order_len) {
  664. return 0;
  665. }
  666. /* A DSA signature is two INTEGERs. */
  667. size_t value_len = 2 * integer_len;
  668. if (value_len < integer_len) {
  669. return 0;
  670. }
  671. /* Add the header. */
  672. size_t ret = 1 /* tag */ + der_len_len(value_len) + value_len;
  673. if (ret < value_len) {
  674. return 0;
  675. }
  676. return ret;
  677. }
  678. int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv,
  679. BIGNUM **out_r) {
  680. BN_CTX *ctx;
  681. BIGNUM k, kq, *K, *kinv = NULL, *r = NULL;
  682. int ret = 0;
  683. if (!dsa->p || !dsa->q || !dsa->g) {
  684. OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
  685. return 0;
  686. }
  687. BN_init(&k);
  688. BN_init(&kq);
  689. ctx = ctx_in;
  690. if (ctx == NULL) {
  691. ctx = BN_CTX_new();
  692. if (ctx == NULL) {
  693. goto err;
  694. }
  695. }
  696. r = BN_new();
  697. if (r == NULL) {
  698. goto err;
  699. }
  700. /* Get random k */
  701. do {
  702. if (!BN_rand_range(&k, dsa->q)) {
  703. goto err;
  704. }
  705. } while (BN_is_zero(&k));
  706. BN_set_flags(&k, BN_FLG_CONSTTIME);
  707. if (BN_MONT_CTX_set_locked((BN_MONT_CTX **)&dsa->method_mont_p,
  708. (CRYPTO_MUTEX *)&dsa->method_mont_p_lock, dsa->p,
  709. ctx) == NULL) {
  710. goto err;
  711. }
  712. /* Compute r = (g^k mod p) mod q */
  713. if (!BN_copy(&kq, &k)) {
  714. goto err;
  715. }
  716. /* We do not want timing information to leak the length of k,
  717. * so we compute g^k using an equivalent exponent of fixed length.
  718. *
  719. * (This is a kludge that we need because the BN_mod_exp_mont()
  720. * does not let us specify the desired timing behaviour.) */
  721. if (!BN_add(&kq, &kq, dsa->q)) {
  722. goto err;
  723. }
  724. if (BN_num_bits(&kq) <= BN_num_bits(dsa->q) && !BN_add(&kq, &kq, dsa->q)) {
  725. goto err;
  726. }
  727. K = &kq;
  728. if (!BN_mod_exp_mont(r, dsa->g, K, dsa->p, ctx, dsa->method_mont_p)) {
  729. goto err;
  730. }
  731. if (!BN_mod(r, r, dsa->q, ctx)) {
  732. goto err;
  733. }
  734. /* Compute part of 's = inv(k) (m + xr) mod q' */
  735. kinv = BN_mod_inverse(NULL, &k, dsa->q, ctx);
  736. if (kinv == NULL) {
  737. goto err;
  738. }
  739. BN_clear_free(*out_kinv);
  740. *out_kinv = kinv;
  741. kinv = NULL;
  742. BN_clear_free(*out_r);
  743. *out_r = r;
  744. ret = 1;
  745. err:
  746. if (!ret) {
  747. OPENSSL_PUT_ERROR(DSA, ERR_R_BN_LIB);
  748. if (r != NULL) {
  749. BN_clear_free(r);
  750. }
  751. }
  752. if (ctx_in == NULL) {
  753. BN_CTX_free(ctx);
  754. }
  755. BN_clear_free(&k);
  756. BN_clear_free(&kq);
  757. return ret;
  758. }
  759. int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  760. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  761. int index;
  762. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
  763. free_func)) {
  764. return -1;
  765. }
  766. return index;
  767. }
  768. int DSA_set_ex_data(DSA *d, int idx, void *arg) {
  769. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  770. }
  771. void *DSA_get_ex_data(const DSA *d, int idx) {
  772. return CRYPTO_get_ex_data(&d->ex_data, idx);
  773. }
  774. DH *DSA_dup_DH(const DSA *r) {
  775. DH *ret = NULL;
  776. if (r == NULL) {
  777. goto err;
  778. }
  779. ret = DH_new();
  780. if (ret == NULL) {
  781. goto err;
  782. }
  783. if (r->q != NULL) {
  784. ret->priv_length = BN_num_bits(r->q);
  785. if ((ret->q = BN_dup(r->q)) == NULL) {
  786. goto err;
  787. }
  788. }
  789. if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
  790. (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
  791. (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
  792. (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
  793. goto err;
  794. }
  795. return ret;
  796. err:
  797. DH_free(ret);
  798. return NULL;
  799. }