dh_key.c 12 KB

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