Mercurial > dropbear
comparison random.c @ 272:3be7ae2e8dfa
Only read /dev/random once when the program starts
rather than for every connection, to "conserve entropy".
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 11 Feb 2006 15:15:37 +0000 |
parents | 65585699d980 |
children | 79bf1023cf11 7dad470ad4aa |
comparison
equal
deleted
inserted
replaced
271:be18c7dd486e | 272:3be7ae2e8dfa |
---|---|
28 #include "bignum.h" | 28 #include "bignum.h" |
29 | 29 |
30 static int donerandinit = 0; | 30 static int donerandinit = 0; |
31 | 31 |
32 /* this is used to generate unique output from the same hashpool */ | 32 /* this is used to generate unique output from the same hashpool */ |
33 static unsigned int counter = 0; | 33 static uint32_t counter = 0; |
34 #define MAX_COUNTER 1000000/* the max value for the counter, so it won't loop */ | 34 #define MAX_COUNTER 1<<31 /* the max value for the counter, so it won't loop */ |
35 | 35 |
36 static unsigned char hashpool[SHA1_HASH_SIZE]; | 36 static unsigned char hashpool[SHA1_HASH_SIZE]; |
37 | 37 |
38 #define INIT_SEED_SIZE 32 /* 256 bits */ | 38 #define INIT_SEED_SIZE 32 /* 256 bits */ |
39 | 39 |
130 | 130 |
131 unsigned char readbuf[INIT_SEED_SIZE]; | 131 unsigned char readbuf[INIT_SEED_SIZE]; |
132 | 132 |
133 hash_state hs; | 133 hash_state hs; |
134 | 134 |
135 /* initialise so compilers will be happy about hashing it */ | 135 /* initialise so that things won't warn about |
136 * hashing an undefined buffer */ | |
136 if (!donerandinit) { | 137 if (!donerandinit) { |
137 m_burn(hashpool, sizeof(hashpool)); | 138 m_burn(hashpool, sizeof(hashpool)); |
138 } | 139 } |
139 | 140 |
140 /* get the seed data */ | 141 /* get the seed data */ |
146 sha1_process(&hs, (void*)readbuf, sizeof(readbuf)); | 147 sha1_process(&hs, (void*)readbuf, sizeof(readbuf)); |
147 sha1_done(&hs, hashpool); | 148 sha1_done(&hs, hashpool); |
148 | 149 |
149 counter = 0; | 150 counter = 0; |
150 donerandinit = 1; | 151 donerandinit = 1; |
152 } | |
153 | |
154 /* hash the current random pool with some unique identifiers | |
155 * for this process and point-in-time. this is used to separate | |
156 * the random pools for fork()ed processes. */ | |
157 void reseedrandom() { | |
158 | |
159 pid_t pid; | |
160 struct timeval tv; | |
161 | |
162 if (!donerandinit) { | |
163 dropbear_exit("seedrandom not done"); | |
164 } | |
165 | |
166 pid = getpid(); | |
167 gettimeofday(&tv, NULL); | |
168 | |
169 hash_state hs; | |
170 unsigned char hash[SHA1_HASH_SIZE]; | |
171 sha1_init(&hs); | |
172 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); | |
173 sha1_process(&hs, (void*)&pid, sizeof(pid)); | |
174 sha1_process(&hs, (void*)&tv, sizeof(tv)); | |
175 sha1_done(&hs, hashpool); | |
151 } | 176 } |
152 | 177 |
153 /* return len bytes of pseudo-random data */ | 178 /* return len bytes of pseudo-random data */ |
154 void genrandom(unsigned char* buf, unsigned int len) { | 179 void genrandom(unsigned char* buf, unsigned int len) { |
155 | 180 |