rng.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2018, The Tor Project, Inc.
  2. // Copyright (c) 2018, isis agora lovecruft
  3. // See LICENSE for licensing information
  4. //! Wrappers for Tor's random number generators to provide implementations of
  5. //! `rand_core` traits.
  6. // This is the real implementation, in use in production, which calls into our C
  7. // wrappers in /src/common/crypto_rand.c, which call into OpenSSL, system
  8. // libraries, and make syscalls.
  9. #[cfg(not(test))]
  10. mod internal {
  11. use std::u64;
  12. use rand_core::CryptoRng;
  13. use rand_core::Error;
  14. use rand_core::RngCore;
  15. use rand_core::impls::next_u32_via_fill;
  16. use rand_core::impls::next_u64_via_fill;
  17. use external::c_tor_crypto_rand;
  18. use external::c_tor_crypto_strongest_rand;
  19. use external::c_tor_crypto_seed_rng;
  20. use tor_log::LogDomain;
  21. use tor_log::LogSeverity;
  22. /// Largest strong entropy request permitted.
  23. //
  24. // C_RUST_COUPLED: `MAX_STRONGEST_RAND_SIZE` /src/common/crypto_rand.c
  25. const MAX_STRONGEST_RAND_SIZE: usize = 256;
  26. /// A wrapper around OpenSSL's RNG.
  27. pub struct TorRng {
  28. // This private, zero-length field forces the struct to be treated the
  29. // same as its opaque C couterpart.
  30. _unused: [u8; 0],
  31. }
  32. /// Mark `TorRng` as being suitable for cryptographic purposes.
  33. impl CryptoRng for TorRng {}
  34. impl TorRng {
  35. // C_RUST_COUPLED: `crypto_seed_rng()` /src/common/crypto_rand.c
  36. #[allow(dead_code)]
  37. pub fn new() -> Self {
  38. if !c_tor_crypto_seed_rng() {
  39. tor_log_msg!(LogSeverity::Warn, LogDomain::General,
  40. "TorRng::from_seed()",
  41. "The RNG could not be seeded!");
  42. }
  43. // XXX also log success at info level —isis
  44. TorRng{ _unused: [0u8; 0] }
  45. }
  46. }
  47. impl RngCore for TorRng {
  48. // C_RUST_COUPLED: `crypto_strongest_rand()` /src/common/crypto_rand.c
  49. fn next_u32(&mut self) -> u32 {
  50. next_u32_via_fill(self)
  51. }
  52. // C_RUST_COUPLED: `crypto_strongest_rand()` /src/common/crypto_rand.c
  53. fn next_u64(&mut self) -> u64 {
  54. next_u64_via_fill(self)
  55. }
  56. // C_RUST_COUPLED: `crypto_strongest_rand()` /src/common/crypto_rand.c
  57. fn fill_bytes(&mut self, dest: &mut [u8]) {
  58. c_tor_crypto_rand(dest);
  59. }
  60. // C_RUST_COUPLED: `crypto_strongest_rand()` /src/common/crypto_rand.c
  61. fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
  62. Ok(self.fill_bytes(dest))
  63. }
  64. }
  65. /// A CSPRNG which hashes together randomness from OpenSSL's RNG and entropy
  66. /// obtained from the operating system.
  67. pub struct TorStrongestRng {
  68. // This private, zero-length field forces the struct to be treated the
  69. // same as its opaque C couterpart.
  70. _unused: [u8; 0],
  71. }
  72. /// Mark `TorRng` as being suitable for cryptographic purposes.
  73. impl CryptoRng for TorStrongestRng {}
  74. impl TorStrongestRng {
  75. // C_RUST_COUPLED: `crypto_seed_rng()` /src/common/crypto_rand.c
  76. #[allow(dead_code)]
  77. pub fn new() -> Self {
  78. if !c_tor_crypto_seed_rng() {
  79. tor_log_msg!(LogSeverity::Warn, LogDomain::General,
  80. "TorStrongestRng::from_seed()",
  81. "The RNG could not be seeded!");
  82. }
  83. // XXX also log success at info level —isis
  84. TorStrongestRng{ _unused: [0u8; 0] }
  85. }
  86. }
  87. impl RngCore for TorStrongestRng {
  88. // C_RUST_COUPLED: `crypto_strongest_rand()` /src/common/crypto_rand.c
  89. fn next_u32(&mut self) -> u32 {
  90. next_u32_via_fill(self)
  91. }
  92. // C_RUST_COUPLED: `crypto_strongest_rand()` /src/common/crypto_rand.c
  93. fn next_u64(&mut self) -> u64 {
  94. next_u64_via_fill(self)
  95. }
  96. // C_RUST_COUPLED: `crypto_strongest_rand()` /src/common/crypto_rand.c
  97. fn fill_bytes(&mut self, dest: &mut [u8]) {
  98. debug_assert!(dest.len() <= MAX_STRONGEST_RAND_SIZE);
  99. c_tor_crypto_strongest_rand(dest);
  100. }
  101. // C_RUST_COUPLED: `crypto_strongest_rand()` /src/common/crypto_rand.c
  102. fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
  103. Ok(self.fill_bytes(dest))
  104. }
  105. }
  106. }
  107. // For testing, we expose a pure-Rust implementation.
  108. #[cfg(test)]
  109. mod internal {
  110. // It doesn't matter if we pretend ChaCha is a CSPRNG in tests.
  111. pub use rand::ChaChaRng as TorRng;
  112. pub use rand::ChaChaRng as TorStrongestRng;
  113. }
  114. // Finally, expose the public functionality of whichever appropriate internal
  115. // module.
  116. pub use self::internal::*;