dh_check.c 6.1 KB

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