LibgcryptDHKeyExchange.h 4.8 KB

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