dh_key.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright 1995-2023 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 "dh_local.h"
  17. #include "crypto/bn.h"
  18. #include "crypto/dh.h"
  19. #include "crypto/security_bits.h"
  20. #ifdef FIPS_MODULE
  21. # define MIN_STRENGTH 112
  22. #else
  23. # define MIN_STRENGTH 80
  24. #endif
  25. static int generate_key(DH *dh);
  26. static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
  27. const BIGNUM *a, const BIGNUM *p,
  28. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  29. static int dh_init(DH *dh);
  30. static int dh_finish(DH *dh);
  31. /*
  32. * See SP800-56Ar3 Section 5.7.1.1
  33. * Finite Field Cryptography Diffie-Hellman (FFC DH) Primitive
  34. */
  35. int ossl_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  36. {
  37. BN_CTX *ctx = NULL;
  38. BN_MONT_CTX *mont = NULL;
  39. BIGNUM *z = NULL, *pminus1;
  40. int ret = -1;
  41. if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  42. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  43. goto err;
  44. }
  45. if (dh->params.q != NULL
  46. && BN_num_bits(dh->params.q) > OPENSSL_DH_MAX_MODULUS_BITS) {
  47. ERR_raise(ERR_LIB_DH, DH_R_Q_TOO_LARGE);
  48. goto err;
  49. }
  50. if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
  51. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  52. return 0;
  53. }
  54. ctx = BN_CTX_new_ex(dh->libctx);
  55. if (ctx == NULL)
  56. goto err;
  57. BN_CTX_start(ctx);
  58. pminus1 = BN_CTX_get(ctx);
  59. z = BN_CTX_get(ctx);
  60. if (z == NULL)
  61. goto err;
  62. if (dh->priv_key == NULL) {
  63. ERR_raise(ERR_LIB_DH, DH_R_NO_PRIVATE_VALUE);
  64. goto err;
  65. }
  66. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  67. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  68. dh->lock, dh->params.p, ctx);
  69. BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
  70. if (!mont)
  71. goto err;
  72. }
  73. /* (Step 1) Z = pub_key^priv_key mod p */
  74. if (!dh->meth->bn_mod_exp(dh, z, pub_key, dh->priv_key, dh->params.p, ctx,
  75. mont)) {
  76. ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
  77. goto err;
  78. }
  79. /* (Step 2) Error if z <= 1 or z = p - 1 */
  80. if (BN_copy(pminus1, dh->params.p) == NULL
  81. || !BN_sub_word(pminus1, 1)
  82. || BN_cmp(z, BN_value_one()) <= 0
  83. || BN_cmp(z, pminus1) == 0) {
  84. ERR_raise(ERR_LIB_DH, DH_R_INVALID_SECRET);
  85. goto err;
  86. }
  87. /* return the padded key, i.e. same number of bytes as the modulus */
  88. ret = BN_bn2binpad(z, key, BN_num_bytes(dh->params.p));
  89. err:
  90. BN_clear(z); /* (Step 2) destroy intermediate values */
  91. BN_CTX_end(ctx);
  92. BN_CTX_free(ctx);
  93. return ret;
  94. }
  95. /*-
  96. * NB: This function is inherently not constant time due to the
  97. * RFC 5246 (8.1.2) padding style that strips leading zero bytes.
  98. */
  99. int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  100. {
  101. int ret = 0, i;
  102. volatile size_t npad = 0, mask = 1;
  103. /* compute the key; ret is constant unless compute_key is external */
  104. #ifdef FIPS_MODULE
  105. ret = ossl_dh_compute_key(key, pub_key, dh);
  106. #else
  107. ret = dh->meth->compute_key(key, pub_key, dh);
  108. #endif
  109. if (ret <= 0)
  110. return ret;
  111. /* count leading zero bytes, yet still touch all bytes */
  112. for (i = 0; i < ret; i++) {
  113. mask &= !key[i];
  114. npad += mask;
  115. }
  116. /* unpad key */
  117. ret -= npad;
  118. /* key-dependent memory access, potentially leaking npad / ret */
  119. memmove(key, key + npad, ret);
  120. /* key-dependent memory access, potentially leaking npad / ret */
  121. memset(key + ret, 0, npad);
  122. return ret;
  123. }
  124. int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  125. {
  126. int rv, pad;
  127. /* rv is constant unless compute_key is external */
  128. #ifdef FIPS_MODULE
  129. rv = ossl_dh_compute_key(key, pub_key, dh);
  130. #else
  131. rv = dh->meth->compute_key(key, pub_key, dh);
  132. #endif
  133. if (rv <= 0)
  134. return rv;
  135. pad = BN_num_bytes(dh->params.p) - rv;
  136. /* pad is constant (zero) unless compute_key is external */
  137. if (pad > 0) {
  138. memmove(key + pad, key, rv);
  139. memset(key, 0, pad);
  140. }
  141. return rv + pad;
  142. }
  143. static DH_METHOD dh_ossl = {
  144. "OpenSSL DH Method",
  145. generate_key,
  146. ossl_dh_compute_key,
  147. dh_bn_mod_exp,
  148. dh_init,
  149. dh_finish,
  150. DH_FLAG_FIPS_METHOD,
  151. NULL,
  152. NULL
  153. };
  154. static const DH_METHOD *default_DH_method = &dh_ossl;
  155. const DH_METHOD *DH_OpenSSL(void)
  156. {
  157. return &dh_ossl;
  158. }
  159. const DH_METHOD *DH_get_default_method(void)
  160. {
  161. return default_DH_method;
  162. }
  163. static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
  164. const BIGNUM *a, const BIGNUM *p,
  165. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  166. {
  167. #ifdef S390X_MOD_EXP
  168. return s390x_mod_exp(r, a, p, m, ctx, m_ctx);
  169. #else
  170. return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
  171. #endif
  172. }
  173. static int dh_init(DH *dh)
  174. {
  175. dh->flags |= DH_FLAG_CACHE_MONT_P;
  176. dh->dirty_cnt++;
  177. return 1;
  178. }
  179. static int dh_finish(DH *dh)
  180. {
  181. BN_MONT_CTX_free(dh->method_mont_p);
  182. return 1;
  183. }
  184. #ifndef FIPS_MODULE
  185. void DH_set_default_method(const DH_METHOD *meth)
  186. {
  187. default_DH_method = meth;
  188. }
  189. #endif /* FIPS_MODULE */
  190. int DH_generate_key(DH *dh)
  191. {
  192. #ifdef FIPS_MODULE
  193. return generate_key(dh);
  194. #else
  195. return dh->meth->generate_key(dh);
  196. #endif
  197. }
  198. int ossl_dh_generate_public_key(BN_CTX *ctx, const DH *dh,
  199. const BIGNUM *priv_key, BIGNUM *pub_key)
  200. {
  201. int ret = 0;
  202. BIGNUM *prk = BN_new();
  203. BN_MONT_CTX *mont = NULL;
  204. if (prk == NULL)
  205. return 0;
  206. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  207. /*
  208. * We take the input DH as const, but we lie, because in some cases we
  209. * want to get a hold of its Montgomery context.
  210. *
  211. * We cast to remove the const qualifier in this case, it should be
  212. * fine...
  213. */
  214. BN_MONT_CTX **pmont = (BN_MONT_CTX **)&dh->method_mont_p;
  215. mont = BN_MONT_CTX_set_locked(pmont, dh->lock, dh->params.p, ctx);
  216. if (mont == NULL)
  217. goto err;
  218. }
  219. BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
  220. /* pub_key = g^priv_key mod p */
  221. if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
  222. ctx, mont))
  223. goto err;
  224. ret = 1;
  225. err:
  226. BN_clear_free(prk);
  227. return ret;
  228. }
  229. static int generate_key(DH *dh)
  230. {
  231. int ok = 0;
  232. int generate_new_key = 0;
  233. #ifndef FIPS_MODULE
  234. unsigned l;
  235. #endif
  236. BN_CTX *ctx = NULL;
  237. BIGNUM *pub_key = NULL, *priv_key = NULL;
  238. if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  239. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  240. return 0;
  241. }
  242. if (dh->params.q != NULL
  243. && BN_num_bits(dh->params.q) > OPENSSL_DH_MAX_MODULUS_BITS) {
  244. ERR_raise(ERR_LIB_DH, DH_R_Q_TOO_LARGE);
  245. return 0;
  246. }
  247. if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
  248. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  249. return 0;
  250. }
  251. ctx = BN_CTX_new_ex(dh->libctx);
  252. if (ctx == NULL)
  253. goto err;
  254. if (dh->priv_key == NULL) {
  255. priv_key = BN_secure_new();
  256. if (priv_key == NULL)
  257. goto err;
  258. generate_new_key = 1;
  259. } else {
  260. priv_key = dh->priv_key;
  261. }
  262. if (dh->pub_key == NULL) {
  263. pub_key = BN_new();
  264. if (pub_key == NULL)
  265. goto err;
  266. } else {
  267. pub_key = dh->pub_key;
  268. }
  269. if (generate_new_key) {
  270. /* Is it an approved safe prime ?*/
  271. if (DH_get_nid(dh) != NID_undef) {
  272. int max_strength =
  273. ossl_ifc_ffc_compute_security_bits(BN_num_bits(dh->params.p));
  274. if (dh->params.q == NULL
  275. || dh->length > BN_num_bits(dh->params.q))
  276. goto err;
  277. /* dh->length = maximum bit length of generated private key */
  278. if (!ossl_ffc_generate_private_key(ctx, &dh->params, dh->length,
  279. max_strength, priv_key))
  280. goto err;
  281. } else {
  282. #ifdef FIPS_MODULE
  283. if (dh->params.q == NULL)
  284. goto err;
  285. #else
  286. if (dh->params.q == NULL) {
  287. /* secret exponent length, must satisfy 2^(l-1) <= p */
  288. if (dh->length != 0
  289. && dh->length >= BN_num_bits(dh->params.p))
  290. goto err;
  291. l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1;
  292. if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE,
  293. BN_RAND_BOTTOM_ANY, 0, ctx))
  294. goto err;
  295. /*
  296. * We handle just one known case where g is a quadratic non-residue:
  297. * for g = 2: p % 8 == 3
  298. */
  299. if (BN_is_word(dh->params.g, DH_GENERATOR_2)
  300. && !BN_is_bit_set(dh->params.p, 2)) {
  301. /* clear bit 0, since it won't be a secret anyway */
  302. if (!BN_clear_bit(priv_key, 0))
  303. goto err;
  304. }
  305. } else
  306. #endif
  307. {
  308. /* Do a partial check for invalid p, q, g */
  309. if (!ossl_ffc_params_simple_validate(dh->libctx, &dh->params,
  310. FFC_PARAM_TYPE_DH, NULL))
  311. goto err;
  312. /*
  313. * For FFC FIPS 186-4 keygen
  314. * security strength s = 112,
  315. * Max Private key size N = len(q)
  316. */
  317. if (!ossl_ffc_generate_private_key(ctx, &dh->params,
  318. BN_num_bits(dh->params.q),
  319. MIN_STRENGTH,
  320. priv_key))
  321. goto err;
  322. }
  323. }
  324. }
  325. if (!ossl_dh_generate_public_key(ctx, dh, priv_key, pub_key))
  326. goto err;
  327. dh->pub_key = pub_key;
  328. dh->priv_key = priv_key;
  329. dh->dirty_cnt++;
  330. ok = 1;
  331. err:
  332. if (ok != 1)
  333. ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
  334. if (pub_key != dh->pub_key)
  335. BN_free(pub_key);
  336. if (priv_key != dh->priv_key)
  337. BN_free(priv_key);
  338. BN_CTX_free(ctx);
  339. return ok;
  340. }
  341. int ossl_dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
  342. {
  343. int err_reason = DH_R_BN_ERROR;
  344. BIGNUM *pubkey = NULL;
  345. const BIGNUM *p;
  346. int ret;
  347. if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
  348. goto err;
  349. DH_get0_pqg(dh, &p, NULL, NULL);
  350. if (p == NULL || BN_num_bytes(p) == 0) {
  351. err_reason = DH_R_NO_PARAMETERS_SET;
  352. goto err;
  353. }
  354. /* Prevent small subgroup attacks per RFC 8446 Section 4.2.8.1 */
  355. if (!ossl_dh_check_pub_key_partial(dh, pubkey, &ret)) {
  356. err_reason = DH_R_INVALID_PUBKEY;
  357. goto err;
  358. }
  359. if (DH_set0_key(dh, pubkey, NULL) != 1)
  360. goto err;
  361. return 1;
  362. err:
  363. ERR_raise(ERR_LIB_DH, err_reason);
  364. BN_free(pubkey);
  365. return 0;
  366. }
  367. size_t ossl_dh_key2buf(const DH *dh, unsigned char **pbuf_out, size_t size,
  368. int alloc)
  369. {
  370. const BIGNUM *pubkey;
  371. unsigned char *pbuf = NULL;
  372. const BIGNUM *p;
  373. int p_size;
  374. DH_get0_pqg(dh, &p, NULL, NULL);
  375. DH_get0_key(dh, &pubkey, NULL);
  376. if (p == NULL || pubkey == NULL
  377. || (p_size = BN_num_bytes(p)) == 0
  378. || BN_num_bytes(pubkey) == 0) {
  379. ERR_raise(ERR_LIB_DH, DH_R_INVALID_PUBKEY);
  380. return 0;
  381. }
  382. if (pbuf_out != NULL && (alloc || *pbuf_out != NULL)) {
  383. if (!alloc) {
  384. if (size >= (size_t)p_size)
  385. pbuf = *pbuf_out;
  386. if (pbuf == NULL)
  387. ERR_raise(ERR_LIB_DH, DH_R_INVALID_SIZE);
  388. } else {
  389. pbuf = OPENSSL_malloc(p_size);
  390. }
  391. /* Errors raised above */
  392. if (pbuf == NULL)
  393. return 0;
  394. /*
  395. * As per Section 4.2.8.1 of RFC 8446 left pad public
  396. * key with zeros to the size of p
  397. */
  398. if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
  399. if (alloc)
  400. OPENSSL_free(pbuf);
  401. ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
  402. return 0;
  403. }
  404. *pbuf_out = pbuf;
  405. }
  406. return p_size;
  407. }