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

  ViewVC Help
Powered by ViewVC 1.1.5