Random.cpp 3.7 KB

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