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