2020-05-27 10:25:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-27 10:25:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Platform.h>
|
2023-04-02 17:08:43 +00:00
|
|
|
#include <AK/Span.h>
|
2022-12-09 13:46:13 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-05-27 10:25:46 +00:00
|
|
|
#include <AK/Types.h>
|
2023-04-02 16:33:15 +00:00
|
|
|
#include <stdlib.h>
|
2020-05-27 10:25:46 +00:00
|
|
|
|
2020-10-17 20:25:13 +00:00
|
|
|
#if defined(__unix__)
|
2020-05-27 10:25:46 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2023-04-02 17:08:43 +00:00
|
|
|
inline void fill_with_random([[maybe_unused]] Bytes bytes)
|
2020-05-27 10:25:46 +00:00
|
|
|
{
|
2023-08-27 18:41:43 +00:00
|
|
|
#if defined(AK_OS_SERENITY) || defined(AK_OS_ANDROID) || defined(AK_OS_BSD_GENERIC) || defined(AK_OS_HAIKU) || AK_LIBC_GLIBC_PREREQ(2, 36)
|
2023-04-02 17:08:43 +00:00
|
|
|
arc4random_buf(bytes.data(), bytes.size());
|
2020-11-27 22:57:02 +00:00
|
|
|
#elif defined(OSS_FUZZ)
|
2022-09-20 07:15:12 +00:00
|
|
|
#else
|
2023-04-02 16:33:15 +00:00
|
|
|
auto fill_with_random_fallback = [&]() {
|
2023-04-02 17:08:43 +00:00
|
|
|
for (auto& byte : bytes)
|
|
|
|
byte = rand();
|
2023-04-02 16:33:15 +00:00
|
|
|
};
|
|
|
|
|
2023-09-05 21:34:28 +00:00
|
|
|
# if defined(__unix__)
|
2023-04-02 16:33:15 +00:00
|
|
|
// The maximum permitted value for the getentropy length argument.
|
|
|
|
static constexpr size_t getentropy_length_limit = 256;
|
2023-04-02 17:08:43 +00:00
|
|
|
auto iterations = bytes.size() / getentropy_length_limit;
|
2023-04-02 16:33:15 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < iterations; ++i) {
|
2023-04-02 17:08:43 +00:00
|
|
|
if (getentropy(bytes.data(), getentropy_length_limit) != 0) {
|
2023-04-02 16:33:15 +00:00
|
|
|
fill_with_random_fallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-02 17:08:43 +00:00
|
|
|
bytes = bytes.slice(getentropy_length_limit);
|
2022-09-20 07:15:12 +00:00
|
|
|
}
|
2023-04-02 16:33:15 +00:00
|
|
|
|
2023-04-02 17:08:43 +00:00
|
|
|
if (bytes.is_empty() || getentropy(bytes.data(), bytes.size()) == 0)
|
2023-04-02 16:33:15 +00:00
|
|
|
return;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
fill_with_random_fallback();
|
2020-05-27 10:25:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T get_random()
|
|
|
|
{
|
|
|
|
T t;
|
2023-04-02 17:08:43 +00:00
|
|
|
fill_with_random({ &t, sizeof(T) });
|
2020-05-27 10:25:46 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:16:06 +00:00
|
|
|
u32 get_random_uniform(u32 max_bounds);
|
2023-12-30 15:23:59 +00:00
|
|
|
u64 get_random_uniform_64(u64 max_bounds);
|
2021-05-14 15:16:06 +00:00
|
|
|
|
2022-12-09 13:46:13 +00:00
|
|
|
template<typename Collection>
|
|
|
|
inline void shuffle(Collection& collection)
|
|
|
|
{
|
|
|
|
// Fisher-Yates shuffle
|
|
|
|
for (size_t i = collection.size() - 1; i >= 1; --i) {
|
|
|
|
size_t j = get_random_uniform(i + 1);
|
|
|
|
AK::swap(collection[i], collection[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 10:25:46 +00:00
|
|
|
}
|
2020-10-16 19:59:53 +00:00
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-10-16 19:59:53 +00:00
|
|
|
using AK::fill_with_random;
|
|
|
|
using AK::get_random;
|
2021-05-14 15:16:06 +00:00
|
|
|
using AK::get_random_uniform;
|
2022-12-09 13:46:13 +00:00
|
|
|
using AK::shuffle;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|