dh_check.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright 1995-2020 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. /*
  10. * DH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/bn.h>
  17. #include "dh_local.h"
  18. #include "crypto/dh.h"
  19. /*-
  20. * Check that p and g are suitable enough
  21. *
  22. * p is odd
  23. * 1 < g < p - 1
  24. */
  25. int DH_check_params_ex(const DH *dh)
  26. {
  27. int errflags = 0;
  28. if (!DH_check_params(dh, &errflags))
  29. return 0;
  30. if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
  31. ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
  32. if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
  33. ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
  34. if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
  35. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  36. if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
  37. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  38. return errflags == 0;
  39. }
  40. #ifdef FIPS_MODULE
  41. int DH_check_params(const DH *dh, int *ret)
  42. {
  43. int nid;
  44. *ret = 0;
  45. /*
  46. * SP800-56A R3 Section 5.5.2 Assurances of Domain Parameter Validity
  47. * (1a) The domain parameters correspond to any approved safe prime group.
  48. */
  49. nid = DH_get_nid((DH *)dh);
  50. if (nid != NID_undef)
  51. return 1;
  52. /*
  53. * OR
  54. * (2b) FFC domain params conform to FIPS-186-4 explicit domain param
  55. * validity tests.
  56. */
  57. return ossl_ffc_params_FIPS186_4_validate(dh->libctx, &dh->params,
  58. FFC_PARAM_TYPE_DH, ret, NULL);
  59. }
  60. #else
  61. int DH_check_params(const DH *dh, int *ret)
  62. {
  63. int ok = 0;
  64. BIGNUM *tmp = NULL;
  65. BN_CTX *ctx = NULL;
  66. *ret = 0;
  67. ctx = BN_CTX_new();
  68. if (ctx == NULL)
  69. goto err;
  70. BN_CTX_start(ctx);
  71. tmp = BN_CTX_get(ctx);
  72. if (tmp == NULL)
  73. goto err;
  74. if (!BN_is_odd(dh->params.p))
  75. *ret |= DH_CHECK_P_NOT_PRIME;
  76. if (BN_is_negative(dh->params.g)
  77. || BN_is_zero(dh->params.g)
  78. || BN_is_one(dh->params.g))
  79. *ret |= DH_NOT_SUITABLE_GENERATOR;
  80. if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
  81. goto err;
  82. if (BN_cmp(dh->params.g, tmp) >= 0)
  83. *ret |= DH_NOT_SUITABLE_GENERATOR;
  84. if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
  85. *ret |= DH_MODULUS_TOO_SMALL;
  86. if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
  87. *ret |= DH_MODULUS_TOO_LARGE;
  88. ok = 1;
  89. err:
  90. BN_CTX_end(ctx);
  91. BN_CTX_free(ctx);
  92. return ok;
  93. }
  94. #endif /* FIPS_MODULE */
  95. /*-
  96. * Check that p is a safe prime and
  97. * g is a suitable generator.
  98. */
  99. int DH_check_ex(const DH *dh)
  100. {
  101. int errflags = 0;
  102. if (!DH_check(dh, &errflags))
  103. return 0;
  104. if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
  105. ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
  106. if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
  107. ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
  108. if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
  109. ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
  110. if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
  111. ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
  112. if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
  113. ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
  114. if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
  115. ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
  116. if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
  117. ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
  118. if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
  119. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  120. if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
  121. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  122. return errflags == 0;
  123. }
  124. /* Note: according to documentation - this only checks the params */
  125. int DH_check(const DH *dh, int *ret)
  126. {
  127. #ifdef FIPS_MODULE
  128. return DH_check_params(dh, ret);
  129. #else
  130. int ok = 0, r;
  131. BN_CTX *ctx = NULL;
  132. BIGNUM *t1 = NULL, *t2 = NULL;
  133. int nid = DH_get_nid((DH *)dh);
  134. *ret = 0;
  135. if (nid != NID_undef)
  136. return 1;
  137. if (!DH_check_params(dh, ret))
  138. return 0;
  139. ctx = BN_CTX_new();
  140. if (ctx == NULL)
  141. goto err;
  142. BN_CTX_start(ctx);
  143. t1 = BN_CTX_get(ctx);
  144. t2 = BN_CTX_get(ctx);
  145. if (t2 == NULL)
  146. goto err;
  147. if (dh->params.q != NULL) {
  148. if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
  149. *ret |= DH_NOT_SUITABLE_GENERATOR;
  150. else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
  151. *ret |= DH_NOT_SUITABLE_GENERATOR;
  152. else {
  153. /* Check g^q == 1 mod p */
  154. if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
  155. goto err;
  156. if (!BN_is_one(t1))
  157. *ret |= DH_NOT_SUITABLE_GENERATOR;
  158. }
  159. r = BN_check_prime(dh->params.q, ctx, NULL);
  160. if (r < 0)
  161. goto err;
  162. if (!r)
  163. *ret |= DH_CHECK_Q_NOT_PRIME;
  164. /* Check p == 1 mod q i.e. q divides p - 1 */
  165. if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
  166. goto err;
  167. if (!BN_is_one(t2))
  168. *ret |= DH_CHECK_INVALID_Q_VALUE;
  169. if (dh->params.j != NULL
  170. && BN_cmp(dh->params.j, t1))
  171. *ret |= DH_CHECK_INVALID_J_VALUE;
  172. }
  173. r = BN_check_prime(dh->params.p, ctx, NULL);
  174. if (r < 0)
  175. goto err;
  176. if (!r)
  177. *ret |= DH_CHECK_P_NOT_PRIME;
  178. else if (dh->params.q == NULL) {
  179. if (!BN_rshift1(t1, dh->params.p))
  180. goto err;
  181. r = BN_check_prime(t1, ctx, NULL);
  182. if (r < 0)
  183. goto err;
  184. if (!r)
  185. *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
  186. }
  187. ok = 1;
  188. err:
  189. BN_CTX_end(ctx);
  190. BN_CTX_free(ctx);
  191. return ok;
  192. #endif /* FIPS_MODULE */
  193. }
  194. int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
  195. {
  196. int errflags = 0;
  197. if (!DH_check_pub_key(dh, pub_key, &errflags))
  198. return 0;
  199. if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
  200. ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
  201. if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
  202. ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
  203. if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
  204. ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
  205. return errflags == 0;
  206. }
  207. /*
  208. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
  209. */
  210. int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
  211. {
  212. return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
  213. }
  214. /*
  215. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
  216. * To only be used with ephemeral FFC public keys generated using the approved
  217. * safe-prime groups.
  218. */
  219. int dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret)
  220. {
  221. return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret);
  222. }
  223. int dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
  224. {
  225. int ok = 0;
  226. BIGNUM *two_powN = NULL, *upper;
  227. *ret = 0;
  228. two_powN = BN_new();
  229. if (two_powN == NULL)
  230. return 0;
  231. if (dh->params.q == NULL)
  232. goto err;
  233. upper = dh->params.q;
  234. /* Is it from an approved Safe prime group ?*/
  235. if (DH_get_nid((DH *)dh) != NID_undef) {
  236. if (!BN_lshift(two_powN, BN_value_one(), dh->length))
  237. goto err;
  238. if (BN_cmp(two_powN, dh->params.q) < 0)
  239. upper = two_powN;
  240. }
  241. if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
  242. goto err;
  243. ok = 1;
  244. err:
  245. BN_free(two_powN);
  246. return ok;
  247. }
  248. /*
  249. * FFC pairwise check from SP800-56A R3.
  250. * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
  251. */
  252. int dh_check_pairwise(const DH *dh)
  253. {
  254. int ret = 0;
  255. BN_CTX *ctx = NULL;
  256. BIGNUM *pub_key = NULL;
  257. if (dh->params.p == NULL
  258. || dh->params.g == NULL
  259. || dh->priv_key == NULL
  260. || dh->pub_key == NULL)
  261. return 0;
  262. ctx = BN_CTX_new_ex(dh->libctx);
  263. if (ctx == NULL)
  264. goto err;
  265. pub_key = BN_new();
  266. if (pub_key == NULL)
  267. goto err;
  268. /* recalculate the public key = (g ^ priv) mod p */
  269. if (!dh_generate_public_key(ctx, dh, dh->priv_key, pub_key))
  270. goto err;
  271. /* check it matches the existing pubic_key */
  272. ret = BN_cmp(pub_key, dh->pub_key) == 0;
  273. err:
  274. BN_free(pub_key);
  275. BN_CTX_free(ctx);
  276. return ret;
  277. }