Skip to content

Commit e192be9

Browse files
committed
random: replace non-blocking pool with a Chacha20-based CRNG
The CRNG is faster, and we don't pretend to track entropy usage in the CRNG any more. Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent b1132de commit e192be9

5 files changed

Lines changed: 357 additions & 164 deletions

File tree

‎crypto/chacha20_generic.c‎

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,11 @@
1515
#include <linux/module.h>
1616
#include <crypto/chacha20.h>
1717

18-
static inline u32 rotl32(u32 v, u8 n)
19-
{
20-
return (v << n) | (v >> (sizeof(v) * 8 - n));
21-
}
22-
2318
static inline u32 le32_to_cpuvp(const void *p)
2419
{
2520
return le32_to_cpup(p);
2621
}
2722

28-
static void chacha20_block(u32 *state, void *stream)
29-
{
30-
u32 x[16], *out = stream;
31-
int i;
32-
33-
for (i = 0; i < ARRAY_SIZE(x); i++)
34-
x[i] = state[i];
35-
36-
for (i = 0; i < 20; i += 2) {
37-
x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 16);
38-
x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 16);
39-
x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 16);
40-
x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 16);
41-
42-
x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 12);
43-
x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 12);
44-
x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 12);
45-
x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 12);
46-
47-
x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 8);
48-
x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 8);
49-
x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 8);
50-
x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 8);
51-
52-
x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 7);
53-
x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 7);
54-
x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 7);
55-
x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 7);
56-
57-
x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 16);
58-
x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 16);
59-
x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 16);
60-
x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 16);
61-
62-
x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 12);
63-
x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 12);
64-
x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 12);
65-
x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 12);
66-
67-
x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 8);
68-
x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 8);
69-
x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 8);
70-
x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 8);
71-
72-
x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 7);
73-
x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 7);
74-
x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 7);
75-
x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 7);
76-
}
77-
78-
for (i = 0; i < ARRAY_SIZE(x); i++)
79-
out[i] = cpu_to_le32(x[i] + state[i]);
80-
81-
state[12]++;
82-
}
83-
8423
static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
8524
unsigned int bytes)
8625
{

0 commit comments

Comments
 (0)