dh_gen.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 1995-2019 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. #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. static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
  53. BN_GENCB *cb)
  54. {
  55. BIGNUM *t1, *t2;
  56. int g, ok = -1;
  57. BN_CTX *ctx = NULL;
  58. if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) {
  59. DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_LARGE);
  60. return 0;
  61. }
  62. if (prime_len < DH_MIN_MODULUS_BITS) {
  63. DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_SMALL);
  64. return 0;
  65. }
  66. ctx = BN_CTX_new();
  67. if (ctx == NULL)
  68. goto err;
  69. BN_CTX_start(ctx);
  70. t1 = BN_CTX_get(ctx);
  71. t2 = BN_CTX_get(ctx);
  72. if (t2 == NULL)
  73. goto err;
  74. /* Make sure 'ret' has the necessary elements */
  75. if (!ret->p && ((ret->p = BN_new()) == NULL))
  76. goto err;
  77. if (!ret->g && ((ret->g = BN_new()) == NULL))
  78. goto err;
  79. if (generator <= 1) {
  80. DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
  81. goto err;
  82. }
  83. if (generator == DH_GENERATOR_2) {
  84. if (!BN_set_word(t1, 24))
  85. goto err;
  86. if (!BN_set_word(t2, 23))
  87. goto err;
  88. g = 2;
  89. } else if (generator == DH_GENERATOR_5) {
  90. if (!BN_set_word(t1, 60))
  91. goto err;
  92. if (!BN_set_word(t2, 59))
  93. goto err;
  94. g = 5;
  95. } else {
  96. /*
  97. * in the general case, don't worry if 'generator' is a generator or
  98. * not: since we are using safe primes, it will generate either an
  99. * order-q or an order-2q group, which both is OK
  100. */
  101. if (!BN_set_word(t1, 12))
  102. goto err;
  103. if (!BN_set_word(t2, 11))
  104. goto err;
  105. g = generator;
  106. }
  107. if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))
  108. goto err;
  109. if (!BN_GENCB_call(cb, 3, 0))
  110. goto err;
  111. if (!BN_set_word(ret->g, g))
  112. goto err;
  113. ret->dirty_cnt++;
  114. ok = 1;
  115. err:
  116. if (ok == -1) {
  117. DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
  118. ok = 0;
  119. }
  120. BN_CTX_end(ctx);
  121. BN_CTX_free(ctx);
  122. return ok;
  123. }