dh_check.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright 1995-2025 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 <openssl/self_test.h>
  18. #include "dh_local.h"
  19. #include "crypto/dh.h"
  20. /*-
  21. * Check that p and g are suitable enough
  22. *
  23. * p is odd
  24. * 1 < g < p - 1
  25. */
  26. int DH_check_params_ex(const DH *dh)
  27. {
  28. int errflags = 0;
  29. if (!DH_check_params(dh, &errflags))
  30. return 0;
  31. if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
  32. ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
  33. if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
  34. ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
  35. if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
  36. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  37. if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
  38. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  39. return errflags == 0;
  40. }
  41. #ifdef FIPS_MODULE
  42. int DH_check_params(const DH *dh, int *ret)
  43. {
  44. int nid;
  45. *ret = 0;
  46. /*
  47. * SP800-56A R3 Section 5.5.2 Assurances of Domain Parameter Validity
  48. * (1a) The domain parameters correspond to any approved safe prime group.
  49. */
  50. nid = DH_get_nid((DH *)dh);
  51. if (nid != NID_undef)
  52. return 1;
  53. /*
  54. * OR
  55. * (2b) FFC domain params conform to FIPS-186-4 explicit domain param
  56. * validity tests.
  57. */
  58. return ossl_ffc_params_FIPS186_4_validate(dh->libctx, &dh->params,
  59. FFC_PARAM_TYPE_DH, ret, NULL);
  60. }
  61. #else
  62. int DH_check_params(const DH *dh, int *ret)
  63. {
  64. int ok = 0;
  65. BIGNUM *tmp = NULL;
  66. BN_CTX *ctx = NULL;
  67. *ret = 0;
  68. ctx = BN_CTX_new_ex(dh->libctx);
  69. if (ctx == NULL)
  70. goto err;
  71. BN_CTX_start(ctx);
  72. tmp = BN_CTX_get(ctx);
  73. if (tmp == NULL)
  74. goto err;
  75. if (!BN_is_odd(dh->params.p))
  76. *ret |= DH_CHECK_P_NOT_PRIME;
  77. if (BN_is_negative(dh->params.g)
  78. || BN_is_zero(dh->params.g)
  79. || BN_is_one(dh->params.g))
  80. *ret |= DH_NOT_SUITABLE_GENERATOR;
  81. if (BN_copy(tmp, dh->params.p) == NULL || !BN_sub_word(tmp, 1))
  82. goto err;
  83. if (BN_cmp(dh->params.g, tmp) >= 0)
  84. *ret |= DH_NOT_SUITABLE_GENERATOR;
  85. if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS)
  86. *ret |= DH_MODULUS_TOO_SMALL;
  87. if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS)
  88. *ret |= DH_MODULUS_TOO_LARGE;
  89. ok = 1;
  90. err:
  91. BN_CTX_end(ctx);
  92. BN_CTX_free(ctx);
  93. return ok;
  94. }
  95. #endif /* FIPS_MODULE */
  96. /*-
  97. * Check that p is a safe prime and
  98. * g is a suitable generator.
  99. */
  100. int DH_check_ex(const DH *dh)
  101. {
  102. int errflags = 0;
  103. if (!DH_check(dh, &errflags))
  104. return 0;
  105. if ((errflags & DH_NOT_SUITABLE_GENERATOR) != 0)
  106. ERR_raise(ERR_LIB_DH, DH_R_NOT_SUITABLE_GENERATOR);
  107. if ((errflags & DH_CHECK_Q_NOT_PRIME) != 0)
  108. ERR_raise(ERR_LIB_DH, DH_R_CHECK_Q_NOT_PRIME);
  109. if ((errflags & DH_CHECK_INVALID_Q_VALUE) != 0)
  110. ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_Q_VALUE);
  111. if ((errflags & DH_CHECK_INVALID_J_VALUE) != 0)
  112. ERR_raise(ERR_LIB_DH, DH_R_CHECK_INVALID_J_VALUE);
  113. if ((errflags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
  114. ERR_raise(ERR_LIB_DH, DH_R_UNABLE_TO_CHECK_GENERATOR);
  115. if ((errflags & DH_CHECK_P_NOT_PRIME) != 0)
  116. ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_PRIME);
  117. if ((errflags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
  118. ERR_raise(ERR_LIB_DH, DH_R_CHECK_P_NOT_SAFE_PRIME);
  119. if ((errflags & DH_MODULUS_TOO_SMALL) != 0)
  120. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  121. if ((errflags & DH_MODULUS_TOO_LARGE) != 0)
  122. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  123. return errflags == 0;
  124. }
  125. /* Note: according to documentation - this only checks the params */
  126. int DH_check(const DH *dh, int *ret)
  127. {
  128. #ifdef FIPS_MODULE
  129. return DH_check_params(dh, ret);
  130. #else
  131. int ok = 0, r, q_good = 0;
  132. BN_CTX *ctx = NULL;
  133. BIGNUM *t1 = NULL, *t2 = NULL;
  134. int nid = DH_get_nid((DH *)dh);
  135. *ret = 0;
  136. if (nid != NID_undef)
  137. return 1;
  138. /* Don't do any checks at all with an excessively large modulus */
  139. if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
  140. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  141. *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_P_NOT_PRIME;
  142. return 0;
  143. }
  144. if (!DH_check_params(dh, ret))
  145. return 0;
  146. ctx = BN_CTX_new_ex(dh->libctx);
  147. if (ctx == NULL)
  148. goto err;
  149. BN_CTX_start(ctx);
  150. t1 = BN_CTX_get(ctx);
  151. t2 = BN_CTX_get(ctx);
  152. if (t2 == NULL)
  153. goto err;
  154. if (dh->params.q != NULL) {
  155. if (BN_ucmp(dh->params.p, dh->params.q) > 0)
  156. q_good = 1;
  157. else
  158. *ret |= DH_CHECK_INVALID_Q_VALUE;
  159. }
  160. if (q_good) {
  161. if (BN_cmp(dh->params.g, BN_value_one()) <= 0)
  162. *ret |= DH_NOT_SUITABLE_GENERATOR;
  163. else if (BN_cmp(dh->params.g, dh->params.p) >= 0)
  164. *ret |= DH_NOT_SUITABLE_GENERATOR;
  165. else {
  166. /* Check g^q == 1 mod p */
  167. if (!BN_mod_exp(t1, dh->params.g, dh->params.q, dh->params.p, ctx))
  168. goto err;
  169. if (!BN_is_one(t1))
  170. *ret |= DH_NOT_SUITABLE_GENERATOR;
  171. }
  172. r = BN_check_prime(dh->params.q, ctx, NULL);
  173. if (r < 0)
  174. goto err;
  175. if (!r)
  176. *ret |= DH_CHECK_Q_NOT_PRIME;
  177. /* Check p == 1 mod q i.e. q divides p - 1 */
  178. if (!BN_div(t1, t2, dh->params.p, dh->params.q, ctx))
  179. goto err;
  180. if (!BN_is_one(t2))
  181. *ret |= DH_CHECK_INVALID_Q_VALUE;
  182. if (dh->params.j != NULL
  183. && BN_cmp(dh->params.j, t1))
  184. *ret |= DH_CHECK_INVALID_J_VALUE;
  185. }
  186. r = BN_check_prime(dh->params.p, ctx, NULL);
  187. if (r < 0)
  188. goto err;
  189. if (!r)
  190. *ret |= DH_CHECK_P_NOT_PRIME;
  191. else if (dh->params.q == NULL) {
  192. if (!BN_rshift1(t1, dh->params.p))
  193. goto err;
  194. r = BN_check_prime(t1, ctx, NULL);
  195. if (r < 0)
  196. goto err;
  197. if (!r)
  198. *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
  199. }
  200. ok = 1;
  201. err:
  202. BN_CTX_end(ctx);
  203. BN_CTX_free(ctx);
  204. return ok;
  205. #endif /* FIPS_MODULE */
  206. }
  207. int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
  208. {
  209. int errflags = 0;
  210. if (!DH_check_pub_key(dh, pub_key, &errflags))
  211. return 0;
  212. if ((errflags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
  213. ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_SMALL);
  214. if ((errflags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
  215. ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_TOO_LARGE);
  216. if ((errflags & DH_CHECK_PUBKEY_INVALID) != 0)
  217. ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
  218. return errflags == 0;
  219. }
  220. /*
  221. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
  222. */
  223. int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
  224. {
  225. /* Don't do any checks at all with an excessively large modulus */
  226. if (BN_num_bits(dh->params.p) > OPENSSL_DH_CHECK_MAX_MODULUS_BITS) {
  227. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  228. *ret = DH_MODULUS_TOO_LARGE | DH_CHECK_PUBKEY_INVALID;
  229. return 0;
  230. }
  231. if (dh->params.q != NULL && BN_ucmp(dh->params.p, dh->params.q) < 0) {
  232. *ret |= DH_CHECK_INVALID_Q_VALUE | DH_CHECK_PUBKEY_INVALID;
  233. return 1;
  234. }
  235. return ossl_ffc_validate_public_key(&dh->params, pub_key, ret);
  236. }
  237. /*
  238. * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation.
  239. * To only be used with ephemeral FFC public keys generated using the approved
  240. * safe-prime groups.
  241. */
  242. int ossl_dh_check_pub_key_partial(const DH *dh, const BIGNUM *pub_key, int *ret)
  243. {
  244. return ossl_ffc_validate_public_key_partial(&dh->params, pub_key, ret)
  245. && *ret == 0;
  246. }
  247. int ossl_dh_check_priv_key(const DH *dh, const BIGNUM *priv_key, int *ret)
  248. {
  249. int ok = 0;
  250. BIGNUM *two_powN = NULL, *upper;
  251. *ret = 0;
  252. two_powN = BN_new();
  253. if (two_powN == NULL)
  254. return 0;
  255. if (dh->params.q != NULL) {
  256. upper = dh->params.q;
  257. #ifndef FIPS_MODULE
  258. } else if (dh->params.p != NULL) {
  259. /*
  260. * We do not have q so we just check the key is within some
  261. * reasonable range, or the number of bits is equal to dh->length.
  262. */
  263. int length = dh->length;
  264. if (length == 0) {
  265. length = BN_num_bits(dh->params.p) - 1;
  266. if (BN_num_bits(priv_key) <= length
  267. && BN_num_bits(priv_key) > 1)
  268. ok = 1;
  269. } else if (BN_num_bits(priv_key) == length) {
  270. ok = 1;
  271. }
  272. goto end;
  273. #endif
  274. } else {
  275. goto end;
  276. }
  277. /* Is it from an approved Safe prime group ?*/
  278. if (DH_get_nid((DH *)dh) != NID_undef && dh->length != 0) {
  279. if (!BN_lshift(two_powN, BN_value_one(), dh->length))
  280. goto end;
  281. if (BN_cmp(two_powN, dh->params.q) < 0)
  282. upper = two_powN;
  283. }
  284. if (!ossl_ffc_validate_private_key(upper, priv_key, ret))
  285. goto end;
  286. ok = 1;
  287. end:
  288. BN_free(two_powN);
  289. return ok;
  290. }
  291. /*
  292. * FFC pairwise check from SP800-56A R3.
  293. * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
  294. */
  295. int ossl_dh_check_pairwise(const DH *dh, int return_on_null_numbers)
  296. {
  297. int ret = 0;
  298. BN_CTX *ctx = NULL;
  299. BIGNUM *pub_key = NULL;
  300. OSSL_SELF_TEST *st = NULL;
  301. OSSL_CALLBACK *stcb = NULL;
  302. void *stcbarg = NULL;
  303. if (dh->params.p == NULL
  304. || dh->params.g == NULL
  305. || dh->priv_key == NULL
  306. || dh->pub_key == NULL)
  307. return return_on_null_numbers;
  308. OSSL_SELF_TEST_get_callback(dh->libctx, &stcb, &stcbarg);
  309. st = OSSL_SELF_TEST_new(stcb, stcbarg);
  310. if (st == NULL)
  311. goto err;
  312. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
  313. OSSL_SELF_TEST_DESC_PCT_DH);
  314. ctx = BN_CTX_new_ex(dh->libctx);
  315. if (ctx == NULL)
  316. goto err;
  317. pub_key = BN_new();
  318. if (pub_key == NULL)
  319. goto err;
  320. /* recalculate the public key = (g ^ priv) mod p */
  321. if (!ossl_dh_generate_public_key(ctx, dh, dh->priv_key, pub_key))
  322. goto err;
  323. #ifdef FIPS_MODULE
  324. {
  325. int len;
  326. unsigned char bytes[1024] = {0}; /* Max key size of 8192 bits */
  327. if (BN_num_bytes(pub_key) > (int)sizeof(bytes))
  328. goto err;
  329. len = BN_bn2bin(pub_key, bytes);
  330. OSSL_SELF_TEST_oncorrupt_byte(st, bytes);
  331. if (BN_bin2bn(bytes, len, pub_key) == NULL)
  332. goto err;
  333. }
  334. #endif
  335. /* check it matches the existing public_key */
  336. ret = BN_cmp(pub_key, dh->pub_key) == 0;
  337. err:
  338. BN_free(pub_key);
  339. BN_CTX_free(ctx);
  340. OSSL_SELF_TEST_onend(st, ret);
  341. OSSL_SELF_TEST_free(st);
  342. return ret;
  343. }