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
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
28
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32
33 #if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__))
34
35 #ifdef HAVE_FCNTL
36 #include <fcntl.h>
37 #endif
38 #ifdef HAVE_LIMITS_H
39 #include <limits.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
46 #endif
47 #ifdef HAVE_SYS_TIME_H
48 #include <sys/time.h>
49 #endif
50 #ifdef HAVE_PTHREAD_H
51 #include <pthread.h>
52 #endif
53
54 static void arc4random_buf(void *, size_t);
55
56 #endif /* HAVE_ARC4RANDOM_BUF */
57
58 #include "archive.h"
59 #include "archive_random_private.h"
60
61 #if defined(HAVE_WINCRYPT_H) && !defined(__CYGWIN__)
62 #include <wincrypt.h>
63 #endif
64
65 #ifndef O_CLOEXEC
66 #define O_CLOEXEC 0
67 #endif
68
69 /*
70 * Random number generator function.
71 * This simply calls arc4random_buf function if the platform provides it.
72 */
73
74 int
archive_random(void * buf,size_t nbytes)75 archive_random(void *buf, size_t nbytes)
76 {
77 #if defined(_WIN32) && !defined(__CYGWIN__)
78 HCRYPTPROV hProv;
79 BOOL success;
80
81 success = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
82 CRYPT_VERIFYCONTEXT);
83 if (!success && GetLastError() == (DWORD)NTE_BAD_KEYSET) {
84 success = CryptAcquireContext(&hProv, NULL, NULL,
85 PROV_RSA_FULL, CRYPT_NEWKEYSET);
86 }
87 if (success) {
88 success = CryptGenRandom(hProv, (DWORD)nbytes, (BYTE*)buf);
89 CryptReleaseContext(hProv, 0);
90 if (success)
91 return ARCHIVE_OK;
92 }
93 /* TODO: Does this case really happen? */
94 return ARCHIVE_FAILED;
95 #else
96 arc4random_buf(buf, nbytes);
97 return ARCHIVE_OK;
98 #endif
99 }
100
101 #if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__))
102
103 /* $OpenBSD: arc4random.c,v 1.24 2013/06/11 16:59:50 deraadt Exp $ */
104 /*
105 * Copyright (c) 1996, David Mazieres <dm@uun.org>
106 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
107 *
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.
111 *
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.
119 */
120
121 /*
122 * Arc4 random number generator for OpenBSD.
123 *
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.
129 *
130 * RC4 is a registered trademark of RSA Laboratories.
131 */
132
133 #ifdef __GNUC__
134 #define inline __inline
135 #else /* !__GNUC__ */
136 #define inline
137 #endif /* !__GNUC__ */
138
139 struct arc4_stream {
140 uint8_t i;
141 uint8_t j;
142 uint8_t s[256];
143 };
144
145 #define RANDOMDEV "/dev/urandom"
146 #define KEYSIZE 128
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);
151 #else
152 #define _ARC4_LOCK()
153 #define _ARC4_UNLOCK()
154 #endif
155
156 static int rs_initialized;
157 static struct arc4_stream rs;
158 static pid_t arc4_stir_pid;
159 static int arc4_count;
160
161 static inline uint8_t arc4_getbyte(void);
162 static void arc4_stir(void);
163
164 static inline void
arc4_init(void)165 arc4_init(void)
166 {
167 int n;
168
169 for (n = 0; n < 256; n++)
170 rs.s[n] = n;
171 rs.i = 0;
172 rs.j = 0;
173 }
174
175 static inline void
arc4_addrandom(uint8_t * dat,int datlen)176 arc4_addrandom(uint8_t *dat, int datlen)
177 {
178 int n;
179 uint8_t si;
180
181 rs.i--;
182 for (n = 0; n < 256; n++) {
183 rs.i = (rs.i + 1);
184 si = rs.s[rs.i];
185 rs.j = (rs.j + si + dat[n % datlen]);
186 rs.s[rs.i] = rs.s[rs.j];
187 rs.s[rs.j] = si;
188 }
189 rs.j = rs.i;
190 }
191
192 static void
arc4_stir(void)193 arc4_stir(void)
194 {
195 int done, fd, i;
196 struct {
197 struct timeval tv;
198 pid_t pid;
199 uint8_t rnd[KEYSIZE];
200 } rdat;
201
202 if (!rs_initialized) {
203 arc4_init();
204 rs_initialized = 1;
205 }
206 done = 0;
207 fd = open(RANDOMDEV, O_RDONLY | O_CLOEXEC, 0);
208 if (fd >= 0) {
209 if (read(fd, &rdat, KEYSIZE) == KEYSIZE)
210 done = 1;
211 (void)close(fd);
212 }
213 if (!done) {
214 (void)gettimeofday(&rdat.tv, NULL);
215 rdat.pid = getpid();
216 /* We'll just take whatever was on the stack too... */
217 }
218
219 arc4_addrandom((uint8_t *)&rdat, KEYSIZE);
220
221 /*
222 * Discard early keystream, as per recommendations in:
223 * "(Not So) Random Shuffles of RC4" by Ilya Mironov.
224 * As per the Network Operations Division, cryptographic requirements
225 * published on wikileaks on March 2017.
226 */
227
228 for (i = 0; i < 3072; i++)
229 (void)arc4_getbyte();
230 arc4_count = 1600000;
231 }
232
233 static void
arc4_stir_if_needed(void)234 arc4_stir_if_needed(void)
235 {
236 pid_t pid = getpid();
237
238 if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) {
239 arc4_stir_pid = pid;
240 arc4_stir();
241 }
242 }
243
244 static inline uint8_t
arc4_getbyte(void)245 arc4_getbyte(void)
246 {
247 uint8_t si, sj;
248
249 rs.i = (rs.i + 1);
250 si = rs.s[rs.i];
251 rs.j = (rs.j + si);
252 sj = rs.s[rs.j];
253 rs.s[rs.i] = sj;
254 rs.s[rs.j] = si;
255 return (rs.s[(si + sj) & 0xff]);
256 }
257
258 static void
arc4random_buf(void * _buf,size_t n)259 arc4random_buf(void *_buf, size_t n)
260 {
261 uint8_t *buf = (uint8_t *)_buf;
262 _ARC4_LOCK();
263 arc4_stir_if_needed();
264 while (n--) {
265 if (--arc4_count <= 0)
266 arc4_stir();
267 buf[n] = arc4_getbyte();
268 }
269 _ARC4_UNLOCK();
270 }
271
272 #endif /* !HAVE_ARC4RANDOM_BUF */
273