0019-random-replace-non-blocking-pool-with-a-Chacha20-bas.patch 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. From af72f4e73aa5a951d998e88260154082c5ffd340 Mon Sep 17 00:00:00 2001
  2. From: Theodore Ts'o <tytso@mit.edu>
  3. Date: Sun, 12 Jun 2016 18:13:36 -0400
  4. Subject: [PATCH 19/32] random: replace non-blocking pool with a Chacha20-based
  5. CRNG
  6. The CRNG is faster, and we don't pretend to track entropy usage in the
  7. CRNG any more.
  8. Signed-off-by: Theodore Ts'o <tytso@mit.edu>
  9. ---
  10. crypto/chacha20_generic.c | 61 --------
  11. drivers/char/random.c | 378 +++++++++++++++++++++++++++++++++-------------
  12. include/crypto/chacha20.h | 1 +
  13. lib/Makefile | 2 +-
  14. lib/chacha20.c | 79 ++++++++++
  15. 5 files changed, 357 insertions(+), 164 deletions(-)
  16. create mode 100644 lib/chacha20.c
  17. diff --git a/crypto/chacha20_generic.c b/crypto/chacha20_generic.c
  18. index da9c89968223..1cab83146e33 100644
  19. --- a/crypto/chacha20_generic.c
  20. +++ b/crypto/chacha20_generic.c
  21. @@ -15,72 +15,11 @@
  22. #include <linux/module.h>
  23. #include <crypto/chacha20.h>
  24. -static inline u32 rotl32(u32 v, u8 n)
  25. -{
  26. - return (v << n) | (v >> (sizeof(v) * 8 - n));
  27. -}
  28. -
  29. static inline u32 le32_to_cpuvp(const void *p)
  30. {
  31. return le32_to_cpup(p);
  32. }
  33. -static void chacha20_block(u32 *state, void *stream)
  34. -{
  35. - u32 x[16], *out = stream;
  36. - int i;
  37. -
  38. - for (i = 0; i < ARRAY_SIZE(x); i++)
  39. - x[i] = state[i];
  40. -
  41. - for (i = 0; i < 20; i += 2) {
  42. - x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 16);
  43. - x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 16);
  44. - x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 16);
  45. - x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 16);
  46. -
  47. - x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 12);
  48. - x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 12);
  49. - x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 12);
  50. - x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 12);
  51. -
  52. - x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 8);
  53. - x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 8);
  54. - x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 8);
  55. - x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 8);
  56. -
  57. - x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 7);
  58. - x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 7);
  59. - x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 7);
  60. - x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 7);
  61. -
  62. - x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 16);
  63. - x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 16);
  64. - x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 16);
  65. - x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 16);
  66. -
  67. - x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 12);
  68. - x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 12);
  69. - x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 12);
  70. - x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 12);
  71. -
  72. - x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 8);
  73. - x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 8);
  74. - x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 8);
  75. - x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 8);
  76. -
  77. - x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 7);
  78. - x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 7);
  79. - x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 7);
  80. - x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 7);
  81. - }
  82. -
  83. - for (i = 0; i < ARRAY_SIZE(x); i++)
  84. - out[i] = cpu_to_le32(x[i] + state[i]);
  85. -
  86. - state[12]++;
  87. -}
  88. -
  89. static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
  90. unsigned int bytes)
  91. {
  92. diff --git a/drivers/char/random.c b/drivers/char/random.c
  93. index 2916d08ee30e..6d2abeae9434 100644
  94. --- a/drivers/char/random.c
  95. +++ b/drivers/char/random.c
  96. @@ -260,6 +260,7 @@
  97. #include <linux/irq.h>
  98. #include <linux/syscalls.h>
  99. #include <linux/completion.h>
  100. +#include <crypto/chacha20.h>
  101. #include <asm/processor.h>
  102. #include <asm/uaccess.h>
  103. @@ -412,6 +413,31 @@ static struct fasync_struct *fasync;
  104. static DEFINE_SPINLOCK(random_ready_list_lock);
  105. static LIST_HEAD(random_ready_list);
  106. +struct crng_state {
  107. + __u32 state[16];
  108. + unsigned long init_time;
  109. + spinlock_t lock;
  110. +};
  111. +
  112. +struct crng_state primary_crng = {
  113. + .lock = __SPIN_LOCK_UNLOCKED(primary_crng.lock),
  114. +};
  115. +
  116. +/*
  117. + * crng_init = 0 --> Uninitialized
  118. + * 1 --> Initialized
  119. + * 2 --> Initialized from input_pool
  120. + *
  121. + * crng_init is protected by primary_crng->lock, and only increases
  122. + * its value (from 0->1->2).
  123. + */
  124. +static int crng_init = 0;
  125. +#define crng_ready() (likely(crng_init > 0))
  126. +static int crng_init_cnt = 0;
  127. +#define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE)
  128. +static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE]);
  129. +static void process_random_ready_list(void);
  130. +
  131. /**********************************************************************
  132. *
  133. * OS independent entropy store. Here are the functions which handle
  134. @@ -441,10 +467,15 @@ struct entropy_store {
  135. __u8 last_data[EXTRACT_SIZE];
  136. };
  137. +static ssize_t extract_entropy(struct entropy_store *r, void *buf,
  138. + size_t nbytes, int min, int rsvd);
  139. +static ssize_t _extract_entropy(struct entropy_store *r, void *buf,
  140. + size_t nbytes, int fips);
  141. +
  142. +static void crng_reseed(struct crng_state *crng, struct entropy_store *r);
  143. static void push_to_pool(struct work_struct *work);
  144. static __u32 input_pool_data[INPUT_POOL_WORDS];
  145. static __u32 blocking_pool_data[OUTPUT_POOL_WORDS];
  146. -static __u32 nonblocking_pool_data[OUTPUT_POOL_WORDS];
  147. static struct entropy_store input_pool = {
  148. .poolinfo = &poolinfo_table[0],
  149. @@ -465,16 +496,6 @@ static struct entropy_store blocking_pool = {
  150. push_to_pool),
  151. };
  152. -static struct entropy_store nonblocking_pool = {
  153. - .poolinfo = &poolinfo_table[1],
  154. - .name = "nonblocking",
  155. - .pull = &input_pool,
  156. - .lock = __SPIN_LOCK_UNLOCKED(nonblocking_pool.lock),
  157. - .pool = nonblocking_pool_data,
  158. - .push_work = __WORK_INITIALIZER(nonblocking_pool.push_work,
  159. - push_to_pool),
  160. -};
  161. -
  162. static __u32 const twist_table[8] = {
  163. 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
  164. 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
  165. @@ -677,12 +698,6 @@ retry:
  166. if (!r->initialized && r->entropy_total > 128) {
  167. r->initialized = 1;
  168. r->entropy_total = 0;
  169. - if (r == &nonblocking_pool) {
  170. - prandom_reseed_late();
  171. - process_random_ready_list();
  172. - wake_up_all(&urandom_init_wait);
  173. - pr_notice("random: %s pool is initialized\n", r->name);
  174. - }
  175. }
  176. trace_credit_entropy_bits(r->name, nbits,
  177. @@ -692,30 +707,27 @@ retry:
  178. if (r == &input_pool) {
  179. int entropy_bits = entropy_count >> ENTROPY_SHIFT;
  180. + if (crng_init < 2 && entropy_bits >= 128) {
  181. + crng_reseed(&primary_crng, r);
  182. + entropy_bits = r->entropy_count >> ENTROPY_SHIFT;
  183. + }
  184. +
  185. /* should we wake readers? */
  186. if (entropy_bits >= random_read_wakeup_bits) {
  187. wake_up_interruptible(&random_read_wait);
  188. kill_fasync(&fasync, SIGIO, POLL_IN);
  189. }
  190. /* If the input pool is getting full, send some
  191. - * entropy to the two output pools, flipping back and
  192. - * forth between them, until the output pools are 75%
  193. - * full.
  194. + * entropy to the blocking pool until it is 75% full.
  195. */
  196. if (entropy_bits > random_write_wakeup_bits &&
  197. r->initialized &&
  198. r->entropy_total >= 2*random_read_wakeup_bits) {
  199. - static struct entropy_store *last = &blocking_pool;
  200. struct entropy_store *other = &blocking_pool;
  201. - if (last == &blocking_pool)
  202. - other = &nonblocking_pool;
  203. if (other->entropy_count <=
  204. - 3 * other->poolinfo->poolfracbits / 4)
  205. - last = other;
  206. - if (last->entropy_count <=
  207. - 3 * last->poolinfo->poolfracbits / 4) {
  208. - schedule_work(&last->push_work);
  209. + 3 * other->poolinfo->poolfracbits / 4) {
  210. + schedule_work(&other->push_work);
  211. r->entropy_total = 0;
  212. }
  213. }
  214. @@ -736,6 +748,152 @@ static int credit_entropy_bits_safe(struct entropy_store *r, int nbits)
  215. return 0;
  216. }
  217. +/*********************************************************************
  218. + *
  219. + * CRNG using CHACHA20
  220. + *
  221. + *********************************************************************/
  222. +
  223. +#define CRNG_RESEED_INTERVAL (300*HZ)
  224. +
  225. +static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait);
  226. +
  227. +static void crng_initialize(struct crng_state *crng)
  228. +{
  229. + int i;
  230. + unsigned long rv;
  231. +
  232. + memcpy(&crng->state[0], "expand 32-byte k", 16);
  233. + if (crng == &primary_crng)
  234. + _extract_entropy(&input_pool, &crng->state[4],
  235. + sizeof(__u32) * 12, 0);
  236. + else
  237. + get_random_bytes(&crng->state[4], sizeof(__u32) * 12);
  238. + for (i = 4; i < 16; i++) {
  239. + if (!arch_get_random_seed_long(&rv) &&
  240. + !arch_get_random_long(&rv))
  241. + rv = random_get_entropy();
  242. + crng->state[i] ^= rv;
  243. + }
  244. + crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
  245. +}
  246. +
  247. +static int crng_fast_load(const char *cp, size_t len)
  248. +{
  249. + unsigned long flags;
  250. + char *p;
  251. +
  252. + if (!spin_trylock_irqsave(&primary_crng.lock, flags))
  253. + return 0;
  254. + if (crng_ready()) {
  255. + spin_unlock_irqrestore(&primary_crng.lock, flags);
  256. + return 0;
  257. + }
  258. + p = (unsigned char *) &primary_crng.state[4];
  259. + while (len > 0 && crng_init_cnt < CRNG_INIT_CNT_THRESH) {
  260. + p[crng_init_cnt % CHACHA20_KEY_SIZE] ^= *cp;
  261. + cp++; crng_init_cnt++; len--;
  262. + }
  263. + if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) {
  264. + crng_init = 1;
  265. + wake_up_interruptible(&crng_init_wait);
  266. + pr_notice("random: fast init done\n");
  267. + }
  268. + spin_unlock_irqrestore(&primary_crng.lock, flags);
  269. + return 1;
  270. +}
  271. +
  272. +static void crng_reseed(struct crng_state *crng, struct entropy_store *r)
  273. +{
  274. + unsigned long flags;
  275. + int i, num;
  276. + union {
  277. + __u8 block[CHACHA20_BLOCK_SIZE];
  278. + __u32 key[8];
  279. + } buf;
  280. +
  281. + if (r) {
  282. + num = extract_entropy(r, &buf, 32, 16, 0);
  283. + if (num == 0)
  284. + return;
  285. + } else
  286. + extract_crng(buf.block);
  287. + spin_lock_irqsave(&primary_crng.lock, flags);
  288. + for (i = 0; i < 8; i++) {
  289. + unsigned long rv;
  290. + if (!arch_get_random_seed_long(&rv) &&
  291. + !arch_get_random_long(&rv))
  292. + rv = random_get_entropy();
  293. + crng->state[i+4] ^= buf.key[i] ^ rv;
  294. + }
  295. + memzero_explicit(&buf, sizeof(buf));
  296. + crng->init_time = jiffies;
  297. + if (crng == &primary_crng && crng_init < 2) {
  298. + crng_init = 2;
  299. + process_random_ready_list();
  300. + wake_up_interruptible(&crng_init_wait);
  301. + pr_notice("random: crng init done\n");
  302. + }
  303. + spin_unlock_irqrestore(&primary_crng.lock, flags);
  304. +}
  305. +
  306. +static inline void crng_wait_ready(void)
  307. +{
  308. + wait_event_interruptible(crng_init_wait, crng_ready());
  309. +}
  310. +
  311. +static void extract_crng(__u8 out[CHACHA20_BLOCK_SIZE])
  312. +{
  313. + unsigned long v, flags;
  314. + struct crng_state *crng = &primary_crng;
  315. +
  316. + if (crng_init > 1 &&
  317. + time_after(jiffies, crng->init_time + CRNG_RESEED_INTERVAL))
  318. + crng_reseed(crng, &input_pool);
  319. + spin_lock_irqsave(&crng->lock, flags);
  320. + if (arch_get_random_long(&v))
  321. + crng->state[14] ^= v;
  322. + chacha20_block(&crng->state[0], out);
  323. + if (crng->state[12] == 0)
  324. + crng->state[13]++;
  325. + spin_unlock_irqrestore(&crng->lock, flags);
  326. +}
  327. +
  328. +static ssize_t extract_crng_user(void __user *buf, size_t nbytes)
  329. +{
  330. + ssize_t ret = 0, i;
  331. + __u8 tmp[CHACHA20_BLOCK_SIZE];
  332. + int large_request = (nbytes > 256);
  333. +
  334. + while (nbytes) {
  335. + if (large_request && need_resched()) {
  336. + if (signal_pending(current)) {
  337. + if (ret == 0)
  338. + ret = -ERESTARTSYS;
  339. + break;
  340. + }
  341. + schedule();
  342. + }
  343. +
  344. + extract_crng(tmp);
  345. + i = min_t(int, nbytes, CHACHA20_BLOCK_SIZE);
  346. + if (copy_to_user(buf, tmp, i)) {
  347. + ret = -EFAULT;
  348. + break;
  349. + }
  350. +
  351. + nbytes -= i;
  352. + buf += i;
  353. + ret += i;
  354. + }
  355. +
  356. + /* Wipe data just written to memory */
  357. + memzero_explicit(tmp, sizeof(tmp));
  358. +
  359. + return ret;
  360. +}
  361. +
  362. +
  363. /*********************************************************************
  364. *
  365. * Entropy input management
  366. @@ -752,12 +910,12 @@ struct timer_rand_state {
  367. #define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, };
  368. /*
  369. - * Add device- or boot-specific data to the input and nonblocking
  370. - * pools to help initialize them to unique values.
  371. + * Add device- or boot-specific data to the input pool to help
  372. + * initialize it.
  373. *
  374. - * None of this adds any entropy, it is meant to avoid the
  375. - * problem of the nonblocking pool having similar initial state
  376. - * across largely identical devices.
  377. + * None of this adds any entropy; it is meant to avoid the problem of
  378. + * the entropy pool having similar initial state across largely
  379. + * identical devices.
  380. */
  381. void add_device_randomness(const void *buf, unsigned int size)
  382. {
  383. @@ -769,11 +927,6 @@ void add_device_randomness(const void *buf, unsigned int size)
  384. _mix_pool_bytes(&input_pool, buf, size);
  385. _mix_pool_bytes(&input_pool, &time, sizeof(time));
  386. spin_unlock_irqrestore(&input_pool.lock, flags);
  387. -
  388. - spin_lock_irqsave(&nonblocking_pool.lock, flags);
  389. - _mix_pool_bytes(&nonblocking_pool, buf, size);
  390. - _mix_pool_bytes(&nonblocking_pool, &time, sizeof(time));
  391. - spin_unlock_irqrestore(&nonblocking_pool.lock, flags);
  392. }
  393. EXPORT_SYMBOL(add_device_randomness);
  394. @@ -804,7 +957,7 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  395. sample.jiffies = jiffies;
  396. sample.cycles = random_get_entropy();
  397. sample.num = num;
  398. - r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
  399. + r = &input_pool;
  400. mix_pool_bytes(r, &sample, sizeof(sample));
  401. /*
  402. @@ -924,11 +1077,21 @@ void add_interrupt_randomness(int irq, int irq_flags)
  403. fast_mix(fast_pool);
  404. add_interrupt_bench(cycles);
  405. + if (!crng_ready()) {
  406. + if ((fast_pool->count >= 64) &&
  407. + crng_fast_load((char *) fast_pool->pool,
  408. + sizeof(fast_pool->pool))) {
  409. + fast_pool->count = 0;
  410. + fast_pool->last = now;
  411. + }
  412. + return;
  413. + }
  414. +
  415. if ((fast_pool->count < 64) &&
  416. !time_after(now, fast_pool->last + HZ))
  417. return;
  418. - r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
  419. + r = &input_pool;
  420. if (!spin_trylock(&r->lock))
  421. return;
  422. @@ -972,9 +1135,6 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);
  423. *
  424. *********************************************************************/
  425. -static ssize_t extract_entropy(struct entropy_store *r, void *buf,
  426. - size_t nbytes, int min, int rsvd);
  427. -
  428. /*
  429. * This utility inline function is responsible for transferring entropy
  430. * from the primary pool to the secondary extraction pool. We make
  431. @@ -1149,6 +1309,36 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
  432. memzero_explicit(&hash, sizeof(hash));
  433. }
  434. +static ssize_t _extract_entropy(struct entropy_store *r, void *buf,
  435. + size_t nbytes, int fips)
  436. +{
  437. + ssize_t ret = 0, i;
  438. + __u8 tmp[EXTRACT_SIZE];
  439. + unsigned long flags;
  440. +
  441. + while (nbytes) {
  442. + extract_buf(r, tmp);
  443. +
  444. + if (fips) {
  445. + spin_lock_irqsave(&r->lock, flags);
  446. + if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
  447. + panic("Hardware RNG duplicated output!\n");
  448. + memcpy(r->last_data, tmp, EXTRACT_SIZE);
  449. + spin_unlock_irqrestore(&r->lock, flags);
  450. + }
  451. + i = min_t(int, nbytes, EXTRACT_SIZE);
  452. + memcpy(buf, tmp, i);
  453. + nbytes -= i;
  454. + buf += i;
  455. + ret += i;
  456. + }
  457. +
  458. + /* Wipe data just returned from memory */
  459. + memzero_explicit(tmp, sizeof(tmp));
  460. +
  461. + return ret;
  462. +}
  463. +
  464. /*
  465. * This function extracts randomness from the "entropy pool", and
  466. * returns it in a buffer.
  467. @@ -1161,7 +1351,6 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
  468. static ssize_t extract_entropy(struct entropy_store *r, void *buf,
  469. size_t nbytes, int min, int reserved)
  470. {
  471. - ssize_t ret = 0, i;
  472. __u8 tmp[EXTRACT_SIZE];
  473. unsigned long flags;
  474. @@ -1185,27 +1374,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
  475. xfer_secondary_pool(r, nbytes);
  476. nbytes = account(r, nbytes, min, reserved);
  477. - while (nbytes) {
  478. - extract_buf(r, tmp);
  479. -
  480. - if (fips_enabled) {
  481. - spin_lock_irqsave(&r->lock, flags);
  482. - if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
  483. - panic("Hardware RNG duplicated output!\n");
  484. - memcpy(r->last_data, tmp, EXTRACT_SIZE);
  485. - spin_unlock_irqrestore(&r->lock, flags);
  486. - }
  487. - i = min_t(int, nbytes, EXTRACT_SIZE);
  488. - memcpy(buf, tmp, i);
  489. - nbytes -= i;
  490. - buf += i;
  491. - ret += i;
  492. - }
  493. -
  494. - /* Wipe data just returned from memory */
  495. - memzero_explicit(tmp, sizeof(tmp));
  496. -
  497. - return ret;
  498. + return _extract_entropy(r, buf, nbytes, fips_enabled);
  499. }
  500. /*
  501. @@ -1260,15 +1429,26 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
  502. */
  503. void get_random_bytes(void *buf, int nbytes)
  504. {
  505. + __u8 tmp[CHACHA20_BLOCK_SIZE];
  506. +
  507. #if DEBUG_RANDOM_BOOT > 0
  508. - if (unlikely(nonblocking_pool.initialized == 0))
  509. + if (!crng_ready())
  510. printk(KERN_NOTICE "random: %pF get_random_bytes called "
  511. - "with %d bits of entropy available\n",
  512. - (void *) _RET_IP_,
  513. - nonblocking_pool.entropy_total);
  514. + "with crng_init = %d\n", (void *) _RET_IP_, crng_init);
  515. #endif
  516. trace_get_random_bytes(nbytes, _RET_IP_);
  517. - extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);
  518. +
  519. + while (nbytes >= CHACHA20_BLOCK_SIZE) {
  520. + extract_crng(buf);
  521. + buf += CHACHA20_BLOCK_SIZE;
  522. + nbytes -= CHACHA20_BLOCK_SIZE;
  523. + }
  524. +
  525. + if (nbytes > 0) {
  526. + extract_crng(tmp);
  527. + memcpy(buf, tmp, nbytes);
  528. + memzero_explicit(tmp, nbytes);
  529. + }
  530. }
  531. EXPORT_SYMBOL(get_random_bytes);
  532. @@ -1286,7 +1466,7 @@ int add_random_ready_callback(struct random_ready_callback *rdy)
  533. unsigned long flags;
  534. int err = -EALREADY;
  535. - if (likely(nonblocking_pool.initialized))
  536. + if (crng_ready())
  537. return err;
  538. owner = rdy->owner;
  539. @@ -1294,7 +1474,7 @@ int add_random_ready_callback(struct random_ready_callback *rdy)
  540. return -ENOENT;
  541. spin_lock_irqsave(&random_ready_list_lock, flags);
  542. - if (nonblocking_pool.initialized)
  543. + if (crng_ready())
  544. goto out;
  545. owner = NULL;
  546. @@ -1358,7 +1538,7 @@ void get_random_bytes_arch(void *buf, int nbytes)
  547. }
  548. if (nbytes)
  549. - extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
  550. + get_random_bytes(p, nbytes);
  551. }
  552. EXPORT_SYMBOL(get_random_bytes_arch);
  553. @@ -1403,7 +1583,7 @@ static int rand_initialize(void)
  554. {
  555. init_std_data(&input_pool);
  556. init_std_data(&blocking_pool);
  557. - init_std_data(&nonblocking_pool);
  558. + crng_initialize(&primary_crng);
  559. return 0;
  560. }
  561. early_initcall(rand_initialize);
  562. @@ -1465,22 +1645,22 @@ random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  563. static ssize_t
  564. urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  565. {
  566. + unsigned long flags;
  567. static int maxwarn = 10;
  568. int ret;
  569. - if (unlikely(nonblocking_pool.initialized == 0) &&
  570. - maxwarn > 0) {
  571. + if (!crng_ready() && maxwarn > 0) {
  572. maxwarn--;
  573. printk(KERN_NOTICE "random: %s: uninitialized urandom read "
  574. - "(%zd bytes read, %d bits of entropy available)\n",
  575. - current->comm, nbytes, nonblocking_pool.entropy_total);
  576. + "(%zd bytes read)\n",
  577. + current->comm, nbytes);
  578. + spin_lock_irqsave(&primary_crng.lock, flags);
  579. + crng_init_cnt = 0;
  580. + spin_unlock_irqrestore(&primary_crng.lock, flags);
  581. }
  582. -
  583. nbytes = min_t(size_t, nbytes, INT_MAX >> (ENTROPY_SHIFT + 3));
  584. - ret = extract_entropy_user(&nonblocking_pool, buf, nbytes);
  585. -
  586. - trace_urandom_read(8 * nbytes, ENTROPY_BITS(&nonblocking_pool),
  587. - ENTROPY_BITS(&input_pool));
  588. + ret = extract_crng_user(buf, nbytes);
  589. + trace_urandom_read(8 * nbytes, 0, ENTROPY_BITS(&input_pool));
  590. return ret;
  591. }
  592. @@ -1534,10 +1714,7 @@ static ssize_t random_write(struct file *file, const char __user *buffer,
  593. {
  594. size_t ret;
  595. - ret = write_pool(&blocking_pool, buffer, count);
  596. - if (ret)
  597. - return ret;
  598. - ret = write_pool(&nonblocking_pool, buffer, count);
  599. + ret = write_pool(&input_pool, buffer, count);
  600. if (ret)
  601. return ret;
  602. @@ -1586,7 +1763,6 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  603. if (!capable(CAP_SYS_ADMIN))
  604. return -EPERM;
  605. input_pool.entropy_count = 0;
  606. - nonblocking_pool.entropy_count = 0;
  607. blocking_pool.entropy_count = 0;
  608. return 0;
  609. default:
  610. @@ -1628,11 +1804,10 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count,
  611. if (flags & GRND_RANDOM)
  612. return _random_read(flags & GRND_NONBLOCK, buf, count);
  613. - if (unlikely(nonblocking_pool.initialized == 0)) {
  614. + if (!crng_ready()) {
  615. if (flags & GRND_NONBLOCK)
  616. return -EAGAIN;
  617. - wait_event_interruptible(urandom_init_wait,
  618. - nonblocking_pool.initialized);
  619. + crng_wait_ready();
  620. if (signal_pending(current))
  621. return -ERESTARTSYS;
  622. }
  623. @@ -1888,18 +2063,17 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
  624. {
  625. struct entropy_store *poolp = &input_pool;
  626. - if (unlikely(nonblocking_pool.initialized == 0))
  627. - poolp = &nonblocking_pool;
  628. - else {
  629. - /* Suspend writing if we're above the trickle
  630. - * threshold. We'll be woken up again once below
  631. - * random_write_wakeup_thresh, or when the calling
  632. - * thread is about to terminate.
  633. - */
  634. - wait_event_interruptible(random_write_wait,
  635. - kthread_should_stop() ||
  636. - ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits);
  637. + if (!crng_ready()) {
  638. + crng_fast_load(buffer, count);
  639. + return;
  640. }
  641. +
  642. + /* Suspend writing if we're above the trickle threshold.
  643. + * We'll be woken up again once below random_write_wakeup_thresh,
  644. + * or when the calling thread is about to terminate.
  645. + */
  646. + wait_event_interruptible(random_write_wait, kthread_should_stop() ||
  647. + ENTROPY_BITS(&input_pool) <= random_write_wakeup_bits);
  648. mix_pool_bytes(poolp, buffer, count);
  649. credit_entropy_bits(poolp, entropy);
  650. }
  651. diff --git a/include/crypto/chacha20.h b/include/crypto/chacha20.h
  652. index 274bbaeeed0f..20d20f681a72 100644
  653. --- a/include/crypto/chacha20.h
  654. +++ b/include/crypto/chacha20.h
  655. @@ -16,6 +16,7 @@ struct chacha20_ctx {
  656. u32 key[8];
  657. };
  658. +void chacha20_block(u32 *state, void *stream);
  659. void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv);
  660. int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
  661. unsigned int keysize);
  662. diff --git a/lib/Makefile b/lib/Makefile
  663. index 34a7460c7005..c9ab674107ef 100644
  664. --- a/lib/Makefile
  665. +++ b/lib/Makefile
  666. @@ -10,7 +10,7 @@ endif
  667. lib-y := ctype.o string.o vsprintf.o cmdline.o \
  668. rbtree.o radix-tree.o dump_stack.o timerqueue.o\
  669. idr.o int_sqrt.o extable.o \
  670. - sha1.o md5.o irq_regs.o argv_split.o \
  671. + sha1.o chacha20.o md5.o irq_regs.o argv_split.o \
  672. proportions.o flex_proportions.o ratelimit.o show_mem.o \
  673. is_single_threaded.o plist.o decompress.o kobject_uevent.o \
  674. earlycpio.o seq_buf.o siphash.o nmi_backtrace.o
  675. diff --git a/lib/chacha20.c b/lib/chacha20.c
  676. new file mode 100644
  677. index 000000000000..250ceed9ec9a
  678. --- /dev/null
  679. +++ b/lib/chacha20.c
  680. @@ -0,0 +1,79 @@
  681. +/*
  682. + * ChaCha20 256-bit cipher algorithm, RFC7539
  683. + *
  684. + * Copyright (C) 2015 Martin Willi
  685. + *
  686. + * This program is free software; you can redistribute it and/or modify
  687. + * it under the terms of the GNU General Public License as published by
  688. + * the Free Software Foundation; either version 2 of the License, or
  689. + * (at your option) any later version.
  690. + */
  691. +
  692. +#include <linux/kernel.h>
  693. +#include <linux/export.h>
  694. +#include <linux/bitops.h>
  695. +#include <linux/cryptohash.h>
  696. +#include <asm/unaligned.h>
  697. +#include <crypto/chacha20.h>
  698. +
  699. +static inline u32 rotl32(u32 v, u8 n)
  700. +{
  701. + return (v << n) | (v >> (sizeof(v) * 8 - n));
  702. +}
  703. +
  704. +extern void chacha20_block(u32 *state, void *stream)
  705. +{
  706. + u32 x[16], *out = stream;
  707. + int i;
  708. +
  709. + for (i = 0; i < ARRAY_SIZE(x); i++)
  710. + x[i] = state[i];
  711. +
  712. + for (i = 0; i < 20; i += 2) {
  713. + x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 16);
  714. + x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 16);
  715. + x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 16);
  716. + x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 16);
  717. +
  718. + x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 12);
  719. + x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 12);
  720. + x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 12);
  721. + x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 12);
  722. +
  723. + x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 8);
  724. + x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 8);
  725. + x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 8);
  726. + x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 8);
  727. +
  728. + x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 7);
  729. + x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 7);
  730. + x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 7);
  731. + x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 7);
  732. +
  733. + x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 16);
  734. + x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 16);
  735. + x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 16);
  736. + x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 16);
  737. +
  738. + x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 12);
  739. + x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 12);
  740. + x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 12);
  741. + x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 12);
  742. +
  743. + x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 8);
  744. + x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 8);
  745. + x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 8);
  746. + x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 8);
  747. +
  748. + x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 7);
  749. + x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 7);
  750. + x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 7);
  751. + x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 7);
  752. + }
  753. +
  754. + for (i = 0; i < ARRAY_SIZE(x); i++)
  755. + out[i] = cpu_to_le32(x[i] + state[i]);
  756. +
  757. + state[12]++;
  758. +}
  759. +EXPORT_SYMBOL(chacha20_block);
  760. --
  761. 2.16.4