aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/powerpc/include/asm/archrandom.h18
-rw-r--r--arch/x86/include/asm/archrandom.h42
-rw-r--r--drivers/char/random.c244
-rw-r--r--include/linux/random.h16
4 files changed, 211 insertions, 109 deletions
diff --git a/arch/powerpc/include/asm/archrandom.h b/arch/powerpc/include/asm/archrandom.h
index d853d163ba47..bde531103638 100644
--- a/arch/powerpc/include/asm/archrandom.h
+++ b/arch/powerpc/include/asm/archrandom.h
@@ -25,8 +25,26 @@ static inline int arch_get_random_int(unsigned int *v)
25 return rc; 25 return rc;
26} 26}
27 27
28static inline int arch_has_random(void)
29{
30 return !!ppc_md.get_random_long;
31}
32
28int powernv_get_random_long(unsigned long *v); 33int powernv_get_random_long(unsigned long *v);
29 34
35static inline int arch_get_random_seed_long(unsigned long *v)
36{
37 return 0;
38}
39static inline int arch_get_random_seed_int(unsigned int *v)
40{
41 return 0;
42}
43static inline int arch_has_random_seed(void)
44{
45 return 0;
46}
47
30#endif /* CONFIG_ARCH_RANDOM */ 48#endif /* CONFIG_ARCH_RANDOM */
31 49
32#endif /* _ASM_POWERPC_ARCHRANDOM_H */ 50#endif /* _ASM_POWERPC_ARCHRANDOM_H */
diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h
index e6a92455740e..69f1366f1aa3 100644
--- a/arch/x86/include/asm/archrandom.h
+++ b/arch/x86/include/asm/archrandom.h
@@ -1,7 +1,7 @@
1/* 1/*
2 * This file is part of the Linux kernel. 2 * This file is part of the Linux kernel.
3 * 3 *
4 * Copyright (c) 2011, Intel Corporation 4 * Copyright (c) 2011-2014, Intel Corporation
5 * Authors: Fenghua Yu <fenghua.yu@intel.com>, 5 * Authors: Fenghua Yu <fenghua.yu@intel.com>,
6 * H. Peter Anvin <hpa@linux.intel.com> 6 * H. Peter Anvin <hpa@linux.intel.com>
7 * 7 *
@@ -31,10 +31,13 @@
31#define RDRAND_RETRY_LOOPS 10 31#define RDRAND_RETRY_LOOPS 10
32 32
33#define RDRAND_INT ".byte 0x0f,0xc7,0xf0" 33#define RDRAND_INT ".byte 0x0f,0xc7,0xf0"
34#define RDSEED_INT ".byte 0x0f,0xc7,0xf8"
34#ifdef CONFIG_X86_64 35#ifdef CONFIG_X86_64
35# define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0" 36# define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
37# define RDSEED_LONG ".byte 0x48,0x0f,0xc7,0xf8"
36#else 38#else
37# define RDRAND_LONG RDRAND_INT 39# define RDRAND_LONG RDRAND_INT
40# define RDSEED_LONG RDSEED_INT
38#endif 41#endif
39 42
40#ifdef CONFIG_ARCH_RANDOM 43#ifdef CONFIG_ARCH_RANDOM
@@ -53,6 +56,16 @@ static inline int rdrand_long(unsigned long *v)
53 return ok; 56 return ok;
54} 57}
55 58
59/* A single attempt at RDSEED */
60static inline bool rdseed_long(unsigned long *v)
61{
62 unsigned char ok;
63 asm volatile(RDSEED_LONG "\n\t"
64 "setc %0"
65 : "=qm" (ok), "=a" (*v));
66 return ok;
67}
68
56#define GET_RANDOM(name, type, rdrand, nop) \ 69#define GET_RANDOM(name, type, rdrand, nop) \
57static inline int name(type *v) \ 70static inline int name(type *v) \
58{ \ 71{ \
@@ -70,18 +83,40 @@ static inline int name(type *v) \
70 return ok; \ 83 return ok; \
71} 84}
72 85
86#define GET_SEED(name, type, rdseed, nop) \
87static inline int name(type *v) \
88{ \
89 unsigned char ok; \
90 alternative_io("movb $0, %0\n\t" \
91 nop, \
92 rdseed "\n\t" \
93 "setc %0", \
94 X86_FEATURE_RDSEED, \
95 ASM_OUTPUT2("=q" (ok), "=a" (*v))); \
96 return ok; \
97}
98
73#ifdef CONFIG_X86_64 99#ifdef CONFIG_X86_64
74 100
75GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP5); 101GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP5);
76GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP4); 102GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP4);
77 103
104GET_SEED(arch_get_random_seed_long, unsigned long, RDSEED_LONG, ASM_NOP5);
105GET_SEED(arch_get_random_seed_int, unsigned int, RDSEED_INT, ASM_NOP4);
106
78#else 107#else
79 108
80GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP3); 109GET_RANDOM(arch_get_random_long, unsigned long, RDRAND_LONG, ASM_NOP3);
81GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP3); 110GET_RANDOM(arch_get_random_int, unsigned int, RDRAND_INT, ASM_NOP3);
82 111
112GET_SEED(arch_get_random_seed_long, unsigned long, RDSEED_LONG, ASM_NOP4);
113GET_SEED(arch_get_random_seed_int, unsigned int, RDSEED_INT, ASM_NOP4);
114
83#endif /* CONFIG_X86_64 */ 115#endif /* CONFIG_X86_64 */
84 116
117#define arch_has_random() static_cpu_has(X86_FEATURE_RDRAND)
118#define arch_has_random_seed() static_cpu_has(X86_FEATURE_RDSEED)
119
85#else 120#else
86 121
87static inline int rdrand_long(unsigned long *v) 122static inline int rdrand_long(unsigned long *v)
@@ -89,6 +124,11 @@ static inline int rdrand_long(unsigned long *v)
89 return 0; 124 return 0;
90} 125}
91 126
127static inline bool rdseed_long(unsigned long *v)
128{
129 return 0;
130}
131
92#endif /* CONFIG_ARCH_RANDOM */ 132#endif /* CONFIG_ARCH_RANDOM */
93 133
94extern void x86_init_rdrand(struct cpuinfo_x86 *c); 134extern void x86_init_rdrand(struct cpuinfo_x86 *c);
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 429b75bb60e8..6b75713d953a 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -295,17 +295,17 @@
295 * The minimum number of bits of entropy before we wake up a read on 295 * The minimum number of bits of entropy before we wake up a read on
296 * /dev/random. Should be enough to do a significant reseed. 296 * /dev/random. Should be enough to do a significant reseed.
297 */ 297 */
298static int random_read_wakeup_thresh = 64; 298static int random_read_wakeup_bits = 64;
299 299
300/* 300/*
301 * If the entropy count falls under this number of bits, then we 301 * If the entropy count falls under this number of bits, then we
302 * should wake up processes which are selecting or polling on write 302 * should wake up processes which are selecting or polling on write
303 * access to /dev/random. 303 * access to /dev/random.
304 */ 304 */
305static int random_write_wakeup_thresh = 28 * OUTPUT_POOL_WORDS; 305static int random_write_wakeup_bits = 28 * OUTPUT_POOL_WORDS;
306 306
307/* 307/*
308 * The minimum number of seconds between urandom pool resending. We 308 * The minimum number of seconds between urandom pool reseeding. We
309 * do this to limit the amount of entropy that can be drained from the 309 * do this to limit the amount of entropy that can be drained from the
310 * input pool even if there are heavy demands on /dev/urandom. 310 * input pool even if there are heavy demands on /dev/urandom.
311 */ 311 */
@@ -322,7 +322,7 @@ static int random_min_urandom_seed = 60;
322 * Register. (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR 322 * Register. (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR
323 * generators. ACM Transactions on Modeling and Computer Simulation 323 * generators. ACM Transactions on Modeling and Computer Simulation
324 * 2(3):179-194. Also see M. Matsumoto & Y. Kurita, 1994. Twisted 324 * 2(3):179-194. Also see M. Matsumoto & Y. Kurita, 1994. Twisted
325 * GFSR generators II. ACM Transactions on Mdeling and Computer 325 * GFSR generators II. ACM Transactions on Modeling and Computer
326 * Simulation 4:254-266) 326 * Simulation 4:254-266)
327 * 327 *
328 * Thanks to Colin Plumb for suggesting this. 328 * Thanks to Colin Plumb for suggesting this.
@@ -666,10 +666,10 @@ retry:
666 r->entropy_total, _RET_IP_); 666 r->entropy_total, _RET_IP_);
667 667
668 if (r == &input_pool) { 668 if (r == &input_pool) {
669 int entropy_bytes = entropy_count >> ENTROPY_SHIFT; 669 int entropy_bits = entropy_count >> ENTROPY_SHIFT;
670 670
671 /* should we wake readers? */ 671 /* should we wake readers? */
672 if (entropy_bytes >= random_read_wakeup_thresh) { 672 if (entropy_bits >= random_read_wakeup_bits) {
673 wake_up_interruptible(&random_read_wait); 673 wake_up_interruptible(&random_read_wait);
674 kill_fasync(&fasync, SIGIO, POLL_IN); 674 kill_fasync(&fasync, SIGIO, POLL_IN);
675 } 675 }
@@ -678,9 +678,9 @@ retry:
678 * forth between them, until the output pools are 75% 678 * forth between them, until the output pools are 75%
679 * full. 679 * full.
680 */ 680 */
681 if (entropy_bytes > random_write_wakeup_thresh && 681 if (entropy_bits > random_write_wakeup_bits &&
682 r->initialized && 682 r->initialized &&
683 r->entropy_total >= 2*random_read_wakeup_thresh) { 683 r->entropy_total >= 2*random_read_wakeup_bits) {
684 static struct entropy_store *last = &blocking_pool; 684 static struct entropy_store *last = &blocking_pool;
685 struct entropy_store *other = &blocking_pool; 685 struct entropy_store *other = &blocking_pool;
686 686
@@ -844,6 +844,8 @@ void add_interrupt_randomness(int irq, int irq_flags)
844 cycles_t cycles = random_get_entropy(); 844 cycles_t cycles = random_get_entropy();
845 __u32 input[4], c_high, j_high; 845 __u32 input[4], c_high, j_high;
846 __u64 ip; 846 __u64 ip;
847 unsigned long seed;
848 int credit;
847 849
848 c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0; 850 c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
849 j_high = (sizeof(now) > 4) ? now >> 32 : 0; 851 j_high = (sizeof(now) > 4) ? now >> 32 : 0;
@@ -862,20 +864,33 @@ void add_interrupt_randomness(int irq, int irq_flags)
862 864
863 r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool; 865 r = nonblocking_pool.initialized ? &input_pool : &nonblocking_pool;
864 __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool), NULL); 866 __mix_pool_bytes(r, &fast_pool->pool, sizeof(fast_pool->pool), NULL);
867
865 /* 868 /*
866 * If we don't have a valid cycle counter, and we see 869 * If we don't have a valid cycle counter, and we see
867 * back-to-back timer interrupts, then skip giving credit for 870 * back-to-back timer interrupts, then skip giving credit for
868 * any entropy. 871 * any entropy, otherwise credit 1 bit.
869 */ 872 */
873 credit = 1;
870 if (cycles == 0) { 874 if (cycles == 0) {
871 if (irq_flags & __IRQF_TIMER) { 875 if (irq_flags & __IRQF_TIMER) {
872 if (fast_pool->last_timer_intr) 876 if (fast_pool->last_timer_intr)
873 return; 877 credit = 0;
874 fast_pool->last_timer_intr = 1; 878 fast_pool->last_timer_intr = 1;
875 } else 879 } else
876 fast_pool->last_timer_intr = 0; 880 fast_pool->last_timer_intr = 0;
877 } 881 }
878 credit_entropy_bits(r, 1); 882
883 /*
884 * If we have architectural seed generator, produce a seed and
885 * add it to the pool. For the sake of paranoia count it as
886 * 50% entropic.
887 */
888 if (arch_get_random_seed_long(&seed)) {
889 __mix_pool_bytes(r, &seed, sizeof(seed), NULL);
890 credit += sizeof(seed) * 4;
891 }
892
893 credit_entropy_bits(r, credit);
879} 894}
880 895
881#ifdef CONFIG_BLOCK 896#ifdef CONFIG_BLOCK
@@ -924,19 +939,19 @@ static void _xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
924{ 939{
925 __u32 tmp[OUTPUT_POOL_WORDS]; 940 __u32 tmp[OUTPUT_POOL_WORDS];
926 941
927 /* For /dev/random's pool, always leave two wakeup worth's BITS */ 942 /* For /dev/random's pool, always leave two wakeups' worth */
928 int rsvd = r->limit ? 0 : random_read_wakeup_thresh/4; 943 int rsvd_bytes = r->limit ? 0 : random_read_wakeup_bits / 4;
929 int bytes = nbytes; 944 int bytes = nbytes;
930 945
931 /* pull at least as many as BYTES as wakeup BITS */ 946 /* pull at least as much as a wakeup */
932 bytes = max_t(int, bytes, random_read_wakeup_thresh / 8); 947 bytes = max_t(int, bytes, random_read_wakeup_bits / 8);
933 /* but never more than the buffer size */ 948 /* but never more than the buffer size */
934 bytes = min_t(int, bytes, sizeof(tmp)); 949 bytes = min_t(int, bytes, sizeof(tmp));
935 950
936 trace_xfer_secondary_pool(r->name, bytes * 8, nbytes * 8, 951 trace_xfer_secondary_pool(r->name, bytes * 8, nbytes * 8,
937 ENTROPY_BITS(r), ENTROPY_BITS(r->pull)); 952 ENTROPY_BITS(r), ENTROPY_BITS(r->pull));
938 bytes = extract_entropy(r->pull, tmp, bytes, 953 bytes = extract_entropy(r->pull, tmp, bytes,
939 random_read_wakeup_thresh / 8, rsvd); 954 random_read_wakeup_bits / 8, rsvd_bytes);
940 mix_pool_bytes(r, tmp, bytes, NULL); 955 mix_pool_bytes(r, tmp, bytes, NULL);
941 credit_entropy_bits(r, bytes*8); 956 credit_entropy_bits(r, bytes*8);
942} 957}
@@ -952,35 +967,22 @@ static void push_to_pool(struct work_struct *work)
952 struct entropy_store *r = container_of(work, struct entropy_store, 967 struct entropy_store *r = container_of(work, struct entropy_store,
953 push_work); 968 push_work);
954 BUG_ON(!r); 969 BUG_ON(!r);
955 _xfer_secondary_pool(r, random_read_wakeup_thresh/8); 970 _xfer_secondary_pool(r, random_read_wakeup_bits/8);
956 trace_push_to_pool(r->name, r->entropy_count >> ENTROPY_SHIFT, 971 trace_push_to_pool(r->name, r->entropy_count >> ENTROPY_SHIFT,
957 r->pull->entropy_count >> ENTROPY_SHIFT); 972 r->pull->entropy_count >> ENTROPY_SHIFT);
958} 973}
959 974
960/* 975/*
961 * These functions extracts randomness from the "entropy pool", and 976 * This function decides how many bytes to actually take from the
962 * returns it in a buffer. 977 * given pool, and also debits the entropy count accordingly.
963 *
964 * The min parameter specifies the minimum amount we can pull before
965 * failing to avoid races that defeat catastrophic reseeding while the
966 * reserved parameter indicates how much entropy we must leave in the
967 * pool after each pull to avoid starving other readers.
968 *
969 * Note: extract_entropy() assumes that .poolwords is a multiple of 16 words.
970 */ 978 */
971
972static size_t account(struct entropy_store *r, size_t nbytes, int min, 979static size_t account(struct entropy_store *r, size_t nbytes, int min,
973 int reserved) 980 int reserved)
974{ 981{
975 unsigned long flags;
976 int wakeup_write = 0;
977 int have_bytes; 982 int have_bytes;
978 int entropy_count, orig; 983 int entropy_count, orig;
979 size_t ibytes; 984 size_t ibytes;
980 985
981 /* Hold lock while accounting */
982 spin_lock_irqsave(&r->lock, flags);
983
984 BUG_ON(r->entropy_count > r->poolinfo->poolfracbits); 986 BUG_ON(r->entropy_count > r->poolinfo->poolfracbits);
985 987
986 /* Can we pull enough? */ 988 /* Can we pull enough? */
@@ -988,29 +990,19 @@ retry:
988 entropy_count = orig = ACCESS_ONCE(r->entropy_count); 990 entropy_count = orig = ACCESS_ONCE(r->entropy_count);
989 have_bytes = entropy_count >> (ENTROPY_SHIFT + 3); 991 have_bytes = entropy_count >> (ENTROPY_SHIFT + 3);
990 ibytes = nbytes; 992 ibytes = nbytes;
991 if (have_bytes < min + reserved) { 993 /* If limited, never pull more than available */
994 if (r->limit)
995 ibytes = min_t(size_t, ibytes, have_bytes - reserved);
996 if (ibytes < min)
992 ibytes = 0; 997 ibytes = 0;
993 } else { 998 entropy_count = max_t(int, 0,
994 /* If limited, never pull more than available */ 999 entropy_count - (ibytes << (ENTROPY_SHIFT + 3)));
995 if (r->limit && ibytes + reserved >= have_bytes) 1000 if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
996 ibytes = have_bytes - reserved; 1001 goto retry;
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 1002
1012 trace_debit_entropy(r->name, 8 * ibytes); 1003 trace_debit_entropy(r->name, 8 * ibytes);
1013 if (wakeup_write) { 1004 if (ibytes &&
1005 (r->entropy_count >> ENTROPY_SHIFT) < random_write_wakeup_bits) {
1014 wake_up_interruptible(&random_write_wait); 1006 wake_up_interruptible(&random_write_wait);
1015 kill_fasync(&fasync, SIGIO, POLL_OUT); 1007 kill_fasync(&fasync, SIGIO, POLL_OUT);
1016 } 1008 }
@@ -1018,6 +1010,12 @@ retry:
1018 return ibytes; 1010 return ibytes;
1019} 1011}
1020 1012
1013/*
1014 * This function does the actual extraction for extract_entropy and
1015 * extract_entropy_user.
1016 *
1017 * Note: we assume that .poolwords is a multiple of 16 words.
1018 */
1021static void extract_buf(struct entropy_store *r, __u8 *out) 1019static void extract_buf(struct entropy_store *r, __u8 *out)
1022{ 1020{
1023 int i; 1021 int i;
@@ -1029,23 +1027,23 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
1029 __u8 extract[64]; 1027 __u8 extract[64];
1030 unsigned long flags; 1028 unsigned long flags;
1031 1029
1032 /* Generate a hash across the pool, 16 words (512 bits) at a time */
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 /* 1030 /*
1039 * If we have a architectural hardware random number 1031 * If we have an architectural hardware random number
1040 * generator, mix that in, too. 1032 * generator, use it for SHA's initial vector
1041 */ 1033 */
1034 sha_init(hash.w);
1042 for (i = 0; i < LONGS(20); i++) { 1035 for (i = 0; i < LONGS(20); i++) {
1043 unsigned long v; 1036 unsigned long v;
1044 if (!arch_get_random_long(&v)) 1037 if (!arch_get_random_long(&v))
1045 break; 1038 break;
1046 hash.l[i] ^= v; 1039 hash.l[i] = v;
1047 } 1040 }
1048 1041
1042 /* Generate a hash across the pool, 16 words (512 bits) at a time */
1043 spin_lock_irqsave(&r->lock, flags);
1044 for (i = 0; i < r->poolinfo->poolwords; i += 16)
1045 sha_transform(hash.w, (__u8 *)(r->pool + i), workspace);
1046
1049 /* 1047 /*
1050 * We mix the hash back into the pool to prevent backtracking 1048 * We mix the hash back into the pool to prevent backtracking
1051 * attacks (where the attacker knows the state of the pool 1049 * attacks (where the attacker knows the state of the pool
@@ -1079,6 +1077,15 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
1079 memset(&hash, 0, sizeof(hash)); 1077 memset(&hash, 0, sizeof(hash));
1080} 1078}
1081 1079
1080/*
1081 * This function extracts randomness from the "entropy pool", and
1082 * returns it in a buffer.
1083 *
1084 * The min parameter specifies the minimum amount we can pull before
1085 * failing to avoid races that defeat catastrophic reseeding while the
1086 * reserved parameter indicates how much entropy we must leave in the
1087 * pool after each pull to avoid starving other readers.
1088 */
1082static ssize_t extract_entropy(struct entropy_store *r, void *buf, 1089static ssize_t extract_entropy(struct entropy_store *r, void *buf,
1083 size_t nbytes, int min, int reserved) 1090 size_t nbytes, int min, int reserved)
1084{ 1091{
@@ -1129,6 +1136,10 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
1129 return ret; 1136 return ret;
1130} 1137}
1131 1138
1139/*
1140 * This function extracts randomness from the "entropy pool", and
1141 * returns it in a userspace buffer.
1142 */
1132static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf, 1143static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
1133 size_t nbytes) 1144 size_t nbytes)
1134{ 1145{
@@ -1170,8 +1181,9 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
1170/* 1181/*
1171 * This function is the exported kernel interface. It returns some 1182 * This function is the exported kernel interface. It returns some
1172 * number of good random numbers, suitable for key generation, seeding 1183 * number of good random numbers, suitable for key generation, seeding
1173 * TCP sequence numbers, etc. It does not use the hw random number 1184 * TCP sequence numbers, etc. It does not rely on the hardware random
1174 * generator, if available; use get_random_bytes_arch() for that. 1185 * number generator. For random bytes direct from the hardware RNG
1186 * (when available), use get_random_bytes_arch().
1175 */ 1187 */
1176void get_random_bytes(void *buf, int nbytes) 1188void get_random_bytes(void *buf, int nbytes)
1177{ 1189{
@@ -1238,7 +1250,8 @@ static void init_std_data(struct entropy_store *r)
1238 r->last_pulled = jiffies; 1250 r->last_pulled = jiffies;
1239 mix_pool_bytes(r, &now, sizeof(now), NULL); 1251 mix_pool_bytes(r, &now, sizeof(now), NULL);
1240 for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) { 1252 for (i = r->poolinfo->poolbytes; i > 0; i -= sizeof(rv)) {
1241 if (!arch_get_random_long(&rv)) 1253 if (!arch_get_random_seed_long(&rv) &&
1254 !arch_get_random_long(&rv))
1242 rv = random_get_entropy(); 1255 rv = random_get_entropy();
1243 mix_pool_bytes(r, &rv, sizeof(rv), NULL); 1256 mix_pool_bytes(r, &rv, sizeof(rv), NULL);
1244 } 1257 }
@@ -1281,56 +1294,71 @@ void rand_initialize_disk(struct gendisk *disk)
1281} 1294}
1282#endif 1295#endif
1283 1296
1284static ssize_t 1297/*
1285random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) 1298 * Attempt an emergency refill using arch_get_random_seed_long().
1299 *
1300 * As with add_interrupt_randomness() be paranoid and only
1301 * credit the output as 50% entropic.
1302 */
1303static int arch_random_refill(void)
1286{ 1304{
1287 ssize_t n, retval = 0, count = 0; 1305 const unsigned int nlongs = 64; /* Arbitrary number */
1306 unsigned int n = 0;
1307 unsigned int i;
1308 unsigned long buf[nlongs];
1288 1309
1289 if (nbytes == 0) 1310 if (!arch_has_random_seed())
1290 return 0; 1311 return 0;
1291 1312
1292 while (nbytes > 0) { 1313 for (i = 0; i < nlongs; i++) {
1293 n = nbytes; 1314 if (arch_get_random_seed_long(&buf[n]))
1294 if (n > SEC_XFER_SIZE) 1315 n++;
1295 n = SEC_XFER_SIZE; 1316 }
1296 1317
1297 n = extract_entropy_user(&blocking_pool, buf, n); 1318 if (n) {
1319 unsigned int rand_bytes = n * sizeof(unsigned long);
1298 1320
1299 if (n < 0) { 1321 mix_pool_bytes(&input_pool, buf, rand_bytes, NULL);
1300 retval = n; 1322 credit_entropy_bits(&input_pool, rand_bytes*4);
1301 break; 1323 }
1302 }
1303 1324
1325 return n;
1326}
1327
1328static ssize_t
1329random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1330{
1331 ssize_t n;
1332
1333 if (nbytes == 0)
1334 return 0;
1335
1336 nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE);
1337 while (1) {
1338 n = extract_entropy_user(&blocking_pool, buf, nbytes);
1339 if (n < 0)
1340 return n;
1304 trace_random_read(n*8, (nbytes-n)*8, 1341 trace_random_read(n*8, (nbytes-n)*8,
1305 ENTROPY_BITS(&blocking_pool), 1342 ENTROPY_BITS(&blocking_pool),
1306 ENTROPY_BITS(&input_pool)); 1343 ENTROPY_BITS(&input_pool));
1344 if (n > 0)
1345 return n;
1307 1346
1308 if (n == 0) { 1347 /* Pool is (near) empty. Maybe wait and retry. */
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 1348
1349 /* First try an emergency refill */
1350 if (arch_random_refill())
1323 continue; 1351 continue;
1324 }
1325 1352
1326 count += n; 1353 if (file->f_flags & O_NONBLOCK)
1327 buf += n; 1354 return -EAGAIN;
1328 nbytes -= n;
1329 break; /* This break makes the device work */
1330 /* like a named pipe */
1331 }
1332 1355
1333 return (count ? count : retval); 1356 wait_event_interruptible(random_read_wait,
1357 ENTROPY_BITS(&input_pool) >=
1358 random_read_wakeup_bits);
1359 if (signal_pending(current))
1360 return -ERESTARTSYS;
1361 }
1334} 1362}
1335 1363
1336static ssize_t 1364static ssize_t
@@ -1358,9 +1386,9 @@ random_poll(struct file *file, poll_table * wait)
1358 poll_wait(file, &random_read_wait, wait); 1386 poll_wait(file, &random_read_wait, wait);
1359 poll_wait(file, &random_write_wait, wait); 1387 poll_wait(file, &random_write_wait, wait);
1360 mask = 0; 1388 mask = 0;
1361 if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_thresh) 1389 if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits)
1362 mask |= POLLIN | POLLRDNORM; 1390 mask |= POLLIN | POLLRDNORM;
1363 if (ENTROPY_BITS(&input_pool) < random_write_wakeup_thresh) 1391 if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits)
1364 mask |= POLLOUT | POLLWRNORM; 1392 mask |= POLLOUT | POLLWRNORM;
1365 return mask; 1393 return mask;
1366} 1394}
@@ -1507,18 +1535,18 @@ EXPORT_SYMBOL(generate_random_uuid);
1507#include <linux/sysctl.h> 1535#include <linux/sysctl.h>
1508 1536
1509static int min_read_thresh = 8, min_write_thresh; 1537static int min_read_thresh = 8, min_write_thresh;
1510static int max_read_thresh = INPUT_POOL_WORDS * 32; 1538static int max_read_thresh = OUTPUT_POOL_WORDS * 32;
1511static int max_write_thresh = INPUT_POOL_WORDS * 32; 1539static int max_write_thresh = INPUT_POOL_WORDS * 32;
1512static char sysctl_bootid[16]; 1540static char sysctl_bootid[16];
1513 1541
1514/* 1542/*
1515 * These functions is used to return both the bootid UUID, and random 1543 * This function is used to return both the bootid UUID, and random
1516 * UUID. The difference is in whether table->data is NULL; if it is, 1544 * UUID. The difference is in whether table->data is NULL; if it is,
1517 * then a new UUID is generated and returned to the user. 1545 * then a new UUID is generated and returned to the user.
1518 * 1546 *
1519 * If the user accesses this via the proc interface, it will be returned 1547 * If the user accesses this via the proc interface, the UUID will be
1520 * as an ASCII string in the standard UUID format. If accesses via the 1548 * returned as an ASCII string in the standard UUID format; if via the
1521 * sysctl system call, it is returned as 16 bytes of binary data. 1549 * sysctl system call, as 16 bytes of binary data.
1522 */ 1550 */
1523static int proc_do_uuid(struct ctl_table *table, int write, 1551static int proc_do_uuid(struct ctl_table *table, int write,
1524 void __user *buffer, size_t *lenp, loff_t *ppos) 1552 void __user *buffer, size_t *lenp, loff_t *ppos)
@@ -1583,7 +1611,7 @@ struct ctl_table random_table[] = {
1583 }, 1611 },
1584 { 1612 {
1585 .procname = "read_wakeup_threshold", 1613 .procname = "read_wakeup_threshold",
1586 .data = &random_read_wakeup_thresh, 1614 .data = &random_read_wakeup_bits,
1587 .maxlen = sizeof(int), 1615 .maxlen = sizeof(int),
1588 .mode = 0644, 1616 .mode = 0644,
1589 .proc_handler = proc_dointvec_minmax, 1617 .proc_handler = proc_dointvec_minmax,
@@ -1592,7 +1620,7 @@ struct ctl_table random_table[] = {
1592 }, 1620 },
1593 { 1621 {
1594 .procname = "write_wakeup_threshold", 1622 .procname = "write_wakeup_threshold",
1595 .data = &random_write_wakeup_thresh, 1623 .data = &random_write_wakeup_bits,
1596 .maxlen = sizeof(int), 1624 .maxlen = sizeof(int),
1597 .mode = 0644, 1625 .mode = 0644,
1598 .proc_handler = proc_dointvec_minmax, 1626 .proc_handler = proc_dointvec_minmax,
diff --git a/include/linux/random.h b/include/linux/random.h
index 1cfce0e24dbd..57fbbffd77a0 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -88,6 +88,22 @@ static inline int arch_get_random_int(unsigned int *v)
88{ 88{
89 return 0; 89 return 0;
90} 90}
91static inline int arch_has_random(void)
92{
93 return 0;
94}
95static inline int arch_get_random_seed_long(unsigned long *v)
96{
97 return 0;
98}
99static inline int arch_get_random_seed_int(unsigned int *v)
100{
101 return 0;
102}
103static inline int arch_has_random_seed(void)
104{
105 return 0;
106}
91#endif 107#endif
92 108
93/* Pseudo random number generator from numerical recipes. */ 109/* Pseudo random number generator from numerical recipes. */