[1] | 1 | /* $Id: rand.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Internal RTRand header
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
[76559] | 37 | #ifndef IPRT_INCLUDED_INTERNAL_rand_h
|
---|
| 38 | #define IPRT_INCLUDED_INTERNAL_rand_h
|
---|
[76513] | 39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 40 | # pragma once
|
---|
| 41 | #endif
|
---|
[1] | 42 |
|
---|
| 43 | #include <iprt/types.h>
|
---|
[11347] | 44 | #include <iprt/critsect.h>
|
---|
[1] | 45 |
|
---|
[11347] | 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 | */
|
---|
[85121] | 70 | DECLCALLBACKMEMBER(void , pfnGetBytes,(PRTRANDINT pThis, uint8_t *pb, size_t cb));
|
---|
[11347] | 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 | */
|
---|
[85121] | 80 | DECLCALLBACKMEMBER(uint32_t, pfnGetU32,(PRTRANDINT pThis, uint32_t u32First, uint32_t u32Last));
|
---|
[11347] | 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 | */
|
---|
[85121] | 90 | DECLCALLBACKMEMBER(uint64_t, pfnGetU64,(PRTRANDINT pThis, uint64_t u64First, uint64_t u64Last));
|
---|
[11347] | 91 |
|
---|
| 92 | /**
|
---|
| 93 | * Generic seeding.
|
---|
| 94 | *
|
---|
| 95 | * @returns IPRT status code.
|
---|
[11523] | 96 | * @retval VERR_NOT_SUPPORTED if it isn't a pseudo generator.
|
---|
| 97 | *
|
---|
[11347] | 98 | * @param pThis Pointer to the instance data.
|
---|
| 99 | * @param u64Seed The seed.
|
---|
| 100 | */
|
---|
[85121] | 101 | DECLCALLBACKMEMBER(int, pfnSeed,(PRTRANDINT pThis, uint64_t u64Seed));
|
---|
[11347] | 102 |
|
---|
| 103 | /**
|
---|
[11523] | 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 | *
|
---|
[25645] | 116 | * @param pThis Pointer to the instance data.
|
---|
[11523] | 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 | */
|
---|
[85121] | 123 | DECLCALLBACKMEMBER(int, pfnSaveState,(PRTRANDINT pThis, char *pszState, size_t *pcbState));
|
---|
[11523] | 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Restores the state of a pseudo generator.
|
---|
| 127 | *
|
---|
[33540] | 128 | * The state must have been obtained using pfnGetState.
|
---|
[11523] | 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 | *
|
---|
[25645] | 134 | * @param pThis Pointer to the instance data.
|
---|
[11523] | 135 | * @param pszState The state to load.
|
---|
| 136 | */
|
---|
[85121] | 137 | DECLCALLBACKMEMBER(int, pfnRestoreState,(PRTRANDINT pThis, char const *pszState));
|
---|
[11523] | 138 |
|
---|
| 139 | /**
|
---|
[11347] | 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 | */
|
---|
[85121] | 148 | DECLCALLBACKMEMBER(int, pfnDestroy,(PRTRANDINT pThis));
|
---|
[11347] | 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;
|
---|
[11523] | 162 |
|
---|
| 163 | struct RTRandFile
|
---|
| 164 | {
|
---|
[37596] | 165 | /** The file handle (native). */
|
---|
| 166 | intptr_t hFile;
|
---|
[11523] | 167 | } File;
|
---|
[11347] | 168 | } u;
|
---|
| 169 | } RTRANDINT;
|
---|
| 170 |
|
---|
| 171 |
|
---|
[20374] | 172 | RT_C_DECLS_BEGIN
|
---|
[1] | 173 |
|
---|
[85126] | 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);
|
---|
[1] | 184 |
|
---|
[20374] | 185 | RT_C_DECLS_END
|
---|
[1] | 186 |
|
---|
[76585] | 187 | #endif /* !IPRT_INCLUDED_INTERNAL_rand_h */
|
---|
[11347] | 188 |
|
---|