comparison random.c @ 305:1876c6bb084b ucc-axis-hack

A few changes for the 0.48 merge
author matt-ucc@ucc.asn.au
date Sat, 25 Mar 2006 17:10:27 +0000
parents 7dad470ad4aa
children
comparison
equal deleted inserted replaced
304:1f36a94e01b9 305:1876c6bb084b
34 /* the max value for the counter, so it won't integer overflow */ 34 /* the max value for the counter, so it won't integer overflow */
35 #define MAX_COUNTER 1<<30 35 #define MAX_COUNTER 1<<30
36 36
37 static unsigned char hashpool[SHA1_HASH_SIZE]; 37 static unsigned char hashpool[SHA1_HASH_SIZE];
38 38
39 #define INIT_SEED_SIZE 32 /* 256 bits */ 39 /* 256 bits */
40 #define INIT_SEED_SIZE 32
40 41
41 static void readrand(unsigned char* buf, unsigned int buflen); 42 static void readrand(unsigned char* buf, unsigned int buflen);
42 43
43 /* The basic setup is we read some data from /dev/(u)random or prngd and hash it 44 /* The basic setup is we read some data from /dev/(u)random or prngd and hash it
44 * into hashpool. To read data, we hash together current hashpool contents, 45 * into hashpool. To read data, we hash together current hashpool contents,
127 } 128 }
128 129
129 /* initialise the prng from /dev/(u)random or prngd */ 130 /* initialise the prng from /dev/(u)random or prngd */
130 void seedrandom() { 131 void seedrandom() {
131 132
133 hash_state hs;
132 unsigned char readbuf[INIT_SEED_SIZE]; 134 unsigned char readbuf[INIT_SEED_SIZE];
133 135
134 hash_state hs;
135
136 /* initialise so that things won't warn about 136 /* initialise so that things won't warn about
137 * hashing an undefined buffer */ 137 * hashing an undefined buffer */
138 if (!donerandinit) { 138 if (!donerandinit) {
139 m_burn(hashpool, sizeof(hashpool)); 139 m_burn(hashpool, sizeof(hashpool));
140 } 140 }
141 141
142 /* get the seed data */ 142 /* get the seed data */
155 /* hash the current random pool with some unique identifiers 155 /* hash the current random pool with some unique identifiers
156 * for this process and point-in-time. this is used to separate 156 * for this process and point-in-time. this is used to separate
157 * the random pools for fork()ed processes. */ 157 * the random pools for fork()ed processes. */
158 void reseedrandom() { 158 void reseedrandom() {
159 159
160 pid_t pid; 160 pid_t pid;
161 struct timeval tv; 161 hash_state hs;
162 struct timeval tv;
162 163
163 if (!donerandinit) { 164 if (!donerandinit) {
164 dropbear_exit("seedrandom not done"); 165 dropbear_exit("seedrandom not done");
165 } 166 }
166 167
167 pid = getpid(); 168 pid = getpid();
168 gettimeofday(&tv, NULL); 169 gettimeofday(&tv, NULL);
169 170
170 hash_state hs;
171 sha1_init(&hs); 171 sha1_init(&hs);
172 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); 172 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
173 sha1_process(&hs, (void*)&pid, sizeof(pid)); 173 sha1_process(&hs, (void*)&pid, sizeof(pid));
174 sha1_process(&hs, (void*)&tv, sizeof(tv)); 174 sha1_process(&hs, (void*)&tv, sizeof(tv));
175 sha1_done(&hs, hashpool); 175 sha1_done(&hs, hashpool);
231 randbuf[0] &= masks[size_bits % 8]; 231 randbuf[0] &= masks[size_bits % 8];
232 232
233 bytes_to_mp(rand, randbuf, len); 233 bytes_to_mp(rand, randbuf, len);
234 234
235 /* keep regenerating until we get one satisfying 235 /* keep regenerating until we get one satisfying
236 * 0 < rand < max */ 236 * 0 < rand < max */
237 } while ( ( (max != NULL) && (mp_cmp(rand, max) != MP_LT) ) 237 } while ( ( (max != NULL) && (mp_cmp(rand, max) != MP_LT) )
238 || (mp_cmp_d(rand, 0) != MP_GT) ); 238 || (mp_cmp_d(rand, 0) != MP_GT) );
239 m_burn(randbuf, len); 239 m_burn(randbuf, len);
240 m_free(randbuf); 240 m_free(randbuf);
241 } 241 }