diff options
| author | Steven Fackler <[email protected]> | 2016-08-07 14:19:44 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-08-07 14:33:18 -0700 |
| commit | 05089bacb3cde8fe3ec9f68b5682668d4f63383c (patch) | |
| tree | e2f7d2bb028e02504e1eecb62b4f91e2f83adb9c /openssl/src/crypto | |
| parent | Clean up asn1time (diff) | |
| download | rust-openssl-05089bacb3cde8fe3ec9f68b5682668d4f63383c.tar.xz rust-openssl-05089bacb3cde8fe3ec9f68b5682668d4f63383c.zip | |
Refactor BigNum
Diffstat (limited to 'openssl/src/crypto')
| -rw-r--r-- | openssl/src/crypto/dsa.rs | 18 | ||||
| -rw-r--r-- | openssl/src/crypto/pkey.rs | 11 | ||||
| -rw-r--r-- | openssl/src/crypto/rsa.rs | 50 |
3 files changed, 44 insertions, 35 deletions
diff --git a/openssl/src/crypto/dsa.rs b/openssl/src/crypto/dsa.rs index 40702627..84024379 100644 --- a/openssl/src/crypto/dsa.rs +++ b/openssl/src/crypto/dsa.rs @@ -4,7 +4,7 @@ use error::ErrorStack; use std::ptr; use libc::{c_uint, c_int, c_char, c_void}; -use bn::BigNum; +use bn::BigNumRef; use bio::{MemBio, MemBioSlice}; use crypto::hash; use crypto::HashTypeInternals; @@ -189,25 +189,27 @@ impl DSA { self.0 } - // The following getters are unsafe, since BigNum::new_from_ffi fails upon null pointers - pub fn p(&self) -> Result<BigNum, ErrorStack> { - unsafe { BigNum::new_from_ffi((*self.0).p) } + pub fn p<'a>(&'a self) -> BigNumRef<'a> { + assert!(self.has_p()); + unsafe { BigNumRef::from_handle((*self.0).p) } } pub fn has_p(&self) -> bool { unsafe { !(*self.0).p.is_null() } } - pub fn q(&self) -> Result<BigNum, ErrorStack> { - unsafe { BigNum::new_from_ffi((*self.0).q) } + pub fn q<'a>(&'a self) -> BigNumRef<'a> { + assert!(self.has_q()); + unsafe { BigNumRef::from_handle((*self.0).q) } } pub fn has_q(&self) -> bool { unsafe { !(*self.0).q.is_null() } } - pub fn g(&self) -> Result<BigNum, ErrorStack> { - unsafe { BigNum::new_from_ffi((*self.0).g) } + pub fn g<'a>(&'a self) -> BigNumRef<'a> { + assert!(self.has_g()); + unsafe { BigNumRef::from_handle((*self.0).g) } } pub fn has_g(&self) -> bool { diff --git a/openssl/src/crypto/pkey.rs b/openssl/src/crypto/pkey.rs index e554f3b2..9a3e140a 100644 --- a/openssl/src/crypto/pkey.rs +++ b/openssl/src/crypto/pkey.rs @@ -799,7 +799,7 @@ mod tests { let sig = k0.sign(&msg); let r0 = k0.get_rsa(); - let r1 = RSA::from_public_components(r0.n().expect("n"), r0.e().expect("e")).expect("r1"); + let r1 = RSA::from_public_components(r0.n().to_owned().unwrap(), r0.e().to_owned().unwrap()).expect("r1"); k1.set_rsa(&r1); assert!(k1.can(super::Role::Encrypt)); @@ -847,12 +847,13 @@ mod tests { fn test_pkey_clone_creates_copy() { let mut pkey = super::PKey::new(); pkey.gen(512); - let old_pkey_n = pkey.get_rsa().n().unwrap(); + let rsa = pkey.get_rsa(); + let old_pkey_n = rsa.n(); let mut pkey2 = pkey.clone(); pkey2.gen(512); - assert!(old_pkey_n == pkey.get_rsa().n().unwrap()); + assert!(old_pkey_n == rsa.n()); } #[test] @@ -862,7 +863,7 @@ mod tests { let pkey2 = pkey.clone(); - assert!(pkey.get_rsa().q().unwrap() == pkey2.get_rsa().q().unwrap()); + assert!(pkey.get_rsa().q() == pkey2.get_rsa().q()); } #[test] @@ -874,6 +875,6 @@ mod tests { let pub_key2 = pub_key.clone(); - assert!(pub_key.get_rsa().n().unwrap() == pub_key2.get_rsa().n().unwrap()); + assert!(pub_key.get_rsa().n() == pub_key2.get_rsa().n()); } } diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs index 5c70c8ea..73239731 100644 --- a/openssl/src/crypto/rsa.rs +++ b/openssl/src/crypto/rsa.rs @@ -3,7 +3,7 @@ use std::fmt; use std::ptr; use libc::{c_int, c_void, c_char}; -use bn::BigNum; +use bn::{BigNum, BigNumRef}; use bio::{MemBio, MemBioSlice}; use error::ErrorStack; use crypto::HashTypeInternals; @@ -171,43 +171,49 @@ impl RSA { self.0 } - // The following getters are unsafe, since BigNum::new_from_ffi fails upon null pointers - pub fn n(&self) -> Result<BigNum, ErrorStack> { - unsafe { - BigNum::new_from_ffi((*self.0).n) - } + pub fn n<'a>(&'a self) -> BigNumRef<'a> { + assert!(self.has_n()); + unsafe { BigNumRef::from_handle((*self.0).n) } } pub fn has_n(&self) -> bool { unsafe { !(*self.0).n.is_null() } } - pub fn d(&self) -> Result<BigNum, ErrorStack> { - unsafe { - BigNum::new_from_ffi((*self.0).d) - } + pub fn d<'a>(&self) -> BigNumRef<'a> { + assert!(self.has_d()); + unsafe { BigNumRef::from_handle((*self.0).d) } } - pub fn e(&self) -> Result<BigNum, ErrorStack> { - unsafe { - BigNum::new_from_ffi((*self.0).e) - } + pub fn has_d(&self) -> bool { + unsafe { !(*self.0).d.is_null() } + } + + pub fn e<'a>(&'a self) -> BigNumRef<'a> { + assert!(self.has_e()); + unsafe { BigNumRef::from_handle((*self.0).e) } } pub fn has_e(&self) -> bool { unsafe { !(*self.0).e.is_null() } } - pub fn p(&self) -> Result<BigNum, ErrorStack> { - unsafe { - BigNum::new_from_ffi((*self.0).p) - } + pub fn p<'a>(&'a self) -> BigNumRef<'a> { + assert!(self.has_p()); + unsafe { BigNumRef::from_handle((*self.0).p) } } - pub fn q(&self) -> Result<BigNum, ErrorStack> { - unsafe { - BigNum::new_from_ffi((*self.0).q) - } + pub fn has_p(&self) -> bool { + unsafe { !(*self.0).p.is_null() } + } + + pub fn q<'a>(&'a self) -> BigNumRef<'a> { + assert!(self.has_q()); + unsafe { BigNumRef::from_handle((*self.0).q) } + } + + pub fn has_q(&self) -> bool { + unsafe { !(*self.0).q.is_null() } } } |