dh_key.c 12 KB

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