Random.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <AK/Assertions.h>
  29. #include <AK/ByteBuffer.h>
  30. #include <AK/Types.h>
  31. #include <Kernel/Arch/i386/CPU.h>
  32. #include <Kernel/Lock.h>
  33. #include <Kernel/StdLib.h>
  34. #include <LibCrypto/Cipher/AES.h>
  35. #include <LibCrypto/Cipher/Cipher.h>
  36. #include <LibCrypto/Hash/SHA2.h>
  37. namespace Kernel {
  38. template<typename CipherT, typename HashT, int KeySize>
  39. class FortunaPRNG {
  40. public:
  41. constexpr static size_t pool_count = 32;
  42. constexpr static size_t reseed_threshold = 16;
  43. using CipherType = CipherT;
  44. using BlockType = CipherT::BlockType;
  45. using HashType = HashT;
  46. using DigestType = HashT::DigestType;
  47. FortunaPRNG()
  48. : m_counter(ByteBuffer::create_zeroed(BlockType::block_size()))
  49. {
  50. }
  51. void get_random_bytes(u8* buffer, size_t n)
  52. {
  53. if (m_p0_len >= reseed_threshold) {
  54. this->reseed();
  55. }
  56. ASSERT(is_seeded());
  57. // FIXME: More than 2^20 bytes cannot be generated without refreshing the key.
  58. ASSERT(n < (1 << 20));
  59. typename CipherType::CTRMode cipher(m_key, KeySize);
  60. auto wrapped_buffer = ByteBuffer::wrap(buffer, n);
  61. m_counter = cipher.key_stream(wrapped_buffer, m_counter).value();
  62. // Extract a new key from the prng stream.
  63. m_counter = cipher.key_stream(m_key, m_counter).value();
  64. }
  65. template<typename T>
  66. void add_random_event(const T& event_data, size_t pool)
  67. {
  68. pool %= pool_count;
  69. if (pool == 0) {
  70. m_p0_len++;
  71. }
  72. m_pools[pool].update(reinterpret_cast<const u8*>(&event_data), sizeof(T));
  73. }
  74. bool is_seeded() const
  75. {
  76. return m_reseed_number > 0;
  77. }
  78. bool is_ready() const
  79. {
  80. return is_seeded() || m_p0_len >= reseed_threshold;
  81. }
  82. private:
  83. void reseed()
  84. {
  85. HashType new_key;
  86. new_key.update(m_key);
  87. for (size_t i = 0; i < pool_count; ++i) {
  88. if (m_reseed_number % (1 << i) == 0) {
  89. DigestType digest = m_pools[i].digest();
  90. new_key.update(digest.immutable_data(), digest.data_length());
  91. }
  92. }
  93. DigestType digest = new_key.digest();
  94. m_key = ByteBuffer::copy(digest.immutable_data(),
  95. digest.data_length());
  96. m_reseed_number++;
  97. m_p0_len = 0;
  98. }
  99. ByteBuffer m_counter;
  100. size_t m_reseed_number { 0 };
  101. size_t m_p0_len { 0 };
  102. ByteBuffer m_key;
  103. HashType m_pools[pool_count];
  104. };
  105. class KernelRng : public Lockable<FortunaPRNG<Crypto::Cipher::AESCipher, Crypto::Hash::SHA256, 256>> {
  106. AK_MAKE_ETERNAL;
  107. public:
  108. static KernelRng& the();
  109. void wait_for_entropy();
  110. void wake_if_ready();
  111. private:
  112. KernelRng();
  113. WaitQueue m_seed_queue;
  114. };
  115. class EntropySource {
  116. template<typename T>
  117. struct Event {
  118. u64 timestamp;
  119. size_t source;
  120. T event_data;
  121. };
  122. public:
  123. EntropySource()
  124. : m_source(next_source++)
  125. {
  126. }
  127. template<typename T>
  128. void add_random_event(const T& event_data)
  129. {
  130. // We don't lock this because on the off chance a pool is corrupted, entropy isn't lost.
  131. Event<T> event = { read_tsc(), m_source, event_data };
  132. KernelRng::the().resource().add_random_event(event, m_pool);
  133. m_pool++;
  134. KernelRng::the().wake_if_ready();
  135. }
  136. private:
  137. static size_t next_source;
  138. size_t m_pool { 0 };
  139. size_t m_source;
  140. Lock m_lock;
  141. };
  142. // NOTE: These API's are primarily about expressing intent/needs in the calling code.
  143. // The only difference is that get_fast_random is guaranteed not to block.
  144. void get_fast_random_bytes(u8*, size_t);
  145. void get_good_random_bytes(u8*, size_t);
  146. template<typename T>
  147. inline T get_fast_random()
  148. {
  149. T value;
  150. get_fast_random_bytes(reinterpret_cast<u8*>(&value), sizeof(T));
  151. return value;
  152. }
  153. template<typename T>
  154. inline T get_good_random()
  155. {
  156. T value;
  157. get_good_random_bytes(reinterpret_cast<u8*>(&value), sizeof(T));
  158. return value;
  159. }
  160. }