Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

327 wiersze
9.1 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 <openssl/bn.h>
  58. #include <openssl/err.h>
  59. #include <openssl/thread.h>
  60. #include "internal.h"
  61. #define OPENSSL_DH_MAX_MODULUS_BITS 10000
  62. static int generate_parameters(DH *ret, int prime_bits, int generator, BN_GENCB *cb) {
  63. /* We generate DH parameters as follows
  64. * find a prime q which is prime_bits/2 bits long.
  65. * p=(2*q)+1 or (p-1)/2 = q
  66. * For this case, g is a generator if
  67. * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
  68. * Since the factors of p-1 are q and 2, we just need to check
  69. * g^2 mod p != 1 and g^q mod p != 1.
  70. *
  71. * Having said all that,
  72. * there is another special case method for the generators 2, 3 and 5.
  73. * for 2, p mod 24 == 11
  74. * for 3, p mod 12 == 5 <<<<< does not work for safe primes.
  75. * for 5, p mod 10 == 3 or 7
  76. *
  77. * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
  78. * special generators and for answering some of my questions.
  79. *
  80. * I've implemented the second simple method :-).
  81. * Since DH should be using a safe prime (both p and q are prime),
  82. * this generator function can take a very very long time to run.
  83. */
  84. /* Actually there is no reason to insist that 'generator' be a generator.
  85. * It's just as OK (and in some sense better) to use a generator of the
  86. * order-q subgroup.
  87. */
  88. BIGNUM *t1, *t2;
  89. int g, ok = 0;
  90. BN_CTX *ctx = NULL;
  91. ctx = BN_CTX_new();
  92. if (ctx == NULL) {
  93. goto err;
  94. }
  95. BN_CTX_start(ctx);
  96. t1 = BN_CTX_get(ctx);
  97. t2 = BN_CTX_get(ctx);
  98. if (t1 == NULL || t2 == NULL) {
  99. goto err;
  100. }
  101. /* Make sure 'ret' has the necessary elements */
  102. if (!ret->p && ((ret->p = BN_new()) == NULL)) {
  103. goto err;
  104. }
  105. if (!ret->g && ((ret->g = BN_new()) == NULL)) {
  106. goto err;
  107. }
  108. if (generator <= 1) {
  109. OPENSSL_PUT_ERROR(DH, generate_parameters, DH_R_BAD_GENERATOR);
  110. goto err;
  111. }
  112. if (generator == DH_GENERATOR_2) {
  113. if (!BN_set_word(t1, 24)) {
  114. goto err;
  115. }
  116. if (!BN_set_word(t2, 11)) {
  117. goto err;
  118. }
  119. g = 2;
  120. } else if (generator == DH_GENERATOR_5) {
  121. if (!BN_set_word(t1, 10)) {
  122. goto err;
  123. }
  124. if (!BN_set_word(t2, 3)) {
  125. goto err;
  126. }
  127. /* BN_set_word(t3,7); just have to miss
  128. * out on these ones :-( */
  129. g = 5;
  130. } else {
  131. /* in the general case, don't worry if 'generator' is a
  132. * generator or not: since we are using safe primes,
  133. * it will generate either an order-q or an order-2q group,
  134. * which both is OK */
  135. if (!BN_set_word(t1, 2)) {
  136. goto err;
  137. }
  138. if (!BN_set_word(t2, 1)) {
  139. goto err;
  140. }
  141. g = generator;
  142. }
  143. if (!BN_generate_prime_ex(ret->p, prime_bits, 1, t1, t2, cb)) {
  144. goto err;
  145. }
  146. if (!BN_GENCB_call(cb, 3, 0)) {
  147. goto err;
  148. }
  149. if (!BN_set_word(ret->g, g)) {
  150. goto err;
  151. }
  152. ok = 1;
  153. err:
  154. if (!ok) {
  155. OPENSSL_PUT_ERROR(DH, generate_parameters, ERR_R_BN_LIB);
  156. }
  157. if (ctx != NULL) {
  158. BN_CTX_end(ctx);
  159. BN_CTX_free(ctx);
  160. }
  161. return ok;
  162. }
  163. static int generate_key(DH *dh) {
  164. int ok = 0;
  165. int generate_new_key = 0;
  166. unsigned l;
  167. BN_CTX *ctx;
  168. BN_MONT_CTX *mont = NULL;
  169. BIGNUM *pub_key = NULL, *priv_key = NULL;
  170. BIGNUM local_priv;
  171. ctx = BN_CTX_new();
  172. if (ctx == NULL) {
  173. goto err;
  174. }
  175. if (dh->priv_key == NULL) {
  176. priv_key = BN_new();
  177. if (priv_key == NULL) {
  178. goto err;
  179. }
  180. generate_new_key = 1;
  181. } else {
  182. priv_key = dh->priv_key;
  183. }
  184. if (dh->pub_key == NULL) {
  185. pub_key = BN_new();
  186. if (pub_key == NULL) {
  187. goto err;
  188. }
  189. } else {
  190. pub_key = dh->pub_key;
  191. }
  192. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
  193. dh->p, ctx);
  194. if (!mont) {
  195. goto err;
  196. }
  197. if (generate_new_key) {
  198. if (dh->q) {
  199. do {
  200. if (!BN_rand_range(priv_key, dh->q)) {
  201. goto err;
  202. }
  203. } while (BN_is_zero(priv_key) || BN_is_one(priv_key));
  204. } else {
  205. /* secret exponent length */
  206. DH_check_standard_parameters(dh);
  207. l = dh->priv_length ? dh->priv_length : BN_num_bits(dh->p) - 1;
  208. if (!BN_rand(priv_key, l, 0, 0)) {
  209. goto err;
  210. }
  211. }
  212. }
  213. BN_with_flags(&local_priv, priv_key, BN_FLG_CONSTTIME);
  214. if (!BN_mod_exp_mont(pub_key, dh->g, &local_priv, dh->p, ctx, mont)) {
  215. goto err;
  216. }
  217. dh->pub_key = pub_key;
  218. dh->priv_key = priv_key;
  219. ok = 1;
  220. err:
  221. if (ok != 1) {
  222. OPENSSL_PUT_ERROR(DH, generate_key, ERR_R_BN_LIB);
  223. }
  224. if (pub_key != NULL && dh->pub_key == NULL) {
  225. BN_free(pub_key);
  226. }
  227. if (priv_key != NULL && dh->priv_key == NULL) {
  228. BN_free(priv_key);
  229. }
  230. BN_CTX_free(ctx);
  231. return ok;
  232. }
  233. static int compute_key(DH *dh, unsigned char *out, const BIGNUM *pub_key) {
  234. BN_CTX *ctx = NULL;
  235. BN_MONT_CTX *mont = NULL;
  236. BIGNUM *shared_key;
  237. int ret = -1;
  238. int check_result;
  239. BIGNUM local_priv;
  240. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  241. OPENSSL_PUT_ERROR(DH, compute_key, DH_R_MODULUS_TOO_LARGE);
  242. goto err;
  243. }
  244. ctx = BN_CTX_new();
  245. if (ctx == NULL) {
  246. goto err;
  247. }
  248. BN_CTX_start(ctx);
  249. shared_key = BN_CTX_get(ctx);
  250. if (shared_key == NULL) {
  251. goto err;
  252. }
  253. if (dh->priv_key == NULL) {
  254. OPENSSL_PUT_ERROR(DH, compute_key, DH_R_NO_PRIVATE_VALUE);
  255. goto err;
  256. }
  257. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
  258. dh->p, ctx);
  259. if (!mont) {
  260. goto err;
  261. }
  262. if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
  263. OPENSSL_PUT_ERROR(DH, compute_key, DH_R_INVALID_PUBKEY);
  264. goto err;
  265. }
  266. BN_with_flags(&local_priv, dh->priv_key, BN_FLG_CONSTTIME);
  267. if (!BN_mod_exp_mont(shared_key, pub_key, &local_priv, dh->p, ctx,
  268. mont)) {
  269. OPENSSL_PUT_ERROR(DH, compute_key, ERR_R_BN_LIB);
  270. goto err;
  271. }
  272. ret = BN_bn2bin(shared_key, out);
  273. err:
  274. if (ctx != NULL) {
  275. BN_CTX_end(ctx);
  276. BN_CTX_free(ctx);
  277. }
  278. return ret;
  279. }
  280. const struct dh_method DH_default_method = {
  281. {
  282. 0 /* references */,
  283. 1 /* is_static */,
  284. },
  285. NULL /* app_data */,
  286. NULL /* init */,
  287. NULL /* finish */,
  288. generate_parameters,
  289. generate_key,
  290. compute_key,
  291. };