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