Debian Codegraph @

random.c

raw symbols SCIP
1 /* -*- Mode: C; c-file-style: "stroustrup" -*- */
2
3 #include <stdio.h>
4 #include <limits.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include "random.h"
8
9 int initialized = 0;
10
11 /* on machines that have /dev/urandom -- use it */
12
13 #if defined( __linux__ ) || defined( __FreeBSD__ ) || defined( __MACH__ ) || \
14 defined( __OpenBSD__ ) || defined( DEV_URANDOM )
15
16 #define URANDOM_FILE "/dev/urandom"
17 FILE* urandom = NULL;
18
19 int random_init( void )
20 {
21 int res = (urandom = fopen( URANDOM_FILE, "r" )) != NULL;
22 if ( res ) { initialized = 1; }
23 return res;
24 }
25
26 int random_getbytes( void* data, size_t len )
27 {
28 if ( !initialized && !random_init() ) { return 0; }
29 return fread( data, len, 1, urandom );
30 }
31
32 int random_final( void )
33 {
34 int res = 0;
35 if ( urandom ) { res = (fclose( urandom ) == 0); }
36 return res;
37 }
38
39 #else
40
41 /* if no /dev/urandom fall back to a crappy rng who's only
42 * entropy source is your high resolution timer
43 */
44
45 /* on windows we are ok as we can use CAPI, but use CAPI combined with
46 the below just to be sure! */
47
48 /* WARNING: on platforms other than windows this is not of
49 * cryptographic quality
50 */
51
52 #include <stdlib.h>
53
54 #if defined( unix ) || defined( VMS )
55 #include <unistd.h>
56 #include <sys/time.h>
57 #elif defined( WIN32 )
58 #include <process.h>
59 #include <windows.h>
60 #include <wincrypt.h>
61 #include <sys/time.h>
62 #else
63 #include <time.h>
64 #endif
65 #include <time.h>
66 #if defined( OPENSSL )
67 #include <openssl/sha.h>
68 #define SHA1_ctx SHA_CTX
69 #define SHA1_Final( x, digest ) SHA1_Final( digest, x )
70 #define SHA1_DIGEST_BYTES SHA_DIGEST_LENGTH
71 #else
72 #include "sha1.h"
73 #endif
74
75 #if defined( WIN32 )
76 #define pid_t int
77 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXT)(HCRYPTPROV *, LPCTSTR, LPCTSTR,
78 DWORD, DWORD);
79 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *);
80 typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV, DWORD);
81 HCRYPTPROV hProvider = 0;
82 CRYPTRELEASECONTEXT release = 0;
83 CRYPTGENRANDOM gen = 0;
84 #endif
85
86 byte state[ SHA1_DIGEST_BYTES ];
87 byte output[ SHA1_DIGEST_BYTES ];
88 long counter = 0;
89
90 /* output = SHA1( input || time || pid || counter++ ) */
91
92 static void random_stir( const byte input[SHA1_DIGEST_BYTES],
93 byte output[SHA1_DIGEST_BYTES] )
94 {
95 SHA1_ctx sha1;
96 #if defined(__unix__) || defined(WIN32)
97 pid_t pid = getpid();
98 #else
99 unsigned long pid = rand();
100 #endif
101 #if defined(__unix__)
102 struct timeval tv = {};
103 struct timezone tz = {};
104 #endif
105 #if defined(WIN32)
106 SYSTEMTIME tw;
107 BYTE buf[64];
108 #endif
109 clock_t t = clock();
110 time_t t2 = time(0);
111
112 SHA1_Init( &sha1 );
113 #if defined(__unix__)
114 gettimeofday(&tv,&tz);
115 SHA1_Update( &sha1, &tv, sizeof( tv ) );
116 SHA1_Update( &sha1, &tz, sizeof( tz ) );
117 #endif
118 #if defined(WIN32)
119 GetSystemTime(&tw);
120 SHA1_Update( &sha1, &tw, sizeof( tw ) );
121 if ( gen ) {
122 if (gen(hProvider, sizeof(buf), buf)) {
123 SHA1_Update( &sha1, buf, sizeof(buf) );
124 }
125 }
126 #endif
127 SHA1_Update( &sha1, input, SHA1_DIGEST_BYTES );
128 SHA1_Update( &sha1, &t, sizeof( clock_t ) );
129 SHA1_Update( &sha1, &t2, sizeof( time_t ) );
130 SHA1_Update( &sha1, &pid, sizeof( pid ) );
131 SHA1_Update( &sha1, &counter, sizeof( long ) );
132
133 SHA1_Final( &sha1, output );
134 counter++;
135 }
136
137 int random_init( void )
138 {
139 #if defined(WIN32)
140 HMODULE advapi = 0;
141 CRYPTACQUIRECONTEXT acquire = 0;
142 #endif
143
144 #if defined(WIN32)
145 advapi = LoadLibrary(TEXT("ADVAPI32.DLL"));
146 if (advapi) {
147 acquire = (CRYPTACQUIRECONTEXT)
148 GetProcAddress(advapi, TEXT("CryptAcquireContextA"));
149 gen = (CRYPTGENRANDOM)
150 GetProcAddress(advapi, TEXT("CryptGenRandom"));
151 release = (CRYPTRELEASECONTEXT)
152 GetProcAddress(advapi, TEXT("CryptReleaseContext"));
153 }
154 if ( acquire && gen ) {
155 if (!acquire(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
156 gen = NULL;
157 }
158 }
159 #endif
160 srand(clock());
161 random_stir( state, state );
162
163 initialized = 1;
164
165 return 1;
166 }
167
168 int random_final( void )
169 {
170 #if defined(WIN32)
171 if ( hProvider && release ) { release(hProvider,0); }
172 #endif
173 return 1;
174 }
175
176 #define CHUNK_LEN (SHA1_DIGEST_BYTES)
177
178 int random_getbytes( void* rnd, size_t len )
179 {
180 byte* rndp = (byte*)rnd;
181 int use = 0;
182
183 if ( !initialized && !random_init() ) { return 0; }
184
185 random_stir( state, state ); /* mix in the time, pid */
186 for ( ; len > 0; len -= use, rndp += CHUNK_LEN ) {
187 random_stir( state, output );
188 use = len > CHUNK_LEN ? CHUNK_LEN : len;
189 memcpy( rndp, output, use );
190 }
191 return 1;
192 }
193
194 #endif
195
196 static int count_bits( long val )
197 {
198 int count = 0 ;
199 for ( count = 0; val; val >>= 1, count++ ) { }
200 return count;
201 }
202
203 int random_rectangular( long top, long* resp )
204 {
205 long mask = 0 ;
206 int neg = 1;
207 long res = 0 ;
208
209 if ( top < 0 ) { neg = -1; top = -top; }
210 mask = ~( LONG_MAX << count_bits( top ) );
211 do {
212 if ( !random_getbytes( &res, sizeof( long ) ) ) { return 0; }
213 res &= mask;
214 } while ( res > top );
215 *resp = res * neg;
216 return 1;
217 }