random.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* random.c
  2. *
  3. * Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. /* on HPUX 11 you may need to install /dev/random see
  22. http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=KRNG11I
  23. */
  24. #include "random.h"
  25. #include "error.h"
  26. #if defined(USE_WINDOWS_API)
  27. #define _WIN32_WINNT 0x0400
  28. #include <windows.h>
  29. #include <wincrypt.h>
  30. #else
  31. #ifndef NO_DEV_RANDOM
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #else
  35. /* include headers that may be needed to get good seed */
  36. #endif
  37. #endif /* USE_WINDOWS_API */
  38. /* Get seed and key cipher */
  39. int InitRng(RNG* rng)
  40. {
  41. byte key[32];
  42. byte junk[256];
  43. int ret = GenerateSeed(&rng->seed, key, sizeof(key));
  44. if (ret == 0) {
  45. Arc4SetKey(&rng->cipher, key, sizeof(key));
  46. RNG_GenerateBlock(rng, junk, sizeof(junk)); /* rid initial state */
  47. }
  48. return ret;
  49. }
  50. /* place a generated block in output */
  51. void RNG_GenerateBlock(RNG* rng, byte* output, word32 sz)
  52. {
  53. XMEMSET(output, 0, sz);
  54. Arc4Process(&rng->cipher, output, output, sz);
  55. }
  56. byte RNG_GenerateByte(RNG* rng)
  57. {
  58. byte b;
  59. RNG_GenerateBlock(rng, &b, 1);
  60. return b;
  61. }
  62. #if defined(USE_WINDOWS_API)
  63. int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  64. {
  65. if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
  66. CRYPT_VERIFYCONTEXT))
  67. return WINCRYPT_E;
  68. if (!CryptGenRandom(os->handle, sz, output))
  69. return CRYPTGEN_E;
  70. CryptReleaseContext(os->handle, 0);
  71. return 0;
  72. }
  73. #elif defined(THREADX)
  74. #include "rtprand.h" /* rtp_rand () */
  75. #include "rtptime.h" /* rtp_get_system_msec() */
  76. int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  77. {
  78. int i;
  79. rtp_srand(rtp_get_system_msec());
  80. for (i = 0; i < sz; i++ ) {
  81. output[i] = rtp_rand() % 256;
  82. if ( (i % 8) == 7)
  83. rtp_srand(rtp_get_system_msec());
  84. }
  85. return 0;
  86. }
  87. #elif defined(MICRIUM)
  88. int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  89. {
  90. #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
  91. NetSecure_InitSeed(output, sz);
  92. #endif
  93. return 0;
  94. }
  95. #elif defined(MBED)
  96. /* write a real one !!!, just for testing board */
  97. int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  98. {
  99. int i;
  100. for (i = 0; i < sz; i++ )
  101. output[i] = i;
  102. return 0;
  103. }
  104. #elif defined(NO_DEV_RANDOM)
  105. #error "you need to write an os specific GenerateSeed() here"
  106. #else /* !USE_WINDOWS_API && !THREADX && !MICRIUM && !NO_DEV_RANDOM */
  107. /* may block */
  108. int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
  109. {
  110. os->fd = open("/dev/urandom",O_RDONLY);
  111. if (os->fd == -1) {
  112. /* may still have /dev/random */
  113. os->fd = open("/dev/random",O_RDONLY);
  114. if (os->fd == -1)
  115. return OPEN_RAN_E;
  116. }
  117. while (sz) {
  118. int len = read(os->fd, output, sz);
  119. if (len == -1)
  120. return READ_RAN_E;
  121. sz -= len;
  122. output += len;
  123. if (sz)
  124. #ifdef BLOCKING
  125. sleep(0); /* context switch */
  126. #else
  127. return RAN_BLOCK_E;
  128. #endif
  129. }
  130. close(os->fd);
  131. return 0;
  132. }
  133. #endif /* USE_WINDOWS_API */