dh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. BN_MONT_CTX *mont = NULL;
  213. BIGNUM *pub_key = NULL, *priv_key = NULL;
  214. BIGNUM local_priv;
  215. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  216. OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
  217. goto err;
  218. }
  219. ctx = BN_CTX_new();
  220. if (ctx == NULL) {
  221. goto err;
  222. }
  223. if (dh->priv_key == NULL) {
  224. priv_key = BN_new();
  225. if (priv_key == NULL) {
  226. goto err;
  227. }
  228. generate_new_key = 1;
  229. } else {
  230. priv_key = dh->priv_key;
  231. }
  232. if (dh->pub_key == NULL) {
  233. pub_key = BN_new();
  234. if (pub_key == NULL) {
  235. goto err;
  236. }
  237. } else {
  238. pub_key = dh->pub_key;
  239. }
  240. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
  241. dh->p, ctx);
  242. if (!mont) {
  243. goto err;
  244. }
  245. if (generate_new_key) {
  246. if (dh->q) {
  247. do {
  248. if (!BN_rand_range(priv_key, dh->q)) {
  249. goto err;
  250. }
  251. } while (BN_is_zero(priv_key) || BN_is_one(priv_key));
  252. } else {
  253. /* secret exponent length */
  254. DH_check_standard_parameters(dh);
  255. l = dh->priv_length ? dh->priv_length : BN_num_bits(dh->p) - 1;
  256. if (!BN_rand(priv_key, l, 0, 0)) {
  257. goto err;
  258. }
  259. }
  260. }
  261. BN_with_flags(&local_priv, priv_key, BN_FLG_CONSTTIME);
  262. if (!BN_mod_exp_mont(pub_key, dh->g, &local_priv, dh->p, ctx, mont)) {
  263. goto err;
  264. }
  265. dh->pub_key = pub_key;
  266. dh->priv_key = priv_key;
  267. ok = 1;
  268. err:
  269. if (ok != 1) {
  270. OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
  271. }
  272. if (dh->pub_key == NULL) {
  273. BN_free(pub_key);
  274. }
  275. if (dh->priv_key == NULL) {
  276. BN_free(priv_key);
  277. }
  278. BN_CTX_free(ctx);
  279. return ok;
  280. }
  281. int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
  282. BN_CTX *ctx = NULL;
  283. BN_MONT_CTX *mont = NULL;
  284. BIGNUM *shared_key;
  285. int ret = -1;
  286. int check_result;
  287. BIGNUM local_priv;
  288. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  289. OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
  290. goto err;
  291. }
  292. ctx = BN_CTX_new();
  293. if (ctx == NULL) {
  294. goto err;
  295. }
  296. BN_CTX_start(ctx);
  297. shared_key = BN_CTX_get(ctx);
  298. if (shared_key == NULL) {
  299. goto err;
  300. }
  301. if (dh->priv_key == NULL) {
  302. OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
  303. goto err;
  304. }
  305. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
  306. dh->p, ctx);
  307. if (!mont) {
  308. goto err;
  309. }
  310. if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
  311. OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
  312. goto err;
  313. }
  314. BN_with_flags(&local_priv, dh->priv_key, BN_FLG_CONSTTIME);
  315. if (!BN_mod_exp_mont(shared_key, peers_key, &local_priv, dh->p, ctx,
  316. mont)) {
  317. OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
  318. goto err;
  319. }
  320. ret = BN_bn2bin(shared_key, out);
  321. err:
  322. if (ctx != NULL) {
  323. BN_CTX_end(ctx);
  324. BN_CTX_free(ctx);
  325. }
  326. return ret;
  327. }
  328. int DH_size(const DH *dh) { return BN_num_bytes(dh->p); }
  329. unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); }
  330. int DH_up_ref(DH *dh) {
  331. CRYPTO_refcount_inc(&dh->references);
  332. return 1;
  333. }
  334. static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
  335. BIGNUM *a = NULL;
  336. if (src) {
  337. a = BN_dup(src);
  338. if (!a) {
  339. return 0;
  340. }
  341. }
  342. BN_free(*dst);
  343. *dst = a;
  344. return 1;
  345. }
  346. static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
  347. if (is_x942 == -1) {
  348. is_x942 = !!from->q;
  349. }
  350. if (!int_dh_bn_cpy(&to->p, from->p) ||
  351. !int_dh_bn_cpy(&to->g, from->g)) {
  352. return 0;
  353. }
  354. if (!is_x942) {
  355. return 1;
  356. }
  357. if (!int_dh_bn_cpy(&to->q, from->q) ||
  358. !int_dh_bn_cpy(&to->j, from->j)) {
  359. return 0;
  360. }
  361. OPENSSL_free(to->seed);
  362. to->seed = NULL;
  363. to->seedlen = 0;
  364. if (from->seed) {
  365. to->seed = BUF_memdup(from->seed, from->seedlen);
  366. if (!to->seed) {
  367. return 0;
  368. }
  369. to->seedlen = from->seedlen;
  370. }
  371. return 1;
  372. }
  373. DH *DHparams_dup(const DH *dh) {
  374. DH *ret = DH_new();
  375. if (!ret) {
  376. return NULL;
  377. }
  378. if (!int_dh_param_copy(ret, dh, -1)) {
  379. DH_free(ret);
  380. return NULL;
  381. }
  382. return ret;
  383. }
  384. int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  385. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  386. int index;
  387. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
  388. free_func)) {
  389. return -1;
  390. }
  391. return index;
  392. }
  393. int DH_set_ex_data(DH *d, int idx, void *arg) {
  394. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  395. }
  396. void *DH_get_ex_data(DH *d, int idx) {
  397. return CRYPTO_get_ex_data(&d->ex_data, idx);
  398. }