/[pkg-mixmaster]/branches/mixmaster_2_9_STABLE/Mix/Src/random.c
ViewVC logotype

Contents of /branches/mixmaster_2_9_STABLE/Mix/Src/random.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 458 - (show annotations) (download)
Sun Jan 12 22:21:26 2003 UTC (10 years, 4 months ago) by colintu
File MIME type: text/plain
File size: 4070 byte(s)
changes for Windows build
1 /* Mixmaster version 2.9 -- (C) 1999 - 2002 Anonymizer Inc. and others.
2
3 Mixmaster may be redistributed and modified under certain conditions.
4 This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
5 ANY KIND, either express or implied. See the file COPYRIGHT for
6 details.
7
8 Randomness
9 $Id: random.c,v 1.2.2.3 2003/01/12 22:21:26 colintu Exp $ */
10
11
12 #include "mix3.h"
13 #include "crypto.h"
14 #include <fcntl.h>
15 #ifdef POSIX
16 #include <sys/time.h>
17 #include <unistd.h>
18 #else /* end of POSIX */
19 #include <io.h>
20 #include <process.h>
21 #endif /* else if not POSIX */
22 #ifdef WIN32
23 #include <windows.h>
24 #endif /* WIN32 */
25 #include <assert.h>
26 #include <string.h>
27
28 int rnd_state = RND_NOTSEEDED;
29
30 #ifdef USE_OPENSSL
31 int rnd_init(void)
32 {
33 char r[PATHMAX];
34 int n;
35 LOCK *rndlock;
36
37 if (rnd_state == RND_SEEDED)
38 return(0);
39 rndlock = lockfile(MIXRAND);
40 mixfile(r, MIXRAND);
41 n = RAND_load_file(r, 1024);
42 if (n < 256 && rnd_seed() == -1)
43 goto err;
44 rnd_time();
45 RAND_write_file(r);
46 rnd_state = RND_SEEDED;
47 err:
48 unlockfile(rndlock);
49 return (rnd_state == RND_SEEDED ? 0 : -1);
50 }
51
52 int rnd_final(void)
53 {
54 int err = 0;
55 char r[PATHMAX];
56 LOCK *rndlock;
57
58 if (rnd_state != RND_SEEDED)
59 return(-1);
60
61 rnd_update(NULL, 0);
62 rndlock = lockfile(MIXRAND);
63 mixfile(r, MIXRAND);
64 RAND_load_file(r, 1024); /* add seed file again in case other instances
65 of the program have used it */
66 if (RAND_write_file(r) < 1)
67 err = -1;
68 unlockfile(rndlock);
69 RAND_cleanup();
70 return (err);
71 }
72
73 int rnd_add(byte *b, int l)
74 {
75 RAND_seed(b, l);
76 return (0);
77 }
78 #endif /* USE_OPENSSL */
79
80 void rnd_time(void)
81 {
82 int pid;
83
84 #ifdef WIN32
85 SYSTEMTIME t;
86 #endif /* WIN32 */
87
88 #ifdef HAVE_GETTIMEOFDAY
89 struct timeval tv;
90
91 gettimeofday(&tv, 0);
92 rnd_add((byte *) &tv, sizeof(tv));
93 #elif defined(WIN32) /* end of HAVE_GETTIMEOFDAY */
94 GetSystemTime(&t);
95 rnd_add((byte *) &t, sizeof(t));
96 #else /* end of defined(WIN32) */
97 rnd_add((byte *) time(NULL), sizeof(time_t));
98 #endif /* else if not defined(WIN32), HAVE_GETTIMEOFDAY */
99 pid = getpid();
100 rnd_add((byte *) &pid, sizeof(pid));
101 }
102
103 void rnd_update(byte *seed, int l)
104 {
105 int fd = -1;
106 #ifdef DEV_URANDOM
107 byte b[512];
108 #endif /* DEV_URANDOM */
109
110 rnd_time();
111 if (seed)
112 rnd_add(seed, l);
113 #ifdef DEV_URANDOM
114 fd = open(DEV_URANDOM, O_RDONLY);
115 if (fd != -1) {
116 ssize_t ret;
117
118 ret = read(fd, b, sizeof(b));
119 if (ret > 0) {
120 rnd_add(b, ret);
121 }
122 close(fd);
123 }
124 #endif /* DEV_URANDOM */
125 }
126
127 int rnd_bytes(byte *b, int n)
128 {
129 /* we frequently need to get small amounts of random data.
130 speed up by pre-generating dating data */
131
132 static byte rand[BUFSIZE];
133 static int idx = BUFSIZE;
134
135 if (rnd_state != RND_SEEDED)
136 rnd_error();
137
138 if (n + idx < BUFSIZE) {
139 memcpy(b, rand + idx, n);
140 idx += n;
141 } else
142 RAND_bytes(b, n);
143
144 if (idx + 256 > BUFSIZE) {
145 RAND_bytes(rand, BUFSIZE);
146 idx = 0;
147 }
148 return (0);
149 }
150
151 int rnd_number(int n)
152 {
153 int r;
154
155 assert(n > 0);
156 if (n > 65535)
157 do
158 r = rnd_byte() * 65536 +
159 rnd_byte() * 256 + rnd_byte();
160 while (r >= n);
161 else if (n > 255)
162 do
163 r = rnd_byte() * 256 + rnd_byte();
164 while (r >= n);
165 else
166 do
167 r = rnd_byte();
168 while (r >= n);
169 return r;
170 }
171
172 byte rnd_byte()
173 {
174 byte b;
175
176 rnd_bytes(&b, 1);
177 return b;
178 }
179
180 void rnd_initialized(void)
181 {
182 rnd_state = RND_SEEDED;
183 }
184
185 #ifdef WIN32
186
187 #define NEEDED 256
188
189 int rnd_mouse(UINT i, WPARAM w, LPARAM l)
190 {
191 static int entropy = 0;
192 static int x, y, dx, dy;
193 int newx, newy, newdx, newdy;
194 int rnd[4];
195
196 if (i == WM_MOUSEMOVE) {
197 newx = LOWORD(l);
198 newy = HIWORD(l);
199 newdx = x - newx;
200 newdy = y - newy;
201 if (dx != 0 && dy != 0 && dx - newdx != 0 && dy - newdy != 0) {
202 entropy++;
203 if (entropy >= NEEDED)
204 rnd_state = RND_SEEDED;
205 }
206 x = newx, y = newy, dx = newdx, dy = newdy;
207 rnd[0] = x; rnd[1] = y; rnd[2] = dx; rnd[3] = dy;
208 rnd_update((byte*)rnd, 4 * sizeof(int));
209 }
210 return (rnd_state == RND_SEEDED ? 100 : entropy * 100 / NEEDED);
211 }
212 #endif /* WIN32 */

  ViewVC Help
Powered by ViewVC 1.1.5