Random.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include <AK/Singleton.h>
  28. #include <Kernel/Arch/i386/CPU.h>
  29. #include <Kernel/Devices/RandomDevice.h>
  30. #include <Kernel/Random.h>
  31. #include <Kernel/Time/TimeManagement.h>
  32. namespace Kernel {
  33. static AK::Singleton<KernelRng> s_the;
  34. KernelRng& KernelRng::the()
  35. {
  36. return *s_the;
  37. }
  38. KernelRng::KernelRng()
  39. {
  40. bool supports_rdseed = Processor::current().has_feature(CPUFeature::RDSEED);
  41. bool supports_rdrand = Processor::current().has_feature(CPUFeature::RDRAND);
  42. if (supports_rdseed || supports_rdrand) {
  43. for (size_t i = 0; i < resource().pool_count * resource().reseed_threshold; ++i) {
  44. u32 value = 0;
  45. if (supports_rdseed) {
  46. asm volatile(
  47. "1:\n"
  48. "rdseed %0\n"
  49. "jnc 1b\n"
  50. : "=r"(value));
  51. } else {
  52. asm volatile(
  53. "1:\n"
  54. "rdrand %0\n"
  55. "jnc 1b\n"
  56. : "=r"(value));
  57. }
  58. this->resource().add_random_event(value, i % 32);
  59. }
  60. }
  61. }
  62. void KernelRng::wait_for_entropy()
  63. {
  64. if (!resource().is_ready()) {
  65. dbgln("Entropy starvation...");
  66. m_seed_queue.wait_on({}, "KernelRng");
  67. }
  68. }
  69. void KernelRng::wake_if_ready()
  70. {
  71. if (resource().is_ready()) {
  72. m_seed_queue.wake_all();
  73. }
  74. }
  75. size_t EntropySource::next_source { static_cast<size_t>(EntropySource::Static::MaxHardcodedSourceIndex) };
  76. void get_good_random_bytes(u8* buffer, size_t buffer_size)
  77. {
  78. KernelRng::the().wait_for_entropy();
  79. // FIXME: What if interrupts are disabled because we're in an interrupt?
  80. if (are_interrupts_enabled()) {
  81. LOCKER(KernelRng::the().lock());
  82. KernelRng::the().resource().get_random_bytes(buffer, buffer_size);
  83. } else {
  84. KernelRng::the().resource().get_random_bytes(buffer, buffer_size);
  85. }
  86. }
  87. void get_fast_random_bytes(u8* buffer, size_t buffer_size)
  88. {
  89. if (KernelRng::the().resource().is_ready()) {
  90. return get_good_random_bytes(buffer, buffer_size);
  91. }
  92. static u32 next = 1;
  93. union {
  94. u8 bytes[4];
  95. u32 value;
  96. } u;
  97. size_t offset = 4;
  98. for (size_t i = 0; i < buffer_size; ++i) {
  99. if (offset >= 4) {
  100. next = next * 1103515245 + 12345;
  101. u.value = next;
  102. offset = 0;
  103. }
  104. buffer[i] = u.bytes[offset++];
  105. }
  106. }
  107. }