From 2e2fde4b1a868050245b185b26a12187c379ec0d Mon Sep 17 00:00:00 2001 From: Chris Cole Date: Fri, 2 Jan 2015 18:47:29 -0500 Subject: [PATCH] Added BN_add_word, BN_sub_word, BN_mul_word, BN_div_word. Removed BIGNUM_PTR struct. --- openssl-sys/src/lib.rs | 17 +++++------- src/bn/mod.rs | 61 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index df38215d..7471616f 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -88,13 +88,6 @@ pub struct BIGNUM { impl Copy for BIGNUM {} -#[repr(C)] -pub struct BIGNUM_PTR { - pub ptr: *mut BIGNUM, -} - -impl Copy for BIGNUM_PTR {} - pub type CRYPTO_EX_new = extern "C" fn(parent: *mut c_void, ptr: *mut c_void, ad: *const CRYPTO_EX_DATA, idx: c_int, argl: c_long, argp: *const c_void) -> c_int; @@ -275,7 +268,11 @@ extern "C" { pub fn BN_mod_sub(r: *mut BIGNUM, a: *mut BIGNUM, b: *mut BIGNUM, m: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; pub fn BN_mul(r: *mut BIGNUM, a: *mut BIGNUM, b: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; pub fn BN_nnmod(rem: *mut BIGNUM, a: *mut BIGNUM, m: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; - pub fn BN_mod_word(r: *mut BIGNUM, w: c_ulong) -> c_ulong; + pub fn BN_add_word(r: *mut BIGNUM, w: c_ulong) -> c_int; + pub fn BN_sub_word(r: *mut BIGNUM, w: c_ulong) -> c_int; + pub fn BN_mul_word(r: *mut BIGNUM, w: c_ulong) -> c_int; + pub fn BN_div_word(r: *mut BIGNUM, w: c_ulong) -> c_ulong; + pub fn BN_mod_word(r: *const BIGNUM, w: c_ulong) -> c_ulong; pub fn BN_sqr(r: *mut BIGNUM, a: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; pub fn BN_sub(r: *mut BIGNUM, a: *mut BIGNUM, b: *mut BIGNUM) -> c_int; @@ -309,11 +306,11 @@ extern "C" { pub fn BN_bn2bin(a: *mut BIGNUM, to: *mut u8) -> c_int; /* Conversion from/to decimal string representation */ - pub fn BN_dec2bn(a: *mut BIGNUM_PTR, s: *const i8) -> c_int; + pub fn BN_dec2bn(a: *const *mut BIGNUM, s: *const i8) -> c_int; pub fn BN_bn2dec(a: *mut BIGNUM) -> *const c_char; /* Conversion from/to hexidecimal string representation */ - pub fn BN_hex2bn(a: *mut BIGNUM_PTR, s: *const i8) -> c_int; + pub fn BN_hex2bn(a: *const *mut BIGNUM, s: *const i8) -> c_int; pub fn BN_bn2hex(a: *mut BIGNUM) -> *const c_char; pub fn CRYPTO_num_locks() -> c_int; diff --git a/src/bn/mod.rs b/src/bn/mod.rs index a4d23302..137adc43 100644 --- a/src/bn/mod.rs +++ b/src/bn/mod.rs @@ -87,18 +87,16 @@ impl BigNum { pub fn from_dec_str(s: &str) -> Result { BigNum::new().and_then(|v| unsafe { - let ref mut bn_ptr = ffi::BIGNUM_PTR { ptr: v.raw(), }; let c_str = s.to_c_str(); - try_ssl!(ffi::BN_dec2bn(bn_ptr, c_str.as_ptr())); + try_ssl!(ffi::BN_dec2bn(v.raw_ptr(), c_str.as_ptr())); Ok(v) }) } pub fn from_hex_str(s: &str) -> Result { BigNum::new().and_then(|v| unsafe { - let ref mut bn_ptr = ffi::BIGNUM_PTR { ptr: v.raw(), }; let c_str = s.to_c_str(); - try_ssl!(ffi::BN_hex2bn(bn_ptr, c_str.as_ptr())); + try_ssl!(ffi::BN_hex2bn(v.raw_ptr(), c_str.as_ptr())); Ok(v) }) } @@ -164,9 +162,55 @@ impl BigNum { } } - pub fn mod_word(&self, w: c_ulong) -> c_ulong { + pub fn add_word(&mut self, w: c_ulong) -> Result<(), SslError> { unsafe { - ffi::BN_mod_word(self.raw(), w) + if ffi::BN_add_word(self.raw(), w) == 1 { + Ok(()) + } else { + Err(SslError::get()) + } + } + } + + pub fn sub_word(&mut self, w: c_ulong) -> Result<(), SslError> { + unsafe { + if ffi::BN_sub_word(self.raw(), w) == 1 { + Ok(()) + } else { + Err(SslError::get()) + } + } + } + + pub fn mul_word(&mut self, w: c_ulong) -> Result<(), SslError> { + unsafe { + if ffi::BN_mul_word(self.raw(), w) == 1 { + Ok(()) + } else { + Err(SslError::get()) + } + } + } + + pub fn div_word(&mut self, w: c_ulong) -> Result { + unsafe { + let result = ffi::BN_div_word(self.raw(), w); + if result != -1 as c_ulong { + Ok(result) + } else { + Err(SslError::get()) + } + } + } + + pub fn mod_word(&self, w: c_ulong) -> Result { + unsafe { + let result = ffi::BN_mod_word(self.raw(), w); + if result != -1 as c_ulong { + Ok(result) + } else { + Err(SslError::get()) + } } } @@ -357,6 +401,11 @@ impl BigNum { n } + unsafe fn raw_ptr(&self) -> *const *mut ffi::BIGNUM { + let BigNum(ref n) = *self; + n + } + pub fn to_vec(&self) -> Vec { let size = self.num_bytes() as uint; let mut v = Vec::with_capacity(size);