comparison dbrandom.c @ 1563:1cbb7b3d6703

Merge fuzzing branch
author Matt Johnston <matt@ucc.asn.au>
date Wed, 28 Feb 2018 22:12:05 +0800
parents 02b226c2675e
children 60fceff95858
comparison
equal deleted inserted replaced
1560:f5026f7486de 1563:1cbb7b3d6703
25 #include "includes.h" 25 #include "includes.h"
26 #include "buffer.h" 26 #include "buffer.h"
27 #include "dbutil.h" 27 #include "dbutil.h"
28 #include "bignum.h" 28 #include "bignum.h"
29 #include "dbrandom.h" 29 #include "dbrandom.h"
30 30 #include "runopts.h"
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 uint32_t counter = 0; 33 static uint32_t counter = 0;
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)
143 143
144 void addrandom(const unsigned char * buf, unsigned int len) 144 void addrandom(const unsigned char * buf, unsigned int len)
145 { 145 {
146 hash_state hs; 146 hash_state hs;
147 147
148 #if DROPBEAR_FUZZ
149 if (fuzz.fuzzing) {
150 return;
151 }
152 #endif
153
148 /* hash in the new seed data */ 154 /* hash in the new seed data */
149 sha1_init(&hs); 155 sha1_init(&hs);
150 /* existing state (zeroes on startup) */ 156 /* existing state (zeroes on startup) */
151 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); 157 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
152 158
155 sha1_done(&hs, hashpool); 161 sha1_done(&hs, hashpool);
156 } 162 }
157 163
158 static void write_urandom() 164 static void write_urandom()
159 { 165 {
166 #if DROPBEAR_FUZZ
167 if (fuzz.fuzzing) {
168 return;
169 }
170 #endif
160 #if !DROPBEAR_USE_PRNGD 171 #if !DROPBEAR_USE_PRNGD
161 /* This is opportunistic, don't worry about failure */ 172 /* This is opportunistic, don't worry about failure */
162 unsigned char buf[INIT_SEED_SIZE]; 173 unsigned char buf[INIT_SEED_SIZE];
163 FILE *f = fopen(DROPBEAR_URANDOM_DEV, "w"); 174 FILE *f = fopen(DROPBEAR_URANDOM_DEV, "w");
164 if (!f) { 175 if (!f) {
168 fwrite(buf, sizeof(buf), 1, f); 179 fwrite(buf, sizeof(buf), 1, f);
169 fclose(f); 180 fclose(f);
170 #endif 181 #endif
171 } 182 }
172 183
184 #if DROPBEAR_FUZZ
185 void fuzz_seed(void) {
186 hash_state hs;
187 sha1_init(&hs);
188 sha1_process(&hs, "fuzzfuzzfuzz", strlen("fuzzfuzzfuzz"));
189 sha1_done(&hs, hashpool);
190
191 counter = 0;
192 donerandinit = 1;
193 }
194 #endif
195
173 /* Initialise the prng from /dev/urandom or prngd. This function can 196 /* Initialise the prng from /dev/urandom or prngd. This function can
174 * be called multiple times */ 197 * be called multiple times */
175 void seedrandom() { 198 void seedrandom() {
176 199
177 hash_state hs; 200 hash_state hs;
178 201
179 pid_t pid; 202 pid_t pid;
180 struct timeval tv; 203 struct timeval tv;
181 clock_t clockval; 204 clock_t clockval;
182 205
206 #if DROPBEAR_FUZZ
207 if (fuzz.fuzzing) {
208 return;
209 }
210 #endif
211
183 /* hash in the new seed data */ 212 /* hash in the new seed data */
184 sha1_init(&hs); 213 sha1_init(&hs);
214
185 /* existing state */ 215 /* existing state */
186 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); 216 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
187 217
188 #if DROPBEAR_USE_PRNGD 218 #if DROPBEAR_USE_PRNGD
189 if (process_file(&hs, DROPBEAR_PRNGD_SOCKET, INIT_SEED_SIZE, 1) 219 if (process_file(&hs, DROPBEAR_PRNGD_SOCKET, INIT_SEED_SIZE, 1)