LibgmpDHKeyExchange.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2011 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "LibgmpDHKeyExchange.h"
  36. #include <cstring>
  37. #include "DlAbortEx.h"
  38. #include "fmt.h"
  39. #include "a2gmp.h"
  40. #include "util.h"
  41. namespace aria2 {
  42. namespace {
  43. void handleError(int err)
  44. {
  45. throw DL_ABORT_EX(
  46. fmt("Exception in libgmp routine(DHKeyExchange class): code%d", err));
  47. }
  48. } // namespace
  49. DHKeyExchange::DHKeyExchange() : keyLength_(0)
  50. {
  51. mpz_init(prime_);
  52. mpz_init(generator_);
  53. mpz_init(privateKey_);
  54. mpz_init(publicKey_);
  55. }
  56. DHKeyExchange::~DHKeyExchange()
  57. {
  58. mpz_clear(prime_);
  59. mpz_clear(generator_);
  60. mpz_clear(privateKey_);
  61. mpz_clear(publicKey_);
  62. }
  63. void DHKeyExchange::init(const unsigned char* prime, size_t primeBits,
  64. const unsigned char* generator, size_t privateKeyBits)
  65. {
  66. if (mpz_set_str(prime_, reinterpret_cast<const char*>(prime), 16) == -1) {
  67. handleError(-1);
  68. }
  69. if (mpz_set_str(generator_, reinterpret_cast<const char*>(generator), 16) ==
  70. -1) {
  71. handleError(-1);
  72. }
  73. mpz_urandomb(privateKey_, global::gmpRandstate, privateKeyBits);
  74. keyLength_ = (primeBits + 7) / 8;
  75. }
  76. void DHKeyExchange::generatePublicKey()
  77. {
  78. #if HAVE_GMP_SEC
  79. mpz_powm_sec(publicKey_, generator_, privateKey_, prime_);
  80. #else // HAVE_GMP_SEC
  81. mpz_powm(publicKey_, generator_, privateKey_, prime_);
  82. #endif // HAVE_GMP_SEC
  83. }
  84. size_t DHKeyExchange::getPublicKey(unsigned char* out, size_t outLength) const
  85. {
  86. if (outLength < keyLength_) {
  87. throw DL_ABORT_EX(
  88. fmt("Insufficient buffer for public key. expect:%lu, actual:%lu",
  89. static_cast<unsigned long>(keyLength_),
  90. static_cast<unsigned long>(outLength)));
  91. }
  92. memset(out, 0, outLength);
  93. size_t publicKeyBytes = (mpz_sizeinbase(publicKey_, 2) + 7) / 8;
  94. size_t offset = keyLength_ - publicKeyBytes;
  95. size_t nwritten;
  96. mpz_export(out + offset, &nwritten, 1, 1, 1, 0, publicKey_);
  97. return nwritten;
  98. }
  99. void DHKeyExchange::generateNonce(unsigned char* out, size_t outLength) const
  100. {
  101. util::generateRandomData(out, outLength);
  102. }
  103. size_t DHKeyExchange::computeSecret(unsigned char* out, size_t outLength,
  104. const unsigned char* peerPublicKeyData,
  105. size_t peerPublicKeyLength) const
  106. {
  107. if (outLength < keyLength_) {
  108. throw DL_ABORT_EX(
  109. fmt("Insufficient buffer for secret. expect:%lu, actual:%lu",
  110. static_cast<unsigned long>(keyLength_),
  111. static_cast<unsigned long>(outLength)));
  112. }
  113. mpz_t peerPublicKey;
  114. mpz_init(peerPublicKey);
  115. mpz_import(peerPublicKey, peerPublicKeyLength, 1, 1, 1, 0, peerPublicKeyData);
  116. mpz_t secret;
  117. mpz_init(secret);
  118. #if HAVE_GMP_SEC
  119. mpz_powm_sec(secret, peerPublicKey, privateKey_, prime_);
  120. #else // HAVE_GMP_SEC
  121. mpz_powm(secret, peerPublicKey, privateKey_, prime_);
  122. #endif // HAVE_GMP_SEC
  123. mpz_clear(peerPublicKey);
  124. memset(out, 0, outLength);
  125. size_t secretBytes = (mpz_sizeinbase(secret, 2) + 7) / 8;
  126. size_t offset = keyLength_ - secretBytes;
  127. size_t nwritten;
  128. mpz_export(out + offset, &nwritten, 1, 1, 1, 0, secret);
  129. mpz_clear(secret);
  130. return nwritten;
  131. }
  132. } // namespace aria2