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
|
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022-2026 The TokTok team.
*/
#ifndef C_TOXCORE_TOXCORE_RNG_H
#define C_TOXCORE_TOXCORE_RNG_H
#include <stdint.h>
#include "attributes.h"
#ifdef __cplusplus
extern "C" {
#endif
/** @brief Fill a byte array with random bytes.
*
* This is the key generator callback and as such must be a cryptographically
* secure pseudo-random number generator (CSPRNG). The security of Tox heavily
* depends on the security of this RNG.
*/
typedef void random_bytes_cb(void *_Nullable self, uint8_t *_Nonnull bytes, uint32_t length);
/** @brief Generate a random integer between 0 and @p upper_bound.
*
* Should produce a uniform random distribution, but Tox security does not
* depend on this being correct. In principle, it could even be a non-CSPRNG.
*/
typedef uint32_t random_uniform_cb(void *_Nullable self, uint32_t upper_bound);
/** @brief Virtual function table for Random. */
typedef struct Random_Funcs {
random_bytes_cb *_Nonnull bytes_callback;
random_uniform_cb *_Nonnull uniform_callback;
} Random_Funcs;
/** @brief Random number generator object.
*
* Can be used by test code and fuzzers to make toxcore behave in specific
* well-defined (non-random) ways. Production code ought to use libsodium's
* CSPRNG and use `os_random` below.
*/
typedef struct Random {
const Random_Funcs *_Nonnull funcs;
void *_Nullable user_data;
} Random;
void rng_bytes(const Random *_Nonnull rng, uint8_t *_Nonnull bytes, uint32_t length);
uint32_t rng_uniform(const Random *_Nonnull rng, uint32_t upper_bound);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* C_TOXCORE_TOXCORE_RNG_H */
|