LibgcryptDHKeyExchange.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 DlAbortEx
  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 DlAbortEx
  104. (StringFormat("Insufficient buffer for public key. expect:%u, actual:%u",
  105. _keyLength, outLength).str());
  106. }
  107. memset(out, 0, outLength);
  108. size_t publicKeyBytes = (gcry_mpi_get_nbits(_publicKey)+7)/8;
  109. size_t offset = _keyLength-publicKeyBytes;
  110. size_t nwritten;
  111. gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out+offset,
  112. outLength-offset, &nwritten, _publicKey);
  113. if(r) {
  114. handleError(r);
  115. }
  116. return nwritten;
  117. }
  118. void generateNonce(unsigned char* out, size_t outLength) const
  119. {
  120. gcry_create_nonce(out, outLength);
  121. }
  122. size_t computeSecret(unsigned char* out, size_t outLength,
  123. const unsigned char* peerPublicKeyData,
  124. size_t peerPublicKeyLength) const
  125. {
  126. if(outLength < _keyLength) {
  127. throw DlAbortEx
  128. (StringFormat("Insufficient buffer for secret. expect:%u, actual:%u",
  129. _keyLength, outLength).str());
  130. }
  131. gcry_mpi_t peerPublicKey;
  132. {
  133. gcry_error_t r = gcry_mpi_scan(&peerPublicKey, GCRYMPI_FMT_USG, peerPublicKeyData,
  134. peerPublicKeyLength, 0);
  135. if(r) {
  136. handleError(r);
  137. }
  138. }
  139. gcry_mpi_t secret = gcry_mpi_new(0);
  140. gcry_mpi_powm(secret, peerPublicKey, _privateKey, _prime);
  141. gcry_mpi_release(peerPublicKey);
  142. memset(out, 0, outLength);
  143. size_t secretBytes = (gcry_mpi_get_nbits(secret)+7)/8;
  144. size_t offset = _keyLength-secretBytes;
  145. size_t nwritten;
  146. {
  147. gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out+offset,
  148. outLength-offset, &nwritten, secret);
  149. gcry_mpi_release(secret);
  150. if(r) {
  151. handleError(r);
  152. }
  153. }
  154. return nwritten;
  155. }
  156. };
  157. } // namespace aria2
  158. #endif // _D_LIBGCRYPT_DH_KEY_EXCHANGE_H_