comparison random.c @ 302:973fccb59ea4 ucc-axis-hack

propagate from branch 'au.asn.ucc.matt.dropbear' (head 11034278bd1917bebcbdc69cf53b1891ce9db121) to branch 'au.asn.ucc.matt.dropbear.ucc-axis-hack' (head 10a1f614fec73d0820c3f61160d9db409b9beb46)
author Matt Johnston <matt@ucc.asn.au>
date Sat, 25 Mar 2006 12:59:58 +0000
parents 7dad470ad4aa
children 1876c6bb084b 36d21680a9d3
comparison
equal deleted inserted replaced
299:740e782679be 302:973fccb59ea4
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 /* the max value for the counter, so it won't integer overflow */
35 #define MAX_COUNTER 1<<30
35 36
36 static unsigned char hashpool[SHA1_HASH_SIZE]; 37 static unsigned char hashpool[SHA1_HASH_SIZE];
37 38
38 #define INIT_SEED_SIZE 32 /* 256 bits */ 39 #define INIT_SEED_SIZE 32 /* 256 bits */
39 40
130 131
131 unsigned char readbuf[INIT_SEED_SIZE]; 132 unsigned char readbuf[INIT_SEED_SIZE];
132 133
133 hash_state hs; 134 hash_state hs;
134 135
135 /* initialise so compilers will be happy about hashing it */ 136 /* initialise so that things won't warn about
137 * hashing an undefined buffer */
136 if (!donerandinit) { 138 if (!donerandinit) {
137 m_burn(hashpool, sizeof(hashpool)); 139 m_burn(hashpool, sizeof(hashpool));
138 } 140 }
139 141
140 /* get the seed data */ 142 /* get the seed data */
146 sha1_process(&hs, (void*)readbuf, sizeof(readbuf)); 148 sha1_process(&hs, (void*)readbuf, sizeof(readbuf));
147 sha1_done(&hs, hashpool); 149 sha1_done(&hs, hashpool);
148 150
149 counter = 0; 151 counter = 0;
150 donerandinit = 1; 152 donerandinit = 1;
153 }
154
155 /* hash the current random pool with some unique identifiers
156 * for this process and point-in-time. this is used to separate
157 * the random pools for fork()ed processes. */
158 void reseedrandom() {
159
160 pid_t pid;
161 struct timeval tv;
162
163 if (!donerandinit) {
164 dropbear_exit("seedrandom not done");
165 }
166
167 pid = getpid();
168 gettimeofday(&tv, NULL);
169
170 hash_state hs;
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