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
  
// $Id: randomness.pmod,v 1.25 2003/03/07 17:41:48 nilsson Exp $ 
 
//! Assorted stronger or weaker randomnumber generators. 
//! These devices try to collect entropy from the environment. 
//! They differ in behaviour when they run low on entropy, /dev/random 
//! will block if it can't provide enough random bits, while /dev/urandom 
//! will degenerate into a reasonably strong pseudo random generator 
 
#pike __REAL_VERSION__ 
// #pragma strict_types 
 
static constant RANDOM_DEVICE = "/dev/random"; 
static constant PRANDOM_DEVICE = "/dev/urandom"; 
 
/* Collect somewhat random data from the environment. Unfortunately, 
 * this is quite system dependent 
 */ 
static constant PATH = "/usr/sbin:/usr/etc:/usr/bin/:/sbin/:/etc:/bin"; 
 
#ifndef __NT__ 
static constant SYSTEM_COMMANDS = ({ 
  "last -256", "arp -a", 
  "netstat -anv","netstat -mv","netstat -sv", 
  "uptime","ps -fel","ps aux", 
  "vmstat -s","vmstat -M", 
  "iostat","iostat -cdDItx" 
}); 
#endif 
 
static RandomSource global_arcfour; 
static int(0..1) goodseed; 
 
#ifdef __NT__ 
static string nt_random_string(int len) { 
  object ctx = Crypto.nt.CryptAcquireContext(0, 0, Crypto.nt.PROV_RSA_FULL, 
                                             Crypto.nt.CRYPT_VERIFYCONTEXT 
                                             /*|Crypto.nt.CRYPT_SILENT*/); 
  if(!ctx) 
    error( "Couldn't create crypto context.\n" ); 
 
  string res = ctx->CryptGenRandom(len); 
 
  if(!res) 
    error( "Couldn't generate randomness.\n" ); 
 
  destruct(ctx); 
  return res; 
} 
#endif 
 
//! Executes several programs (last -256, arp -a, netstat -anv, netstat -mv, 
//! netstat -sv, uptime, ps -fel, ps aux, vmstat -s, vmstat -M, iostat, 
//! iostat -cdDItx) to generate some entropy from their output. On Microsoft 
//! Windows the Windows cryptographic routines are called to generate random 
//! data. 
string some_entropy() 
{ 
#ifdef __NT__ 
  return nt_random_string(8192); 
#else /* !__NT__ */ 
  mapping env = [mapping(string:string)]getenv(); 
  env->PATH = PATH; 
 
  Stdio.File parent_pipe = Stdio.File(); 
  Stdio.File child_pipe = parent_pipe->pipe(); 
  if (!child_pipe) 
    error( "Couldn't create pipe.\n" ); 
 
  Stdio.File null = Stdio.File("/dev/null","rw"); 
 
  foreach(SYSTEM_COMMANDS, string cmd) 
    { 
      catch { 
        Process.create_process(Process.split_quoted_string(cmd), 
                                       (["stdin":null, 
                                        "stdout":child_pipe, 
                                        "stderr":null, 
                                        "env":env])); 
      }; 
    } 
 
  destruct(child_pipe); 
 
  return parent_pipe->read(); 
#endif 
} 
 
//! Virtual class for randomness source object. 
class RandomSource { 
 
  //! Returns a string of length len with (pseudo) random values. 
  string read(int(0..) len) { return random_string(len); } 
} 
 
// Compatibility 
class pike_random { 
  inherit RandomSource; 
} 
 
#ifdef __NT__ 
static class NTSource { 
  string read(int(0..) len) { return nt_random_string(len); } 
} 
#endif 
 
#if constant(Crypto.arcfour) 
//! A pseudo random generator based on the arcfour crypto. 
class arcfour_random { 
 
  inherit Crypto.arcfour : arcfour; 
 
  //! Initialize and seed the arcfour random generator. 
  void create(string secret) 
  { 
    Crypto.sha hash = Crypto.sha(); 
    hash->update(secret); 
 
    arcfour::set_encrypt_key(hash->digest()); 
  } 
 
  //! Return a string of the next len random characters from the 
  //! arcfour random generator. 
  string read(int len) 
  { 
    if (len > 16384) return read(len/2)+read(len-len/2); 
    return arcfour::crypt("\47" * len); 
  } 
} 
 
#endif /* constant(Crypto.arcfour) */ 
 
//! Returns a reasonably random random-source. 
RandomSource reasonably_random() 
{ 
#ifdef __NT__ 
  return NTSource(); 
#endif 
 
  if (file_stat(PRANDOM_DEVICE)) 
  { 
    Stdio.File res = Stdio.File(); 
    if (res->open(PRANDOM_DEVICE, "r")) 
      return res; 
  } 
 
  if (global_arcfour) 
    return global_arcfour; 
 
  string seed = some_entropy(); 
#if constant(Crypto.arcfour) 
  if (sizeof(seed) < 2001) 
    seed = random_string(2001); // Well, we're only at reasonably random... 
  return (global_arcfour = arcfour_random(sprintf("%4c%O%s", time(), 
                                                  _memory_usage(), seed))); 
#else /* !constant(Crypto.arcfour) */ 
 
  // Not very random, but at least a fallback... 
  if(!goodseed) { 
    random_seed( time() + Array.sum( (array)seed ) ); 
    goodseed = 1; 
  } 
  return global_arcfour = pike_random(); 
#endif /* constant(Crypto.arcfour) */ 
} 
 
//! Returns a really random random-source. 
RandomSource really_random(int|void may_block) 
{ 
#ifdef __NT__ 
  return NTSource(); 
#endif 
  Stdio.File res = Stdio.File(); 
  if (may_block && file_stat(RANDOM_DEVICE)) 
  { 
    if (res->open(RANDOM_DEVICE, "r")) 
      return res; 
  } 
 
  if (file_stat(PRANDOM_DEVICE)) 
  { 
    if (res->open(PRANDOM_DEVICE, "r")) 
      return res; 
  } 
 
  error( "No source found.\n" ); 
}