dh_check.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 1995-2019 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. # define DH_NUMBER_ITERATIONS_FOR_PRIME 64
  14. /*-
  15. * Check that p and g are suitable enough
  16. *
  17. * p is odd
  18. * 1 < g < p - 1
  19. */
  20. int DH_check_params_ex(const DH *dh)
  21. {
  22. int errflags = 0;
  23. (void)DH_check_params(dh, &errflags);
  24. if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
  25. DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_CHECK_P_NOT_PRIME);
  26. if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
  27. DHerr(DH_F_DH_CHECK_PARAMS_EX, DH_R_NOT_SUITABLE_GENERATOR);
  28. return errflags == 0;
  29. }
  30. int DH_check_params(const DH *dh, int *ret)
  31. {
  32. int ok = 0;
  33. BIGNUM *tmp = NULL;
  34. BN_CTX *ctx = NULL;
  35. *ret = 0;
  36. ctx = BN_CTX_new();
  37. if (ctx == NULL)
  38. goto err;
  39. BN_CTX_start(ctx);
  40. tmp = BN_CTX_get(ctx);
  41. if (tmp == NULL)
  42. goto err;
  43. if (!BN_is_odd(dh->p))
  44. *ret |= DH_CHECK_P_NOT_PRIME;
  45. if (BN_is_negative(dh->g) || BN_is_zero(dh->g) || BN_is_one(dh->g))
  46. *ret |= DH_NOT_SUITABLE_GENERATOR;
  47. if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
  48. goto err;
  49. if (BN_cmp(dh->g, tmp) >= 0)
  50. *ret |= DH_NOT_SUITABLE_GENERATOR;
  51. ok = 1;
  52. err:
  53. BN_CTX_end(ctx);
  54. BN_CTX_free(ctx);
  55. return ok;
  56. }
  57. /*-
  58. * Check that p is a safe prime and
  59. * if g is 2, 3 or 5, check that it is a suitable generator
  60. * where
  61. * for 2, p mod 24 == 11
  62. * for 3, p mod 12 == 5
  63. * for 5, p mod 10 == 3 or 7
  64. * should hold.
  65. */
  66. int DH_check_ex(const DH *dh)
  67. {
  68. int errflags = 0;
  69. (void)DH_check(dh, &errflags);
  70. if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
  71. DHerr(DH_F_DH_CHECK_EX, DH_R_NOT_SUITABLE_GENERATOR);
  72. if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
  73. DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_Q_NOT_PRIME);
  74. if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
  75. DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_Q_VALUE);
  76. if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
  77. DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_INVALID_J_VALUE);
  78. if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
  79. DHerr(DH_F_DH_CHECK_EX, DH_R_UNABLE_TO_CHECK_GENERATOR);
  80. if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
  81. DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_PRIME);
  82. if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
  83. DHerr(DH_F_DH_CHECK_EX, DH_R_CHECK_P_NOT_SAFE_PRIME);
  84. return errflags == 0;
  85. }
  86. int DH_check(const DH *dh, int *ret)
  87. {
  88. int ok = 0, r;
  89. BN_CTX *ctx = NULL;
  90. BN_ULONG l;
  91. BIGNUM *t1 = NULL, *t2 = NULL;
  92. *ret = 0;
  93. ctx = BN_CTX_new();
  94. if (ctx == NULL)
  95. goto err;
  96. BN_CTX_start(ctx);
  97. t1 = BN_CTX_get(ctx);
  98. t2 = BN_CTX_get(ctx);
  99. if (t2 == NULL)
  100. goto err;
  101. if (dh->q) {
  102. if (BN_cmp(dh->g, BN_value_one()) <= 0)
  103. *ret |= DH_NOT_SUITABLE_GENERATOR;
  104. else if (BN_cmp(dh->g, dh->p) >= 0)
  105. *ret |= DH_NOT_SUITABLE_GENERATOR;
  106. else {
  107. /* Check g^q == 1 mod p */
  108. if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
  109. goto err;
  110. if (!BN_is_one(t1))
  111. *ret |= DH_NOT_SUITABLE_GENERATOR;
  112. }
  113. r = BN_is_prime_ex(dh->q, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
  114. if (r < 0)
  115. goto err;
  116. if (!r)
  117. *ret |= DH_CHECK_Q_NOT_PRIME;
  118. /* Check p == 1 mod q i.e. q divides p - 1 */
  119. if (!BN_div(t1, t2, dh->p, dh->q, ctx))
  120. goto err;
  121. if (!BN_is_one(t2))
  122. *ret |= DH_CHECK_INVALID_Q_VALUE;
  123. if (dh->j && BN_cmp(dh->j, t1))
  124. *ret |= DH_CHECK_INVALID_J_VALUE;
  125. } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {
  126. l = BN_mod_word(dh->p, 24);
  127. if (l == (BN_ULONG)-1)
  128. goto err;
  129. if (l != 11)
  130. *ret |= DH_NOT_SUITABLE_GENERATOR;
  131. } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {
  132. l = BN_mod_word(dh->p, 10);
  133. if (l == (BN_ULONG)-1)
  134. goto err;
  135. if ((l != 3) && (l != 7))
  136. *ret |= DH_NOT_SUITABLE_GENERATOR;
  137. } else
  138. *ret |= DH_UNABLE_TO_CHECK_GENERATOR;
  139. r = BN_is_prime_ex(dh->p, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
  140. if (r < 0)
  141. goto err;
  142. if (!r)
  143. *ret |= DH_CHECK_P_NOT_PRIME;
  144. else if (!dh->q) {
  145. if (!BN_rshift1(t1, dh->p))
  146. goto err;
  147. r = BN_is_prime_ex(t1, DH_NUMBER_ITERATIONS_FOR_PRIME, ctx, NULL);
  148. if (r < 0)
  149. goto err;
  150. if (!r)
  151. *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
  152. }
  153. ok = 1;
  154. err:
  155. BN_CTX_end(ctx);
  156. BN_CTX_free(ctx);
  157. return ok;
  158. }
  159. int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
  160. {
  161. int errflags = 0;
  162. (void)DH_check(dh, &errflags);
  163. if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
  164. DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_SMALL);
  165. if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
  166. DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_TOO_LARGE);
  167. if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
  168. DHerr(DH_F_DH_CHECK_PUB_KEY_EX, DH_R_CHECK_PUBKEY_INVALID);
  169. return errflags == 0;
  170. }
  171. int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
  172. {
  173. int ok = 0;
  174. BIGNUM *tmp = NULL;
  175. BN_CTX *ctx = NULL;
  176. *ret = 0;
  177. ctx = BN_CTX_new();
  178. if (ctx == NULL)
  179. goto err;
  180. BN_CTX_start(ctx);
  181. tmp = BN_CTX_get(ctx);
  182. if (tmp == NULL || !BN_set_word(tmp, 1))
  183. goto err;
  184. if (BN_cmp(pub_key, tmp) <= 0)
  185. *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
  186. if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
  187. goto err;
  188. if (BN_cmp(pub_key, tmp) >= 0)
  189. *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
  190. if (dh->q != NULL) {
  191. /* Check pub_key^q == 1 mod p */
  192. if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
  193. goto err;
  194. if (!BN_is_one(tmp))
  195. *ret |= DH_CHECK_PUBKEY_INVALID;
  196. }
  197. ok = 1;
  198. err:
  199. BN_CTX_end(ctx);
  200. BN_CTX_free(ctx);
  201. return ok;
  202. }