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 2417d15b25SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2517d15b25SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2617d15b25SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2717d15b25SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2817d15b25SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 2917d15b25SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 3017d15b25SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3117d15b25SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 3217d15b25SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3317d15b25SDag-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 a random generator state for use 52b7579f77SDag-Erling Smørgrav * @param from: if not NULL, the seed is taken from this random structure. 53b7579f77SDag-Erling Smørgrav * can be used to seed random states via a parent-random-state that 54b7579f77SDag-Erling Smørgrav * is itself seeded with entropy. 55b7579f77SDag-Erling Smørgrav * @return new state or NULL alloc failure. 56b7579f77SDag-Erling Smørgrav */ 57*0eefd307SCy Schubert struct ub_randstate* ub_initstate(struct ub_randstate* from); 58b7579f77SDag-Erling Smørgrav 59b7579f77SDag-Erling Smørgrav /** 60b7579f77SDag-Erling Smørgrav * Generate next random number from the state passed along. 61b7579f77SDag-Erling Smørgrav * Thread safe, so random numbers are repeatable. 62b7579f77SDag-Erling Smørgrav * @param state: must have been initialised with ub_initstate. 63b7579f77SDag-Erling Smørgrav * @return: random 31 bit value. 64b7579f77SDag-Erling Smørgrav */ 65b7579f77SDag-Erling Smørgrav long int ub_random(struct ub_randstate* state); 66b7579f77SDag-Erling Smørgrav 67b7579f77SDag-Erling Smørgrav /** 68b7579f77SDag-Erling Smørgrav * Generate random number between 0 and x-1. No modulo bias. 69b7579f77SDag-Erling Smørgrav * @param state: must have been initialised with ub_initstate. 70b7579f77SDag-Erling Smørgrav * @param x: an upper limit. not (negative or zero). must be smaller than 2**31. 71b7579f77SDag-Erling Smørgrav * @return: random value between 0..x-1. Possibly more than one 72b7579f77SDag-Erling Smørgrav * random number is picked from the random stream to satisfy this. 73b7579f77SDag-Erling Smørgrav */ 74b7579f77SDag-Erling Smørgrav long int ub_random_max(struct ub_randstate* state, long int x); 75b7579f77SDag-Erling Smørgrav 76b7579f77SDag-Erling Smørgrav /** 77b7579f77SDag-Erling Smørgrav * Delete the random state. 78b7579f77SDag-Erling Smørgrav * @param state: to delete. 79b7579f77SDag-Erling Smørgrav */ 80b7579f77SDag-Erling Smørgrav void ub_randfree(struct ub_randstate* state); 81b7579f77SDag-Erling Smørgrav 82b7579f77SDag-Erling Smørgrav #endif /* UTIL_RANDOM_H */ 83