dh_key.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 1995-2019 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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "dh_local.h"
  12. #include "crypto/bn.h"
  13. static int generate_key(DH *dh);
  14. static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
  15. static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
  16. const BIGNUM *a, const BIGNUM *p,
  17. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  18. static int dh_init(DH *dh);
  19. static int dh_finish(DH *dh);
  20. int DH_generate_key(DH *dh)
  21. {
  22. return dh->meth->generate_key(dh);
  23. }
  24. int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  25. {
  26. return dh->meth->compute_key(key, pub_key, dh);
  27. }
  28. int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  29. {
  30. int rv, pad;
  31. rv = dh->meth->compute_key(key, pub_key, dh);
  32. if (rv <= 0)
  33. return rv;
  34. pad = BN_num_bytes(dh->p) - rv;
  35. if (pad > 0) {
  36. memmove(key + pad, key, rv);
  37. memset(key, 0, pad);
  38. }
  39. return rv + pad;
  40. }
  41. static DH_METHOD dh_ossl = {
  42. "OpenSSL DH Method",
  43. generate_key,
  44. compute_key,
  45. dh_bn_mod_exp,
  46. dh_init,
  47. dh_finish,
  48. DH_FLAG_FIPS_METHOD,
  49. NULL,
  50. NULL
  51. };
  52. static const DH_METHOD *default_DH_method = &dh_ossl;
  53. const DH_METHOD *DH_OpenSSL(void)
  54. {
  55. return &dh_ossl;
  56. }
  57. void DH_set_default_method(const DH_METHOD *meth)
  58. {
  59. default_DH_method = meth;
  60. }
  61. const DH_METHOD *DH_get_default_method(void)
  62. {
  63. return default_DH_method;
  64. }
  65. static int generate_key(DH *dh)
  66. {
  67. int ok = 0;
  68. int generate_new_key = 0;
  69. unsigned l;
  70. BN_CTX *ctx = NULL;
  71. BN_MONT_CTX *mont = NULL;
  72. BIGNUM *pub_key = NULL, *priv_key = NULL;
  73. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  74. DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_LARGE);
  75. return 0;
  76. }
  77. if (BN_num_bits(dh->p) < DH_MIN_MODULUS_BITS) {
  78. DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_SMALL);
  79. return 0;
  80. }
  81. ctx = BN_CTX_new();
  82. if (ctx == NULL)
  83. goto err;
  84. if (dh->priv_key == NULL) {
  85. priv_key = BN_secure_new();
  86. if (priv_key == NULL)
  87. goto err;
  88. generate_new_key = 1;
  89. } else
  90. priv_key = dh->priv_key;
  91. if (dh->pub_key == NULL) {
  92. pub_key = BN_new();
  93. if (pub_key == NULL)
  94. goto err;
  95. } else
  96. pub_key = dh->pub_key;
  97. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  98. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  99. dh->lock, dh->p, ctx);
  100. if (!mont)
  101. goto err;
  102. }
  103. if (generate_new_key) {
  104. if (dh->q) {
  105. do {
  106. if (!BN_priv_rand_range(priv_key, dh->q))
  107. goto err;
  108. }
  109. while (BN_is_zero(priv_key) || BN_is_one(priv_key));
  110. } else {
  111. /* secret exponent length */
  112. l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
  113. if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  114. goto err;
  115. /*
  116. * We handle just one known case where g is a quadratic non-residue:
  117. * for g = 2: p % 8 == 3
  118. */
  119. if (BN_is_word(dh->g, DH_GENERATOR_2) && !BN_is_bit_set(dh->p, 2)) {
  120. /* clear bit 0, since it won't be a secret anyway */
  121. if (!BN_clear_bit(priv_key, 0))
  122. goto err;
  123. }
  124. }
  125. }
  126. {
  127. BIGNUM *prk = BN_new();
  128. if (prk == NULL)
  129. goto err;
  130. BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
  131. if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
  132. BN_clear_free(prk);
  133. goto err;
  134. }
  135. /* We MUST free prk before any further use of priv_key */
  136. BN_clear_free(prk);
  137. }
  138. dh->pub_key = pub_key;
  139. dh->priv_key = priv_key;
  140. dh->dirty_cnt++;
  141. ok = 1;
  142. err:
  143. if (ok != 1)
  144. DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
  145. if (pub_key != dh->pub_key)
  146. BN_free(pub_key);
  147. if (priv_key != dh->priv_key)
  148. BN_free(priv_key);
  149. BN_CTX_free(ctx);
  150. return ok;
  151. }
  152. static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  153. {
  154. BN_CTX *ctx = NULL;
  155. BN_MONT_CTX *mont = NULL;
  156. BIGNUM *tmp;
  157. int ret = -1;
  158. int check_result;
  159. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  160. DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
  161. goto err;
  162. }
  163. if (BN_num_bits(dh->p) < DH_MIN_MODULUS_BITS) {
  164. DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_SMALL);
  165. return 0;
  166. }
  167. ctx = BN_CTX_new();
  168. if (ctx == NULL)
  169. goto err;
  170. BN_CTX_start(ctx);
  171. tmp = BN_CTX_get(ctx);
  172. if (tmp == NULL)
  173. goto err;
  174. if (dh->priv_key == NULL) {
  175. DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
  176. goto err;
  177. }
  178. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  179. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  180. dh->lock, dh->p, ctx);
  181. BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
  182. if (!mont)
  183. goto err;
  184. }
  185. if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
  186. DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
  187. goto err;
  188. }
  189. if (!dh->
  190. meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
  191. DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
  192. goto err;
  193. }
  194. ret = BN_bn2bin(tmp, key);
  195. err:
  196. BN_CTX_end(ctx);
  197. BN_CTX_free(ctx);
  198. return ret;
  199. }
  200. static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
  201. const BIGNUM *a, const BIGNUM *p,
  202. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  203. {
  204. return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
  205. }
  206. static int dh_init(DH *dh)
  207. {
  208. dh->flags |= DH_FLAG_CACHE_MONT_P;
  209. return 1;
  210. }
  211. static int dh_finish(DH *dh)
  212. {
  213. BN_MONT_CTX_free(dh->method_mont_p);
  214. return 1;
  215. }
  216. int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
  217. {
  218. int err_reason = DH_R_BN_ERROR;
  219. BIGNUM *pubkey = NULL;
  220. const BIGNUM *p;
  221. size_t p_size;
  222. if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
  223. goto err;
  224. DH_get0_pqg(dh, &p, NULL, NULL);
  225. if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
  226. err_reason = DH_R_NO_PARAMETERS_SET;
  227. goto err;
  228. }
  229. /*
  230. * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
  231. * public key is of size not equal to size of p
  232. */
  233. if (BN_is_zero(pubkey) || p_size != len) {
  234. err_reason = DH_R_INVALID_PUBKEY;
  235. goto err;
  236. }
  237. if (DH_set0_key(dh, pubkey, NULL) != 1)
  238. goto err;
  239. return 1;
  240. err:
  241. DHerr(DH_F_DH_BUF2KEY, err_reason);
  242. BN_free(pubkey);
  243. return 0;
  244. }
  245. size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
  246. {
  247. const BIGNUM *pubkey;
  248. unsigned char *pbuf;
  249. const BIGNUM *p;
  250. int p_size;
  251. DH_get0_pqg(dh, &p, NULL, NULL);
  252. DH_get0_key(dh, &pubkey, NULL);
  253. if (p == NULL || pubkey == NULL
  254. || (p_size = BN_num_bytes(p)) == 0
  255. || BN_num_bytes(pubkey) == 0) {
  256. DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY);
  257. return 0;
  258. }
  259. if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
  260. DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
  261. return 0;
  262. }
  263. /*
  264. * As per Section 4.2.8.1 of RFC 8446 left pad public
  265. * key with zeros to the size of p
  266. */
  267. if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
  268. OPENSSL_free(pbuf);
  269. DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);
  270. return 0;
  271. }
  272. *pbuf_out = pbuf;
  273. return p_size;
  274. }