1*d3273b5bSchristos /* $NetBSD: rand-timer.c,v 1.2 2017/01/28 21:31:47 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 1995, 1996, 1997, 1999, 2007 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric * may be used to endorse or promote products derived from this software
21ca1c9b0cSelric * without specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric * SUCH DAMAGE.
34ca1c9b0cSelric */
35ca1c9b0cSelric
36ca1c9b0cSelric #include <config.h>
37ca1c9b0cSelric #include <krb5/roken.h>
38ca1c9b0cSelric
39b9d004c6Schristos #include <rand.h>
40b9d004c6Schristos
41ca1c9b0cSelric #include "randi.h"
42ca1c9b0cSelric
43ca1c9b0cSelric #ifndef WIN32 /* don't bother with this on windows */
44ca1c9b0cSelric
45ca1c9b0cSelric static volatile int counter;
46ca1c9b0cSelric static volatile unsigned char *gdata; /* Global data */
47ca1c9b0cSelric static volatile int igdata; /* Index into global data */
48ca1c9b0cSelric static int gsize;
49ca1c9b0cSelric
50ca1c9b0cSelric static
51ca1c9b0cSelric RETSIGTYPE
sigALRM(int sig)52ca1c9b0cSelric sigALRM(int sig)
53ca1c9b0cSelric {
54ca1c9b0cSelric if (igdata < gsize)
55ca1c9b0cSelric gdata[igdata++] ^= counter & 0xff;
56ca1c9b0cSelric
57ca1c9b0cSelric #ifndef HAVE_SIGACTION
58ca1c9b0cSelric signal(SIGALRM, sigALRM); /* Reinstall SysV signal handler */
59ca1c9b0cSelric #endif
60ca1c9b0cSelric SIGRETURN(0);
61ca1c9b0cSelric }
62ca1c9b0cSelric
63ca1c9b0cSelric #ifndef HAVE_SETITIMER
64ca1c9b0cSelric static void
pacemaker(struct timeval * tv)65ca1c9b0cSelric pacemaker(struct timeval *tv)
66ca1c9b0cSelric {
67ca1c9b0cSelric fd_set fds;
68ca1c9b0cSelric pid_t pid;
69ca1c9b0cSelric pid = getppid();
70ca1c9b0cSelric while(1){
71ca1c9b0cSelric FD_ZERO(&fds);
72ca1c9b0cSelric FD_SET(0, &fds);
73ca1c9b0cSelric select(1, &fds, NULL, NULL, tv);
74ca1c9b0cSelric kill(pid, SIGALRM);
75ca1c9b0cSelric }
76ca1c9b0cSelric }
77ca1c9b0cSelric #endif
78ca1c9b0cSelric
79ca1c9b0cSelric #ifdef HAVE_SIGACTION
80ca1c9b0cSelric /* XXX ugly hack, should perhaps use function from roken */
81ca1c9b0cSelric static RETSIGTYPE
fake_signal(int sig,RETSIGTYPE (* f)(int))82ca1c9b0cSelric (*fake_signal(int sig, RETSIGTYPE (*f)(int)))(int)
83ca1c9b0cSelric {
84ca1c9b0cSelric struct sigaction sa, osa;
85ca1c9b0cSelric sa.sa_handler = f;
86ca1c9b0cSelric sa.sa_flags = 0;
87ca1c9b0cSelric sigemptyset(&sa.sa_mask);
88ca1c9b0cSelric sigaction(sig, &sa, &osa);
89ca1c9b0cSelric return osa.sa_handler;
90ca1c9b0cSelric }
91ca1c9b0cSelric #define signal(S, F) fake_signal((S), (F))
92ca1c9b0cSelric #endif
93ca1c9b0cSelric
94ca1c9b0cSelric #endif /* WIN32*/
95ca1c9b0cSelric
96ca1c9b0cSelric /*
97ca1c9b0cSelric *
98ca1c9b0cSelric */
99ca1c9b0cSelric
100ca1c9b0cSelric static void
timer_seed(const void * indata,int size)101ca1c9b0cSelric timer_seed(const void *indata, int size)
102ca1c9b0cSelric {
103ca1c9b0cSelric }
104ca1c9b0cSelric
105ca1c9b0cSelric static int
timer_bytes(unsigned char * outdata,int size)106ca1c9b0cSelric timer_bytes(unsigned char *outdata, int size)
107ca1c9b0cSelric {
108ca1c9b0cSelric #ifdef WIN32
109ca1c9b0cSelric return 0;
110ca1c9b0cSelric #else /* WIN32 */
111ca1c9b0cSelric struct itimerval tv, otv;
112ca1c9b0cSelric RETSIGTYPE (*osa)(int);
113ca1c9b0cSelric int i, j;
114ca1c9b0cSelric #ifndef HAVE_SETITIMER
115ca1c9b0cSelric RETSIGTYPE (*ochld)(int);
116ca1c9b0cSelric pid_t pid;
117ca1c9b0cSelric #endif
118ca1c9b0cSelric
119ca1c9b0cSelric gdata = outdata;
120ca1c9b0cSelric gsize = size;
121ca1c9b0cSelric igdata = 0;
122ca1c9b0cSelric
123ca1c9b0cSelric osa = signal(SIGALRM, sigALRM);
124ca1c9b0cSelric
125ca1c9b0cSelric /* Start timer */
126ca1c9b0cSelric tv.it_value.tv_sec = 0;
127ca1c9b0cSelric tv.it_value.tv_usec = 10 * 1000; /* 10 ms */
128ca1c9b0cSelric tv.it_interval = tv.it_value;
129ca1c9b0cSelric #ifdef HAVE_SETITIMER
130ca1c9b0cSelric setitimer(ITIMER_REAL, &tv, &otv);
131ca1c9b0cSelric #else
132ca1c9b0cSelric ochld = signal(SIGCHLD, SIG_IGN);
133ca1c9b0cSelric pid = fork();
134ca1c9b0cSelric if(pid == -1){
135ca1c9b0cSelric signal(SIGCHLD, ochld != SIG_ERR ? ochld : SIG_DFL);
136ca1c9b0cSelric des_not_rand_data(data, size);
137ca1c9b0cSelric return;
138ca1c9b0cSelric }
139ca1c9b0cSelric if(pid == 0)
140ca1c9b0cSelric pacemaker(&tv.it_interval);
141ca1c9b0cSelric #endif
142ca1c9b0cSelric
143ca1c9b0cSelric for(i = 0; i < 4; i++) {
144ca1c9b0cSelric for (igdata = 0; igdata < size;) /* igdata++ in sigALRM */
145ca1c9b0cSelric counter++;
146ca1c9b0cSelric for (j = 0; j < size; j++) /* Only use 2 bits each lap */
147ca1c9b0cSelric gdata[j] = (gdata[j]>>2) | (gdata[j]<<6);
148ca1c9b0cSelric }
149ca1c9b0cSelric #ifdef HAVE_SETITIMER
150ca1c9b0cSelric setitimer(ITIMER_REAL, &otv, 0);
151ca1c9b0cSelric #else
152ca1c9b0cSelric kill(pid, SIGKILL);
153ca1c9b0cSelric while(waitpid(pid, NULL, 0) != pid);
154ca1c9b0cSelric signal(SIGCHLD, ochld != SIG_ERR ? ochld : SIG_DFL);
155ca1c9b0cSelric #endif
156ca1c9b0cSelric signal(SIGALRM, osa != SIG_ERR ? osa : SIG_DFL);
157ca1c9b0cSelric
158ca1c9b0cSelric return 1;
159ca1c9b0cSelric #endif
160ca1c9b0cSelric }
161ca1c9b0cSelric
162ca1c9b0cSelric static void
timer_cleanup(void)163ca1c9b0cSelric timer_cleanup(void)
164ca1c9b0cSelric {
165ca1c9b0cSelric }
166ca1c9b0cSelric
167ca1c9b0cSelric static void
timer_add(const void * indata,int size,double entropi)168ca1c9b0cSelric timer_add(const void *indata, int size, double entropi)
169ca1c9b0cSelric {
170ca1c9b0cSelric }
171ca1c9b0cSelric
172ca1c9b0cSelric static int
timer_pseudorand(unsigned char * outdata,int size)173ca1c9b0cSelric timer_pseudorand(unsigned char *outdata, int size)
174ca1c9b0cSelric {
175ca1c9b0cSelric return timer_bytes(outdata, size);
176ca1c9b0cSelric }
177ca1c9b0cSelric
178ca1c9b0cSelric static int
timer_status(void)179ca1c9b0cSelric timer_status(void)
180ca1c9b0cSelric {
181ca1c9b0cSelric #ifdef WIN32
182ca1c9b0cSelric return 0;
183ca1c9b0cSelric #else
184ca1c9b0cSelric return 1;
185ca1c9b0cSelric #endif
186ca1c9b0cSelric }
187ca1c9b0cSelric
188b9d004c6Schristos #if defined(__GNUC__) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901)
189b9d004c6Schristos const RAND_METHOD hc_rand_timer_method = {
190b9d004c6Schristos .seed = timer_seed,
191b9d004c6Schristos .bytes = timer_bytes,
192b9d004c6Schristos .cleanup = timer_cleanup,
193b9d004c6Schristos .add = timer_add,
194b9d004c6Schristos .pseudorand = timer_pseudorand,
195b9d004c6Schristos .status = timer_status
196b9d004c6Schristos };
197b9d004c6Schristos #else
198ca1c9b0cSelric const RAND_METHOD hc_rand_timer_method = {
199ca1c9b0cSelric timer_seed,
200ca1c9b0cSelric timer_bytes,
201ca1c9b0cSelric timer_cleanup,
202ca1c9b0cSelric timer_add,
203ca1c9b0cSelric timer_pseudorand,
204ca1c9b0cSelric timer_status
205ca1c9b0cSelric };
206b9d004c6Schristos #endif
207ca1c9b0cSelric
208ca1c9b0cSelric const RAND_METHOD *
RAND_timer_method(void)209ca1c9b0cSelric RAND_timer_method(void)
210ca1c9b0cSelric {
211ca1c9b0cSelric return &hc_rand_timer_method;
212ca1c9b0cSelric }
213