2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2020-06-23 05:23:35 +03:00
|
|
|
* Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-01-03 14:36:30 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-23 02:10:45 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/ByteBuffer.h>
|
2020-01-03 14:36:30 +03:00
|
|
|
#include <AK/Types.h>
|
2021-03-07 23:28:28 +03:00
|
|
|
#include <Kernel/Arch/x86/CPU.h>
|
2020-06-24 23:07:28 +03:00
|
|
|
#include <Kernel/Lock.h>
|
2020-06-23 02:10:45 +03:00
|
|
|
#include <Kernel/StdLib.h>
|
2020-06-23 05:23:35 +03:00
|
|
|
#include <LibCrypto/Cipher/AES.h>
|
2020-06-23 23:05:26 +03:00
|
|
|
#include <LibCrypto/Cipher/Cipher.h>
|
2020-06-23 05:23:35 +03:00
|
|
|
#include <LibCrypto/Hash/SHA2.h>
|
2020-01-03 14:36:30 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-06-23 02:10:45 +03:00
|
|
|
template<typename CipherT, typename HashT, int KeySize>
|
|
|
|
class FortunaPRNG {
|
2020-06-23 05:23:35 +03:00
|
|
|
public:
|
2020-06-23 02:10:45 +03:00
|
|
|
constexpr static size_t pool_count = 32;
|
|
|
|
constexpr static size_t reseed_threshold = 16;
|
|
|
|
|
|
|
|
using CipherType = CipherT;
|
2021-02-28 04:31:17 +03:00
|
|
|
using BlockType = typename CipherT::BlockType;
|
2020-06-23 02:10:45 +03:00
|
|
|
using HashType = HashT;
|
2021-02-28 04:31:17 +03:00
|
|
|
using DigestType = typename HashT::DigestType;
|
2020-06-23 02:10:45 +03:00
|
|
|
|
2020-06-23 23:05:26 +03:00
|
|
|
FortunaPRNG()
|
|
|
|
: m_counter(ByteBuffer::create_zeroed(BlockType::block_size()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-27 23:16:30 +03:00
|
|
|
bool get_random_bytes(u8* buffer, size_t n)
|
2020-06-23 02:10:45 +03:00
|
|
|
{
|
2021-01-27 23:16:30 +03:00
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
if (!is_ready())
|
|
|
|
return false;
|
2020-06-23 02:10:45 +03:00
|
|
|
if (m_p0_len >= reseed_threshold) {
|
|
|
|
this->reseed();
|
|
|
|
}
|
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_seeded());
|
2020-06-23 02:10:45 +03:00
|
|
|
|
|
|
|
// FIXME: More than 2^20 bytes cannot be generated without refreshing the key.
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(n < (1 << 20));
|
2020-06-23 02:10:45 +03:00
|
|
|
|
2020-11-27 23:13:55 +03:00
|
|
|
typename CipherType::CTRMode cipher(m_key, KeySize, Crypto::Cipher::Intent::Encryption);
|
2020-06-23 02:10:45 +03:00
|
|
|
|
2020-08-11 22:00:49 +03:00
|
|
|
Bytes buffer_span { buffer, n };
|
2020-08-15 19:38:24 +03:00
|
|
|
auto counter_span = m_counter.bytes();
|
2020-08-11 22:00:49 +03:00
|
|
|
cipher.key_stream(buffer_span, counter_span, &counter_span);
|
2020-06-23 02:10:45 +03:00
|
|
|
|
|
|
|
// Extract a new key from the prng stream.
|
2020-08-15 19:38:24 +03:00
|
|
|
Bytes key_span = m_key.bytes();
|
2020-08-13 19:53:26 +03:00
|
|
|
cipher.key_stream(key_span, counter_span, &counter_span);
|
2021-01-27 23:16:30 +03:00
|
|
|
return true;
|
2020-06-23 02:10:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void add_random_event(const T& event_data, size_t pool)
|
|
|
|
{
|
|
|
|
pool %= pool_count;
|
|
|
|
if (pool == 0) {
|
|
|
|
m_p0_len++;
|
|
|
|
}
|
2020-06-23 05:23:35 +03:00
|
|
|
m_pools[pool].update(reinterpret_cast<const u8*>(&event_data), sizeof(T));
|
2020-06-23 02:10:45 +03:00
|
|
|
}
|
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool is_seeded() const
|
2020-06-27 20:10:01 +03:00
|
|
|
{
|
|
|
|
return m_reseed_number > 0;
|
|
|
|
}
|
|
|
|
|
2020-12-26 12:47:08 +03:00
|
|
|
[[nodiscard]] bool is_ready() const
|
2020-06-27 20:10:01 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_lock.is_locked());
|
2020-06-27 20:10:01 +03:00
|
|
|
return is_seeded() || m_p0_len >= reseed_threshold;
|
|
|
|
}
|
|
|
|
|
2021-01-27 23:16:30 +03:00
|
|
|
SpinLock<u8>& get_lock() { return m_lock; }
|
|
|
|
|
2020-06-23 02:10:45 +03:00
|
|
|
private:
|
|
|
|
void reseed()
|
|
|
|
{
|
|
|
|
HashType new_key;
|
|
|
|
new_key.update(m_key);
|
|
|
|
for (size_t i = 0; i < pool_count; ++i) {
|
2021-02-05 22:04:19 +03:00
|
|
|
if (m_reseed_number % (1u << i) == 0) {
|
2020-06-23 02:10:45 +03:00
|
|
|
DigestType digest = m_pools[i].digest();
|
|
|
|
new_key.update(digest.immutable_data(), digest.data_length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DigestType digest = new_key.digest();
|
|
|
|
m_key = ByteBuffer::copy(digest.immutable_data(),
|
|
|
|
digest.data_length());
|
|
|
|
|
|
|
|
m_reseed_number++;
|
|
|
|
m_p0_len = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:05:26 +03:00
|
|
|
ByteBuffer m_counter;
|
2020-06-23 02:10:45 +03:00
|
|
|
size_t m_reseed_number { 0 };
|
|
|
|
size_t m_p0_len { 0 };
|
|
|
|
ByteBuffer m_key;
|
|
|
|
HashType m_pools[pool_count];
|
2021-01-27 23:16:30 +03:00
|
|
|
SpinLock<u8> m_lock;
|
2020-06-23 02:10:45 +03:00
|
|
|
};
|
|
|
|
|
2020-06-24 23:07:28 +03:00
|
|
|
class KernelRng : public Lockable<FortunaPRNG<Crypto::Cipher::AESCipher, Crypto::Hash::SHA256, 256>> {
|
2020-06-23 05:23:35 +03:00
|
|
|
AK_MAKE_ETERNAL;
|
2020-06-23 23:05:26 +03:00
|
|
|
|
2020-06-23 05:23:35 +03:00
|
|
|
public:
|
2020-08-25 04:35:19 +03:00
|
|
|
KernelRng();
|
2020-06-23 05:23:35 +03:00
|
|
|
static KernelRng& the();
|
|
|
|
|
2020-06-27 20:10:01 +03:00
|
|
|
void wait_for_entropy();
|
|
|
|
|
|
|
|
void wake_if_ready();
|
|
|
|
|
2021-01-27 23:16:30 +03:00
|
|
|
SpinLock<u8>& get_lock() { return resource().get_lock(); }
|
|
|
|
|
2020-06-23 05:23:35 +03:00
|
|
|
private:
|
2020-06-27 20:10:01 +03:00
|
|
|
WaitQueue m_seed_queue;
|
2020-06-23 05:23:35 +03:00
|
|
|
};
|
|
|
|
|
2020-06-24 23:07:28 +03:00
|
|
|
class EntropySource {
|
|
|
|
template<typename T>
|
|
|
|
struct Event {
|
|
|
|
u64 timestamp;
|
|
|
|
size_t source;
|
|
|
|
T event_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2021-01-24 20:17:54 +03:00
|
|
|
enum class Static : size_t {
|
|
|
|
Interrupts,
|
|
|
|
MaxHardcodedSourceIndex,
|
|
|
|
};
|
|
|
|
|
2020-06-24 23:07:28 +03:00
|
|
|
EntropySource()
|
|
|
|
: m_source(next_source++)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-24 20:17:54 +03:00
|
|
|
EntropySource(Static hardcoded_source)
|
|
|
|
: m_source(static_cast<size_t>(hardcoded_source))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-24 23:07:28 +03:00
|
|
|
template<typename T>
|
|
|
|
void add_random_event(const T& event_data)
|
|
|
|
{
|
2021-01-27 23:16:30 +03:00
|
|
|
auto& kernel_rng = KernelRng::the();
|
|
|
|
ScopedSpinLock lock(kernel_rng.get_lock());
|
2020-06-24 23:07:28 +03:00
|
|
|
// We don't lock this because on the off chance a pool is corrupted, entropy isn't lost.
|
|
|
|
Event<T> event = { read_tsc(), m_source, event_data };
|
2021-01-27 23:16:30 +03:00
|
|
|
kernel_rng.resource().add_random_event(event, m_pool);
|
2020-06-24 23:07:28 +03:00
|
|
|
m_pool++;
|
2021-01-27 23:16:30 +03:00
|
|
|
kernel_rng.wake_if_ready();
|
2020-06-24 23:07:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static size_t next_source;
|
|
|
|
size_t m_pool { 0 };
|
|
|
|
size_t m_source;
|
|
|
|
};
|
|
|
|
|
2020-01-03 14:36:30 +03:00
|
|
|
// NOTE: These API's are primarily about expressing intent/needs in the calling code.
|
2020-06-27 20:10:01 +03:00
|
|
|
// The only difference is that get_fast_random is guaranteed not to block.
|
2020-01-03 14:36:30 +03:00
|
|
|
|
|
|
|
void get_fast_random_bytes(u8*, size_t);
|
2021-01-27 23:16:30 +03:00
|
|
|
bool get_good_random_bytes(u8*, size_t, bool allow_wait = true, bool fallback_to_fast = true);
|
2020-01-03 14:36:30 +03:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T get_fast_random()
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
get_fast_random_bytes(reinterpret_cast<u8*>(&value), sizeof(T));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T get_good_random()
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
get_good_random_bytes(reinterpret_cast<u8*>(&value), sizeof(T));
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|