1933707f3Ssthen /* 2933707f3Ssthen * util/random.h - thread safe random generator, which is reasonably secure. 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 245d76a658Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 255d76a658Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 265d76a658Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 275d76a658Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 285d76a658Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 295d76a658Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 305d76a658Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 315d76a658Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 325d76a658Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 335d76a658Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen 36933707f3Ssthen #ifndef UTIL_RANDOM_H 37933707f3Ssthen #define UTIL_RANDOM_H 38933707f3Ssthen 39933707f3Ssthen /** 40933707f3Ssthen * \file 41933707f3Ssthen * Thread safe random functions. Similar to arc4random() with an explicit 42933707f3Ssthen * initialisation routine. 43933707f3Ssthen */ 44933707f3Ssthen 45933707f3Ssthen /** 46933707f3Ssthen * random state structure. 47933707f3Ssthen */ 48933707f3Ssthen struct ub_randstate; 49933707f3Ssthen 50933707f3Ssthen /** 51933707f3Ssthen * Initialize a random generator state for use 52933707f3Ssthen * @param from: if not NULL, the seed is taken from this random structure. 53933707f3Ssthen * can be used to seed random states via a parent-random-state that 54933707f3Ssthen * is itself seeded with entropy. 55933707f3Ssthen * @return new state or NULL alloc failure. 56933707f3Ssthen */ 57*b0dfc31bSsthen struct ub_randstate* ub_initstate(struct ub_randstate* from); 58933707f3Ssthen 59933707f3Ssthen /** 60933707f3Ssthen * Generate next random number from the state passed along. 61933707f3Ssthen * Thread safe, so random numbers are repeatable. 62933707f3Ssthen * @param state: must have been initialised with ub_initstate. 63933707f3Ssthen * @return: random 31 bit value. 64933707f3Ssthen */ 65933707f3Ssthen long int ub_random(struct ub_randstate* state); 66933707f3Ssthen 67933707f3Ssthen /** 68933707f3Ssthen * Generate random number between 0 and x-1. No modulo bias. 69933707f3Ssthen * @param state: must have been initialised with ub_initstate. 70933707f3Ssthen * @param x: an upper limit. not (negative or zero). must be smaller than 2**31. 71933707f3Ssthen * @return: random value between 0..x-1. Possibly more than one 72933707f3Ssthen * random number is picked from the random stream to satisfy this. 73933707f3Ssthen */ 74933707f3Ssthen long int ub_random_max(struct ub_randstate* state, long int x); 75933707f3Ssthen 76933707f3Ssthen /** 77933707f3Ssthen * Delete the random state. 78933707f3Ssthen * @param state: to delete. 79933707f3Ssthen */ 80933707f3Ssthen void ub_randfree(struct ub_randstate* state); 81933707f3Ssthen 82933707f3Ssthen #endif /* UTIL_RANDOM_H */ 83