comparison random.c @ 345:959c66ccf1b5 debug-unrandom

Remove actual randomness, to (hopefully) allow comparison between hosts
author Matt Johnston <matt@ucc.asn.au>
date Fri, 04 Aug 2006 17:15:05 +0000
parents 36d21680a9d3
children 381834084475
comparison
equal deleted inserted replaced
344:bf29e6659fb9 345:959c66ccf1b5
91 if (write(readfd, egdcmd, 2) < 0) 91 if (write(readfd, egdcmd, 2) < 0)
92 dropbear_exit("can't send command to egd"); 92 dropbear_exit("can't send command to egd");
93 #endif 93 #endif
94 94
95 /* read the actual random data */ 95 /* read the actual random data */
96 readpos = 0;
97 do {
98 if (!already_blocked)
99 {
100 int ret;
101 struct timeval timeout;
102 fd_set read_fds;
103
104 timeout.tv_sec = 2; /* two seconds should be enough */
105 timeout.tv_usec = 0;
106
107 FD_ZERO(&read_fds);
108 FD_SET(readfd, &read_fds);
109 ret = select(readfd + 1, &read_fds, NULL, NULL, &timeout);
110 if (ret == 0)
111 {
112 dropbear_log(LOG_INFO, "Warning: Reading the random source seems to have blocked.\nIf you experience problems, you probably need to find a better entropy source.");
113 already_blocked = 1;
114 }
115 }
116 readlen = read(readfd, &buf[readpos], buflen - readpos);
117 if (readlen <= 0) {
118 if (readlen < 0 && errno == EINTR) {
119 continue;
120 }
121 dropbear_exit("error reading random source");
122 }
123 readpos += readlen;
124 } while (readpos < buflen);
125 96
126 close (readfd); 97 close (readfd);
127 } 98 }
128 99
129 /* initialise the prng from /dev/(u)random or prngd */ 100 /* initialise the prng from /dev/(u)random or prngd */
155 /* hash the current random pool with some unique identifiers 126 /* hash the current random pool with some unique identifiers
156 * for this process and point-in-time. this is used to separate 127 * for this process and point-in-time. this is used to separate
157 * the random pools for fork()ed processes. */ 128 * the random pools for fork()ed processes. */
158 void reseedrandom() { 129 void reseedrandom() {
159 130
160 pid_t pid;
161 hash_state hs; 131 hash_state hs;
162 struct timeval tv;
163 132
164 if (!donerandinit) { 133 if (!donerandinit) {
165 dropbear_exit("seedrandom not done"); 134 dropbear_exit("seedrandom not done");
166 } 135 }
167 136
168 pid = getpid();
169 gettimeofday(&tv, NULL);
170
171 sha1_init(&hs); 137 sha1_init(&hs);
172 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); 138 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); 139 sha1_done(&hs, hashpool);
176 } 140 }
177 141
178 /* return len bytes of pseudo-random data */ 142 /* return len bytes of pseudo-random data */
179 void genrandom(unsigned char* buf, unsigned int len) { 143 void genrandom(unsigned char* buf, unsigned int len) {