LibgcryptDHKeyExchange.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <gcrypt.h>
  40. namespace aria2 {
  41. class DHKeyExchange {
  42. private:
  43. gcry_mpi_t _prime;
  44. gcry_mpi_t _generator;
  45. gcry_mpi_t _privateKey;
  46. gcry_mpi_t _publicKey;
  47. void handleError(int errno) const
  48. {
  49. throw new DlAbortEx("Exception in libgcrypt routine(DHKeyExchange class): %s",
  50. gcry_strerror(errno));
  51. }
  52. public:
  53. DHKeyExchange():_prime(0),
  54. _generator(0),
  55. _privateKey(0),
  56. _publicKey(0) {}
  57. ~DHKeyExchange()
  58. {
  59. gcry_mpi_release(_prime);
  60. gcry_mpi_release(_generator);
  61. gcry_mpi_release(_privateKey);
  62. gcry_mpi_release(_publicKey);
  63. }
  64. void init(const unsigned char* prime, const unsigned char* generator,
  65. size_t privateKeyBits)
  66. {
  67. gcry_mpi_release(_prime);
  68. gcry_mpi_release(_generator);
  69. gcry_mpi_release(_privateKey);
  70. {
  71. gcry_error_t r = gcry_mpi_scan(&_prime, GCRYMPI_FMT_HEX,
  72. prime, 0, 0);
  73. if(r) {
  74. handleError(r);
  75. }
  76. }
  77. {
  78. gcry_error_t r = gcry_mpi_scan(&_generator, GCRYMPI_FMT_HEX,
  79. generator, 0, 0);
  80. if(r) {
  81. handleError(r);
  82. }
  83. }
  84. _privateKey = gcry_mpi_new(0);
  85. gcry_mpi_randomize(_privateKey, privateKeyBits, GCRY_STRONG_RANDOM);
  86. }
  87. void generatePublicKey()
  88. {
  89. gcry_mpi_release(_publicKey);
  90. _publicKey = gcry_mpi_new(0);
  91. gcry_mpi_powm(_publicKey, _generator, _privateKey, _prime);
  92. }
  93. size_t publicKeyLength() const
  94. {
  95. return (gcry_mpi_get_nbits(_publicKey)+7)/8;
  96. }
  97. size_t getPublicKey(unsigned char* out, size_t outLength) const
  98. {
  99. if(outLength < publicKeyLength()) {
  100. throw new DlAbortEx("Insufficient buffer for public key. expect:%u, actual:%u",
  101. publicKeyLength(), outLength);
  102. }
  103. size_t nwritten;
  104. gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out, outLength, &nwritten, _publicKey);
  105. if(r) {
  106. handleError(r);
  107. }
  108. return nwritten;
  109. }
  110. void generateNonce(unsigned char* out, size_t outLength) const
  111. {
  112. gcry_create_nonce(out, outLength);
  113. }
  114. size_t computeSecret(unsigned char* out, size_t outLength,
  115. const unsigned char* peerPublicKeyData,
  116. size_t peerPublicKeyLength) const
  117. {
  118. if(outLength < publicKeyLength()) {
  119. throw new DlAbortEx("Insufficient buffer for secret. expect:%u, actual:%u",
  120. publicKeyLength(), outLength);
  121. }
  122. gcry_mpi_t peerPublicKey;
  123. {
  124. gcry_error_t r = gcry_mpi_scan(&peerPublicKey, GCRYMPI_FMT_USG, peerPublicKeyData,
  125. peerPublicKeyLength, 0);
  126. if(r) {
  127. handleError(r);
  128. }
  129. }
  130. gcry_mpi_t secret = gcry_mpi_new(0);
  131. gcry_mpi_powm(secret, peerPublicKey, _privateKey, _prime);
  132. gcry_mpi_release(peerPublicKey);
  133. size_t nwritten;
  134. {
  135. gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out, outLength, &nwritten, secret);
  136. gcry_mpi_release(secret);
  137. if(r) {
  138. handleError(r);
  139. }
  140. }
  141. return nwritten;
  142. }
  143. };
  144. } // namespace aria2
  145. #endif // _D_LIBGCRYPT_DH_KEY_EXCHANGE_H_