2 * Copyright (c) 2014 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
33 #if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__))
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
47 #ifdef HAVE_SYS_TIME_H
54 static void arc4random_buf(void *, size_t);
56 #endif /* HAVE_ARC4RANDOM_BUF */
59 #include "archive_random_private.h"
61 #if defined(HAVE_WINCRYPT_H) && !defined(__CYGWIN__)
70 * Random number generator function.
71 * This simply calls arc4random_buf function if the platform provides it.
75 archive_random(void *buf, size_t nbytes)
77 #if defined(_WIN32) && !defined(__CYGWIN__)
81 success = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
83 if (!success && GetLastError() == NTE_BAD_KEYSET) {
84 success = CryptAcquireContext(&hProv, NULL, NULL,
85 PROV_RSA_FULL, CRYPT_NEWKEYSET);
88 success = CryptGenRandom(hProv, (DWORD)nbytes, (BYTE*)buf);
89 CryptReleaseContext(hProv, 0);
93 /* TODO: Does this case really happen? */
94 return ARCHIVE_FAILED;
96 arc4random_buf(buf, nbytes);
101 #if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__))
103 /* $OpenBSD: arc4random.c,v 1.24 2013/06/11 16:59:50 deraadt Exp $ */
105 * Copyright (c) 1996, David Mazieres <dm@uun.org>
106 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
108 * Permission to use, copy, modify, and distribute this software for any
109 * purpose with or without fee is hereby granted, provided that the above
110 * copyright notice and this permission notice appear in all copies.
112 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
113 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
114 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
115 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
116 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
117 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
118 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
122 * Arc4 random number generator for OpenBSD.
124 * This code is derived from section 17.1 of Applied Cryptography,
125 * second edition, which describes a stream cipher allegedly
126 * compatible with RSA Labs "RC4" cipher (the actual description of
127 * which is a trade secret). The same algorithm is used as a stream
128 * cipher called "arcfour" in Tatu Ylonen's ssh package.
130 * RC4 is a registered trademark of RSA Laboratories.
134 #define inline __inline
135 #else /* !__GNUC__ */
137 #endif /* !__GNUC__ */
145 #define RANDOMDEV "/dev/urandom"
147 #ifdef HAVE_PTHREAD_H
148 static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
149 #define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx);
150 #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx);
153 #define _ARC4_UNLOCK()
156 static int rs_initialized;
157 static struct arc4_stream rs;
158 static pid_t arc4_stir_pid;
159 static int arc4_count;
161 static inline uint8_t arc4_getbyte(void);
162 static void arc4_stir(void);
169 for (n = 0; n < 256; n++)
176 arc4_addrandom(u_char *dat, int datlen)
182 for (n = 0; n < 256; n++) {
185 rs.j = (rs.j + si + dat[n % datlen]);
186 rs.s[rs.i] = rs.s[rs.j];
202 if (!rs_initialized) {
207 fd = open(RANDOMDEV, O_RDONLY | O_CLOEXEC, 0);
209 if (read(fd, &rdat, KEYSIZE) == KEYSIZE)
214 (void)gettimeofday(&rdat.tv, NULL);
216 /* We'll just take whatever was on the stack too... */
219 arc4_addrandom((u_char *)&rdat, KEYSIZE);
222 * Discard early keystream, as per recommendations in:
223 * "(Not So) Random Shuffles of RC4" by Ilya Mironov.
225 for (i = 0; i < 1024; i++)
226 (void)arc4_getbyte();
227 arc4_count = 1600000;
231 arc4_stir_if_needed(void)
233 pid_t pid = getpid();
235 if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) {
241 static inline uint8_t
252 return (rs.s[(si + sj) & 0xff]);
256 arc4random_buf(void *_buf, size_t n)
258 u_char *buf = (u_char *)_buf;
260 arc4_stir_if_needed();
262 if (--arc4_count <= 0)
264 buf[n] = arc4_getbyte();
269 #endif /* !HAVE_ARC4RANDOM_BUF */