2
0

dh_gen.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 1995-2021 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 "crypto/security_bits.h"
  29. #include "dh_local.h"
  30. #ifndef FIPS_MODULE
  31. static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
  32. BN_GENCB *cb);
  33. #endif /* FIPS_MODULE */
  34. int ossl_dh_generate_ffc_parameters(DH *dh, int type, int pbits, int qbits,
  35. BN_GENCB *cb)
  36. {
  37. int ret, res;
  38. #ifndef FIPS_MODULE
  39. if (type == DH_PARAMGEN_TYPE_FIPS_186_2)
  40. ret = ossl_ffc_params_FIPS186_2_generate(dh->libctx, &dh->params,
  41. FFC_PARAM_TYPE_DH,
  42. pbits, qbits, &res, cb);
  43. else
  44. #endif
  45. ret = ossl_ffc_params_FIPS186_4_generate(dh->libctx, &dh->params,
  46. FFC_PARAM_TYPE_DH,
  47. pbits, qbits, &res, cb);
  48. if (ret > 0)
  49. dh->dirty_cnt++;
  50. return ret;
  51. }
  52. int ossl_dh_get_named_group_uid_from_size(int pbits)
  53. {
  54. /*
  55. * Just choose an approved safe prime group.
  56. * The alternative to this is to generate FIPS186-4 domain parameters i.e.
  57. * return dh_generate_ffc_parameters(ret, prime_len, 0, NULL, cb);
  58. * As the FIPS186-4 generated params are for backwards compatibility,
  59. * the safe prime group should be used as the default.
  60. */
  61. int nid;
  62. switch (pbits) {
  63. case 2048:
  64. nid = NID_ffdhe2048;
  65. break;
  66. case 3072:
  67. nid = NID_ffdhe3072;
  68. break;
  69. case 4096:
  70. nid = NID_ffdhe4096;
  71. break;
  72. case 6144:
  73. nid = NID_ffdhe6144;
  74. break;
  75. case 8192:
  76. nid = NID_ffdhe8192;
  77. break;
  78. /* unsupported prime_len */
  79. default:
  80. return NID_undef;
  81. }
  82. return nid;
  83. }
  84. #ifdef FIPS_MODULE
  85. static int dh_gen_named_group(OSSL_LIB_CTX *libctx, DH *ret, int prime_len)
  86. {
  87. DH *dh;
  88. int ok = 0;
  89. int nid = ossl_dh_get_named_group_uid_from_size(prime_len);
  90. if (nid == NID_undef)
  91. return 0;
  92. dh = ossl_dh_new_by_nid_ex(libctx, nid);
  93. if (dh != NULL
  94. && ossl_ffc_params_copy(&ret->params, &dh->params)) {
  95. ok = 1;
  96. ret->dirty_cnt++;
  97. }
  98. DH_free(dh);
  99. return ok;
  100. }
  101. #endif /* FIPS_MODULE */
  102. int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
  103. BN_GENCB *cb)
  104. {
  105. #ifdef FIPS_MODULE
  106. if (generator != 2)
  107. return 0;
  108. return dh_gen_named_group(ret->libctx, ret, prime_len);
  109. #else
  110. if (ret->meth->generate_params)
  111. return ret->meth->generate_params(ret, prime_len, generator, cb);
  112. return dh_builtin_genparams(ret, prime_len, generator, cb);
  113. #endif /* FIPS_MODULE */
  114. }
  115. #ifndef FIPS_MODULE
  116. /*-
  117. * We generate DH parameters as follows
  118. * find a prime p which is prime_len bits long,
  119. * where q=(p-1)/2 is also prime.
  120. * In the following we assume that g is not 0, 1 or p-1, since it
  121. * would generate only trivial subgroups.
  122. * For this case, g is a generator of the order-q subgroup if
  123. * g^q mod p == 1.
  124. * Or in terms of the Legendre symbol: (g/p) == 1.
  125. *
  126. * Having said all that,
  127. * there is another special case method for the generators 2, 3 and 5.
  128. * Using the quadratic reciprocity law it is possible to solve
  129. * (g/p) == 1 for the special values 2, 3, 5:
  130. * (2/p) == 1 if p mod 8 == 1 or 7.
  131. * (3/p) == 1 if p mod 12 == 1 or 11.
  132. * (5/p) == 1 if p mod 5 == 1 or 4.
  133. * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol
  134. *
  135. * Since all safe primes > 7 must satisfy p mod 12 == 11
  136. * and all safe primes > 11 must satisfy p mod 5 != 1
  137. * we can further improve the condition for g = 2, 3 and 5:
  138. * for 2, p mod 24 == 23
  139. * for 3, p mod 12 == 11
  140. * for 5, p mod 60 == 59
  141. */
  142. static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
  143. BN_GENCB *cb)
  144. {
  145. BIGNUM *t1, *t2;
  146. int g, ok = -1;
  147. BN_CTX *ctx = NULL;
  148. if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) {
  149. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  150. return 0;
  151. }
  152. if (prime_len < DH_MIN_MODULUS_BITS) {
  153. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  154. return 0;
  155. }
  156. ctx = BN_CTX_new_ex(ret->libctx);
  157. if (ctx == NULL)
  158. goto err;
  159. BN_CTX_start(ctx);
  160. t1 = BN_CTX_get(ctx);
  161. t2 = BN_CTX_get(ctx);
  162. if (t2 == NULL)
  163. goto err;
  164. /* Make sure 'ret' has the necessary elements */
  165. if (ret->params.p == NULL && ((ret->params.p = BN_new()) == NULL))
  166. goto err;
  167. if (ret->params.g == NULL && ((ret->params.g = BN_new()) == NULL))
  168. goto err;
  169. if (generator <= 1) {
  170. ERR_raise(ERR_LIB_DH, DH_R_BAD_GENERATOR);
  171. goto err;
  172. }
  173. if (generator == DH_GENERATOR_2) {
  174. if (!BN_set_word(t1, 24))
  175. goto err;
  176. if (!BN_set_word(t2, 23))
  177. goto err;
  178. g = 2;
  179. } else if (generator == DH_GENERATOR_5) {
  180. if (!BN_set_word(t1, 60))
  181. goto err;
  182. if (!BN_set_word(t2, 59))
  183. goto err;
  184. g = 5;
  185. } else {
  186. /*
  187. * in the general case, don't worry if 'generator' is a generator or
  188. * not: since we are using safe primes, it will generate either an
  189. * order-q or an order-2q group, which both is OK
  190. */
  191. if (!BN_set_word(t1, 12))
  192. goto err;
  193. if (!BN_set_word(t2, 11))
  194. goto err;
  195. g = generator;
  196. }
  197. if (!BN_generate_prime_ex2(ret->params.p, prime_len, 1, t1, t2, cb, ctx))
  198. goto err;
  199. if (!BN_GENCB_call(cb, 3, 0))
  200. goto err;
  201. if (!BN_set_word(ret->params.g, g))
  202. goto err;
  203. /* We are using safe prime p, set key length equivalent to RFC 7919 */
  204. ret->length = (2 * ossl_ifc_ffc_compute_security_bits(prime_len)
  205. + 24) / 25 * 25;
  206. ret->dirty_cnt++;
  207. ok = 1;
  208. err:
  209. if (ok == -1) {
  210. ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
  211. ok = 0;
  212. }
  213. BN_CTX_end(ctx);
  214. BN_CTX_free(ctx);
  215. return ok;
  216. }
  217. #endif /* FIPS_MODULE */