25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

482 satır
12 KiB

  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. // Actually there is no reason to insist that 'generator' be a generator.
  142. // It's just as OK (and in some sense better) to use a generator of the
  143. // order-q subgroup.
  144. BIGNUM *t1, *t2;
  145. int g, ok = 0;
  146. BN_CTX *ctx = NULL;
  147. ctx = BN_CTX_new();
  148. if (ctx == NULL) {
  149. goto err;
  150. }
  151. BN_CTX_start(ctx);
  152. t1 = BN_CTX_get(ctx);
  153. t2 = BN_CTX_get(ctx);
  154. if (t1 == NULL || t2 == NULL) {
  155. goto err;
  156. }
  157. // Make sure |dh| has the necessary elements
  158. if (dh->p == NULL) {
  159. dh->p = BN_new();
  160. if (dh->p == NULL) {
  161. goto err;
  162. }
  163. }
  164. if (dh->g == NULL) {
  165. dh->g = BN_new();
  166. if (dh->g == NULL) {
  167. goto err;
  168. }
  169. }
  170. if (generator <= 1) {
  171. OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
  172. goto err;
  173. }
  174. if (generator == DH_GENERATOR_2) {
  175. if (!BN_set_word(t1, 24)) {
  176. goto err;
  177. }
  178. if (!BN_set_word(t2, 11)) {
  179. goto err;
  180. }
  181. g = 2;
  182. } else if (generator == DH_GENERATOR_5) {
  183. if (!BN_set_word(t1, 10)) {
  184. goto err;
  185. }
  186. if (!BN_set_word(t2, 3)) {
  187. goto err;
  188. }
  189. // BN_set_word(t3,7); just have to miss
  190. // out on these ones :-(
  191. g = 5;
  192. } else {
  193. // in the general case, don't worry if 'generator' is a
  194. // generator or not: since we are using safe primes,
  195. // it will generate either an order-q or an order-2q group,
  196. // which both is OK
  197. if (!BN_set_word(t1, 2)) {
  198. goto err;
  199. }
  200. if (!BN_set_word(t2, 1)) {
  201. goto err;
  202. }
  203. g = generator;
  204. }
  205. if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
  206. goto err;
  207. }
  208. if (!BN_GENCB_call(cb, 3, 0)) {
  209. goto err;
  210. }
  211. if (!BN_set_word(dh->g, g)) {
  212. goto err;
  213. }
  214. ok = 1;
  215. err:
  216. if (!ok) {
  217. OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
  218. }
  219. if (ctx != NULL) {
  220. BN_CTX_end(ctx);
  221. BN_CTX_free(ctx);
  222. }
  223. return ok;
  224. }
  225. int DH_generate_key(DH *dh) {
  226. int ok = 0;
  227. int generate_new_key = 0;
  228. BN_CTX *ctx = NULL;
  229. BIGNUM *pub_key = NULL, *priv_key = NULL;
  230. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  231. OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
  232. goto err;
  233. }
  234. ctx = BN_CTX_new();
  235. if (ctx == NULL) {
  236. goto err;
  237. }
  238. if (dh->priv_key == NULL) {
  239. priv_key = BN_new();
  240. if (priv_key == NULL) {
  241. goto err;
  242. }
  243. generate_new_key = 1;
  244. } else {
  245. priv_key = dh->priv_key;
  246. }
  247. if (dh->pub_key == NULL) {
  248. pub_key = BN_new();
  249. if (pub_key == NULL) {
  250. goto err;
  251. }
  252. } else {
  253. pub_key = dh->pub_key;
  254. }
  255. if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
  256. dh->p, ctx)) {
  257. goto err;
  258. }
  259. if (generate_new_key) {
  260. if (dh->q) {
  261. if (!BN_rand_range_ex(priv_key, 2, dh->q)) {
  262. goto err;
  263. }
  264. } else {
  265. // secret exponent length
  266. unsigned priv_bits = dh->priv_length;
  267. if (priv_bits == 0) {
  268. const unsigned p_bits = BN_num_bits(dh->p);
  269. if (p_bits == 0) {
  270. goto err;
  271. }
  272. priv_bits = p_bits - 1;
  273. }
  274. if (!BN_rand(priv_key, priv_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) {
  275. goto err;
  276. }
  277. }
  278. }
  279. if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx,
  280. dh->method_mont_p)) {
  281. goto err;
  282. }
  283. dh->pub_key = pub_key;
  284. dh->priv_key = priv_key;
  285. ok = 1;
  286. err:
  287. if (ok != 1) {
  288. OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
  289. }
  290. if (dh->pub_key == NULL) {
  291. BN_free(pub_key);
  292. }
  293. if (dh->priv_key == NULL) {
  294. BN_free(priv_key);
  295. }
  296. BN_CTX_free(ctx);
  297. return ok;
  298. }
  299. int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
  300. BN_CTX *ctx = NULL;
  301. BIGNUM *shared_key;
  302. int ret = -1;
  303. int check_result;
  304. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  305. OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
  306. goto err;
  307. }
  308. ctx = BN_CTX_new();
  309. if (ctx == NULL) {
  310. goto err;
  311. }
  312. BN_CTX_start(ctx);
  313. shared_key = BN_CTX_get(ctx);
  314. if (shared_key == NULL) {
  315. goto err;
  316. }
  317. if (dh->priv_key == NULL) {
  318. OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
  319. goto err;
  320. }
  321. if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
  322. dh->p, ctx)) {
  323. goto err;
  324. }
  325. if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
  326. OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
  327. goto err;
  328. }
  329. if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p,
  330. ctx, dh->method_mont_p)) {
  331. OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
  332. goto err;
  333. }
  334. ret = BN_bn2bin(shared_key, out);
  335. err:
  336. if (ctx != NULL) {
  337. BN_CTX_end(ctx);
  338. BN_CTX_free(ctx);
  339. }
  340. return ret;
  341. }
  342. int DH_size(const DH *dh) { return BN_num_bytes(dh->p); }
  343. unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); }
  344. int DH_up_ref(DH *dh) {
  345. CRYPTO_refcount_inc(&dh->references);
  346. return 1;
  347. }
  348. static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
  349. BIGNUM *a = NULL;
  350. if (src) {
  351. a = BN_dup(src);
  352. if (!a) {
  353. return 0;
  354. }
  355. }
  356. BN_free(*dst);
  357. *dst = a;
  358. return 1;
  359. }
  360. static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
  361. if (is_x942 == -1) {
  362. is_x942 = !!from->q;
  363. }
  364. if (!int_dh_bn_cpy(&to->p, from->p) ||
  365. !int_dh_bn_cpy(&to->g, from->g)) {
  366. return 0;
  367. }
  368. if (!is_x942) {
  369. return 1;
  370. }
  371. if (!int_dh_bn_cpy(&to->q, from->q) ||
  372. !int_dh_bn_cpy(&to->j, from->j)) {
  373. return 0;
  374. }
  375. OPENSSL_free(to->seed);
  376. to->seed = NULL;
  377. to->seedlen = 0;
  378. if (from->seed) {
  379. to->seed = BUF_memdup(from->seed, from->seedlen);
  380. if (!to->seed) {
  381. return 0;
  382. }
  383. to->seedlen = from->seedlen;
  384. }
  385. return 1;
  386. }
  387. DH *DHparams_dup(const DH *dh) {
  388. DH *ret = DH_new();
  389. if (!ret) {
  390. return NULL;
  391. }
  392. if (!int_dh_param_copy(ret, dh, -1)) {
  393. DH_free(ret);
  394. return NULL;
  395. }
  396. return ret;
  397. }
  398. int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  399. CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
  400. int index;
  401. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  402. free_func)) {
  403. return -1;
  404. }
  405. return index;
  406. }
  407. int DH_set_ex_data(DH *d, int idx, void *arg) {
  408. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  409. }
  410. void *DH_get_ex_data(DH *d, int idx) {
  411. return CRYPTO_get_ex_data(&d->ex_data, idx);
  412. }