1b7579f77SDag-Erling Smørgrav /* 2b7579f77SDag-Erling Smørgrav * util/random.h - thread safe random generator, which is reasonably secure. 3b7579f77SDag-Erling Smørgrav * 4b7579f77SDag-Erling Smørgrav * Copyright (c) 2007, NLnet Labs. All rights reserved. 5b7579f77SDag-Erling Smørgrav * 6b7579f77SDag-Erling Smørgrav * This software is open source. 7b7579f77SDag-Erling Smørgrav * 8b7579f77SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9b7579f77SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10b7579f77SDag-Erling Smørgrav * are met: 11b7579f77SDag-Erling Smørgrav * 12b7579f77SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14b7579f77SDag-Erling Smørgrav * 15b7579f77SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17b7579f77SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18b7579f77SDag-Erling Smørgrav * 19b7579f77SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20b7579f77SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21b7579f77SDag-Erling Smørgrav * specific prior written permission. 22b7579f77SDag-Erling Smørgrav * 23b7579f77SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24*17d15b25SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25*17d15b25SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26*17d15b25SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27*17d15b25SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28*17d15b25SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29*17d15b25SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30*17d15b25SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31*17d15b25SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32*17d15b25SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33*17d15b25SDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34b7579f77SDag-Erling Smørgrav */ 35b7579f77SDag-Erling Smørgrav 36b7579f77SDag-Erling Smørgrav #ifndef UTIL_RANDOM_H 37b7579f77SDag-Erling Smørgrav #define UTIL_RANDOM_H 38b7579f77SDag-Erling Smørgrav 39b7579f77SDag-Erling Smørgrav /** 40b7579f77SDag-Erling Smørgrav * \file 41b7579f77SDag-Erling Smørgrav * Thread safe random functions. Similar to arc4random() with an explicit 42b7579f77SDag-Erling Smørgrav * initialisation routine. 43b7579f77SDag-Erling Smørgrav */ 44b7579f77SDag-Erling Smørgrav 45b7579f77SDag-Erling Smørgrav /** 46b7579f77SDag-Erling Smørgrav * random state structure. 47b7579f77SDag-Erling Smørgrav */ 48b7579f77SDag-Erling Smørgrav struct ub_randstate; 49b7579f77SDag-Erling Smørgrav 50b7579f77SDag-Erling Smørgrav /** 51b7579f77SDag-Erling Smørgrav * Initialize the system randomness. Obtains entropy from the system 52b7579f77SDag-Erling Smørgrav * before a chroot or privilege makes it unavailable. 53b7579f77SDag-Erling Smørgrav * You do not have to call this, otherwise ub_initstate does so. 54b7579f77SDag-Erling Smørgrav * @param seed: seed value to create state (if no good entropy is found). 55b7579f77SDag-Erling Smørgrav */ 56b7579f77SDag-Erling Smørgrav void ub_systemseed(unsigned int seed); 57b7579f77SDag-Erling Smørgrav 58b7579f77SDag-Erling Smørgrav /** 59b7579f77SDag-Erling Smørgrav * Initialize a random generator state for use 60b7579f77SDag-Erling Smørgrav * @param seed: seed value to create state contents. 61b7579f77SDag-Erling Smørgrav * (ignored for arc4random). 62b7579f77SDag-Erling Smørgrav * @param from: if not NULL, the seed is taken from this random structure. 63b7579f77SDag-Erling Smørgrav * can be used to seed random states via a parent-random-state that 64b7579f77SDag-Erling Smørgrav * is itself seeded with entropy. 65b7579f77SDag-Erling Smørgrav * @return new state or NULL alloc failure. 66b7579f77SDag-Erling Smørgrav */ 67b7579f77SDag-Erling Smørgrav struct ub_randstate* ub_initstate(unsigned int seed, 68b7579f77SDag-Erling Smørgrav struct ub_randstate* from); 69b7579f77SDag-Erling Smørgrav 70b7579f77SDag-Erling Smørgrav /** 71b7579f77SDag-Erling Smørgrav * Generate next random number from the state passed along. 72b7579f77SDag-Erling Smørgrav * Thread safe, so random numbers are repeatable. 73b7579f77SDag-Erling Smørgrav * @param state: must have been initialised with ub_initstate. 74b7579f77SDag-Erling Smørgrav * @return: random 31 bit value. 75b7579f77SDag-Erling Smørgrav */ 76b7579f77SDag-Erling Smørgrav long int ub_random(struct ub_randstate* state); 77b7579f77SDag-Erling Smørgrav 78b7579f77SDag-Erling Smørgrav /** 79b7579f77SDag-Erling Smørgrav * Generate random number between 0 and x-1. No modulo bias. 80b7579f77SDag-Erling Smørgrav * @param state: must have been initialised with ub_initstate. 81b7579f77SDag-Erling Smørgrav * @param x: an upper limit. not (negative or zero). must be smaller than 2**31. 82b7579f77SDag-Erling Smørgrav * @return: random value between 0..x-1. Possibly more than one 83b7579f77SDag-Erling Smørgrav * random number is picked from the random stream to satisfy this. 84b7579f77SDag-Erling Smørgrav */ 85b7579f77SDag-Erling Smørgrav long int ub_random_max(struct ub_randstate* state, long int x); 86b7579f77SDag-Erling Smørgrav 87b7579f77SDag-Erling Smørgrav /** 88b7579f77SDag-Erling Smørgrav * Delete the random state. 89b7579f77SDag-Erling Smørgrav * @param state: to delete. 90b7579f77SDag-Erling Smørgrav */ 91b7579f77SDag-Erling Smørgrav void ub_randfree(struct ub_randstate* state); 92b7579f77SDag-Erling Smørgrav 93b7579f77SDag-Erling Smørgrav #endif /* UTIL_RANDOM_H */ 94