diff options
| author | Steven Fackler <[email protected]> | 2016-08-10 21:25:18 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-08-10 21:25:18 -0700 |
| commit | c15642ccea8e38362ab65cfbb94d638518375658 (patch) | |
| tree | ae4fc2f42a1afbd96b7e41e268ceabaa6fe4e68e /openssl/src/crypto | |
| parent | More API cleanup (diff) | |
| download | rust-openssl-c15642ccea8e38362ab65cfbb94d638518375658.tar.xz rust-openssl-c15642ccea8e38362ab65cfbb94d638518375658.zip | |
Tweaks
Diffstat (limited to 'openssl/src/crypto')
| -rw-r--r-- | openssl/src/crypto/dsa.rs | 7 | ||||
| -rw-r--r-- | openssl/src/crypto/rand.rs | 20 |
2 files changed, 10 insertions, 17 deletions
diff --git a/openssl/src/crypto/dsa.rs b/openssl/src/crypto/dsa.rs index 7e6f0381..7d6e1c05 100644 --- a/openssl/src/crypto/dsa.rs +++ b/openssl/src/crypto/dsa.rs @@ -15,7 +15,7 @@ use crypto::util::{CallbackState, invoke_passwd_cb}; pub struct DSAParams(*mut ffi::DSA); impl DSAParams { - pub fn with_size(size: usize) -> Result<DSAParams, ErrorStack> { + pub fn with_size(size: u32) -> Result<DSAParams, ErrorStack> { unsafe { // Wrap it so that if we panic we'll call the dtor let dsa = DSAParams(try_ssl_null!(ffi::DSA_new())); @@ -55,14 +55,13 @@ impl Drop for DSA { } impl DSA { - /// the caller should assert that the dsa pointer is valid. - pub unsafe fn from_raw(dsa: *mut ffi::DSA) -> DSA { + pub unsafe fn from_ptr(dsa: *mut ffi::DSA) -> DSA { DSA(dsa) } /// Generate a DSA key pair /// For more complicated key generation scenarios see the `DSAParams` type - pub fn generate(size: usize) -> Result<DSA, ErrorStack> { + pub fn generate(size: u32) -> Result<DSA, ErrorStack> { let params = try!(DSAParams::with_size(size)); params.generate() } diff --git a/openssl/src/crypto/rand.rs b/openssl/src/crypto/rand.rs index ba57a8a1..519449e9 100644 --- a/openssl/src/crypto/rand.rs +++ b/openssl/src/crypto/rand.rs @@ -1,19 +1,13 @@ use libc::c_int; use ffi; +use error::ErrorStack; -pub fn rand_bytes(len: usize) -> Vec<u8> { +pub fn rand_bytes(buf: &mut [u8]) -> Result<(), ErrorStack> { unsafe { - let mut out = Vec::with_capacity(len); - ffi::init(); - let r = ffi::RAND_bytes(out.as_mut_ptr(), len as c_int); - if r != 1 as c_int { - panic!() - } - - out.set_len(len); - - out + assert!(buf.len() <= c_int::max_value() as usize); + try_ssl_if!(ffi::RAND_bytes(buf.as_mut_ptr(), buf.len() as c_int) != 1); + Ok(()) } } @@ -23,7 +17,7 @@ mod tests { #[test] fn test_rand_bytes() { - let bytes = rand_bytes(32); - println!("{:?}", bytes); + let mut buf = [0; 32]; + rand_bytes(&mut buf).unwrap(); } } |