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
239
240
241
242
243#include <linux/utsname.h>
244#include <linux/config.h>
245#include <linux/module.h>
246#include <linux/kernel.h>
247#include <linux/major.h>
248#include <linux/string.h>
249#include <linux/fcntl.h>
250#include <linux/slab.h>
251#include <linux/random.h>
252#include <linux/poll.h>
253#include <linux/init.h>
254#include <linux/interrupt.h>
255#include <linux/spinlock.h>
256
257#include <asm/processor.h>
258#include <asm/uaccess.h>
259#include <asm/irq.h>
260#include <asm/io.h>
261
262
263
264
265#define DEFAULT_POOL_SIZE 512
266#define SECONDARY_POOL_SIZE 128
267#define BATCH_ENTROPY_SIZE 256
268#define USE_SHA
269
270
271
272
273
274static int random_read_wakeup_thresh = 8;
275
276
277
278
279
280
281static int random_write_wakeup_thresh = 128;
282
283
284
285
286
287
288
289
290
291static struct poolinfo {
292 int poolwords;
293 int tap1, tap2, tap3, tap4, tap5;
294} poolinfo_table[] = {
295
296 { 2048, 1638, 1231, 819, 411, 1 },
297
298
299 { 1024, 817, 615, 412, 204, 1 },
300#if 0
301
302 { 1024, 819, 616, 410, 207, 2 },
303#endif
304
305
306 { 512, 411, 308, 208, 104, 1 },
307#if 0
308
309 { 512, 409, 307, 206, 102, 2 },
310
311 { 512, 409, 309, 205, 103, 2 },
312#endif
313
314
315 { 256, 205, 155, 101, 52, 1 },
316
317
318 { 128, 103, 76, 51, 25, 1 },
319#if 0
320
321 { 128, 103, 78, 51, 27, 2 },
322#endif
323
324
325 { 64, 52, 39, 26, 14, 1 },
326
327
328 { 32, 26, 20, 14, 7, 1 },
329
330 { 0, 0, 0, 0, 0, 0 },
331};
332
333#define POOLBITS poolwords*32
334#define POOLBYTES poolwords*4
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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#ifndef DECLARE_WAITQUEUE
383#define DECLARE_WAITQUEUE(WAIT, PTR) struct wait_queue WAIT = { PTR, NULL }
384#endif
385#ifndef DECLARE_WAIT_QUEUE_HEAD
386#define DECLARE_WAIT_QUEUE_HEAD(WAIT) struct wait_queue *WAIT
387#endif
388
389
390
391
392static struct entropy_store *random_state;
393static struct entropy_store *sec_random_state;
394static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
395static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
396
397
398
399
400#ifdef CONFIG_SYSCTL
401static void sysctl_init_random(struct entropy_store *random_state);
402#endif
403
404
405
406
407
408
409
410
411
412
413
414
415
416#if (!defined (__i386__))
417static inline __u32 rotate_left(int i, __u32 word)
418{
419 return (word << i) | (word >> (32 - i));
420
421}
422#else
423static inline __u32 rotate_left(int i, __u32 word)
424{
425 __asm__("roll %%cl,%0"
426 :"=r" (word)
427 :"0" (word),"c" (i));
428 return word;
429}
430#endif
431
432
433
434
435
436
437
438
439
440
441#if 0
442static inline __u32 int_ln_12bits(__u32 word)
443{
444 __u32 nbits = 0;
445
446 while (word >>= 1)
447 nbits++;
448 return nbits;
449}
450#else
451static inline __u32 int_ln_12bits(__u32 word)
452{
453
454 word |= word >> 8;
455 word |= word >> 4;
456 word |= word >> 2;
457 word |= word >> 1;
458
459 word >>= 1;
460
461 word -= (word >> 1) & 0x555;
462 word = (word & 0x333) + ((word >> 2) & 0x333);
463 word += (word >> 4);
464 word += (word >> 8);
465 return word & 15;
466}
467#endif
468
469#if 0
470#define DEBUG_ENT(fmt, arg...) printk(KERN_DEBUG "random: " fmt, ## arg)
471#else
472#define DEBUG_ENT(fmt, arg...) do {} while (0)
473#endif
474
475
476
477
478
479
480
481
482struct entropy_store {
483 unsigned add_ptr;
484 int entropy_count;
485 int input_rotate;
486 int extract_count;
487 struct poolinfo poolinfo;
488 __u32 *pool;
489};
490
491
492
493
494
495
496
497static int create_entropy_store(int size, struct entropy_store **ret_bucket)
498{
499 struct entropy_store *r;
500 struct poolinfo *p;
501 int poolwords;
502
503 poolwords = (size + 3) / 4;
504
505 poolwords = ((poolwords + 15) / 16) * 16;
506
507 for (p = poolinfo_table; p->poolwords; p++) {
508 if (poolwords == p->poolwords)
509 break;
510 }
511 if (p->poolwords == 0)
512 return -EINVAL;
513
514 r = kmalloc(sizeof(struct entropy_store), GFP_KERNEL);
515 if (!r)
516 return -ENOMEM;
517
518 memset (r, 0, sizeof(struct entropy_store));
519 r->poolinfo = *p;
520
521 r->pool = kmalloc(POOLBYTES, GFP_KERNEL);
522 if (!r->pool) {
523 kfree(r);
524 return -ENOMEM;
525 }
526 memset(r->pool, 0, POOLBYTES);
527 *ret_bucket = r;
528 return 0;
529}
530
531
532static void clear_entropy_store(struct entropy_store *r)
533{
534 r->add_ptr = 0;
535 r->entropy_count = 0;
536 r->input_rotate = 0;
537 r->extract_count = 0;
538 memset(r->pool, 0, r->poolinfo.POOLBYTES);
539}
540
541static void free_entropy_store(struct entropy_store *r)
542{
543 if (r->pool)
544 kfree(r->pool);
545 kfree(r);
546}
547
548
549
550
551
552
553
554
555
556
557
558static void add_entropy_words(struct entropy_store *r, const __u32 *in,
559 int nwords)
560{
561 static __u32 const twist_table[8] = {
562 0, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
563 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
564 unsigned i;
565 int new_rotate;
566 int wordmask = r->poolinfo.poolwords - 1;
567 __u32 w;
568
569 while (nwords--) {
570 w = rotate_left(r->input_rotate, *in++);
571 i = r->add_ptr = (r->add_ptr - 1) & wordmask;
572
573
574
575
576
577
578 new_rotate = r->input_rotate + 14;
579 if (i)
580 new_rotate = r->input_rotate + 7;
581 r->input_rotate = new_rotate & 31;
582
583
584 w ^= r->pool[(i + r->poolinfo.tap1) & wordmask];
585 w ^= r->pool[(i + r->poolinfo.tap2) & wordmask];
586 w ^= r->pool[(i + r->poolinfo.tap3) & wordmask];
587 w ^= r->pool[(i + r->poolinfo.tap4) & wordmask];
588 w ^= r->pool[(i + r->poolinfo.tap5) & wordmask];
589 w ^= r->pool[i];
590 r->pool[i] = (w >> 3) ^ twist_table[w & 7];
591 }
592}
593
594
595
596
597static void credit_entropy_store(struct entropy_store *r, int nbits)
598{
599 if (r->entropy_count + nbits < 0) {
600 DEBUG_ENT("negative entropy/overflow (%d+%d)\n",
601 r->entropy_count, nbits);
602 r->entropy_count = 0;
603 } else if (r->entropy_count + nbits > r->poolinfo.POOLBITS) {
604 r->entropy_count = r->poolinfo.POOLBITS;
605 } else {
606 r->entropy_count += nbits;
607 if (nbits)
608 DEBUG_ENT("%s added %d bits, now %d\n",
609 r == sec_random_state ? "secondary" :
610 r == random_state ? "primary" : "unknown",
611 nbits, r->entropy_count);
612 }
613}
614
615
616
617
618
619
620
621
622
623static __u32 *batch_entropy_pool;
624static int *batch_entropy_credit;
625static int batch_max;
626static int batch_head, batch_tail;
627static struct tq_struct batch_tqueue;
628static void batch_entropy_process(void *private_);
629
630
631static int __init batch_entropy_init(int size, struct entropy_store *r)
632{
633 batch_entropy_pool = kmalloc(2*size*sizeof(__u32), GFP_KERNEL);
634 if (!batch_entropy_pool)
635 return -1;
636 batch_entropy_credit =kmalloc(size*sizeof(int), GFP_KERNEL);
637 if (!batch_entropy_credit) {
638 kfree(batch_entropy_pool);
639 return -1;
640 }
641 batch_head = batch_tail = 0;
642 batch_max = size;
643 batch_tqueue.routine = batch_entropy_process;
644 batch_tqueue.data = r;
645 return 0;
646}
647
648
649
650
651
652
653
654void batch_entropy_store(u32 a, u32 b, int num)
655{
656 int new;
657
658 if (!batch_max)
659 return;
660
661 batch_entropy_pool[2*batch_head] = a;
662 batch_entropy_pool[(2*batch_head) + 1] = b;
663 batch_entropy_credit[batch_head] = num;
664
665 new = (batch_head+1) & (batch_max-1);
666 if (new != batch_tail) {
667 queue_task(&batch_tqueue, &tq_timer);
668 batch_head = new;
669 } else {
670 DEBUG_ENT("batch entropy buffer full\n");
671 }
672}
673
674
675
676
677
678
679static void batch_entropy_process(void *private_)
680{
681 struct entropy_store *r = (struct entropy_store *) private_, *p;
682 int max_entropy = r->poolinfo.POOLBITS;
683
684 if (!batch_max)
685 return;
686
687 p = r;
688 while (batch_head != batch_tail) {
689 if (r->entropy_count >= max_entropy) {
690 r = (r == sec_random_state) ? random_state :
691 sec_random_state;
692 max_entropy = r->poolinfo.POOLBITS;
693 }
694 add_entropy_words(r, batch_entropy_pool + 2*batch_tail, 2);
695 credit_entropy_store(r, batch_entropy_credit[batch_tail]);
696 batch_tail = (batch_tail+1) & (batch_max-1);
697 }
698 if (p->entropy_count >= random_read_wakeup_thresh)
699 wake_up_interruptible(&random_read_wait);
700}
701
702
703
704
705
706
707
708
709struct timer_rand_state {
710 __u32 last_time;
711 __s32 last_delta,last_delta2;
712 int dont_count_entropy:1;
713};
714
715static struct timer_rand_state keyboard_timer_state;
716static struct timer_rand_state mouse_timer_state;
717static struct timer_rand_state extract_timer_state;
718#ifndef CONFIG_ARCH_S390
719static struct timer_rand_state *irq_timer_state[NR_IRQS];
720#endif
721static struct timer_rand_state *blkdev_timer_state[MAX_BLKDEV];
722
723
724
725
726
727
728
729
730
731
732
733
734
735static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
736{
737 __u32 time;
738 __s32 delta, delta2, delta3;
739 int entropy = 0;
740
741#if defined (__i386__)
742 if (cpu_has_tsc) {
743 __u32 high;
744 rdtsc(time, high);
745 num ^= high;
746 } else {
747 time = jiffies;
748 }
749#elif defined (__x86_64__)
750 __u32 high;
751 rdtsc(time, high);
752 num ^= high;
753#elif defined (__sparc_v9__)
754 unsigned long tick = tick_ops->get_tick();
755
756 time = (unsigned int) tick;
757 num ^= (tick >> 32UL);
758#else
759 time = jiffies;
760#endif
761
762
763
764
765
766
767 if (!state->dont_count_entropy) {
768 delta = time - state->last_time;
769 state->last_time = time;
770
771 delta2 = delta - state->last_delta;
772 state->last_delta = delta;
773
774 delta3 = delta2 - state->last_delta2;
775 state->last_delta2 = delta2;
776
777 if (delta < 0)
778 delta = -delta;
779 if (delta2 < 0)
780 delta2 = -delta2;
781 if (delta3 < 0)
782 delta3 = -delta3;
783 if (delta > delta2)
784 delta = delta2;
785 if (delta > delta3)
786 delta = delta3;
787
788
789
790
791
792
793 delta >>= 1;
794 delta &= (1 << 12) - 1;
795
796 entropy = int_ln_12bits(delta);
797 }
798 batch_entropy_store(num, time, entropy);
799}
800
801#ifndef CONFIG_ARCH_S390
802void add_keyboard_randomness(unsigned char scancode)
803{
804 static unsigned char last_scancode;
805
806 if (scancode != last_scancode) {
807 last_scancode = scancode;
808 add_timer_randomness(&keyboard_timer_state, scancode);
809 }
810}
811
812void add_mouse_randomness(__u32 mouse_data)
813{
814 add_timer_randomness(&mouse_timer_state, mouse_data);
815}
816
817void add_interrupt_randomness(int irq)
818{
819 if (irq >= NR_IRQS || irq_timer_state[irq] == 0)
820 return;
821
822 add_timer_randomness(irq_timer_state[irq], 0x100+irq);
823}
824#endif
825
826void add_blkdev_randomness(int major)
827{
828 if (major >= MAX_BLKDEV)
829 return;
830
831 if (blkdev_timer_state[major] == 0) {
832 rand_initialize_blkdev(major, GFP_ATOMIC);
833 if (blkdev_timer_state[major] == 0)
834 return;
835 }
836
837 add_timer_randomness(blkdev_timer_state[major], 0x200+major);
838}
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871#ifdef USE_SHA
872
873#define HASH_BUFFER_SIZE 5
874#define HASH_EXTRA_SIZE 80
875#define HASH_TRANSFORM SHATransform
876
877
878#define SHA_CODE_SIZE 0
879
880
881
882
883
884
885
886
887#define f1(x,y,z) ( z ^ (x & (y^z)) )
888#define f2(x,y,z) (x ^ y ^ z)
889#define f3(x,y,z) ( (x & y) + (z & (x ^ y)) )
890#define f4(x,y,z) (x ^ y ^ z)
891
892
893
894#define K1 0x5A827999L
895#define K2 0x6ED9EBA1L
896#define K3 0x8F1BBCDCL
897#define K4 0xCA62C1D6L
898
899#define ROTL(n,X) ( ( ( X ) << n ) | ( ( X ) >> ( 32 - n ) ) )
900
901#define subRound(a, b, c, d, e, f, k, data) \
902 ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) )
903
904
905static void SHATransform(__u32 digest[85], __u32 const data[16])
906{
907 __u32 A, B, C, D, E;
908 __u32 TEMP;
909 int i;
910#define W (digest + HASH_BUFFER_SIZE)
911
912
913
914
915
916
917
918 memcpy(W, data, 16*sizeof(__u32));
919 for (i = 0; i < 64; i++) {
920 TEMP = W[i] ^ W[i+2] ^ W[i+8] ^ W[i+13];
921 W[i+16] = ROTL(1, TEMP);
922 }
923
924
925 A = digest[ 0 ];
926 B = digest[ 1 ];
927 C = digest[ 2 ];
928 D = digest[ 3 ];
929 E = digest[ 4 ];
930
931
932#if SHA_CODE_SIZE == 0
933
934
935
936
937 for (i = 0; i < 80; i++) {
938 if (i < 40) {
939 if (i < 20)
940 TEMP = f1(B, C, D) + K1;
941 else
942 TEMP = f2(B, C, D) + K2;
943 } else {
944 if (i < 60)
945 TEMP = f3(B, C, D) + K3;
946 else
947 TEMP = f4(B, C, D) + K4;
948 }
949 TEMP += ROTL(5, A) + E + W[i];
950 E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
951 }
952#elif SHA_CODE_SIZE == 1
953 for (i = 0; i < 20; i++) {
954 TEMP = f1(B, C, D) + K1 + ROTL(5, A) + E + W[i];
955 E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
956 }
957 for (; i < 40; i++) {
958 TEMP = f2(B, C, D) + K2 + ROTL(5, A) + E + W[i];
959 E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
960 }
961 for (; i < 60; i++) {
962 TEMP = f3(B, C, D) + K3 + ROTL(5, A) + E + W[i];
963 E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
964 }
965 for (; i < 80; i++) {
966 TEMP = f4(B, C, D) + K4 + ROTL(5, A) + E + W[i];
967 E = D; D = C; C = ROTL(30, B); B = A; A = TEMP;
968 }
969#elif SHA_CODE_SIZE == 2
970 for (i = 0; i < 20; i += 5) {
971 subRound( A, B, C, D, E, f1, K1, W[ i ] );
972 subRound( E, A, B, C, D, f1, K1, W[ i+1 ] );
973 subRound( D, E, A, B, C, f1, K1, W[ i+2 ] );
974 subRound( C, D, E, A, B, f1, K1, W[ i+3 ] );
975 subRound( B, C, D, E, A, f1, K1, W[ i+4 ] );
976 }
977 for (; i < 40; i += 5) {
978 subRound( A, B, C, D, E, f2, K2, W[ i ] );
979 subRound( E, A, B, C, D, f2, K2, W[ i+1 ] );
980 subRound( D, E, A, B, C, f2, K2, W[ i+2 ] );
981 subRound( C, D, E, A, B, f2, K2, W[ i+3 ] );
982 subRound( B, C, D, E, A, f2, K2, W[ i+4 ] );
983 }
984 for (; i < 60; i += 5) {
985 subRound( A, B, C, D, E, f3, K3, W[ i ] );
986 subRound( E, A, B, C, D, f3, K3, W[ i+1 ] );
987 subRound( D, E, A, B, C, f3, K3, W[ i+2 ] );
988 subRound( C, D, E, A, B, f3, K3, W[ i+3 ] );
989 subRound( B, C, D, E, A, f3, K3, W[ i+4 ] );
990 }
991 for (; i < 80; i += 5) {
992 subRound( A, B, C, D, E, f4, K4, W[ i ] );
993 subRound( E, A, B, C, D, f4, K4, W[ i+1 ] );
994 subRound( D, E, A, B, C, f4, K4, W[ i+2 ] );
995 subRound( C, D, E, A, B, f4, K4, W[ i+3 ] );
996 subRound( B, C, D, E, A, f4, K4, W[ i+4 ] );
997 }
998#elif SHA_CODE_SIZE == 3
999 subRound( A, B, C, D, E, f1, K1, W[ 0 ] );
1000 subRound( E, A, B, C, D, f1, K1, W[ 1 ] );
1001 subRound( D, E, A, B, C, f1, K1, W[ 2 ] );
1002 subRound( C, D, E, A, B, f1, K1, W[ 3 ] );
1003 subRound( B, C, D, E, A, f1, K1, W[ 4 ] );
1004 subRound( A, B, C, D, E, f1, K1, W[ 5 ] );
1005 subRound( E, A, B, C, D, f1, K1, W[ 6 ] );
1006 subRound( D, E, A, B, C, f1, K1, W[ 7 ] );
1007 subRound( C, D, E, A, B, f1, K1, W[ 8 ] );
1008 subRound( B, C, D, E, A, f1, K1, W[ 9 ] );
1009 subRound( A, B, C, D, E, f1, K1, W[ 10 ] );
1010 subRound( E, A, B, C, D, f1, K1, W[ 11 ] );
1011 subRound( D, E, A, B, C, f1, K1, W[ 12 ] );
1012 subRound( C, D, E, A, B, f1, K1, W[ 13 ] );
1013 subRound( B, C, D, E, A, f1, K1, W[ 14 ] );
1014 subRound( A, B, C, D, E, f1, K1, W[ 15 ] );
1015 subRound( E, A, B, C, D, f1, K1, W[ 16 ] );
1016 subRound( D, E, A, B, C, f1, K1, W[ 17 ] );
1017 subRound( C, D, E, A, B, f1, K1, W[ 18 ] );
1018 subRound( B, C, D, E, A, f1, K1, W[ 19 ] );
1019
1020 subRound( A, B, C, D, E, f2, K2, W[ 20 ] );
1021 subRound( E, A, B, C, D, f2, K2, W[ 21 ] );
1022 subRound( D, E, A, B, C, f2, K2, W[ 22 ] );
1023 subRound( C, D, E, A, B, f2, K2, W[ 23 ] );
1024 subRound( B, C, D, E, A, f2, K2, W[ 24 ] );
1025 subRound( A, B, C, D, E, f2, K2, W[ 25 ] );
1026 subRound( E, A, B, C, D, f2, K2, W[ 26 ] );
1027 subRound( D, E, A, B, C, f2, K2, W[ 27 ] );
1028 subRound( C, D, E, A, B, f2, K2, W[ 28 ] );
1029 subRound( B, C, D, E, A, f2, K2, W[ 29 ] );
1030 subRound( A, B, C, D, E, f2, K2, W[ 30 ] );
1031 subRound( E, A, B, C, D, f2, K2, W[ 31 ] );
1032 subRound( D, E, A, B, C, f2, K2, W[ 32 ] );
1033 subRound( C, D, E, A, B, f2, K2, W[ 33 ] );
1034 subRound( B, C, D, E, A, f2, K2, W[ 34 ] );
1035 subRound( A, B, C, D, E, f2, K2, W[ 35 ] );
1036 subRound( E, A, B, C, D, f2, K2, W[ 36 ] );
1037 subRound( D, E, A, B, C, f2, K2, W[ 37 ] );
1038 subRound( C, D, E, A, B, f2, K2, W[ 38 ] );
1039 subRound( B, C, D, E, A, f2, K2, W[ 39 ] );
1040
1041 subRound( A, B, C, D, E, f3, K3, W[ 40 ] );
1042 subRound( E, A, B, C, D, f3, K3, W[ 41 ] );
1043 subRound( D, E, A, B, C, f3, K3, W[ 42 ] );
1044 subRound( C, D, E, A, B, f3, K3, W[ 43 ] );
1045 subRound( B, C, D, E, A, f3, K3, W[ 44 ] );
1046 subRound( A, B, C, D, E, f3, K3, W[ 45 ] );
1047 subRound( E, A, B, C, D, f3, K3, W[ 46 ] );
1048 subRound( D, E, A, B, C, f3, K3, W[ 47 ] );
1049 subRound( C, D, E, A, B, f3, K3, W[ 48 ] );
1050 subRound( B, C, D, E, A, f3, K3, W[ 49 ] );
1051 subRound( A, B, C, D, E, f3, K3, W[ 50 ] );
1052 subRound( E, A, B, C, D, f3, K3, W[ 51 ] );
1053 subRound( D, E, A, B, C, f3, K3, W[ 52 ] );
1054 subRound( C, D, E, A, B, f3, K3, W[ 53 ] );
1055 subRound( B, C, D, E, A, f3, K3, W[ 54 ] );
1056 subRound( A, B, C, D, E, f3, K3, W[ 55 ] );
1057 subRound( E, A, B, C, D, f3, K3, W[ 56 ] );
1058 subRound( D, E, A, B, C, f3, K3, W[ 57 ] );
1059 subRound( C, D, E, A, B, f3, K3, W[ 58 ] );
1060 subRound( B, C, D, E, A, f3, K3, W[ 59 ] );
1061
1062 subRound( A, B, C, D, E, f4, K4, W[ 60 ] );
1063 subRound( E, A, B, C, D, f4, K4, W[ 61 ] );
1064 subRound( D, E, A, B, C, f4, K4, W[ 62 ] );
1065 subRound( C, D, E, A, B, f4, K4, W[ 63 ] );
1066 subRound( B, C, D, E, A, f4, K4, W[ 64 ] );
1067 subRound( A, B, C, D, E, f4, K4, W[ 65 ] );
1068 subRound( E, A, B, C, D, f4, K4, W[ 66 ] );
1069 subRound( D, E, A, B, C, f4, K4, W[ 67 ] );
1070 subRound( C, D, E, A, B, f4, K4, W[ 68 ] );
1071 subRound( B, C, D, E, A, f4, K4, W[ 69 ] );
1072 subRound( A, B, C, D, E, f4, K4, W[ 70 ] );
1073 subRound( E, A, B, C, D, f4, K4, W[ 71 ] );
1074 subRound( D, E, A, B, C, f4, K4, W[ 72 ] );
1075 subRound( C, D, E, A, B, f4, K4, W[ 73 ] );
1076 subRound( B, C, D, E, A, f4, K4, W[ 74 ] );
1077 subRound( A, B, C, D, E, f4, K4, W[ 75 ] );
1078 subRound( E, A, B, C, D, f4, K4, W[ 76 ] );
1079 subRound( D, E, A, B, C, f4, K4, W[ 77 ] );
1080 subRound( C, D, E, A, B, f4, K4, W[ 78 ] );
1081 subRound( B, C, D, E, A, f4, K4, W[ 79 ] );
1082#else
1083#error Illegal SHA_CODE_SIZE
1084#endif
1085
1086
1087 digest[ 0 ] += A;
1088 digest[ 1 ] += B;
1089 digest[ 2 ] += C;
1090 digest[ 3 ] += D;
1091 digest[ 4 ] += E;
1092
1093
1094#undef W
1095}
1096
1097#undef ROTL
1098#undef f1
1099#undef f2
1100#undef f3
1101#undef f4
1102#undef K1
1103#undef K2
1104#undef K3
1105#undef K4
1106#undef subRound
1107
1108#else
1109
1110#define HASH_BUFFER_SIZE 4
1111#define HASH_EXTRA_SIZE 0
1112#define HASH_TRANSFORM MD5Transform
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122#define F1(x, y, z) (z ^ (x & (y ^ z)))
1123#define F2(x, y, z) F1(z, x, y)
1124#define F3(x, y, z) (x ^ y ^ z)
1125#define F4(x, y, z) (y ^ (x | ~z))
1126
1127
1128#define MD5STEP(f, w, x, y, z, data, s) \
1129 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
1130
1131
1132
1133
1134
1135
1136static void MD5Transform(__u32 buf[HASH_BUFFER_SIZE], __u32 const in[16])
1137{
1138 __u32 a, b, c, d;
1139
1140 a = buf[0];
1141 b = buf[1];
1142 c = buf[2];
1143 d = buf[3];
1144
1145 MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
1146 MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
1147 MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
1148 MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
1149 MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
1150 MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
1151 MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
1152 MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
1153 MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
1154 MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
1155 MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
1156 MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
1157 MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
1158 MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
1159 MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
1160 MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
1161
1162 MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
1163 MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
1164 MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
1165 MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
1166 MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
1167 MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
1168 MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
1169 MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
1170 MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
1171 MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
1172 MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
1173 MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
1174 MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
1175 MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
1176 MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
1177 MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
1178
1179 MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
1180 MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
1181 MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
1182 MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
1183 MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
1184 MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
1185 MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
1186 MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
1187 MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
1188 MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
1189 MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
1190 MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
1191 MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
1192 MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
1193 MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
1194 MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
1195
1196 MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
1197 MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
1198 MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
1199 MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
1200 MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
1201 MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
1202 MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
1203 MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
1204 MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
1205 MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
1206 MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
1207 MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
1208 MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
1209 MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
1210 MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
1211 MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
1212
1213 buf[0] += a;
1214 buf[1] += b;
1215 buf[2] += c;
1216 buf[3] += d;
1217}
1218
1219#undef F1
1220#undef F2
1221#undef F3
1222#undef F4
1223#undef MD5STEP
1224
1225#endif
1226
1227
1228
1229
1230
1231
1232
1233#define EXTRACT_ENTROPY_USER 1
1234#define EXTRACT_ENTROPY_SECONDARY 2
1235#define TMP_BUF_SIZE (HASH_BUFFER_SIZE + HASH_EXTRA_SIZE)
1236#define SEC_XFER_SIZE (TMP_BUF_SIZE*4)
1237
1238static ssize_t extract_entropy(struct entropy_store *r, void * buf,
1239 size_t nbytes, int flags);
1240
1241
1242
1243
1244
1245
1246
1247
1248static inline void xfer_secondary_pool(struct entropy_store *r,
1249 size_t nbytes, __u32 *tmp)
1250{
1251 if (r->entropy_count < nbytes * 8 &&
1252 r->entropy_count < r->poolinfo.POOLBITS) {
1253 int nwords = min_t(int,
1254 r->poolinfo.poolwords - r->entropy_count/32,
1255 sizeof(tmp) / 4);
1256
1257 DEBUG_ENT("xfer %d from primary to %s (have %d, need %d)\n",
1258 nwords * 32,
1259 r == sec_random_state ? "secondary" : "unknown",
1260 r->entropy_count, nbytes * 8);
1261
1262 extract_entropy(random_state, tmp, nwords * 4, 0);
1263 add_entropy_words(r, tmp, nwords);
1264 credit_entropy_store(r, nwords * 32);
1265 }
1266 if (r->extract_count > 1024) {
1267 DEBUG_ENT("reseeding %s with %d from primary\n",
1268 r == sec_random_state ? "secondary" : "unknown",
1269 sizeof(tmp) * 8);
1270 extract_entropy(random_state, tmp, sizeof(tmp), 0);
1271 add_entropy_words(r, tmp, sizeof(tmp) / 4);
1272 r->extract_count = 0;
1273 }
1274}
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289static ssize_t extract_entropy(struct entropy_store *r, void * buf,
1290 size_t nbytes, int flags)
1291{
1292 ssize_t ret, i;
1293 __u32 tmp[TMP_BUF_SIZE];
1294 __u32 x;
1295
1296 add_timer_randomness(&extract_timer_state, nbytes);
1297
1298
1299 if (r->entropy_count > r->poolinfo.POOLBITS)
1300 r->entropy_count = r->poolinfo.POOLBITS;
1301
1302 if (flags & EXTRACT_ENTROPY_SECONDARY)
1303 xfer_secondary_pool(r, nbytes, tmp);
1304
1305 DEBUG_ENT("%s has %d bits, want %d bits\n",
1306 r == sec_random_state ? "secondary" :
1307 r == random_state ? "primary" : "unknown",
1308 r->entropy_count, nbytes * 8);
1309
1310 if (r->entropy_count / 8 >= nbytes)
1311 r->entropy_count -= nbytes*8;
1312 else
1313 r->entropy_count = 0;
1314
1315 if (r->entropy_count < random_write_wakeup_thresh)
1316 wake_up_interruptible(&random_write_wait);
1317
1318 r->extract_count += nbytes;
1319
1320 ret = 0;
1321 while (nbytes) {
1322
1323
1324
1325 if ((flags & EXTRACT_ENTROPY_USER) && current->need_resched) {
1326 if (signal_pending(current)) {
1327 if (ret == 0)
1328 ret = -ERESTARTSYS;
1329 break;
1330 }
1331 schedule();
1332 }
1333
1334
1335 tmp[0] = 0x67452301;
1336 tmp[1] = 0xefcdab89;
1337 tmp[2] = 0x98badcfe;
1338 tmp[3] = 0x10325476;
1339#ifdef USE_SHA
1340 tmp[4] = 0xc3d2e1f0;
1341#endif
1342
1343
1344
1345
1346
1347
1348
1349
1350 for (i = 0, x = 0; i < r->poolinfo.poolwords; i += 16, x+=2) {
1351 HASH_TRANSFORM(tmp, r->pool+i);
1352 add_entropy_words(r, &tmp[x%HASH_BUFFER_SIZE], 1);
1353 }
1354
1355
1356
1357
1358
1359 for (i = 0; i < HASH_BUFFER_SIZE/2; i++)
1360 tmp[i] ^= tmp[i + (HASH_BUFFER_SIZE+1)/2];
1361#if HASH_BUFFER_SIZE & 1
1362 x = tmp[HASH_BUFFER_SIZE/2];
1363 x ^= (x >> 16);
1364 ((__u16 *)tmp)[HASH_BUFFER_SIZE-1] = (__u16)x;
1365#endif
1366
1367
1368 i = min(nbytes, HASH_BUFFER_SIZE*sizeof(__u32)/2);
1369 if (flags & EXTRACT_ENTROPY_USER) {
1370 i -= copy_to_user(buf, (__u8 const *)tmp, i);
1371 if (!i) {
1372 ret = -EFAULT;
1373 break;
1374 }
1375 } else
1376 memcpy(buf, (__u8 const *)tmp, i);
1377 nbytes -= i;
1378 buf += i;
1379 ret += i;
1380 add_timer_randomness(&extract_timer_state, nbytes);
1381 }
1382
1383
1384 memset(tmp, 0, sizeof(tmp));
1385
1386 return ret;
1387}
1388
1389
1390
1391
1392
1393
1394void get_random_bytes(void *buf, int nbytes)
1395{
1396 if (sec_random_state)
1397 extract_entropy(sec_random_state, (char *) buf, nbytes,
1398 EXTRACT_ENTROPY_SECONDARY);
1399 else if (random_state)
1400 extract_entropy(random_state, (char *) buf, nbytes, 0);
1401 else
1402 printk(KERN_NOTICE "get_random_bytes called before "
1403 "random driver initialization\n");
1404}
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417static void init_std_data(struct entropy_store *r)
1418{
1419 struct timeval tv;
1420 __u32 words[2];
1421 char *p;
1422 int i;
1423
1424 do_gettimeofday(&tv);
1425 words[0] = tv.tv_sec;
1426 words[1] = tv.tv_usec;
1427 add_entropy_words(r, words, 2);
1428
1429
1430
1431
1432
1433 p = (char *) &system_utsname;
1434 for (i = sizeof(system_utsname) / sizeof(words); i; i--) {
1435 memcpy(words, p, sizeof(words));
1436 add_entropy_words(r, words, sizeof(words)/4);
1437 p += sizeof(words);
1438 }
1439}
1440
1441void __init rand_initialize(void)
1442{
1443 int i;
1444
1445 if (create_entropy_store(DEFAULT_POOL_SIZE, &random_state))
1446 return;
1447 if (batch_entropy_init(BATCH_ENTROPY_SIZE, random_state))
1448 return;
1449 if (create_entropy_store(SECONDARY_POOL_SIZE, &sec_random_state))
1450 return;
1451 clear_entropy_store(random_state);
1452 clear_entropy_store(sec_random_state);
1453 init_std_data(random_state);
1454#ifdef CONFIG_SYSCTL
1455 sysctl_init_random(random_state);
1456#endif
1457#ifndef CONFIG_ARCH_S390
1458 for (i = 0; i < NR_IRQS; i++)
1459 irq_timer_state[i] = NULL;
1460#endif
1461 for (i = 0; i < MAX_BLKDEV; i++)
1462 blkdev_timer_state[i] = NULL;
1463 memset(&keyboard_timer_state, 0, sizeof(struct timer_rand_state));
1464 memset(&mouse_timer_state, 0, sizeof(struct timer_rand_state));
1465 memset(&extract_timer_state, 0, sizeof(struct timer_rand_state));
1466 extract_timer_state.dont_count_entropy = 1;
1467}
1468
1469#ifndef CONFIG_ARCH_S390
1470void rand_initialize_irq(int irq)
1471{
1472 struct timer_rand_state *state;
1473
1474 if (irq >= NR_IRQS || irq_timer_state[irq])
1475 return;
1476
1477
1478
1479
1480
1481 state = kmalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
1482 if (state) {
1483 memset(state, 0, sizeof(struct timer_rand_state));
1484 irq_timer_state[irq] = state;
1485 }
1486}
1487#endif
1488
1489void rand_initialize_blkdev(int major, int mode)
1490{
1491 struct timer_rand_state *state;
1492
1493 if (major >= MAX_BLKDEV || blkdev_timer_state[major])
1494 return;
1495
1496
1497
1498
1499
1500 state = kmalloc(sizeof(struct timer_rand_state), mode);
1501 if (state) {
1502 memset(state, 0, sizeof(struct timer_rand_state));
1503 blkdev_timer_state[major] = state;
1504 }
1505}
1506
1507
1508static ssize_t
1509random_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
1510{
1511 DECLARE_WAITQUEUE(wait, current);
1512 ssize_t n, retval = 0, count = 0;
1513
1514 if (nbytes == 0)
1515 return 0;
1516
1517 add_wait_queue(&random_read_wait, &wait);
1518 while (nbytes > 0) {
1519 set_current_state(TASK_INTERRUPTIBLE);
1520
1521 n = nbytes;
1522 if (n > SEC_XFER_SIZE)
1523 n = SEC_XFER_SIZE;
1524 if (n > random_state->entropy_count / 8)
1525 n = random_state->entropy_count / 8;
1526 if (n == 0) {
1527 if (file->f_flags & O_NONBLOCK) {
1528 retval = -EAGAIN;
1529 break;
1530 }
1531 if (signal_pending(current)) {
1532 retval = -ERESTARTSYS;
1533 break;
1534 }
1535 schedule();
1536 continue;
1537 }
1538 n = extract_entropy(sec_random_state, buf, n,
1539 EXTRACT_ENTROPY_USER |
1540 EXTRACT_ENTROPY_SECONDARY);
1541 if (n < 0) {
1542 retval = n;
1543 break;
1544 }
1545 count += n;
1546 buf += n;
1547 nbytes -= n;
1548 break;
1549
1550 }
1551 current->state = TASK_RUNNING;
1552 remove_wait_queue(&random_read_wait, &wait);
1553
1554
1555
1556
1557 if (count != 0) {
1558 UPDATE_ATIME(file->f_dentry->d_inode);
1559 }
1560
1561 return (count ? count : retval);
1562}
1563
1564static ssize_t
1565urandom_read(struct file * file, char * buf,
1566 size_t nbytes, loff_t *ppos)
1567{
1568 return extract_entropy(sec_random_state, buf, nbytes,
1569 EXTRACT_ENTROPY_USER |
1570 EXTRACT_ENTROPY_SECONDARY);
1571}
1572
1573static unsigned int
1574random_poll(struct file *file, poll_table * wait)
1575{
1576 unsigned int mask;
1577
1578 poll_wait(file, &random_read_wait, wait);
1579 poll_wait(file, &random_write_wait, wait);
1580 mask = 0;
1581 if (random_state->entropy_count >= random_read_wakeup_thresh)
1582 mask |= POLLIN | POLLRDNORM;
1583 if (random_state->entropy_count < random_write_wakeup_thresh)
1584 mask |= POLLOUT | POLLWRNORM;
1585 return mask;
1586}
1587
1588static ssize_t
1589random_write(struct file * file, const char * buffer,
1590 size_t count, loff_t *ppos)
1591{
1592 int ret = 0;
1593 size_t bytes;
1594 __u32 buf[16];
1595 const char *p = buffer;
1596 size_t c = count;
1597
1598 while (c > 0) {
1599 bytes = min(c, sizeof(buf));
1600
1601 bytes -= copy_from_user(&buf, p, bytes);
1602 if (!bytes) {
1603 ret = -EFAULT;
1604 break;
1605 }
1606 c -= bytes;
1607 p += bytes;
1608
1609 add_entropy_words(random_state, buf, (bytes + 3) / 4);
1610 }
1611 if (p == buffer) {
1612 return (ssize_t)ret;
1613 } else {
1614 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
1615 mark_inode_dirty(file->f_dentry->d_inode);
1616 return (ssize_t)(p - buffer);
1617 }
1618}
1619
1620static int
1621random_ioctl(struct inode * inode, struct file * file,
1622 unsigned int cmd, unsigned long arg)
1623{
1624 int *p, size, ent_count;
1625 int retval;
1626
1627 switch (cmd) {
1628 case RNDGETENTCNT:
1629 ent_count = random_state->entropy_count;
1630 if (put_user(ent_count, (int *) arg))
1631 return -EFAULT;
1632 return 0;
1633 case RNDADDTOENTCNT:
1634 if (!capable(CAP_SYS_ADMIN))
1635 return -EPERM;
1636 if (get_user(ent_count, (int *) arg))
1637 return -EFAULT;
1638 credit_entropy_store(random_state, ent_count);
1639
1640
1641
1642
1643 if (random_state->entropy_count >= random_read_wakeup_thresh)
1644 wake_up_interruptible(&random_read_wait);
1645 return 0;
1646 case RNDGETPOOL:
1647 if (!capable(CAP_SYS_ADMIN))
1648 return -EPERM;
1649 p = (int *) arg;
1650 ent_count = random_state->entropy_count;
1651 if (put_user(ent_count, p++) ||
1652 get_user(size, p) ||
1653 put_user(random_state->poolinfo.poolwords, p++))
1654 return -EFAULT;
1655 if (size < 0)
1656 return -EINVAL;
1657 if (size > random_state->poolinfo.poolwords)
1658 size = random_state->poolinfo.poolwords;
1659 if (copy_to_user(p, random_state->pool, size * sizeof(__u32)))
1660 return -EFAULT;
1661 return 0;
1662 case RNDADDENTROPY:
1663 if (!capable(CAP_SYS_ADMIN))
1664 return -EPERM;
1665 p = (int *) arg;
1666 if (get_user(ent_count, p++))
1667 return -EFAULT;
1668 if (ent_count < 0)
1669 return -EINVAL;
1670 if (get_user(size, p++))
1671 return -EFAULT;
1672 retval = random_write(file, (const char *) p,
1673 size, &file->f_pos);
1674 if (retval < 0)
1675 return retval;
1676 credit_entropy_store(random_state, ent_count);
1677
1678
1679
1680
1681 if (random_state->entropy_count >= random_read_wakeup_thresh)
1682 wake_up_interruptible(&random_read_wait);
1683 return 0;
1684 case RNDZAPENTCNT:
1685 if (!capable(CAP_SYS_ADMIN))
1686 return -EPERM;
1687 random_state->entropy_count = 0;
1688 return 0;
1689 case RNDCLEARPOOL:
1690
1691 if (!capable(CAP_SYS_ADMIN))
1692 return -EPERM;
1693 clear_entropy_store(random_state);
1694 init_std_data(random_state);
1695 return 0;
1696 default:
1697 return -EINVAL;
1698 }
1699}
1700
1701struct file_operations random_fops = {
1702 read: random_read,
1703 write: random_write,
1704 poll: random_poll,
1705 ioctl: random_ioctl,
1706};
1707
1708struct file_operations urandom_fops = {
1709 read: urandom_read,
1710 write: random_write,
1711 ioctl: random_ioctl,
1712};
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724void generate_random_uuid(unsigned char uuid_out[16])
1725{
1726 get_random_bytes(uuid_out, 16);
1727
1728 uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40;
1729
1730 uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
1731}
1732
1733
1734
1735
1736
1737
1738
1739#ifdef CONFIG_SYSCTL
1740
1741#include <linux/sysctl.h>
1742
1743static int sysctl_poolsize;
1744static int min_read_thresh, max_read_thresh;
1745static int min_write_thresh, max_write_thresh;
1746static char sysctl_bootid[16];
1747
1748
1749
1750
1751
1752static int change_poolsize(int poolsize)
1753{
1754 struct entropy_store *new_store, *old_store;
1755 int ret;
1756
1757 if ((ret = create_entropy_store(poolsize, &new_store)))
1758 return ret;
1759
1760 add_entropy_words(new_store, random_state->pool,
1761 random_state->poolinfo.poolwords);
1762 credit_entropy_store(new_store, random_state->entropy_count);
1763
1764 sysctl_init_random(new_store);
1765 old_store = random_state;
1766 random_state = batch_tqueue.data = new_store;
1767 free_entropy_store(old_store);
1768 return 0;
1769}
1770
1771static int proc_do_poolsize(ctl_table *table, int write, struct file *filp,
1772 void *buffer, size_t *lenp)
1773{
1774 int ret;
1775
1776 sysctl_poolsize = random_state->poolinfo.POOLBYTES;
1777
1778 ret = proc_dointvec(table, write, filp, buffer, lenp);
1779 if (ret || !write ||
1780 (sysctl_poolsize == random_state->poolinfo.POOLBYTES))
1781 return ret;
1782
1783 return change_poolsize(sysctl_poolsize);
1784}
1785
1786static int poolsize_strategy(ctl_table *table, int *name, int nlen,
1787 void *oldval, size_t *oldlenp,
1788 void *newval, size_t newlen, void **context)
1789{
1790 int len;
1791
1792 sysctl_poolsize = random_state->poolinfo.POOLBYTES;
1793
1794
1795
1796
1797
1798
1799 if (newval && newlen) {
1800 len = newlen;
1801 if (len > table->maxlen)
1802 len = table->maxlen;
1803 if (copy_from_user(table->data, newval, len))
1804 return -EFAULT;
1805 }
1806
1807 if (sysctl_poolsize != random_state->poolinfo.POOLBYTES)
1808 return change_poolsize(sysctl_poolsize);
1809
1810 return 0;
1811}
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822static int proc_do_uuid(ctl_table *table, int write, struct file *filp,
1823 void *buffer, size_t *lenp)
1824{
1825 ctl_table fake_table;
1826 unsigned char buf[64], tmp_uuid[16], *uuid;
1827
1828 uuid = table->data;
1829 if (!uuid) {
1830 uuid = tmp_uuid;
1831 uuid[8] = 0;
1832 }
1833 if (uuid[8] == 0)
1834 generate_random_uuid(uuid);
1835
1836 sprintf(buf, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
1837 "%02x%02x%02x%02x%02x%02x",
1838 uuid[0], uuid[1], uuid[2], uuid[3],
1839 uuid[4], uuid[5], uuid[6], uuid[7],
1840 uuid[8], uuid[9], uuid[10], uuid[11],
1841 uuid[12], uuid[13], uuid[14], uuid[15]);
1842 fake_table.data = buf;
1843 fake_table.maxlen = sizeof(buf);
1844
1845 return proc_dostring(&fake_table, write, filp, buffer, lenp);
1846}
1847
1848static int uuid_strategy(ctl_table *table, int *name, int nlen,
1849 void *oldval, size_t *oldlenp,
1850 void *newval, size_t newlen, void **context)
1851{
1852 unsigned char tmp_uuid[16], *uuid;
1853 unsigned int len;
1854
1855 if (!oldval || !oldlenp)
1856 return 1;
1857
1858 uuid = table->data;
1859 if (!uuid) {
1860 uuid = tmp_uuid;
1861 uuid[8] = 0;
1862 }
1863 if (uuid[8] == 0)
1864 generate_random_uuid(uuid);
1865
1866 if (get_user(len, oldlenp))
1867 return -EFAULT;
1868 if (len) {
1869 if (len > 16)
1870 len = 16;
1871 if (copy_to_user(oldval, uuid, len) ||
1872 put_user(len, oldlenp))
1873 return -EFAULT;
1874 }
1875 return 1;
1876}
1877
1878ctl_table random_table[] = {
1879 {RANDOM_POOLSIZE, "poolsize",
1880 &sysctl_poolsize, sizeof(int), 0644, NULL,
1881 &proc_do_poolsize, &poolsize_strategy},
1882 {RANDOM_ENTROPY_COUNT, "entropy_avail",
1883 NULL, sizeof(int), 0444, NULL,
1884 &proc_dointvec},
1885 {RANDOM_READ_THRESH, "read_wakeup_threshold",
1886 &random_read_wakeup_thresh, sizeof(int), 0644, NULL,
1887 &proc_dointvec_minmax, &sysctl_intvec, 0,
1888 &min_read_thresh, &max_read_thresh},
1889 {RANDOM_WRITE_THRESH, "write_wakeup_threshold",
1890 &random_write_wakeup_thresh, sizeof(int), 0644, NULL,
1891 &proc_dointvec_minmax, &sysctl_intvec, 0,
1892 &min_write_thresh, &max_write_thresh},
1893 {RANDOM_BOOT_ID, "boot_id",
1894 &sysctl_bootid, 16, 0444, NULL,
1895 &proc_do_uuid, &uuid_strategy},
1896 {RANDOM_UUID, "uuid",
1897 NULL, 16, 0444, NULL,
1898 &proc_do_uuid, &uuid_strategy},
1899 {0}
1900};
1901
1902static void sysctl_init_random(struct entropy_store *random_state)
1903{
1904 min_read_thresh = 8;
1905 min_write_thresh = 0;
1906 max_read_thresh = max_write_thresh = random_state->poolinfo.POOLBITS;
1907 random_table[1].data = &random_state->entropy_count;
1908}
1909#endif
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
1932#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
1933#define H(x, y, z) ((x) ^ (y) ^ (z))
1934
1935
1936
1937
1938
1939
1940
1941#define ROUND(f, a, b, c, d, x, s) \
1942 (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
1943#define K1 0
1944#define K2 013240474631UL
1945#define K3 015666365641UL
1946
1947
1948
1949
1950static __u32 halfMD4Transform (__u32 const buf[4], __u32 const in[8])
1951{
1952 __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
1953
1954
1955 ROUND(F, a, b, c, d, in[0] + K1, 3);
1956 ROUND(F, d, a, b, c, in[1] + K1, 7);
1957 ROUND(F, c, d, a, b, in[2] + K1, 11);
1958 ROUND(F, b, c, d, a, in[3] + K1, 19);
1959 ROUND(F, a, b, c, d, in[4] + K1, 3);
1960 ROUND(F, d, a, b, c, in[5] + K1, 7);
1961 ROUND(F, c, d, a, b, in[6] + K1, 11);
1962 ROUND(F, b, c, d, a, in[7] + K1, 19);
1963
1964
1965 ROUND(G, a, b, c, d, in[1] + K2, 3);
1966 ROUND(G, d, a, b, c, in[3] + K2, 5);
1967 ROUND(G, c, d, a, b, in[5] + K2, 9);
1968 ROUND(G, b, c, d, a, in[7] + K2, 13);
1969 ROUND(G, a, b, c, d, in[0] + K2, 3);
1970 ROUND(G, d, a, b, c, in[2] + K2, 5);
1971 ROUND(G, c, d, a, b, in[4] + K2, 9);
1972 ROUND(G, b, c, d, a, in[6] + K2, 13);
1973
1974
1975 ROUND(H, a, b, c, d, in[3] + K3, 3);
1976 ROUND(H, d, a, b, c, in[7] + K3, 9);
1977 ROUND(H, c, d, a, b, in[2] + K3, 11);
1978 ROUND(H, b, c, d, a, in[6] + K3, 15);
1979 ROUND(H, a, b, c, d, in[1] + K3, 3);
1980 ROUND(H, d, a, b, c, in[5] + K3, 9);
1981 ROUND(H, c, d, a, b, in[0] + K3, 11);
1982 ROUND(H, b, c, d, a, in[4] + K3, 15);
1983
1984 return buf[1] + b;
1985
1986}
1987
1988#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1989
1990static __u32 twothirdsMD4Transform (__u32 const buf[4], __u32 const in[12])
1991{
1992 __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
1993
1994
1995 ROUND(F, a, b, c, d, in[ 0] + K1, 3);
1996 ROUND(F, d, a, b, c, in[ 1] + K1, 7);
1997 ROUND(F, c, d, a, b, in[ 2] + K1, 11);
1998 ROUND(F, b, c, d, a, in[ 3] + K1, 19);
1999 ROUND(F, a, b, c, d, in[ 4] + K1, 3);
2000 ROUND(F, d, a, b, c, in[ 5] + K1, 7);
2001 ROUND(F, c, d, a, b, in[ 6] + K1, 11);
2002 ROUND(F, b, c, d, a, in[ 7] + K1, 19);
2003 ROUND(F, a, b, c, d, in[ 8] + K1, 3);
2004 ROUND(F, d, a, b, c, in[ 9] + K1, 7);
2005 ROUND(F, c, d, a, b, in[10] + K1, 11);
2006 ROUND(F, b, c, d, a, in[11] + K1, 19);
2007
2008
2009 ROUND(G, a, b, c, d, in[ 1] + K2, 3);
2010 ROUND(G, d, a, b, c, in[ 3] + K2, 5);
2011 ROUND(G, c, d, a, b, in[ 5] + K2, 9);
2012 ROUND(G, b, c, d, a, in[ 7] + K2, 13);
2013 ROUND(G, a, b, c, d, in[ 9] + K2, 3);
2014 ROUND(G, d, a, b, c, in[11] + K2, 5);
2015 ROUND(G, c, d, a, b, in[ 0] + K2, 9);
2016 ROUND(G, b, c, d, a, in[ 2] + K2, 13);
2017 ROUND(G, a, b, c, d, in[ 4] + K2, 3);
2018 ROUND(G, d, a, b, c, in[ 6] + K2, 5);
2019 ROUND(G, c, d, a, b, in[ 8] + K2, 9);
2020 ROUND(G, b, c, d, a, in[10] + K2, 13);
2021
2022
2023 ROUND(H, a, b, c, d, in[ 3] + K3, 3);
2024 ROUND(H, d, a, b, c, in[ 7] + K3, 9);
2025 ROUND(H, c, d, a, b, in[11] + K3, 11);
2026 ROUND(H, b, c, d, a, in[ 2] + K3, 15);
2027 ROUND(H, a, b, c, d, in[ 6] + K3, 3);
2028 ROUND(H, d, a, b, c, in[10] + K3, 9);
2029 ROUND(H, c, d, a, b, in[ 1] + K3, 11);
2030 ROUND(H, b, c, d, a, in[ 5] + K3, 15);
2031 ROUND(H, a, b, c, d, in[ 9] + K3, 3);
2032 ROUND(H, d, a, b, c, in[ 0] + K3, 9);
2033 ROUND(H, c, d, a, b, in[ 4] + K3, 11);
2034 ROUND(H, b, c, d, a, in[ 8] + K3, 15);
2035
2036 return buf[1] + b;
2037
2038}
2039#endif
2040
2041#undef ROUND
2042#undef F
2043#undef G
2044#undef H
2045#undef K1
2046#undef K2
2047#undef K3
2048
2049
2050#define REKEY_INTERVAL 300
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070#define COUNT_BITS 8
2071#define COUNT_MASK ( (1<<COUNT_BITS)-1)
2072#define HASH_BITS 24
2073#define HASH_MASK ( (1<<HASH_BITS)-1 )
2074
2075static struct keydata {
2076 time_t rekey_time;
2077 __u32 count;
2078 __u32 secret[12];
2079} ____cacheline_aligned ip_keydata[2];
2080
2081static spinlock_t ip_lock = SPIN_LOCK_UNLOCKED;
2082static unsigned int ip_cnt;
2083
2084static struct keydata *__check_and_rekey(time_t time)
2085{
2086 struct keydata *keyptr;
2087 spin_lock_bh(&ip_lock);
2088 keyptr = &ip_keydata[ip_cnt&1];
2089 if (!keyptr->rekey_time || (time - keyptr->rekey_time) > REKEY_INTERVAL) {
2090 keyptr = &ip_keydata[1^(ip_cnt&1)];
2091 keyptr->rekey_time = time;
2092 get_random_bytes(keyptr->secret, sizeof(keyptr->secret));
2093 keyptr->count = (ip_cnt&COUNT_MASK)<<HASH_BITS;
2094 mb();
2095 ip_cnt++;
2096 }
2097 spin_unlock_bh(&ip_lock);
2098 return keyptr;
2099}
2100
2101static inline struct keydata *check_and_rekey(time_t time)
2102{
2103 struct keydata *keyptr = &ip_keydata[ip_cnt&1];
2104
2105 rmb();
2106 if (!keyptr->rekey_time || (time - keyptr->rekey_time) > REKEY_INTERVAL) {
2107 keyptr = __check_and_rekey(time);
2108 }
2109
2110 return keyptr;
2111}
2112
2113#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2114__u32 secure_tcpv6_sequence_number(__u32 *saddr, __u32 *daddr,
2115 __u16 sport, __u16 dport)
2116{
2117 struct timeval tv;
2118 __u32 seq;
2119 __u32 hash[12];
2120 struct keydata *keyptr;
2121
2122
2123
2124
2125
2126 do_gettimeofday(&tv);
2127 keyptr = check_and_rekey(tv.tv_sec);
2128
2129 memcpy(hash, saddr, 16);
2130 hash[4]=(sport << 16) + dport;
2131 memcpy(&hash[5],keyptr->secret,sizeof(__u32)*7);
2132
2133 seq = twothirdsMD4Transform(daddr, hash) & HASH_MASK;
2134 seq += keyptr->count;
2135 seq += tv.tv_usec + tv.tv_sec*1000000;
2136
2137 return seq;
2138}
2139
2140__u32 secure_ipv6_id(__u32 *daddr)
2141{
2142 struct keydata *keyptr;
2143
2144 keyptr = check_and_rekey(CURRENT_TIME);
2145
2146 return halfMD4Transform(daddr, keyptr->secret);
2147}
2148
2149#endif
2150
2151
2152__u32 secure_tcp_sequence_number(__u32 saddr, __u32 daddr,
2153 __u16 sport, __u16 dport)
2154{
2155 struct timeval tv;
2156 __u32 seq;
2157 __u32 hash[4];
2158 struct keydata *keyptr;
2159
2160
2161
2162
2163 do_gettimeofday(&tv);
2164 keyptr = check_and_rekey(tv.tv_sec);
2165
2166
2167
2168
2169
2170
2171
2172 hash[0]=saddr;
2173 hash[1]=daddr;
2174 hash[2]=(sport << 16) + dport;
2175 hash[3]=keyptr->secret[11];
2176
2177 seq = halfMD4Transform(hash, keyptr->secret) & HASH_MASK;
2178 seq += keyptr->count;
2179
2180
2181
2182
2183
2184
2185
2186
2187 seq += tv.tv_usec + tv.tv_sec*1000000;
2188#if 0
2189 printk("init_seq(%lx, %lx, %d, %d) = %d\n",
2190 saddr, daddr, sport, dport, seq);
2191#endif
2192 return seq;
2193}
2194
2195
2196
2197
2198__u32 secure_ip_id(__u32 daddr)
2199{
2200 struct keydata *keyptr;
2201 __u32 hash[4];
2202
2203 keyptr = check_and_rekey(CURRENT_TIME);
2204
2205
2206
2207
2208
2209
2210 hash[0] = daddr;
2211 hash[1] = keyptr->secret[9];
2212 hash[2] = keyptr->secret[10];
2213 hash[3] = keyptr->secret[11];
2214
2215 return halfMD4Transform(hash, keyptr->secret);
2216}
2217
2218#ifdef CONFIG_SYN_COOKIES
2219
2220
2221
2222
2223
2224
2225
2226
2227#define COOKIEBITS 24
2228#define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
2229
2230static int syncookie_init;
2231static __u32 syncookie_secret[2][16-3+HASH_BUFFER_SIZE];
2232
2233__u32 secure_tcp_syn_cookie(__u32 saddr, __u32 daddr, __u16 sport,
2234 __u16 dport, __u32 sseq, __u32 count, __u32 data)
2235{
2236 __u32 tmp[16 + HASH_BUFFER_SIZE + HASH_EXTRA_SIZE];
2237 __u32 seq;
2238
2239
2240
2241
2242 if (syncookie_init == 0) {
2243 get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
2244 syncookie_init = 1;
2245 }
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258 memcpy(tmp+3, syncookie_secret[0], sizeof(syncookie_secret[0]));
2259 tmp[0]=saddr;
2260 tmp[1]=daddr;
2261 tmp[2]=(sport << 16) + dport;
2262 HASH_TRANSFORM(tmp+16, tmp);
2263 seq = tmp[17] + sseq + (count << COOKIEBITS);
2264
2265 memcpy(tmp+3, syncookie_secret[1], sizeof(syncookie_secret[1]));
2266 tmp[0]=saddr;
2267 tmp[1]=daddr;
2268 tmp[2]=(sport << 16) + dport;
2269 tmp[3] = count;
2270 HASH_TRANSFORM(tmp+16, tmp);
2271
2272
2273 return seq + ((tmp[17] + data) & COOKIEMASK);
2274}
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285__u32 check_tcp_syn_cookie(__u32 cookie, __u32 saddr, __u32 daddr, __u16 sport,
2286 __u16 dport, __u32 sseq, __u32 count, __u32 maxdiff)
2287{
2288 __u32 tmp[16 + HASH_BUFFER_SIZE + HASH_EXTRA_SIZE];
2289 __u32 diff;
2290
2291 if (syncookie_init == 0)
2292 return (__u32)-1;
2293
2294
2295 memcpy(tmp+3, syncookie_secret[0], sizeof(syncookie_secret[0]));
2296 tmp[0]=saddr;
2297 tmp[1]=daddr;
2298 tmp[2]=(sport << 16) + dport;
2299 HASH_TRANSFORM(tmp+16, tmp);
2300 cookie -= tmp[17] + sseq;
2301
2302
2303 diff = (count - (cookie >> COOKIEBITS)) & ((__u32)-1 >> COOKIEBITS);
2304 if (diff >= maxdiff)
2305 return (__u32)-1;
2306
2307 memcpy(tmp+3, syncookie_secret[1], sizeof(syncookie_secret[1]));
2308 tmp[0] = saddr;
2309 tmp[1] = daddr;
2310 tmp[2] = (sport << 16) + dport;
2311 tmp[3] = count - diff;
2312 HASH_TRANSFORM(tmp+16, tmp);
2313
2314 return (cookie - tmp[17]) & COOKIEMASK;
2315}
2316#endif
2317
2318
2319
2320#ifndef CONFIG_ARCH_S390
2321EXPORT_SYMBOL(add_keyboard_randomness);
2322EXPORT_SYMBOL(add_mouse_randomness);
2323EXPORT_SYMBOL(add_interrupt_randomness);
2324#endif
2325EXPORT_SYMBOL(add_blkdev_randomness);
2326EXPORT_SYMBOL(batch_entropy_store);
2327EXPORT_SYMBOL(generate_random_uuid);
2328
2329