2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-01-03 12:36:30 +01:00
|
|
|
#include <Kernel/Devices/RandomDevice.h>
|
|
|
|
#include <Kernel/Random.h>
|
2018-10-15 00:44:54 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-02-19 18:41:50 +01:00
|
|
|
UNMAP_AFTER_INIT RandomDevice::RandomDevice()
|
2018-10-30 13:59:29 +01:00
|
|
|
: CharacterDevice(1, 8)
|
2018-10-15 00:44:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-19 18:41:50 +01:00
|
|
|
UNMAP_AFTER_INIT RandomDevice::~RandomDevice()
|
2018-10-15 00:44:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:44:42 +10:00
|
|
|
bool RandomDevice::can_read(const FileDescription&, size_t) const
|
2018-10-25 13:07:59 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-17 13:18:51 +01:00
|
|
|
KResultOr<size_t> RandomDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
|
2018-10-15 00:44:54 +02:00
|
|
|
{
|
2021-05-13 09:08:44 +02:00
|
|
|
return buffer.write_buffered<256>(size, [&](u8* data, size_t data_size) {
|
2020-09-11 21:11:07 -06:00
|
|
|
get_good_random_bytes(data, data_size);
|
2021-05-13 09:08:44 +02:00
|
|
|
return data_size;
|
2020-09-11 21:11:07 -06:00
|
|
|
});
|
2018-10-15 00:44:54 +02:00
|
|
|
}
|
|
|
|
|
2021-03-17 13:18:51 +01:00
|
|
|
KResultOr<size_t> RandomDevice::write(FileDescription&, u64, const UserOrKernelBuffer&, size_t size)
|
2018-10-15 00:44:54 +02:00
|
|
|
{
|
|
|
|
// FIXME: Use input for entropy? I guess that could be a neat feature?
|
2020-08-04 18:02:23 +02:00
|
|
|
return min(static_cast<size_t>(PAGE_SIZE), size);
|
2018-10-15 00:44:54 +02:00
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|