dh_key.c 12 KB

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