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
259#ifdef CONFIG_GENERIC_HARDIRQS
260# include <linux/irq.h>
261#endif
262
263#include <asm/processor.h>
264#include <asm/uaccess.h>
265#include <asm/irq.h>
266#include <asm/irq_regs.h>
267#include <asm/io.h>
268
269#define CREATE_TRACE_POINTS
270#include <trace/events/random.h>
271
272
273
274
275#define INPUT_POOL_WORDS 128
276#define OUTPUT_POOL_WORDS 32
277#define SEC_XFER_SIZE 512
278#define EXTRACT_SIZE 10
279
280#define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long))
281
282
283
284
285
286static int random_read_wakeup_thresh = 64;
287
288
289
290
291
292
293static int random_write_wakeup_thresh = 128;
294
295
296
297
298
299
300static int trickle_thresh __read_mostly = INPUT_POOL_WORDS * 28;
301
302static DEFINE_PER_CPU(int, trickle_count);
303
304
305
306
307
308
309
310
311
312static struct poolinfo {
313 int poolwords;
314 int tap1, tap2, tap3, tap4, tap5;
315} poolinfo_table[] = {
316
317 { 128, 103, 76, 51, 25, 1 },
318
319 { 32, 26, 20, 14, 7, 1 },
320#if 0
321
322 { 2048, 1638, 1231, 819, 411, 1 },
323
324
325 { 1024, 817, 615, 412, 204, 1 },
326
327
328 { 1024, 819, 616, 410, 207, 2 },
329
330
331 { 512, 411, 308, 208, 104, 1 },
332
333
334 { 512, 409, 307, 206, 102, 2 },
335
336 { 512, 409, 309, 205, 103, 2 },
337
338
339 { 256, 205, 155, 101, 52, 1 },
340
341
342 { 128, 103, 78, 51, 27, 2 },
343
344
345 { 64, 52, 39, 26, 14, 1 },
346#endif
347};
348
349#define POOLBITS poolwords*32
350#define POOLBYTES poolwords*4
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
399static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
400static struct fasync_struct *fasync;
401
402static bool debug;
403module_param(debug, bool, 0644);
404#define DEBUG_ENT(fmt, arg...) do { \
405 if (debug) \
406 printk(KERN_DEBUG "random %04d %04d %04d: " \
407 fmt,\
408 input_pool.entropy_count,\
409 blocking_pool.entropy_count,\
410 nonblocking_pool.entropy_count,\
411 ## arg); } while (0)
412
413
414
415
416
417
418
419
420struct entropy_store;
421struct entropy_store {
422
423 struct poolinfo *poolinfo;
424 __u32 *pool;
425 const char *name;
426 struct entropy_store *pull;
427 int limit;
428
429
430 spinlock_t lock;
431 unsigned add_ptr;
432 unsigned input_rotate;
433 int entropy_count;
434 int entropy_total;
435 unsigned int initialized:1;
436 bool last_data_init;
437 __u8 last_data[EXTRACT_SIZE];
438};
439
440static __u32 input_pool_data[INPUT_POOL_WORDS];
441static __u32 blocking_pool_data[OUTPUT_POOL_WORDS];
442static __u32 nonblocking_pool_data[OUTPUT_POOL_WORDS];
443
444static struct entropy_store input_pool = {
445 .poolinfo = &poolinfo_table[0],
446 .name = "input",
447 .limit = 1,
448 .lock = __SPIN_LOCK_UNLOCKED(&input_pool.lock),
449 .pool = input_pool_data
450};
451
452static struct entropy_store blocking_pool = {
453 .poolinfo = &poolinfo_table[1],
454 .name = "blocking",
455 .limit = 1,
456 .pull = &input_pool,
457 .lock = __SPIN_LOCK_UNLOCKED(&blocking_pool.lock),
458 .pool = blocking_pool_data
459};
460
461static struct entropy_store nonblocking_pool = {
462 .poolinfo = &poolinfo_table[1],
463 .name = "nonblocking",
464 .pull = &input_pool,
465 .lock = __SPIN_LOCK_UNLOCKED(&nonblocking_pool.lock),
466 .pool = nonblocking_pool_data
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 & 31);
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 += i ? 7 : 14;
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, const void *in, int nbytes)
568{
569 const char *bytes = in;
570 __u32 w;
571 unsigned i = f->count;
572 unsigned input_rotate = f->rotate;
573
574 while (nbytes--) {
575 w = rol32(*bytes++, input_rotate & 31) ^ f->pool[i & 3] ^
576 f->pool[(i + 1) & 3];
577 f->pool[i & 3] = (w >> 3) ^ twist_table[w & 7];
578 input_rotate += (i++ & 3) ? 7 : 14;
579 }
580 f->count = i;
581 f->rotate = input_rotate;
582}
583
584
585
586
587static void credit_entropy_bits(struct entropy_store *r, int nbits)
588{
589 int entropy_count, orig;
590
591 if (!nbits)
592 return;
593
594 DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name);
595retry:
596 entropy_count = orig = ACCESS_ONCE(r->entropy_count);
597 entropy_count += nbits;
598
599 if (entropy_count < 0) {
600 DEBUG_ENT("negative entropy/overflow\n");
601 entropy_count = 0;
602 } else if (entropy_count > r->poolinfo->POOLBITS)
603 entropy_count = r->poolinfo->POOLBITS;
604 if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
605 goto retry;
606
607 if (!r->initialized && nbits > 0) {
608 r->entropy_total += nbits;
609 if (r->entropy_total > 128)
610 r->initialized = 1;
611 }
612
613 trace_credit_entropy_bits(r->name, nbits, entropy_count,
614 r->entropy_total, _RET_IP_);
615
616
617 if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) {
618 wake_up_interruptible(&random_read_wait);
619 kill_fasync(&fasync, SIGIO, POLL_IN);
620 }
621}
622
623
624
625
626
627
628
629
630struct timer_rand_state {
631 cycles_t last_time;
632 long last_delta, last_delta2;
633 unsigned dont_count_entropy:1;
634};
635
636
637
638
639
640
641
642
643
644void add_device_randomness(const void *buf, unsigned int size)
645{
646 unsigned long time = get_cycles() ^ jiffies;
647
648 mix_pool_bytes(&input_pool, buf, size, NULL);
649 mix_pool_bytes(&input_pool, &time, sizeof(time), NULL);
650 mix_pool_bytes(&nonblocking_pool, buf, size, NULL);
651 mix_pool_bytes(&nonblocking_pool, &time, sizeof(time), NULL);
652}
653EXPORT_SYMBOL(add_device_randomness);
654
655static struct timer_rand_state input_timer_state;
656
657
658
659
660
661
662
663
664
665
666
667static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
668{
669 struct {
670 long jiffies;
671 unsigned cycles;
672 unsigned num;
673 } sample;
674 long delta, delta2, delta3;
675
676 preempt_disable();
677
678 if (input_pool.entropy_count > trickle_thresh &&
679 ((__this_cpu_inc_return(trickle_count) - 1) & 0xfff))
680 goto out;
681
682 sample.jiffies = jiffies;
683 sample.cycles = get_cycles();
684 sample.num = num;
685 mix_pool_bytes(&input_pool, &sample, sizeof(sample), NULL);
686
687
688
689
690
691
692
693 if (!state->dont_count_entropy) {
694 delta = sample.jiffies - state->last_time;
695 state->last_time = sample.jiffies;
696
697 delta2 = delta - state->last_delta;
698 state->last_delta = delta;
699
700 delta3 = delta2 - state->last_delta2;
701 state->last_delta2 = delta2;
702
703 if (delta < 0)
704 delta = -delta;
705 if (delta2 < 0)
706 delta2 = -delta2;
707 if (delta3 < 0)
708 delta3 = -delta3;
709 if (delta > delta2)
710 delta = delta2;
711 if (delta > delta3)
712 delta = delta3;
713
714
715
716
717
718
719 credit_entropy_bits(&input_pool,
720 min_t(int, fls(delta>>1), 11));
721 }
722out:
723 preempt_enable();
724}
725
726void add_input_randomness(unsigned int type, unsigned int code,
727 unsigned int value)
728{
729 static unsigned char last_value;
730
731
732 if (value == last_value)
733 return;
734
735 DEBUG_ENT("input event\n");
736 last_value = value;
737 add_timer_randomness(&input_timer_state,
738 (type << 4) ^ code ^ (code >> 4) ^ value);
739}
740EXPORT_SYMBOL_GPL(add_input_randomness);
741
742static DEFINE_PER_CPU(struct fast_pool, irq_randomness);
743
744void add_interrupt_randomness(int irq, int irq_flags)
745{
746 struct entropy_store *r;
747 struct fast_pool *fast_pool = &__get_cpu_var(irq_randomness);
748 struct pt_regs *regs = get_irq_regs();
749 unsigned long now = jiffies;
750 __u32 input[4], cycles = get_cycles();
751
752 input[0] = cycles ^ jiffies;
753 input[1] = irq;
754 if (regs) {
755 __u64 ip = instruction_pointer(regs);
756 input[2] = ip;
757 input[3] = ip >> 32;
758 }
759
760 fast_mix(fast_pool, input, sizeof(input));
761
762 if ((fast_pool->count & 1023) &&
763 !time_after(now, fast_pool->last + HZ))
764 return;
765
766 fast_pool->last = now;
767
768 r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
769 __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool), NULL);
770
771
772
773
774
775 if (cycles == 0) {
776 if (irq_flags & __IRQF_TIMER) {
777 if (fast_pool->last_timer_intr)
778 return;
779 fast_pool->last_timer_intr = 1;
780 } else
781 fast_pool->last_timer_intr = 0;
782 }
783 credit_entropy_bits(r, 1);
784}
785
786#ifdef CONFIG_BLOCK
787void add_disk_randomness(struct gendisk *disk)
788{
789 if (!disk || !disk->random)
790 return;
791
792 DEBUG_ENT("disk event %d:%d\n",
793 MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)));
794
795 add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
796}
797#endif
798
799
800
801
802
803
804
805static ssize_t extract_entropy(struct entropy_store *r, void *buf,
806 size_t nbytes, int min, int rsvd);
807
808
809
810
811
812
813static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
814{
815 __u32 tmp[OUTPUT_POOL_WORDS];
816
817 if (r->pull && r->entropy_count < nbytes * 8 &&
818 r->entropy_count < r->poolinfo->POOLBITS) {
819
820 int rsvd = r->limit ? 0 : random_read_wakeup_thresh/4;
821 int bytes = nbytes;
822
823
824 bytes = max_t(int, bytes, random_read_wakeup_thresh / 8);
825
826 bytes = min_t(int, bytes, sizeof(tmp));
827
828 DEBUG_ENT("going to reseed %s with %d bits "
829 "(%zu of %d requested)\n",
830 r->name, bytes * 8, nbytes * 8, r->entropy_count);
831
832 bytes = extract_entropy(r->pull, tmp, bytes,
833 random_read_wakeup_thresh / 8, rsvd);
834 mix_pool_bytes(r, tmp, bytes, NULL);
835 credit_entropy_bits(r, bytes*8);
836 }
837}
838
839
840
841
842
843
844
845
846
847
848
849
850
851static size_t account(struct entropy_store *r, size_t nbytes, int min,
852 int reserved)
853{
854 unsigned long flags;
855 int wakeup_write = 0;
856
857
858 spin_lock_irqsave(&r->lock, flags);
859
860 BUG_ON(r->entropy_count > r->poolinfo->POOLBITS);
861 DEBUG_ENT("trying to extract %zu bits from %s\n",
862 nbytes * 8, r->name);
863
864
865 if (r->entropy_count / 8 < min + reserved) {
866 nbytes = 0;
867 } else {
868
869 if (r->limit && nbytes + reserved >= r->entropy_count / 8)
870 nbytes = r->entropy_count/8 - reserved;
871
872 if (r->entropy_count / 8 >= nbytes + reserved)
873 r->entropy_count -= nbytes*8;
874 else
875 r->entropy_count = reserved;
876
877 if (r->entropy_count < random_write_wakeup_thresh)
878 wakeup_write = 1;
879 }
880
881 DEBUG_ENT("debiting %zu entropy credits from %s%s\n",
882 nbytes * 8, r->name, r->limit ? "" : " (unlimited)");
883
884 spin_unlock_irqrestore(&r->lock, flags);
885
886 if (wakeup_write) {
887 wake_up_interruptible(&random_write_wait);
888 kill_fasync(&fasync, SIGIO, POLL_OUT);
889 }
890
891 return nbytes;
892}
893
894static void extract_buf(struct entropy_store *r, __u8 *out)
895{
896 int i;
897 union {
898 __u32 w[5];
899 unsigned long l[LONGS(EXTRACT_SIZE)];
900 } hash;
901 __u32 workspace[SHA_WORKSPACE_WORDS];
902 __u8 extract[64];
903 unsigned long flags;
904
905
906 sha_init(hash.w);
907 spin_lock_irqsave(&r->lock, flags);
908 for (i = 0; i < r->poolinfo->poolwords; i += 16)
909 sha_transform(hash.w, (__u8 *)(r->pool + i), workspace);
910
911
912
913
914
915
916
917
918
919
920 __mix_pool_bytes(r, hash.w, sizeof(hash.w), extract);
921 spin_unlock_irqrestore(&r->lock, flags);
922
923
924
925
926
927 sha_transform(hash.w, extract, workspace);
928 memset(extract, 0, sizeof(extract));
929 memset(workspace, 0, sizeof(workspace));
930
931
932
933
934
935
936 hash.w[0] ^= hash.w[3];
937 hash.w[1] ^= hash.w[4];
938 hash.w[2] ^= rol32(hash.w[2], 16);
939
940
941
942
943
944 for (i = 0; i < LONGS(EXTRACT_SIZE); i++) {
945 unsigned long v;
946 if (!arch_get_random_long(&v))
947 break;
948 hash.l[i] ^= v;
949 }
950
951 memcpy(out, &hash, EXTRACT_SIZE);
952 memset(&hash, 0, sizeof(hash));
953}
954
955static ssize_t extract_entropy(struct entropy_store *r, void *buf,
956 size_t nbytes, int min, int reserved)
957{
958 ssize_t ret = 0, i;
959 __u8 tmp[EXTRACT_SIZE];
960
961
962 if (fips_enabled && !r->last_data_init)
963 nbytes += EXTRACT_SIZE;
964
965 trace_extract_entropy(r->name, nbytes, r->entropy_count, _RET_IP_);
966 xfer_secondary_pool(r, nbytes);
967 nbytes = account(r, nbytes, min, reserved);
968
969 while (nbytes) {
970 extract_buf(r, tmp);
971
972 if (fips_enabled) {
973 unsigned long flags;
974
975
976
977 if (!r->last_data_init) {
978 spin_lock_irqsave(&r->lock, flags);
979 memcpy(r->last_data, tmp, EXTRACT_SIZE);
980 r->last_data_init = true;
981 nbytes -= EXTRACT_SIZE;
982 spin_unlock_irqrestore(&r->lock, flags);
983 extract_buf(r, tmp);
984 }
985
986 spin_lock_irqsave(&r->lock, flags);
987 if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
988 panic("Hardware RNG duplicated output!\n");
989 memcpy(r->last_data, tmp, EXTRACT_SIZE);
990 spin_unlock_irqrestore(&r->lock, flags);
991 }
992 i = min_t(int, nbytes, EXTRACT_SIZE);
993 memcpy(buf, tmp, i);
994 nbytes -= i;
995 buf += i;
996 ret += i;
997 }
998
999
1000 memset(tmp, 0, sizeof(tmp));
1001
1002 return ret;
1003}
1004
1005static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
1006 size_t nbytes)
1007{
1008 ssize_t ret = 0, i;
1009 __u8 tmp[EXTRACT_SIZE];
1010
1011 trace_extract_entropy_user(r->name, nbytes, r->entropy_count, _RET_IP_);
1012 xfer_secondary_pool(r, nbytes);
1013 nbytes = account(r, nbytes, 0, 0);
1014
1015 while (nbytes) {
1016 if (need_resched()) {
1017 if (signal_pending(current)) {
1018 if (ret == 0)
1019 ret = -ERESTARTSYS;
1020 break;
1021 }
1022 schedule();
1023 }
1024
1025 extract_buf(r, tmp);
1026 i = min_t(int, nbytes, EXTRACT_SIZE);
1027 if (copy_to_user(buf, tmp, i)) {
1028 ret = -EFAULT;
1029 break;
1030 }
1031
1032 nbytes -= i;
1033 buf += i;
1034 ret += i;
1035 }
1036
1037
1038 memset(tmp, 0, sizeof(tmp));
1039
1040 return ret;
1041}
1042
1043
1044
1045
1046
1047
1048
1049void get_random_bytes(void *buf, int nbytes)
1050{
1051 extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);
1052}
1053EXPORT_SYMBOL(get_random_bytes);
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065void get_random_bytes_arch(void *buf, int nbytes)
1066{
1067 char *p = buf;
1068
1069 trace_get_random_bytes(nbytes, _RET_IP_);
1070 while (nbytes) {
1071 unsigned long v;
1072 int chunk = min(nbytes, (int)sizeof(unsigned long));
1073
1074 if (!arch_get_random_long(&v))
1075 break;
1076
1077 memcpy(p, &v, chunk);
1078 p += chunk;
1079 nbytes -= chunk;
1080 }
1081
1082 if (nbytes)
1083 extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
1084}
1085EXPORT_SYMBOL(get_random_bytes_arch);
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097static void init_std_data(struct entropy_store *r)
1098{
1099 int i;
1100 ktime_t now = ktime_get_real();
1101 unsigned long rv;
1102
1103 r->entropy_count = 0;
1104 r->entropy_total = 0;
1105 r->last_data_init = false;
1106 mix_pool_bytes(r, &now, sizeof(now), NULL);
1107 for (i = r->poolinfo->POOLBYTES; i > 0; i -= sizeof(rv)) {
1108 if (!arch_get_random_long(&rv))
1109 break;
1110 mix_pool_bytes(r, &rv, sizeof(rv), NULL);
1111 }
1112 mix_pool_bytes(r, utsname(), sizeof(*(utsname())), NULL);
1113}
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125static int rand_initialize(void)
1126{
1127 init_std_data(&input_pool);
1128 init_std_data(&blocking_pool);
1129 init_std_data(&nonblocking_pool);
1130 return 0;
1131}
1132module_init(rand_initialize);
1133
1134#ifdef CONFIG_BLOCK
1135void rand_initialize_disk(struct gendisk *disk)
1136{
1137 struct timer_rand_state *state;
1138
1139
1140
1141
1142
1143 state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
1144 if (state)
1145 disk->random = state;
1146}
1147#endif
1148
1149static ssize_t
1150random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1151{
1152 ssize_t n, retval = 0, count = 0;
1153
1154 if (nbytes == 0)
1155 return 0;
1156
1157 while (nbytes > 0) {
1158 n = nbytes;
1159 if (n > SEC_XFER_SIZE)
1160 n = SEC_XFER_SIZE;
1161
1162 DEBUG_ENT("reading %zu bits\n", n*8);
1163
1164 n = extract_entropy_user(&blocking_pool, buf, n);
1165
1166 if (n < 0) {
1167 retval = n;
1168 break;
1169 }
1170
1171 DEBUG_ENT("read got %zd bits (%zd still needed)\n",
1172 n*8, (nbytes-n)*8);
1173
1174 if (n == 0) {
1175 if (file->f_flags & O_NONBLOCK) {
1176 retval = -EAGAIN;
1177 break;
1178 }
1179
1180 DEBUG_ENT("sleeping?\n");
1181
1182 wait_event_interruptible(random_read_wait,
1183 input_pool.entropy_count >=
1184 random_read_wakeup_thresh);
1185
1186 DEBUG_ENT("awake\n");
1187
1188 if (signal_pending(current)) {
1189 retval = -ERESTARTSYS;
1190 break;
1191 }
1192
1193 continue;
1194 }
1195
1196 count += n;
1197 buf += n;
1198 nbytes -= n;
1199 break;
1200
1201 }
1202
1203 return (count ? count : retval);
1204}
1205
1206static ssize_t
1207urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1208{
1209 return extract_entropy_user(&nonblocking_pool, buf, nbytes);
1210}
1211
1212static unsigned int
1213random_poll(struct file *file, poll_table * wait)
1214{
1215 unsigned int mask;
1216
1217 poll_wait(file, &random_read_wait, wait);
1218 poll_wait(file, &random_write_wait, wait);
1219 mask = 0;
1220 if (input_pool.entropy_count >= random_read_wakeup_thresh)
1221 mask |= POLLIN | POLLRDNORM;
1222 if (input_pool.entropy_count < random_write_wakeup_thresh)
1223 mask |= POLLOUT | POLLWRNORM;
1224 return mask;
1225}
1226
1227static int
1228write_pool(struct entropy_store *r, const char __user *buffer, size_t count)
1229{
1230 size_t bytes;
1231 __u32 buf[16];
1232 const char __user *p = buffer;
1233
1234 while (count > 0) {
1235 bytes = min(count, sizeof(buf));
1236 if (copy_from_user(&buf, p, bytes))
1237 return -EFAULT;
1238
1239 count -= bytes;
1240 p += bytes;
1241
1242 mix_pool_bytes(r, buf, bytes, NULL);
1243 cond_resched();
1244 }
1245
1246 return 0;
1247}
1248
1249static ssize_t random_write(struct file *file, const char __user *buffer,
1250 size_t count, loff_t *ppos)
1251{
1252 size_t ret;
1253
1254 ret = write_pool(&blocking_pool, buffer, count);
1255 if (ret)
1256 return ret;
1257 ret = write_pool(&nonblocking_pool, buffer, count);
1258 if (ret)
1259 return ret;
1260
1261 return (ssize_t)count;
1262}
1263
1264static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1265{
1266 int size, ent_count;
1267 int __user *p = (int __user *)arg;
1268 int retval;
1269
1270 switch (cmd) {
1271 case RNDGETENTCNT:
1272
1273 if (put_user(input_pool.entropy_count, p))
1274 return -EFAULT;
1275 return 0;
1276 case RNDADDTOENTCNT:
1277 if (!capable(CAP_SYS_ADMIN))
1278 return -EPERM;
1279 if (get_user(ent_count, p))
1280 return -EFAULT;
1281 credit_entropy_bits(&input_pool, ent_count);
1282 return 0;
1283 case RNDADDENTROPY:
1284 if (!capable(CAP_SYS_ADMIN))
1285 return -EPERM;
1286 if (get_user(ent_count, p++))
1287 return -EFAULT;
1288 if (ent_count < 0)
1289 return -EINVAL;
1290 if (get_user(size, p++))
1291 return -EFAULT;
1292 retval = write_pool(&input_pool, (const char __user *)p,
1293 size);
1294 if (retval < 0)
1295 return retval;
1296 credit_entropy_bits(&input_pool, ent_count);
1297 return 0;
1298 case RNDZAPENTCNT:
1299 case RNDCLEARPOOL:
1300
1301 if (!capable(CAP_SYS_ADMIN))
1302 return -EPERM;
1303 rand_initialize();
1304 return 0;
1305 default:
1306 return -EINVAL;
1307 }
1308}
1309
1310static int random_fasync(int fd, struct file *filp, int on)
1311{
1312 return fasync_helper(fd, filp, on, &fasync);
1313}
1314
1315const struct file_operations random_fops = {
1316 .read = random_read,
1317 .write = random_write,
1318 .poll = random_poll,
1319 .unlocked_ioctl = random_ioctl,
1320 .fasync = random_fasync,
1321 .llseek = noop_llseek,
1322};
1323
1324const struct file_operations urandom_fops = {
1325 .read = urandom_read,
1326 .write = random_write,
1327 .unlocked_ioctl = random_ioctl,
1328 .fasync = random_fasync,
1329 .llseek = noop_llseek,
1330};
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342void generate_random_uuid(unsigned char uuid_out[16])
1343{
1344 get_random_bytes(uuid_out, 16);
1345
1346 uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40;
1347
1348 uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
1349}
1350EXPORT_SYMBOL(generate_random_uuid);
1351
1352
1353
1354
1355
1356
1357
1358#ifdef CONFIG_SYSCTL
1359
1360#include <linux/sysctl.h>
1361
1362static int min_read_thresh = 8, min_write_thresh;
1363static int max_read_thresh = INPUT_POOL_WORDS * 32;
1364static int max_write_thresh = INPUT_POOL_WORDS * 32;
1365static char sysctl_bootid[16];
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376static int proc_do_uuid(ctl_table *table, int write,
1377 void __user *buffer, size_t *lenp, loff_t *ppos)
1378{
1379 ctl_table fake_table;
1380 unsigned char buf[64], tmp_uuid[16], *uuid;
1381
1382 uuid = table->data;
1383 if (!uuid) {
1384 uuid = tmp_uuid;
1385 generate_random_uuid(uuid);
1386 } else {
1387 static DEFINE_SPINLOCK(bootid_spinlock);
1388
1389 spin_lock(&bootid_spinlock);
1390 if (!uuid[8])
1391 generate_random_uuid(uuid);
1392 spin_unlock(&bootid_spinlock);
1393 }
1394
1395 sprintf(buf, "%pU", uuid);
1396
1397 fake_table.data = buf;
1398 fake_table.maxlen = sizeof(buf);
1399
1400 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
1401}
1402
1403static int sysctl_poolsize = INPUT_POOL_WORDS * 32;
1404extern ctl_table random_table[];
1405ctl_table random_table[] = {
1406 {
1407 .procname = "poolsize",
1408 .data = &sysctl_poolsize,
1409 .maxlen = sizeof(int),
1410 .mode = 0444,
1411 .proc_handler = proc_dointvec,
1412 },
1413 {
1414 .procname = "entropy_avail",
1415 .maxlen = sizeof(int),
1416 .mode = 0444,
1417 .proc_handler = proc_dointvec,
1418 .data = &input_pool.entropy_count,
1419 },
1420 {
1421 .procname = "read_wakeup_threshold",
1422 .data = &random_read_wakeup_thresh,
1423 .maxlen = sizeof(int),
1424 .mode = 0644,
1425 .proc_handler = proc_dointvec_minmax,
1426 .extra1 = &min_read_thresh,
1427 .extra2 = &max_read_thresh,
1428 },
1429 {
1430 .procname = "write_wakeup_threshold",
1431 .data = &random_write_wakeup_thresh,
1432 .maxlen = sizeof(int),
1433 .mode = 0644,
1434 .proc_handler = proc_dointvec_minmax,
1435 .extra1 = &min_write_thresh,
1436 .extra2 = &max_write_thresh,
1437 },
1438 {
1439 .procname = "boot_id",
1440 .data = &sysctl_bootid,
1441 .maxlen = 16,
1442 .mode = 0444,
1443 .proc_handler = proc_do_uuid,
1444 },
1445 {
1446 .procname = "uuid",
1447 .maxlen = 16,
1448 .mode = 0444,
1449 .proc_handler = proc_do_uuid,
1450 },
1451 { }
1452};
1453#endif
1454
1455static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned;
1456
1457static int __init random_int_secret_init(void)
1458{
1459 get_random_bytes(random_int_secret, sizeof(random_int_secret));
1460 return 0;
1461}
1462late_initcall(random_int_secret_init);
1463
1464
1465
1466
1467
1468
1469
1470static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash);
1471unsigned int get_random_int(void)
1472{
1473 __u32 *hash;
1474 unsigned int ret;
1475
1476 if (arch_get_random_int(&ret))
1477 return ret;
1478
1479 hash = get_cpu_var(get_random_int_hash);
1480
1481 hash[0] += current->pid + jiffies + get_cycles();
1482 md5_transform(hash, random_int_secret);
1483 ret = hash[0];
1484 put_cpu_var(get_random_int_hash);
1485
1486 return ret;
1487}
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498unsigned long
1499randomize_range(unsigned long start, unsigned long end, unsigned long len)
1500{
1501 unsigned long range = end - len - start;
1502
1503 if (end <= start + len)
1504 return 0;
1505 return PAGE_ALIGN(get_random_int() % range + start);
1506}
1507