LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/hcrypto - rand-timer.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 0 43 0.0 %
Date: 2024-04-21 15:09:00 Functions: 0 9 0.0 %

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

Generated by: LCOV version 1.14