Loading openssl/src/asn1.rs +1 −3 Original line number Diff line number Diff line Loading @@ -64,9 +64,7 @@ impl Deref for Asn1Time { type Target = Asn1TimeRef; fn deref(&self) -> &Asn1TimeRef { unsafe { Asn1TimeRef::from_ptr(self.0) } unsafe { Asn1TimeRef::from_ptr(self.0) } } } Loading openssl/src/bio.rs +3 −6 Original line number Diff line number Diff line Loading @@ -22,9 +22,8 @@ impl<'a> MemBioSlice<'a> { ffi::init(); assert!(buf.len() <= c_int::max_value() as usize); let bio = unsafe { try!(cvt_p(BIO_new_mem_buf(buf.as_ptr() as *const _, buf.len() as c_int))) }; let bio = unsafe { try!(cvt_p(BIO_new_mem_buf(buf.as_ptr() as *const _, buf.len() as c_int))) }; Ok(MemBioSlice(bio, PhantomData)) } Loading @@ -48,9 +47,7 @@ impl MemBio { pub fn new() -> Result<MemBio, ErrorStack> { ffi::init(); let bio = unsafe { try!(cvt_p(ffi::BIO_new(ffi::BIO_s_mem()))) }; let bio = unsafe { try!(cvt_p(ffi::BIO_new(ffi::BIO_s_mem()))) }; Ok(MemBio(bio)) } Loading openssl/src/bn.rs +50 −92 Original line number Diff line number Diff line Loading @@ -37,9 +37,7 @@ impl Drop for BnCtx { impl BnCtx { /// Returns a new `BnCtx`. pub fn new() -> Result<BnCtx, ErrorStack> { unsafe { cvt_p(ffi::BN_CTX_new()).map(BnCtx) } unsafe { cvt_p(ffi::BN_CTX_new()).map(BnCtx) } } pub fn as_ptr(&self) -> *mut ffi::BN_CTX { Loading @@ -52,9 +50,7 @@ impl BnCtx { a: &BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mul(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_mul(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Places the result of `a / b` in `dv` and `a mod b` in `rem`. Loading @@ -76,19 +72,16 @@ impl BnCtx { /// Places the result of `a²` in `r`. pub fn sqr(&mut self, r: &mut BigNumRef, a: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_sqr(r.as_ptr(), a.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_sqr(r.as_ptr(), a.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Places the result of `a mod m` in `r`. pub fn nnmod(&mut self, r: &mut BigNumRef, a: &BigNumRef, m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_nnmod(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_nnmod(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } } /// Places the result of `(a + b) mod m` in `r`. Loading Loading @@ -131,20 +124,18 @@ impl BnCtx { pub fn mod_sqr(&mut self, r: &mut BigNumRef, a: &BigNumRef, m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mod_sqr(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mod_sqr(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } } /// Places the result of `a^p` in `r`. pub fn exp(&mut self, r: &mut BigNumRef, a: &BigNumRef, p: &BigNumRef) -> Result<(), ErrorStack> { unsafe{ cvt(ffi::BN_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), self.0)).map(|_| ()) } p: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), self.0)).map(|_| ()) } } /// Places the result of `a^p mod m` in `r`. Loading @@ -152,7 +143,8 @@ impl BnCtx { r: &mut BigNumRef, a: &BigNumRef, p: &BigNumRef, m: &BigNumRef) -> Result<(), ErrorStack> { m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mod_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } Loading @@ -162,9 +154,11 @@ impl BnCtx { pub fn mod_inverse(&mut self, r: &mut BigNumRef, a: &BigNumRef, n: &BigNumRef) -> Result<(), ErrorStack> { n: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt_p(ffi::BN_mod_inverse(r.as_ptr(), a.as_ptr(), n.as_ptr(), self.as_ptr())).map(|_| ()) cvt_p(ffi::BN_mod_inverse(r.as_ptr(), a.as_ptr(), n.as_ptr(), self.as_ptr())) .map(|_| ()) } } Loading @@ -172,10 +166,9 @@ impl BnCtx { pub fn gcd(&mut self, r: &mut BigNumRef, a: &BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_gcd(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_gcd(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Checks whether `p` is prime. Loading @@ -185,7 +178,8 @@ impl BnCtx { /// Returns `true` if `p` is prime with an error probability of less than `0.25 ^ checks`. pub fn is_prime(&mut self, p: &BigNumRef, checks: i32) -> Result<bool, ErrorStack> { unsafe { cvt_n(ffi::BN_is_prime_ex(p.as_ptr(), checks.into(), self.as_ptr(), ptr::null_mut())).map(|r| r != 0) cvt_n(ffi::BN_is_prime_ex(p.as_ptr(), checks.into(), self.as_ptr(), ptr::null_mut())) .map(|r| r != 0) } } Loading @@ -201,7 +195,8 @@ impl BnCtx { pub fn is_prime_fasttest(&mut self, p: &BigNumRef, checks: i32, do_trial_division: bool) -> Result<bool, ErrorStack> { do_trial_division: bool) -> Result<bool, ErrorStack> { unsafe { cvt_n(ffi::BN_is_prime_fasttest_ex(p.as_ptr(), checks.into(), Loading Loading @@ -236,7 +231,8 @@ impl BnCtx { odd: bool) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_pseudo_rand(r.as_ptr(), bits.into(), prop as c_int, odd as c_int)).map(|_| ()) cvt(ffi::BN_pseudo_rand(r.as_ptr(), bits.into(), prop as c_int, odd as c_int)) .map(|_| ()) } } } Loading @@ -259,23 +255,17 @@ impl BigNumRef { /// Adds a `u32` to `self`. pub fn add_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_add_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } unsafe { cvt(ffi::BN_add_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } } /// Subtracts a `u32` from `self`. pub fn sub_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_sub_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } unsafe { cvt(ffi::BN_sub_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } } /// Multiplies a `u32` by `self`. pub fn mul_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mul_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } unsafe { cvt(ffi::BN_mul_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } } /// Divides `self` by a `u32`, returning the remainder. Loading Loading @@ -305,105 +295,77 @@ impl BigNumRef { /// Places a cryptographically-secure pseudo-random number nonnegative /// number less than `self` in `rnd`. pub fn rand_in_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) } } /// The cryptographically weak counterpart to `rand_in_range`. pub fn pseudo_rand_in_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_pseudo_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_pseudo_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) } } /// Sets bit `n`. Equivalent to `self |= (1 << n)`. /// /// When setting a bit outside of `self`, it is expanded. pub fn set_bit(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_set_bit(self.as_ptr(), n.into())).map(|_| ()) } unsafe { cvt(ffi::BN_set_bit(self.as_ptr(), n.into())).map(|_| ()) } } /// Clears bit `n`, setting it to 0. Equivalent to `self &= ~(1 << n)`. /// /// When clearing a bit outside of `self`, an error is returned. pub fn clear_bit(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_clear_bit(self.as_ptr(), n.into())).map(|_| ()) } unsafe { cvt(ffi::BN_clear_bit(self.as_ptr(), n.into())).map(|_| ()) } } /// Returns `true` if the `n`th bit of `self` is set to 1, `false` otherwise. pub fn is_bit_set(&self, n: i32) -> bool { unsafe { ffi::BN_is_bit_set(self.as_ptr(), n.into()) == 1 } unsafe { ffi::BN_is_bit_set(self.as_ptr(), n.into()) == 1 } } /// Truncates `self` to the lowest `n` bits. /// /// An error occurs if `self` is already shorter than `n` bits. pub fn mask_bits(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mask_bits(self.as_ptr(), n.into())).map(|_| ()) } unsafe { cvt(ffi::BN_mask_bits(self.as_ptr(), n.into())).map(|_| ()) } } /// Places `self << 1` in `r`. pub fn lshift1(&self, r: &mut BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_lshift1(r.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_lshift1(r.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Places `self >> 1` in `r`. pub fn rshift1(&self, r: &mut BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_rshift1(r.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_rshift1(r.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Places `self + b` in `r`. pub fn add(&self, r: &mut BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_add(r.as_ptr(), self.as_ptr(), b.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_add(r.as_ptr(), self.as_ptr(), b.as_ptr())).map(|_| ()) } } /// Places `self - b` in `r`. pub fn sub(&self, r: &mut BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_sub(r.as_ptr(), self.as_ptr(), b.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_sub(r.as_ptr(), self.as_ptr(), b.as_ptr())).map(|_| ()) } } /// Places `self << n` in `r`. pub fn lshift(&self, r: &mut BigNumRef, b: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_lshift(r.as_ptr(), self.as_ptr(), b.into())).map(|_| ()) } unsafe { cvt(ffi::BN_lshift(r.as_ptr(), self.as_ptr(), b.into())).map(|_| ()) } } /// Places `self >> n` in `r`. pub fn rshift(&self, r: &mut BigNumRef, n: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_rshift(r.as_ptr(), self.as_ptr(), n.into())).map(|_| ()) } unsafe { cvt(ffi::BN_rshift(r.as_ptr(), self.as_ptr(), n.into())).map(|_| ()) } } pub fn to_owned(&self) -> Result<BigNum, ErrorStack> { unsafe { cvt_p(ffi::BN_dup(self.as_ptr())).map(|b| BigNum::from_ptr(b)) } unsafe { cvt_p(ffi::BN_dup(self.as_ptr())).map(|b| BigNum::from_ptr(b)) } } /// Sets the sign of `self`. pub fn set_negative(&mut self, negative: bool) { unsafe { ffi::BN_set_negative(self.as_ptr(), negative as c_int) } unsafe { ffi::BN_set_negative(self.as_ptr(), negative as c_int) } } /// Compare the absolute values of `self` and `oth`. Loading @@ -417,9 +379,7 @@ impl BigNumRef { /// assert_eq!(s.ucmp(&o), Ordering::Equal); /// ``` pub fn ucmp(&self, oth: &BigNumRef) -> Ordering { unsafe { ffi::BN_ucmp(self.as_ptr(), oth.as_ptr()).cmp(&0) } unsafe { ffi::BN_ucmp(self.as_ptr(), oth.as_ptr()).cmp(&0) } } pub fn is_negative(&self) -> bool { Loading Loading @@ -592,7 +552,9 @@ impl BigNum { impl Drop for BigNum { fn drop(&mut self) { unsafe { ffi::BN_clear_free(self.as_ptr()); } unsafe { ffi::BN_clear_free(self.as_ptr()); } } } Loading @@ -600,17 +562,13 @@ impl Deref for BigNum { type Target = BigNumRef; fn deref(&self) -> &BigNumRef { unsafe { BigNumRef::from_ptr(self.0) } unsafe { BigNumRef::from_ptr(self.0) } } } impl DerefMut for BigNum { fn deref_mut(&mut self) -> &mut BigNumRef { unsafe { BigNumRef::from_ptr_mut(self.0) } unsafe { BigNumRef::from_ptr_mut(self.0) } } } Loading openssl/src/dh.rs +10 −19 Original line number Diff line number Diff line Loading @@ -50,33 +50,25 @@ impl Dh { /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))] pub fn get_1024_160() -> Result<Dh, ErrorStack> { unsafe { cvt_p(ffi::DH_get_1024_160()).map(Dh) } unsafe { cvt_p(ffi::DH_get_1024_160()).map(Dh) } } /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))] pub fn get_2048_224() -> Result<Dh, ErrorStack> { unsafe { cvt_p(ffi::DH_get_2048_224()).map(Dh) } unsafe { cvt_p(ffi::DH_get_2048_224()).map(Dh) } } /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))] pub fn get_2048_256() -> Result<Dh, ErrorStack> { unsafe { cvt_p(ffi::DH_get_2048_256()).map(Dh) } unsafe { cvt_p(ffi::DH_get_2048_256()).map(Dh) } } } impl Drop for Dh { fn drop(&mut self) { unsafe { ffi::DH_free(self.0) } unsafe { ffi::DH_free(self.0) } } } Loading @@ -84,9 +76,7 @@ impl Deref for Dh { type Target = DhRef; fn deref(&self) -> &DhRef { unsafe { DhRef::from_ptr(self.0) } unsafe { DhRef::from_ptr(self.0) } } } Loading @@ -104,7 +94,8 @@ mod compat { pub unsafe fn DH_set0_pqg(dh: *mut ffi::DH, p: *mut ffi::BIGNUM, q: *mut ffi::BIGNUM, g: *mut ffi::BIGNUM) -> c_int { g: *mut ffi::BIGNUM) -> c_int { (*dh).p = p; (*dh).q = q; (*dh).g = g; Loading openssl/src/dsa.rs +12 −13 Original line number Diff line number Diff line Loading @@ -105,8 +105,7 @@ impl Dsa { } /// Writes an DSA private key as unencrypted PEM formatted data pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { assert!(self.has_private_key()); let mem_bio = try!(MemBio::new()); Loading @@ -120,8 +119,7 @@ impl Dsa { } /// Reads an DSA public key from PEM formatted data. pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> { pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> { ffi::init(); let mem_bio = try!(MemBioSlice::new(buf)); Loading Loading @@ -258,7 +256,8 @@ mod test { password[4] = b's' as c_char; password[5] = b's' as c_char; 6 }).unwrap(); }) .unwrap(); assert!(password_queried); } Loading Loading
openssl/src/asn1.rs +1 −3 Original line number Diff line number Diff line Loading @@ -64,9 +64,7 @@ impl Deref for Asn1Time { type Target = Asn1TimeRef; fn deref(&self) -> &Asn1TimeRef { unsafe { Asn1TimeRef::from_ptr(self.0) } unsafe { Asn1TimeRef::from_ptr(self.0) } } } Loading
openssl/src/bio.rs +3 −6 Original line number Diff line number Diff line Loading @@ -22,9 +22,8 @@ impl<'a> MemBioSlice<'a> { ffi::init(); assert!(buf.len() <= c_int::max_value() as usize); let bio = unsafe { try!(cvt_p(BIO_new_mem_buf(buf.as_ptr() as *const _, buf.len() as c_int))) }; let bio = unsafe { try!(cvt_p(BIO_new_mem_buf(buf.as_ptr() as *const _, buf.len() as c_int))) }; Ok(MemBioSlice(bio, PhantomData)) } Loading @@ -48,9 +47,7 @@ impl MemBio { pub fn new() -> Result<MemBio, ErrorStack> { ffi::init(); let bio = unsafe { try!(cvt_p(ffi::BIO_new(ffi::BIO_s_mem()))) }; let bio = unsafe { try!(cvt_p(ffi::BIO_new(ffi::BIO_s_mem()))) }; Ok(MemBio(bio)) } Loading
openssl/src/bn.rs +50 −92 Original line number Diff line number Diff line Loading @@ -37,9 +37,7 @@ impl Drop for BnCtx { impl BnCtx { /// Returns a new `BnCtx`. pub fn new() -> Result<BnCtx, ErrorStack> { unsafe { cvt_p(ffi::BN_CTX_new()).map(BnCtx) } unsafe { cvt_p(ffi::BN_CTX_new()).map(BnCtx) } } pub fn as_ptr(&self) -> *mut ffi::BN_CTX { Loading @@ -52,9 +50,7 @@ impl BnCtx { a: &BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mul(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_mul(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Places the result of `a / b` in `dv` and `a mod b` in `rem`. Loading @@ -76,19 +72,16 @@ impl BnCtx { /// Places the result of `a²` in `r`. pub fn sqr(&mut self, r: &mut BigNumRef, a: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_sqr(r.as_ptr(), a.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_sqr(r.as_ptr(), a.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Places the result of `a mod m` in `r`. pub fn nnmod(&mut self, r: &mut BigNumRef, a: &BigNumRef, m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_nnmod(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_nnmod(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } } /// Places the result of `(a + b) mod m` in `r`. Loading Loading @@ -131,20 +124,18 @@ impl BnCtx { pub fn mod_sqr(&mut self, r: &mut BigNumRef, a: &BigNumRef, m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mod_sqr(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mod_sqr(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } } /// Places the result of `a^p` in `r`. pub fn exp(&mut self, r: &mut BigNumRef, a: &BigNumRef, p: &BigNumRef) -> Result<(), ErrorStack> { unsafe{ cvt(ffi::BN_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), self.0)).map(|_| ()) } p: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), self.0)).map(|_| ()) } } /// Places the result of `a^p mod m` in `r`. Loading @@ -152,7 +143,8 @@ impl BnCtx { r: &mut BigNumRef, a: &BigNumRef, p: &BigNumRef, m: &BigNumRef) -> Result<(), ErrorStack> { m: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mod_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } Loading @@ -162,9 +154,11 @@ impl BnCtx { pub fn mod_inverse(&mut self, r: &mut BigNumRef, a: &BigNumRef, n: &BigNumRef) -> Result<(), ErrorStack> { n: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt_p(ffi::BN_mod_inverse(r.as_ptr(), a.as_ptr(), n.as_ptr(), self.as_ptr())).map(|_| ()) cvt_p(ffi::BN_mod_inverse(r.as_ptr(), a.as_ptr(), n.as_ptr(), self.as_ptr())) .map(|_| ()) } } Loading @@ -172,10 +166,9 @@ impl BnCtx { pub fn gcd(&mut self, r: &mut BigNumRef, a: &BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_gcd(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_gcd(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Checks whether `p` is prime. Loading @@ -185,7 +178,8 @@ impl BnCtx { /// Returns `true` if `p` is prime with an error probability of less than `0.25 ^ checks`. pub fn is_prime(&mut self, p: &BigNumRef, checks: i32) -> Result<bool, ErrorStack> { unsafe { cvt_n(ffi::BN_is_prime_ex(p.as_ptr(), checks.into(), self.as_ptr(), ptr::null_mut())).map(|r| r != 0) cvt_n(ffi::BN_is_prime_ex(p.as_ptr(), checks.into(), self.as_ptr(), ptr::null_mut())) .map(|r| r != 0) } } Loading @@ -201,7 +195,8 @@ impl BnCtx { pub fn is_prime_fasttest(&mut self, p: &BigNumRef, checks: i32, do_trial_division: bool) -> Result<bool, ErrorStack> { do_trial_division: bool) -> Result<bool, ErrorStack> { unsafe { cvt_n(ffi::BN_is_prime_fasttest_ex(p.as_ptr(), checks.into(), Loading Loading @@ -236,7 +231,8 @@ impl BnCtx { odd: bool) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_pseudo_rand(r.as_ptr(), bits.into(), prop as c_int, odd as c_int)).map(|_| ()) cvt(ffi::BN_pseudo_rand(r.as_ptr(), bits.into(), prop as c_int, odd as c_int)) .map(|_| ()) } } } Loading @@ -259,23 +255,17 @@ impl BigNumRef { /// Adds a `u32` to `self`. pub fn add_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_add_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } unsafe { cvt(ffi::BN_add_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } } /// Subtracts a `u32` from `self`. pub fn sub_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_sub_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } unsafe { cvt(ffi::BN_sub_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } } /// Multiplies a `u32` by `self`. pub fn mul_word(&mut self, w: u32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mul_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } unsafe { cvt(ffi::BN_mul_word(self.as_ptr(), w as ffi::BN_ULONG)).map(|_| ()) } } /// Divides `self` by a `u32`, returning the remainder. Loading Loading @@ -305,105 +295,77 @@ impl BigNumRef { /// Places a cryptographically-secure pseudo-random number nonnegative /// number less than `self` in `rnd`. pub fn rand_in_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) } } /// The cryptographically weak counterpart to `rand_in_range`. pub fn pseudo_rand_in_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_pseudo_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_pseudo_rand_range(self.as_ptr(), rnd.as_ptr())).map(|_| ()) } } /// Sets bit `n`. Equivalent to `self |= (1 << n)`. /// /// When setting a bit outside of `self`, it is expanded. pub fn set_bit(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_set_bit(self.as_ptr(), n.into())).map(|_| ()) } unsafe { cvt(ffi::BN_set_bit(self.as_ptr(), n.into())).map(|_| ()) } } /// Clears bit `n`, setting it to 0. Equivalent to `self &= ~(1 << n)`. /// /// When clearing a bit outside of `self`, an error is returned. pub fn clear_bit(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_clear_bit(self.as_ptr(), n.into())).map(|_| ()) } unsafe { cvt(ffi::BN_clear_bit(self.as_ptr(), n.into())).map(|_| ()) } } /// Returns `true` if the `n`th bit of `self` is set to 1, `false` otherwise. pub fn is_bit_set(&self, n: i32) -> bool { unsafe { ffi::BN_is_bit_set(self.as_ptr(), n.into()) == 1 } unsafe { ffi::BN_is_bit_set(self.as_ptr(), n.into()) == 1 } } /// Truncates `self` to the lowest `n` bits. /// /// An error occurs if `self` is already shorter than `n` bits. pub fn mask_bits(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_mask_bits(self.as_ptr(), n.into())).map(|_| ()) } unsafe { cvt(ffi::BN_mask_bits(self.as_ptr(), n.into())).map(|_| ()) } } /// Places `self << 1` in `r`. pub fn lshift1(&self, r: &mut BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_lshift1(r.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_lshift1(r.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Places `self >> 1` in `r`. pub fn rshift1(&self, r: &mut BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_rshift1(r.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_rshift1(r.as_ptr(), self.as_ptr())).map(|_| ()) } } /// Places `self + b` in `r`. pub fn add(&self, r: &mut BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_add(r.as_ptr(), self.as_ptr(), b.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_add(r.as_ptr(), self.as_ptr(), b.as_ptr())).map(|_| ()) } } /// Places `self - b` in `r`. pub fn sub(&self, r: &mut BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_sub(r.as_ptr(), self.as_ptr(), b.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_sub(r.as_ptr(), self.as_ptr(), b.as_ptr())).map(|_| ()) } } /// Places `self << n` in `r`. pub fn lshift(&self, r: &mut BigNumRef, b: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_lshift(r.as_ptr(), self.as_ptr(), b.into())).map(|_| ()) } unsafe { cvt(ffi::BN_lshift(r.as_ptr(), self.as_ptr(), b.into())).map(|_| ()) } } /// Places `self >> n` in `r`. pub fn rshift(&self, r: &mut BigNumRef, n: i32) -> Result<(), ErrorStack> { unsafe { cvt(ffi::BN_rshift(r.as_ptr(), self.as_ptr(), n.into())).map(|_| ()) } unsafe { cvt(ffi::BN_rshift(r.as_ptr(), self.as_ptr(), n.into())).map(|_| ()) } } pub fn to_owned(&self) -> Result<BigNum, ErrorStack> { unsafe { cvt_p(ffi::BN_dup(self.as_ptr())).map(|b| BigNum::from_ptr(b)) } unsafe { cvt_p(ffi::BN_dup(self.as_ptr())).map(|b| BigNum::from_ptr(b)) } } /// Sets the sign of `self`. pub fn set_negative(&mut self, negative: bool) { unsafe { ffi::BN_set_negative(self.as_ptr(), negative as c_int) } unsafe { ffi::BN_set_negative(self.as_ptr(), negative as c_int) } } /// Compare the absolute values of `self` and `oth`. Loading @@ -417,9 +379,7 @@ impl BigNumRef { /// assert_eq!(s.ucmp(&o), Ordering::Equal); /// ``` pub fn ucmp(&self, oth: &BigNumRef) -> Ordering { unsafe { ffi::BN_ucmp(self.as_ptr(), oth.as_ptr()).cmp(&0) } unsafe { ffi::BN_ucmp(self.as_ptr(), oth.as_ptr()).cmp(&0) } } pub fn is_negative(&self) -> bool { Loading Loading @@ -592,7 +552,9 @@ impl BigNum { impl Drop for BigNum { fn drop(&mut self) { unsafe { ffi::BN_clear_free(self.as_ptr()); } unsafe { ffi::BN_clear_free(self.as_ptr()); } } } Loading @@ -600,17 +562,13 @@ impl Deref for BigNum { type Target = BigNumRef; fn deref(&self) -> &BigNumRef { unsafe { BigNumRef::from_ptr(self.0) } unsafe { BigNumRef::from_ptr(self.0) } } } impl DerefMut for BigNum { fn deref_mut(&mut self) -> &mut BigNumRef { unsafe { BigNumRef::from_ptr_mut(self.0) } unsafe { BigNumRef::from_ptr_mut(self.0) } } } Loading
openssl/src/dh.rs +10 −19 Original line number Diff line number Diff line Loading @@ -50,33 +50,25 @@ impl Dh { /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))] pub fn get_1024_160() -> Result<Dh, ErrorStack> { unsafe { cvt_p(ffi::DH_get_1024_160()).map(Dh) } unsafe { cvt_p(ffi::DH_get_1024_160()).map(Dh) } } /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))] pub fn get_2048_224() -> Result<Dh, ErrorStack> { unsafe { cvt_p(ffi::DH_get_2048_224()).map(Dh) } unsafe { cvt_p(ffi::DH_get_2048_224()).map(Dh) } } /// Requires the `v102` or `v110` features and OpenSSL 1.0.2 or OpenSSL 1.1.0. #[cfg(any(all(feature = "v102", ossl102), all(feature = "v110", ossl110)))] pub fn get_2048_256() -> Result<Dh, ErrorStack> { unsafe { cvt_p(ffi::DH_get_2048_256()).map(Dh) } unsafe { cvt_p(ffi::DH_get_2048_256()).map(Dh) } } } impl Drop for Dh { fn drop(&mut self) { unsafe { ffi::DH_free(self.0) } unsafe { ffi::DH_free(self.0) } } } Loading @@ -84,9 +76,7 @@ impl Deref for Dh { type Target = DhRef; fn deref(&self) -> &DhRef { unsafe { DhRef::from_ptr(self.0) } unsafe { DhRef::from_ptr(self.0) } } } Loading @@ -104,7 +94,8 @@ mod compat { pub unsafe fn DH_set0_pqg(dh: *mut ffi::DH, p: *mut ffi::BIGNUM, q: *mut ffi::BIGNUM, g: *mut ffi::BIGNUM) -> c_int { g: *mut ffi::BIGNUM) -> c_int { (*dh).p = p; (*dh).q = q; (*dh).g = g; Loading
openssl/src/dsa.rs +12 −13 Original line number Diff line number Diff line Loading @@ -105,8 +105,7 @@ impl Dsa { } /// Writes an DSA private key as unencrypted PEM formatted data pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> { assert!(self.has_private_key()); let mem_bio = try!(MemBio::new()); Loading @@ -120,8 +119,7 @@ impl Dsa { } /// Reads an DSA public key from PEM formatted data. pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> { pub fn public_key_from_pem(buf: &[u8]) -> Result<Dsa, ErrorStack> { ffi::init(); let mem_bio = try!(MemBioSlice::new(buf)); Loading Loading @@ -258,7 +256,8 @@ mod test { password[4] = b's' as c_char; password[5] = b's' as c_char; 6 }).unwrap(); }) .unwrap(); assert!(password_queried); } Loading