source: branches/samba-3.5.x/source4/heimdal/lib/hcrypto/rand-timer.c

Last change on this file was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 1995, 1996, 1997, 1999, 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34#include <config.h>
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <rand.h>
39
40#include <roken.h>
41
42#include "randi.h"
43
44#ifndef WIN32 /* don't bother with this on windows */
45
46static volatile int counter;
47static volatile unsigned char *gdata; /* Global data */
48static volatile int igdata; /* Index into global data */
49static int gsize;
50
51static
52RETSIGTYPE
53sigALRM(int sig)
54{
55 if (igdata < gsize)
56 gdata[igdata++] ^= counter & 0xff;
57
58#ifndef HAVE_SIGACTION
59 signal(SIGALRM, sigALRM); /* Reinstall SysV signal handler */
60#endif
61 SIGRETURN(0);
62}
63
64#ifndef HAVE_SETITIMER
65static void
66pacemaker(struct timeval *tv)
67{
68 fd_set fds;
69 pid_t pid;
70 pid = getppid();
71 while(1){
72 FD_ZERO(&fds);
73 FD_SET(0, &fds);
74 select(1, &fds, NULL, NULL, tv);
75 kill(pid, SIGALRM);
76 }
77}
78#endif
79
80#ifdef HAVE_SIGACTION
81/* XXX ugly hack, should perhaps use function from roken */
82static RETSIGTYPE
83(*fake_signal(int sig, RETSIGTYPE (*f)(int)))(int)
84{
85 struct sigaction sa, osa;
86 sa.sa_handler = f;
87 sa.sa_flags = 0;
88 sigemptyset(&sa.sa_mask);
89 sigaction(sig, &sa, &osa);
90 return osa.sa_handler;
91}
92#define signal(S, F) fake_signal((S), (F))
93#endif
94
95#endif /* WIN32*/
96
97/*
98 *
99 */
100
101static void
102timer_seed(const void *indata, int size)
103{
104}
105
106static int
107timer_bytes(unsigned char *outdata, int size)
108{
109#ifdef WIN32
110 return 0;
111#else /* WIN32 */
112 struct itimerval tv, otv;
113 RETSIGTYPE (*osa)(int);
114 int i, j;
115#ifndef HAVE_SETITIMER
116 RETSIGTYPE (*ochld)(int);
117 pid_t pid;
118#endif
119
120 gdata = outdata;
121 gsize = size;
122 igdata = 0;
123
124 osa = signal(SIGALRM, sigALRM);
125
126 /* Start timer */
127 tv.it_value.tv_sec = 0;
128 tv.it_value.tv_usec = 10 * 1000; /* 10 ms */
129 tv.it_interval = tv.it_value;
130#ifdef HAVE_SETITIMER
131 setitimer(ITIMER_REAL, &tv, &otv);
132#else
133 ochld = signal(SIGCHLD, SIG_IGN);
134 pid = fork();
135 if(pid == -1){
136 signal(SIGCHLD, ochld != SIG_ERR ? ochld : SIG_DFL);
137 des_not_rand_data(data, size);
138 return;
139 }
140 if(pid == 0)
141 pacemaker(&tv.it_interval);
142#endif
143
144 for(i = 0; i < 4; i++) {
145 for (igdata = 0; igdata < size;) /* igdata++ in sigALRM */
146 counter++;
147 for (j = 0; j < size; j++) /* Only use 2 bits each lap */
148 gdata[j] = (gdata[j]>>2) | (gdata[j]<<6);
149 }
150#ifdef HAVE_SETITIMER
151 setitimer(ITIMER_REAL, &otv, 0);
152#else
153 kill(pid, SIGKILL);
154 while(waitpid(pid, NULL, 0) != pid);
155 signal(SIGCHLD, ochld != SIG_ERR ? ochld : SIG_DFL);
156#endif
157 signal(SIGALRM, osa != SIG_ERR ? osa : SIG_DFL);
158
159 return 1;
160#endif
161}
162
163static void
164timer_cleanup(void)
165{
166}
167
168static void
169timer_add(const void *indata, int size, double entropi)
170{
171}
172
173static int
174timer_pseudorand(unsigned char *outdata, int size)
175{
176 return timer_bytes(outdata, size);
177}
178
179static int
180timer_status(void)
181{
182#ifdef WIN32
183 return 0;
184#else
185 return 1;
186#endif
187}
188
189const RAND_METHOD hc_rand_timer_method = {
190 timer_seed,
191 timer_bytes,
192 timer_cleanup,
193 timer_add,
194 timer_pseudorand,
195 timer_status
196};
197
198const RAND_METHOD *
199RAND_timer_method(void)
200{
201 return &hc_rand_timer_method;
202}
Note: See TracBrowser for help on using the repository browser.