1299425Smm/*- 2299425Smm * Copyright (c) 2014 Michihiro NAKAJIMA 3299425Smm * All rights reserved. 4299425Smm * 5299425Smm * Redistribution and use in source and binary forms, with or without 6299425Smm * modification, are permitted provided that the following conditions 7299425Smm * are met: 8299425Smm * 1. Redistributions of source code must retain the above copyright 9299425Smm * notice, this list of conditions and the following disclaimer. 10299425Smm * 2. Redistributions in binary form must reproduce the above copyright 11299425Smm * notice, this list of conditions and the following disclaimer in the 12299425Smm * documentation and/or other materials provided with the distribution. 13299425Smm * 14299425Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15299425Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16299425Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17299425Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18299425Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19299425Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20299425Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21299425Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22299425Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23299425Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24299425Smm */ 25299425Smm 26299425Smm#include "archive_platform.h" 27299425Smm__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/archive_random.c 316338 2017-03-31 20:17:30Z mm $"); 28299425Smm 29299425Smm#ifdef HAVE_STDLIB_H 30299425Smm#include <stdlib.h> 31299425Smm#endif 32299425Smm 33299425Smm#if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__)) 34299425Smm 35299425Smm#ifdef HAVE_FCNTL 36299425Smm#include <fcntl.h> 37299425Smm#endif 38299425Smm#ifdef HAVE_LIMITS_H 39299425Smm#include <limits.h> 40299425Smm#endif 41299425Smm#ifdef HAVE_UNISTD_H 42299425Smm#include <unistd.h> 43299425Smm#endif 44299425Smm#ifdef HAVE_SYS_TYPES_H 45299425Smm#include <sys/types.h> 46299425Smm#endif 47299425Smm#ifdef HAVE_SYS_TIME_H 48299425Smm#include <sys/time.h> 49299425Smm#endif 50299425Smm#ifdef HAVE_PTHREAD_H 51299425Smm#include <pthread.h> 52299425Smm#endif 53299425Smm 54299425Smmstatic void arc4random_buf(void *, size_t); 55299425Smm 56299425Smm#endif /* HAVE_ARC4RANDOM_BUF */ 57299425Smm 58299425Smm#include "archive.h" 59299425Smm#include "archive_random_private.h" 60299425Smm 61299425Smm#if defined(HAVE_WINCRYPT_H) && !defined(__CYGWIN__) 62299425Smm#include <wincrypt.h> 63299425Smm#endif 64299425Smm 65299425Smm#ifndef O_CLOEXEC 66299425Smm#define O_CLOEXEC 0 67299425Smm#endif 68299425Smm 69299425Smm/* 70299425Smm * Random number generator function. 71299425Smm * This simply calls arc4random_buf function if the platform provides it. 72299425Smm */ 73299425Smm 74299425Smmint 75299425Smmarchive_random(void *buf, size_t nbytes) 76299425Smm{ 77299425Smm#if defined(_WIN32) && !defined(__CYGWIN__) 78299425Smm HCRYPTPROV hProv; 79299425Smm BOOL success; 80299425Smm 81299425Smm success = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 82299425Smm CRYPT_VERIFYCONTEXT); 83313571Smm if (!success && GetLastError() == (DWORD)NTE_BAD_KEYSET) { 84299425Smm success = CryptAcquireContext(&hProv, NULL, NULL, 85299425Smm PROV_RSA_FULL, CRYPT_NEWKEYSET); 86299425Smm } 87299425Smm if (success) { 88299425Smm success = CryptGenRandom(hProv, (DWORD)nbytes, (BYTE*)buf); 89299425Smm CryptReleaseContext(hProv, 0); 90299425Smm if (success) 91299425Smm return ARCHIVE_OK; 92299425Smm } 93299425Smm /* TODO: Does this case really happen? */ 94299425Smm return ARCHIVE_FAILED; 95299425Smm#else 96299425Smm arc4random_buf(buf, nbytes); 97299425Smm return ARCHIVE_OK; 98299425Smm#endif 99299425Smm} 100299425Smm 101299425Smm#if !defined(HAVE_ARC4RANDOM_BUF) && (!defined(_WIN32) || defined(__CYGWIN__)) 102299425Smm 103299425Smm/* $OpenBSD: arc4random.c,v 1.24 2013/06/11 16:59:50 deraadt Exp $ */ 104299425Smm/* 105299425Smm * Copyright (c) 1996, David Mazieres <dm@uun.org> 106299425Smm * Copyright (c) 2008, Damien Miller <djm@openbsd.org> 107299425Smm * 108299425Smm * Permission to use, copy, modify, and distribute this software for any 109299425Smm * purpose with or without fee is hereby granted, provided that the above 110299425Smm * copyright notice and this permission notice appear in all copies. 111299425Smm * 112299425Smm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 113299425Smm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 114299425Smm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 115299425Smm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 116299425Smm * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 117299425Smm * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 118299425Smm * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 119299425Smm */ 120299425Smm 121299425Smm/* 122299425Smm * Arc4 random number generator for OpenBSD. 123299425Smm * 124299425Smm * This code is derived from section 17.1 of Applied Cryptography, 125299425Smm * second edition, which describes a stream cipher allegedly 126299425Smm * compatible with RSA Labs "RC4" cipher (the actual description of 127299425Smm * which is a trade secret). The same algorithm is used as a stream 128299425Smm * cipher called "arcfour" in Tatu Ylonen's ssh package. 129299425Smm * 130299425Smm * RC4 is a registered trademark of RSA Laboratories. 131299425Smm */ 132299425Smm 133299425Smm#ifdef __GNUC__ 134299425Smm#define inline __inline 135299425Smm#else /* !__GNUC__ */ 136299425Smm#define inline 137299425Smm#endif /* !__GNUC__ */ 138299425Smm 139299425Smmstruct arc4_stream { 140299425Smm uint8_t i; 141299425Smm uint8_t j; 142299425Smm uint8_t s[256]; 143299425Smm}; 144299425Smm 145299425Smm#define RANDOMDEV "/dev/urandom" 146299425Smm#define KEYSIZE 128 147299425Smm#ifdef HAVE_PTHREAD_H 148299425Smmstatic pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; 149299425Smm#define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx); 150299425Smm#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx); 151299425Smm#else 152299425Smm#define _ARC4_LOCK() 153299425Smm#define _ARC4_UNLOCK() 154299425Smm#endif 155299425Smm 156299425Smmstatic int rs_initialized; 157299425Smmstatic struct arc4_stream rs; 158299425Smmstatic pid_t arc4_stir_pid; 159299425Smmstatic int arc4_count; 160299425Smm 161299425Smmstatic inline uint8_t arc4_getbyte(void); 162299425Smmstatic void arc4_stir(void); 163299425Smm 164299425Smmstatic inline void 165299425Smmarc4_init(void) 166299425Smm{ 167299425Smm int n; 168299425Smm 169299425Smm for (n = 0; n < 256; n++) 170299425Smm rs.s[n] = n; 171299425Smm rs.i = 0; 172299425Smm rs.j = 0; 173299425Smm} 174299425Smm 175299425Smmstatic inline void 176299425Smmarc4_addrandom(u_char *dat, int datlen) 177299425Smm{ 178299425Smm int n; 179299425Smm uint8_t si; 180299425Smm 181299425Smm rs.i--; 182299425Smm for (n = 0; n < 256; n++) { 183299425Smm rs.i = (rs.i + 1); 184299425Smm si = rs.s[rs.i]; 185299425Smm rs.j = (rs.j + si + dat[n % datlen]); 186299425Smm rs.s[rs.i] = rs.s[rs.j]; 187299425Smm rs.s[rs.j] = si; 188299425Smm } 189299425Smm rs.j = rs.i; 190299425Smm} 191299425Smm 192299425Smmstatic void 193299425Smmarc4_stir(void) 194299425Smm{ 195299425Smm int done, fd, i; 196299425Smm struct { 197299425Smm struct timeval tv; 198299425Smm pid_t pid; 199299425Smm u_char rnd[KEYSIZE]; 200299425Smm } rdat; 201299425Smm 202299425Smm if (!rs_initialized) { 203299425Smm arc4_init(); 204299425Smm rs_initialized = 1; 205299425Smm } 206299425Smm done = 0; 207299425Smm fd = open(RANDOMDEV, O_RDONLY | O_CLOEXEC, 0); 208299425Smm if (fd >= 0) { 209299425Smm if (read(fd, &rdat, KEYSIZE) == KEYSIZE) 210299425Smm done = 1; 211299425Smm (void)close(fd); 212299425Smm } 213299425Smm if (!done) { 214299425Smm (void)gettimeofday(&rdat.tv, NULL); 215299425Smm rdat.pid = getpid(); 216299425Smm /* We'll just take whatever was on the stack too... */ 217299425Smm } 218299425Smm 219299425Smm arc4_addrandom((u_char *)&rdat, KEYSIZE); 220299425Smm 221299425Smm /* 222299425Smm * Discard early keystream, as per recommendations in: 223299425Smm * "(Not So) Random Shuffles of RC4" by Ilya Mironov. 224316338Smm * As per the Network Operations Division, cryptographic requirements 225316338Smm * published on wikileaks on March 2017. 226299425Smm */ 227316338Smm 228316338Smm for (i = 0; i < 3072; i++) 229299425Smm (void)arc4_getbyte(); 230299425Smm arc4_count = 1600000; 231299425Smm} 232299425Smm 233299425Smmstatic void 234299425Smmarc4_stir_if_needed(void) 235299425Smm{ 236299425Smm pid_t pid = getpid(); 237299425Smm 238299425Smm if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) { 239299425Smm arc4_stir_pid = pid; 240299425Smm arc4_stir(); 241299425Smm } 242299425Smm} 243299425Smm 244299425Smmstatic inline uint8_t 245299425Smmarc4_getbyte(void) 246299425Smm{ 247299425Smm uint8_t si, sj; 248299425Smm 249299425Smm rs.i = (rs.i + 1); 250299425Smm si = rs.s[rs.i]; 251299425Smm rs.j = (rs.j + si); 252299425Smm sj = rs.s[rs.j]; 253299425Smm rs.s[rs.i] = sj; 254299425Smm rs.s[rs.j] = si; 255299425Smm return (rs.s[(si + sj) & 0xff]); 256299425Smm} 257299425Smm 258299425Smmstatic void 259299425Smmarc4random_buf(void *_buf, size_t n) 260299425Smm{ 261299425Smm u_char *buf = (u_char *)_buf; 262299425Smm _ARC4_LOCK(); 263299425Smm arc4_stir_if_needed(); 264299425Smm while (n--) { 265299425Smm if (--arc4_count <= 0) 266299425Smm arc4_stir(); 267299425Smm buf[n] = arc4_getbyte(); 268299425Smm } 269299425Smm _ARC4_UNLOCK(); 270299425Smm} 271299425Smm 272299425Smm#endif /* !HAVE_ARC4RANDOM_BUF */ 273