util_security.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2014 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 "util_security.h"
  36. #include "FatalException.h"
  37. #include "util.h"
  38. namespace {
  39. using namespace aria2;
  40. static inline size_t getBlockSize(const std::string& algorithm)
  41. {
  42. std::string canon;
  43. if (!MessageDigest::supports(algorithm)) {
  44. goto err;
  45. }
  46. canon = MessageDigest::getCanonicalHashType(algorithm);
  47. // Per RFC 6234
  48. if (canon == "sha-1") {
  49. return 64;
  50. }
  51. if (canon == "sha-224") {
  52. return 64;
  53. }
  54. if (canon == "sha-256") {
  55. return 64;
  56. }
  57. if (canon == "sha-384") {
  58. return 128;
  59. }
  60. if (canon == "sha-512") {
  61. // RFC 4868
  62. return 128;
  63. }
  64. err:
  65. throw FATAL_EXCEPTION(
  66. fmt("HMAC does not support algorithm %s", algorithm.c_str()));
  67. }
  68. } // namespace
  69. namespace aria2 {
  70. namespace util {
  71. namespace security {
  72. bool compare(const unsigned char a, const unsigned char b)
  73. {
  74. unsigned char rv = ~(a ^ b);
  75. rv &= rv >> 4;
  76. rv &= rv >> 2;
  77. rv &= rv >> 1;
  78. return rv;
  79. }
  80. bool compare(const uint8_t* a, const uint8_t* b, size_t length)
  81. {
  82. unsigned char rv = 0;
  83. for (size_t i = 0; i < length; ++i) {
  84. rv |= a[i] ^ b[i];
  85. }
  86. return compare(rv, 0);
  87. }
  88. HMAC::HMAC(const std::string& algorithm, const char* secret, size_t length)
  89. : blockSize_(getBlockSize(algorithm)),
  90. md_(MessageDigest::create(algorithm)),
  91. clean_(false)
  92. {
  93. ipad_.assign(blockSize_, 0x36);
  94. opad_.assign(blockSize_, 0x5c);
  95. if (length > blockSize_) {
  96. md_->reset();
  97. md_->update(secret, length);
  98. auto hash = md_->digest();
  99. for (size_t i = 0uL, e = hash.length(); i < e; ++i) {
  100. ipad_.replace(i, 1, 1, hash[i] ^ 0x36);
  101. opad_.replace(i, 1, 1, hash[i] ^ 0x5c);
  102. }
  103. }
  104. else {
  105. for (size_t i = 0uL, e = length; i < e; ++i) {
  106. ipad_.replace(i, 1, 1, secret[i] ^ 0x36);
  107. opad_.replace(i, 1, 1, secret[i] ^ 0x5c);
  108. }
  109. }
  110. reset();
  111. }
  112. std::unique_ptr<HMAC> HMAC::createRandom(const std::string& algorithm)
  113. {
  114. const auto len = MessageDigest::getDigestLength(algorithm);
  115. if (len == 0) {
  116. return nullptr;
  117. }
  118. auto buf = make_unique<char[]>(len);
  119. generateRandomData((unsigned char*)buf.get(), len);
  120. return create(algorithm, buf.get(), len);
  121. }
  122. bool HMAC::supports(const std::string& algorithm)
  123. {
  124. if (!MessageDigest::supports(algorithm)) {
  125. return false;
  126. }
  127. const auto canon = MessageDigest::getCanonicalHashType(algorithm);
  128. return canon == "sha-1" || canon == "sha-224" || canon == "sha-256" ||
  129. canon == "sha-384" || canon == "sha-512";
  130. }
  131. HMACResult PBKDF2(HMAC* hmac, const char* salt, size_t salt_length,
  132. size_t iterations, size_t key_length)
  133. {
  134. if (!hmac) {
  135. throw FATAL_EXCEPTION("hmac cannot be null");
  136. }
  137. const size_t hmac_length = hmac->length();
  138. if (key_length == 0) {
  139. key_length = hmac_length;
  140. }
  141. auto work = make_unique<char[]>(hmac_length);
  142. char* p = work.get();
  143. std::string rv;
  144. hmac->reset();
  145. for (uint32_t counter = 1; key_length; ++counter) {
  146. hmac->update(salt, salt_length);
  147. const uint32_t c = htonl(counter);
  148. hmac->update((char*)&c, sizeof(c));
  149. auto bytes = hmac->getResult().getBytes();
  150. memcpy(p, bytes.data(), bytes.length());
  151. for (size_t i = 1uL; i < iterations; ++i) {
  152. hmac->update(bytes);
  153. bytes = hmac->getResult().getBytes();
  154. for (size_t j = 0uL; j < hmac_length; ++j) {
  155. p[j] ^= bytes[j];
  156. }
  157. }
  158. auto use = std::min(key_length, hmac_length);
  159. rv.append(p, use);
  160. key_length -= use;
  161. }
  162. return HMACResult(rv);
  163. }
  164. } // namespace security
  165. } // namespace util
  166. } // namespace aria2