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