238 #include <linux/utsname.h>
239 #include <linux/module.h>
240 #include <linux/kernel.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>
248 #include <linux/fs.h>
256 #include <linux/ptrace.h>
259 #ifdef CONFIG_GENERIC_HARDIRQS
263 #include <asm/processor.h>
264 #include <asm/uaccess.h>
266 #include <asm/irq_regs.h>
269 #define CREATE_TRACE_POINTS
275 #define INPUT_POOL_WORDS 128
276 #define OUTPUT_POOL_WORDS 32
277 #define SEC_XFER_SIZE 512
278 #define EXTRACT_SIZE 10
280 #define LONGS(x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long))
286 static int random_read_wakeup_thresh = 64;
293 static int random_write_wakeup_thresh = 128;
312 static struct poolinfo {
314 int tap1, tap2, tap3, tap4, tap5;
315 } poolinfo_table[] = {
317 { 128, 103, 76, 51, 25, 1 },
319 { 32, 26, 20, 14, 7, 1 },
322 { 2048, 1638, 1231, 819, 411, 1 },
325 { 1024, 817, 615, 412, 204, 1 },
328 { 1024, 819, 616, 410, 207, 2 },
331 { 512, 411, 308, 208, 104, 1 },
334 { 512, 409, 307, 206, 102, 2 },
336 { 512, 409, 309, 205, 103, 2 },
339 { 256, 205, 155, 101, 52, 1 },
342 { 128, 103, 78, 51, 27, 2 },
345 { 64, 52, 39, 26, 14, 1 },
349 #define POOLBITS poolwords*32
350 #define POOLBYTES poolwords*4
405 #define DEBUG_ENT(fmt, arg...) do { \
407 printk(KERN_DEBUG "random %04d %04d %04d: " \
409 input_pool.entropy_count,\
410 blocking_pool.entropy_count,\
411 nonblocking_pool.entropy_count,\
414 #define DEBUG_ENT(fmt, arg...) do {} while (0)
448 .poolinfo = &poolinfo_table[0],
452 .pool = input_pool_data
456 .poolinfo = &poolinfo_table[1],
461 .pool = blocking_pool_data
465 .poolinfo = &poolinfo_table[1],
466 .name =
"nonblocking",
469 .pool = nonblocking_pool_data
472 static __u32 const twist_table[8] = {
473 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
474 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
489 unsigned long i,
j, tap1, tap2, tap3, tap4, tap5;
491 int wordmask = r->
poolinfo->poolwords - 1;
507 w = rol32(*bytes++, input_rotate & 31);
508 i = (i - 1) & wordmask;
512 w ^= r->
pool[(i + tap1) & wordmask];
513 w ^= r->
pool[(i + tap2) & wordmask];
514 w ^= r->
pool[(i + tap3) & wordmask];
515 w ^= r->
pool[(i + tap4) & wordmask];
516 w ^= r->
pool[(i + tap5) & wordmask];
519 r->
pool[
i] = (w >> 3) ^ twist_table[w & 7];
527 input_rotate += i ? 7 : 14;
535 for (j = 0; j < 16; j++)
539 static void __mix_pool_bytes(
struct entropy_store *r,
const void *in,
540 int nbytes,
__u8 out[64])
543 _mix_pool_bytes(r, in, nbytes, out);
546 static void mix_pool_bytes(
struct entropy_store *r,
const void *in,
547 int nbytes,
__u8 out[64])
553 _mix_pool_bytes(r, in, nbytes, out);
554 spin_unlock_irqrestore(&r->
lock, flags);
570 static void fast_mix(
struct fast_pool *
f,
const void *in,
int nbytes)
572 const char *bytes =
in;
574 unsigned i = f->
count;
575 unsigned input_rotate = f->
rotate;
578 w = rol32(*bytes++, input_rotate & 31) ^ f->
pool[i & 3] ^
579 f->
pool[(i + 1) & 3];
580 f->
pool[i & 3] = (w >> 3) ^ twist_table[w & 7];
581 input_rotate += (i++ & 3) ? 7 : 14;
590 static void credit_entropy_bits(
struct entropy_store *r,
int nbits)
592 int entropy_count, orig;
597 DEBUG_ENT(
"added %d entropy credits to %s\n", nbits, r->
name);
600 entropy_count += nbits;
602 if (entropy_count < 0) {
603 DEBUG_ENT(
"negative entropy/overflow\n");
605 }
else if (entropy_count > r->
poolinfo->POOLBITS)
606 entropy_count = r->
poolinfo->POOLBITS;
616 trace_credit_entropy_bits(r->
name, nbits, entropy_count,
620 if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) {
651 mix_pool_bytes(&input_pool, buf, size,
NULL);
652 mix_pool_bytes(&input_pool, &time,
sizeof(time),
NULL);
653 mix_pool_bytes(&nonblocking_pool, buf, size,
NULL);
654 mix_pool_bytes(&nonblocking_pool, &time,
sizeof(time),
NULL);
677 long delta, delta2, delta3;
722 credit_entropy_bits(&input_pool,
723 min_t(
int, fls(delta>>1), 11));
732 static unsigned char last_value;
735 if (value == last_value)
740 add_timer_randomness(&input_timer_state,
741 (type << 4) ^ code ^ (code >> 4) ^ value);
763 fast_mix(fast_pool, input,
sizeof(input));
765 if ((fast_pool->
count & 1023) &&
769 fast_pool->
last = now;
771 r = nonblocking_pool.
initialized ? &input_pool : &nonblocking_pool;
772 __mix_pool_bytes(r, &fast_pool->
pool,
sizeof(fast_pool->
pool),
NULL);
786 credit_entropy_bits(r, 1);
790 void add_disk_randomness(
struct gendisk *disk)
792 if (!disk || !disk->random)
796 MAJOR(disk_devt(disk)),
MINOR(disk_devt(disk)));
798 add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
809 size_t nbytes,
int min,
int rsvd);
816 static void xfer_secondary_pool(
struct entropy_store *r,
size_t nbytes)
823 int rsvd = r->
limit ? 0 : random_read_wakeup_thresh/4;
827 bytes =
max_t(
int, bytes, random_read_wakeup_thresh / 8);
829 bytes =
min_t(
int, bytes,
sizeof(tmp));
831 DEBUG_ENT(
"going to reseed %s with %d bits "
832 "(%d of %d requested)\n",
835 bytes = extract_entropy(r->
pull, tmp, bytes,
836 random_read_wakeup_thresh / 8, rsvd);
837 mix_pool_bytes(r, tmp, bytes,
NULL);
838 credit_entropy_bits(r, bytes*8);
863 DEBUG_ENT(
"trying to extract %d bits from %s\n",
864 nbytes * 8, r->
name);
885 DEBUG_ENT(
"debiting %d entropy credits from %s%s\n",
886 nbytes * 8, r->
name, r->
limit ?
"" :
" (unlimited)");
888 spin_unlock_irqrestore(&r->
lock, flags);
907 for (i = 0; i < r->
poolinfo->poolwords; i += 16)
919 __mix_pool_bytes(r,
hash.w,
sizeof(
hash.w), extract);
920 spin_unlock_irqrestore(&r->
lock, flags);
927 memset(extract, 0,
sizeof(extract));
928 memset(workspace, 0,
sizeof(workspace));
945 if (!arch_get_random_long(&v))
955 size_t nbytes,
int min,
int reserved)
961 xfer_secondary_pool(r, nbytes);
962 nbytes = account(r, nbytes, min, reserved);
972 panic(
"Hardware RNG duplicated output!\n");
974 spin_unlock_irqrestore(&r->
lock, flags);
984 memset(tmp, 0,
sizeof(tmp));
996 xfer_secondary_pool(r, nbytes);
997 nbytes = account(r, nbytes, 0, 0);
1000 if (need_resched()) {
1001 if (signal_pending(
current)) {
1009 extract_buf(r, tmp);
1022 memset(tmp, 0,
sizeof(tmp));
1035 extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);
1053 trace_get_random_bytes(nbytes,
_RET_IP_);
1056 int chunk =
min(nbytes, (
int)
sizeof(
unsigned long));
1058 if (!arch_get_random_long(&v))
1067 extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
1089 mix_pool_bytes(r, &now,
sizeof(now),
NULL);
1090 for (i = r->
poolinfo->POOLBYTES; i > 0; i -=
sizeof(rv)) {
1091 if (!arch_get_random_long(&rv))
1093 mix_pool_bytes(r, &rv,
sizeof(rv),
NULL);
1095 mix_pool_bytes(r, utsname(),
sizeof(*(utsname())),
NULL);
1108 static int rand_initialize(
void)
1110 init_std_data(&input_pool);
1111 init_std_data(&blocking_pool);
1112 init_std_data(&nonblocking_pool);
1118 void rand_initialize_disk(
struct gendisk *disk)
1128 disk->random =
state;
1133 random_read(
struct file *
file,
char __user *buf,
size_t nbytes, loff_t *ppos)
1140 while (nbytes > 0) {
1147 n = extract_entropy_user(&blocking_pool, buf, n);
1149 DEBUG_ENT(
"read got %d bits (%d still needed)\n",
1162 random_read_wakeup_thresh);
1166 if (signal_pending(
current)) {
1189 urandom_read(
struct file *file,
char __user *buf,
size_t nbytes, loff_t *ppos)
1191 return extract_entropy_user(&nonblocking_pool, buf, nbytes);
1199 poll_wait(file, &random_read_wait, wait);
1200 poll_wait(file, &random_write_wait, wait);
1217 bytes =
min(count,
sizeof(buf));
1224 mix_pool_bytes(r, buf, bytes,
NULL);
1231 static ssize_t random_write(
struct file *file,
const char __user *buffer,
1232 size_t count, loff_t *ppos)
1236 ret = write_pool(&blocking_pool, buffer, count);
1239 ret = write_pool(&nonblocking_pool, buffer, count);
1246 static long random_ioctl(
struct file *
f,
unsigned int cmd,
unsigned long arg)
1248 int size, ent_count;
1263 credit_entropy_bits(&input_pool, ent_count);
1274 retval = write_pool(&input_pool, (
const char __user *)p,
1278 credit_entropy_bits(&input_pool, ent_count);
1292 static int random_fasync(
int fd,
struct file *filp,
int on)
1298 .read = random_read,
1299 .write = random_write,
1300 .poll = random_poll,
1301 .unlocked_ioctl = random_ioctl,
1302 .fasync = random_fasync,
1307 .read = urandom_read,
1308 .write = random_write,
1309 .unlocked_ioctl = random_ioctl,
1310 .fasync = random_fasync,
1328 uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40;
1330 uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
1340 #ifdef CONFIG_SYSCTL
1342 #include <linux/sysctl.h>
1344 static int min_read_thresh = 8, min_write_thresh;
1347 static char sysctl_bootid[16];
1359 void __user *buffer,
size_t *lenp, loff_t *ppos)
1362 unsigned char buf[64], tmp_uuid[16], *
uuid;
1371 spin_lock(&bootid_spinlock);
1374 spin_unlock(&bootid_spinlock);
1382 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
1390 .data = &sysctl_poolsize,
1391 .maxlen =
sizeof(
int),
1396 .procname =
"entropy_avail",
1397 .maxlen =
sizeof(
int),
1403 .procname =
"read_wakeup_threshold",
1404 .data = &random_read_wakeup_thresh,
1405 .maxlen =
sizeof(
int),
1408 .extra1 = &min_read_thresh,
1409 .extra2 = &max_read_thresh,
1412 .procname =
"write_wakeup_threshold",
1413 .data = &random_write_wakeup_thresh,
1414 .maxlen =
sizeof(
int),
1417 .extra1 = &min_write_thresh,
1418 .extra2 = &max_write_thresh,
1421 .procname =
"boot_id",
1422 .data = &sysctl_bootid,
1425 .proc_handler = proc_do_uuid,
1431 .proc_handler = proc_do_uuid,
1439 static int __init random_int_secret_init(
void)
1458 if (arch_get_random_int(&ret))
1485 if (end <= start + len)