root/source4/heimdal/lib/hcrypto/rand-timer.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sigALRM
  2. pacemaker
  3. timer_seed
  4. timer_bytes
  5. timer_cleanup
  6. timer_add
  7. timer_pseudorand
  8. timer_status
  9. RAND_timer_method

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

/* [<][>][^][v][top][bottom][index][help] */