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