|
8 | 8 | // option. This file may not be copied, modified, or distributed |
9 | 9 | // except according to those terms. |
10 | 10 |
|
11 | | -//! Interfaces to the operating system provided random number |
12 | | -//! generators. |
13 | | -
|
14 | 11 | pub use self::imp::OsRng; |
15 | 12 |
|
16 | 13 | #[cfg(all(unix, not(target_os = "ios"), not(target_os = "openbsd")))] |
@@ -125,17 +122,6 @@ mod imp { |
125 | 122 | target_arch = "powerpc64"))))] |
126 | 123 | fn is_getrandom_available() -> bool { false } |
127 | 124 |
|
128 | | - /// A random number generator that retrieves randomness straight from |
129 | | - /// the operating system. Platform sources: |
130 | | - /// |
131 | | - /// - Unix-like systems (Linux, Android, Mac OSX): read directly from |
132 | | - /// `/dev/urandom`, or from `getrandom(2)` system call if available. |
133 | | - /// - Windows: calls `CryptGenRandom`, using the default cryptographic |
134 | | - /// service provider with the `PROV_RSA_FULL` type. |
135 | | - /// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed. |
136 | | - /// - OpenBSD: uses the `getentropy(2)` system call. |
137 | | - /// |
138 | | - /// This does not block. |
139 | 125 | pub struct OsRng { |
140 | 126 | inner: OsRngInner, |
141 | 127 | } |
@@ -189,17 +175,6 @@ mod imp { |
189 | 175 | use sys::os::errno; |
190 | 176 | use rand::Rng; |
191 | 177 |
|
192 | | - /// A random number generator that retrieves randomness straight from |
193 | | - /// the operating system. Platform sources: |
194 | | - /// |
195 | | - /// - Unix-like systems (Linux, Android, Mac OSX): read directly from |
196 | | - /// `/dev/urandom`, or from `getrandom(2)` system call if available. |
197 | | - /// - Windows: calls `CryptGenRandom`, using the default cryptographic |
198 | | - /// service provider with the `PROV_RSA_FULL` type. |
199 | | - /// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed. |
200 | | - /// - OpenBSD: uses the `getentropy(2)` system call. |
201 | | - /// |
202 | | - /// This does not block. |
203 | 178 | pub struct OsRng { |
204 | 179 | // dummy field to ensure that this struct cannot be constructed outside |
205 | 180 | // of this module |
@@ -246,17 +221,6 @@ mod imp { |
246 | 221 | use rand::Rng; |
247 | 222 | use libc::{c_int, size_t}; |
248 | 223 |
|
249 | | - /// A random number generator that retrieves randomness straight from |
250 | | - /// the operating system. Platform sources: |
251 | | - /// |
252 | | - /// - Unix-like systems (Linux, Android, Mac OSX): read directly from |
253 | | - /// `/dev/urandom`, or from `getrandom(2)` system call if available. |
254 | | - /// - Windows: calls `CryptGenRandom`, using the default cryptographic |
255 | | - /// service provider with the `PROV_RSA_FULL` type. |
256 | | - /// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed. |
257 | | - /// - OpenBSD: uses the `getentropy(2)` system call. |
258 | | - /// |
259 | | - /// This does not block. |
260 | 224 | pub struct OsRng { |
261 | 225 | // dummy field to ensure that this struct cannot be constructed outside |
262 | 226 | // of this module |
@@ -307,133 +271,3 @@ mod imp { |
307 | 271 | } |
308 | 272 | } |
309 | 273 | } |
310 | | - |
311 | | -#[cfg(windows)] |
312 | | -mod imp { |
313 | | - use io; |
314 | | - use mem; |
315 | | - use rand::Rng; |
316 | | - use sys::c; |
317 | | - |
318 | | - /// A random number generator that retrieves randomness straight from |
319 | | - /// the operating system. Platform sources: |
320 | | - /// |
321 | | - /// - Unix-like systems (Linux, Android, Mac OSX): read directly from |
322 | | - /// `/dev/urandom`, or from `getrandom(2)` system call if available. |
323 | | - /// - Windows: calls `CryptGenRandom`, using the default cryptographic |
324 | | - /// service provider with the `PROV_RSA_FULL` type. |
325 | | - /// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed. |
326 | | - /// - OpenBSD: uses the `getentropy(2)` system call. |
327 | | - /// |
328 | | - /// This does not block. |
329 | | - pub struct OsRng { |
330 | | - hcryptprov: c::HCRYPTPROV |
331 | | - } |
332 | | - |
333 | | - impl OsRng { |
334 | | - /// Create a new `OsRng`. |
335 | | - pub fn new() -> io::Result<OsRng> { |
336 | | - let mut hcp = 0; |
337 | | - let ret = unsafe { |
338 | | - c::CryptAcquireContextA(&mut hcp, 0 as c::LPCSTR, 0 as c::LPCSTR, |
339 | | - c::PROV_RSA_FULL, |
340 | | - c::CRYPT_VERIFYCONTEXT | c::CRYPT_SILENT) |
341 | | - }; |
342 | | - |
343 | | - if ret == 0 { |
344 | | - Err(io::Error::last_os_error()) |
345 | | - } else { |
346 | | - Ok(OsRng { hcryptprov: hcp }) |
347 | | - } |
348 | | - } |
349 | | - } |
350 | | - |
351 | | - impl Rng for OsRng { |
352 | | - fn next_u32(&mut self) -> u32 { |
353 | | - let mut v = [0; 4]; |
354 | | - self.fill_bytes(&mut v); |
355 | | - unsafe { mem::transmute(v) } |
356 | | - } |
357 | | - fn next_u64(&mut self) -> u64 { |
358 | | - let mut v = [0; 8]; |
359 | | - self.fill_bytes(&mut v); |
360 | | - unsafe { mem::transmute(v) } |
361 | | - } |
362 | | - fn fill_bytes(&mut self, v: &mut [u8]) { |
363 | | - let ret = unsafe { |
364 | | - c::CryptGenRandom(self.hcryptprov, v.len() as c::DWORD, |
365 | | - v.as_mut_ptr()) |
366 | | - }; |
367 | | - if ret == 0 { |
368 | | - panic!("couldn't generate random bytes: {}", |
369 | | - io::Error::last_os_error()); |
370 | | - } |
371 | | - } |
372 | | - } |
373 | | - |
374 | | - impl Drop for OsRng { |
375 | | - fn drop(&mut self) { |
376 | | - let ret = unsafe { |
377 | | - c::CryptReleaseContext(self.hcryptprov, 0) |
378 | | - }; |
379 | | - if ret == 0 { |
380 | | - panic!("couldn't release context: {}", |
381 | | - io::Error::last_os_error()); |
382 | | - } |
383 | | - } |
384 | | - } |
385 | | -} |
386 | | - |
387 | | -#[cfg(test)] |
388 | | -mod tests { |
389 | | - use sync::mpsc::channel; |
390 | | - use rand::Rng; |
391 | | - use super::OsRng; |
392 | | - use thread; |
393 | | - |
394 | | - #[test] |
395 | | - fn test_os_rng() { |
396 | | - let mut r = OsRng::new().unwrap(); |
397 | | - |
398 | | - r.next_u32(); |
399 | | - r.next_u64(); |
400 | | - |
401 | | - let mut v = [0; 1000]; |
402 | | - r.fill_bytes(&mut v); |
403 | | - } |
404 | | - |
405 | | - #[test] |
406 | | - fn test_os_rng_tasks() { |
407 | | - |
408 | | - let mut txs = vec!(); |
409 | | - for _ in 0..20 { |
410 | | - let (tx, rx) = channel(); |
411 | | - txs.push(tx); |
412 | | - |
413 | | - thread::spawn(move|| { |
414 | | - // wait until all the threads are ready to go. |
415 | | - rx.recv().unwrap(); |
416 | | - |
417 | | - // deschedule to attempt to interleave things as much |
418 | | - // as possible (XXX: is this a good test?) |
419 | | - let mut r = OsRng::new().unwrap(); |
420 | | - thread::yield_now(); |
421 | | - let mut v = [0; 1000]; |
422 | | - |
423 | | - for _ in 0..100 { |
424 | | - r.next_u32(); |
425 | | - thread::yield_now(); |
426 | | - r.next_u64(); |
427 | | - thread::yield_now(); |
428 | | - r.fill_bytes(&mut v); |
429 | | - thread::yield_now(); |
430 | | - } |
431 | | - }); |
432 | | - } |
433 | | - |
434 | | - // start all the threads |
435 | | - for tx in &txs { |
436 | | - tx.send(()).unwrap(); |
437 | | - } |
438 | | - } |
439 | | -} |
0 commit comments