LibgcryptDHKeyExchange.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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 "LibgcryptDHKeyExchange.h"
  36. #include "DlAbortEx.h"
  37. #include "fmt.h"
  38. namespace aria2 {
  39. namespace {
  40. void handleError(gcry_error_t err)
  41. {
  42. throw DL_ABORT_EX(
  43. fmt("Exception in libgcrypt routine(DHKeyExchange class): %s",
  44. gcry_strerror(err)));
  45. }
  46. } // namespace
  47. DHKeyExchange::DHKeyExchange()
  48. : keyLength_(0),
  49. prime_(nullptr),
  50. generator_(nullptr),
  51. privateKey_(nullptr),
  52. publicKey_(nullptr)
  53. {
  54. }
  55. DHKeyExchange::~DHKeyExchange()
  56. {
  57. gcry_mpi_release(prime_);
  58. gcry_mpi_release(generator_);
  59. gcry_mpi_release(privateKey_);
  60. gcry_mpi_release(publicKey_);
  61. }
  62. void DHKeyExchange::init(const unsigned char* prime, size_t primeBits,
  63. const unsigned char* generator, size_t privateKeyBits)
  64. {
  65. gcry_mpi_release(prime_);
  66. gcry_mpi_release(generator_);
  67. gcry_mpi_release(privateKey_);
  68. {
  69. gcry_error_t r = gcry_mpi_scan(&prime_, GCRYMPI_FMT_HEX, prime, 0, nullptr);
  70. if (r) {
  71. handleError(r);
  72. }
  73. }
  74. {
  75. gcry_error_t r =
  76. gcry_mpi_scan(&generator_, GCRYMPI_FMT_HEX, generator, 0, nullptr);
  77. if (r) {
  78. handleError(r);
  79. }
  80. }
  81. privateKey_ = gcry_mpi_new(0);
  82. gcry_mpi_randomize(privateKey_, privateKeyBits, GCRY_STRONG_RANDOM);
  83. keyLength_ = (primeBits + 7) / 8;
  84. }
  85. void DHKeyExchange::generatePublicKey()
  86. {
  87. gcry_mpi_release(publicKey_);
  88. publicKey_ = gcry_mpi_new(0);
  89. gcry_mpi_powm(publicKey_, generator_, privateKey_, prime_);
  90. }
  91. size_t DHKeyExchange::getPublicKey(unsigned char* out, size_t outLength) const
  92. {
  93. if (outLength < keyLength_) {
  94. throw DL_ABORT_EX(
  95. fmt("Insufficient buffer for public key. expect:%lu, actual:%lu",
  96. static_cast<unsigned long>(keyLength_),
  97. static_cast<unsigned long>(outLength)));
  98. }
  99. memset(out, 0, outLength);
  100. size_t publicKeyBytes = (gcry_mpi_get_nbits(publicKey_) + 7) / 8;
  101. size_t offset = keyLength_ - publicKeyBytes;
  102. size_t nwritten;
  103. gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out + offset,
  104. outLength - offset, &nwritten, publicKey_);
  105. if (r) {
  106. handleError(r);
  107. }
  108. return nwritten;
  109. }
  110. void DHKeyExchange::generateNonce(unsigned char* out, size_t outLength) const
  111. {
  112. gcry_create_nonce(out, outLength);
  113. }
  114. size_t DHKeyExchange::computeSecret(unsigned char* out, size_t outLength,
  115. const unsigned char* peerPublicKeyData,
  116. size_t peerPublicKeyLength) const
  117. {
  118. if (outLength < keyLength_) {
  119. throw DL_ABORT_EX(
  120. fmt("Insufficient buffer for secret. expect:%lu, actual:%lu",
  121. static_cast<unsigned long>(keyLength_),
  122. static_cast<unsigned long>(outLength)));
  123. }
  124. gcry_mpi_t peerPublicKey;
  125. {
  126. gcry_error_t r =
  127. gcry_mpi_scan(&peerPublicKey, GCRYMPI_FMT_USG, peerPublicKeyData,
  128. peerPublicKeyLength, nullptr);
  129. if (r) {
  130. handleError(r);
  131. }
  132. }
  133. gcry_mpi_t secret = gcry_mpi_new(0);
  134. gcry_mpi_powm(secret, peerPublicKey, privateKey_, prime_);
  135. gcry_mpi_release(peerPublicKey);
  136. memset(out, 0, outLength);
  137. size_t secretBytes = (gcry_mpi_get_nbits(secret) + 7) / 8;
  138. size_t offset = keyLength_ - secretBytes;
  139. size_t nwritten;
  140. {
  141. gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out + offset,
  142. outLength - offset, &nwritten, secret);
  143. gcry_mpi_release(secret);
  144. if (r) {
  145. handleError(r);
  146. }
  147. }
  148. return nwritten;
  149. }
  150. } // namespace aria2