2
0

dh_check.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 1995-2016 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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/bn.h>
  12. #include "dh_locl.h"
  13. /*-
  14. * Check that p and g are suitable enough
  15. *
  16. * p is odd
  17. * 1 < g < p - 1
  18. */
  19. int DH_check_params(const DH *dh, int *ret)
  20. {
  21. int ok = 0;
  22. BIGNUM *tmp = NULL;
  23. BN_CTX *ctx = NULL;
  24. *ret = 0;
  25. ctx = BN_CTX_new();
  26. if (ctx == NULL)
  27. goto err;
  28. BN_CTX_start(ctx);
  29. tmp = BN_CTX_get(ctx);
  30. if (tmp == NULL)
  31. goto err;
  32. if (!BN_is_odd(dh->p))
  33. *ret |= DH_CHECK_P_NOT_PRIME;
  34. if (BN_is_negative(dh->g) || BN_is_zero(dh->g) || BN_is_one(dh->g))
  35. *ret |= DH_NOT_SUITABLE_GENERATOR;
  36. if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
  37. goto err;
  38. if (BN_cmp(dh->g, tmp) >= 0)
  39. *ret |= DH_NOT_SUITABLE_GENERATOR;
  40. ok = 1;
  41. err:
  42. if (ctx != NULL) {
  43. BN_CTX_end(ctx);
  44. BN_CTX_free(ctx);
  45. }
  46. return (ok);
  47. }
  48. /*-
  49. * Check that p is a safe prime and
  50. * if g is 2, 3 or 5, check that it is a suitable generator
  51. * where
  52. * for 2, p mod 24 == 11
  53. * for 3, p mod 12 == 5
  54. * for 5, p mod 10 == 3 or 7
  55. * should hold.
  56. */
  57. int DH_check(const DH *dh, int *ret)
  58. {
  59. int ok = 0, r;
  60. BN_CTX *ctx = NULL;
  61. BN_ULONG l;
  62. BIGNUM *t1 = NULL, *t2 = NULL;
  63. *ret = 0;
  64. ctx = BN_CTX_new();
  65. if (ctx == NULL)
  66. goto err;
  67. BN_CTX_start(ctx);
  68. t1 = BN_CTX_get(ctx);
  69. if (t1 == NULL)
  70. goto err;
  71. t2 = BN_CTX_get(ctx);
  72. if (t2 == NULL)
  73. goto err;
  74. if (dh->q) {
  75. if (BN_cmp(dh->g, BN_value_one()) <= 0)
  76. *ret |= DH_NOT_SUITABLE_GENERATOR;
  77. else if (BN_cmp(dh->g, dh->p) >= 0)
  78. *ret |= DH_NOT_SUITABLE_GENERATOR;
  79. else {
  80. /* Check g^q == 1 mod p */
  81. if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
  82. goto err;
  83. if (!BN_is_one(t1))
  84. *ret |= DH_NOT_SUITABLE_GENERATOR;
  85. }
  86. r = BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL);
  87. if (r < 0)
  88. goto err;
  89. if (!r)
  90. *ret |= DH_CHECK_Q_NOT_PRIME;
  91. /* Check p == 1 mod q i.e. q divides p - 1 */
  92. if (!BN_div(t1, t2, dh->p, dh->q, ctx))
  93. goto err;
  94. if (!BN_is_one(t2))
  95. *ret |= DH_CHECK_INVALID_Q_VALUE;
  96. if (dh->j && BN_cmp(dh->j, t1))
  97. *ret |= DH_CHECK_INVALID_J_VALUE;
  98. } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {
  99. l = BN_mod_word(dh->p, 24);
  100. if (l == (BN_ULONG)-1)
  101. goto err;
  102. if (l != 11)
  103. *ret |= DH_NOT_SUITABLE_GENERATOR;
  104. } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {
  105. l = BN_mod_word(dh->p, 10);
  106. if (l == (BN_ULONG)-1)
  107. goto err;
  108. if ((l != 3) && (l != 7))
  109. *ret |= DH_NOT_SUITABLE_GENERATOR;
  110. } else
  111. *ret |= DH_UNABLE_TO_CHECK_GENERATOR;
  112. r = BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL);
  113. if (r < 0)
  114. goto err;
  115. if (!r)
  116. *ret |= DH_CHECK_P_NOT_PRIME;
  117. else if (!dh->q) {
  118. if (!BN_rshift1(t1, dh->p))
  119. goto err;
  120. r = BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL);
  121. if (r < 0)
  122. goto err;
  123. if (!r)
  124. *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
  125. }
  126. ok = 1;
  127. err:
  128. if (ctx != NULL) {
  129. BN_CTX_end(ctx);
  130. BN_CTX_free(ctx);
  131. }
  132. return (ok);
  133. }
  134. int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
  135. {
  136. int ok = 0;
  137. BIGNUM *tmp = NULL;
  138. BN_CTX *ctx = NULL;
  139. *ret = 0;
  140. ctx = BN_CTX_new();
  141. if (ctx == NULL)
  142. goto err;
  143. BN_CTX_start(ctx);
  144. tmp = BN_CTX_get(ctx);
  145. if (tmp == NULL || !BN_set_word(tmp, 1))
  146. goto err;
  147. if (BN_cmp(pub_key, tmp) <= 0)
  148. *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
  149. if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
  150. goto err;
  151. if (BN_cmp(pub_key, tmp) >= 0)
  152. *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
  153. if (dh->q != NULL) {
  154. /* Check pub_key^q == 1 mod p */
  155. if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
  156. goto err;
  157. if (!BN_is_one(tmp))
  158. *ret |= DH_CHECK_PUBKEY_INVALID;
  159. }
  160. ok = 1;
  161. err:
  162. if (ctx != NULL) {
  163. BN_CTX_end(ctx);
  164. BN_CTX_free(ctx);
  165. }
  166. return (ok);
  167. }