No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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