comparison random.c @ 297:79bf1023cf11 agent-client

propagate from branch 'au.asn.ucc.matt.dropbear' (head 0501e6f661b5415eb76f3b312d183c3adfbfb712) to branch 'au.asn.ucc.matt.dropbear.cli-agent' (head 01038174ec27245b51bd43a66c01ad930880f67b)
author Matt Johnston <matt@ucc.asn.au>
date Tue, 21 Mar 2006 16:20:59 +0000
parents ca7e76d981d9 3be7ae2e8dfa
children 08b69964e408
comparison
equal deleted inserted replaced
225:ca7e76d981d9 297:79bf1023cf11
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
126 126
127 unsigned char readbuf[INIT_SEED_SIZE]; 127 unsigned char readbuf[INIT_SEED_SIZE];
128 128
129 hash_state hs; 129 hash_state hs;
130 130
131 /* initialise so compilers will be happy about hashing it */ 131 /* initialise so that things won't warn about
132 * hashing an undefined buffer */
132 if (!donerandinit) { 133 if (!donerandinit) {
133 m_burn(hashpool, sizeof(hashpool)); 134 m_burn(hashpool, sizeof(hashpool));
134 } 135 }
135 136
136 /* get the seed data */ 137 /* get the seed data */
142 sha1_process(&hs, (void*)readbuf, sizeof(readbuf)); 143 sha1_process(&hs, (void*)readbuf, sizeof(readbuf));
143 sha1_done(&hs, hashpool); 144 sha1_done(&hs, hashpool);
144 145
145 counter = 0; 146 counter = 0;
146 donerandinit = 1; 147 donerandinit = 1;
148 }
149
150 /* hash the current random pool with some unique identifiers
151 * for this process and point-in-time. this is used to separate
152 * the random pools for fork()ed processes. */
153 void reseedrandom() {
154
155 pid_t pid;
156 struct timeval tv;
157
158 if (!donerandinit) {
159 dropbear_exit("seedrandom not done");
160 }
161
162 pid = getpid();
163 gettimeofday(&tv, NULL);
164
165 hash_state hs;
166 unsigned char hash[SHA1_HASH_SIZE];
167 sha1_init(&hs);
168 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
169 sha1_process(&hs, (void*)&pid, sizeof(pid));
170 sha1_process(&hs, (void*)&tv, sizeof(tv));
171 sha1_done(&hs, hashpool);
147 } 172 }
148 173
149 /* return len bytes of pseudo-random data */ 174 /* return len bytes of pseudo-random data */
150 void genrandom(unsigned char* buf, unsigned int len) { 175 void genrandom(unsigned char* buf, unsigned int len) {
151 176