chiark / gitweb /
48fe6bd0bdf33b25619366c44ff84867c0c848e4
[catacomb] / noise.h
1 /* -*-c-*-
2  *
3  * $Id: noise.h,v 1.6 2004/04/08 01:36:15 mdw Exp $
4  *
5  * Acquisition of environmental noise (Unix-specific)
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------*
11  *
12  * This file is part of Catacomb.
13  *
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.
18  *
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.
23  *
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,
27  * MA 02111-1307, USA.
28  */
29
30 #ifndef CATACOMB_NOISE_H
31 #define CATACOMB_NOISE_H
32
33 #ifdef __cplusplus
34   extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <sys/types.h>
40
41 #ifndef CATACOMB_RAND_H
42 #  include "rand.h"
43 #endif
44
45 /*----- Noise source definition -------------------------------------------*/
46
47 extern const rand_source noise_source;
48
49 /*----- Magic numbers -----------------------------------------------------*/
50
51 #define NOISE_NOSETUID ((uid_t)-1)
52 #define NOISE_NOSETGID ((gid_t)-1)
53
54 /*----- Functions provided ------------------------------------------------*/
55
56 /* --- @noise_timer@ --- *
57  *
58  * Arguments:   @rand_pool *r@ = pointer to a randomness pool
59  *
60  * Returns:     Nonzero if some randomness was contributed.
61  *
62  * Use:         Contributes the current time to the randomness pool.
63  *              A guess at the number of useful bits contributed is made,
64  *              based on first and second order bit differences.  This isn't
65  *              ever-so reliable, but it's better than nothing.
66  */
67
68 extern int noise_timer(rand_pool */*r*/);
69
70 /* --- @noise_devrandom@ --- *
71  *
72  * Arguments:   @rand_pool *r@ = pointer to a randomness pool
73  *
74  * Returns:     Nonzero if some randomness was contributed.
75  *
76  * Use:         Attempts to obtain some randomness from the system entropy
77  *              pool.  All bits from the device are assumed to be good.
78  */
79
80 extern int noise_devrandom(rand_pool */*r*/);
81
82 /* --- @noise_setid@ --- *
83  *
84  * Arguments:   @uid_t uid@ = uid to set
85  *              @gid_t gid@ = gid to set
86  *
87  * Returns:     ---
88  *
89  * Use:         Sets the user and group ids to be used by @noise_filter@
90  *              when running child processes.  This is useful to avoid
91  *              giving shell commands (even carefully written ones) undue
92  *              privileges.  This interface is Unix-specific.
93  */
94
95 extern void noise_setid(uid_t /*uid*/, gid_t /*gid*/);
96
97 /* --- @noise_filter@ --- *
98  *
99  * Arguments:   @rand_pool *r@ = pointer to a randomness pool
100  *              @int good@ = number of good bits per 1024 bits
101  *              @const char *c@ = shell command to run
102  *
103  * Returns:     Nonzero if some randomness was contributed.
104  *
105  * Use:         Attempts to execute a shell command, and dump it into the
106  *              randomness pool.  A very rough estimate of the number of
107  *              good bits is made, based on the size of the command's output.
108  *              This function calls @waitpid@, so be careful.  Before execing
109  *              the command, the process uid and gid are set to the values
110  *              given to @noise_setid@, and an attempt is made to reset the
111  *              list of supplementary groups.  The environment passed to
112  *              the command has been severly lobotimized.  If the command
113  *              fails to complete within a short time period, it is killed.
114  *              Paranoid use of close-on-exec flags for file descriptors is
115  *              recommended.
116  *
117  *              This interface is Unix-specific.
118  */
119
120 extern int noise_filter(rand_pool */*r*/, int /*good*/, const char */*c*/);
121
122 /* --- @noise_freewheel@ --- *
123  *
124  * Arguments:   @rand_pool *r@ = pointer to a randomness pool
125  *
126  * Returns:     Nonzero if some randomness was contributed.
127  *
128  * Use:         Runs a free counter for a short while as a desparate attempt
129  *              to get randomness from somewhere.  This is actually quite
130  *              effective.
131  */
132
133 int noise_freewheel(rand_pool */*r*/);
134
135 /* --- @noise_enquire@ --- *
136  *
137  * Arguments:   @rand_pool *r@ = pointer to a randomness pool
138  *
139  * Returns:     Nonzero if some randomness was contributed.
140  *
141  * Use:         Runs some shell commands to enquire about the prevailing
142  *              environment.  This can gather quite a lot of low-quality
143  *              entropy.
144  */
145
146 extern int noise_enquire(rand_pool */*r*/);
147
148 /* --- @noise_acquire@ --- *
149  *
150  * Arguments:   @rand_pool *r@ = pointer to a randomness pool
151  *
152  * Returns:     ---
153  *
154  * Use:         Acquires some randomness from somewhere.
155  */
156
157 extern void noise_acquire(rand_pool */*r*/);
158
159 /*----- That's all, folks -------------------------------------------------*/
160
161 #ifdef __cplusplus
162   }
163 #endif
164
165 #endif