dh_key.c 11 KB

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