diff options
| author | Steven Fackler <[email protected]> | 2014-09-27 21:04:02 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2014-09-27 21:04:02 -0700 |
| commit | 8b6bc824cc7c0b23338a35b69a14c416b59f1676 (patch) | |
| tree | 697246ee8baad66867aa6dc2d727593f0a1d421f /src | |
| parent | Merge pull request #46 from vhbit/tls1-2-support (diff) | |
| parent | Removed BN_sub_word signature for the time being. (diff) | |
| download | rust-openssl-8b6bc824cc7c0b23338a35b69a14c416b59f1676.tar.xz rust-openssl-8b6bc824cc7c0b23338a35b69a14c416b59f1676.zip | |
Merge pull request #55 from cjcole/master
Additions and one bug fix (BN_mod_inverse).
Diffstat (limited to 'src')
| -rw-r--r-- | src/bn/mod.rs | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/src/bn/mod.rs b/src/bn/mod.rs index e1ec1360..cc64d8cd 100644 --- a/src/bn/mod.rs +++ b/src/bn/mod.rs @@ -1,6 +1,8 @@ -use libc::{c_void, c_int, c_ulong}; -use std::ptr; +use libc::{c_void, c_int, c_ulong, c_char}; +use std::{fmt, ptr}; +use std::c_str::CString; +use std::num::{One, Zero}; use ssl::error::SslError; @@ -42,7 +44,7 @@ extern { fn BN_mod_sqr(r: *mut BIGNUM, a: *mut BIGNUM, m: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; fn BN_exp(r: *mut BIGNUM, a: *mut BIGNUM, p: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; fn BN_mod_exp(r: *mut BIGNUM, a: *mut BIGNUM, p: *mut BIGNUM, m: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; - fn BN_mod_inverse(r: *mut BIGNUM, a: *mut BIGNUM, n: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; + fn BN_mod_inverse(r: *mut BIGNUM, a: *mut BIGNUM, n: *mut BIGNUM, ctx: *mut BN_CTX) -> *const BIGNUM; fn BN_gcd(r: *mut BIGNUM, a: *mut BIGNUM, b: *mut BIGNUM, ctx: *mut BN_CTX) -> c_int; /* Bit operations on BIGNUMs */ @@ -58,6 +60,7 @@ extern { /* Comparisons on BIGNUMs */ fn BN_cmp(a: *mut BIGNUM, b: *mut BIGNUM) -> c_int; fn BN_ucmp(a: *mut BIGNUM, b: *mut BIGNUM) -> c_int; + fn BN_is_zero(a: *mut BIGNUM) -> c_int; /* Prime handling */ fn BN_generate_prime_ex(r: *mut BIGNUM, bits: c_int, safe: c_int, add: *mut BIGNUM, rem: *mut BIGNUM, cb: *const c_void) -> c_int; @@ -73,6 +76,10 @@ extern { /* Conversion from/to binary representation */ fn BN_bn2bin(a: *mut BIGNUM, to: *mut u8) -> c_int; fn BN_bin2bn(s: *const u8, size: c_int, ret: *mut BIGNUM) -> *mut BIGNUM; + + /* Conversion from/to string representation */ + fn BN_bn2dec(a: *mut BIGNUM) -> *const c_char; + fn CRYPTO_free(buf: *const c_char); } pub struct BigNum(*mut BIGNUM); @@ -221,7 +228,7 @@ impl BigNum { pub fn checked_mod_inv(&self, n: &BigNum) -> Result<BigNum, SslError> { unsafe { - with_bn_in_ctx!(r, ctx, { BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx) == 1 }) + with_bn_in_ctx!(r, ctx, { !BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx).is_null() }) } } @@ -421,6 +428,40 @@ impl BigNum { } v } + + pub fn to_dec_str(&self) -> String { + unsafe { + let buf = BN_bn2dec(self.raw()); + assert!(!buf.is_null()); + let c_str = CString::new(buf, false); + let str = c_str.as_str().unwrap().to_string(); + CRYPTO_free(buf); + str + } + } +} + +impl fmt::Show for BigNum { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.to_dec_str()) + } +} + +impl One for BigNum { + fn one() -> BigNum { + BigNum::new_from(1).unwrap() + } +} + +impl Zero for BigNum { + fn zero() -> BigNum { + BigNum::new_from(0).unwrap() + } + fn is_zero(&self) -> bool { + unsafe { + BN_is_zero(self.raw()) == 1 + } + } } impl Eq for BigNum { } |