InternalDHKeyExchange.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Nils Maier
  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 "InternalDHKeyExchange.h"
  36. #include <cstring>
  37. #include "DlAbortEx.h"
  38. #include "LogFactory.h"
  39. #include "fmt.h"
  40. #include "util.h"
  41. namespace aria2 {
  42. void DHKeyExchange::init(const unsigned char* prime, size_t primeBits,
  43. const unsigned char* generator, size_t privateKeyBits)
  44. {
  45. std::string pr = reinterpret_cast<const char*>(prime);
  46. if (pr.length() % 2) {
  47. pr = "0" + pr;
  48. }
  49. pr = util::fromHex(pr.begin(), pr.end());
  50. if (pr.empty()) {
  51. throw DL_ABORT_EX("No valid prime supplied");
  52. }
  53. prime_ = n(pr.c_str(), pr.length());
  54. std::string gen = reinterpret_cast<const char*>(generator);
  55. if (gen.length() % 2) {
  56. gen = "0" + gen;
  57. }
  58. gen = util::fromHex(gen.begin(), gen.end());
  59. if (gen.empty()) {
  60. throw DL_ABORT_EX("No valid generator supplied");
  61. }
  62. generator_ = n(gen.c_str(), gen.length());
  63. size_t pbytes = (privateKeyBits + 7) / 8;
  64. unsigned char buf[pbytes];
  65. util::generateRandomData(buf, pbytes);
  66. privateKey_ = n(reinterpret_cast<char*>(buf), pbytes);
  67. keyLength_ = (primeBits + 7) / 8;
  68. }
  69. void DHKeyExchange::generatePublicKey()
  70. {
  71. publicKey_ = generator_.mul_mod(privateKey_, prime_);
  72. }
  73. size_t DHKeyExchange::getPublicKey(unsigned char* out, size_t outLength) const
  74. {
  75. if (outLength < keyLength_) {
  76. throw DL_ABORT_EX(
  77. fmt("Insufficient buffer for public key. expect:%lu, actual:%lu",
  78. static_cast<unsigned long>(keyLength_),
  79. static_cast<unsigned long>(outLength)));
  80. }
  81. publicKey_.binary(reinterpret_cast<char*>(out), outLength);
  82. return keyLength_;
  83. }
  84. void DHKeyExchange::generateNonce(unsigned char* out, size_t outLength) const
  85. {
  86. util::generateRandomData(out, outLength);
  87. }
  88. size_t DHKeyExchange::computeSecret(unsigned char* out, size_t outLength,
  89. const unsigned char* peerPublicKeyData,
  90. size_t peerPublicKeyLength) const
  91. {
  92. if (outLength < keyLength_) {
  93. throw DL_ABORT_EX(
  94. fmt("Insufficient buffer for secret. expect:%lu, actual:%lu",
  95. static_cast<unsigned long>(keyLength_),
  96. static_cast<unsigned long>(outLength)));
  97. }
  98. if (prime_.length() < peerPublicKeyLength) {
  99. throw DL_ABORT_EX(
  100. fmt("peer public key overflows bignum. max:%lu, actual:%lu",
  101. static_cast<unsigned long>(prime_.length()),
  102. static_cast<unsigned long>(peerPublicKeyLength)));
  103. }
  104. n peerKey(reinterpret_cast<const char*>(peerPublicKeyData),
  105. peerPublicKeyLength);
  106. n secret = peerKey.mul_mod(privateKey_, prime_);
  107. secret.binary(reinterpret_cast<char*>(out), outLength);
  108. return outLength;
  109. }
  110. } // namespace aria2