hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/crypto/jitterentropy-kcapi.c
....@@ -37,29 +37,18 @@
3737 * DAMAGE.
3838 */
3939
40
+#include <linux/kernel.h>
4041 #include <linux/module.h>
4142 #include <linux/slab.h>
4243 #include <linux/fips.h>
4344 #include <linux/time.h>
44
-#include <linux/crypto.h>
4545 #include <crypto/internal/rng.h>
4646
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);
47
+#include "jitterentropy.h"
5448
5549 /***************************************************************************
5650 * Helper function
5751 ***************************************************************************/
58
-
59
-__u64 jent_rol64(__u64 word, unsigned int shift)
60
-{
61
- return rol64(word, shift);
62
-}
6352
6453 void *jent_zalloc(unsigned int len)
6554 {
....@@ -68,7 +57,7 @@
6857
6958 void jent_zfree(void *ptr)
7059 {
71
- kzfree(ptr);
60
+ kfree_sensitive(ptr);
7261 }
7362
7463 int jent_fips_enabled(void)
....@@ -119,6 +108,7 @@
119108 struct jitterentropy {
120109 spinlock_t jent_lock;
121110 struct rand_data *entropy_collector;
111
+ unsigned int reset_cnt;
122112 };
123113
124114 static int jent_kcapi_init(struct crypto_tfm *tfm)
....@@ -153,7 +143,33 @@
153143 int ret = 0;
154144
155145 spin_lock(&rng->jent_lock);
146
+
147
+ /* Return a permanent error in case we had too many resets in a row. */
148
+ if (rng->reset_cnt > (1<<10)) {
149
+ ret = -EFAULT;
150
+ goto out;
151
+ }
152
+
156153 ret = jent_read_entropy(rng->entropy_collector, rdata, dlen);
154
+
155
+ /* Reset RNG in case of health failures */
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
+ /* Convert the Jitter RNG error into a usable error code */
168
+ if (ret == -1)
169
+ ret = -EINVAL;
170
+ }
171
+
172
+out:
157173 spin_unlock(&rng->jent_lock);
158174
159175 return ret;