3 * $Id: noise.c,v 1.1 1999/09/03 08:41:12 mdw Exp $
5 * Acquisition of environmental noise (Unix specific)
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/09/03 08:41:12 mdw
38 /*----- Header files ------------------------------------------------------*/
48 #include <sys/types.h>
59 #include <mLib/bits.h>
66 /*----- Magical numbers ---------------------------------------------------*/
68 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
69 #define MILLION 1000000 /* One million */
71 /*----- Noise source definition -------------------------------------------*/
73 rand_source noise_source
= { noise_acquire
, noise_timer
};
75 /*----- Static variables --------------------------------------------------*/
77 /* --- Timer differences --- */
79 static unsigned long noise_last
; /* Last time recorded */
80 static unsigned long noise_diff
; /* Last first order difference */
82 /* --- Setuid program handling --- */
84 static uid_t noise_uid
; /* Uid to set to spawn processes */
85 static gid_t noise_gid
; /* Gid to set to spawn processes */
87 /*----- Main code ---------------------------------------------------------*/
89 /* --- @bitcount@ --- *
91 * Arguments: @unsigned long x@ = a word containing bits
93 * Returns: The number of bits set in the word.
96 static int bitcount(unsigned long x
)
98 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
99 1, 2, 2, 3, 2, 3, 3, 4 };
102 count
+= ctab
[x
& 0xfu
];
110 * Arguments: @rand_pool *r@ = pointer to randomness pool
111 * @struct timeval *tv@ = pointer to time block
113 * Returns: Nonzer if some randomness was contributed.
115 * Use: Low-level timer contributor.
118 static int timer(rand_pool
*r
, struct timeval
*tv
)
120 unsigned long x
, d
, dd
;
124 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
130 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
132 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
136 /* --- @noise_timer@ --- *
138 * Arguments: @rand_pool *r@ = pointer to a randomness pool
140 * Returns: Nonzero if some randomness was contributed.
142 * Use: Contributes the current time to the randomness pool.
143 * A guess at the number of useful bits contributed is made,
144 * based on first and second order bit differences. This isn't
145 * ever-so reliable, but it's better than nothing.
148 int noise_timer(rand_pool
*r
)
151 gettimeofday(&tv
, 0);
152 return (timer(r
, &tv
));
155 /* --- @noise_devrandom@ --- *
157 * Arguments: @rand_pool *r@ = pointer to a randomness pool
159 * Returns: Nonzero if some randomness was contributed.
161 * Use: Attempts to obtain some randomness from the system entropy
162 * pool. All bits from the device are assumed to be good.
165 int noise_devrandom(rand_pool
*r
)
168 octet buf
[RAND_POOLSZ
];
172 /* --- Be nice to other clients of the random device --- *
174 * Attempt to open the device nonblockingly. If that works, take up to
175 * one bufferful and then close again. If there's no data to be read,
176 * then that's tough and we go away again, on the grounds that the device
177 * needs to get some more entropy from somewhere.
180 if ((fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
181 if ((len
= read(fd
, buf
, sizeof(buf
))) > 0) {
182 rand_add(r
, buf
, len
, len
* 8);
192 /* --- @noise_setid@ --- *
194 * Arguments: @uid_t uid@ = uid to set
195 * @gid_t gid@ = gid to set
199 * Use: Sets the user and group ids to be used by @noise_filter@
200 * when running child processes. This is useful to avoid
201 * giving shell commands (even carefully written ones) undue
205 void noise_setid(uid_t uid
, gid_t gid
)
211 /* --- @noise_filter@ --- *
213 * Arguments: @rand_pool *r@ = pointer to a randomness pool
214 * @int good@ = number of good bits per 1024 bits
215 * @const char *c@ = shell command to run
217 * Returns: Nonzero if some randomness was contributed.
219 * Use: Attempts to execute a shell command, and dump it into the
220 * randomness pool. A very rough estimate of the number of
221 * good bits is made, based on the size of the command's output.
222 * This function calls @waitpid@, so be careful. Before execing
223 * the command, the process uid and gid are set to the values
224 * given to @noise_setid@, and an attempt is made to reset the
225 * list of supplementary groups. The environment passed to
226 * the command has been severly lobotimized. If the command
227 * fails to complete within a short time period, it is killed.
228 * Paranoid use of close-on-exec flags for file descriptors is
232 int noise_filter(rand_pool
*r
, int good
, const char *c
)
239 const char *env
[] = {
240 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
244 /* --- Remember when this business started --- */
246 gettimeofday(&dead
, 0);
249 /* --- Create a pipe --- */
254 /* --- Fork a child off --- */
263 /* --- Handle the child end of the deal --- */
269 /* --- Set the pipe as standard output, close standard input --- */
271 close(0); close(1); close(2);
274 if (dup2(fd
[1], 1) < 0) _exit(127);
278 if ((f
= open("/dev/null", O_RDONLY
)) != 0 ||
279 (f
= open("/dev/null", O_WRONLY
)) != 2)
282 /* --- Play games with uids --- */
284 if (noise_gid
!= NOISE_NOSETGID
) {
287 #ifdef HAVE_SETGROUPS
288 setgroups(1, &noise_gid
);
292 if (noise_uid
!= NOISE_NOSETUID
) {
297 /* --- Start the process up --- */
299 execle("/bin/sh", "-c", c
, (char *)0, env
);
303 /* --- Sort out my end of the deal --- */
307 /* --- Decide on the deadline --- */
309 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
311 /* --- Now read, and think --- */
314 struct timeval now
, diff
;
317 gettimeofday(&now
, 0);
319 if (TV_CMP(&now
, >, &dead
))
321 TV_SUB(&diff
, &dead
, &now
);
326 if (select(fd
[0] + 1, &rd
, 0, 0, &diff
) < 0)
328 if (FD_ISSET(fd
[0], &rd
)) {
332 if ((sz
= read(fd
[0], buf
, sizeof(buf
))) <= 0)
334 goodbits
= (sz
* good
) / 128;
335 rand_add(r
, buf
, sz
, goodbits
);
340 /* --- We've finished with it: kill the child process --- *
342 * This is morally questionable. On the other hand, I don't want to be
343 * held up in the @waitpid@ call if I can possibly help it. Maybe a
344 * double-fork is worth doing here.
355 /* --- @noise_acquire@ --- *
357 * Arguments: @rand_pool *r@ = pointer to a randomness pool
361 * Use: Acquires some randomness from somewhere.
364 void noise_acquire(rand_pool
*r
)
366 if (!noise_devrandom(r
)) {
368 /* --- Output of `ps' --- *
370 * This is a hopefully cheap way of getting a bit of noise. I'm guessing
371 * the good bit ratio based on about 90 bytes per line of output, and
372 * each line containing maybe 12 bits worth of interesting data. (Some
373 * quick experiments with gzip seem to bear this idea out.) So, 12 good
374 * bits per 90 bytes of output gives slightly more than 17 good bits per
375 * 1024 bits of output. So I'll be a pessimist and say 16 bits.
379 (noise_filter(r
, 16, "ps alxww") ||
380 noise_filter(r
, 16, "ps -elf"));
382 /* --- Output of `netstat' --- *
384 * Another possibly cheap way of getting noise. My output has about
385 * 72 bytes per line of output. My wild guesses are telling me that
386 * there are probably only about four good bits per line (gzip tells
387 * me there's six, but I want to underestimate). Four bits per 72 bytes
388 * is 7 good bits per 1024 bits of output. Pessimist that I am, I'll
392 noise_filter(r
, 6, "netstat -n");
396 /*----- That's all, folks -------------------------------------------------*/