dh_key.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 1995-2018 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. }
  113. {
  114. BIGNUM *prk = BN_new();
  115. if (prk == NULL)
  116. goto err;
  117. BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
  118. if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
  119. BN_free(prk);
  120. goto err;
  121. }
  122. /* We MUST free prk before any further use of priv_key */
  123. BN_free(prk);
  124. }
  125. dh->pub_key = pub_key;
  126. dh->priv_key = priv_key;
  127. ok = 1;
  128. err:
  129. if (ok != 1)
  130. DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
  131. if (pub_key != dh->pub_key)
  132. BN_free(pub_key);
  133. if (priv_key != dh->priv_key)
  134. BN_free(priv_key);
  135. BN_CTX_free(ctx);
  136. return ok;
  137. }
  138. static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  139. {
  140. BN_CTX *ctx = NULL;
  141. BN_MONT_CTX *mont = NULL;
  142. BIGNUM *tmp;
  143. int ret = -1;
  144. int check_result;
  145. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  146. DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
  147. goto err;
  148. }
  149. ctx = BN_CTX_new();
  150. if (ctx == NULL)
  151. goto err;
  152. BN_CTX_start(ctx);
  153. tmp = BN_CTX_get(ctx);
  154. if (tmp == NULL)
  155. goto err;
  156. if (dh->priv_key == NULL) {
  157. DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
  158. goto err;
  159. }
  160. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  161. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  162. dh->lock, dh->p, ctx);
  163. BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
  164. if (!mont)
  165. goto err;
  166. }
  167. if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
  168. DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
  169. goto err;
  170. }
  171. if (!dh->
  172. meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
  173. DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
  174. goto err;
  175. }
  176. ret = BN_bn2bin(tmp, key);
  177. err:
  178. if (ctx != NULL) {
  179. BN_CTX_end(ctx);
  180. BN_CTX_free(ctx);
  181. }
  182. return ret;
  183. }
  184. static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
  185. const BIGNUM *a, const BIGNUM *p,
  186. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  187. {
  188. return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
  189. }
  190. static int dh_init(DH *dh)
  191. {
  192. dh->flags |= DH_FLAG_CACHE_MONT_P;
  193. return 1;
  194. }
  195. static int dh_finish(DH *dh)
  196. {
  197. BN_MONT_CTX_free(dh->method_mont_p);
  198. return 1;
  199. }