xref: /dflybsd-src/contrib/libarchive/libarchive/archive_random.c (revision afd311f52496a4b5c3df02ea6d4bdab591886c60)
16b384f39SPeter Avalos /*-
26b384f39SPeter Avalos  * Copyright (c) 2014 Michihiro NAKAJIMA
36b384f39SPeter Avalos  * All rights reserved.
46b384f39SPeter Avalos  *
56b384f39SPeter Avalos  * Redistribution and use in source and binary forms, with or without
66b384f39SPeter Avalos  * modification, are permitted provided that the following conditions
76b384f39SPeter Avalos  * are met:
86b384f39SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
96b384f39SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
106b384f39SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
116b384f39SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
126b384f39SPeter Avalos  *    documentation and/or other materials provided with the distribution.
136b384f39SPeter Avalos  *
146b384f39SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
156b384f39SPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
166b384f39SPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
176b384f39SPeter Avalos  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
186b384f39SPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
196b384f39SPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
206b384f39SPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
216b384f39SPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
226b384f39SPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
236b384f39SPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
246b384f39SPeter Avalos  */
256b384f39SPeter Avalos 
266b384f39SPeter Avalos #include "archive_platform.h"
276b384f39SPeter Avalos __FBSDID("$FreeBSD$");
286b384f39SPeter Avalos 
296b384f39SPeter Avalos #ifdef HAVE_STDLIB_H
306b384f39SPeter Avalos #include <stdlib.h>
316b384f39SPeter Avalos #endif
326b384f39SPeter Avalos 
336b384f39SPeter Avalos #if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__))
346b384f39SPeter Avalos 
356b384f39SPeter Avalos #ifdef HAVE_FCNTL
366b384f39SPeter Avalos #include <fcntl.h>
376b384f39SPeter Avalos #endif
386b384f39SPeter Avalos #ifdef HAVE_LIMITS_H
396b384f39SPeter Avalos #include <limits.h>
406b384f39SPeter Avalos #endif
416b384f39SPeter Avalos #ifdef HAVE_UNISTD_H
426b384f39SPeter Avalos #include <unistd.h>
436b384f39SPeter Avalos #endif
446b384f39SPeter Avalos #ifdef HAVE_SYS_TYPES_H
456b384f39SPeter Avalos #include <sys/types.h>
466b384f39SPeter Avalos #endif
476b384f39SPeter Avalos #ifdef HAVE_SYS_TIME_H
486b384f39SPeter Avalos #include <sys/time.h>
496b384f39SPeter Avalos #endif
506b384f39SPeter Avalos #ifdef HAVE_PTHREAD_H
516b384f39SPeter Avalos #include <pthread.h>
526b384f39SPeter Avalos #endif
536b384f39SPeter Avalos 
546b384f39SPeter Avalos static void arc4random_buf(void *, size_t);
556b384f39SPeter Avalos 
566b384f39SPeter Avalos #endif /* HAVE_ARC4RANDOM_BUF */
576b384f39SPeter Avalos 
586b384f39SPeter Avalos #include "archive.h"
596b384f39SPeter Avalos #include "archive_random_private.h"
606b384f39SPeter Avalos 
616b384f39SPeter Avalos #if defined(HAVE_WINCRYPT_H) && !defined(__CYGWIN__)
626b384f39SPeter Avalos #include <wincrypt.h>
636b384f39SPeter Avalos #endif
646b384f39SPeter Avalos 
656b384f39SPeter Avalos #ifndef O_CLOEXEC
666b384f39SPeter Avalos #define O_CLOEXEC	0
676b384f39SPeter Avalos #endif
686b384f39SPeter Avalos 
696b384f39SPeter Avalos /*
706b384f39SPeter Avalos  * Random number generator function.
716b384f39SPeter Avalos  * This simply calls arc4random_buf function if the platform provides it.
726b384f39SPeter Avalos  */
736b384f39SPeter Avalos 
746b384f39SPeter Avalos int
archive_random(void * buf,size_t nbytes)756b384f39SPeter Avalos archive_random(void *buf, size_t nbytes)
766b384f39SPeter Avalos {
776b384f39SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
786b384f39SPeter Avalos 	HCRYPTPROV hProv;
796b384f39SPeter Avalos 	BOOL success;
806b384f39SPeter Avalos 
816b384f39SPeter Avalos 	success = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
826b384f39SPeter Avalos 	    CRYPT_VERIFYCONTEXT);
83e95abc47Szrj 	if (!success && GetLastError() == (DWORD)NTE_BAD_KEYSET) {
846b384f39SPeter Avalos 		success = CryptAcquireContext(&hProv, NULL, NULL,
856b384f39SPeter Avalos 		    PROV_RSA_FULL, CRYPT_NEWKEYSET);
866b384f39SPeter Avalos 	}
876b384f39SPeter Avalos 	if (success) {
886b384f39SPeter Avalos 		success = CryptGenRandom(hProv, (DWORD)nbytes, (BYTE*)buf);
896b384f39SPeter Avalos 		CryptReleaseContext(hProv, 0);
906b384f39SPeter Avalos 		if (success)
916b384f39SPeter Avalos 			return ARCHIVE_OK;
926b384f39SPeter Avalos 	}
936b384f39SPeter Avalos 	/* TODO: Does this case really happen? */
946b384f39SPeter Avalos 	return ARCHIVE_FAILED;
956b384f39SPeter Avalos #else
966b384f39SPeter Avalos 	arc4random_buf(buf, nbytes);
976b384f39SPeter Avalos 	return ARCHIVE_OK;
986b384f39SPeter Avalos #endif
996b384f39SPeter Avalos }
1006b384f39SPeter Avalos 
1016b384f39SPeter Avalos #if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__))
1026b384f39SPeter Avalos 
1036b384f39SPeter Avalos /*	$OpenBSD: arc4random.c,v 1.24 2013/06/11 16:59:50 deraadt Exp $	*/
1046b384f39SPeter Avalos /*
1056b384f39SPeter Avalos  * Copyright (c) 1996, David Mazieres <dm@uun.org>
1066b384f39SPeter Avalos  * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
1076b384f39SPeter Avalos  *
1086b384f39SPeter Avalos  * Permission to use, copy, modify, and distribute this software for any
1096b384f39SPeter Avalos  * purpose with or without fee is hereby granted, provided that the above
1106b384f39SPeter Avalos  * copyright notice and this permission notice appear in all copies.
1116b384f39SPeter Avalos  *
1126b384f39SPeter Avalos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1136b384f39SPeter Avalos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1146b384f39SPeter Avalos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1156b384f39SPeter Avalos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1166b384f39SPeter Avalos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1176b384f39SPeter Avalos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1186b384f39SPeter Avalos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1196b384f39SPeter Avalos  */
1206b384f39SPeter Avalos 
1216b384f39SPeter Avalos /*
1226b384f39SPeter Avalos  * Arc4 random number generator for OpenBSD.
1236b384f39SPeter Avalos  *
1246b384f39SPeter Avalos  * This code is derived from section 17.1 of Applied Cryptography,
1256b384f39SPeter Avalos  * second edition, which describes a stream cipher allegedly
1266b384f39SPeter Avalos  * compatible with RSA Labs "RC4" cipher (the actual description of
1276b384f39SPeter Avalos  * which is a trade secret).  The same algorithm is used as a stream
1286b384f39SPeter Avalos  * cipher called "arcfour" in Tatu Ylonen's ssh package.
1296b384f39SPeter Avalos  *
1306b384f39SPeter Avalos  * RC4 is a registered trademark of RSA Laboratories.
1316b384f39SPeter Avalos  */
1326b384f39SPeter Avalos 
1336b384f39SPeter Avalos #ifdef __GNUC__
1346b384f39SPeter Avalos #define inline __inline
1356b384f39SPeter Avalos #else				/* !__GNUC__ */
1366b384f39SPeter Avalos #define inline
1376b384f39SPeter Avalos #endif				/* !__GNUC__ */
1386b384f39SPeter Avalos 
1396b384f39SPeter Avalos struct arc4_stream {
1406b384f39SPeter Avalos 	uint8_t i;
1416b384f39SPeter Avalos 	uint8_t j;
1426b384f39SPeter Avalos 	uint8_t s[256];
1436b384f39SPeter Avalos };
1446b384f39SPeter Avalos 
1456b384f39SPeter Avalos #define	RANDOMDEV	"/dev/urandom"
1466b384f39SPeter Avalos #define	KEYSIZE		128
1476b384f39SPeter Avalos #ifdef HAVE_PTHREAD_H
1486b384f39SPeter Avalos static pthread_mutex_t	arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
1496b384f39SPeter Avalos #define	_ARC4_LOCK()	pthread_mutex_lock(&arc4random_mtx);
1506b384f39SPeter Avalos #define	_ARC4_UNLOCK()  pthread_mutex_unlock(&arc4random_mtx);
1516b384f39SPeter Avalos #else
1526b384f39SPeter Avalos #define	_ARC4_LOCK()
1536b384f39SPeter Avalos #define	_ARC4_UNLOCK()
1546b384f39SPeter Avalos #endif
1556b384f39SPeter Avalos 
1566b384f39SPeter Avalos static int rs_initialized;
1576b384f39SPeter Avalos static struct arc4_stream rs;
1586b384f39SPeter Avalos static pid_t arc4_stir_pid;
1596b384f39SPeter Avalos static int arc4_count;
1606b384f39SPeter Avalos 
1616b384f39SPeter Avalos static inline uint8_t arc4_getbyte(void);
1626b384f39SPeter Avalos static void arc4_stir(void);
1636b384f39SPeter Avalos 
1646b384f39SPeter Avalos static inline void
arc4_init(void)1656b384f39SPeter Avalos arc4_init(void)
1666b384f39SPeter Avalos {
1676b384f39SPeter Avalos 	int     n;
1686b384f39SPeter Avalos 
1696b384f39SPeter Avalos 	for (n = 0; n < 256; n++)
1706b384f39SPeter Avalos 		rs.s[n] = n;
1716b384f39SPeter Avalos 	rs.i = 0;
1726b384f39SPeter Avalos 	rs.j = 0;
1736b384f39SPeter Avalos }
1746b384f39SPeter Avalos 
1756b384f39SPeter Avalos static inline void
arc4_addrandom(uint8_t * dat,int datlen)176*50f8aa9cSAntonio Huete Jimenez arc4_addrandom(uint8_t *dat, int datlen)
1776b384f39SPeter Avalos {
1786b384f39SPeter Avalos 	int     n;
1796b384f39SPeter Avalos 	uint8_t si;
1806b384f39SPeter Avalos 
1816b384f39SPeter Avalos 	rs.i--;
1826b384f39SPeter Avalos 	for (n = 0; n < 256; n++) {
1836b384f39SPeter Avalos 		rs.i = (rs.i + 1);
1846b384f39SPeter Avalos 		si = rs.s[rs.i];
1856b384f39SPeter Avalos 		rs.j = (rs.j + si + dat[n % datlen]);
1866b384f39SPeter Avalos 		rs.s[rs.i] = rs.s[rs.j];
1876b384f39SPeter Avalos 		rs.s[rs.j] = si;
1886b384f39SPeter Avalos 	}
1896b384f39SPeter Avalos 	rs.j = rs.i;
1906b384f39SPeter Avalos }
1916b384f39SPeter Avalos 
1926b384f39SPeter Avalos static void
arc4_stir(void)1936b384f39SPeter Avalos arc4_stir(void)
1946b384f39SPeter Avalos {
1956b384f39SPeter Avalos 	int done, fd, i;
1966b384f39SPeter Avalos 	struct {
1976b384f39SPeter Avalos 		struct timeval	tv;
1986b384f39SPeter Avalos 		pid_t		pid;
199*50f8aa9cSAntonio Huete Jimenez 		uint8_t		rnd[KEYSIZE];
2006b384f39SPeter Avalos 	} rdat;
2016b384f39SPeter Avalos 
2026b384f39SPeter Avalos 	if (!rs_initialized) {
2036b384f39SPeter Avalos 		arc4_init();
2046b384f39SPeter Avalos 		rs_initialized = 1;
2056b384f39SPeter Avalos 	}
2066b384f39SPeter Avalos 	done = 0;
2076b384f39SPeter Avalos 	fd = open(RANDOMDEV, O_RDONLY | O_CLOEXEC, 0);
2086b384f39SPeter Avalos 	if (fd >= 0) {
2096b384f39SPeter Avalos 		if (read(fd, &rdat, KEYSIZE) == KEYSIZE)
2106b384f39SPeter Avalos 			done = 1;
2116b384f39SPeter Avalos 		(void)close(fd);
2126b384f39SPeter Avalos 	}
2136b384f39SPeter Avalos 	if (!done) {
2146b384f39SPeter Avalos 		(void)gettimeofday(&rdat.tv, NULL);
2156b384f39SPeter Avalos 		rdat.pid = getpid();
2166b384f39SPeter Avalos 		/* We'll just take whatever was on the stack too... */
2176b384f39SPeter Avalos 	}
2186b384f39SPeter Avalos 
219*50f8aa9cSAntonio Huete Jimenez 	arc4_addrandom((uint8_t *)&rdat, KEYSIZE);
2206b384f39SPeter Avalos 
2216b384f39SPeter Avalos 	/*
2226b384f39SPeter Avalos 	 * Discard early keystream, as per recommendations in:
2236b384f39SPeter Avalos 	 * "(Not So) Random Shuffles of RC4" by Ilya Mironov.
224e95abc47Szrj 	 * As per the Network Operations Division, cryptographic requirements
225e95abc47Szrj 	 * published on wikileaks on March 2017.
2266b384f39SPeter Avalos 	 */
227e95abc47Szrj 
228e95abc47Szrj 	for (i = 0; i < 3072; i++)
2296b384f39SPeter Avalos 		(void)arc4_getbyte();
2306b384f39SPeter Avalos 	arc4_count = 1600000;
2316b384f39SPeter Avalos }
2326b384f39SPeter Avalos 
2336b384f39SPeter Avalos static void
arc4_stir_if_needed(void)2346b384f39SPeter Avalos arc4_stir_if_needed(void)
2356b384f39SPeter Avalos {
2366b384f39SPeter Avalos 	pid_t pid = getpid();
2376b384f39SPeter Avalos 
2386b384f39SPeter Avalos 	if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) {
2396b384f39SPeter Avalos 		arc4_stir_pid = pid;
2406b384f39SPeter Avalos 		arc4_stir();
2416b384f39SPeter Avalos 	}
2426b384f39SPeter Avalos }
2436b384f39SPeter Avalos 
2446b384f39SPeter Avalos static inline uint8_t
arc4_getbyte(void)2456b384f39SPeter Avalos arc4_getbyte(void)
2466b384f39SPeter Avalos {
2476b384f39SPeter Avalos 	uint8_t si, sj;
2486b384f39SPeter Avalos 
2496b384f39SPeter Avalos 	rs.i = (rs.i + 1);
2506b384f39SPeter Avalos 	si = rs.s[rs.i];
2516b384f39SPeter Avalos 	rs.j = (rs.j + si);
2526b384f39SPeter Avalos 	sj = rs.s[rs.j];
2536b384f39SPeter Avalos 	rs.s[rs.i] = sj;
2546b384f39SPeter Avalos 	rs.s[rs.j] = si;
2556b384f39SPeter Avalos 	return (rs.s[(si + sj) & 0xff]);
2566b384f39SPeter Avalos }
2576b384f39SPeter Avalos 
2586b384f39SPeter Avalos static void
arc4random_buf(void * _buf,size_t n)2596b384f39SPeter Avalos arc4random_buf(void *_buf, size_t n)
2606b384f39SPeter Avalos {
261*50f8aa9cSAntonio Huete Jimenez 	uint8_t *buf = (uint8_t *)_buf;
2626b384f39SPeter Avalos 	_ARC4_LOCK();
2636b384f39SPeter Avalos 	arc4_stir_if_needed();
2646b384f39SPeter Avalos 	while (n--) {
2656b384f39SPeter Avalos 		if (--arc4_count <= 0)
2666b384f39SPeter Avalos 			arc4_stir();
2676b384f39SPeter Avalos 		buf[n] = arc4_getbyte();
2686b384f39SPeter Avalos 	}
2696b384f39SPeter Avalos 	_ARC4_UNLOCK();
2706b384f39SPeter Avalos }
2716b384f39SPeter Avalos 
2726b384f39SPeter Avalos #endif /* !HAVE_ARC4RANDOM_BUF */
273