dh_key.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. /*-
  25. * NB: This function is inherently not constant time due to the
  26. * RFC 5246 (8.1.2) padding style that strips leading zero bytes.
  27. */
  28. int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  29. {
  30. int ret = 0, i;
  31. volatile size_t npad = 0, mask = 1;
  32. /* compute the key; ret is constant unless compute_key is external */
  33. if ((ret = dh->meth->compute_key(key, pub_key, dh)) <= 0)
  34. return ret;
  35. /* count leading zero bytes, yet still touch all bytes */
  36. for (i = 0; i < ret; i++) {
  37. mask &= !key[i];
  38. npad += mask;
  39. }
  40. /* unpad key */
  41. ret -= npad;
  42. /* key-dependent memory access, potentially leaking npad / ret */
  43. memmove(key, key + npad, ret);
  44. /* key-dependent memory access, potentially leaking npad / ret */
  45. memset(key + ret, 0, npad);
  46. return ret;
  47. }
  48. int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  49. {
  50. int rv, pad;
  51. /* rv is constant unless compute_key is external */
  52. rv = dh->meth->compute_key(key, pub_key, dh);
  53. if (rv <= 0)
  54. return rv;
  55. pad = BN_num_bytes(dh->p) - rv;
  56. /* pad is constant (zero) unless compute_key is external */
  57. if (pad > 0) {
  58. memmove(key + pad, key, rv);
  59. memset(key, 0, pad);
  60. }
  61. return rv + pad;
  62. }
  63. static DH_METHOD dh_ossl = {
  64. "OpenSSL DH Method",
  65. generate_key,
  66. compute_key,
  67. dh_bn_mod_exp,
  68. dh_init,
  69. dh_finish,
  70. DH_FLAG_FIPS_METHOD,
  71. NULL,
  72. NULL
  73. };
  74. static const DH_METHOD *default_DH_method = &dh_ossl;
  75. const DH_METHOD *DH_OpenSSL(void)
  76. {
  77. return &dh_ossl;
  78. }
  79. void DH_set_default_method(const DH_METHOD *meth)
  80. {
  81. default_DH_method = meth;
  82. }
  83. const DH_METHOD *DH_get_default_method(void)
  84. {
  85. return default_DH_method;
  86. }
  87. static int generate_key(DH *dh)
  88. {
  89. int ok = 0;
  90. int generate_new_key = 0;
  91. unsigned l;
  92. BN_CTX *ctx = NULL;
  93. BN_MONT_CTX *mont = NULL;
  94. BIGNUM *pub_key = NULL, *priv_key = NULL;
  95. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  96. DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_LARGE);
  97. return 0;
  98. }
  99. ctx = BN_CTX_new();
  100. if (ctx == NULL)
  101. goto err;
  102. if (dh->priv_key == NULL) {
  103. priv_key = BN_secure_new();
  104. if (priv_key == NULL)
  105. goto err;
  106. generate_new_key = 1;
  107. } else
  108. priv_key = dh->priv_key;
  109. if (dh->pub_key == NULL) {
  110. pub_key = BN_new();
  111. if (pub_key == NULL)
  112. goto err;
  113. } else
  114. pub_key = dh->pub_key;
  115. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  116. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  117. dh->lock, dh->p, ctx);
  118. if (!mont)
  119. goto err;
  120. }
  121. if (generate_new_key) {
  122. if (dh->q) {
  123. do {
  124. if (!BN_priv_rand_range(priv_key, dh->q))
  125. goto err;
  126. }
  127. while (BN_is_zero(priv_key) || BN_is_one(priv_key));
  128. } else {
  129. /* secret exponent length */
  130. l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
  131. if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  132. goto err;
  133. /*
  134. * We handle just one known case where g is a quadratic non-residue:
  135. * for g = 2: p % 8 == 3
  136. */
  137. if (BN_is_word(dh->g, DH_GENERATOR_2) && !BN_is_bit_set(dh->p, 2)) {
  138. /* clear bit 0, since it won't be a secret anyway */
  139. if (!BN_clear_bit(priv_key, 0))
  140. goto err;
  141. }
  142. }
  143. }
  144. {
  145. BIGNUM *prk = BN_new();
  146. if (prk == NULL)
  147. goto err;
  148. BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
  149. if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
  150. BN_clear_free(prk);
  151. goto err;
  152. }
  153. /* We MUST free prk before any further use of priv_key */
  154. BN_clear_free(prk);
  155. }
  156. dh->pub_key = pub_key;
  157. dh->priv_key = priv_key;
  158. ok = 1;
  159. err:
  160. if (ok != 1)
  161. DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
  162. if (pub_key != dh->pub_key)
  163. BN_free(pub_key);
  164. if (priv_key != dh->priv_key)
  165. BN_free(priv_key);
  166. BN_CTX_free(ctx);
  167. return ok;
  168. }
  169. static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  170. {
  171. BN_CTX *ctx = NULL;
  172. BN_MONT_CTX *mont = NULL;
  173. BIGNUM *tmp;
  174. int ret = -1;
  175. int check_result;
  176. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  177. DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
  178. goto err;
  179. }
  180. ctx = BN_CTX_new();
  181. if (ctx == NULL)
  182. goto err;
  183. BN_CTX_start(ctx);
  184. tmp = BN_CTX_get(ctx);
  185. if (tmp == NULL)
  186. goto err;
  187. if (dh->priv_key == NULL) {
  188. DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
  189. goto err;
  190. }
  191. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  192. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  193. dh->lock, dh->p, ctx);
  194. BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
  195. if (!mont)
  196. goto err;
  197. }
  198. if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
  199. DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
  200. goto err;
  201. }
  202. if (!dh->
  203. meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
  204. DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
  205. goto err;
  206. }
  207. ret = BN_bn2binpad(tmp, key, BN_num_bytes(dh->p));
  208. err:
  209. BN_CTX_end(ctx);
  210. BN_CTX_free(ctx);
  211. return ret;
  212. }
  213. static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
  214. const BIGNUM *a, const BIGNUM *p,
  215. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  216. {
  217. return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
  218. }
  219. static int dh_init(DH *dh)
  220. {
  221. dh->flags |= DH_FLAG_CACHE_MONT_P;
  222. return 1;
  223. }
  224. static int dh_finish(DH *dh)
  225. {
  226. BN_MONT_CTX_free(dh->method_mont_p);
  227. return 1;
  228. }