dh_key.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 1995-2019 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_locl.h"
  12. #include "internal/bn_int.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. ctx = BN_CTX_new();
  78. if (ctx == NULL)
  79. goto err;
  80. if (dh->priv_key == NULL) {
  81. priv_key = BN_secure_new();
  82. if (priv_key == NULL)
  83. goto err;
  84. generate_new_key = 1;
  85. } else
  86. priv_key = dh->priv_key;
  87. if (dh->pub_key == NULL) {
  88. pub_key = BN_new();
  89. if (pub_key == NULL)
  90. goto err;
  91. } else
  92. pub_key = dh->pub_key;
  93. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  94. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  95. dh->lock, dh->p, ctx);
  96. if (!mont)
  97. goto err;
  98. }
  99. if (generate_new_key) {
  100. if (dh->q) {
  101. do {
  102. if (!BN_priv_rand_range(priv_key, dh->q))
  103. goto err;
  104. }
  105. while (BN_is_zero(priv_key) || BN_is_one(priv_key));
  106. } else {
  107. /* secret exponent length */
  108. l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
  109. if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  110. goto err;
  111. /*
  112. * We handle just one known case where g is a quadratic non-residue:
  113. * for g = 2: p % 8 == 3
  114. */
  115. if (BN_is_word(dh->g, DH_GENERATOR_2) && !BN_is_bit_set(dh->p, 2)) {
  116. /* clear bit 0, since it won't be a secret anyway */
  117. if (!BN_clear_bit(priv_key, 0))
  118. goto err;
  119. }
  120. }
  121. }
  122. {
  123. BIGNUM *prk = BN_new();
  124. if (prk == NULL)
  125. goto err;
  126. BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
  127. if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
  128. BN_clear_free(prk);
  129. goto err;
  130. }
  131. /* We MUST free prk before any further use of priv_key */
  132. BN_clear_free(prk);
  133. }
  134. dh->pub_key = pub_key;
  135. dh->priv_key = priv_key;
  136. ok = 1;
  137. err:
  138. if (ok != 1)
  139. DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
  140. if (pub_key != dh->pub_key)
  141. BN_free(pub_key);
  142. if (priv_key != dh->priv_key)
  143. BN_free(priv_key);
  144. BN_CTX_free(ctx);
  145. return ok;
  146. }
  147. static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  148. {
  149. BN_CTX *ctx = NULL;
  150. BN_MONT_CTX *mont = NULL;
  151. BIGNUM *tmp;
  152. int ret = -1;
  153. int check_result;
  154. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  155. DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
  156. goto err;
  157. }
  158. ctx = BN_CTX_new();
  159. if (ctx == NULL)
  160. goto err;
  161. BN_CTX_start(ctx);
  162. tmp = BN_CTX_get(ctx);
  163. if (tmp == NULL)
  164. goto err;
  165. if (dh->priv_key == NULL) {
  166. DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
  167. goto err;
  168. }
  169. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  170. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  171. dh->lock, dh->p, ctx);
  172. BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
  173. if (!mont)
  174. goto err;
  175. }
  176. if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
  177. DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
  178. goto err;
  179. }
  180. if (!dh->
  181. meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
  182. DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
  183. goto err;
  184. }
  185. ret = BN_bn2bin(tmp, key);
  186. err:
  187. BN_CTX_end(ctx);
  188. BN_CTX_free(ctx);
  189. return ret;
  190. }
  191. static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
  192. const BIGNUM *a, const BIGNUM *p,
  193. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  194. {
  195. return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
  196. }
  197. static int dh_init(DH *dh)
  198. {
  199. dh->flags |= DH_FLAG_CACHE_MONT_P;
  200. return 1;
  201. }
  202. static int dh_finish(DH *dh)
  203. {
  204. BN_MONT_CTX_free(dh->method_mont_p);
  205. return 1;
  206. }