diff options
| author | Alex Crichton <[email protected]> | 2016-09-30 00:43:05 -0700 |
|---|---|---|
| committer | Alex Crichton <[email protected]> | 2016-10-12 22:49:55 -0700 |
| commit | 43c951f743e68fac5f45119eda7c994882a1d489 (patch) | |
| tree | 45169f1b92858a3ba2ad0287de1bf1ecb395804b /openssl/src/bn | |
| parent | Rename NoPadding to None (diff) | |
| download | rust-openssl-43c951f743e68fac5f45119eda7c994882a1d489.tar.xz rust-openssl-43c951f743e68fac5f45119eda7c994882a1d489.zip | |
Add support for OpenSSL 1.1.0
This commit is relatively major refactoring of the `openssl-sys` crate as well
as the `openssl` crate itself. The end goal here was to support OpenSSL 1.1.0,
and lots of other various tweaks happened along the way. The major new features
are:
* OpenSSL 1.1.0 is supported
* OpenSSL 0.9.8 is no longer supported (aka all OSX users by default)
* All FFI bindings are verified with the `ctest` crate (same way as the `libc`
crate)
* CI matrixes are vastly expanded to include 32/64 of all platforms, more
OpenSSL version coverage, as well as ARM coverage on Linux
* The `c_helpers` module is completely removed along with the `gcc` dependency.
* The `openssl-sys` build script was completely rewritten
* Now uses `OPENSSL_DIR` to find the installation, not include/lib env vars.
* Better error messages for mismatched versions.
* Better error messages for failing to find OpenSSL on a platform (more can be
done here)
* Probing of OpenSSL build-time configuration to inform the API of the `*-sys`
crate.
* Many Cargo features have been removed as they're now enabled by default.
As this is a breaking change to both the `openssl` and `openssl-sys` crates this
will necessitate a major version bump of both. There's still a few more API
questions remaining but let's hash that out on a PR!
Closes #452
Diffstat (limited to 'openssl/src/bn')
| -rw-r--r-- | openssl/src/bn/mod.rs | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index de9d0d2a..7d1f5458 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -1,4 +1,4 @@ -use libc::{c_int, c_ulong, c_void}; +use libc::{c_int, c_void}; use std::ffi::{CStr, CString}; use std::cmp::Ordering; use std::{fmt, ptr}; @@ -185,10 +185,11 @@ impl<'a> BigNumRef<'a> { } } - /// Add an `unsigned long` to `self`. This is more efficient than adding a `BigNum`. - pub fn add_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { + /// Add a `u32` to `self`. This is more efficient than adding a + /// `BigNum`. + pub fn add_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_add_word(self.as_ptr(), w) == 1 { + if ffi::BN_add_word(self.as_ptr(), w as ffi::BN_ULONG) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -196,9 +197,9 @@ impl<'a> BigNumRef<'a> { } } - pub fn sub_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { + pub fn sub_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_sub_word(self.as_ptr(), w) == 1 { + if ffi::BN_sub_word(self.as_ptr(), w as ffi::BN_ULONG) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -206,9 +207,9 @@ impl<'a> BigNumRef<'a> { } } - pub fn mul_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { + pub fn mul_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_mul_word(self.as_ptr(), w) == 1 { + if ffi::BN_mul_word(self.as_ptr(), w as ffi::BN_ULONG) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -216,22 +217,22 @@ impl<'a> BigNumRef<'a> { } } - pub fn div_word(&mut self, w: c_ulong) -> Result<c_ulong, ErrorStack> { + pub fn div_word(&mut self, w: u32) -> Result<u64, ErrorStack> { unsafe { - let result = ffi::BN_div_word(self.as_ptr(), w); - if result != !0 as c_ulong { - Ok(result) + let result = ffi::BN_div_word(self.as_ptr(), w as ffi::BN_ULONG); + if result != !0 { + Ok(result.into()) } else { Err(ErrorStack::get()) } } } - pub fn mod_word(&self, w: c_ulong) -> Result<c_ulong, ErrorStack> { + pub fn mod_word(&self, w: u32) -> Result<u64, ErrorStack> { unsafe { - let result = ffi::BN_mod_word(self.as_ptr(), w); - if result != !0 as c_ulong { - Ok(result) + let result = ffi::BN_mod_word(self.as_ptr(), w as ffi::BN_ULONG); + if result != !0 { + Ok(result as u64) } else { Err(ErrorStack::get()) } @@ -257,7 +258,10 @@ impl<'a> BigNumRef<'a> { pub fn is_prime(&self, checks: i32) -> Result<bool, ErrorStack> { unsafe { with_ctx!(ctx, { - Ok(ffi::BN_is_prime_ex(self.as_ptr(), checks as c_int, ctx, ptr::null()) == 1) + Ok(ffi::BN_is_prime_ex(self.as_ptr(), + checks as c_int, + ctx, + ptr::null_mut()) == 1) }) } } @@ -278,7 +282,7 @@ impl<'a> BigNumRef<'a> { checks as c_int, ctx, do_trial_division as c_int, - ptr::null()) == 1) + ptr::null_mut()) == 1) }) } } @@ -483,9 +487,19 @@ impl<'a> BigNumRef<'a> { } pub fn is_negative(&self) -> bool { + self._is_negative() + } + + #[cfg(ossl10x)] + fn _is_negative(&self) -> bool { unsafe { (*self.as_ptr()).neg == 1 } } + #[cfg(ossl110)] + fn _is_negative(&self) -> bool { + unsafe { ffi::BN_is_negative(self.as_ptr()) == 1 } + } + /// Returns the number of significant bits in `self`. pub fn num_bits(&self) -> i32 { unsafe { ffi::BN_num_bits(self.as_ptr()) as i32 } @@ -536,7 +550,7 @@ impl<'a> BigNumRef<'a> { assert!(!buf.is_null()); let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec()) .unwrap(); - ffi::CRYPTO_free(buf as *mut c_void); + CRYPTO_free!(buf as *mut c_void); str } } @@ -555,7 +569,7 @@ impl<'a> BigNumRef<'a> { assert!(!buf.is_null()); let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec()) .unwrap(); - ffi::CRYPTO_free(buf as *mut c_void); + CRYPTO_free!(buf as *mut c_void); str } } @@ -580,27 +594,27 @@ impl BigNum { } /// Creates a new `BigNum` with the given value. - pub fn new_from(n: c_ulong) -> Result<BigNum, ErrorStack> { + pub fn new_from(n: u32) -> Result<BigNum, ErrorStack> { BigNum::new().and_then(|v| unsafe { - try_ssl!(ffi::BN_set_word(v.as_ptr(), n)); + try_ssl!(ffi::BN_set_word(v.as_ptr(), n as ffi::BN_ULONG)); Ok(v) }) } /// Creates a `BigNum` from a decimal string. pub fn from_dec_str(s: &str) -> Result<BigNum, ErrorStack> { - BigNum::new().and_then(|v| unsafe { + BigNum::new().and_then(|mut v| unsafe { let c_str = CString::new(s.as_bytes()).unwrap(); - try_ssl!(ffi::BN_dec2bn(&(v.0).0, c_str.as_ptr() as *const _)); + try_ssl!(ffi::BN_dec2bn(&mut (v.0).0, c_str.as_ptr() as *const _)); Ok(v) }) } /// Creates a `BigNum` from a hexadecimal string. pub fn from_hex_str(s: &str) -> Result<BigNum, ErrorStack> { - BigNum::new().and_then(|v| unsafe { + BigNum::new().and_then(|mut v| unsafe { let c_str = CString::new(s.as_bytes()).unwrap(); - try_ssl!(ffi::BN_hex2bn(&(v.0).0, c_str.as_ptr() as *const _)); + try_ssl!(ffi::BN_hex2bn(&mut (v.0).0, c_str.as_ptr() as *const _)); Ok(v) }) } @@ -646,7 +660,7 @@ impl BigNum { safe as c_int, add_arg, rem_arg, - ptr::null()) == 1 + ptr::null_mut()) == 1 }) } } |