summaryrefslogtreecommitdiffstats
path: root/vendor/openssl/src/rand.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/openssl/src/rand.rs
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/openssl/src/rand.rs')
-rw-r--r--vendor/openssl/src/rand.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/openssl/src/rand.rs b/vendor/openssl/src/rand.rs
new file mode 100644
index 000000000..8317951f8
--- /dev/null
+++ b/vendor/openssl/src/rand.rs
@@ -0,0 +1,60 @@
+//! Utilities for secure random number generation.
+//!
+//! # Examples
+//!
+//! To generate a buffer with cryptographically strong bytes:
+//!
+//! ```
+//! use openssl::rand::rand_bytes;
+//!
+//! let mut buf = [0; 256];
+//! rand_bytes(&mut buf).unwrap();
+//! ```
+use libc::c_int;
+
+use crate::error::ErrorStack;
+use crate::{cvt, LenType};
+use openssl_macros::corresponds;
+
+/// Fill buffer with cryptographically strong pseudo-random bytes.
+///
+/// # Examples
+///
+/// To generate a buffer with cryptographically strong random bytes:
+///
+/// ```
+/// use openssl::rand::rand_bytes;
+///
+/// let mut buf = [0; 256];
+/// rand_bytes(&mut buf).unwrap();
+/// ```
+#[corresponds(RAND_bytes)]
+pub fn rand_bytes(buf: &mut [u8]) -> Result<(), ErrorStack> {
+ unsafe {
+ ffi::init();
+ assert!(buf.len() <= c_int::max_value() as usize);
+ cvt(ffi::RAND_bytes(buf.as_mut_ptr(), buf.len() as LenType)).map(|_| ())
+ }
+}
+
+/// Controls random device file descriptor behavior.
+///
+/// Requires OpenSSL 1.1.1 or newer.
+#[corresponds(RAND_keep_random_devices_open)]
+#[cfg(ossl111)]
+pub fn keep_random_devices_open(keep: bool) {
+ unsafe {
+ ffi::RAND_keep_random_devices_open(keep as LenType);
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::rand_bytes;
+
+ #[test]
+ fn test_rand_bytes() {
+ let mut buf = [0; 32];
+ rand_bytes(&mut buf).unwrap();
+ }
+}