SimpleRandomizer.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "SimpleRandomizer.h"
  36. #include <sys/types.h>
  37. #include <unistd.h>
  38. #include <cstdlib>
  39. #include <cassert>
  40. #include <cstring>
  41. #include "a2time.h"
  42. #include "a2functional.h"
  43. #include "LogFactory.h"
  44. #include "fmt.h"
  45. #ifdef HAVE_GETRANDOM_INTERFACE
  46. # include <errno.h>
  47. # include <linux/errno.h>
  48. # include "getrandom_linux.h"
  49. #endif
  50. namespace aria2 {
  51. std::unique_ptr<SimpleRandomizer> SimpleRandomizer::randomizer_;
  52. const std::unique_ptr<SimpleRandomizer>& SimpleRandomizer::getInstance()
  53. {
  54. if (!randomizer_) {
  55. randomizer_.reset(new SimpleRandomizer());
  56. }
  57. return randomizer_;
  58. }
  59. namespace {
  60. std::random_device rd;
  61. } // namespace
  62. #ifdef __MINGW32__
  63. SimpleRandomizer::SimpleRandomizer()
  64. {
  65. BOOL r = ::CryptAcquireContext(&provider_, 0, 0, PROV_RSA_FULL,
  66. CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
  67. assert(r);
  68. }
  69. #else // !__MINGW32__
  70. SimpleRandomizer::SimpleRandomizer() : gen_(rd()) {}
  71. #endif // !__MINGW32__
  72. SimpleRandomizer::~SimpleRandomizer()
  73. {
  74. #ifdef __MINGW32__
  75. CryptReleaseContext(provider_, 0);
  76. #endif
  77. }
  78. long int SimpleRandomizer::getRandomNumber(long int to)
  79. {
  80. assert(to > 0);
  81. return std::uniform_int_distribution<long int>(0, to - 1)(*this);
  82. }
  83. void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len)
  84. {
  85. #ifdef __MINGW32__
  86. BOOL r = CryptGenRandom(provider_, len, reinterpret_cast<BYTE*>(buf));
  87. assert(r);
  88. #else // ! __MINGW32__
  89. # if defined(HAVE_GETRANDOM_INTERFACE)
  90. static bool have_random_support = true;
  91. if (have_random_support) {
  92. auto rv = getrandom_linux(buf, len);
  93. if (rv != -1) {
  94. // getrandom is not supposed to fail, ever, so, we want to assert here.
  95. assert(rv >= 0 && (size_t)rv == len);
  96. return;
  97. }
  98. have_random_support = false;
  99. A2_LOG_INFO("Disabled getrandom support, because kernel does not "
  100. "implement this feature (ENOSYS)");
  101. }
  102. // Fall through to generic implementation
  103. # endif // defined(HAVE_GETRANDOM_INTERFACE)
  104. auto ubuf = reinterpret_cast<result_type*>(buf);
  105. size_t q = len / sizeof(result_type);
  106. auto dis = std::uniform_int_distribution<result_type>();
  107. for (; q > 0; --q, ++ubuf) {
  108. *ubuf = dis(gen_);
  109. }
  110. const size_t r = len % sizeof(result_type);
  111. auto last = dis(gen_);
  112. memcpy(ubuf, &last, r);
  113. #endif // ! __MINGW32__
  114. }
  115. } // namespace aria2