File: random.c

package info (click to toggle)
mixmaster 3.0b2-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 1,356 kB
  • ctags: 1,173
  • sloc: ansic: 18,314; sh: 1,285; yacc: 698; perl: 314; makefile: 161
file content (210 lines) | stat: -rw-r--r-- 4,009 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Mixmaster version 3.0  --  (C) 1999 - 2004 Anonymizer Inc. and others.

   Mixmaster may be redistributed and modified under certain conditions.
   This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
   ANY KIND, either express or implied. See the file COPYRIGHT for
   details.

   Randomness
   $Id: random.c 710 2004-01-21 00:47:21Z rabbi $ */


#include "mix3.h"
#include "crypto.h"
#include <fcntl.h>
#ifdef POSIX
#include <sys/time.h>
#include <unistd.h>
#else /* end of POSIX */
#include <io.h>
#include <process.h>
#endif /* else if not POSIX */
#ifdef WIN32
#include <windows.h>
#endif /* WIN32 */
#include <assert.h>
#include <string.h>

int rnd_state = RND_NOTSEEDED;

#ifdef USE_OPENSSL
int rnd_init(void)
{
  char r[PATHMAX];
  int n;
  LOCK *rndlock;

  if (rnd_state == RND_SEEDED)
    return(0);
  rndlock = lockfile(MIXRAND);
  mixfile(r, MIXRAND);
  n = RAND_load_file(r, 1024);
  if (n < 256 && rnd_seed() == -1)
    goto err;
  rnd_time();
  RAND_write_file(r);
  rnd_state = RND_SEEDED;
 err:
  unlockfile(rndlock);
  return (rnd_state == RND_SEEDED ? 0 : -1);
}

int rnd_final(void)
{
  int err = 0;
  char r[PATHMAX];
  LOCK *rndlock;

  if (rnd_state != RND_SEEDED)
    return(-1);

  rnd_update(NULL, 0);
  rndlock = lockfile(MIXRAND);
  mixfile(r, MIXRAND);
  RAND_load_file(r, 1024);	/* add seed file again in case other instances
				   of the program have used it */
  if (RAND_write_file(r) < 1)
    err = -1;
  unlockfile(rndlock);
  RAND_cleanup();
  return (err);
}

int rnd_add(byte *b, int l)
{
  RAND_seed(b, l);
  return (0);
}
#endif /* USE_OPENSSL */

void rnd_time(void)
{
  int pid;

#ifdef WIN32
  SYSTEMTIME t;
#endif /* WIN32 */

#ifdef HAVE_GETTIMEOFDAY
  struct timeval tv;

  gettimeofday(&tv, 0);
  rnd_add((byte *) &tv, sizeof(tv));
#elif defined(WIN32) /* end of HAVE_GETTIMEOFDAY */
  GetSystemTime(&t);
  rnd_add((byte *) &t, sizeof(t));
#else /* end of defined(WIN32) */
  rnd_add((byte *) time(NULL), sizeof(time_t));
#endif /* else if not defined(WIN32), HAVE_GETTIMEOFDAY */
  pid = getpid();
  rnd_add((byte *) &pid, sizeof(pid));
}

void rnd_update(byte *seed, int l)
{
  int fd = -1;
  byte b[512];

  rnd_time();
  if (seed)
    rnd_add(seed, l);
#ifdef DEV_URANDOM
  fd = open(DEV_URANDOM, O_RDONLY);
  if (fd != -1) {
    ssize_t ret;

    ret = read(fd, b, sizeof(b));
    if (ret > 0) {
      rnd_add(b, ret);
    }
    close(fd);
  }
#endif /* DEV_URANDOM */
}

int rnd_bytes(byte *b, int n)
{
  /* we frequently need to get small amounts of random data.
     speed up by pre-generating dating data */

  static byte rand[BUFSIZE];
  static int idx = BUFSIZE;

  if (rnd_state != RND_SEEDED)
    rnd_error();

  if (n + idx < BUFSIZE) {
    memcpy(b, rand + idx, n);
    idx += n;
  } else
    RAND_bytes(b, n);

  if (idx + 256 > BUFSIZE) {
    RAND_bytes(rand, BUFSIZE);
    idx = 0;
  }
  return (0);
}

int rnd_number(int n)
{
  int r;

  assert(n > 0);
  if (n > 65535)
    do
      r = rnd_byte() * 65536 +
	rnd_byte() * 256 + rnd_byte();
    while (r >= n);
  else if (n > 255)
    do
      r = rnd_byte() * 256 + rnd_byte();
    while (r >= n);
  else
    do
      r = rnd_byte();
    while (r >= n);
  return r;
}

byte rnd_byte()
{
  byte b;

  rnd_bytes(&b, 1);
  return b;
}

void rnd_initialized(void)
{
  rnd_state = RND_SEEDED;
}

#ifdef WIN32

#define NEEDED 256

int rnd_mouse(UINT i, WPARAM w, LPARAM l)
{
  static int entropy = 0;
  static int x, y, dx, dy;
  int newx, newy, newdx, newdy;
  int rnd[4];

  if (i == WM_MOUSEMOVE) {
    newx = LOWORD(l);
    newy = HIWORD(l);
    newdx = x - newx;
    newdy = y - newy;
    if (dx != 0 && dy != 0 && dx - newdx != 0 && dy - newdy != 0) {
      entropy++;
      if (entropy >= NEEDED)
	rnd_state = RND_SEEDED;
    }
    x = newx, y = newy, dx = newdx, dy = newdy;
    rnd[0] = x; rnd[1] = y; rnd[2] = dx; rnd[3] = dy;
    rnd_update((byte*)rnd, 4 * sizeof(int));
  }
  return (rnd_state == RND_SEEDED ? 100 : entropy * 100 / NEEDED);
}
#endif /* WIN32 */