dh_gen.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * NB: These functions have been upgraded - the previous prototypes are in
  11. * dh_depr.c as wrappers to these ones. - Geoff
  12. */
  13. /*
  14. * DH low level APIs are deprecated for public use, but still ok for
  15. * internal use.
  16. *
  17. * NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
  18. * states that no additional pairwise tests are required (apart from the tests
  19. * specified in SP800-56A) when generating keys. Hence DH pairwise tests are
  20. * omitted here.
  21. */
  22. #include "internal/deprecated.h"
  23. #include <stdio.h>
  24. #include "internal/cryptlib.h"
  25. #include <openssl/bn.h>
  26. #include <openssl/sha.h>
  27. #include "crypto/dh.h"
  28. #include "dh_local.h"
  29. #ifndef FIPS_MODULE
  30. static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
  31. BN_GENCB *cb);
  32. #endif /* FIPS_MODULE */
  33. int dh_generate_ffc_parameters(DH *dh, int type, int pbits, int qbits,
  34. BN_GENCB *cb)
  35. {
  36. int ret, res;
  37. #ifndef FIPS_MODULE
  38. if (type == DH_PARAMGEN_TYPE_FIPS_186_2)
  39. ret = ossl_ffc_params_FIPS186_2_generate(dh->libctx, &dh->params,
  40. FFC_PARAM_TYPE_DH,
  41. pbits, qbits, &res, cb);
  42. else
  43. #endif
  44. ret = ossl_ffc_params_FIPS186_4_generate(dh->libctx, &dh->params,
  45. FFC_PARAM_TYPE_DH,
  46. pbits, qbits, &res, cb);
  47. if (ret > 0)
  48. dh->dirty_cnt++;
  49. return ret;
  50. }
  51. int dh_get_named_group_uid_from_size(int pbits)
  52. {
  53. /*
  54. * Just choose an approved safe prime group.
  55. * The alternative to this is to generate FIPS186-4 domain parameters i.e.
  56. * return dh_generate_ffc_parameters(ret, prime_len, 0, NULL, cb);
  57. * As the FIPS186-4 generated params are for backwards compatibility,
  58. * the safe prime group should be used as the default.
  59. */
  60. int nid;
  61. switch (pbits) {
  62. case 2048:
  63. nid = NID_ffdhe2048;
  64. break;
  65. case 3072:
  66. nid = NID_ffdhe3072;
  67. break;
  68. case 4096:
  69. nid = NID_ffdhe4096;
  70. break;
  71. case 6144:
  72. nid = NID_ffdhe6144;
  73. break;
  74. case 8192:
  75. nid = NID_ffdhe8192;
  76. break;
  77. /* unsupported prime_len */
  78. default:
  79. return NID_undef;
  80. }
  81. return nid;
  82. }
  83. #ifdef FIPS_MODULE
  84. static int dh_gen_named_group(OSSL_LIB_CTX *libctx, DH *ret, int prime_len)
  85. {
  86. DH *dh;
  87. int ok = 0;
  88. int nid = dh_get_named_group_uid_from_size(prime_len);
  89. if (nid == NID_undef)
  90. return 0;
  91. dh = dh_new_by_nid_ex(libctx, nid);
  92. if (dh != NULL
  93. && ossl_ffc_params_copy(&ret->params, &dh->params)) {
  94. ok = 1;
  95. ret->dirty_cnt++;
  96. }
  97. DH_free(dh);
  98. return ok;
  99. }
  100. #endif /* FIPS_MODULE */
  101. int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
  102. BN_GENCB *cb)
  103. {
  104. #ifdef FIPS_MODULE
  105. if (generator != 2)
  106. return 0;
  107. return dh_gen_named_group(ret->libctx, ret, prime_len);
  108. #else
  109. if (ret->meth->generate_params)
  110. return ret->meth->generate_params(ret, prime_len, generator, cb);
  111. return dh_builtin_genparams(ret, prime_len, generator, cb);
  112. #endif /* FIPS_MODULE */
  113. }
  114. #ifndef FIPS_MODULE
  115. /*-
  116. * We generate DH parameters as follows
  117. * find a prime p which is prime_len bits long,
  118. * where q=(p-1)/2 is also prime.
  119. * In the following we assume that g is not 0, 1 or p-1, since it
  120. * would generate only trivial subgroups.
  121. * For this case, g is a generator of the order-q subgroup if
  122. * g^q mod p == 1.
  123. * Or in terms of the Legendre symbol: (g/p) == 1.
  124. *
  125. * Having said all that,
  126. * there is another special case method for the generators 2, 3 and 5.
  127. * Using the quadratic reciprocity law it is possible to solve
  128. * (g/p) == 1 for the special values 2, 3, 5:
  129. * (2/p) == 1 if p mod 8 == 1 or 7.
  130. * (3/p) == 1 if p mod 12 == 1 or 11.
  131. * (5/p) == 1 if p mod 5 == 1 or 4.
  132. * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol
  133. *
  134. * Since all safe primes > 7 must satisfy p mod 12 == 11
  135. * and all safe primes > 11 must satisfy p mod 5 != 1
  136. * we can further improve the condition for g = 2, 3 and 5:
  137. * for 2, p mod 24 == 23
  138. * for 3, p mod 12 == 11
  139. * for 5, p mod 60 == 59
  140. */
  141. static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
  142. BN_GENCB *cb)
  143. {
  144. BIGNUM *t1, *t2;
  145. int g, ok = -1;
  146. BN_CTX *ctx = NULL;
  147. if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) {
  148. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  149. return 0;
  150. }
  151. if (prime_len < DH_MIN_MODULUS_BITS) {
  152. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  153. return 0;
  154. }
  155. ctx = BN_CTX_new();
  156. if (ctx == NULL)
  157. goto err;
  158. BN_CTX_start(ctx);
  159. t1 = BN_CTX_get(ctx);
  160. t2 = BN_CTX_get(ctx);
  161. if (t2 == NULL)
  162. goto err;
  163. /* Make sure 'ret' has the necessary elements */
  164. if (ret->params.p == NULL && ((ret->params.p = BN_new()) == NULL))
  165. goto err;
  166. if (ret->params.g == NULL && ((ret->params.g = BN_new()) == NULL))
  167. goto err;
  168. if (generator <= 1) {
  169. ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
  170. goto err;
  171. }
  172. if (generator == DH_GENERATOR_2) {
  173. if (!BN_set_word(t1, 24))
  174. goto err;
  175. if (!BN_set_word(t2, 23))
  176. goto err;
  177. g = 2;
  178. } else if (generator == DH_GENERATOR_5) {
  179. if (!BN_set_word(t1, 60))
  180. goto err;
  181. if (!BN_set_word(t2, 59))
  182. goto err;
  183. g = 5;
  184. } else {
  185. /*
  186. * in the general case, don't worry if 'generator' is a generator or
  187. * not: since we are using safe primes, it will generate either an
  188. * order-q or an order-2q group, which both is OK
  189. */
  190. if (!BN_set_word(t1, 12))
  191. goto err;
  192. if (!BN_set_word(t2, 11))
  193. goto err;
  194. g = generator;
  195. }
  196. if (!BN_generate_prime_ex(ret->params.p, prime_len, 1, t1, t2, cb))
  197. goto err;
  198. if (!BN_GENCB_call(cb, 3, 0))
  199. goto err;
  200. if (!BN_set_word(ret->params.g, g))
  201. goto err;
  202. ret->dirty_cnt++;
  203. ok = 1;
  204. err:
  205. if (ok == -1) {
  206. ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
  207. ok = 0;
  208. }
  209. BN_CTX_end(ctx);
  210. BN_CTX_free(ctx);
  211. return ok;
  212. }
  213. #endif /* FIPS_MODULE */