LibgcryptDHKeyExchange.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. DHKeyExchange::~DHKeyExchange()
  55. {
  56. gcry_mpi_release(prime_);
  57. gcry_mpi_release(generator_);
  58. gcry_mpi_release(privateKey_);
  59. gcry_mpi_release(publicKey_);
  60. }
  61. void DHKeyExchange::init(const unsigned char* prime, size_t primeBits,
  62. const unsigned char* generator,
  63. 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,
  70. prime, 0, nullptr);
  71. if(r) {
  72. handleError(r);
  73. }
  74. }
  75. {
  76. gcry_error_t r = gcry_mpi_scan(&generator_, GCRYMPI_FMT_HEX,
  77. generator, 0, nullptr);
  78. if(r) {
  79. handleError(r);
  80. }
  81. }
  82. privateKey_ = gcry_mpi_new(0);
  83. gcry_mpi_randomize(privateKey_, privateKeyBits, GCRY_STRONG_RANDOM);
  84. keyLength_ = (primeBits+7)/8;
  85. }
  86. void DHKeyExchange::generatePublicKey()
  87. {
  88. gcry_mpi_release(publicKey_);
  89. publicKey_ = gcry_mpi_new(0);
  90. gcry_mpi_powm(publicKey_, generator_, privateKey_, prime_);
  91. }
  92. size_t DHKeyExchange::getPublicKey(unsigned char* out, size_t outLength) const
  93. {
  94. if(outLength < keyLength_) {
  95. throw DL_ABORT_EX
  96. (fmt("Insufficient buffer for public key. expect:%lu, actual:%lu",
  97. static_cast<unsigned long>(keyLength_),
  98. static_cast<unsigned long>(outLength)));
  99. }
  100. memset(out, 0, outLength);
  101. size_t publicKeyBytes = (gcry_mpi_get_nbits(publicKey_)+7)/8;
  102. size_t offset = keyLength_-publicKeyBytes;
  103. size_t nwritten;
  104. gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out+offset,
  105. outLength-offset, &nwritten, publicKey_);
  106. if(r) {
  107. handleError(r);
  108. }
  109. return nwritten;
  110. }
  111. void DHKeyExchange::generateNonce(unsigned char* out, size_t outLength) const
  112. {
  113. gcry_create_nonce(out, outLength);
  114. }
  115. size_t DHKeyExchange::computeSecret(unsigned char* out, size_t outLength,
  116. const unsigned char* peerPublicKeyData,
  117. size_t peerPublicKeyLength) const
  118. {
  119. if(outLength < keyLength_) {
  120. throw DL_ABORT_EX
  121. (fmt("Insufficient buffer for secret. expect:%lu, actual:%lu",
  122. static_cast<unsigned long>(keyLength_),
  123. static_cast<unsigned long>(outLength)));
  124. }
  125. gcry_mpi_t peerPublicKey;
  126. {
  127. gcry_error_t r = gcry_mpi_scan(&peerPublicKey,
  128. GCRYMPI_FMT_USG,
  129. peerPublicKeyData,
  130. peerPublicKeyLength,
  131. nullptr);
  132. if(r) {
  133. handleError(r);
  134. }
  135. }
  136. gcry_mpi_t secret = gcry_mpi_new(0);
  137. gcry_mpi_powm(secret, peerPublicKey, privateKey_, prime_);
  138. gcry_mpi_release(peerPublicKey);
  139. memset(out, 0, outLength);
  140. size_t secretBytes = (gcry_mpi_get_nbits(secret)+7)/8;
  141. size_t offset = keyLength_-secretBytes;
  142. size_t nwritten;
  143. {
  144. gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out+offset,
  145. outLength-offset, &nwritten, secret);
  146. gcry_mpi_release(secret);
  147. if(r) {
  148. handleError(r);
  149. }
  150. }
  151. return nwritten;
  152. }
  153. } // namespace aria2