Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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