LibgcryptDHKeyExchange.h 5.3 KB

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