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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include <rdr/RandomStream.h>
  19. #include <rdr/Exception.h>
  20. #include <rfb/LogWriter.h>
  21. #include <time.h>
  22. #include <stdlib.h>
  23. #ifndef WIN32
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #else
  27. #define getpid() GetCurrentProcessId()
  28. #ifndef RFB_HAVE_WINCRYPT
  29. #pragma message(" NOTE: Not building WinCrypt-based RandomStream")
  30. #endif
  31. #endif
  32. static rfb::LogWriter vlog("RandomStream");
  33. using namespace rdr;
  34. const int DEFAULT_BUF_LEN = 256;
  35. unsigned int RandomStream::seed;
  36. RandomStream::RandomStream()
  37. : offset(0)
  38. {
  39. ptr = end = start = new U8[DEFAULT_BUF_LEN];
  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. delete [] start;
  71. #ifdef RFB_HAVE_WINCRYPT
  72. if (provider)
  73. CryptReleaseContext(provider, 0);
  74. #endif
  75. #ifndef WIN32
  76. if (fp) fclose(fp);
  77. #endif
  78. }
  79. int RandomStream::pos() {
  80. return offset + ptr - start;
  81. }
  82. int RandomStream::overrun(int itemSize, int nItems, bool wait) {
  83. if (itemSize > DEFAULT_BUF_LEN)
  84. throw Exception("RandomStream overrun: max itemSize exceeded");
  85. if (end - ptr != 0)
  86. memmove(start, ptr, end - ptr);
  87. end -= ptr - start;
  88. offset += ptr - start;
  89. ptr = start;
  90. int length = start + DEFAULT_BUF_LEN - end;
  91. #ifdef RFB_HAVE_WINCRYPT
  92. if (provider) {
  93. if (!CryptGenRandom(provider, length, (U8*)end))
  94. throw rdr::SystemException("unable to CryptGenRandom", GetLastError());
  95. end += length;
  96. } else {
  97. #else
  98. #ifndef WIN32
  99. if (fp) {
  100. int n = fread((U8*)end, length, 1, fp);
  101. if (n != 1)
  102. throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
  103. errno);
  104. end += length;
  105. } else {
  106. #else
  107. {
  108. #endif
  109. #endif
  110. for (int i=0; i<length; i++)
  111. *(U8*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
  112. }
  113. if (itemSize * nItems > end - ptr)
  114. nItems = (end - ptr) / itemSize;
  115. return nItems;
  116. }