/[pkg-mixmaster]/trunk/Mix/Src/rndseed.c
ViewVC logotype

Contents of /trunk/Mix/Src/rndseed.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 226 - (hide annotations) (download)
Sat Sep 7 11:16:37 2002 UTC (10 years, 9 months ago) by disastry
File MIME type: text/plain
File size: 3276 byte(s)
There is Sleep() in Windows, not sleep().
Well, this code is never used, but fix anyway
1 rabbi 1 /* Mixmaster version 3 -- (C) 1999 Anonymizer Inc.
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     Get randomness from device or user
9 disastry 226 $Id: rndseed.c,v 1.3 2002/09/07 11:16:37 disastry Exp $ */
10 rabbi 1
11    
12     #include "mix3.h"
13     #include <assert.h>
14     #include <time.h>
15     #include <fcntl.h>
16     #include <time.h>
17 weaselp 120 #include <stdlib.h>
18 rabbi 1 #ifdef POSIX
19     #include <unistd.h>
20     #include <termios.h>
21     #else
22     #include <io.h>
23     #include <process.h>
24     #endif
25     #if defined(WIN32) || defined(MSDOS)
26     #include <conio.h>
27     #endif
28     #ifdef WIN32
29     #include <windows.h>
30     #endif
31    
32     #define NEEDED 128
33    
34     #ifndef O_NDELAY
35     #define O_NDELAY 0
36     #endif
37    
38     int kbd_noecho(void)
39     {
40     #ifdef HAVE_TERMIOS
41     int fd;
42     struct termios attr;
43    
44     setbuf(stdin, NULL);
45     fd = fileno(stdin);
46     if (tcgetattr(fd, &attr) != 0)
47     return (-1);
48     attr.c_lflag &= ~(ECHO | ICANON);
49     if (tcsetattr(fd, TCSAFLUSH, &attr) != 0)
50     return (-1);
51     #endif
52     return (0);
53     }
54    
55     int kbd_echo(void)
56     {
57     #ifdef HAVE_TERMIOS
58     int fd;
59     struct termios attr;
60    
61     setvbuf(stdin, NULL, _IOLBF, BUFSIZ);
62     fd = fileno(stdin);
63     if (tcgetattr(fd, &attr) != 0)
64     return (-1);
65     attr.c_lflag |= ECHO | ICANON;
66     if (tcsetattr(fd, TCSAFLUSH, &attr) != 0)
67     return (-1);
68     #endif
69     return (0);
70     }
71    
72     void rnd_error(void)
73     {
74     errlog(ERRORMSG,
75     "Random number generator not initialized. Aborting.\n\
76     Run the program interactively to seed the generator.\n");
77     exit(3);
78     }
79    
80     /* get randomness from system or user. If the application has promised that
81     it will seed the RNG later, we do not ask for user input */
82    
83     int rnd_seed(void)
84     {
85     int fd = -1;
86     byte b[512], c = 0;
87     int bytes = 0;
88    
89     #ifdef DEV_RANDOM
90     fd = open(DEV_RANDOM, O_RDONLY | O_NDELAY);
91     #endif
92     if (fd == -1) {
93     #if 1
94     if (rnd_state == RND_WILLSEED)
95     return(-1);
96     if (!isatty(fileno(stdin)))
97     rnd_error();
98     #else
99     #error "should initialize the prng from system ressources"
100     #endif
101     fprintf(stderr, "Please enter some random characters.\n");
102     kbd_noecho();
103     while (bytes < NEEDED) {
104     fprintf(stderr, " %d \r", NEEDED - bytes);
105     #ifdef HAVE_GETKEY
106     if (kbhit(), *b = getkey())
107     #else
108     if (read(fileno(stdin), b, 1) > 0)
109     #endif
110     {
111     rnd_add(b, 1);
112     rnd_time();
113     if (*b != c)
114     bytes++;
115     c = *b;
116     }
117     }
118     fprintf(stderr, "Thanks.\n");
119     #ifdef WIN32
120     Sleep(1000);
121     #else
122     sleep(1);
123     #endif
124     kbd_echo();
125     }
126     #ifdef DEV_RANDOM
127     else {
128     bytes = read(fd, b, sizeof(b));
129 weaselp 120 if (bytes > 0) {
130     rnd_add(b, bytes);
131     } else {
132     bytes = 0;
133     }
134 rabbi 1 close(fd);
135     if (bytes < NEEDED) {
136     fd = open(DEV_RANDOM, O_RDONLY); /* re-open in blocking mode */
137     if (isatty(fileno(stdin))) {
138     fprintf(stderr,
139     "Please move the mouse, enter random characters, etc.\n");
140     kbd_noecho();
141     }
142     while (bytes < NEEDED) {
143     if (isatty(fileno(stdin)))
144     fprintf(stderr, " %d \r", NEEDED - bytes);
145     if (read(fd, b, 1) > 0) {
146     rnd_add(b, 1);
147     bytes++;
148     }
149     }
150     if (isatty(fileno(stdin))) {
151     fprintf(stderr, "Thanks.\n");
152     #ifdef WIN32
153 disastry 226 Sleep(1000);
154 rabbi 1 #else
155     sleep(1);
156     #endif
157     kbd_echo();
158     }
159     close(fd);
160     }
161     }
162     #endif
163     rnd_state = RND_SEEDED;
164     return (0);
165     }

  ViewVC Help
Powered by ViewVC 1.1.5