You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RandomStream.cxx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <rdr/RandomStream.h>
  22. #include <rdr/Exception.h>
  23. #include <rfb/LogWriter.h>
  24. #include <time.h>
  25. #include <stdlib.h>
  26. #ifndef WIN32
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #else
  30. #define getpid() GetCurrentProcessId()
  31. #ifndef RFB_HAVE_WINCRYPT
  32. #pragma message(" NOTE: Not building WinCrypt-based RandomStream")
  33. #endif
  34. #endif
  35. static rfb::LogWriter vlog("RandomStream");
  36. using namespace rdr;
  37. unsigned int RandomStream::seed;
  38. RandomStream::RandomStream()
  39. {
  40. #ifdef RFB_HAVE_WINCRYPT
  41. provider = 0;
  42. if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, 0)) {
  43. if (GetLastError() == (DWORD)NTE_BAD_KEYSET) {
  44. if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
  45. vlog.error("unable to create keyset");
  46. provider = 0;
  47. }
  48. } else {
  49. vlog.error("unable to acquire context");
  50. provider = 0;
  51. }
  52. }
  53. if (!provider) {
  54. #else
  55. #ifndef WIN32
  56. fp = fopen("/dev/urandom", "r");
  57. if (!fp)
  58. fp = fopen("/dev/random", "r");
  59. if (!fp) {
  60. #else
  61. {
  62. #endif
  63. #endif
  64. vlog.error("no OS supplied random source - using rand()");
  65. seed += (unsigned int) time(0) + getpid() + getpid() * 987654 + rand();
  66. srand(seed);
  67. }
  68. }
  69. RandomStream::~RandomStream() {
  70. #ifdef RFB_HAVE_WINCRYPT
  71. if (provider)
  72. CryptReleaseContext(provider, 0);
  73. #endif
  74. #ifndef WIN32
  75. if (fp) fclose(fp);
  76. #endif
  77. }
  78. bool RandomStream::fillBuffer() {
  79. #ifdef RFB_HAVE_WINCRYPT
  80. if (provider) {
  81. if (!CryptGenRandom(provider, availSpace(), (uint8_t*)end))
  82. throw rdr::SystemException("unable to CryptGenRandom", GetLastError());
  83. end += availSpace();
  84. } else {
  85. #else
  86. #ifndef WIN32
  87. if (fp) {
  88. size_t n = fread((uint8_t*)end, 1, availSpace(), fp);
  89. if (n <= 0)
  90. throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
  91. errno);
  92. end += n;
  93. } else {
  94. #else
  95. {
  96. #endif
  97. #endif
  98. for (size_t i=availSpace(); i>0; i--)
  99. *(uint8_t*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
  100. }
  101. return true;
  102. }