dh_key.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
  163. }
  164. static int dh_init(DH *dh)
  165. {
  166. dh->flags |= DH_FLAG_CACHE_MONT_P;
  167. dh->dirty_cnt++;
  168. return 1;
  169. }
  170. static int dh_finish(DH *dh)
  171. {
  172. BN_MONT_CTX_free(dh->method_mont_p);
  173. return 1;
  174. }
  175. #ifndef FIPS_MODULE
  176. void DH_set_default_method(const DH_METHOD *meth)
  177. {
  178. default_DH_method = meth;
  179. }
  180. #endif /* FIPS_MODULE */
  181. int DH_generate_key(DH *dh)
  182. {
  183. #ifdef FIPS_MODULE
  184. return generate_key(dh);
  185. #else
  186. return dh->meth->generate_key(dh);
  187. #endif
  188. }
  189. int ossl_dh_generate_public_key(BN_CTX *ctx, const DH *dh,
  190. const BIGNUM *priv_key, BIGNUM *pub_key)
  191. {
  192. int ret = 0;
  193. BIGNUM *prk = BN_new();
  194. BN_MONT_CTX *mont = NULL;
  195. if (prk == NULL)
  196. return 0;
  197. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  198. /*
  199. * We take the input DH as const, but we lie, because in some cases we
  200. * want to get a hold of its Montgomery context.
  201. *
  202. * We cast to remove the const qualifier in this case, it should be
  203. * fine...
  204. */
  205. BN_MONT_CTX **pmont = (BN_MONT_CTX **)&dh->method_mont_p;
  206. mont = BN_MONT_CTX_set_locked(pmont, dh->lock, dh->params.p, ctx);
  207. if (mont == NULL)
  208. goto err;
  209. }
  210. BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
  211. /* pub_key = g^priv_key mod p */
  212. if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
  213. ctx, mont))
  214. goto err;
  215. ret = 1;
  216. err:
  217. BN_clear_free(prk);
  218. return ret;
  219. }
  220. static int generate_key(DH *dh)
  221. {
  222. int ok = 0;
  223. int generate_new_key = 0;
  224. #ifndef FIPS_MODULE
  225. unsigned l;
  226. #endif
  227. BN_CTX *ctx = NULL;
  228. BIGNUM *pub_key = NULL, *priv_key = NULL;
  229. if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  230. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
  231. return 0;
  232. }
  233. if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
  234. ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
  235. return 0;
  236. }
  237. ctx = BN_CTX_new_ex(dh->libctx);
  238. if (ctx == NULL)
  239. goto err;
  240. if (dh->priv_key == NULL) {
  241. priv_key = BN_secure_new();
  242. if (priv_key == NULL)
  243. goto err;
  244. generate_new_key = 1;
  245. } else {
  246. priv_key = dh->priv_key;
  247. }
  248. if (dh->pub_key == NULL) {
  249. pub_key = BN_new();
  250. if (pub_key == NULL)
  251. goto err;
  252. } else {
  253. pub_key = dh->pub_key;
  254. }
  255. if (generate_new_key) {
  256. /* Is it an approved safe prime ?*/
  257. if (DH_get_nid(dh) != NID_undef) {
  258. int max_strength =
  259. ossl_ifc_ffc_compute_security_bits(BN_num_bits(dh->params.p));
  260. if (dh->params.q == NULL
  261. || dh->length > BN_num_bits(dh->params.q))
  262. goto err;
  263. /* dh->length = maximum bit length of generated private key */
  264. if (!ossl_ffc_generate_private_key(ctx, &dh->params, dh->length,
  265. max_strength, priv_key))
  266. goto err;
  267. } else {
  268. #ifdef FIPS_MODULE
  269. if (dh->params.q == NULL)
  270. goto err;
  271. #else
  272. if (dh->params.q == NULL) {
  273. /* secret exponent length, must satisfy 2^(l-1) <= p */
  274. if (dh->length != 0
  275. && dh->length >= BN_num_bits(dh->params.p))
  276. goto err;
  277. l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1;
  278. if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE,
  279. BN_RAND_BOTTOM_ANY, 0, ctx))
  280. goto err;
  281. /*
  282. * We handle just one known case where g is a quadratic non-residue:
  283. * for g = 2: p % 8 == 3
  284. */
  285. if (BN_is_word(dh->params.g, DH_GENERATOR_2)
  286. && !BN_is_bit_set(dh->params.p, 2)) {
  287. /* clear bit 0, since it won't be a secret anyway */
  288. if (!BN_clear_bit(priv_key, 0))
  289. goto err;
  290. }
  291. } else
  292. #endif
  293. {
  294. /* Do a partial check for invalid p, q, g */
  295. if (!ossl_ffc_params_simple_validate(dh->libctx, &dh->params,
  296. FFC_PARAM_TYPE_DH, NULL))
  297. goto err;
  298. /*
  299. * For FFC FIPS 186-4 keygen
  300. * security strength s = 112,
  301. * Max Private key size N = len(q)
  302. */
  303. if (!ossl_ffc_generate_private_key(ctx, &dh->params,
  304. BN_num_bits(dh->params.q),
  305. MIN_STRENGTH,
  306. priv_key))
  307. goto err;
  308. }
  309. }
  310. }
  311. if (!ossl_dh_generate_public_key(ctx, dh, priv_key, pub_key))
  312. goto err;
  313. dh->pub_key = pub_key;
  314. dh->priv_key = priv_key;
  315. dh->dirty_cnt++;
  316. ok = 1;
  317. err:
  318. if (ok != 1)
  319. ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
  320. if (pub_key != dh->pub_key)
  321. BN_free(pub_key);
  322. if (priv_key != dh->priv_key)
  323. BN_free(priv_key);
  324. BN_CTX_free(ctx);
  325. return ok;
  326. }
  327. int ossl_dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
  328. {
  329. int err_reason = DH_R_BN_ERROR;
  330. BIGNUM *pubkey = NULL;
  331. const BIGNUM *p;
  332. int ret;
  333. if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
  334. goto err;
  335. DH_get0_pqg(dh, &p, NULL, NULL);
  336. if (p == NULL || BN_num_bytes(p) == 0) {
  337. err_reason = DH_R_NO_PARAMETERS_SET;
  338. goto err;
  339. }
  340. /* Prevent small subgroup attacks per RFC 8446 Section 4.2.8.1 */
  341. if (!ossl_dh_check_pub_key_partial(dh, pubkey, &ret)) {
  342. err_reason = DH_R_INVALID_PUBKEY;
  343. goto err;
  344. }
  345. if (DH_set0_key(dh, pubkey, NULL) != 1)
  346. goto err;
  347. return 1;
  348. err:
  349. ERR_raise(ERR_LIB_DH, err_reason);
  350. BN_free(pubkey);
  351. return 0;
  352. }
  353. size_t ossl_dh_key2buf(const DH *dh, unsigned char **pbuf_out, size_t size,
  354. int alloc)
  355. {
  356. const BIGNUM *pubkey;
  357. unsigned char *pbuf = NULL;
  358. const BIGNUM *p;
  359. int p_size;
  360. DH_get0_pqg(dh, &p, NULL, NULL);
  361. DH_get0_key(dh, &pubkey, NULL);
  362. if (p == NULL || pubkey == NULL
  363. || (p_size = BN_num_bytes(p)) == 0
  364. || BN_num_bytes(pubkey) == 0) {
  365. ERR_raise(ERR_LIB_DH, DH_R_INVALID_PUBKEY);
  366. return 0;
  367. }
  368. if (pbuf_out != NULL && (alloc || *pbuf_out != NULL)) {
  369. if (!alloc) {
  370. if (size >= (size_t)p_size)
  371. pbuf = *pbuf_out;
  372. } else {
  373. pbuf = OPENSSL_malloc(p_size);
  374. }
  375. if (pbuf == NULL) {
  376. ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
  377. return 0;
  378. }
  379. /*
  380. * As per Section 4.2.8.1 of RFC 8446 left pad public
  381. * key with zeros to the size of p
  382. */
  383. if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
  384. if (alloc)
  385. OPENSSL_free(pbuf);
  386. ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
  387. return 0;
  388. }
  389. *pbuf_out = pbuf;
  390. }
  391. return p_size;
  392. }