dh_check.c 6.3 KB

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