Ignore:
Timestamp:
Feb 23, 2006, 12:03:58 AM (19 years ago)
Author:
rainer
Message:

Minor code revisions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/samhain_setpwd.c

    r1 r22  
    11#include "config_xor.h"
    2 
    3 #ifdef HAVE_BROKEN_INCLUDES
    4 #define _ANSI_C_SOURCE
    5 #define _POSIX_SOURCE
    6 #endif
    72
    83#include <stdio.h>
     
    138#include <unistd.h>
    149#include <sys/types.h>
     10#include <sys/wait.h>
    1511#include <sys/stat.h>
    1612#include <fcntl.h>
     13#include <errno.h>
     14#include <sys/time.h>
    1715#include <time.h>
    1816
     17#if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_YIELD)
     18#include <sched.h>
     19#endif
     20
     21#if defined(HAVE_INT_32)
     22typedef unsigned int UINT32;
     23#elif defined(HAVE_LONG_32)
     24typedef unsigned long UINT32;
     25#elif defined(HAVE_SHORT_32)
     26typedef unsigned short UINT32;
     27#endif
     28
     29#define TAUS_MAX 4294967295UL
     30
     31static UINT32 taus_state[3];
     32
     33static UINT32 taus_get ()
     34{
     35
     36#define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)
     37  taus_state[0] = TAUSWORTHE (taus_state[0], 13, 19, 4294967294UL, 12);
     38  taus_state[1] = TAUSWORTHE (taus_state[1],  2, 25, 4294967288UL,  4);
     39  taus_state[2] = TAUSWORTHE (taus_state[2],  3, 11, 4294967280UL, 17);
     40  return (taus_state[0] ^ taus_state[1] ^ taus_state[2]);
     41}
     42
     43static void taus_seed ()
     44{
     45  unsigned char buf[12];
     46  unsigned char buf2[12];
     47  unsigned char buf3[12];
     48  ssize_t count;
     49  size_t nbytes = sizeof(buf);
     50  size_t where  = 0;
     51
     52  struct timeval t1, t2;
     53  UINT32 delta, k[3];
     54  int i, j;
     55
     56  int fd = open ("/dev/urandom", O_RDONLY);
     57
     58  if (fd == -1)
     59    {
     60      gettimeofday(&t1, NULL);
     61      delta = t1.tv_usec;
     62      memcpy(&buf[0], &delta, 4);
     63      gettimeofday(&t1, NULL);
     64      delta = t1.tv_usec;
     65      memcpy(&buf[4], &delta, 4);
     66      gettimeofday(&t1, NULL);
     67      delta = t1.tv_usec;
     68      memcpy(&buf[8], &delta, 4);
     69      goto second;
     70    }
     71
     72  while (nbytes) {
     73    count = read(fd, &buf[where], nbytes);
     74    if (count == -1 && errno == EINTR)
     75      continue;
     76    where  += count;
     77    nbytes -= count;
     78  } while (count == -1 && errno == EINTR);
     79
     80  close(fd);
     81
     82 second:
     83  for (i = 0; i < 12; ++i)
     84    {
     85      gettimeofday(&t1, NULL);
     86      if (0 == fork())
     87        _exit(EXIT_SUCCESS);
     88      wait(NULL);
     89      gettimeofday(&t2, NULL);
     90      delta = t2.tv_usec - t1.tv_usec;
     91      buf2[i] = (unsigned char) delta;
     92    }
     93
     94  for (i = 0; i < 12; ++i)
     95    {
     96      gettimeofday(&t1, NULL);
     97      for (j = 0; j < 32768; ++j)
     98        {
     99          if (0 == kill (j,0))
     100            k[i % 3] ^= j;
     101        }
     102      gettimeofday(&t2, NULL);
     103      delta = t2.tv_usec - t1.tv_usec;
     104      buf3[i] ^= (unsigned char) delta;
     105    }
     106
     107  memcpy(&taus_state[0], &buf3[0], 4);
     108  memcpy(&taus_state[1], &buf3[4], 4);
     109  memcpy(&taus_state[2], &buf3[8], 4);
     110
     111  taus_state[0] ^= k[0];
     112  taus_state[1] ^= k[1];
     113  taus_state[2] ^= k[2];
     114 
     115  memcpy(&k[0], &buf2[0], 4);
     116  memcpy(&k[1], &buf2[4], 4);
     117  memcpy(&k[2], &buf2[8], 4);
     118
     119  taus_state[0] ^= k[0];
     120  taus_state[1] ^= k[1];
     121  taus_state[2] ^= k[2];
     122 
     123  memcpy(&k[0], &buf[0], 4);
     124  memcpy(&k[1], &buf[4], 4);
     125  memcpy(&k[2], &buf[8], 4);
     126
     127  taus_state[0] ^= k[0];
     128  taus_state[1] ^= k[1];
     129  taus_state[2] ^= k[2];
     130
     131  taus_state[0] |= (UINT32) 0x03;
     132  taus_state[1] |= (UINT32) 0x09;
     133  taus_state[2] |= (UINT32) 0x17;
     134}
    19135
    20136#ifdef SH_STEALTH
     
    112228
    113229  char * newn;
     230  size_t nlen;
    114231  int    oldf;
    115232  int    newf;
     
    196313  (void) umask (0);
    197314
    198   srand(time(NULL) ^ getpid());
     315  taus_seed();
    199316
    200317  bytecount = 0;
     
    206323  oldf = open(argv[1], O_RDONLY);
    207324
    208   newn = (char *) malloc (strlen(argv[1])+strlen(argv[2])+2);
    209   strcpy(newn, argv[1]);
    210   strcat(newn, ".");
    211   strcat(newn, argv[2]);
     325  nlen = strlen(argv[1])+strlen(argv[2])+2;
     326  newn = (char *) malloc (nlen);
     327  strncpy(newn, argv[1], nlen); newn[nlen-1] = '\0';
     328  strncat(newn, ".", nlen);     newn[nlen-1] = '\0';
     329  strncat(newn, argv[2], nlen); newn[nlen-1] = '\0';
    212330  newf = open(newn, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
    213331
     
    265383                      (unsigned char) *found_it);
    266384
    267               ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0));
     385              ccd = (unsigned char) (256.0 * (taus_get()/(TAUS_MAX+1.0)));
    268386              sprintf(&newpwd[i*2], _("%02x"),
    269387                      (unsigned char) ccd);
     
    340458                      (unsigned char) *found_it);
    341459
    342               ccd = (unsigned char) (256.0 * rand()/(RAND_MAX+1.0));
     460              ccd = (unsigned char) (256.0 * taus_get()/(TAUS_MAX+1.0));
    343461              sprintf(&newpwd[i*2], _("%02x"),
    344462                      (unsigned char) ccd);
Note: See TracChangeset for help on using the changeset viewer.