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