dh_check.c 5.7 KB

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