dh_gen.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 1995-2017 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_locl.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 q which is prime_len/2 bits long.
  29. * p=(2*q)+1 or (p-1)/2 = q
  30. * For this case, g is a generator if
  31. * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
  32. * Since the factors of p-1 are q and 2, we just need to check
  33. * g^2 mod p != 1 and g^q mod p != 1.
  34. *
  35. * Having said all that,
  36. * there is another special case method for the generators 2, 3 and 5.
  37. * for 2, p mod 24 == 11
  38. * for 3, p mod 12 == 5 <<<<< does not work for safe primes.
  39. * for 5, p mod 10 == 3 or 7
  40. *
  41. * Thanks to Phil Karn for the pointers about the
  42. * special generators and for answering some of my questions.
  43. *
  44. * I've implemented the second simple method :-).
  45. * Since DH should be using a safe prime (both p and q are prime),
  46. * this generator function can take a very very long time to run.
  47. */
  48. /*
  49. * Actually there is no reason to insist that 'generator' be a generator.
  50. * It's just as OK (and in some sense better) to use a generator of the
  51. * order-q subgroup.
  52. */
  53. static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
  54. BN_GENCB *cb)
  55. {
  56. BIGNUM *t1, *t2;
  57. int g, ok = -1;
  58. BN_CTX *ctx = NULL;
  59. ctx = BN_CTX_new();
  60. if (ctx == NULL)
  61. goto err;
  62. BN_CTX_start(ctx);
  63. t1 = BN_CTX_get(ctx);
  64. t2 = BN_CTX_get(ctx);
  65. if (t2 == NULL)
  66. goto err;
  67. /* Make sure 'ret' has the necessary elements */
  68. if (!ret->p && ((ret->p = BN_new()) == NULL))
  69. goto err;
  70. if (!ret->g && ((ret->g = BN_new()) == NULL))
  71. goto err;
  72. if (generator <= 1) {
  73. DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
  74. goto err;
  75. }
  76. if (generator == DH_GENERATOR_2) {
  77. if (!BN_set_word(t1, 24))
  78. goto err;
  79. if (!BN_set_word(t2, 11))
  80. goto err;
  81. g = 2;
  82. } else if (generator == DH_GENERATOR_5) {
  83. if (!BN_set_word(t1, 10))
  84. goto err;
  85. if (!BN_set_word(t2, 3))
  86. goto err;
  87. /*
  88. * BN_set_word(t3,7); just have to miss out on these ones :-(
  89. */
  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, 2))
  98. goto err;
  99. if (!BN_set_word(t2, 1))
  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. if (ctx != NULL) {
  116. BN_CTX_end(ctx);
  117. BN_CTX_free(ctx);
  118. }
  119. return ok;
  120. }