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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. unsigned int RandomStream::seed;
  35. RandomStream::RandomStream()
  36. {
  37. #ifdef RFB_HAVE_WINCRYPT
  38. provider = 0;
  39. if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, 0)) {
  40. if (GetLastError() == (DWORD)NTE_BAD_KEYSET) {
  41. if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
  42. vlog.error("unable to create keyset");
  43. provider = 0;
  44. }
  45. } else {
  46. vlog.error("unable to acquire context");
  47. provider = 0;
  48. }
  49. }
  50. if (!provider) {
  51. #else
  52. #ifndef WIN32
  53. fp = fopen("/dev/urandom", "r");
  54. if (!fp)
  55. fp = fopen("/dev/random", "r");
  56. if (!fp) {
  57. #else
  58. {
  59. #endif
  60. #endif
  61. vlog.error("no OS supplied random source - using rand()");
  62. seed += (unsigned int) time(0) + getpid() + getpid() * 987654 + rand();
  63. srand(seed);
  64. }
  65. }
  66. RandomStream::~RandomStream() {
  67. #ifdef RFB_HAVE_WINCRYPT
  68. if (provider)
  69. CryptReleaseContext(provider, 0);
  70. #endif
  71. #ifndef WIN32
  72. if (fp) fclose(fp);
  73. #endif
  74. }
  75. bool RandomStream::fillBuffer(size_t maxSize) {
  76. #ifdef RFB_HAVE_WINCRYPT
  77. if (provider) {
  78. if (!CryptGenRandom(provider, maxSize, (U8*)end))
  79. throw rdr::SystemException("unable to CryptGenRandom", GetLastError());
  80. end += maxSize;
  81. } else {
  82. #else
  83. #ifndef WIN32
  84. if (fp) {
  85. size_t n = fread((U8*)end, 1, maxSize, fp);
  86. if (n <= 0)
  87. throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
  88. errno);
  89. end += n;
  90. } else {
  91. #else
  92. {
  93. #endif
  94. #endif
  95. for (size_t i=0; i<maxSize; i++)
  96. *(U8*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
  97. }
  98. return true;
  99. }