arch_random.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * s390 arch random implementation.
  4. *
  5. * Copyright IBM Corp. 2017, 2018
  6. * Author(s): Harald Freudenberger
  7. *
  8. * The s390_arch_random_generate() function may be called from random.c
  9. * in interrupt context. So this implementation does the best to be very
  10. * fast. There is a buffer of random data which is asynchronously checked
  11. * and filled by a workqueue thread.
  12. * If there are enough bytes in the buffer the s390_arch_random_generate()
  13. * just delivers these bytes. Otherwise false is returned until the
  14. * worker thread refills the buffer.
  15. * The worker fills the rng buffer by pulling fresh entropy from the
  16. * high quality (but slow) true hardware random generator. This entropy
  17. * is then spread over the buffer with an pseudo random generator PRNG.
  18. * As the arch_get_random_seed_long() fetches 8 bytes and the calling
  19. * function add_interrupt_randomness() counts this as 1 bit entropy the
  20. * distribution needs to make sure there is in fact 1 bit entropy contained
  21. * in 8 bytes of the buffer. The current values pull 32 byte entropy
  22. * and scatter this into a 2048 byte buffer. So 8 byte in the buffer
  23. * will contain 1 bit of entropy.
  24. * The worker thread is rescheduled based on the charge level of the
  25. * buffer but at least with 500 ms delay to avoid too much CPU consumption.
  26. * So the max. amount of rng data delivered via arch_get_random_seed is
  27. * limited to 4k bytes per second.
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/atomic.h>
  31. #include <linux/random.h>
  32. #include <linux/slab.h>
  33. #include <linux/static_key.h>
  34. #include <linux/workqueue.h>
  35. #include <asm/cpacf.h>
  36. DEFINE_STATIC_KEY_FALSE(s390_arch_random_available);
  37. atomic64_t s390_arch_random_counter = ATOMIC64_INIT(0);
  38. EXPORT_SYMBOL(s390_arch_random_counter);
  39. #define ARCH_REFILL_TICKS (HZ/2)
  40. #define ARCH_PRNG_SEED_SIZE 32
  41. #define ARCH_RNG_BUF_SIZE 2048
  42. static DEFINE_SPINLOCK(arch_rng_lock);
  43. static u8 *arch_rng_buf;
  44. static unsigned int arch_rng_buf_idx;
  45. static void arch_rng_refill_buffer(struct work_struct *);
  46. static DECLARE_DELAYED_WORK(arch_rng_work, arch_rng_refill_buffer);
  47. bool s390_arch_random_generate(u8 *buf, unsigned int nbytes)
  48. {
  49. /* lock rng buffer */
  50. if (!spin_trylock(&arch_rng_lock))
  51. return false;
  52. /* try to resolve the requested amount of bytes from the buffer */
  53. arch_rng_buf_idx -= nbytes;
  54. if (arch_rng_buf_idx < ARCH_RNG_BUF_SIZE) {
  55. memcpy(buf, arch_rng_buf + arch_rng_buf_idx, nbytes);
  56. atomic64_add(nbytes, &s390_arch_random_counter);
  57. spin_unlock(&arch_rng_lock);
  58. return true;
  59. }
  60. /* not enough bytes in rng buffer, refill is done asynchronously */
  61. spin_unlock(&arch_rng_lock);
  62. return false;
  63. }
  64. EXPORT_SYMBOL(s390_arch_random_generate);
  65. static void arch_rng_refill_buffer(struct work_struct *unused)
  66. {
  67. unsigned int delay = ARCH_REFILL_TICKS;
  68. spin_lock(&arch_rng_lock);
  69. if (arch_rng_buf_idx > ARCH_RNG_BUF_SIZE) {
  70. /* buffer is exhausted and needs refill */
  71. u8 seed[ARCH_PRNG_SEED_SIZE];
  72. u8 prng_wa[240];
  73. /* fetch ARCH_PRNG_SEED_SIZE bytes of entropy */
  74. cpacf_trng(NULL, 0, seed, sizeof(seed));
  75. /* blow this entropy up to ARCH_RNG_BUF_SIZE with PRNG */
  76. memset(prng_wa, 0, sizeof(prng_wa));
  77. cpacf_prno(CPACF_PRNO_SHA512_DRNG_SEED,
  78. &prng_wa, NULL, 0, seed, sizeof(seed));
  79. cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN,
  80. &prng_wa, arch_rng_buf, ARCH_RNG_BUF_SIZE, NULL, 0);
  81. arch_rng_buf_idx = ARCH_RNG_BUF_SIZE;
  82. }
  83. delay += (ARCH_REFILL_TICKS * arch_rng_buf_idx) / ARCH_RNG_BUF_SIZE;
  84. spin_unlock(&arch_rng_lock);
  85. /* kick next check */
  86. queue_delayed_work(system_long_wq, &arch_rng_work, delay);
  87. }
  88. static int __init s390_arch_random_init(void)
  89. {
  90. /* all the needed PRNO subfunctions available ? */
  91. if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG) &&
  92. cpacf_query_func(CPACF_PRNO, CPACF_PRNO_SHA512_DRNG_GEN)) {
  93. /* alloc arch random working buffer */
  94. arch_rng_buf = kmalloc(ARCH_RNG_BUF_SIZE, GFP_KERNEL);
  95. if (!arch_rng_buf)
  96. return -ENOMEM;
  97. /* kick worker queue job to fill the random buffer */
  98. queue_delayed_work(system_long_wq,
  99. &arch_rng_work, ARCH_REFILL_TICKS);
  100. /* enable arch random to the outside world */
  101. static_branch_enable(&s390_arch_random_available);
  102. }
  103. return 0;
  104. }
  105. arch_initcall(s390_arch_random_init);