1 | /* |
---|
2 | * Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com> |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by the |
---|
6 | * Free Software Foundation; either version 2 of the License, or (at your |
---|
7 | * option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, but |
---|
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
11 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
12 | * for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License along |
---|
15 | * with this program; if not, write to the Free software Foundation, Inc., |
---|
16 | * 59 Temple Place, suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | */ |
---|
19 | #include <linux/config.h> |
---|
20 | #include <linux/module.h> |
---|
21 | #include <linux/init.h> |
---|
22 | #include <linux/sched.h> |
---|
23 | #include <linux/random.h> |
---|
24 | |
---|
25 | #include "hash_table.h" |
---|
26 | |
---|
27 | #define MODULE_NAME "RANDOM" |
---|
28 | #define DEBUG 1 |
---|
29 | #undef DEBUG |
---|
30 | #include "debug.h" |
---|
31 | |
---|
32 | /** @file |
---|
33 | * Source of randomness. |
---|
34 | * Current implementation is not enough. |
---|
35 | * Needs to be cryptographically strong. |
---|
36 | */ |
---|
37 | |
---|
38 | static unsigned long seed = 0; |
---|
39 | static unsigned long count = 0; |
---|
40 | |
---|
41 | /** Contribute some random bytes. |
---|
42 | * |
---|
43 | * @param src bytes to contribute |
---|
44 | * @param src_n number of bytes |
---|
45 | */ |
---|
46 | void add_random_bytes(const void *src, int src_n){ |
---|
47 | ++count; |
---|
48 | seed = hash_hvoid(seed, &count, sizeof(count)); |
---|
49 | seed = hash_hvoid(seed, src, src_n); |
---|
50 | } |
---|
51 | |
---|
52 | /** Get one random byte. |
---|
53 | * |
---|
54 | * @return random byte |
---|
55 | */ |
---|
56 | int get_random_byte(void){ |
---|
57 | int tmp = jiffies; |
---|
58 | add_random_bytes(&tmp, sizeof(tmp)); |
---|
59 | return seed; |
---|
60 | } |
---|
61 | |
---|
62 | #ifndef __KERNEL__ |
---|
63 | /* Get some random bytes. |
---|
64 | * |
---|
65 | * @param dst destination for the bytes |
---|
66 | * @param dst_n number of bytes to get |
---|
67 | */ |
---|
68 | void get_random_bytes(void *dst, int dst_n){ |
---|
69 | int i; |
---|
70 | char *p = (char *)dst; |
---|
71 | for(i = 0; i < dst_n; i++){ |
---|
72 | *p++ = get_random_byte(); |
---|
73 | } |
---|
74 | } |
---|
75 | #endif |
---|
76 | |
---|
77 | int __init random_module_init(void){ |
---|
78 | int dummy; |
---|
79 | int tmp = jiffies; |
---|
80 | seed = (unsigned long)&dummy; |
---|
81 | add_random_bytes(&tmp, sizeof(tmp)); |
---|
82 | return 0; |
---|
83 | } |
---|
84 | |
---|
85 | void __exit random_module_exit(void){ |
---|
86 | } |
---|
87 | |
---|