serenity/Kernel/Devices/RandomDevice.cpp
Andreas Kling 9026598999 Kernel: Add a more expressive API for getting random bytes
We now have these API's in <Kernel/Random.h>:

    - get_fast_random_bytes(u8* buffer, size_t buffer_size)
    - get_good_random_bytes(u8* buffer, size_t buffer_size)
    - get_fast_random<T>()
    - get_good_random<T>()

Internally they both use x86 RDRAND if available, otherwise they fall
back to the same LCG we had in RandomDevice all along.

The main purpose of this patch is to give kernel code a way to better
express its needs for random data.

Randomness is something that will require a lot more work, but this is
hopefully a step in the right direction.
2020-01-03 12:43:07 +01:00

28 lines
563 B
C++

#include <Kernel/Devices/RandomDevice.h>
#include <Kernel/Random.h>
RandomDevice::RandomDevice()
: CharacterDevice(1, 8)
{
}
RandomDevice::~RandomDevice()
{
}
bool RandomDevice::can_read(const FileDescription&) const
{
return true;
}
ssize_t RandomDevice::read(FileDescription&, u8* buffer, ssize_t size)
{
get_good_random_bytes(buffer, size);
return size;
}
ssize_t RandomDevice::write(FileDescription&, const u8*, ssize_t size)
{
// FIXME: Use input for entropy? I guess that could be a neat feature?
return min(PAGE_SIZE, size);
}