3 * $Id: noise.c,v 1.7 2004/04/02 01:03:49 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.7 2004/04/02 01:03:49 mdw
34 * Miscellaneous constification.
36 * Revision 1.6 2000/06/17 12:57:47 mdw
37 * New free counter noise generator, for use if /dev/random is
40 * Revision 1.5 1999/12/22 15:57:55 mdw
41 * Label system-specific parts more clearly.
43 * Revision 1.4 1999/12/10 23:25:15 mdw
44 * Bug fix: remove old spurious fflush.
46 * Revision 1.3 1999/12/10 23:24:11 mdw
47 * Bug fix: flush buffers before forking.
49 * Revision 1.2 1999/11/11 00:59:08 mdw
50 * A bit of reformatting. Initialize the uid and gid correctly.
52 * Revision 1.1 1999/09/03 08:41:12 mdw
57 /*----- Header files ------------------------------------------------------*/
66 #include <sys/types.h>
77 #include <mLib/bits.h>
84 /*----- Magical numbers ---------------------------------------------------*/
86 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
87 #define MILLION 1000000 /* One million */
89 /*----- Noise source definition -------------------------------------------*/
91 const rand_source noise_source
= { noise_acquire
, noise_timer
};
93 /*----- Static variables --------------------------------------------------*/
95 /* --- Timer differences --- */
97 static unsigned long noise_last
; /* Last time recorded */
98 static unsigned long noise_diff
; /* Last first order difference */
100 /* --- Setuid program handling --- */
102 static uid_t noise_uid
= NOISE_NOSETUID
; /* Uid to set to spawn processes */
103 static gid_t noise_gid
= NOISE_NOSETGID
; /* Gid to set to spawn processes */
105 /*----- Main code ---------------------------------------------------------*/
107 /* --- @bitcount@ --- *
109 * Arguments: @unsigned long x@ = a word containing bits
111 * Returns: The number of bits set in the word.
114 static int bitcount(unsigned long x
)
116 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
117 1, 2, 2, 3, 2, 3, 3, 4 };
120 count
+= ctab
[x
& 0xfu
];
128 * Arguments: @rand_pool *r@ = pointer to randomness pool
129 * @struct timeval *tv@ = pointer to time block
131 * Returns: Nonzero if some randomness was contributed.
133 * Use: Low-level timer contributor.
136 static int timer(rand_pool
*r
, struct timeval
*tv
)
138 unsigned long x
, d
, dd
;
142 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
148 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
150 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
154 /* --- @noise_timer@ --- *
156 * Arguments: @rand_pool *r@ = pointer to a randomness pool
158 * Returns: Nonzero if some randomness was contributed.
160 * Use: Contributes the current time to the randomness pool.
161 * A guess at the number of useful bits contributed is made,
162 * based on first and second order bit differences. This isn't
163 * ever-so reliable, but it's better than nothing.
166 int noise_timer(rand_pool
*r
)
169 gettimeofday(&tv
, 0);
170 return (timer(r
, &tv
));
173 /* --- @noise_devrandom@ --- *
175 * Arguments: @rand_pool *r@ = pointer to a randomness pool
177 * Returns: Nonzero if some randomness was contributed.
179 * Use: Attempts to obtain some randomness from the system entropy
180 * pool. All bits from the device are assumed to be good.
183 int noise_devrandom(rand_pool
*r
)
186 octet buf
[RAND_POOLSZ
];
190 /* --- Be nice to other clients of the random device --- *
192 * Attempt to open the device nonblockingly. If that works, take up to
193 * one bufferful and then close again. If there's no data to be read,
194 * then that's tough and we go away again, on the grounds that the device
195 * needs to get some more entropy from somewhere.
198 if ((fd
= open("/dev/urandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
199 (fd
= open("/dev/arandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
200 (fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
201 if ((len
= read(fd
, buf
, sizeof(buf
))) > 0) {
202 rand_add(r
, buf
, len
, len
* 8);
212 /* --- @noise_setid@ --- *
214 * Arguments: @uid_t uid@ = uid to set
215 * @gid_t gid@ = gid to set
219 * Use: Sets the user and group ids to be used by @noise_filter@
220 * when running child processes. This is useful to avoid
221 * giving shell commands (even carefully written ones) undue
222 * privileges. This interface is Unix-specific
225 void noise_setid(uid_t uid
, gid_t gid
)
231 /* --- @noise_filter@ --- *
233 * Arguments: @rand_pool *r@ = pointer to a randomness pool
234 * @int good@ = number of good bits per 1024 bits
235 * @const char *c@ = shell command to run
237 * Returns: Nonzero if some randomness was contributed.
239 * Use: Attempts to execute a shell command, and dump it into the
240 * randomness pool. A very rough estimate of the number of
241 * good bits is made, based on the size of the command's output.
242 * This function calls @waitpid@, so be careful. Before execing
243 * the command, the process uid and gid are set to the values
244 * given to @noise_setid@, and an attempt is made to reset the
245 * list of supplementary groups. The environment passed to
246 * the command has been severly lobotimized. If the command
247 * fails to complete within a short time period, it is killed.
248 * Paranoid use of close-on-exec flags for file descriptors is
251 * This interface is Unix-specific.
254 int noise_filter(rand_pool
*r
, int good
, const char *c
)
261 const char *env
[] = {
262 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
266 /* --- Remember when this business started --- */
268 gettimeofday(&dead
, 0);
271 /* --- Create a pipe --- */
276 /* --- Fork a child off --- */
286 /* --- Handle the child end of the deal --- */
291 /* --- Set the pipe as standard output, close standard input --- */
293 close(0); close(1); close(2);
296 if (dup2(fd
[1], 1) < 0) _exit(127);
300 if ((f
= open("/dev/null", O_RDONLY
)) != 0 ||
301 (f
= open("/dev/null", O_WRONLY
)) != 2)
304 /* --- Play games with uids --- */
306 if (noise_gid
!= NOISE_NOSETGID
) {
309 #ifdef HAVE_SETGROUPS
310 setgroups(1, &noise_gid
);
314 if (noise_uid
!= NOISE_NOSETUID
) {
319 /* --- Start the process up --- */
321 execle("/bin/sh", "-c", c
, (char *)0, env
);
325 /* --- Sort out my end of the deal --- */
329 /* --- Decide on the deadline --- */
331 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
333 /* --- Now read, and think --- */
336 struct timeval now
, diff
;
339 gettimeofday(&now
, 0);
341 if (TV_CMP(&now
, >, &dead
))
343 TV_SUB(&diff
, &dead
, &now
);
348 if (select(fd
[0] + 1, &rd
, 0, 0, &diff
) < 0)
350 if (FD_ISSET(fd
[0], &rd
)) {
354 if ((sz
= read(fd
[0], buf
, sizeof(buf
))) <= 0)
356 goodbits
= (sz
* good
) / 128;
357 rand_add(r
, buf
, sz
, goodbits
);
362 /* --- We've finished with it: kill the child process --- *
364 * This is morally questionable. On the other hand, I don't want to be
365 * held up in the @waitpid@ call if I can possibly help it. Maybe a
366 * double-fork is worth doing here.
377 /* --- @noise_freewheel@ --- *
379 * Arguments: @rand_pool *r@ = pointer to a randomness pool
381 * Returns: Nonzero if some randomness was contributed.
383 * Use: Runs a free counter for a short while as a desparate attempt
384 * to get randomness from somewhere. This is actually quite
390 static jmp_buf fwjmp
;
392 static void fwalarm(int sig
)
394 siglongjmp(fwjmp
, 1);
397 int noise_freewheel(rand_pool
*r
)
399 void (*sigal
)(int) = 0;
400 struct itimerval oitv
, itv
= { { 0, 0 }, { 0, 5000 } };
402 volatile uint32 fwcount
= 0;
404 if (!sigsetjmp(fwjmp
, 1)) {
405 if ((sigal
= signal(SIGALRM
, fwalarm
)) == SIG_ERR
)
407 if (setitimer(ITIMER_REAL
, &itv
, &oitv
))
413 STORE32(buf
, fwcount
);
414 rand_add(r
, buf
, sizeof(buf
), 16);
419 signal(SIGALRM
, sigal
);
420 TV_SUB(&oitv
.it_value
, &oitv
.it_value
, &itv
.it_value
);
421 setitimer(ITIMER_REAL
, &oitv
, 0);
427 int noise_freewheel(rand_pool
*r
)
434 /* --- @noise_enquire@ --- *
436 * Arguments: @rand_pool *r@ = pointer to a randomness pool
438 * Returns: Nonzero if some randomness was contributed.
440 * Use: Runs some shell commands to enquire about the prevailing
441 * environment. This can gather quite a lot of low-quality
445 int noise_enquire(rand_pool
*r
)
451 { "ps alxww || ps -elf", 16 },
455 { "ls -align /tmp/.", 10 },
460 for (i
= 0; tab
[i
].cmd
; i
++)
461 noise_filter(r
, tab
[i
].rate
, tab
[i
].cmd
);
465 /* --- @noise_acquire@ --- *
467 * Arguments: @rand_pool *r@ = pointer to a randomness pool
471 * Use: Acquires some randomness from somewhere.
474 void noise_acquire(rand_pool
*r
)
477 for (i
= 0; i
< 8; i
++)
479 if (!noise_devrandom(r
)) {
481 for (i
= 0; i
< 8; i
++)
486 /*----- That's all, folks -------------------------------------------------*/