Update bignum

This commit is contained in:
Steven Fackler 2016-10-31 19:45:52 -07:00
parent 927c3e924c
commit 3363046c34
3 changed files with 114 additions and 159 deletions

View File

@ -3,12 +3,12 @@ use libc::c_int;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::ffi::CString; use std::ffi::CString;
use std::{fmt, ptr}; use std::{fmt, ptr};
use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub, Deref, DerefMut}; use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub, Deref};
use {cvt, cvt_p, cvt_n}; use {cvt, cvt_p, cvt_n};
use crypto::CryptoString; use crypto::CryptoString;
use error::ErrorStack; use error::ErrorStack;
use opaque::Opaque; use types::{Ref, OpenSslType};
/// Specifies the desired properties of a randomly generated `BigNum`. /// Specifies the desired properties of a randomly generated `BigNum`.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -33,19 +33,19 @@ impl BnCtx {
/// Places the result of `a * b` in `r`. /// Places the result of `a * b` in `r`.
pub fn mul(&mut self, pub fn mul(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
b: &BigNumRef) b: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> 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`. /// Places the result of `a / b` in `dv` and `a mod b` in `rem`.
pub fn div(&mut self, pub fn div(&mut self,
dv: Option<&mut BigNumRef>, dv: Option<&mut Ref<BigNum>>,
rem: Option<&mut BigNumRef>, rem: Option<&mut Ref<BigNum>>,
a: &BigNumRef, a: &Ref<BigNum>,
b: &BigNumRef) b: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { unsafe {
cvt(ffi::BN_div(dv.map(|b| b.as_ptr()).unwrap_or(ptr::null_mut()), cvt(ffi::BN_div(dv.map(|b| b.as_ptr()).unwrap_or(ptr::null_mut()),
@ -58,25 +58,25 @@ impl BnCtx {
} }
/// Places the result of `a²` in `r`. /// Places the result of `a²` in `r`.
pub fn sqr(&mut self, r: &mut BigNumRef, a: &BigNumRef) -> Result<(), ErrorStack> { pub fn sqr(&mut self, r: &mut Ref<BigNum>, a: &Ref<BigNum>) -> 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`. /// Places the result of `a mod m` in `r`.
pub fn nnmod(&mut self, pub fn nnmod(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
m: &BigNumRef) m: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { cvt(ffi::BN_nnmod(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } 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`. /// Places the result of `(a + b) mod m` in `r`.
pub fn mod_add(&mut self, pub fn mod_add(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
b: &BigNumRef, b: &Ref<BigNum>,
m: &BigNumRef) m: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { unsafe {
cvt(ffi::BN_mod_add(r.as_ptr(), a.as_ptr(), b.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) cvt(ffi::BN_mod_add(r.as_ptr(), a.as_ptr(), b.as_ptr(), m.as_ptr(), self.0)).map(|_| ())
@ -85,10 +85,10 @@ impl BnCtx {
/// Places the result of `(a - b) mod m` in `r`. /// Places the result of `(a - b) mod m` in `r`.
pub fn mod_sub(&mut self, pub fn mod_sub(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
b: &BigNumRef, b: &Ref<BigNum>,
m: &BigNumRef) m: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { unsafe {
cvt(ffi::BN_mod_sub(r.as_ptr(), a.as_ptr(), b.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) cvt(ffi::BN_mod_sub(r.as_ptr(), a.as_ptr(), b.as_ptr(), m.as_ptr(), self.0)).map(|_| ())
@ -97,10 +97,10 @@ impl BnCtx {
/// Places the result of `(a * b) mod m` in `r`. /// Places the result of `(a * b) mod m` in `r`.
pub fn mod_mul(&mut self, pub fn mod_mul(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
b: &BigNumRef, b: &Ref<BigNum>,
m: &BigNumRef) m: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { unsafe {
cvt(ffi::BN_mod_mul(r.as_ptr(), a.as_ptr(), b.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) cvt(ffi::BN_mod_mul(r.as_ptr(), a.as_ptr(), b.as_ptr(), m.as_ptr(), self.0)).map(|_| ())
@ -109,28 +109,28 @@ impl BnCtx {
/// Places the result of `a² mod m` in `r`. /// Places the result of `a² mod m` in `r`.
pub fn mod_sqr(&mut self, pub fn mod_sqr(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
m: &BigNumRef) m: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { cvt(ffi::BN_mod_sqr(r.as_ptr(), a.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) } 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`. /// Places the result of `a^p` in `r`.
pub fn exp(&mut self, pub fn exp(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
p: &BigNumRef) p: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { cvt(ffi::BN_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), self.0)).map(|_| ()) } 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`. /// Places the result of `a^p mod m` in `r`.
pub fn mod_exp(&mut self, pub fn mod_exp(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
p: &BigNumRef, p: &Ref<BigNum>,
m: &BigNumRef) m: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { unsafe {
cvt(ffi::BN_mod_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), m.as_ptr(), self.0)).map(|_| ()) cvt(ffi::BN_mod_exp(r.as_ptr(), a.as_ptr(), p.as_ptr(), m.as_ptr(), self.0)).map(|_| ())
@ -139,9 +139,9 @@ impl BnCtx {
/// Places the inverse of `a` modulo `n` in `r`. /// Places the inverse of `a` modulo `n` in `r`.
pub fn mod_inverse(&mut self, pub fn mod_inverse(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
n: &BigNumRef) n: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { unsafe {
cvt_p(ffi::BN_mod_inverse(r.as_ptr(), a.as_ptr(), n.as_ptr(), self.as_ptr())) cvt_p(ffi::BN_mod_inverse(r.as_ptr(), a.as_ptr(), n.as_ptr(), self.as_ptr()))
@ -151,9 +151,9 @@ impl BnCtx {
/// Places the greatest common denominator of `a` and `b` in `r`. /// Places the greatest common denominator of `a` and `b` in `r`.
pub fn gcd(&mut self, pub fn gcd(&mut self,
r: &mut BigNumRef, r: &mut Ref<BigNum>,
a: &BigNumRef, a: &Ref<BigNum>,
b: &BigNumRef) b: &Ref<BigNum>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { cvt(ffi::BN_gcd(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) } unsafe { cvt(ffi::BN_gcd(r.as_ptr(), a.as_ptr(), b.as_ptr(), self.as_ptr())).map(|_| ()) }
} }
@ -163,7 +163,7 @@ impl BnCtx {
/// Performs a Miller-Rabin probabilistic primality test with `checks` iterations. /// Performs a Miller-Rabin probabilistic primality test with `checks` iterations.
/// ///
/// Returns `true` if `p` is prime with an error probability of less than `0.25 ^ checks`. /// 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> { pub fn is_prime(&mut self, p: &Ref<BigNum>, checks: i32) -> Result<bool, ErrorStack> {
unsafe { unsafe {
cvt_n(ffi::BN_is_prime_ex(p.as_ptr(), checks.into(), self.as_ptr(), ptr::null_mut())) cvt_n(ffi::BN_is_prime_ex(p.as_ptr(), checks.into(), self.as_ptr(), ptr::null_mut()))
.map(|r| r != 0) .map(|r| r != 0)
@ -180,7 +180,7 @@ impl BnCtx {
/// ///
/// Returns `true` if `p` is prime with an error probability of less than `0.25 ^ checks`. /// Returns `true` if `p` is prime with an error probability of less than `0.25 ^ checks`.
pub fn is_prime_fasttest(&mut self, pub fn is_prime_fasttest(&mut self,
p: &BigNumRef, p: &Ref<BigNum>,
checks: i32, checks: i32,
do_trial_division: bool) do_trial_division: bool)
-> Result<bool, ErrorStack> { -> Result<bool, ErrorStack> {
@ -201,7 +201,7 @@ impl BnCtx {
/// * `bits`: Length of the number in bits. /// * `bits`: Length of the number in bits.
/// * `prop`: The desired properties of the number. /// * `prop`: The desired properties of the number.
/// * `odd`: If `true`, the generated number will be odd. /// * `odd`: If `true`, the generated number will be odd.
pub fn rand(r: &mut BigNumRef, pub fn rand(r: &mut Ref<BigNum>,
bits: i32, bits: i32,
prop: RNGProperty, prop: RNGProperty,
odd: bool) odd: bool)
@ -212,7 +212,7 @@ impl BnCtx {
} }
/// The cryptographically weak counterpart to `checked_new_random`. /// The cryptographically weak counterpart to `checked_new_random`.
pub fn pseudo_rand(r: &mut BigNumRef, pub fn pseudo_rand(r: &mut Ref<BigNum>,
bits: i32, bits: i32,
prop: RNGProperty, prop: RNGProperty,
odd: bool) odd: bool)
@ -224,22 +224,7 @@ impl BnCtx {
} }
} }
/// A borrowed, signed, arbitrary-precision integer. impl Ref<BigNum> {
pub struct BigNumRef(Opaque);
impl BigNumRef {
pub unsafe fn from_ptr<'a>(handle: *mut ffi::BIGNUM) -> &'a BigNumRef {
&*(handle as *mut _)
}
pub unsafe fn from_ptr_mut<'a>(handle: *mut ffi::BIGNUM) -> &'a mut BigNumRef {
&mut *(handle as *mut _)
}
pub fn as_ptr(&self) -> *mut ffi::BIGNUM {
self as *const _ as *mut _
}
/// Adds a `u32` to `self`. /// Adds a `u32` to `self`.
pub fn add_word(&mut self, w: u32) -> Result<(), ErrorStack> { 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(|_| ()) }
@ -281,12 +266,12 @@ impl BigNumRef {
/// Places a cryptographically-secure pseudo-random number nonnegative /// Places a cryptographically-secure pseudo-random number nonnegative
/// number less than `self` in `rnd`. /// number less than `self` in `rnd`.
pub fn rand_in_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> { pub fn rand_in_range(&self, rnd: &mut Ref<BigNum>) -> 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`. /// The cryptographically weak counterpart to `rand_in_range`.
pub fn pseudo_rand_in_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack> { pub fn pseudo_rand_in_range(&self, rnd: &mut Ref<BigNum>) -> 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(|_| ()) }
} }
@ -317,32 +302,32 @@ impl BigNumRef {
} }
/// Places `self << 1` in `r`. /// Places `self << 1` in `r`.
pub fn lshift1(&self, r: &mut BigNumRef) -> Result<(), ErrorStack> { pub fn lshift1(&self, r: &mut Ref<BigNum>) -> 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`. /// Places `self >> 1` in `r`.
pub fn rshift1(&self, r: &mut BigNumRef) -> Result<(), ErrorStack> { pub fn rshift1(&self, r: &mut Ref<BigNum>) -> 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`. /// Places `self + b` in `r`.
pub fn add(&self, r: &mut BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { pub fn add(&self, r: &mut Ref<BigNum>, b: &Ref<BigNum>) -> 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`. /// Places `self - b` in `r`.
pub fn sub(&self, r: &mut BigNumRef, b: &BigNumRef) -> Result<(), ErrorStack> { pub fn sub(&self, r: &mut Ref<BigNum>, b: &Ref<BigNum>) -> 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`. /// Places `self << n` in `r`.
pub fn lshift(&self, r: &mut BigNumRef, b: i32) -> Result<(), ErrorStack> { pub fn lshift(&self, r: &mut Ref<BigNum>, 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`. /// Places `self >> n` in `r`.
pub fn rshift(&self, r: &mut BigNumRef, n: i32) -> Result<(), ErrorStack> { pub fn rshift(&self, r: &mut Ref<BigNum>, 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(|_| ()) }
} }
@ -365,7 +350,7 @@ impl BigNumRef {
/// ///
/// assert_eq!(s.ucmp(&o), Ordering::Equal); /// assert_eq!(s.ucmp(&o), Ordering::Equal);
/// ``` /// ```
pub fn ucmp(&self, oth: &BigNumRef) -> Ordering { pub fn ucmp(&self, oth: &Ref<BigNum>) -> 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) }
} }
@ -446,13 +431,7 @@ impl BigNumRef {
} }
} }
/// An owned, signed, arbitrary-precision integer. type_!(BigNum, ffi::BIGNUM, ffi::BN_clear_free);
///
/// `BigNum` provides wrappers around OpenSSL's checked arithmetic functions.
/// Additionally, it implements the standard operators (`std::ops`), which
/// perform unchecked arithmetic, unwrapping the returned `Result` of the
/// checked operations.
pub struct BigNum(*mut ffi::BIGNUM);
impl BigNum { impl BigNum {
/// Creates a new `BigNum` with the value 0. /// Creates a new `BigNum` with the value 0.
@ -491,10 +470,6 @@ impl BigNum {
} }
} }
pub unsafe fn from_ptr(handle: *mut ffi::BIGNUM) -> BigNum {
BigNum(handle)
}
/// Creates a new `BigNum` from an unsigned, big-endian encoded number of arbitrary length. /// Creates a new `BigNum` from an unsigned, big-endian encoded number of arbitrary length.
/// ///
/// ``` /// ```
@ -519,11 +494,11 @@ impl BigNum {
/// * `safe`: If true, returns a "safe" prime `p` so that `(p-1)/2` is also prime. /// * `safe`: If true, returns a "safe" prime `p` so that `(p-1)/2` is also prime.
/// * `add`/`rem`: If `add` is set to `Some(add)`, `p % add == rem` will hold, where `p` is the /// * `add`/`rem`: If `add` is set to `Some(add)`, `p % add == rem` will hold, where `p` is the
/// generated prime and `rem` is `1` if not specified (`None`). /// generated prime and `rem` is `1` if not specified (`None`).
pub fn generate_prime(r: &mut BigNumRef, pub fn generate_prime(r: &mut Ref<BigNum>,
bits: i32, bits: i32,
safe: bool, safe: bool,
add: Option<&BigNumRef>, add: Option<&Ref<BigNum>>,
rem: Option<&BigNumRef>) rem: Option<&Ref<BigNum>>)
-> Result<(), ErrorStack> { -> Result<(), ErrorStack> {
unsafe { unsafe {
cvt(ffi::BN_generate_prime_ex(r.as_ptr(), cvt(ffi::BN_generate_prime_ex(r.as_ptr(),
@ -537,35 +512,13 @@ impl BigNum {
} }
} }
impl Drop for BigNum { impl AsRef<Ref<BigNum>> for BigNum {
fn drop(&mut self) { fn as_ref(&self) -> &Ref<BigNum> {
unsafe {
ffi::BN_clear_free(self.as_ptr());
}
}
}
impl Deref for BigNum {
type Target = BigNumRef;
fn deref(&self) -> &BigNumRef {
unsafe { BigNumRef::from_ptr(self.0) }
}
}
impl DerefMut for BigNum {
fn deref_mut(&mut self) -> &mut BigNumRef {
unsafe { BigNumRef::from_ptr_mut(self.0) }
}
}
impl AsRef<BigNumRef> for BigNum {
fn as_ref(&self) -> &BigNumRef {
self.deref() self.deref()
} }
} }
impl fmt::Debug for BigNumRef { impl fmt::Debug for Ref<BigNum> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.to_dec_str() { match self.to_dec_str() {
Ok(s) => f.write_str(&s), Ok(s) => f.write_str(&s),
@ -583,7 +536,7 @@ impl fmt::Debug for BigNum {
} }
} }
impl fmt::Display for BigNumRef { impl fmt::Display for Ref<BigNum> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.to_dec_str() { match self.to_dec_str() {
Ok(s) => f.write_str(&s), Ok(s) => f.write_str(&s),
@ -601,19 +554,19 @@ impl fmt::Display for BigNum {
} }
} }
impl PartialEq<BigNumRef> for BigNumRef { impl PartialEq<Ref<BigNum>> for Ref<BigNum> {
fn eq(&self, oth: &BigNumRef) -> bool { fn eq(&self, oth: &Ref<BigNum>) -> bool {
self.cmp(oth) == Ordering::Equal self.cmp(oth) == Ordering::Equal
} }
} }
impl PartialEq<BigNum> for BigNumRef { impl PartialEq<BigNum> for Ref<BigNum> {
fn eq(&self, oth: &BigNum) -> bool { fn eq(&self, oth: &BigNum) -> bool {
self.eq(oth.deref()) self.eq(oth.deref())
} }
} }
impl Eq for BigNumRef {} impl Eq for Ref<BigNum> {}
impl PartialEq for BigNum { impl PartialEq for BigNum {
fn eq(&self, oth: &BigNum) -> bool { fn eq(&self, oth: &BigNum) -> bool {
@ -621,28 +574,28 @@ impl PartialEq for BigNum {
} }
} }
impl PartialEq<BigNumRef> for BigNum { impl PartialEq<Ref<BigNum>> for BigNum {
fn eq(&self, oth: &BigNumRef) -> bool { fn eq(&self, oth: &Ref<BigNum>) -> bool {
self.deref().eq(oth) self.deref().eq(oth)
} }
} }
impl Eq for BigNum {} impl Eq for BigNum {}
impl PartialOrd<BigNumRef> for BigNumRef { impl PartialOrd<Ref<BigNum>> for Ref<BigNum> {
fn partial_cmp(&self, oth: &BigNumRef) -> Option<Ordering> { fn partial_cmp(&self, oth: &Ref<BigNum>) -> Option<Ordering> {
Some(self.cmp(oth)) Some(self.cmp(oth))
} }
} }
impl PartialOrd<BigNum> for BigNumRef { impl PartialOrd<BigNum> for Ref<BigNum> {
fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> { fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> {
Some(self.cmp(oth.deref())) Some(self.cmp(oth.deref()))
} }
} }
impl Ord for BigNumRef { impl Ord for Ref<BigNum> {
fn cmp(&self, oth: &BigNumRef) -> Ordering { fn cmp(&self, oth: &Ref<BigNum>) -> Ordering {
unsafe { ffi::BN_cmp(self.as_ptr(), oth.as_ptr()).cmp(&0) } unsafe { ffi::BN_cmp(self.as_ptr(), oth.as_ptr()).cmp(&0) }
} }
} }
@ -653,8 +606,8 @@ impl PartialOrd for BigNum {
} }
} }
impl PartialOrd<BigNumRef> for BigNum { impl PartialOrd<Ref<BigNum>> for BigNum {
fn partial_cmp(&self, oth: &BigNumRef) -> Option<Ordering> { fn partial_cmp(&self, oth: &Ref<BigNum>) -> Option<Ordering> {
self.deref().partial_cmp(oth) self.deref().partial_cmp(oth)
} }
} }
@ -667,7 +620,7 @@ impl Ord for BigNum {
macro_rules! delegate { macro_rules! delegate {
($t:ident, $m:ident) => { ($t:ident, $m:ident) => {
impl<'a, 'b> $t<&'b BigNum> for &'a BigNumRef { impl<'a, 'b> $t<&'b BigNum> for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn $m(self, oth: &BigNum) -> BigNum { fn $m(self, oth: &BigNum) -> BigNum {
@ -675,10 +628,10 @@ macro_rules! delegate {
} }
} }
impl<'a, 'b> $t<&'b BigNumRef> for &'a BigNum { impl<'a, 'b> $t<&'b Ref<BigNum>> for &'a BigNum {
type Output = BigNum; type Output = BigNum;
fn $m(self, oth: &BigNumRef) -> BigNum { fn $m(self, oth: &Ref<BigNum>) -> BigNum {
$t::$m(self.deref(), oth) $t::$m(self.deref(), oth)
} }
} }
@ -693,10 +646,10 @@ macro_rules! delegate {
} }
} }
impl<'a, 'b> Add<&'b BigNumRef> for &'a BigNumRef { impl<'a, 'b> Add<&'b Ref<BigNum>> for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn add(self, oth: &BigNumRef) -> BigNum { fn add(self, oth: &Ref<BigNum>) -> BigNum {
let mut r = BigNum::new().unwrap(); let mut r = BigNum::new().unwrap();
self.add(&mut r, oth).unwrap(); self.add(&mut r, oth).unwrap();
r r
@ -705,10 +658,10 @@ impl<'a, 'b> Add<&'b BigNumRef> for &'a BigNumRef {
delegate!(Add, add); delegate!(Add, add);
impl<'a, 'b> Sub<&'b BigNumRef> for &'a BigNumRef { impl<'a, 'b> Sub<&'b Ref<BigNum>> for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn sub(self, oth: &BigNumRef) -> BigNum { fn sub(self, oth: &Ref<BigNum>) -> BigNum {
let mut r = BigNum::new().unwrap(); let mut r = BigNum::new().unwrap();
self.sub(&mut r, oth).unwrap(); self.sub(&mut r, oth).unwrap();
r r
@ -717,10 +670,10 @@ impl<'a, 'b> Sub<&'b BigNumRef> for &'a BigNumRef {
delegate!(Sub, sub); delegate!(Sub, sub);
impl<'a, 'b> Mul<&'b BigNumRef> for &'a BigNumRef { impl<'a, 'b> Mul<&'b Ref<BigNum>> for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn mul(self, oth: &BigNumRef) -> BigNum { fn mul(self, oth: &Ref<BigNum>) -> BigNum {
let mut ctx = BnCtx::new().unwrap(); let mut ctx = BnCtx::new().unwrap();
let mut r = BigNum::new().unwrap(); let mut r = BigNum::new().unwrap();
ctx.mul(&mut r, self, oth).unwrap(); ctx.mul(&mut r, self, oth).unwrap();
@ -730,10 +683,10 @@ impl<'a, 'b> Mul<&'b BigNumRef> for &'a BigNumRef {
delegate!(Mul, mul); delegate!(Mul, mul);
impl<'a, 'b> Div<&'b BigNumRef> for &'a BigNumRef { impl<'a, 'b> Div<&'b Ref<BigNum>> for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn div(self, oth: &'b BigNumRef) -> BigNum { fn div(self, oth: &'b Ref<BigNum>) -> BigNum {
let mut ctx = BnCtx::new().unwrap(); let mut ctx = BnCtx::new().unwrap();
let mut dv = BigNum::new().unwrap(); let mut dv = BigNum::new().unwrap();
ctx.div(Some(&mut dv), None, self, oth).unwrap(); ctx.div(Some(&mut dv), None, self, oth).unwrap();
@ -743,10 +696,10 @@ impl<'a, 'b> Div<&'b BigNumRef> for &'a BigNumRef {
delegate!(Div, div); delegate!(Div, div);
impl<'a, 'b> Rem<&'b BigNumRef> for &'a BigNumRef { impl<'a, 'b> Rem<&'b Ref<BigNum>> for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn rem(self, oth: &'b BigNumRef) -> BigNum { fn rem(self, oth: &'b Ref<BigNum>) -> BigNum {
let mut ctx = BnCtx::new().unwrap(); let mut ctx = BnCtx::new().unwrap();
let mut rem = BigNum::new().unwrap(); let mut rem = BigNum::new().unwrap();
ctx.div(None, Some(&mut rem), self, oth).unwrap(); ctx.div(None, Some(&mut rem), self, oth).unwrap();
@ -756,7 +709,7 @@ impl<'a, 'b> Rem<&'b BigNumRef> for &'a BigNumRef {
delegate!(Rem, rem); delegate!(Rem, rem);
impl<'a> Shl<i32> for &'a BigNumRef { impl<'a> Shl<i32> for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn shl(self, n: i32) -> BigNum { fn shl(self, n: i32) -> BigNum {
@ -774,7 +727,7 @@ impl<'a> Shl<i32> for &'a BigNum {
} }
} }
impl<'a> Shr<i32> for &'a BigNumRef { impl<'a> Shr<i32> for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn shr(self, n: i32) -> BigNum { fn shr(self, n: i32) -> BigNum {
@ -792,7 +745,7 @@ impl<'a> Shr<i32> for &'a BigNum {
} }
} }
impl<'a> Neg for &'a BigNumRef { impl<'a> Neg for &'a Ref<BigNum> {
type Output = BigNum; type Output = BigNum;
fn neg(self) -> BigNum { fn neg(self) -> BigNum {

View File

@ -5,11 +5,12 @@ use std::fmt;
use std::ops::Deref; use std::ops::Deref;
use std::ptr; use std::ptr;
use {cvt, cvt_p};
use bn::BigNumRef;
use bio::{MemBio, MemBioSlice}; use bio::{MemBio, MemBioSlice};
use util::{CallbackState, invoke_passwd_cb}; use bn::BigNum;
use {cvt, cvt_p};
use opaque::Opaque; use opaque::Opaque;
use types::Ref;
use util::{CallbackState, invoke_passwd_cb};
pub struct DsaRef(Opaque); pub struct DsaRef(Opaque);
@ -53,35 +54,35 @@ impl DsaRef {
} }
} }
pub fn p(&self) -> Option<&BigNumRef> { pub fn p(&self) -> Option<&Ref<BigNum>> {
unsafe { unsafe {
let p = compat::pqg(self.as_ptr())[0]; let p = compat::pqg(self.as_ptr())[0];
if p.is_null() { if p.is_null() {
None None
} else { } else {
Some(BigNumRef::from_ptr(p as *mut _)) Some(Ref::<BigNum>::from_ptr(p as *mut _))
} }
} }
} }
pub fn q(&self) -> Option<&BigNumRef> { pub fn q(&self) -> Option<&Ref<BigNum>> {
unsafe { unsafe {
let q = compat::pqg(self.as_ptr())[1]; let q = compat::pqg(self.as_ptr())[1];
if q.is_null() { if q.is_null() {
None None
} else { } else {
Some(BigNumRef::from_ptr(q as *mut _)) Some(Ref::<BigNum>::from_ptr(q as *mut _))
} }
} }
} }
pub fn g(&self) -> Option<&BigNumRef> { pub fn g(&self) -> Option<&Ref<BigNum>> {
unsafe { unsafe {
let g = compat::pqg(self.as_ptr())[2]; let g = compat::pqg(self.as_ptr())[2];
if g.is_null() { if g.is_null() {
None None
} else { } else {
Some(BigNumRef::from_ptr(g as *mut _)) Some(Ref::<BigNum>::from_ptr(g as *mut _))
} }
} }
} }

View File

@ -6,10 +6,11 @@ use std::ops::Deref;
use libc::{c_int, c_void, c_char}; use libc::{c_int, c_void, c_char};
use {cvt, cvt_p, cvt_n}; use {cvt, cvt_p, cvt_n};
use bn::{BigNum, BigNumRef}; use bn::BigNum;
use bio::{MemBio, MemBioSlice}; use bio::{MemBio, MemBioSlice};
use error::ErrorStack; use error::ErrorStack;
use util::{CallbackState, invoke_passwd_cb}; use util::{CallbackState, invoke_passwd_cb};
use types::{OpenSslType, Ref};
use opaque::Opaque; use opaque::Opaque;
/// Type of encryption padding to use. /// Type of encryption padding to use.
@ -162,57 +163,57 @@ impl RsaRef {
} }
} }
pub fn n(&self) -> Option<&BigNumRef> { pub fn n(&self) -> Option<&Ref<BigNum>> {
unsafe { unsafe {
let n = compat::key(self.as_ptr())[0]; let n = compat::key(self.as_ptr())[0];
if n.is_null() { if n.is_null() {
None None
} else { } else {
Some(BigNumRef::from_ptr(n as *mut _)) Some(Ref::<BigNum>::from_ptr(n as *mut _))
} }
} }
} }
pub fn d(&self) -> Option<&BigNumRef> { pub fn d(&self) -> Option<&Ref<BigNum>> {
unsafe { unsafe {
let d = compat::key(self.as_ptr())[2]; let d = compat::key(self.as_ptr())[2];
if d.is_null() { if d.is_null() {
None None
} else { } else {
Some(BigNumRef::from_ptr(d as *mut _)) Some(Ref::<BigNum>::from_ptr(d as *mut _))
} }
} }
} }
pub fn e(&self) -> Option<&BigNumRef> { pub fn e(&self) -> Option<&Ref<BigNum>> {
unsafe { unsafe {
let e = compat::key(self.as_ptr())[1]; let e = compat::key(self.as_ptr())[1];
if e.is_null() { if e.is_null() {
None None
} else { } else {
Some(BigNumRef::from_ptr(e as *mut _)) Some(Ref::<BigNum>::from_ptr(e as *mut _))
} }
} }
} }
pub fn p(&self) -> Option<&BigNumRef> { pub fn p(&self) -> Option<&Ref<BigNum>> {
unsafe { unsafe {
let p = compat::factors(self.as_ptr())[0]; let p = compat::factors(self.as_ptr())[0];
if p.is_null() { if p.is_null() {
None None
} else { } else {
Some(BigNumRef::from_ptr(p as *mut _)) Some(Ref::<BigNum>::from_ptr(p as *mut _))
} }
} }
} }
pub fn q(&self) -> Option<&BigNumRef> { pub fn q(&self) -> Option<&Ref<BigNum>> {
unsafe { unsafe {
let q = compat::factors(self.as_ptr())[1]; let q = compat::factors(self.as_ptr())[1];
if q.is_null() { if q.is_null() {
None None
} else { } else {
Some(BigNumRef::from_ptr(q as *mut _)) Some(Ref::<BigNum>::from_ptr(q as *mut _))
} }
} }
} }