aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--openssl-sys/src/lib.rs15
-rw-r--r--src/bn/mod.rs35
2 files changed, 49 insertions, 1 deletions
diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs
index 1fdd5580..df38215d 100644
--- a/openssl-sys/src/lib.rs
+++ b/openssl-sys/src/lib.rs
@@ -88,6 +88,13 @@ 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;
@@ -268,6 +275,7 @@ 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_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;
@@ -300,9 +308,14 @@ extern "C" {
pub fn BN_bin2bn(s: *const u8, size: c_int, ret: *mut BIGNUM) -> *mut BIGNUM;
pub fn BN_bn2bin(a: *mut BIGNUM, to: *mut u8) -> c_int;
- /* Conversion from/to string representation */
+ /* Conversion from/to decimal string representation */
+ pub fn BN_dec2bn(a: *mut BIGNUM_PTR, 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_bn2hex(a: *mut BIGNUM) -> *const c_char;
+
pub fn CRYPTO_num_locks() -> c_int;
pub fn CRYPTO_set_locking_callback(func: extern "C" fn(mode: c_int,
n: c_int,
diff --git a/src/bn/mod.rs b/src/bn/mod.rs
index bcf6c104..a4d23302 100644
--- a/src/bn/mod.rs
+++ b/src/bn/mod.rs
@@ -85,6 +85,24 @@ impl BigNum {
})
}
+ pub fn from_dec_str(s: &str) -> Result<BigNum, SslError> {
+ 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()));
+ Ok(v)
+ })
+ }
+
+ pub fn from_hex_str(s: &str) -> Result<BigNum, SslError> {
+ 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()));
+ Ok(v)
+ })
+ }
+
pub fn new_from_slice(n: &[u8]) -> Result<BigNum, SslError> {
BigNum::new().and_then(|v| unsafe {
try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw()));
@@ -146,6 +164,12 @@ impl BigNum {
}
}
+ pub fn mod_word(&self, w: c_ulong) -> c_ulong {
+ unsafe {
+ ffi::BN_mod_word(self.raw(), w)
+ }
+ }
+
pub fn checked_gcd(&self, a: &BigNum) -> Result<BigNum, SslError> {
unsafe {
with_bn_in_ctx!(r, ctx, { ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1 })
@@ -353,6 +377,17 @@ impl BigNum {
str
}
}
+
+ pub fn to_hex_str(&self) -> String {
+ unsafe {
+ let buf = ffi::BN_bn2hex(self.raw());
+ assert!(!buf.is_null());
+ let c_str = CString::new(buf, false);
+ let str = c_str.as_str().unwrap().to_string();
+ ffi::CRYPTO_free(buf as *mut c_void);
+ str
+ }
+ }
}
impl fmt::Show for BigNum {