You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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