dh_key.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 1995-2016 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. const DH_METHOD *DH_OpenSSL(void)
  53. {
  54. return &dh_ossl;
  55. }
  56. static int generate_key(DH *dh)
  57. {
  58. int ok = 0;
  59. int generate_new_key = 0;
  60. unsigned l;
  61. BN_CTX *ctx;
  62. BN_MONT_CTX *mont = NULL;
  63. BIGNUM *pub_key = NULL, *priv_key = NULL;
  64. ctx = BN_CTX_new();
  65. if (ctx == NULL)
  66. goto err;
  67. if (dh->priv_key == NULL) {
  68. priv_key = BN_secure_new();
  69. if (priv_key == NULL)
  70. goto err;
  71. generate_new_key = 1;
  72. } else
  73. priv_key = dh->priv_key;
  74. if (dh->pub_key == NULL) {
  75. pub_key = BN_new();
  76. if (pub_key == NULL)
  77. goto err;
  78. } else
  79. pub_key = dh->pub_key;
  80. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  81. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  82. dh->lock, dh->p, ctx);
  83. if (!mont)
  84. goto err;
  85. }
  86. if (generate_new_key) {
  87. if (dh->q) {
  88. do {
  89. if (!BN_rand_range(priv_key, dh->q))
  90. goto err;
  91. }
  92. while (BN_is_zero(priv_key) || BN_is_one(priv_key));
  93. } else {
  94. /* secret exponent length */
  95. l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
  96. if (!BN_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  97. goto err;
  98. }
  99. }
  100. {
  101. BIGNUM *prk = BN_new();
  102. if (prk == NULL)
  103. goto err;
  104. BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
  105. if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
  106. BN_free(prk);
  107. goto err;
  108. }
  109. /* We MUST free prk before any further use of priv_key */
  110. BN_free(prk);
  111. }
  112. dh->pub_key = pub_key;
  113. dh->priv_key = priv_key;
  114. ok = 1;
  115. err:
  116. if (ok != 1)
  117. DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
  118. if (pub_key != dh->pub_key)
  119. BN_free(pub_key);
  120. if (priv_key != dh->priv_key)
  121. BN_free(priv_key);
  122. BN_CTX_free(ctx);
  123. return (ok);
  124. }
  125. static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
  126. {
  127. BN_CTX *ctx = NULL;
  128. BN_MONT_CTX *mont = NULL;
  129. BIGNUM *tmp;
  130. int ret = -1;
  131. int check_result;
  132. if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
  133. DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
  134. goto err;
  135. }
  136. ctx = BN_CTX_new();
  137. if (ctx == NULL)
  138. goto err;
  139. BN_CTX_start(ctx);
  140. tmp = BN_CTX_get(ctx);
  141. if (dh->priv_key == NULL) {
  142. DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
  143. goto err;
  144. }
  145. if (dh->flags & DH_FLAG_CACHE_MONT_P) {
  146. mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
  147. dh->lock, dh->p, ctx);
  148. BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
  149. if (!mont)
  150. goto err;
  151. }
  152. if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
  153. DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
  154. goto err;
  155. }
  156. if (!dh->
  157. meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
  158. DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
  159. goto err;
  160. }
  161. ret = BN_bn2bin(tmp, key);
  162. err:
  163. if (ctx != NULL) {
  164. BN_CTX_end(ctx);
  165. BN_CTX_free(ctx);
  166. }
  167. return (ret);
  168. }
  169. static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
  170. const BIGNUM *a, const BIGNUM *p,
  171. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  172. {
  173. return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
  174. }
  175. static int dh_init(DH *dh)
  176. {
  177. dh->flags |= DH_FLAG_CACHE_MONT_P;
  178. return (1);
  179. }
  180. static int dh_finish(DH *dh)
  181. {
  182. BN_MONT_CTX_free(dh->method_mont_p);
  183. return (1);
  184. }