rand_unix.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #define USE_SOCKETS
  11. #include "e_os.h"
  12. #include "internal/cryptlib.h"
  13. #include <openssl/rand.h>
  14. #include "rand_lcl.h"
  15. #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI))
  16. # include <sys/types.h>
  17. # include <sys/time.h>
  18. # include <sys/times.h>
  19. # include <sys/stat.h>
  20. # include <fcntl.h>
  21. # include <unistd.h>
  22. # include <time.h>
  23. # if defined(OPENSSL_SYS_LINUX) /* should actually be available virtually
  24. * everywhere */
  25. # include <poll.h>
  26. # endif
  27. # include <limits.h>
  28. # ifndef FD_SETSIZE
  29. # define FD_SETSIZE (8*sizeof(fd_set))
  30. # endif
  31. # if defined(OPENSSL_SYS_VOS)
  32. /*
  33. * The following algorithm repeatedly samples the real-time clock (RTC) to
  34. * generate a sequence of unpredictable data. The algorithm relies upon the
  35. * uneven execution speed of the code (due to factors such as cache misses,
  36. * interrupts, bus activity, and scheduling) and upon the rather large
  37. * relative difference between the speed of the clock and the rate at which
  38. * it can be read.
  39. *
  40. * If this code is ported to an environment where execution speed is more
  41. * constant or where the RTC ticks at a much slower rate, or the clock can be
  42. * read with fewer instructions, it is likely that the results would be far
  43. * more predictable.
  44. *
  45. * As a precaution, we generate 4 times the minimum required amount of seed
  46. * data.
  47. */
  48. int RAND_poll(void)
  49. {
  50. short int code;
  51. gid_t curr_gid;
  52. pid_t curr_pid;
  53. uid_t curr_uid;
  54. int i, k;
  55. struct timespec ts;
  56. unsigned char v;
  57. # ifdef OPENSSL_SYS_VOS_HPPA
  58. long duration;
  59. extern void s$sleep(long *_duration, short int *_code);
  60. # else
  61. # ifdef OPENSSL_SYS_VOS_IA32
  62. long long duration;
  63. extern void s$sleep2(long long *_duration, short int *_code);
  64. # else
  65. # error "Unsupported Platform."
  66. # endif /* OPENSSL_SYS_VOS_IA32 */
  67. # endif /* OPENSSL_SYS_VOS_HPPA */
  68. /*
  69. * Seed with the gid, pid, and uid, to ensure *some* variation between
  70. * different processes.
  71. */
  72. curr_gid = getgid();
  73. RAND_add(&curr_gid, sizeof curr_gid, 1);
  74. curr_gid = 0;
  75. curr_pid = getpid();
  76. RAND_add(&curr_pid, sizeof curr_pid, 1);
  77. curr_pid = 0;
  78. curr_uid = getuid();
  79. RAND_add(&curr_uid, sizeof curr_uid, 1);
  80. curr_uid = 0;
  81. for (i = 0; i < (ENTROPY_NEEDED * 4); i++) {
  82. /*
  83. * burn some cpu; hope for interrupts, cache collisions, bus
  84. * interference, etc.
  85. */
  86. for (k = 0; k < 99; k++)
  87. ts.tv_nsec = random();
  88. # ifdef OPENSSL_SYS_VOS_HPPA
  89. /* sleep for 1/1024 of a second (976 us). */
  90. duration = 1;
  91. s$sleep(&duration, &code);
  92. # else
  93. # ifdef OPENSSL_SYS_VOS_IA32
  94. /* sleep for 1/65536 of a second (15 us). */
  95. duration = 1;
  96. s$sleep2(&duration, &code);
  97. # endif /* OPENSSL_SYS_VOS_IA32 */
  98. # endif /* OPENSSL_SYS_VOS_HPPA */
  99. /* get wall clock time. */
  100. clock_gettime(CLOCK_REALTIME, &ts);
  101. /* take 8 bits */
  102. v = (unsigned char)(ts.tv_nsec % 256);
  103. RAND_add(&v, sizeof v, 1);
  104. v = 0;
  105. }
  106. return 1;
  107. }
  108. # elif defined __OpenBSD__
  109. int RAND_poll(void)
  110. {
  111. u_int32_t rnd = 0, i;
  112. unsigned char buf[ENTROPY_NEEDED];
  113. for (i = 0; i < sizeof(buf); i++) {
  114. if (i % 4 == 0)
  115. rnd = arc4random();
  116. buf[i] = rnd;
  117. rnd >>= 8;
  118. }
  119. RAND_add(buf, sizeof(buf), ENTROPY_NEEDED);
  120. OPENSSL_cleanse(buf, sizeof(buf));
  121. return 1;
  122. }
  123. # else /* !defined(__OpenBSD__) */
  124. int RAND_poll(void)
  125. {
  126. unsigned long l;
  127. pid_t curr_pid = getpid();
  128. # if defined(DEVRANDOM) || (!defined(OPENSS_NO_EGD) && defined(DEVRANDOM_EGD))
  129. unsigned char tmpbuf[ENTROPY_NEEDED];
  130. int n = 0;
  131. # endif
  132. # ifdef DEVRANDOM
  133. static const char *randomfiles[] = { DEVRANDOM };
  134. struct stat randomstats[OSSL_NELEM(randomfiles)];
  135. int fd;
  136. unsigned int i;
  137. # endif
  138. # if !defined(OPENSSL_NO_EGD) && defined(DEVRANDOM_EGD)
  139. static const char *egdsockets[] = { DEVRANDOM_EGD, NULL };
  140. const char **egdsocket = NULL;
  141. # endif
  142. # ifdef DEVRANDOM
  143. memset(randomstats, 0, sizeof(randomstats));
  144. /*
  145. * Use a random entropy pool device. Linux, FreeBSD and OpenBSD have
  146. * this. Use /dev/urandom if you can as /dev/random may block if it runs
  147. * out of random entries.
  148. */
  149. for (i = 0; (i < OSSL_NELEM(randomfiles)) && (n < ENTROPY_NEEDED); i++) {
  150. if ((fd = open(randomfiles[i], O_RDONLY
  151. # ifdef O_NONBLOCK
  152. | O_NONBLOCK
  153. # endif
  154. # ifdef O_BINARY
  155. | O_BINARY
  156. # endif
  157. # ifdef O_NOCTTY /* If it happens to be a TTY (god forbid), do
  158. * not make it our controlling tty */
  159. | O_NOCTTY
  160. # endif
  161. )) >= 0) {
  162. int usec = 10 * 1000; /* spend 10ms on each file */
  163. int r;
  164. unsigned int j;
  165. struct stat *st = &randomstats[i];
  166. /*
  167. * Avoid using same input... Used to be O_NOFOLLOW above, but
  168. * it's not universally appropriate...
  169. */
  170. if (fstat(fd, st) != 0) {
  171. close(fd);
  172. continue;
  173. }
  174. for (j = 0; j < i; j++) {
  175. if (randomstats[j].st_ino == st->st_ino &&
  176. randomstats[j].st_dev == st->st_dev)
  177. break;
  178. }
  179. if (j < i) {
  180. close(fd);
  181. continue;
  182. }
  183. do {
  184. int try_read = 0;
  185. # if defined(OPENSSL_SYS_LINUX)
  186. /* use poll() */
  187. struct pollfd pset;
  188. pset.fd = fd;
  189. pset.events = POLLIN;
  190. pset.revents = 0;
  191. if (poll(&pset, 1, usec / 1000) < 0)
  192. usec = 0;
  193. else
  194. try_read = (pset.revents & POLLIN) != 0;
  195. # else
  196. /* use select() */
  197. fd_set fset;
  198. struct timeval t;
  199. t.tv_sec = 0;
  200. t.tv_usec = usec;
  201. if (FD_SETSIZE > 0 && (unsigned)fd >= FD_SETSIZE) {
  202. /*
  203. * can't use select, so just try to read once anyway
  204. */
  205. try_read = 1;
  206. } else {
  207. FD_ZERO(&fset);
  208. FD_SET(fd, &fset);
  209. if (select(fd + 1, &fset, NULL, NULL, &t) >= 0) {
  210. usec = t.tv_usec;
  211. if (FD_ISSET(fd, &fset))
  212. try_read = 1;
  213. } else
  214. usec = 0;
  215. }
  216. # endif
  217. if (try_read) {
  218. r = read(fd, (unsigned char *)tmpbuf + n,
  219. ENTROPY_NEEDED - n);
  220. if (r > 0)
  221. n += r;
  222. } else
  223. r = -1;
  224. /*
  225. * Some Unixen will update t in select(), some won't. For
  226. * those who won't, or if we didn't use select() in the first
  227. * place, give up here, otherwise, we will do this once again
  228. * for the remaining time.
  229. */
  230. if (usec == 10 * 1000)
  231. usec = 0;
  232. }
  233. while ((r > 0 ||
  234. (errno == EINTR || errno == EAGAIN)) && usec != 0
  235. && n < ENTROPY_NEEDED);
  236. close(fd);
  237. }
  238. }
  239. # endif /* defined(DEVRANDOM) */
  240. # if !defined(OPENSSL_NO_EGD) && defined(DEVRANDOM_EGD)
  241. /*
  242. * Use an EGD socket to read entropy from an EGD or PRNGD entropy
  243. * collecting daemon.
  244. */
  245. for (egdsocket = egdsockets; *egdsocket && n < ENTROPY_NEEDED;
  246. egdsocket++) {
  247. int r;
  248. r = RAND_query_egd_bytes(*egdsocket, (unsigned char *)tmpbuf + n,
  249. ENTROPY_NEEDED - n);
  250. if (r > 0)
  251. n += r;
  252. }
  253. # endif /* defined(DEVRANDOM_EGD) */
  254. # if defined(DEVRANDOM) || (!defined(OPENSSL_NO_EGD) && defined(DEVRANDOM_EGD))
  255. if (n > 0) {
  256. RAND_add(tmpbuf, sizeof tmpbuf, (double)n);
  257. OPENSSL_cleanse(tmpbuf, n);
  258. }
  259. # endif
  260. /* put in some default random data, we need more than just this */
  261. l = curr_pid;
  262. RAND_add(&l, sizeof(l), 0.0);
  263. l = getuid();
  264. RAND_add(&l, sizeof(l), 0.0);
  265. l = time(NULL);
  266. RAND_add(&l, sizeof(l), 0.0);
  267. # if defined(DEVRANDOM) || (!defined(OPENSSL_NO_EGD) && defined(DEVRANDOM_EGD))
  268. return 1;
  269. # else
  270. return 0;
  271. # endif
  272. }
  273. # endif /* defined(__OpenBSD__) */
  274. #endif /* !(defined(OPENSSL_SYS_WINDOWS) ||
  275. * defined(OPENSSL_SYS_WIN32) ||
  276. * defined(OPENSSL_SYS_VMS) ||
  277. * defined(OPENSSL_SYS_VXWORKS) */
  278. #if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
  279. int RAND_poll(void)
  280. {
  281. return 0;
  282. }
  283. #endif