mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 10:01:13 +09:00
Kernel: Replace existing random implementation with Fortuna
This commit is contained in:
parent
f2d51f13a6
commit
0f32155fa4
Notes:
sideshowbarker
2024-07-19 05:22:55 +09:00
Author: https://github.com/petelliott
Commit: 0f32155fa4
Pull-request: https://github.com/SerenityOS/serenity/pull/2613
Reviewed-by: https://github.com/alimpfard ✅
3 changed files with 49 additions and 30 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -30,37 +31,35 @@
|
|||
|
||||
namespace Kernel {
|
||||
|
||||
static u32 random32()
|
||||
static KernelRng* s_the;
|
||||
|
||||
KernelRng& KernelRng::the()
|
||||
{
|
||||
if (!s_the) {
|
||||
s_the = new KernelRng;
|
||||
}
|
||||
return *s_the;
|
||||
}
|
||||
|
||||
KernelRng::KernelRng()
|
||||
{
|
||||
if (g_cpu_supports_rdrand) {
|
||||
u32 value = 0;
|
||||
asm volatile(
|
||||
"1:\n"
|
||||
"rdrand %0\n"
|
||||
"jnc 1b\n"
|
||||
: "=r"(value));
|
||||
return value;
|
||||
for (size_t i = 0; i < KernelRng::pool_count * KernelRng::reseed_threshold; ++i) {
|
||||
u32 value = 0;
|
||||
asm volatile(
|
||||
"1:\n"
|
||||
"rdseed %0\n"
|
||||
"jnc 1b\n"
|
||||
: "=r"(value));
|
||||
|
||||
this->add_random_event(value, i % 32);
|
||||
}
|
||||
}
|
||||
// FIXME: This sucks lol
|
||||
static u32 next = 1;
|
||||
next = next * 1103515245 + 12345;
|
||||
return next;
|
||||
}
|
||||
|
||||
void get_good_random_bytes(u8* buffer, size_t buffer_size)
|
||||
{
|
||||
union {
|
||||
u8 bytes[4];
|
||||
u32 value;
|
||||
} u;
|
||||
size_t offset = 4;
|
||||
for (size_t i = 0; i < buffer_size; ++i) {
|
||||
if (offset >= 4) {
|
||||
u.value = random32();
|
||||
offset = 0;
|
||||
}
|
||||
buffer[i] = u.bytes[offset++];
|
||||
}
|
||||
KernelRng::the().get_random_bytes(buffer, buffer_size);
|
||||
}
|
||||
|
||||
void get_fast_random_bytes(u8* buffer, size_t buffer_size)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue