This source file includes following definitions.
- s390_arch_random_generate
- arch_rng_refill_buffer
- s390_arch_random_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 #include <linux/kernel.h>
31 #include <linux/atomic.h>
32 #include <linux/random.h>
33 #include <linux/slab.h>
34 #include <linux/static_key.h>
35 #include <linux/workqueue.h>
36 #include <asm/cpacf.h>
37
38 DEFINE_STATIC_KEY_FALSE(s390_arch_random_available);
39
40 atomic64_t s390_arch_random_counter = ATOMIC64_INIT(0);
41 EXPORT_SYMBOL(s390_arch_random_counter);
42
43 #define ARCH_REFILL_TICKS (HZ/2)
44 #define ARCH_PRNG_SEED_SIZE 32
45 #define ARCH_RNG_BUF_SIZE 2048
46
47 static DEFINE_SPINLOCK(arch_rng_lock);
48 static u8 *arch_rng_buf;
49 static unsigned int arch_rng_buf_idx;
50
51 static void arch_rng_refill_buffer(struct work_struct *);
52 static DECLARE_DELAYED_WORK(arch_rng_work, arch_rng_refill_buffer);
53
54 bool s390_arch_random_generate(u8 *buf, unsigned int nbytes)
55 {
56
57 if (!spin_trylock(&arch_rng_lock))
58 return false;
59
60
61 arch_rng_buf_idx -= nbytes;
62 if (arch_rng_buf_idx < ARCH_RNG_BUF_SIZE) {
63 memcpy(buf, arch_rng_buf + arch_rng_buf_idx, nbytes);
64 atomic64_add(nbytes, &s390_arch_random_counter);
65 spin_unlock(&arch_rng_lock);
66 return true;
67 }
68
69
70 spin_unlock(&arch_rng_lock);
71
72 return false;
73 }
74 EXPORT_SYMBOL(s390_arch_random_generate);
75
76 static void arch_rng_refill_buffer(struct work_struct *unused)
77 {
78 unsigned int delay = ARCH_REFILL_TICKS;
79
80 spin_lock(&arch_rng_lock);
81 if (arch_rng_buf_idx > ARCH_RNG_BUF_SIZE) {
82
83 u8 seed[ARCH_PRNG_SEED_SIZE];
84 u8 prng_wa[240];
85
86 cpacf_trng(NULL, 0, seed, sizeof(seed));
87
88 memset(prng_wa, 0, sizeof(prng_wa));
89 cpacf_prno(CPACF_PRNO_SHA512_DRNG_SEED,
90 &prng_wa, NULL, 0, seed, sizeof(seed));
91 cpacf_prno(CPACF_PRNO_SHA512_DRNG_GEN,
92 &prng_wa, arch_rng_buf, ARCH_RNG_BUF_SIZE, NULL, 0);
93 arch_rng_buf_idx = ARCH_RNG_BUF_SIZE;
94 }
95 delay += (ARCH_REFILL_TICKS * arch_rng_buf_idx) / ARCH_RNG_BUF_SIZE;
96 spin_unlock(&arch_rng_lock);
97
98
99 queue_delayed_work(system_long_wq, &arch_rng_work, delay);
100 }
101
102 static int __init s390_arch_random_init(void)
103 {
104
105 if (cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG) &&
106 cpacf_query_func(CPACF_PRNO, CPACF_PRNO_SHA512_DRNG_GEN)) {
107
108
109 arch_rng_buf = kmalloc(ARCH_RNG_BUF_SIZE, GFP_KERNEL);
110 if (!arch_rng_buf)
111 return -ENOMEM;
112
113
114 queue_delayed_work(system_long_wq,
115 &arch_rng_work, ARCH_REFILL_TICKS);
116
117
118 static_branch_enable(&s390_arch_random_available);
119 }
120
121 return 0;
122 }
123 arch_initcall(s390_arch_random_init);