Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. #include <openssl/dh.h>
  57. #include <string.h>
  58. #include <openssl/bn.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/err.h>
  61. #include <openssl/ex_data.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/thread.h>
  64. #include "../internal.h"
  65. #define OPENSSL_DH_MAX_MODULUS_BITS 10000
  66. static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  67. DH *DH_new(void) {
  68. DH *dh = OPENSSL_malloc(sizeof(DH));
  69. if (dh == NULL) {
  70. OPENSSL_PUT_ERROR(DH, ERR_R_MALLOC_FAILURE);
  71. return NULL;
  72. }
  73. OPENSSL_memset(dh, 0, sizeof(DH));
  74. CRYPTO_MUTEX_init(&dh->method_mont_p_lock);
  75. dh->references = 1;
  76. CRYPTO_new_ex_data(&dh->ex_data);
  77. return dh;
  78. }
  79. void DH_free(DH *dh) {
  80. if (dh == NULL) {
  81. return;
  82. }
  83. if (!CRYPTO_refcount_dec_and_test_zero(&dh->references)) {
  84. return;
  85. }
  86. CRYPTO_free_ex_data(&g_ex_data_class, dh, &dh->ex_data);
  87. BN_MONT_CTX_free(dh->method_mont_p);
  88. BN_clear_free(dh->p);
  89. BN_clear_free(dh->g);
  90. BN_clear_free(dh->q);
  91. BN_clear_free(dh->j);
  92. OPENSSL_free(dh->seed);
  93. BN_clear_free(dh->counter);
  94. BN_clear_free(dh->pub_key);
  95. BN_clear_free(dh->priv_key);
  96. CRYPTO_MUTEX_cleanup(&dh->method_mont_p_lock);
  97. OPENSSL_free(dh);
  98. }
  99. void DH_get0_key(const DH *dh, const BIGNUM **out_pub_key,
  100. const BIGNUM **out_priv_key) {
  101. if (out_pub_key != NULL) {
  102. *out_pub_key = dh->pub_key;
  103. }
  104. if (out_priv_key != NULL) {
  105. *out_priv_key = dh->priv_key;
  106. }
  107. }
  108. void DH_get0_pqg(const DH *dh, const BIGNUM **out_p, const BIGNUM **out_q,
  109. const BIGNUM **out_g) {
  110. if (out_p != NULL) {
  111. *out_p = dh->p;
  112. }
  113. if (out_q != NULL) {
  114. *out_q = dh->q;
  115. }
  116. if (out_g != NULL) {
  117. *out_g = dh->g;
  118. }
  119. }
  120. int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) {
  121. /* We generate DH parameters as follows
  122. * find a prime q which is prime_bits/2 bits long.
  123. * p=(2*q)+1 or (p-1)/2 = q
  124. * For this case, g is a generator if
  125. * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
  126. * Since the factors of p-1 are q and 2, we just need to check
  127. * g^2 mod p != 1 and g^q mod p != 1.
  128. *
  129. * Having said all that,
  130. * there is another special case method for the generators 2, 3 and 5.
  131. * for 2, p mod 24 == 11
  132. * for 3, p mod 12 == 5 <<<<< does not work for safe primes.
  133. * for 5, p mod 10 == 3 or 7
  134. *
  135. * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
  136. * special generators and for answering some of my questions.
  137. *
  138. * I've implemented the second simple method :-).
  139. * Since DH should be using a safe prime (both p and q are prime),
  140. * this generator function can take a very very long time to run.
  141. */
  142. /* Actually there is no reason to insist that 'generator' be a generator.
  143. * It's just as OK (and in some sense better) to use a generator of the
  144. * order-q subgroup.
  145. */
  146. BIGNUM *t1, *t2;
  147. int g, ok = 0;
  148. BN_CTX *ctx = NULL;
  149. ctx = BN_CTX_new();
  150. if (ctx == NULL) {
  151. goto err;
  152. }
  153. BN_CTX_start(ctx);
  154. t1 = BN_CTX_get(ctx);
  155. t2 = BN_CTX_get(ctx);
  156. if (t1 == NULL || t2 == NULL) {
  157. goto err;
  158. }
  159. /* Make sure |dh| has the necessary elements */
  160. if (dh->p == NULL) {
  161. dh->p = BN_new();
  162. if (dh->p == NULL) {
  163. goto err;
  164. }
  165. }
  166. if (dh->g == NULL) {
  167. dh->g = BN_new();
  168. if (dh->g == NULL) {
  169. goto err;
  170. }
  171. }
  172. if (generator <= 1) {
  173. OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
  174. goto err;
  175. }
  176. if (generator == DH_GENERATOR_2) {
  177. if (!BN_set_word(t1, 24)) {
  178. goto err;
  179. }
  180. if (!BN_set_word(t2, 11)) {
  181. goto err;
  182. }
  183. g = 2;
  184. } else if (generator == DH_GENERATOR_5) {
  185. if (!BN_set_word(t1, 10)) {
  186. goto err;
  187. }
  188. if (!BN_set_word(t2, 3)) {
  189. goto err;
  190. }
  191. /* BN_set_word(t3,7); just have to miss
  192. * out on these ones :-( */
  193. g = 5;
  194. } else {
  195. /* in the general case, don't worry if 'generator' is a
  196. * generator or not: since we are using safe primes,
  197. * it will generate either an order-q or an order-2q group,
  198. * which both is OK */
  199. if (!BN_set_word(t1, 2)) {
  200. goto err;
  201. }
  202. if (!BN_set_word(t2, 1)) {
  203. goto err;
  204. }
  205. g = generator;
  206. }
  207. if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
  208. goto err;
  209. }
  210. if (!BN_GENCB_call(cb, 3, 0)) {
  211. goto err;
  212. }
  213. if (!BN_set_word(dh->g, g)) {
  214. goto err;
  215. }
  216. ok = 1;
  217. err:
  218. if (!ok) {
  219. OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
  220. }
  221. if (ctx != NULL) {
  222. BN_CTX_end(ctx);
  223. BN_CTX_free(ctx);
  224. }
  225. return ok;
  226. }
  227. int DH_generate_key(DH *dh) {
  228. int ok = 0;
  229. int generate_new_key = 0;
  230. BN_CTX *ctx = NULL;
  231. BIGNUM *pub_key = NULL, *priv_key = NULL;
  232. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  233. OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
  234. goto err;
  235. }
  236. ctx = BN_CTX_new();
  237. if (ctx == NULL) {
  238. goto err;
  239. }
  240. if (dh->priv_key == NULL) {
  241. priv_key = BN_new();
  242. if (priv_key == NULL) {
  243. goto err;
  244. }
  245. generate_new_key = 1;
  246. } else {
  247. priv_key = dh->priv_key;
  248. }
  249. if (dh->pub_key == NULL) {
  250. pub_key = BN_new();
  251. if (pub_key == NULL) {
  252. goto err;
  253. }
  254. } else {
  255. pub_key = dh->pub_key;
  256. }
  257. if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
  258. dh->p, ctx)) {
  259. goto err;
  260. }
  261. if (generate_new_key) {
  262. if (dh->q) {
  263. if (!BN_rand_range_ex(priv_key, 2, dh->q)) {
  264. goto err;
  265. }
  266. } else {
  267. /* secret exponent length */
  268. unsigned priv_bits = dh->priv_length;
  269. if (priv_bits == 0) {
  270. const unsigned p_bits = BN_num_bits(dh->p);
  271. if (p_bits == 0) {
  272. goto err;
  273. }
  274. priv_bits = p_bits - 1;
  275. }
  276. if (!BN_rand(priv_key, priv_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) {
  277. goto err;
  278. }
  279. }
  280. }
  281. if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx,
  282. dh->method_mont_p)) {
  283. goto err;
  284. }
  285. dh->pub_key = pub_key;
  286. dh->priv_key = priv_key;
  287. ok = 1;
  288. err:
  289. if (ok != 1) {
  290. OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
  291. }
  292. if (dh->pub_key == NULL) {
  293. BN_free(pub_key);
  294. }
  295. if (dh->priv_key == NULL) {
  296. BN_free(priv_key);
  297. }
  298. BN_CTX_free(ctx);
  299. return ok;
  300. }
  301. int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
  302. BN_CTX *ctx = NULL;
  303. BIGNUM *shared_key;
  304. int ret = -1;
  305. int check_result;
  306. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  307. OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
  308. goto err;
  309. }
  310. ctx = BN_CTX_new();
  311. if (ctx == NULL) {
  312. goto err;
  313. }
  314. BN_CTX_start(ctx);
  315. shared_key = BN_CTX_get(ctx);
  316. if (shared_key == NULL) {
  317. goto err;
  318. }
  319. if (dh->priv_key == NULL) {
  320. OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
  321. goto err;
  322. }
  323. if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
  324. dh->p, ctx)) {
  325. goto err;
  326. }
  327. if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
  328. OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
  329. goto err;
  330. }
  331. if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p,
  332. ctx, dh->method_mont_p)) {
  333. OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
  334. goto err;
  335. }
  336. ret = BN_bn2bin(shared_key, out);
  337. err:
  338. if (ctx != NULL) {
  339. BN_CTX_end(ctx);
  340. BN_CTX_free(ctx);
  341. }
  342. return ret;
  343. }
  344. int DH_size(const DH *dh) { return BN_num_bytes(dh->p); }
  345. unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); }
  346. int DH_up_ref(DH *dh) {
  347. CRYPTO_refcount_inc(&dh->references);
  348. return 1;
  349. }
  350. static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
  351. BIGNUM *a = NULL;
  352. if (src) {
  353. a = BN_dup(src);
  354. if (!a) {
  355. return 0;
  356. }
  357. }
  358. BN_free(*dst);
  359. *dst = a;
  360. return 1;
  361. }
  362. static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
  363. if (is_x942 == -1) {
  364. is_x942 = !!from->q;
  365. }
  366. if (!int_dh_bn_cpy(&to->p, from->p) ||
  367. !int_dh_bn_cpy(&to->g, from->g)) {
  368. return 0;
  369. }
  370. if (!is_x942) {
  371. return 1;
  372. }
  373. if (!int_dh_bn_cpy(&to->q, from->q) ||
  374. !int_dh_bn_cpy(&to->j, from->j)) {
  375. return 0;
  376. }
  377. OPENSSL_free(to->seed);
  378. to->seed = NULL;
  379. to->seedlen = 0;
  380. if (from->seed) {
  381. to->seed = BUF_memdup(from->seed, from->seedlen);
  382. if (!to->seed) {
  383. return 0;
  384. }
  385. to->seedlen = from->seedlen;
  386. }
  387. return 1;
  388. }
  389. DH *DHparams_dup(const DH *dh) {
  390. DH *ret = DH_new();
  391. if (!ret) {
  392. return NULL;
  393. }
  394. if (!int_dh_param_copy(ret, dh, -1)) {
  395. DH_free(ret);
  396. return NULL;
  397. }
  398. return ret;
  399. }
  400. int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  401. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  402. int index;
  403. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
  404. free_func)) {
  405. return -1;
  406. }
  407. return index;
  408. }
  409. int DH_set_ex_data(DH *d, int idx, void *arg) {
  410. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  411. }
  412. void *DH_get_ex_data(DH *d, int idx) {
  413. return CRYPTO_get_ex_data(&d->ex_data, idx);
  414. }