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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238#include <linux/utsname.h>
239#include <linux/module.h>
240#include <linux/kernel.h>
241#include <linux/major.h>
242#include <linux/string.h>
243#include <linux/fcntl.h>
244#include <linux/slab.h>
245#include <linux/random.h>
246#include <linux/poll.h>
247#include <linux/init.h>
248#include <linux/fs.h>
249#include <linux/genhd.h>
250#include <linux/interrupt.h>
251#include <linux/mm.h>
252#include <linux/spinlock.h>
253#include <linux/percpu.h>
254#include <linux/cryptohash.h>
255#include <linux/fips.h>
256#include <linux/ptrace.h>
257#include <linux/kmemcheck.h>
258#include <linux/workqueue.h>
259#include <linux/irq.h>
260
261#include <asm/processor.h>
262#include <asm/uaccess.h>
263#include <asm/irq.h>
264#include <asm/irq_regs.h>
265#include <asm/io.h>
266
267#define CREATE_TRACE_POINTS
268#include <trace/events/random.h>
269
270
271
272
273#define INPUT_POOL_SHIFT 12
274#define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5))
275#define OUTPUT_POOL_SHIFT 10
276#define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5))
277#define SEC_XFER_SIZE 512
278#define EXTRACT_SIZE 10
279
280#define DEBUG_RANDOM_BOOT 0
281
282#define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long))
283
284
285
286
287
288
289
290
291#define ENTROPY_SHIFT 3
292#define ENTROPY_BITS(r) ((r)->entropy_count >> ENTROPY_SHIFT)
293
294
295
296
297
298static int random_read_wakeup_thresh = 64;
299
300
301
302
303
304
305static int random_write_wakeup_thresh = 28 * OUTPUT_POOL_WORDS;
306
307
308
309
310
311
312static int random_min_urandom_seed = 60;
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359static struct poolinfo {
360 int poolbitshift, poolwords, poolbytes, poolbits, poolfracbits;
361#define S(x) ilog2(x)+5, (x), (x)*4, (x)*32, (x) << (ENTROPY_SHIFT+5)
362 int tap1, tap2, tap3, tap4, tap5;
363} poolinfo_table[] = {
364
365
366 { S(128), 104, 76, 51, 25, 1 },
367
368
369 { S(32), 26, 19, 14, 7, 1 },
370#if 0
371
372 { S(2048), 1638, 1231, 819, 411, 1 },
373
374
375 { S(1024), 817, 615, 412, 204, 1 },
376
377
378 { S(1024), 819, 616, 410, 207, 2 },
379
380
381 { S(512), 411, 308, 208, 104, 1 },
382
383
384 { S(512), 409, 307, 206, 102, 2 },
385
386 { S(512), 409, 309, 205, 103, 2 },
387
388
389 { S(256), 205, 155, 101, 52, 1 },
390
391
392 { S(128), 103, 78, 51, 27, 2 },
393
394
395 { S(64), 52, 39, 26, 14, 1 },
396#endif
397};
398
399
400
401
402static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
403static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
404static struct fasync_struct *fasync;
405
406
407
408
409
410
411
412
413struct entropy_store;
414struct entropy_store {
415
416 const struct poolinfo *poolinfo;
417 __u32 *pool;
418 const char *name;
419 struct entropy_store *pull;
420 struct work_struct push_work;
421
422
423 unsigned long last_pulled;
424 spinlock_t lock;
425 unsigned short add_ptr;
426 unsigned short input_rotate;
427 int entropy_count;
428 int entropy_total;
429 unsigned int initialized:1;
430 unsigned int limit:1;
431 unsigned int last_data_init:1;
432 __u8 last_data[EXTRACT_SIZE];
433};
434
435static void push_to_pool(struct work_struct *work);
436static __u32 input_pool_data[INPUT_POOL_WORDS];
437static __u32 blocking_pool_data[OUTPUT_POOL_WORDS];
438static __u32 nonblocking_pool_data[OUTPUT_POOL_WORDS];
439
440static struct entropy_store input_pool = {
441 .poolinfo = &poolinfo_table[0],
442 .name = "input",
443 .limit = 1,
444 .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock),
445 .pool = input_pool_data
446};
447
448static struct entropy_store blocking_pool = {
449 .poolinfo = &poolinfo_table[1],
450 .name = "blocking",
451 .limit = 1,
452 .pull = &input_pool,
453 .lock = __SPIN_LOCK_UNLOCKED(blocking_pool.lock),
454 .pool = blocking_pool_data,
455 .push_work = __WORK_INITIALIZER(blocking_pool.push_work,
456 push_to_pool),
457};
458
459static struct entropy_store nonblocking_pool = {
460 .poolinfo = &poolinfo_table[1],
461 .name = "nonblocking",
462 .pull = &input_pool,
463 .lock = __SPIN_LOCK_UNLOCKED(nonblocking_pool.lock),
464 .pool = nonblocking_pool_data,
465 .push_work = __WORK_INITIALIZER(nonblocking_pool.push_work,
466 push_to_pool),
467};
468
469static __u32 const twist_table[8] = {
470 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
471 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
472
473
474
475
476
477
478
479
480
481
482
483static void _mix_pool_bytes(struct entropy_store *r, const void *in,
484 int nbytes, __u8 out[64])
485{
486 unsigned long i, j, tap1, tap2, tap3, tap4, tap5;
487 int input_rotate;
488 int wordmask = r->poolinfo->poolwords - 1;
489 const char *bytes = in;
490 __u32 w;
491
492 tap1 = r->poolinfo->tap1;
493 tap2 = r->poolinfo->tap2;
494 tap3 = r->poolinfo->tap3;
495 tap4 = r->poolinfo->tap4;
496 tap5 = r->poolinfo->tap5;
497
498 smp_rmb();
499 input_rotate = ACCESS_ONCE(r->input_rotate);
500 i = ACCESS_ONCE(r->add_ptr);
501
502
503 while (nbytes--) {
504 w = rol32(*bytes++, input_rotate);
505 i = (i - 1) & wordmask;
506
507
508 w ^= r->pool[i];
509 w ^= r->pool[(i + tap1) & wordmask];
510 w ^= r->pool[(i + tap2) & wordmask];
511 w ^= r->pool[(i + tap3) & wordmask];
512 w ^= r->pool[(i + tap4) & wordmask];
513 w ^= r->pool[(i + tap5) & wordmask];
514
515
516 r->pool[i] = (w >> 3) ^ twist_table[w & 7];
517
518
519
520
521
522
523
524 input_rotate = (input_rotate + (i ? 7 : 14)) & 31;
525 }
526
527 ACCESS_ONCE(r->input_rotate) = input_rotate;
528 ACCESS_ONCE(r->add_ptr) = i;
529 smp_wmb();
530
531 if (out)
532 for (j = 0; j < 16; j++)
533 ((__u32 *)out)[j] = r->pool[(i - j) & wordmask];
534}
535
536static void __mix_pool_bytes(struct entropy_store *r, const void *in,
537 int nbytes, __u8 out[64])
538{
539 trace_mix_pool_bytes_nolock(r->name, nbytes, _RET_IP_);
540 _mix_pool_bytes(r, in, nbytes, out);
541}
542
543static void mix_pool_bytes(struct entropy_store *r, const void *in,
544 int nbytes, __u8 out[64])
545{
546 unsigned long flags;
547
548 trace_mix_pool_bytes(r->name, nbytes, _RET_IP_);
549 spin_lock_irqsave(&r->lock, flags);
550 _mix_pool_bytes(r, in, nbytes, out);
551 spin_unlock_irqrestore(&r->lock, flags);
552}
553
554struct fast_pool {
555 __u32 pool[4];
556 unsigned long last;
557 unsigned short count;
558 unsigned char rotate;
559 unsigned char last_timer_intr;
560};
561
562
563
564
565
566
567static void fast_mix(struct fast_pool *f, __u32 input[4])
568{
569 __u32 w;
570 unsigned input_rotate = f->rotate;
571
572 w = rol32(input[0], input_rotate) ^ f->pool[0] ^ f->pool[3];
573 f->pool[0] = (w >> 3) ^ twist_table[w & 7];
574 input_rotate = (input_rotate + 14) & 31;
575 w = rol32(input[1], input_rotate) ^ f->pool[1] ^ f->pool[0];
576 f->pool[1] = (w >> 3) ^ twist_table[w & 7];
577 input_rotate = (input_rotate + 7) & 31;
578 w = rol32(input[2], input_rotate) ^ f->pool[2] ^ f->pool[1];
579 f->pool[2] = (w >> 3) ^ twist_table[w & 7];
580 input_rotate = (input_rotate + 7) & 31;
581 w = rol32(input[3], input_rotate) ^ f->pool[3] ^ f->pool[2];
582 f->pool[3] = (w >> 3) ^ twist_table[w & 7];
583 input_rotate = (input_rotate + 7) & 31;
584
585 f->rotate = input_rotate;
586 f->count++;
587}
588
589
590
591
592
593
594static void credit_entropy_bits(struct entropy_store *r, int nbits)
595{
596 int entropy_count, orig;
597 const int pool_size = r->poolinfo->poolfracbits;
598 int nfrac = nbits << ENTROPY_SHIFT;
599
600 if (!nbits)
601 return;
602
603retry:
604 entropy_count = orig = ACCESS_ONCE(r->entropy_count);
605 if (nfrac < 0) {
606
607 entropy_count += nfrac;
608 } else {
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630 int pnfrac = nfrac;
631 const int s = r->poolinfo->poolbitshift + ENTROPY_SHIFT + 2;
632
633
634 do {
635 unsigned int anfrac = min(pnfrac, pool_size/2);
636 unsigned int add =
637 ((pool_size - entropy_count)*anfrac*3) >> s;
638
639 entropy_count += add;
640 pnfrac -= anfrac;
641 } while (unlikely(entropy_count < pool_size-2 && pnfrac));
642 }
643
644 if (entropy_count < 0) {
645 pr_warn("random: negative entropy/overflow: pool %s count %d\n",
646 r->name, entropy_count);
647 WARN_ON(1);
648 entropy_count = 0;
649 } else if (entropy_count > pool_size)
650 entropy_count = pool_size;
651 if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
652 goto retry;
653
654 r->entropy_total += nbits;
655 if (!r->initialized && r->entropy_total > 128) {
656 r->initialized = 1;
657 r->entropy_total = 0;
658 if (r == &nonblocking_pool) {
659 prandom_reseed_late();
660 pr_notice("random: %s pool is initialized\n", r->name);
661 }
662 }
663
664 trace_credit_entropy_bits(r->name, nbits,
665 entropy_count >> ENTROPY_SHIFT,
666 r->entropy_total, _RET_IP_);
667
668 if (r == &input_pool) {
669 int entropy_bytes = entropy_count >> ENTROPY_SHIFT;
670
671
672 if (entropy_bytes >= random_read_wakeup_thresh) {
673 wake_up_interruptible(&random_read_wait);
674 kill_fasync(&fasync, SIGIO, POLL_IN);
675 }
676
677
678
679
680
681 if (entropy_bytes > random_write_wakeup_thresh &&
682 r->initialized &&
683 r->entropy_total >= 2*random_read_wakeup_thresh) {
684 static struct entropy_store *last = &blocking_pool;
685 struct entropy_store *other = &blocking_pool;
686
687 if (last == &blocking_pool)
688 other = &nonblocking_pool;
689 if (other->entropy_count <=
690 3 * other->poolinfo->poolfracbits / 4)
691 last = other;
692 if (last->entropy_count <=
693 3 * last->poolinfo->poolfracbits / 4) {
694 schedule_work(&last->push_work);
695 r->entropy_total = 0;
696 }
697 }
698 }
699}
700
701static void credit_entropy_bits_safe(struct entropy_store *r, int nbits)
702{
703 const int nbits_max = (int)(~0U >> (ENTROPY_SHIFT + 1));
704
705
706 nbits = min(nbits, nbits_max);
707 nbits = max(nbits, -nbits_max);
708
709 credit_entropy_bits(r, nbits);
710}
711
712
713
714
715
716
717
718
719struct timer_rand_state {
720 cycles_t last_time;
721 long last_delta, last_delta2;
722 unsigned dont_count_entropy:1;
723};
724
725#define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, };
726
727
728
729
730
731
732
733
734
735void add_device_randomness(const void *buf, unsigned int size)
736{
737 unsigned long time = random_get_entropy() ^ jiffies;
738 unsigned long flags;
739
740 trace_add_device_randomness(size, _RET_IP_);
741 spin_lock_irqsave(&input_pool.lock, flags);
742 _mix_pool_bytes(&input_pool, buf, size, NULL);
743 _mix_pool_bytes(&input_pool, &time, sizeof(time), NULL);
744 spin_unlock_irqrestore(&input_pool.lock, flags);
745
746 spin_lock_irqsave(&nonblocking_pool.lock, flags);
747 _mix_pool_bytes(&nonblocking_pool, buf, size, NULL);
748 _mix_pool_bytes(&nonblocking_pool, &time, sizeof(time), NULL);
749 spin_unlock_irqrestore(&nonblocking_pool.lock, flags);
750}
751EXPORT_SYMBOL(add_device_randomness);
752
753static struct timer_rand_state input_timer_state = INIT_TIMER_RAND_STATE;
754
755
756
757
758
759
760
761
762
763
764
765static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
766{
767 struct entropy_store *r;
768 struct {
769 long jiffies;
770 unsigned cycles;
771 unsigned num;
772 } sample;
773 long delta, delta2, delta3;
774
775 preempt_disable();
776
777 sample.jiffies = jiffies;
778 sample.cycles = random_get_entropy();
779 sample.num = num;
780 r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
781 mix_pool_bytes(r, &sample, sizeof(sample), NULL);
782
783
784
785
786
787
788
789 if (!state->dont_count_entropy) {
790 delta = sample.jiffies - state->last_time;
791 state->last_time = sample.jiffies;
792
793 delta2 = delta - state->last_delta;
794 state->last_delta = delta;
795
796 delta3 = delta2 - state->last_delta2;
797 state->last_delta2 = delta2;
798
799 if (delta < 0)
800 delta = -delta;
801 if (delta2 < 0)
802 delta2 = -delta2;
803 if (delta3 < 0)
804 delta3 = -delta3;
805 if (delta > delta2)
806 delta = delta2;
807 if (delta > delta3)
808 delta = delta3;
809
810
811
812
813
814
815 credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
816 }
817 preempt_enable();
818}
819
820void add_input_randomness(unsigned int type, unsigned int code,
821 unsigned int value)
822{
823 static unsigned char last_value;
824
825
826 if (value == last_value)
827 return;
828
829 last_value = value;
830 add_timer_randomness(&input_timer_state,
831 (type << 4) ^ code ^ (code >> 4) ^ value);
832 trace_add_input_randomness(ENTROPY_BITS(&input_pool));
833}
834EXPORT_SYMBOL_GPL(add_input_randomness);
835
836static DEFINE_PER_CPU(struct fast_pool, irq_randomness);
837
838void add_interrupt_randomness(int irq, int irq_flags)
839{
840 struct entropy_store *r;
841 struct fast_pool *fast_pool = &__get_cpu_var(irq_randomness);
842 struct pt_regs *regs = get_irq_regs();
843 unsigned long now = jiffies;
844 cycles_t cycles = random_get_entropy();
845 __u32 input[4], c_high, j_high;
846 __u64 ip;
847
848 c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
849 j_high = (sizeof(now) > 4) ? now >> 32 : 0;
850 input[0] = cycles ^ j_high ^ irq;
851 input[1] = now ^ c_high;
852 ip = regs ? instruction_pointer(regs) : _RET_IP_;
853 input[2] = ip;
854 input[3] = ip >> 32;
855
856 fast_mix(fast_pool, input);
857
858 if ((fast_pool->count & 63) && !time_after(now, fast_pool->last + HZ))
859 return;
860
861 fast_pool->last = now;
862
863 r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
864 __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool), NULL);
865
866
867
868
869
870 if (cycles == 0) {
871 if (irq_flags & __IRQF_TIMER) {
872 if (fast_pool->last_timer_intr)
873 return;
874 fast_pool->last_timer_intr = 1;
875 } else
876 fast_pool->last_timer_intr = 0;
877 }
878 credit_entropy_bits(r, 1);
879}
880
881#ifdef CONFIG_BLOCK
882void add_disk_randomness(struct gendisk *disk)
883{
884 if (!disk || !disk->random)
885 return;
886
887 add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
888 trace_add_disk_randomness(disk_devt(disk), ENTROPY_BITS(&input_pool));
889}
890#endif
891
892
893
894
895
896
897
898static ssize_t extract_entropy(struct entropy_store *r, void *buf,
899 size_t nbytes, int min, int rsvd);
900
901
902
903
904
905
906static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes);
907static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
908{
909 if (r->limit == 0 && random_min_urandom_seed) {
910 unsigned long now = jiffies;
911
912 if (time_before(now,
913 r->last_pulled + random_min_urandom_seed * HZ))
914 return;
915 r->last_pulled = now;
916 }
917 if (r->pull &&
918 r->entropy_count < (nbytes << (ENTROPY_SHIFT + 3)) &&
919 r->entropy_count < r->poolinfo->poolfracbits)
920 _xfer_secondary_pool(r, nbytes);
921}
922
923static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
924{
925 __u32 tmp[OUTPUT_POOL_WORDS];
926
927
928 int rsvd = r->limit ? 0 : random_read_wakeup_thresh/4;
929 int bytes = nbytes;
930
931
932 bytes = max_t(int, bytes, random_read_wakeup_thresh / 8);
933
934 bytes = min_t(int, bytes, sizeof(tmp));
935
936 trace_xfer_secondary_pool(r->name, bytes * 8, nbytes * 8,
937 ENTROPY_BITS(r), ENTROPY_BITS(r->pull));
938 bytes = extract_entropy(r->pull, tmp, bytes,
939 random_read_wakeup_thresh / 8, rsvd);
940 mix_pool_bytes(r, tmp, bytes, NULL);
941 credit_entropy_bits(r, bytes*8);
942}
943
944
945
946
947
948
949
950static void push_to_pool(struct work_struct *work)
951{
952 struct entropy_store *r = container_of(work, struct entropy_store,
953 push_work);
954 BUG_ON(!r);
955 _xfer_secondary_pool(r, random_read_wakeup_thresh/8);
956 trace_push_to_pool(r->name, r->entropy_count >> ENTROPY_SHIFT,
957 r->pull->entropy_count >> ENTROPY_SHIFT);
958}
959
960
961
962
963
964
965
966
967
968
969
970
971
972static size_t account(struct entropy_store *r, size_t nbytes, int min,
973 int reserved)
974{
975 unsigned long flags;
976 int wakeup_write = 0;
977 int have_bytes;
978 int entropy_count, orig;
979 size_t ibytes;
980
981
982 spin_lock_irqsave(&r->lock, flags);
983
984 BUG_ON(r->entropy_count > r->poolinfo->poolfracbits);
985
986
987retry:
988 entropy_count = orig = ACCESS_ONCE(r->entropy_count);
989 have_bytes = entropy_count >> (ENTROPY_SHIFT + 3);
990 ibytes = nbytes;
991 if (have_bytes < min + reserved) {
992 ibytes = 0;
993 } else {
994
995 if (r->limit && ibytes + reserved >= have_bytes)
996 ibytes = have_bytes - reserved;
997
998 if (have_bytes >= ibytes + reserved)
999 entropy_count -= ibytes << (ENTROPY_SHIFT + 3);
1000 else
1001 entropy_count = reserved << (ENTROPY_SHIFT + 3);
1002
1003 if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
1004 goto retry;
1005
1006 if ((r->entropy_count >> ENTROPY_SHIFT)
1007 < random_write_wakeup_thresh)
1008 wakeup_write = 1;
1009 }
1010 spin_unlock_irqrestore(&r->lock, flags);
1011
1012 trace_debit_entropy(r->name, 8 * ibytes);
1013 if (wakeup_write) {
1014 wake_up_interruptible(&random_write_wait);
1015 kill_fasync(&fasync, SIGIO, POLL_OUT);
1016 }
1017
1018 return ibytes;
1019}
1020
1021static void extract_buf(struct entropy_store *r, __u8 *out)
1022{
1023 int i;
1024 union {
1025 __u32 w[5];
1026 unsigned long l[LONGS(20)];
1027 } hash;
1028 __u32 workspace[SHA_WORKSPACE_WORDS];
1029 __u8 extract[64];
1030 unsigned long flags;
1031
1032
1033 sha_init(hash.w);
1034 spin_lock_irqsave(&r->lock, flags);
1035 for (i = 0; i < r->poolinfo->poolwords; i += 16)
1036 sha_transform(hash.w, (__u8 *)(r->pool + i), workspace);
1037
1038
1039
1040
1041
1042 for (i = 0; i < LONGS(20); i++) {
1043 unsigned long v;
1044 if (!arch_get_random_long(&v))
1045 break;
1046 hash.l[i] ^= v;
1047 }
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058 __mix_pool_bytes(r, hash.w, sizeof(hash.w), extract);
1059 spin_unlock_irqrestore(&r->lock, flags);
1060
1061
1062
1063
1064
1065 sha_transform(hash.w, extract, workspace);
1066 memset(extract, 0, sizeof(extract));
1067 memset(workspace, 0, sizeof(workspace));
1068
1069
1070
1071
1072
1073
1074 hash.w[0] ^= hash.w[3];
1075 hash.w[1] ^= hash.w[4];
1076 hash.w[2] ^= rol32(hash.w[2], 16);
1077
1078 memcpy(out, &hash, EXTRACT_SIZE);
1079 memset(&hash, 0, sizeof(hash));
1080}
1081
1082static ssize_t extract_entropy(struct entropy_store *r, void *buf,
1083 size_t nbytes, int min, int reserved)
1084{
1085 ssize_t ret = 0, i;
1086 __u8 tmp[EXTRACT_SIZE];
1087 unsigned long flags;
1088
1089
1090 if (fips_enabled) {
1091 spin_lock_irqsave(&r->lock, flags);
1092 if (!r->last_data_init) {
1093 r->last_data_init = 1;
1094 spin_unlock_irqrestore(&r->lock, flags);
1095 trace_extract_entropy(r->name, EXTRACT_SIZE,
1096 ENTROPY_BITS(r), _RET_IP_);
1097 xfer_secondary_pool(r, EXTRACT_SIZE);
1098 extract_buf(r, tmp);
1099 spin_lock_irqsave(&r->lock, flags);
1100 memcpy(r->last_data, tmp, EXTRACT_SIZE);
1101 }
1102 spin_unlock_irqrestore(&r->lock, flags);
1103 }
1104
1105 trace_extract_entropy(r->name, nbytes, ENTROPY_BITS(r), _RET_IP_);
1106 xfer_secondary_pool(r, nbytes);
1107 nbytes = account(r, nbytes, min, reserved);
1108
1109 while (nbytes) {
1110 extract_buf(r, tmp);
1111
1112 if (fips_enabled) {
1113 spin_lock_irqsave(&r->lock, flags);
1114 if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
1115 panic("Hardware RNG duplicated output!\n");
1116 memcpy(r->last_data, tmp, EXTRACT_SIZE);
1117 spin_unlock_irqrestore(&r->lock, flags);
1118 }
1119 i = min_t(int, nbytes, EXTRACT_SIZE);
1120 memcpy(buf, tmp, i);
1121 nbytes -= i;
1122 buf += i;
1123 ret += i;
1124 }
1125
1126
1127 memset(tmp, 0, sizeof(tmp));
1128
1129 return ret;
1130}
1131
1132static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
1133 size_t nbytes)
1134{
1135 ssize_t ret = 0, i;
1136 __u8 tmp[EXTRACT_SIZE];
1137
1138 trace_extract_entropy_user(r->name, nbytes, ENTROPY_BITS(r), _RET_IP_);
1139 xfer_secondary_pool(r, nbytes);
1140 nbytes = account(r, nbytes, 0, 0);
1141
1142 while (nbytes) {
1143 if (need_resched()) {
1144 if (signal_pending(current)) {
1145 if (ret == 0)
1146 ret = -ERESTARTSYS;
1147 break;
1148 }
1149 schedule();
1150 }
1151
1152 extract_buf(r, tmp);
1153 i = min_t(int, nbytes, EXTRACT_SIZE);
1154 if (copy_to_user(buf, tmp, i)) {
1155 ret = -EFAULT;
1156 break;
1157 }
1158
1159 nbytes -= i;
1160 buf += i;
1161 ret += i;
1162 }
1163
1164
1165 memset(tmp, 0, sizeof(tmp));
1166
1167 return ret;
1168}
1169
1170
1171
1172
1173
1174
1175
1176void get_random_bytes(void *buf, int nbytes)
1177{
1178#if DEBUG_RANDOM_BOOT > 0
1179 if (unlikely(nonblocking_pool.initialized == 0))
1180 printk(KERN_NOTICE "random: %pF get_random_bytes called "
1181 "with %d bits of entropy available\n",
1182 (void *) _RET_IP_,
1183 nonblocking_pool.entropy_total);
1184#endif
1185 trace_get_random_bytes(nbytes, _RET_IP_);
1186 extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);
1187}
1188EXPORT_SYMBOL(get_random_bytes);
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200void get_random_bytes_arch(void *buf, int nbytes)
1201{
1202 char *p = buf;
1203
1204 trace_get_random_bytes_arch(nbytes, _RET_IP_);
1205 while (nbytes) {
1206 unsigned long v;
1207 int chunk = min(nbytes, (int)sizeof(unsigned long));
1208
1209 if (!arch_get_random_long(&v))
1210 break;
1211
1212 memcpy(p, &v, chunk);
1213 p += chunk;
1214 nbytes -= chunk;
1215 }
1216
1217 if (nbytes)
1218 extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
1219}
1220EXPORT_SYMBOL(get_random_bytes_arch);
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232static void init_std_data(struct entropy_store *r)
1233{
1234 int i;
1235 ktime_t now = ktime_get_real();
1236 unsigned long rv;
1237
1238 r->last_pulled = jiffies;
1239 mix_pool_bytes(r, &now, sizeof(now), NULL);
1240 for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) {
1241 if (!arch_get_random_long(&rv))
1242 rv = random_get_entropy();
1243 mix_pool_bytes(r, &rv, sizeof(rv), NULL);
1244 }
1245 mix_pool_bytes(r, utsname(), sizeof(*(utsname())), NULL);
1246}
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258static int rand_initialize(void)
1259{
1260 init_std_data(&input_pool);
1261 init_std_data(&blocking_pool);
1262 init_std_data(&nonblocking_pool);
1263 return 0;
1264}
1265early_initcall(rand_initialize);
1266
1267#ifdef CONFIG_BLOCK
1268void rand_initialize_disk(struct gendisk *disk)
1269{
1270 struct timer_rand_state *state;
1271
1272
1273
1274
1275
1276 state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
1277 if (state) {
1278 state->last_time = INITIAL_JIFFIES;
1279 disk->random = state;
1280 }
1281}
1282#endif
1283
1284static ssize_t
1285random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1286{
1287 ssize_t n, retval = 0, count = 0;
1288
1289 if (nbytes == 0)
1290 return 0;
1291
1292 while (nbytes > 0) {
1293 n = nbytes;
1294 if (n > SEC_XFER_SIZE)
1295 n = SEC_XFER_SIZE;
1296
1297 n = extract_entropy_user(&blocking_pool, buf, n);
1298
1299 if (n < 0) {
1300 retval = n;
1301 break;
1302 }
1303
1304 trace_random_read(n*8, (nbytes-n)*8,
1305 ENTROPY_BITS(&blocking_pool),
1306 ENTROPY_BITS(&input_pool));
1307
1308 if (n == 0) {
1309 if (file->f_flags & O_NONBLOCK) {
1310 retval = -EAGAIN;
1311 break;
1312 }
1313
1314 wait_event_interruptible(random_read_wait,
1315 ENTROPY_BITS(&input_pool) >=
1316 random_read_wakeup_thresh);
1317
1318 if (signal_pending(current)) {
1319 retval = -ERESTARTSYS;
1320 break;
1321 }
1322
1323 continue;
1324 }
1325
1326 count += n;
1327 buf += n;
1328 nbytes -= n;
1329 break;
1330
1331 }
1332
1333 return (count ? count : retval);
1334}
1335
1336static ssize_t
1337urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1338{
1339 int ret;
1340
1341 if (unlikely(nonblocking_pool.initialized == 0))
1342 printk_once(KERN_NOTICE "random: %s urandom read "
1343 "with %d bits of entropy available\n",
1344 current->comm, nonblocking_pool.entropy_total);
1345
1346 ret = extract_entropy_user(&nonblocking_pool, buf, nbytes);
1347
1348 trace_urandom_read(8 * nbytes, ENTROPY_BITS(&nonblocking_pool),
1349 ENTROPY_BITS(&input_pool));
1350 return ret;
1351}
1352
1353static unsigned int
1354random_poll(struct file *file, poll_table * wait)
1355{
1356 unsigned int mask;
1357
1358 poll_wait(file, &random_read_wait, wait);
1359 poll_wait(file, &random_write_wait, wait);
1360 mask = 0;
1361 if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_thresh)
1362 mask |= POLLIN | POLLRDNORM;
1363 if (ENTROPY_BITS(&input_pool) < random_write_wakeup_thresh)
1364 mask |= POLLOUT | POLLWRNORM;
1365 return mask;
1366}
1367
1368static int
1369write_pool(struct entropy_store *r, const char __user *buffer, size_t count)
1370{
1371 size_t bytes;
1372 __u32 buf[16];
1373 const char __user *p = buffer;
1374
1375 while (count > 0) {
1376 bytes = min(count, sizeof(buf));
1377 if (copy_from_user(&buf, p, bytes))
1378 return -EFAULT;
1379
1380 count -= bytes;
1381 p += bytes;
1382
1383 mix_pool_bytes(r, buf, bytes, NULL);
1384 cond_resched();
1385 }
1386
1387 return 0;
1388}
1389
1390static ssize_t random_write(struct file *file, const char __user *buffer,
1391 size_t count, loff_t *ppos)
1392{
1393 size_t ret;
1394
1395 ret = write_pool(&blocking_pool, buffer, count);
1396 if (ret)
1397 return ret;
1398 ret = write_pool(&nonblocking_pool, buffer, count);
1399 if (ret)
1400 return ret;
1401
1402 return (ssize_t)count;
1403}
1404
1405static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1406{
1407 int size, ent_count;
1408 int __user *p = (int __user *)arg;
1409 int retval;
1410
1411 switch (cmd) {
1412 case RNDGETENTCNT:
1413
1414 ent_count = ENTROPY_BITS(&input_pool);
1415 if (put_user(ent_count, p))
1416 return -EFAULT;
1417 return 0;
1418 case RNDADDTOENTCNT:
1419 if (!capable(CAP_SYS_ADMIN))
1420 return -EPERM;
1421 if (get_user(ent_count, p))
1422 return -EFAULT;
1423 credit_entropy_bits_safe(&input_pool, ent_count);
1424 return 0;
1425 case RNDADDENTROPY:
1426 if (!capable(CAP_SYS_ADMIN))
1427 return -EPERM;
1428 if (get_user(ent_count, p++))
1429 return -EFAULT;
1430 if (ent_count < 0)
1431 return -EINVAL;
1432 if (get_user(size, p++))
1433 return -EFAULT;
1434 retval = write_pool(&input_pool, (const char __user *)p,
1435 size);
1436 if (retval < 0)
1437 return retval;
1438 credit_entropy_bits_safe(&input_pool, ent_count);
1439 return 0;
1440 case RNDZAPENTCNT:
1441 case RNDCLEARPOOL:
1442
1443
1444
1445
1446 if (!capable(CAP_SYS_ADMIN))
1447 return -EPERM;
1448 input_pool.entropy_count = 0;
1449 nonblocking_pool.entropy_count = 0;
1450 blocking_pool.entropy_count = 0;
1451 return 0;
1452 default:
1453 return -EINVAL;
1454 }
1455}
1456
1457static int random_fasync(int fd, struct file *filp, int on)
1458{
1459 return fasync_helper(fd, filp, on, &fasync);
1460}
1461
1462const struct file_operations random_fops = {
1463 .read = random_read,
1464 .write = random_write,
1465 .poll = random_poll,
1466 .unlocked_ioctl = random_ioctl,
1467 .fasync = random_fasync,
1468 .llseek = noop_llseek,
1469};
1470
1471const struct file_operations urandom_fops = {
1472 .read = urandom_read,
1473 .write = random_write,
1474 .unlocked_ioctl = random_ioctl,
1475 .fasync = random_fasync,
1476 .llseek = noop_llseek,
1477};
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489void generate_random_uuid(unsigned char uuid_out[16])
1490{
1491 get_random_bytes(uuid_out, 16);
1492
1493 uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40;
1494
1495 uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
1496}
1497EXPORT_SYMBOL(generate_random_uuid);
1498
1499
1500
1501
1502
1503
1504
1505#ifdef CONFIG_SYSCTL
1506
1507#include <linux/sysctl.h>
1508
1509static int min_read_thresh = 8, min_write_thresh;
1510static int max_read_thresh = INPUT_POOL_WORDS * 32;
1511static int max_write_thresh = INPUT_POOL_WORDS * 32;
1512static char sysctl_bootid[16];
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523static int proc_do_uuid(struct ctl_table *table, int write,
1524 void __user *buffer, size_t *lenp, loff_t *ppos)
1525{
1526 struct ctl_table fake_table;
1527 unsigned char buf[64], tmp_uuid[16], *uuid;
1528
1529 uuid = table->data;
1530 if (!uuid) {
1531 uuid = tmp_uuid;
1532 generate_random_uuid(uuid);
1533 } else {
1534 static DEFINE_SPINLOCK(bootid_spinlock);
1535
1536 spin_lock(&bootid_spinlock);
1537 if (!uuid[8])
1538 generate_random_uuid(uuid);
1539 spin_unlock(&bootid_spinlock);
1540 }
1541
1542 sprintf(buf, "%pU", uuid);
1543
1544 fake_table.data = buf;
1545 fake_table.maxlen = sizeof(buf);
1546
1547 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
1548}
1549
1550
1551
1552
1553static int proc_do_entropy(ctl_table *table, int write,
1554 void __user *buffer, size_t *lenp, loff_t *ppos)
1555{
1556 ctl_table fake_table;
1557 int entropy_count;
1558
1559 entropy_count = *(int *)table->data >> ENTROPY_SHIFT;
1560
1561 fake_table.data = &entropy_count;
1562 fake_table.maxlen = sizeof(entropy_count);
1563
1564 return proc_dointvec(&fake_table, write, buffer, lenp, ppos);
1565}
1566
1567static int sysctl_poolsize = INPUT_POOL_WORDS * 32;
1568extern struct ctl_table random_table[];
1569struct ctl_table random_table[] = {
1570 {
1571 .procname = "poolsize",
1572 .data = &sysctl_poolsize,
1573 .maxlen = sizeof(int),
1574 .mode = 0444,
1575 .proc_handler = proc_dointvec,
1576 },
1577 {
1578 .procname = "entropy_avail",
1579 .maxlen = sizeof(int),
1580 .mode = 0444,
1581 .proc_handler = proc_do_entropy,
1582 .data = &input_pool.entropy_count,
1583 },
1584 {
1585 .procname = "read_wakeup_threshold",
1586 .data = &random_read_wakeup_thresh,
1587 .maxlen = sizeof(int),
1588 .mode = 0644,
1589 .proc_handler = proc_dointvec_minmax,
1590 .extra1 = &min_read_thresh,
1591 .extra2 = &max_read_thresh,
1592 },
1593 {
1594 .procname = "write_wakeup_threshold",
1595 .data = &random_write_wakeup_thresh,
1596 .maxlen = sizeof(int),
1597 .mode = 0644,
1598 .proc_handler = proc_dointvec_minmax,
1599 .extra1 = &min_write_thresh,
1600 .extra2 = &max_write_thresh,
1601 },
1602 {
1603 .procname = "urandom_min_reseed_secs",
1604 .data = &random_min_urandom_seed,
1605 .maxlen = sizeof(int),
1606 .mode = 0644,
1607 .proc_handler = proc_dointvec,
1608 },
1609 {
1610 .procname = "boot_id",
1611 .data = &sysctl_bootid,
1612 .maxlen = 16,
1613 .mode = 0444,
1614 .proc_handler = proc_do_uuid,
1615 },
1616 {
1617 .procname = "uuid",
1618 .maxlen = 16,
1619 .mode = 0444,
1620 .proc_handler = proc_do_uuid,
1621 },
1622 { }
1623};
1624#endif
1625
1626static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned;
1627
1628int random_int_secret_init(void)
1629{
1630 get_random_bytes(random_int_secret, sizeof(random_int_secret));
1631 return 0;
1632}
1633
1634
1635
1636
1637
1638
1639
1640static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash);
1641unsigned int get_random_int(void)
1642{
1643 __u32 *hash;
1644 unsigned int ret;
1645
1646 if (arch_get_random_int(&ret))
1647 return ret;
1648
1649 hash = get_cpu_var(get_random_int_hash);
1650
1651 hash[0] += current->pid + jiffies + random_get_entropy();
1652 md5_transform(hash, random_int_secret);
1653 ret = hash[0];
1654 put_cpu_var(get_random_int_hash);
1655
1656 return ret;
1657}
1658EXPORT_SYMBOL(get_random_int);
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669unsigned long
1670randomize_range(unsigned long start, unsigned long end, unsigned long len)
1671{
1672 unsigned long range = end - len - start;
1673
1674 if (end <= start + len)
1675 return 0;
1676 return PAGE_ALIGN(get_random_int() % range + start);
1677}
1678