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