1 | /* $Id: rand.h 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Internal RTRand header
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 |
|
---|
27 | #ifndef ___internal_rand_h
|
---|
28 | #define ___internal_rand_h
|
---|
29 |
|
---|
30 | #include <iprt/types.h>
|
---|
31 | #include <iprt/critsect.h>
|
---|
32 |
|
---|
33 | /** Pointer to a random number generator instance. */
|
---|
34 | typedef struct RTRANDINT *PRTRANDINT;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Random number generator instance.
|
---|
38 | *
|
---|
39 | * @remarks Not sure if it makes sense to have three random getters...
|
---|
40 | */
|
---|
41 | typedef struct RTRANDINT
|
---|
42 | {
|
---|
43 | /** Magic value (RTRANDINT_MAGIC). */
|
---|
44 | uint32_t u32Magic;
|
---|
45 | #if 0 /** @todo later. */
|
---|
46 | /** Fast mutex semaphore that serializes the access, this is optional. */
|
---|
47 | PRTCRITSECT pCritSect;
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Generates random bytes.
|
---|
52 | *
|
---|
53 | * @param pThis Pointer to the instance data.
|
---|
54 | * @param pb Where to store the bytes.
|
---|
55 | * @param cb The number of bytes to produce.
|
---|
56 | */
|
---|
57 | DECLCALLBACKMEMBER(void , pfnGetBytes)(PRTRANDINT pThis, uint8_t *pb, size_t cb);
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Generates a unsigned 32-bit random number.
|
---|
61 | *
|
---|
62 | * @returns The random number.
|
---|
63 | * @param pThis Pointer to the instance data.
|
---|
64 | * @param u32First The first number in the range.
|
---|
65 | * @param u32Last The last number in the range (i.e. inclusive).
|
---|
66 | */
|
---|
67 | DECLCALLBACKMEMBER(uint32_t, pfnGetU32)(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last);
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Generates a unsigned 64-bit random number.
|
---|
71 | *
|
---|
72 | * @returns The random number.
|
---|
73 | * @param pThis Pointer to the instance data.
|
---|
74 | * @param u64First The first number in the range.
|
---|
75 | * @param u64Last The last number in the range (i.e. inclusive).
|
---|
76 | */
|
---|
77 | DECLCALLBACKMEMBER(uint64_t, pfnGetU64)(PRTRANDINT pThis, uint64_t u64First, uint64_t u64Last);
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Generic seeding.
|
---|
81 | *
|
---|
82 | * @returns IPRT status code.
|
---|
83 | * @retval VERR_NOT_SUPPORTED if it isn't a pseudo generator.
|
---|
84 | *
|
---|
85 | * @param pThis Pointer to the instance data.
|
---|
86 | * @param u64Seed The seed.
|
---|
87 | */
|
---|
88 | DECLCALLBACKMEMBER(int, pfnSeed)(PRTRANDINT pThis, uint64_t u64Seed);
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Save the current state of a pseudo generator.
|
---|
92 | *
|
---|
93 | * This can be use to save the state so it can later be resumed at the same
|
---|
94 | * position.
|
---|
95 | *
|
---|
96 | * @returns IPRT status code.
|
---|
97 | * @retval VINF_SUCCESS on success. *pcbState contains the length of the
|
---|
98 | * returned string and pszState contains the state string.
|
---|
99 | * @retval VERR_BUFFER_OVERFLOW if the supplied buffer is too small. *pcbState
|
---|
100 | * will contain the necessary buffer size.
|
---|
101 | * @retval VERR_NOT_SUPPORTED by non-psuedo generators.
|
---|
102 | *
|
---|
103 | * @param pThis Pointer to the instance data.
|
---|
104 | * @param pszState Where to store the state. The returned string will be
|
---|
105 | * null terminated and printable.
|
---|
106 | * @param pcbState The size of the buffer pszState points to on input, the
|
---|
107 | * size required / used on return (including the
|
---|
108 | * terminator, thus the 'cb' instead of 'cch').
|
---|
109 | */
|
---|
110 | DECLCALLBACKMEMBER(int, pfnSaveState)(PRTRANDINT pThis, char *pszState, size_t *pcbState);
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Restores the state of a pseudo generator.
|
---|
114 | *
|
---|
115 | * The state must have been obtained using pfnGetState.
|
---|
116 | *
|
---|
117 | * @returns IPRT status code.
|
---|
118 | * @retval VERR_PARSE_ERROR if the state string is malformed.
|
---|
119 | * @retval VERR_NOT_SUPPORTED by non-psuedo generators.
|
---|
120 | *
|
---|
121 | * @param pThis Pointer to the instance data.
|
---|
122 | * @param pszState The state to load.
|
---|
123 | */
|
---|
124 | DECLCALLBACKMEMBER(int, pfnRestoreState)(PRTRANDINT pThis, char const *pszState);
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Destroys the instance.
|
---|
128 | *
|
---|
129 | * The callee is responsible for freeing all resources, including
|
---|
130 | * the instance data.
|
---|
131 | *
|
---|
132 | * @returns IPRT status code. State undefined on failure.
|
---|
133 | * @param pThis Pointer to the instance data.
|
---|
134 | */
|
---|
135 | DECLCALLBACKMEMBER(int, pfnDestroy)(PRTRANDINT pThis);
|
---|
136 |
|
---|
137 | /** Union containing the specific state info for each generator. */
|
---|
138 | union
|
---|
139 | {
|
---|
140 | struct RTRandParkMiller
|
---|
141 | {
|
---|
142 | /** The context. */
|
---|
143 | uint32_t u32Ctx;
|
---|
144 | /** The number of single bits used to fill in the 31st bit. */
|
---|
145 | uint32_t u32Bits;
|
---|
146 | /** The number bits in u32Bits. */
|
---|
147 | uint32_t cBits;
|
---|
148 | } ParkMiller;
|
---|
149 |
|
---|
150 | struct RTRandFile
|
---|
151 | {
|
---|
152 | /** The file handle. */
|
---|
153 | RTFILE hFile;
|
---|
154 | } File;
|
---|
155 | } u;
|
---|
156 | } RTRANDINT;
|
---|
157 |
|
---|
158 |
|
---|
159 | RT_C_DECLS_BEGIN
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Initialize OS facilities for generating random bytes.
|
---|
163 | */
|
---|
164 | void rtRandLazyInitNative(void);
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Generate random bytes using OS facilities.
|
---|
168 | *
|
---|
169 | * @returns VINF_SUCCESS on success, some error status code on failure.
|
---|
170 | * @param pv Where to store the random bytes.
|
---|
171 | * @param cb How many random bytes to store.
|
---|
172 | */
|
---|
173 | int rtRandGenBytesNative(void *pv, size_t cb);
|
---|
174 |
|
---|
175 | void rtRandGenBytesFallback(void *pv, size_t cb) RT_NO_THROW;
|
---|
176 |
|
---|
177 | DECLCALLBACK(void) rtRandAdvSynthesizeBytesFromU32(PRTRANDINT pThis, uint8_t *pb, size_t cb);
|
---|
178 | DECLCALLBACK(void) rtRandAdvSynthesizeBytesFromU64(PRTRANDINT pThis, uint8_t *pb, size_t cb);
|
---|
179 | DECLCALLBACK(uint32_t) rtRandAdvSynthesizeU32FromBytes(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last);
|
---|
180 | DECLCALLBACK(uint32_t) rtRandAdvSynthesizeU32FromU64(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last);
|
---|
181 | DECLCALLBACK(uint64_t) rtRandAdvSynthesizeU64FromBytes(PRTRANDINT pThis, uint64_t u64First, uint64_t u64Last);
|
---|
182 | DECLCALLBACK(uint64_t) rtRandAdvSynthesizeU64FromU32(PRTRANDINT pThis, uint64_t u64First, uint64_t u64Last);
|
---|
183 | DECLCALLBACK(int) rtRandAdvStubSeed(PRTRANDINT pThis, uint64_t u64Seed);
|
---|
184 | DECLCALLBACK(int) rtRandAdvStubSaveState(PRTRANDINT pThis, char *pszState, size_t *pcbState);
|
---|
185 | DECLCALLBACK(int) rtRandAdvStubRestoreState(PRTRANDINT pThis, char const *pszState);
|
---|
186 | DECLCALLBACK(int) rtRandAdvDefaultDestroy(PRTRANDINT pThis);
|
---|
187 |
|
---|
188 | RT_C_DECLS_END
|
---|
189 |
|
---|
190 | #endif
|
---|
191 |
|
---|