1 | /* $Id: rand.h 25645 2010-01-05 09:29:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Internal RTRand header
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef ___internal_rand_h
|
---|
32 | #define ___internal_rand_h
|
---|
33 |
|
---|
34 | #include <iprt/types.h>
|
---|
35 | #include <iprt/critsect.h>
|
---|
36 |
|
---|
37 | /** Pointer to a random number generator instance. */
|
---|
38 | typedef struct RTRANDINT *PRTRANDINT;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Random number generator instance.
|
---|
42 | *
|
---|
43 | * @remarks Not sure if it makes sense to have three random getters...
|
---|
44 | */
|
---|
45 | typedef struct RTRANDINT
|
---|
46 | {
|
---|
47 | /** Magic value (RTRANDINT_MAGIC). */
|
---|
48 | uint32_t u32Magic;
|
---|
49 | #if 0 /** @todo later. */
|
---|
50 | /** Fast mutex semaphore that serializes the access, this is optional. */
|
---|
51 | PRTCRITSECT pCritSect;
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Generates random bytes.
|
---|
56 | *
|
---|
57 | * @param pThis Pointer to the instance data.
|
---|
58 | * @param pb Where to store the bytes.
|
---|
59 | * @param cb The number of bytes to produce.
|
---|
60 | */
|
---|
61 | DECLCALLBACKMEMBER(void , pfnGetBytes)(PRTRANDINT pThis, uint8_t *pb, size_t cb);
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Generates a unsigned 32-bit random number.
|
---|
65 | *
|
---|
66 | * @returns The random number.
|
---|
67 | * @param pThis Pointer to the instance data.
|
---|
68 | * @param u32First The first number in the range.
|
---|
69 | * @param u32Last The last number in the range (i.e. inclusive).
|
---|
70 | */
|
---|
71 | DECLCALLBACKMEMBER(uint32_t, pfnGetU32)(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last);
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Generates a unsigned 64-bit random number.
|
---|
75 | *
|
---|
76 | * @returns The random number.
|
---|
77 | * @param pThis Pointer to the instance data.
|
---|
78 | * @param u64First The first number in the range.
|
---|
79 | * @param u64Last The last number in the range (i.e. inclusive).
|
---|
80 | */
|
---|
81 | DECLCALLBACKMEMBER(uint64_t, pfnGetU64)(PRTRANDINT pThis, uint64_t u64First, uint64_t u64Last);
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Generic seeding.
|
---|
85 | *
|
---|
86 | * @returns IPRT status code.
|
---|
87 | * @retval VERR_NOT_SUPPORTED if it isn't a pseudo generator.
|
---|
88 | *
|
---|
89 | * @param pThis Pointer to the instance data.
|
---|
90 | * @param u64Seed The seed.
|
---|
91 | */
|
---|
92 | DECLCALLBACKMEMBER(int, pfnSeed)(PRTRANDINT pThis, uint64_t u64Seed);
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Save the current state of a pseudo generator.
|
---|
96 | *
|
---|
97 | * This can be use to save the state so it can later be resumed at the same
|
---|
98 | * position.
|
---|
99 | *
|
---|
100 | * @returns IPRT status code.
|
---|
101 | * @retval VINF_SUCCESS on success. *pcbState contains the length of the
|
---|
102 | * returned string and pszState contains the state string.
|
---|
103 | * @retval VERR_BUFFER_OVERFLOW if the supplied buffer is too small. *pcbState
|
---|
104 | * will contain the necessary buffer size.
|
---|
105 | * @retval VERR_NOT_SUPPORTED by non-psuedo generators.
|
---|
106 | *
|
---|
107 | * @param pThis Pointer to the instance data.
|
---|
108 | * @param pszState Where to store the state. The returned string will be
|
---|
109 | * null terminated and printable.
|
---|
110 | * @param pcbState The size of the buffer pszState points to on input, the
|
---|
111 | * size required / used on return (including the
|
---|
112 | * terminator, thus the 'cb' instead of 'cch').
|
---|
113 | */
|
---|
114 | DECLCALLBACKMEMBER(int, pfnSaveState)(PRTRANDINT pThis, char *pszState, size_t *pcbState);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Restores the state of a pseudo generator.
|
---|
118 | *
|
---|
119 | * The state must've been obtained using pfnGetState.
|
---|
120 | *
|
---|
121 | * @returns IPRT status code.
|
---|
122 | * @retval VERR_PARSE_ERROR if the state string is malformed.
|
---|
123 | * @retval VERR_NOT_SUPPORTED by non-psuedo generators.
|
---|
124 | *
|
---|
125 | * @param pThis Pointer to the instance data.
|
---|
126 | * @param pszState The state to load.
|
---|
127 | */
|
---|
128 | DECLCALLBACKMEMBER(int, pfnRestoreState)(PRTRANDINT pThis, char const *pszState);
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Destroys the instance.
|
---|
132 | *
|
---|
133 | * The callee is responsible for freeing all resources, including
|
---|
134 | * the instance data.
|
---|
135 | *
|
---|
136 | * @returns IPRT status code. State undefined on failure.
|
---|
137 | * @param pThis Pointer to the instance data.
|
---|
138 | */
|
---|
139 | DECLCALLBACKMEMBER(int, pfnDestroy)(PRTRANDINT pThis);
|
---|
140 |
|
---|
141 | /** Union containing the specific state info for each generator. */
|
---|
142 | union
|
---|
143 | {
|
---|
144 | struct RTRandParkMiller
|
---|
145 | {
|
---|
146 | /** The context. */
|
---|
147 | uint32_t u32Ctx;
|
---|
148 | /** The number of single bits used to fill in the 31st bit. */
|
---|
149 | uint32_t u32Bits;
|
---|
150 | /** The number bits in u32Bits. */
|
---|
151 | uint32_t cBits;
|
---|
152 | } ParkMiller;
|
---|
153 |
|
---|
154 | struct RTRandFile
|
---|
155 | {
|
---|
156 | /** The file handle. */
|
---|
157 | RTFILE hFile;
|
---|
158 | } File;
|
---|
159 | } u;
|
---|
160 | } RTRANDINT;
|
---|
161 |
|
---|
162 |
|
---|
163 | RT_C_DECLS_BEGIN
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Initialize OS facilities for generating random bytes.
|
---|
167 | */
|
---|
168 | void rtRandLazyInitNative(void);
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Generate random bytes using OS facilities.
|
---|
172 | *
|
---|
173 | * @returns VINF_SUCCESS on success, some error status code on failure.
|
---|
174 | * @param pv Where to store the random bytes.
|
---|
175 | * @param cb How many random bytes to store.
|
---|
176 | */
|
---|
177 | int rtRandGenBytesNative(void *pv, size_t cb);
|
---|
178 |
|
---|
179 | void rtRandGenBytesFallback(void *pv, size_t cb) RT_NO_THROW;
|
---|
180 |
|
---|
181 | DECLCALLBACK(void) rtRandAdvSynthesizeBytesFromU32(PRTRANDINT pThis, uint8_t *pb, size_t cb);
|
---|
182 | DECLCALLBACK(void) rtRandAdvSynthesizeBytesFromU64(PRTRANDINT pThis, uint8_t *pb, size_t cb);
|
---|
183 | DECLCALLBACK(uint32_t) rtRandAdvSynthesizeU32FromBytes(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last);
|
---|
184 | DECLCALLBACK(uint32_t) rtRandAdvSynthesizeU32FromU64(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last);
|
---|
185 | DECLCALLBACK(uint64_t) rtRandAdvSynthesizeU64FromBytes(PRTRANDINT pThis, uint64_t u64First, uint64_t u64Last);
|
---|
186 | DECLCALLBACK(uint64_t) rtRandAdvSynthesizeU64FromU32(PRTRANDINT pThis, uint64_t u64First, uint64_t u64Last);
|
---|
187 | DECLCALLBACK(int) rtRandAdvStubSeed(PRTRANDINT pThis, uint64_t u64Seed);
|
---|
188 | DECLCALLBACK(int) rtRandAdvStubSaveState(PRTRANDINT pThis, char *pszState, size_t *pcbState);
|
---|
189 | DECLCALLBACK(int) rtRandAdvStubRestoreState(PRTRANDINT pThis, char const *pszState);
|
---|
190 | DECLCALLBACK(int) rtRandAdvDefaultDestroy(PRTRANDINT pThis);
|
---|
191 |
|
---|
192 | RT_C_DECLS_END
|
---|
193 |
|
---|
194 | #endif
|
---|
195 |
|
---|