archive_random.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*-
  2. * Copyright (c) 2014 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_STDLIB_H
  28. #include <stdlib.h>
  29. #endif
  30. #if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__))
  31. #ifdef HAVE_FCNTL
  32. #include <fcntl.h>
  33. #endif
  34. #ifdef HAVE_LIMITS_H
  35. #include <limits.h>
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #ifdef HAVE_SYS_TYPES_H
  41. #include <sys/types.h>
  42. #endif
  43. #ifdef HAVE_SYS_TIME_H
  44. #include <sys/time.h>
  45. #endif
  46. #ifdef HAVE_PTHREAD_H
  47. #include <pthread.h>
  48. #endif
  49. static void arc4random_buf(void *, size_t);
  50. #endif /* HAVE_ARC4RANDOM_BUF */
  51. #include "archive.h"
  52. #include "archive_random_private.h"
  53. #if defined(HAVE_WINCRYPT_H) && !defined(__CYGWIN__)
  54. #include <wincrypt.h>
  55. #endif
  56. #ifndef O_CLOEXEC
  57. #define O_CLOEXEC 0
  58. #endif
  59. /*
  60. * Random number generator function.
  61. * This simply calls arc4random_buf function if the platform provides it.
  62. */
  63. int
  64. archive_random(void *buf, size_t nbytes)
  65. {
  66. #if defined(_WIN32) && !defined(__CYGWIN__)
  67. HCRYPTPROV hProv;
  68. BOOL success;
  69. success = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
  70. CRYPT_VERIFYCONTEXT);
  71. if (!success && GetLastError() == (DWORD)NTE_BAD_KEYSET) {
  72. success = CryptAcquireContext(&hProv, NULL, NULL,
  73. PROV_RSA_FULL, CRYPT_NEWKEYSET);
  74. }
  75. if (success) {
  76. success = CryptGenRandom(hProv, (DWORD)nbytes, (BYTE*)buf);
  77. CryptReleaseContext(hProv, 0);
  78. if (success)
  79. return ARCHIVE_OK;
  80. }
  81. /* TODO: Does this case really happen? */
  82. return ARCHIVE_FAILED;
  83. #else
  84. arc4random_buf(buf, nbytes);
  85. return ARCHIVE_OK;
  86. #endif
  87. }
  88. #if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__))
  89. /* $OpenBSD: arc4random.c,v 1.24 2013/06/11 16:59:50 deraadt Exp $ */
  90. /*
  91. * Copyright (c) 1996, David Mazieres <dm@uun.org>
  92. * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
  93. *
  94. * Permission to use, copy, modify, and distribute this software for any
  95. * purpose with or without fee is hereby granted, provided that the above
  96. * copyright notice and this permission notice appear in all copies.
  97. *
  98. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  99. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  100. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  101. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  102. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  103. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  104. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  105. */
  106. /*
  107. * Arc4 random number generator for OpenBSD.
  108. *
  109. * This code is derived from section 17.1 of Applied Cryptography,
  110. * second edition, which describes a stream cipher allegedly
  111. * compatible with RSA Labs "RC4" cipher (the actual description of
  112. * which is a trade secret). The same algorithm is used as a stream
  113. * cipher called "arcfour" in Tatu Ylonen's ssh package.
  114. *
  115. * RC4 is a registered trademark of RSA Laboratories.
  116. */
  117. #ifdef __GNUC__
  118. #define inline __inline
  119. #else /* !__GNUC__ */
  120. #define inline
  121. #endif /* !__GNUC__ */
  122. struct arc4_stream {
  123. uint8_t i;
  124. uint8_t j;
  125. uint8_t s[256];
  126. };
  127. #define RANDOMDEV "/dev/urandom"
  128. #define KEYSIZE 128
  129. #ifdef HAVE_PTHREAD_H
  130. static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
  131. #define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx);
  132. #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx);
  133. #else
  134. #define _ARC4_LOCK()
  135. #define _ARC4_UNLOCK()
  136. #endif
  137. static int rs_initialized;
  138. static struct arc4_stream rs;
  139. static pid_t arc4_stir_pid;
  140. static int arc4_count;
  141. static inline uint8_t arc4_getbyte(void);
  142. static void arc4_stir(void);
  143. static inline void
  144. arc4_init(void)
  145. {
  146. int n;
  147. for (n = 0; n < 256; n++)
  148. rs.s[n] = n;
  149. rs.i = 0;
  150. rs.j = 0;
  151. }
  152. static inline void
  153. arc4_addrandom(u_char *dat, int datlen)
  154. {
  155. int n;
  156. uint8_t si;
  157. rs.i--;
  158. for (n = 0; n < 256; n++) {
  159. rs.i = (rs.i + 1);
  160. si = rs.s[rs.i];
  161. rs.j = (rs.j + si + dat[n % datlen]);
  162. rs.s[rs.i] = rs.s[rs.j];
  163. rs.s[rs.j] = si;
  164. }
  165. rs.j = rs.i;
  166. }
  167. static void
  168. arc4_stir(void)
  169. {
  170. int done, fd, i;
  171. struct {
  172. struct timeval tv;
  173. pid_t pid;
  174. u_char rnd[KEYSIZE];
  175. } rdat;
  176. if (!rs_initialized) {
  177. arc4_init();
  178. rs_initialized = 1;
  179. }
  180. done = 0;
  181. fd = open(RANDOMDEV, O_RDONLY | O_CLOEXEC, 0);
  182. if (fd >= 0) {
  183. if (read(fd, &rdat, KEYSIZE) == KEYSIZE)
  184. done = 1;
  185. (void)close(fd);
  186. }
  187. if (!done) {
  188. (void)gettimeofday(&rdat.tv, NULL);
  189. rdat.pid = getpid();
  190. /* We'll just take whatever was on the stack too... */
  191. }
  192. arc4_addrandom((u_char *)&rdat, KEYSIZE);
  193. /*
  194. * Discard early keystream, as per recommendations in:
  195. * "(Not So) Random Shuffles of RC4" by Ilya Mironov.
  196. * As per the Network Operations Division, cryptographic requirements
  197. * published on wikileaks on March 2017.
  198. */
  199. for (i = 0; i < 3072; i++)
  200. (void)arc4_getbyte();
  201. arc4_count = 1600000;
  202. }
  203. static void
  204. arc4_stir_if_needed(void)
  205. {
  206. pid_t pid = getpid();
  207. if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) {
  208. arc4_stir_pid = pid;
  209. arc4_stir();
  210. }
  211. }
  212. static inline uint8_t
  213. arc4_getbyte(void)
  214. {
  215. uint8_t si, sj;
  216. rs.i = (rs.i + 1);
  217. si = rs.s[rs.i];
  218. rs.j = (rs.j + si);
  219. sj = rs.s[rs.j];
  220. rs.s[rs.i] = sj;
  221. rs.s[rs.j] = si;
  222. return (rs.s[(si + sj) & 0xff]);
  223. }
  224. static void
  225. arc4random_buf(void *_buf, size_t n)
  226. {
  227. u_char *buf = (u_char *)_buf;
  228. _ARC4_LOCK();
  229. arc4_stir_if_needed();
  230. while (n--) {
  231. if (--arc4_count <= 0)
  232. arc4_stir();
  233. buf[n] = arc4_getbyte();
  234. }
  235. _ARC4_UNLOCK();
  236. }
  237. #endif /* !HAVE_ARC4RANDOM_BUF */