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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// ---------------- [ File: bitcoin-random/src/randomenv.rs ]
crate::ix!();
//-------------------------------------------[.cpp/bitcoin/src/randomenv.h]
//-------------------------------------------[.cpp/bitcoin/src/randomenv.cpp]
pub const TEN_MINUTES: Duration = Duration::seconds(10 * 60);
pub const ONE_MILLISECOND: Duration = Duration::milliseconds(1);
/**
| Seed with the entire set of perfmon data
|
*/
#[cfg(WIN32)]
pub fn rand_add_seed_perfmon(hasher: &mut Sha512) {
/*
| This can take up to 2 seconds, so only
| do it every 10 minutes.
|
| Initialize last_perfmon to 0 seconds,
| we don't skip the first call.
*/
lazy_static!{
static ref last_perfmon: Mutex<Duration> = Mutex::new(Duration::ZERO);
}
let last_time: Duration = *last_perfmon.lock().unwrap();
let current_time: Duration = get_time();
if current_time < last_time + TEN_MINUTES {
return;
}
*last_perfmon.lock().unwrap() = current_time;
//-------------------------------------
let v_data: Vec<u8> = Vec::with_capacity(250000);
let ret: i64 = 0;
let n_size: u64 = 0;
// Bail out at more than 10MB of performance data
const n_max_size: usize = 10000000;
while true {
n_size = v_data.len().try_into().unwrap();
ret = winreg::RegQueryValueExA(
winreg::HKEY_PERFORMANCE_DATA,
"Global",
null_mut(),
null_mut(),
v_data.as_mut_ptr(),
&n_size);
if ret != winerror::ERROR_MORE_DATA || v_data.len() >= n_max_size {
break;
}
// Grow size of buffer exponentially
v_data.resize(
std::cmp::min((v_data.len() * 3) / 2, n_max_size),
0
);
}
winreg::RegCloseKey(winreg::HKEY_PERFORMANCE_DATA);
if ret == winerror::ERROR_SUCCESS {
hasher.write(
v_data.as_mut_ptr(),
n_size.try_into().unwrap());
memory_cleanse(
v_data.as_mut_ptr() as *mut c_void,
n_size.try_into().unwrap());
} else {
// Performance data is only a best-effort
// attempt at improving the situation when
// the OS randomness (and other sources)
// aren't adequate. As a result, failure
// to read it is isn't considered
// critical, so we don't call
// RandFailure().
//
// TODO: Add logging when the logger is
// made functional before global
// constructors have been invoked.
}
}
#[cfg(not(WIN32))]
pub fn add_sockaddr(
hasher: &mut Sha512,
addr: *const nix::sys::socket::sockaddr) {
if addr == null_mut() {
return;
}
unsafe {
match (*addr).sa_family {
AF_INET => hasher.write(
addr as *mut u8, size_of::<libc::sockaddr_in>()
),
AF_INET6 => hasher.write(
addr as *mut u8, size_of::<libc::sockaddr_in6>()
),
_ => hasher.write(
&(*addr).sa_family as *const _ as *const u8,
size_of_val(&(*addr).sa_family)
)
};
}
}
#[cfg(not(WIN32))]
pub fn add_file(
hasher: &mut Sha512,
path: *const i8) {
let mut sb: libc::stat = unsafe { std::mem::zeroed() };
let f: i32 = unsafe { libc::open(path, libc::O_RDONLY) };
let mut total: usize = 0;
if f != -1 {
let mut fbuf: [u8; 4096] = [0; 4096];
let mut n: i32 = 0;
hasher.write(
&f as *const _ as *const u8,
size_of_val(&f)
);
if unsafe { libc::fstat(f, &mut sb) } == 0 {
hasher.feed_data_in(&mut sb);
}
loop {
n = unsafe {
libc::read(
f,
fbuf.as_mut_ptr() as *mut c_void,
size_of_val(&fbuf)
).try_into().unwrap()
};
if n > 0 {
hasher.write(fbuf.as_mut_ptr(), n.try_into().unwrap());
}
let offset: usize = n.try_into().unwrap();
total += offset;
/* not bothering with EINTR handling. */
/*
| Read only the first 1 Mbyte
|
*/
let onward: bool =
n == (size_of_val(&fbuf) as i32) && total < 1048576;
if !onward {
break;
}
}
unsafe {
libc::close(f);
}
}
}
#[cfg(not(WIN32))]
pub fn add_path(
hasher: &mut Sha512,
path: *const u8) {
let mut sb: libc::stat = unsafe { std::mem::zeroed() };
if unsafe { libc::stat(path as *const i8, &mut sb) } == 0 {
let len = unsafe { libc::strlen(path as *const i8) + 1 };
hasher.write(path, len);
hasher.feed_data_in(&mut sb);
}
}
#[cfg(HAVE_SYSCTL)]
pub fn add_sysctl<const S: i32>(hasher: &mut Sha512) {
todo!();
/*
int CTL[sizeof...(S)] = {S...};
unsigned char buffer[65536];
size_t siz = 65536;
int ret = sysctl(CTL, sizeof...(S), buffer, &siz, nullptr, 0);
if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
hasher << sizeof(CTL);
hasher.Write((const unsigned char*)CTL, sizeof(CTL));
if (siz > sizeof(buffer)) siz = sizeof(buffer);
hasher << siz;
hasher.Write(buffer, siz);
}
*/
}
#[cfg(have_getcpuid)]
#[inline] pub fn addcpuid(
hasher: &mut Sha512,
leaf: u32,
subleaf: u32,
ax: &mut u32,
bx: &mut u32,
cx: &mut u32,
dx: &mut u32) {
getcpuid(leaf, subleaf, ax, bx, cx, dx);
*hasher <<= leaf;
*hasher <<= subleaf;
*hasher <<= ax;
*hasher <<= bx;
*hasher <<= cx;
*hasher <<= dx;
}
#[cfg(have_getcpuid)]
pub fn add_allcpuid(hasher: &mut Sha512) {
let mut ax: u32 = 0;
let mut bx: u32 = 0;
let mut cx: u32 = 0;
let mut dx: u32 = 0;
/*
| Iterate over all standard leaves
|
*/
addcpuid(
hasher,
0,
0,
&mut ax,
&mut bx,
&mut cx,
&mut dx
); // Returns max leaf in ax
let max: u32 = ax;
for leaf in 1..=std::cmp::min(max,0xFF) {
let mut maxsub: u32 = 0;
for subleaf in 0..=0xFF {
addcpuid(
hasher,
leaf,
subleaf,
&mut ax,
&mut bx,
&mut cx,
&mut dx);
/*
| Iterate subleafs for leaf values 4,
| 7, 11, 13
|
*/
if leaf == 4 {
if (ax & 0x1f) == 0 {
break;
}
} else if leaf == 7 {
if subleaf == 0 {
maxsub = ax;
}
if subleaf == maxsub {
break;
}
} else if leaf == 11 {
if (cx & 0xff00) == 0 {
break;
}
} else if leaf == 13 {
if ax == 0
&& bx == 0
&& cx == 0
&& dx == 0
{
break;
}
} else {
/*
| For any other leaf, stop after subleaf
| 0.
|
*/
break;
}
}
}
// Iterate over all extended leaves
addcpuid(hasher,
0x80000000,
0,
&mut ax,
&mut bx,
&mut cx,
&mut dx); // Returns max extended leaf in ax
let ext_max: u32 = ax;
for leaf in 0x80000001..=std::cmp::min(ext_max,0x800000FF) {
addcpuid(
hasher,
leaf,
0,
&mut ax,
&mut bx,
&mut cx,
&mut dx
);
}
}
#[cfg(test)]
mod randomenv_spec {
use super::*;
#[traced_test]
#[cfg(not(WIN32))]
fn add_sockaddr_accepts_null_and_specific_families() {
let mut hasher = Sha512::default();
// null is a no-op
add_sockaddr(&mut hasher, core::ptr::null());
// IPv4 sockaddr
let mut v4: libc::sockaddr_in = unsafe { core::mem::zeroed() };
v4.sin_family = libc::AF_INET as _;
let before = hasher.size();
add_sockaddr(&mut hasher, &v4 as *const _ as *const libc::sockaddr);
assert!(hasher.size() >= before);
}
#[traced_test]
#[cfg(not(WIN32))]
fn add_file_and_add_path_are_best_effort() {
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
let tmp = tempfile::NamedTempFile::new().expect("tmpfile");
std::fs::write(tmp.path(), b"hello world").unwrap();
let mut hasher = Sha512::default();
let before = hasher.size();
// add_file takes *const i8 (C string)
let cpath = CString::new(tmp.path().as_os_str().as_bytes()).unwrap();
add_file(&mut hasher, cpath.as_ptr());
let after_file = hasher.size();
assert!(after_file >= before);
// add_path takes *const u8
let cpath_u8 = CString::new(&b"/"[..]).unwrap();
let before2 = hasher.size();
add_path(&mut hasher, cpath_u8.as_ptr() as *const u8);
let after2 = hasher.size();
assert!(after2 >= before2);
}
}