dh_gen.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  14. #include "internal/cryptlib.h"
  15. #include <openssl/bn.h>
  16. #include "dh_local.h"
  17. static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
  18. BN_GENCB *cb);
  19. int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
  20. BN_GENCB *cb)
  21. {
  22. if (ret->meth->generate_params)
  23. return ret->meth->generate_params(ret, prime_len, generator, cb);
  24. return dh_builtin_genparams(ret, prime_len, generator, cb);
  25. }
  26. /*-
  27. * We generate DH parameters as follows
  28. * find a prime p which is prime_len bits long,
  29. * where q=(p-1)/2 is also prime.
  30. * In the following we assume that g is not 0, 1 or p-1, since it
  31. * would generate only trivial subgroups.
  32. * For this case, g is a generator of the order-q subgroup if
  33. * g^q mod p == 1.
  34. * Or in terms of the Legendre symbol: (g/p) == 1.
  35. *
  36. * Having said all that,
  37. * there is another special case method for the generators 2, 3 and 5.
  38. * Using the quadratic reciprocity law it is possible to solve
  39. * (g/p) == 1 for the special values 2, 3, 5:
  40. * (2/p) == 1 if p mod 8 == 1 or 7.
  41. * (3/p) == 1 if p mod 12 == 1 or 11.
  42. * (5/p) == 1 if p mod 5 == 1 or 4.
  43. * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol
  44. *
  45. * Since all safe primes > 7 must satisfy p mod 12 == 11
  46. * and all safe primes > 11 must satisfy p mod 5 != 1
  47. * we can further improve the condition for g = 2, 3 and 5:
  48. * for 2, p mod 24 == 23
  49. * for 3, p mod 12 == 11
  50. * for 5, p mod 60 == 59
  51. *
  52. * However for compatibility with previous versions we use:
  53. * for 2, p mod 24 == 11
  54. * for 5, p mod 60 == 23
  55. */
  56. static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
  57. BN_GENCB *cb)
  58. {
  59. BIGNUM *t1, *t2;
  60. int g, ok = -1;
  61. BN_CTX *ctx = NULL;
  62. ctx = BN_CTX_new();
  63. if (ctx == NULL)
  64. goto err;
  65. BN_CTX_start(ctx);
  66. t1 = BN_CTX_get(ctx);
  67. t2 = BN_CTX_get(ctx);
  68. if (t2 == NULL)
  69. goto err;
  70. /* Make sure 'ret' has the necessary elements */
  71. if (!ret->p && ((ret->p = BN_new()) == NULL))
  72. goto err;
  73. if (!ret->g && ((ret->g = BN_new()) == NULL))
  74. goto err;
  75. if (generator <= 1) {
  76. DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
  77. goto err;
  78. }
  79. if (generator == DH_GENERATOR_2) {
  80. if (!BN_set_word(t1, 24))
  81. goto err;
  82. if (!BN_set_word(t2, 11))
  83. goto err;
  84. g = 2;
  85. } else if (generator == DH_GENERATOR_5) {
  86. if (!BN_set_word(t1, 60))
  87. goto err;
  88. if (!BN_set_word(t2, 23))
  89. goto err;
  90. g = 5;
  91. } else {
  92. /*
  93. * in the general case, don't worry if 'generator' is a generator or
  94. * not: since we are using safe primes, it will generate either an
  95. * order-q or an order-2q group, which both is OK
  96. */
  97. if (!BN_set_word(t1, 12))
  98. goto err;
  99. if (!BN_set_word(t2, 11))
  100. goto err;
  101. g = generator;
  102. }
  103. if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))
  104. goto err;
  105. if (!BN_GENCB_call(cb, 3, 0))
  106. goto err;
  107. if (!BN_set_word(ret->g, g))
  108. goto err;
  109. ok = 1;
  110. err:
  111. if (ok == -1) {
  112. DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
  113. ok = 0;
  114. }
  115. BN_CTX_end(ctx);
  116. BN_CTX_free(ctx);
  117. return ok;
  118. }