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
31
32
33
34
35
36
37
38
39
40#include <linux/module.h>
41#include <linux/slab.h>
42#include <linux/fips.h>
43#include <linux/time.h>
44#include <linux/crypto.h>
45#include <crypto/internal/rng.h>
46
47#include "jitterentropy.h"
48
49
50
51
52
53void *jent_zalloc(unsigned int len)
54{
55 return kzalloc(len, GFP_KERNEL);
56}
57
58void jent_zfree(void *ptr)
59{
60 kfree_sensitive(ptr);
61}
62
63int jent_fips_enabled(void)
64{
65 return fips_enabled;
66}
67
68void jent_panic(char *s)
69{
70 panic("%s", s);
71}
72
73void jent_memcpy(void *dest, const void *src, unsigned int n)
74{
75 memcpy(dest, src, n);
76}
77
78
79
80
81
82
83
84
85
86
87void jent_get_nstime(__u64 *out)
88{
89 __u64 tmp = 0;
90
91 tmp = random_get_entropy();
92
93
94
95
96
97
98 if (tmp == 0)
99 tmp = ktime_get_ns();
100
101 *out = tmp;
102}
103
104
105
106
107
108struct jitterentropy {
109 spinlock_t jent_lock;
110 struct rand_data *entropy_collector;
111 unsigned int reset_cnt;
112};
113
114static int jent_kcapi_init(struct crypto_tfm *tfm)
115{
116 struct jitterentropy *rng = crypto_tfm_ctx(tfm);
117 int ret = 0;
118
119 rng->entropy_collector = jent_entropy_collector_alloc(1, 0);
120 if (!rng->entropy_collector)
121 ret = -ENOMEM;
122
123 spin_lock_init(&rng->jent_lock);
124 return ret;
125}
126
127static void jent_kcapi_cleanup(struct crypto_tfm *tfm)
128{
129 struct jitterentropy *rng = crypto_tfm_ctx(tfm);
130
131 spin_lock(&rng->jent_lock);
132 if (rng->entropy_collector)
133 jent_entropy_collector_free(rng->entropy_collector);
134 rng->entropy_collector = NULL;
135 spin_unlock(&rng->jent_lock);
136}
137
138static int jent_kcapi_random(struct crypto_rng *tfm,
139 const u8 *src, unsigned int slen,
140 u8 *rdata, unsigned int dlen)
141{
142 struct jitterentropy *rng = crypto_rng_ctx(tfm);
143 int ret = 0;
144
145 spin_lock(&rng->jent_lock);
146
147
148 if (rng->reset_cnt > (1<<10)) {
149 ret = -EFAULT;
150 goto out;
151 }
152
153 ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
154
155
156 if (ret < -1) {
157 pr_warn_ratelimited("Reset Jitter RNG due to health test failure: %s failure\n",
158 (ret == -2) ? "Repetition Count Test" :
159 "Adaptive Proportion Test");
160
161 rng->reset_cnt++;
162
163 ret = -EAGAIN;
164 } else {
165 rng->reset_cnt = 0;
166
167
168 if (ret == -1)
169 ret = -EINVAL;
170 }
171
172out:
173 spin_unlock(&rng->jent_lock);
174
175 return ret;
176}
177
178static int jent_kcapi_reset(struct crypto_rng *tfm,
179 const u8 *seed, unsigned int slen)
180{
181 return 0;
182}
183
184static struct rng_alg jent_alg = {
185 .generate = jent_kcapi_random,
186 .seed = jent_kcapi_reset,
187 .seedsize = 0,
188 .base = {
189 .cra_name = "jitterentropy_rng",
190 .cra_driver_name = "jitterentropy_rng",
191 .cra_priority = 100,
192 .cra_ctxsize = sizeof(struct jitterentropy),
193 .cra_module = THIS_MODULE,
194 .cra_init = jent_kcapi_init,
195 .cra_exit = jent_kcapi_cleanup,
196
197 }
198};
199
200static int __init jent_mod_init(void)
201{
202 int ret = 0;
203
204 ret = jent_entropy_init();
205 if (ret) {
206 pr_info("jitterentropy: Initialization failed with host not compliant with requirements: %d\n", ret);
207 return -EFAULT;
208 }
209 return crypto_register_rng(&jent_alg);
210}
211
212static void __exit jent_mod_exit(void)
213{
214 crypto_unregister_rng(&jent_alg);
215}
216
217module_init(jent_mod_init);
218module_exit(jent_mod_exit);
219
220MODULE_LICENSE("Dual BSD/GPL");
221MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
222MODULE_DESCRIPTION("Non-physical True Random Number Generator based on CPU Jitter");
223MODULE_ALIAS_CRYPTO("jitterentropy_rng");
224