1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2017 Timothy Arceri 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci * 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci#include "detect_os.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#if !DETECT_OS_WINDOWS 28bf215546Sopenharmony_ci#if defined(HAVE_GETRANDOM) 29bf215546Sopenharmony_ci#include <sys/random.h> 30bf215546Sopenharmony_ci#endif 31bf215546Sopenharmony_ci#include <unistd.h> 32bf215546Sopenharmony_ci#include <fcntl.h> 33bf215546Sopenharmony_ci#endif 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci#include <time.h> 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci#include "rand_xor.h" 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci/* Super fast random number generator. 40bf215546Sopenharmony_ci * 41bf215546Sopenharmony_ci * This rand_xorshift128plus function by Sebastiano Vigna belongs 42bf215546Sopenharmony_ci * to the public domain. 43bf215546Sopenharmony_ci */ 44bf215546Sopenharmony_ciuint64_t 45bf215546Sopenharmony_cirand_xorshift128plus(uint64_t seed[2]) 46bf215546Sopenharmony_ci{ 47bf215546Sopenharmony_ci uint64_t *s = seed; 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci uint64_t s1 = s[0]; 50bf215546Sopenharmony_ci const uint64_t s0 = s[1]; 51bf215546Sopenharmony_ci s[0] = s0; 52bf215546Sopenharmony_ci s1 ^= s1 << 23; 53bf215546Sopenharmony_ci s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci return s[1] + s0; 56bf215546Sopenharmony_ci} 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_civoid 59bf215546Sopenharmony_cis_rand_xorshift128plus(uint64_t seed[2], bool randomised_seed) 60bf215546Sopenharmony_ci{ 61bf215546Sopenharmony_ci if (!randomised_seed) { 62bf215546Sopenharmony_ci /* Use a fixed seed */ 63bf215546Sopenharmony_ci seed[0] = 0x3bffb83978e24f88; 64bf215546Sopenharmony_ci seed[1] = 0x9238d5d56c71cd35; 65bf215546Sopenharmony_ci return; 66bf215546Sopenharmony_ci } 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci#if !DETECT_OS_WINDOWS 69bf215546Sopenharmony_ci size_t seed_size = sizeof(uint64_t) * 2; 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci#if defined(HAVE_GETRANDOM) 72bf215546Sopenharmony_ci ssize_t ret = getrandom(seed, seed_size, GRND_NONBLOCK); 73bf215546Sopenharmony_ci if (ret == seed_size) 74bf215546Sopenharmony_ci return; 75bf215546Sopenharmony_ci#endif 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci int fd = open("/dev/urandom", O_RDONLY); 78bf215546Sopenharmony_ci if (fd >= 0) { 79bf215546Sopenharmony_ci if (read(fd, seed, seed_size) == seed_size) { 80bf215546Sopenharmony_ci close(fd); 81bf215546Sopenharmony_ci return; 82bf215546Sopenharmony_ci } 83bf215546Sopenharmony_ci close(fd); 84bf215546Sopenharmony_ci } 85bf215546Sopenharmony_ci#endif 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci seed[0] = 0x3bffb83978e24f88; 88bf215546Sopenharmony_ci seed[1] = time(NULL); 89bf215546Sopenharmony_ci} 90