summaryrefslogtreecommitdiff
path: root/include/iprt/rand.h
blob: 3a58c5ddb998ae8f4659da6267a92d22f292c63d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/** @file
 * IPRT - Random Numbers and Byte Streams.
 */

/*
 * Copyright (C) 2006-2012 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */

#ifndef ___iprt_rand_h
#define ___iprt_rand_h

#include <iprt/cdefs.h>
#include <iprt/types.h>

RT_C_DECLS_BEGIN

/** @defgroup grp_rt_rand       RTRand - Random Numbers and Byte Streams
 * @ingroup grp_rt
 * @{
 */

/**
 * Fills a buffer with random bytes.
 *
 * @param   pv  Where to store the random bytes.
 * @param   cb  Number of bytes to generate.
 */
RTDECL(void) RTRandBytes(void *pv, size_t cb) RT_NO_THROW;

/**
 * Generate a 32-bit signed random number in the set [i32First..i32Last].
 *
 * @returns The random number.
 * @param   i32First    First number in the set.
 * @param   i32Last     Last number in the set.
 */
RTDECL(int32_t) RTRandS32Ex(int32_t i32First, int32_t i32Last) RT_NO_THROW;

/**
 * Generate a 32-bit signed random number.
 *
 * @returns The random number.
 */
RTDECL(int32_t) RTRandS32(void) RT_NO_THROW;

/**
 * Generate a 32-bit unsigned random number in the set [u32First..u32Last].
 *
 * @returns The random number.
 * @param   u32First    First number in the set.
 * @param   u32Last     Last number in the set.
 */
RTDECL(uint32_t) RTRandU32Ex(uint32_t u32First, uint32_t u32Last) RT_NO_THROW;

/**
 * Generate a 32-bit unsigned random number.
 *
 * @returns The random number.
 */
RTDECL(uint32_t) RTRandU32(void) RT_NO_THROW;

/**
 * Generate a 64-bit signed random number in the set [i64First..i64Last].
 *
 * @returns The random number.
 * @param   i64First    First number in the set.
 * @param   i64Last     Last number in the set.
 */
RTDECL(int64_t) RTRandS64Ex(int64_t i64First, int64_t i64Last) RT_NO_THROW;

/**
 * Generate a 64-bit signed random number.
 *
 * @returns The random number.
 */
RTDECL(int64_t) RTRandS64(void) RT_NO_THROW;

/**
 * Generate a 64-bit unsigned random number in the set [u64First..u64Last].
 *
 * @returns The random number.
 * @param   u64First    First number in the set.
 * @param   u64Last     Last number in the set.
 */
RTDECL(uint64_t) RTRandU64Ex(uint64_t u64First, uint64_t u64Last) RT_NO_THROW;

/**
 * Generate a 64-bit unsigned random number.
 *
 * @returns The random number.
 */
RTDECL(uint64_t) RTRandU64(void) RT_NO_THROW;


/**
 * Create an instance of the default random number generator.
 *
 * @returns IPRT status code.
 * @param   phRand      Where to return the handle to the new random number
 *                      generator.
 */
RTDECL(int) RTRandAdvCreate(PRTRAND phRand) RT_NO_THROW;

/**
 * Create an instance of the default pseudo random number generator.
 *
 * @returns IPRT status code.
 * @param   phRand      Where to store the handle to the generator.
 */
RTDECL(int) RTRandAdvCreatePseudo(PRTRAND phRand) RT_NO_THROW;

/**
 * Create an instance of the Park-Miller pseudo random number generator.
 *
 * @returns IPRT status code.
 * @param   phRand      Where to store the handle to the generator.
 */
RTDECL(int) RTRandAdvCreateParkMiller(PRTRAND phRand) RT_NO_THROW;

/**
 * Create an instance of the faster random number generator for the OS.
 *
 * @returns IPRT status code.
 * @retval  VERR_NOT_SUPPORTED on platforms which doesn't have this feature.
 * @retval  VERR_FILE_NOT_FOUND on system where the random generator hasn't
 *          been installed or configured correctly.
 * @retval  VERR_PATH_NOT_FOUND for the same reasons as VERR_FILE_NOT_FOUND.
 *
 * @param   phRand      Where to store the handle to the generator.
 *
 * @remarks Think /dev/urandom.
 */
RTDECL(int) RTRandAdvCreateSystemFaster(PRTRAND phRand) RT_NO_THROW;

/**
 * Create an instance of the truer random number generator for the OS.
 *
 * Don't use this unless you seriously need good random numbers because most
 * systems will have will have problems producing sufficient entropy for this
 * and you'll end up blocking while it accumulates.
 *
 * @returns IPRT status code.
 * @retval  VERR_NOT_SUPPORTED on platforms which doesn't have this feature.
 * @retval  VERR_FILE_NOT_FOUND on system where the random generator hasn't
 *          been installed or configured correctly.
 * @retval  VERR_PATH_NOT_FOUND for the same reasons as VERR_FILE_NOT_FOUND.
 *
 * @param   phRand      Where to store the handle to the generator.
 *
 * @remarks Think /dev/random.
 */
RTDECL(int) RTRandAdvCreateSystemTruer(PRTRAND phRand) RT_NO_THROW;

/**
 * Destroys a random number generator.
 *
 * @returns IPRT status code.
 * @param   hRand       Handle to the random number generator.
 */
RTDECL(int) RTRandAdvDestroy(RTRAND hRand) RT_NO_THROW;

/**
 * Generic method for seeding of a random number generator.
 *
 * The different generators may have specialized methods for
 * seeding, use one of those if you desire better control
 * over the result.
 *
 * @returns IPRT status code.
 * @retval  VERR_NOT_SUPPORTED if it isn't a pseudo generator.
 *
 * @param   hRand       Handle to the random number generator.
 * @param   u64Seed     Seed.
 */
RTDECL(int) RTRandAdvSeed(RTRAND hRand, uint64_t u64Seed) RT_NO_THROW;

/**
 * Save the current state of a pseudo generator.
 *
 * This can be use to save the state so it can later be resumed at the same
 * position.
 *
 * @returns IPRT status code.
 * @retval  VINF_SUCCESS on success. *pcbState contains the length of the
 *          returned string and pszState contains the state string.
 * @retval  VERR_BUFFER_OVERFLOW if the supplied buffer is too small. *pcbState
 *          will contain the necessary buffer size.
 * @retval  VERR_NOT_SUPPORTED by non-psuedo generators.
 *
 * @param   hRand       Handle to the random number generator.
 * @param   pszState    Where to store the state. The returned string will be
 *                      null terminated and printable.
 * @param   pcbState    The size of the buffer pszState points to on input, the
 *                      size required / used on return (including the
 *                      terminator, thus the 'cb' instead of 'cch').
 */
RTDECL(int) RTRandAdvSaveState(RTRAND hRand, char *pszState, size_t *pcbState) RT_NO_THROW;

/**
 * Restores the state of a pseudo generator.
 *
 * The state must have been obtained using RTRandAdvGetState.
 *
 * @returns IPRT status code.
 * @retval  VERR_PARSE_ERROR if the state string is malformed.
 * @retval  VERR_NOT_SUPPORTED by non-psuedo generators.
 *
 * @param   hRand       Handle to the random number generator.
 * @param   pszState    The state to load.
 */
RTDECL(int) RTRandAdvRestoreState(RTRAND hRand, char const *pszState) RT_NO_THROW;

/**
 * Fills a buffer with random bytes.
 *
 * @param   hRand       Handle to the random number generator.
 * @param   pv  Where to store the random bytes.
 * @param   cb  Number of bytes to generate.
 */
RTDECL(void) RTRandAdvBytes(RTRAND hRand, void *pv, size_t cb) RT_NO_THROW;

/**
 * Generate a 32-bit signed random number in the set [i32First..i32Last].
 *
 * @returns The random number.
 * @param   hRand       Handle to the random number generator.
 * @param   i32First    First number in the set.
 * @param   i32Last     Last number in the set.
 */
RTDECL(int32_t) RTRandAdvS32Ex(RTRAND hRand, int32_t i32First, int32_t i32Last) RT_NO_THROW;

/**
 * Generate a 32-bit signed random number.
 *
 * @returns The random number.
 * @param   hRand       Handle to the random number generator.
 */
RTDECL(int32_t) RTRandAdvS32(RTRAND hRand) RT_NO_THROW;

/**
 * Generate a 32-bit unsigned random number in the set [u32First..u32Last].
 *
 * @returns The random number.
 * @param   hRand       Handle to the random number generator.
 * @param   u32First    First number in the set.
 * @param   u32Last     Last number in the set.
 */
RTDECL(uint32_t) RTRandAdvU32Ex(RTRAND hRand, uint32_t u32First, uint32_t u32Last) RT_NO_THROW;

/**
 * Generate a 32-bit unsigned random number.
 *
 * @returns The random number.
 * @param   hRand       Handle to the random number generator.
 */
RTDECL(uint32_t) RTRandAdvU32(RTRAND hRand) RT_NO_THROW;

/**
 * Generate a 64-bit signed random number in the set [i64First..i64Last].
 *
 * @returns The random number.
 * @param   hRand       Handle to the random number generator.
 * @param   i64First    First number in the set.
 * @param   i64Last     Last number in the set.
 */
RTDECL(int64_t) RTRandAdvS64Ex(RTRAND hRand, int64_t i64First, int64_t i64Last) RT_NO_THROW;

/**
 * Generate a 64-bit signed random number.
 *
 * @returns The random number.
 */
RTDECL(int64_t) RTRandAdvS64(RTRAND hRand) RT_NO_THROW;

/**
 * Generate a 64-bit unsigned random number in the set [u64First..u64Last].
 *
 * @returns The random number.
 * @param   hRand       Handle to the random number generator.
 * @param   u64First    First number in the set.
 * @param   u64Last     Last number in the set.
 */
RTDECL(uint64_t) RTRandAdvU64Ex(RTRAND hRand, uint64_t u64First, uint64_t u64Last) RT_NO_THROW;

/**
 * Generate a 64-bit unsigned random number.
 *
 * @returns The random number.
 * @param   hRand       Handle to the random number generator.
 */
RTDECL(uint64_t) RTRandAdvU64(RTRAND hRand) RT_NO_THROW;


/** @} */

RT_C_DECLS_END


#endif