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