Refactor BigNum
This commit is contained in:
parent
5af01a5dbd
commit
05089bacb3
|
|
@ -2,17 +2,12 @@ use libc::{c_int, c_ulong, c_void};
|
||||||
use std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::{fmt, ptr, mem};
|
use std::{fmt, ptr, mem};
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub, Deref, DerefMut};
|
||||||
|
|
||||||
use ffi;
|
use ffi;
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
|
|
||||||
/// A signed arbitrary-precision integer.
|
|
||||||
///
|
|
||||||
/// `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);
|
|
||||||
|
|
||||||
/// Specifies the desired properties of a randomly generated `BigNum`.
|
/// Specifies the desired properties of a randomly generated `BigNum`.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
|
@ -79,68 +74,14 @@ macro_rules! with_bn_in_ctx(
|
||||||
});
|
});
|
||||||
);
|
);
|
||||||
|
|
||||||
impl BigNum {
|
/// A borrowed, signed, arbitrary-precision integer.
|
||||||
/// Creates a new `BigNum` with the value 0.
|
#[derive(Copy, Clone)]
|
||||||
pub fn new() -> Result<BigNum, ErrorStack> {
|
pub struct BigNumRef<'a>(*mut ffi::BIGNUM, PhantomData<&'a ()>);
|
||||||
unsafe {
|
|
||||||
ffi::init();
|
|
||||||
|
|
||||||
let v = try_ssl_null!(ffi::BN_new());
|
|
||||||
Ok(BigNum(v))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a new `BigNum` with the given value.
|
impl<'a> BigNumRef<'a> {
|
||||||
pub fn new_from(n: u64) -> Result<BigNum, ErrorStack> {
|
pub unsafe fn from_handle(handle: *mut ffi::BIGNUM) -> BigNumRef<'a> {
|
||||||
BigNum::new().and_then(|v| unsafe {
|
BigNumRef(handle, PhantomData)
|
||||||
try_ssl!(ffi::BN_set_word(v.raw(), n as c_ulong));
|
|
||||||
Ok(v)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a `BigNum` from a decimal string.
|
|
||||||
pub fn from_dec_str(s: &str) -> Result<BigNum, ErrorStack> {
|
|
||||||
BigNum::new().and_then(|v| unsafe {
|
|
||||||
let c_str = CString::new(s.as_bytes()).unwrap();
|
|
||||||
try_ssl!(ffi::BN_dec2bn(v.raw_ptr(), c_str.as_ptr() as *const _));
|
|
||||||
Ok(v)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a `BigNum` from a hexadecimal string.
|
|
||||||
pub fn from_hex_str(s: &str) -> Result<BigNum, ErrorStack> {
|
|
||||||
BigNum::new().and_then(|v| unsafe {
|
|
||||||
let c_str = CString::new(s.as_bytes()).unwrap();
|
|
||||||
try_ssl!(ffi::BN_hex2bn(v.raw_ptr(), c_str.as_ptr() as *const _));
|
|
||||||
Ok(v)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub unsafe fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result<BigNum, ErrorStack> {
|
|
||||||
if orig.is_null() {
|
|
||||||
panic!("Null Pointer was supplied to BigNum::new_from_ffi");
|
|
||||||
}
|
|
||||||
let r = ffi::BN_dup(orig);
|
|
||||||
if r.is_null() {
|
|
||||||
Err(ErrorStack::get())
|
|
||||||
} else {
|
|
||||||
Ok(BigNum(r))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a new `BigNum` from an unsigned, big-endian encoded number of arbitrary length.
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use openssl::bn::BigNum;
|
|
||||||
/// let bignum = BigNum::new_from_slice(&[0x12, 0x00, 0x34]).unwrap();
|
|
||||||
///
|
|
||||||
/// assert_eq!(bignum, BigNum::new_from(0x120034).unwrap());
|
|
||||||
/// ```
|
|
||||||
pub fn new_from_slice(n: &[u8]) -> Result<BigNum, ErrorStack> {
|
|
||||||
BigNum::new().and_then(|v| unsafe {
|
|
||||||
try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw()));
|
|
||||||
Ok(v)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the square of `self`.
|
/// Returns the square of `self`.
|
||||||
|
|
@ -162,7 +103,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the unsigned remainder of the division `self / n`.
|
/// Returns the unsigned remainder of the division `self / n`.
|
||||||
pub fn checked_nnmod(&self, n: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_nnmod(&self, n: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_nnmod(r.raw(), self.raw(), n.raw(), ctx) == 1
|
ffi::BN_nnmod(r.raw(), self.raw(), n.raw(), ctx) == 1
|
||||||
|
|
@ -181,7 +122,7 @@ impl BigNum {
|
||||||
///
|
///
|
||||||
/// assert_eq!(s.checked_mod_add(a, n).unwrap(), result);
|
/// assert_eq!(s.checked_mod_add(a, n).unwrap(), result);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn checked_mod_add(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_mod_add(&self, a: &BigNumRef, n: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_mod_add(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
|
ffi::BN_mod_add(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
|
||||||
|
|
@ -190,7 +131,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Equivalent to `(self - a) mod n`.
|
/// Equivalent to `(self - a) mod n`.
|
||||||
pub fn checked_mod_sub(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_mod_sub(&self, a: &BigNumRef, n: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_mod_sub(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
|
ffi::BN_mod_sub(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
|
||||||
|
|
@ -199,7 +140,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Equivalent to `(self * a) mod n`.
|
/// Equivalent to `(self * a) mod n`.
|
||||||
pub fn checked_mod_mul(&self, a: &BigNum, n: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_mod_mul(&self, a: &BigNumRef, n: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_mod_mul(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
|
ffi::BN_mod_mul(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1
|
||||||
|
|
@ -208,7 +149,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Equivalent to `self² mod n`.
|
/// Equivalent to `self² mod n`.
|
||||||
pub fn checked_mod_sqr(&self, n: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_mod_sqr(&self, n: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_mod_sqr(r.raw(), self.raw(), n.raw(), ctx) == 1
|
ffi::BN_mod_sqr(r.raw(), self.raw(), n.raw(), ctx) == 1
|
||||||
|
|
@ -217,7 +158,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Raises `self` to the `p`th power.
|
/// Raises `self` to the `p`th power.
|
||||||
pub fn checked_exp(&self, p: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_exp(&self, p: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_exp(r.raw(), self.raw(), p.raw(), ctx) == 1
|
ffi::BN_exp(r.raw(), self.raw(), p.raw(), ctx) == 1
|
||||||
|
|
@ -226,7 +167,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Equivalent to `self.checked_exp(p) mod n`.
|
/// Equivalent to `self.checked_exp(p) mod n`.
|
||||||
pub fn checked_mod_exp(&self, p: &BigNum, n: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_mod_exp(&self, p: &BigNumRef, n: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_mod_exp(r.raw(), self.raw(), p.raw(), n.raw(), ctx) == 1
|
ffi::BN_mod_exp(r.raw(), self.raw(), p.raw(), n.raw(), ctx) == 1
|
||||||
|
|
@ -236,7 +177,7 @@ impl BigNum {
|
||||||
|
|
||||||
/// Calculates the modular multiplicative inverse of `self` modulo `n`, that is, an integer `r`
|
/// Calculates the modular multiplicative inverse of `self` modulo `n`, that is, an integer `r`
|
||||||
/// such that `(self * r) % n == 1`.
|
/// such that `(self * r) % n == 1`.
|
||||||
pub fn checked_mod_inv(&self, n: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_mod_inv(&self, n: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
!ffi::BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx).is_null()
|
!ffi::BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx).is_null()
|
||||||
|
|
@ -298,7 +239,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the greatest common denominator of `self` and `a`.
|
/// Computes the greatest common denominator of `self` and `a`.
|
||||||
pub fn checked_gcd(&self, a: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_gcd(&self, a: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1
|
ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1
|
||||||
|
|
@ -306,34 +247,6 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a prime number.
|
|
||||||
///
|
|
||||||
/// # Parameters
|
|
||||||
///
|
|
||||||
/// * `bits`: The length of the prime in bits (lower bound).
|
|
||||||
/// * `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
|
|
||||||
/// generated prime and `rem` is `1` if not specified (`None`).
|
|
||||||
pub fn checked_generate_prime(bits: i32,
|
|
||||||
safe: bool,
|
|
||||||
add: Option<&BigNum>,
|
|
||||||
rem: Option<&BigNum>)
|
|
||||||
-> Result<BigNum, ErrorStack> {
|
|
||||||
unsafe {
|
|
||||||
with_bn_in_ctx!(r, ctx, {
|
|
||||||
let add_arg = add.map(|a| a.raw()).unwrap_or(ptr::null_mut());
|
|
||||||
let rem_arg = rem.map(|r| r.raw()).unwrap_or(ptr::null_mut());
|
|
||||||
|
|
||||||
ffi::BN_generate_prime_ex(r.raw(),
|
|
||||||
bits as c_int,
|
|
||||||
safe as c_int,
|
|
||||||
add_arg,
|
|
||||||
rem_arg,
|
|
||||||
ptr::null()) == 1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks whether `self` is prime.
|
/// Checks whether `self` is prime.
|
||||||
///
|
///
|
||||||
/// Performs a Miller-Rabin probabilistic primality test with `checks` iterations.
|
/// Performs a Miller-Rabin probabilistic primality test with `checks` iterations.
|
||||||
|
|
@ -370,33 +283,6 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a cryptographically strong pseudo-random `BigNum`.
|
|
||||||
///
|
|
||||||
/// # Parameters
|
|
||||||
///
|
|
||||||
/// * `bits`: Length of the number in bits.
|
|
||||||
/// * `prop`: The desired properties of the number.
|
|
||||||
/// * `odd`: If `true`, the generated number will be odd.
|
|
||||||
pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNum, ErrorStack> {
|
|
||||||
unsafe {
|
|
||||||
with_bn_in_ctx!(r, ctx, {
|
|
||||||
ffi::BN_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The cryptographically weak counterpart to `checked_new_random`.
|
|
||||||
pub fn checked_new_pseudo_random(bits: i32,
|
|
||||||
prop: RNGProperty,
|
|
||||||
odd: bool)
|
|
||||||
-> Result<BigNum, ErrorStack> {
|
|
||||||
unsafe {
|
|
||||||
with_bn_in_ctx!(r, ctx, {
|
|
||||||
ffi::BN_pseudo_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates a cryptographically strong pseudo-random `BigNum` `r` in the range
|
/// Generates a cryptographically strong pseudo-random `BigNum` `r` in the range
|
||||||
/// `0 <= r < self`.
|
/// `0 <= r < self`.
|
||||||
pub fn checked_rand_in_range(&self) -> Result<BigNum, ErrorStack> {
|
pub fn checked_rand_in_range(&self) -> Result<BigNum, ErrorStack> {
|
||||||
|
|
@ -495,7 +381,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_add(&self, a: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_add(&self, a: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn!(r, {
|
with_bn!(r, {
|
||||||
ffi::BN_add(r.raw(), self.raw(), a.raw()) == 1
|
ffi::BN_add(r.raw(), self.raw(), a.raw()) == 1
|
||||||
|
|
@ -503,7 +389,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_sub(&self, a: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_sub(&self, a: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn!(r, {
|
with_bn!(r, {
|
||||||
ffi::BN_sub(r.raw(), self.raw(), a.raw()) == 1
|
ffi::BN_sub(r.raw(), self.raw(), a.raw()) == 1
|
||||||
|
|
@ -511,7 +397,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_mul(&self, a: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_mul(&self, a: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_mul(r.raw(), self.raw(), a.raw(), ctx) == 1
|
ffi::BN_mul(r.raw(), self.raw(), a.raw(), ctx) == 1
|
||||||
|
|
@ -519,7 +405,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_div(&self, a: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_div(&self, a: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_div(r.raw(), ptr::null_mut(), self.raw(), a.raw(), ctx) == 1
|
ffi::BN_div(r.raw(), ptr::null_mut(), self.raw(), a.raw(), ctx) == 1
|
||||||
|
|
@ -527,7 +413,7 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn checked_mod(&self, a: &BigNum) -> Result<BigNum, ErrorStack> {
|
pub fn checked_mod(&self, a: &BigNumRef) -> Result<BigNum, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
with_bn_in_ctx!(r, ctx, {
|
with_bn_in_ctx!(r, ctx, {
|
||||||
ffi::BN_div(ptr::null_mut(), r.raw(), self.raw(), a.raw(), ctx) == 1
|
ffi::BN_div(ptr::null_mut(), r.raw(), self.raw(), a.raw(), ctx) == 1
|
||||||
|
|
@ -551,6 +437,13 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_owned(&self) -> Result<BigNum, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
let r = try_ssl_null!(ffi::BN_dup(self.raw()));
|
||||||
|
Ok(BigNum::from_handle(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Inverts the sign of `self`.
|
/// Inverts the sign of `self`.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
|
@ -574,9 +467,9 @@ impl BigNum {
|
||||||
/// let s = -BigNum::new_from(8).unwrap();
|
/// let s = -BigNum::new_from(8).unwrap();
|
||||||
/// let o = BigNum::new_from(8).unwrap();
|
/// let o = BigNum::new_from(8).unwrap();
|
||||||
///
|
///
|
||||||
/// assert_eq!(s.abs_cmp(o), Ordering::Equal);
|
/// assert_eq!(s.abs_cmp(&o), Ordering::Equal);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn abs_cmp(&self, oth: BigNum) -> Ordering {
|
pub fn abs_cmp(&self, oth: &BigNumRef) -> Ordering {
|
||||||
unsafe {
|
unsafe {
|
||||||
let res = ffi::BN_ucmp(self.raw(), oth.raw()) as i32;
|
let res = ffi::BN_ucmp(self.raw(), oth.raw()) as i32;
|
||||||
if res < 0 {
|
if res < 0 {
|
||||||
|
|
@ -603,19 +496,12 @@ impl BigNum {
|
||||||
(self.num_bits() + 7) / 8
|
(self.num_bits() + 7) / 8
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn raw(&self) -> *mut ffi::BIGNUM {
|
pub fn raw(&self) -> *mut ffi::BIGNUM {
|
||||||
let BigNum(n) = *self;
|
self.0
|
||||||
n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn raw_ptr(&self) -> *const *mut ffi::BIGNUM {
|
pub fn raw_ptr(&self) -> *const *mut ffi::BIGNUM {
|
||||||
let BigNum(ref n) = *self;
|
&self.0
|
||||||
n
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn into_raw(self) -> *mut ffi::BIGNUM {
|
|
||||||
let mut me = self;
|
|
||||||
mem::replace(&mut me.0, ptr::null_mut())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a big-endian byte vector representation of the absolute value of `self`.
|
/// Returns a big-endian byte vector representation of the absolute value of `self`.
|
||||||
|
|
@ -679,134 +565,438 @@ impl BigNum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An owned, signed, arbitrary-precision integer.
|
||||||
|
///
|
||||||
|
/// `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(BigNumRef<'static>);
|
||||||
|
|
||||||
|
impl BigNum {
|
||||||
|
/// Creates a new `BigNum` with the value 0.
|
||||||
|
pub fn new() -> Result<BigNum, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
ffi::init();
|
||||||
|
let v = try_ssl_null!(ffi::BN_new());
|
||||||
|
Ok(BigNum::from_handle(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new `BigNum` with the given value.
|
||||||
|
pub fn new_from(n: u64) -> Result<BigNum, ErrorStack> {
|
||||||
|
BigNum::new().and_then(|v| unsafe {
|
||||||
|
try_ssl!(ffi::BN_set_word(v.raw(), n as c_ulong));
|
||||||
|
Ok(v)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a `BigNum` from a decimal string.
|
||||||
|
pub fn from_dec_str(s: &str) -> Result<BigNum, ErrorStack> {
|
||||||
|
BigNum::new().and_then(|v| unsafe {
|
||||||
|
let c_str = CString::new(s.as_bytes()).unwrap();
|
||||||
|
try_ssl!(ffi::BN_dec2bn(v.raw_ptr(), c_str.as_ptr() as *const _));
|
||||||
|
Ok(v)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a `BigNum` from a hexadecimal string.
|
||||||
|
pub fn from_hex_str(s: &str) -> Result<BigNum, ErrorStack> {
|
||||||
|
BigNum::new().and_then(|v| unsafe {
|
||||||
|
let c_str = CString::new(s.as_bytes()).unwrap();
|
||||||
|
try_ssl!(ffi::BN_hex2bn(v.raw_ptr(), c_str.as_ptr() as *const _));
|
||||||
|
Ok(v)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn from_handle(handle: *mut ffi::BIGNUM) -> BigNum {
|
||||||
|
BigNum(BigNumRef::from_handle(handle))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new `BigNum` from an unsigned, big-endian encoded number of arbitrary length.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use openssl::bn::BigNum;
|
||||||
|
/// let bignum = BigNum::new_from_slice(&[0x12, 0x00, 0x34]).unwrap();
|
||||||
|
///
|
||||||
|
/// assert_eq!(bignum, BigNum::new_from(0x120034).unwrap());
|
||||||
|
/// ```
|
||||||
|
pub fn new_from_slice(n: &[u8]) -> Result<BigNum, ErrorStack> {
|
||||||
|
BigNum::new().and_then(|v| unsafe {
|
||||||
|
try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw()));
|
||||||
|
Ok(v)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/// Generates a prime number.
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
///
|
||||||
|
/// * `bits`: The length of the prime in bits (lower bound).
|
||||||
|
/// * `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
|
||||||
|
/// generated prime and `rem` is `1` if not specified (`None`).
|
||||||
|
pub fn checked_generate_prime(bits: i32,
|
||||||
|
safe: bool,
|
||||||
|
add: Option<&BigNum>,
|
||||||
|
rem: Option<&BigNum>)
|
||||||
|
-> Result<BigNum, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
with_bn_in_ctx!(r, ctx, {
|
||||||
|
let add_arg = add.map(|a| a.raw()).unwrap_or(ptr::null_mut());
|
||||||
|
let rem_arg = rem.map(|r| r.raw()).unwrap_or(ptr::null_mut());
|
||||||
|
|
||||||
|
ffi::BN_generate_prime_ex(r.raw(),
|
||||||
|
bits as c_int,
|
||||||
|
safe as c_int,
|
||||||
|
add_arg,
|
||||||
|
rem_arg,
|
||||||
|
ptr::null()) == 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates a cryptographically strong pseudo-random `BigNum`.
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
///
|
||||||
|
/// * `bits`: Length of the number in bits.
|
||||||
|
/// * `prop`: The desired properties of the number.
|
||||||
|
/// * `odd`: If `true`, the generated number will be odd.
|
||||||
|
pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result<BigNum, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
with_bn_in_ctx!(r, ctx, {
|
||||||
|
ffi::BN_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The cryptographically weak counterpart to `checked_new_random`.
|
||||||
|
pub fn checked_new_pseudo_random(bits: i32,
|
||||||
|
prop: RNGProperty,
|
||||||
|
odd: bool)
|
||||||
|
-> Result<BigNum, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
with_bn_in_ctx!(r, ctx, {
|
||||||
|
ffi::BN_pseudo_rand(r.raw(), bits as c_int, prop as c_int, odd as c_int) == 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_raw(self) -> *mut ffi::BIGNUM {
|
||||||
|
let ptr = self.raw();
|
||||||
|
mem::forget(self);
|
||||||
|
ptr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for BigNum {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe { ffi::BN_clear_free(self.raw()); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for BigNum {
|
||||||
|
type Target = BigNumRef<'static>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &BigNumRef<'static> {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for BigNum {
|
||||||
|
fn deref_mut(&mut self) -> &mut BigNumRef<'static> {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRef<BigNumRef<'static>> for BigNum {
|
||||||
|
fn as_ref(&self) -> &BigNumRef<'static> {
|
||||||
|
self.deref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> fmt::Debug for BigNumRef<'a> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.to_dec_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Debug for BigNum {
|
impl fmt::Debug for BigNum {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self.to_dec_str())
|
write!(f, "{}", self.to_dec_str())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Eq for BigNum {}
|
impl<'a> fmt::Display for BigNumRef<'a> {
|
||||||
impl PartialEq for BigNum {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fn eq(&self, oth: &BigNum) -> bool {
|
write!(f, "{}", self.to_dec_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for BigNum {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.to_dec_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> PartialEq<BigNumRef<'b>> for BigNumRef<'a> {
|
||||||
|
fn eq(&self, oth: &BigNumRef) -> bool {
|
||||||
unsafe { ffi::BN_cmp(self.raw(), oth.raw()) == 0 }
|
unsafe { ffi::BN_cmp(self.raw(), oth.raw()) == 0 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ord for BigNum {
|
impl<'a> PartialEq<BigNum> for BigNumRef<'a> {
|
||||||
fn cmp(&self, oth: &BigNum) -> Ordering {
|
fn eq(&self, oth: &BigNum) -> bool {
|
||||||
self.partial_cmp(oth).unwrap()
|
self.eq(oth.deref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Eq for BigNumRef<'a> {}
|
||||||
|
|
||||||
|
impl PartialEq for BigNum {
|
||||||
|
fn eq(&self, oth: &BigNum) -> bool {
|
||||||
|
self.deref().eq(oth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> PartialEq<BigNumRef<'a>> for BigNum {
|
||||||
|
fn eq(&self, oth: &BigNumRef) -> bool {
|
||||||
|
self.deref().eq(oth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for BigNum {}
|
||||||
|
|
||||||
|
impl<'a, 'b> PartialOrd<BigNumRef<'b>> for BigNumRef<'a> {
|
||||||
|
fn partial_cmp(&self, oth: &BigNumRef) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(oth))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> PartialOrd<BigNum> for BigNumRef<'a> {
|
||||||
|
fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(oth.deref()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Ord for BigNumRef<'a> {
|
||||||
|
fn cmp(&self, oth: &BigNumRef) -> Ordering {
|
||||||
|
unsafe { ffi::BN_cmp(self.raw(), oth.raw()).cmp(&0) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for BigNum {
|
impl PartialOrd for BigNum {
|
||||||
fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> {
|
fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering> {
|
||||||
unsafe {
|
self.deref().partial_cmp(oth.deref())
|
||||||
let v = ffi::BN_cmp(self.raw(), oth.raw());
|
|
||||||
let ret = if v == 0 {
|
|
||||||
Ordering::Equal
|
|
||||||
} else if v < 0 {
|
|
||||||
Ordering::Less
|
|
||||||
} else {
|
|
||||||
Ordering::Greater
|
|
||||||
};
|
|
||||||
Some(ret)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for BigNum {
|
impl<'a> PartialOrd<BigNumRef<'a>> for BigNum {
|
||||||
fn drop(&mut self) {
|
fn partial_cmp(&self, oth: &BigNumRef) -> Option<Ordering> {
|
||||||
unsafe {
|
self.deref().partial_cmp(oth)
|
||||||
if !self.raw().is_null() {
|
|
||||||
ffi::BN_clear_free(self.raw());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)] // This module only contains impls, so it's empty when generating docs
|
impl Ord for BigNum {
|
||||||
pub mod unchecked {
|
fn cmp(&self, oth: &BigNum) -> Ordering {
|
||||||
use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub};
|
self.deref().cmp(oth.deref())
|
||||||
use ffi;
|
|
||||||
use super::BigNum;
|
|
||||||
|
|
||||||
impl<'a, 'b> Add<&'b BigNum> for &'a BigNum {
|
|
||||||
type Output = BigNum;
|
|
||||||
|
|
||||||
fn add(self, oth: &'b BigNum) -> BigNum {
|
|
||||||
self.checked_add(oth).unwrap()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Sub<&'b BigNum> for &'a BigNum {
|
impl<'a, 'b> Add<&'b BigNumRef<'b>> for &'a BigNumRef<'a> {
|
||||||
type Output = BigNum;
|
type Output = BigNum;
|
||||||
|
|
||||||
fn sub(self, oth: &'b BigNum) -> BigNum {
|
fn add(self, oth: &BigNumRef) -> BigNum {
|
||||||
self.checked_sub(oth).unwrap()
|
self.checked_add(oth).unwrap()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Mul<&'b BigNum> for &'a BigNum {
|
impl<'a, 'b> Sub<&'b BigNumRef<'b>> for &'a BigNumRef<'a> {
|
||||||
type Output = BigNum;
|
type Output = BigNum;
|
||||||
|
|
||||||
fn mul(self, oth: &'b BigNum) -> BigNum {
|
fn sub(self, oth: &BigNumRef) -> BigNum {
|
||||||
self.checked_mul(oth).unwrap()
|
self.checked_sub(oth).unwrap()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Div<&'b BigNum> for &'a BigNum {
|
impl<'a, 'b> Sub<&'b BigNum> for &'a BigNumRef<'a> {
|
||||||
type Output = BigNum;
|
type Output = BigNum;
|
||||||
|
|
||||||
fn div(self, oth: &'b BigNum) -> BigNum {
|
fn sub(self, oth: &BigNum) -> BigNum {
|
||||||
self.checked_div(oth).unwrap()
|
self.checked_sub(oth).unwrap()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Rem<&'b BigNum> for &'a BigNum {
|
impl<'a, 'b> Sub<&'b BigNum> for &'a BigNum {
|
||||||
type Output = BigNum;
|
type Output = BigNum;
|
||||||
|
|
||||||
fn rem(self, oth: &'b BigNum) -> BigNum {
|
fn sub(self, oth: &BigNum) -> BigNum {
|
||||||
self.checked_mod(oth).unwrap()
|
self.checked_sub(oth).unwrap()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Shl<i32> for &'a BigNum {
|
impl<'a, 'b> Sub<&'b BigNumRef<'b>> for &'a BigNum {
|
||||||
type Output = BigNum;
|
type Output = BigNum;
|
||||||
|
|
||||||
fn shl(self, n: i32) -> BigNum {
|
fn sub(self, oth: &BigNumRef) -> BigNum {
|
||||||
self.checked_shl(&n).unwrap()
|
self.checked_sub(oth).unwrap()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Shr<i32> for &'a BigNum {
|
impl<'a, 'b> Mul<&'b BigNumRef<'b>> for &'a BigNumRef<'a> {
|
||||||
type Output = BigNum;
|
type Output = BigNum;
|
||||||
|
|
||||||
fn shr(self, n: i32) -> BigNum {
|
fn mul(self, oth: &BigNumRef) -> BigNum {
|
||||||
self.checked_shr(&n).unwrap()
|
self.checked_mul(oth).unwrap()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Clone for BigNum {
|
impl<'a, 'b> Mul<&'b BigNum> for &'a BigNumRef<'a> {
|
||||||
fn clone(&self) -> BigNum {
|
type Output = BigNum;
|
||||||
unsafe {
|
|
||||||
let r = ffi::BN_dup(self.raw());
|
fn mul(self, oth: &BigNum) -> BigNum {
|
||||||
if r.is_null() {
|
self.checked_mul(oth).unwrap()
|
||||||
panic!("Unexpected null pointer from BN_dup(..)")
|
|
||||||
} else {
|
|
||||||
BigNum(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Neg for BigNum {
|
impl<'a, 'b> Mul<&'b BigNum> for &'a BigNum {
|
||||||
type Output = BigNum;
|
type Output = BigNum;
|
||||||
|
|
||||||
fn neg(self) -> BigNum {
|
fn mul(self, oth: &BigNum) -> BigNum {
|
||||||
let mut n = self.clone();
|
self.checked_mul(oth).unwrap()
|
||||||
n.negate();
|
}
|
||||||
n
|
}
|
||||||
}
|
|
||||||
|
impl<'a, 'b> Mul<&'b BigNumRef<'b>> for &'a BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn mul(self, oth: &BigNumRef) -> BigNum {
|
||||||
|
self.checked_mul(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Div<&'b BigNumRef<'b>> for &'a BigNumRef<'a> {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn div(self, oth: &'b BigNumRef<'b>) -> BigNum {
|
||||||
|
self.checked_div(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Div<&'b BigNum> for &'a BigNumRef<'a> {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn div(self, oth: &'b BigNum) -> BigNum {
|
||||||
|
self.checked_div(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Div<&'b BigNum> for &'a BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn div(self, oth: &'b BigNum) -> BigNum {
|
||||||
|
self.checked_div(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Div<&'b BigNumRef<'b>> for &'a BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn div(self, oth: &'b BigNumRef<'b>) -> BigNum {
|
||||||
|
self.checked_div(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Rem<&'b BigNumRef<'b>> for &'a BigNumRef<'a> {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn rem(self, oth: &'b BigNumRef<'b>) -> BigNum {
|
||||||
|
self.checked_mod(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Rem<&'b BigNum> for &'a BigNumRef<'a> {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn rem(self, oth: &'b BigNum) -> BigNum {
|
||||||
|
self.checked_mod(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Rem<&'b BigNumRef<'b>> for &'a BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn rem(self, oth: &'b BigNumRef<'b>) -> BigNum {
|
||||||
|
self.checked_mod(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Rem<&'b BigNum> for &'a BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn rem(self, oth: &'b BigNum) -> BigNum {
|
||||||
|
self.checked_mod(oth).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Shl<i32> for &'a BigNumRef<'a> {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn shl(self, n: i32) -> BigNum {
|
||||||
|
self.checked_shl(&n).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Shl<i32> for &'a BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn shl(self, n: i32) -> BigNum {
|
||||||
|
self.checked_shl(&n).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Shr<i32> for &'a BigNumRef<'a> {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn shr(self, n: i32) -> BigNum {
|
||||||
|
self.checked_shr(&n).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Shr<i32> for &'a BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn shr(self, n: i32) -> BigNum {
|
||||||
|
self.checked_shr(&n).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Neg for &'a BigNumRef<'a> {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn neg(self) -> BigNum {
|
||||||
|
let mut n = self.to_owned().unwrap();
|
||||||
|
n.negate();
|
||||||
|
n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Neg for &'a BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn neg(self) -> BigNum {
|
||||||
|
let mut n = self.deref().to_owned().unwrap();
|
||||||
|
n.negate();
|
||||||
|
n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Neg for BigNum {
|
||||||
|
type Output = BigNum;
|
||||||
|
|
||||||
|
fn neg(mut self) -> BigNum {
|
||||||
|
self.negate();
|
||||||
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use error::ErrorStack;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use libc::{c_uint, c_int, c_char, c_void};
|
use libc::{c_uint, c_int, c_char, c_void};
|
||||||
|
|
||||||
use bn::BigNum;
|
use bn::BigNumRef;
|
||||||
use bio::{MemBio, MemBioSlice};
|
use bio::{MemBio, MemBioSlice};
|
||||||
use crypto::hash;
|
use crypto::hash;
|
||||||
use crypto::HashTypeInternals;
|
use crypto::HashTypeInternals;
|
||||||
|
|
@ -189,25 +189,27 @@ impl DSA {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
// The following getters are unsafe, since BigNum::new_from_ffi fails upon null pointers
|
pub fn p<'a>(&'a self) -> BigNumRef<'a> {
|
||||||
pub fn p(&self) -> Result<BigNum, ErrorStack> {
|
assert!(self.has_p());
|
||||||
unsafe { BigNum::new_from_ffi((*self.0).p) }
|
unsafe { BigNumRef::from_handle((*self.0).p) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_p(&self) -> bool {
|
pub fn has_p(&self) -> bool {
|
||||||
unsafe { !(*self.0).p.is_null() }
|
unsafe { !(*self.0).p.is_null() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn q(&self) -> Result<BigNum, ErrorStack> {
|
pub fn q<'a>(&'a self) -> BigNumRef<'a> {
|
||||||
unsafe { BigNum::new_from_ffi((*self.0).q) }
|
assert!(self.has_q());
|
||||||
|
unsafe { BigNumRef::from_handle((*self.0).q) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_q(&self) -> bool {
|
pub fn has_q(&self) -> bool {
|
||||||
unsafe { !(*self.0).q.is_null() }
|
unsafe { !(*self.0).q.is_null() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn g(&self) -> Result<BigNum, ErrorStack> {
|
pub fn g<'a>(&'a self) -> BigNumRef<'a> {
|
||||||
unsafe { BigNum::new_from_ffi((*self.0).g) }
|
assert!(self.has_g());
|
||||||
|
unsafe { BigNumRef::from_handle((*self.0).g) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_g(&self) -> bool {
|
pub fn has_g(&self) -> bool {
|
||||||
|
|
|
||||||
|
|
@ -799,7 +799,7 @@ mod tests {
|
||||||
let sig = k0.sign(&msg);
|
let sig = k0.sign(&msg);
|
||||||
|
|
||||||
let r0 = k0.get_rsa();
|
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);
|
k1.set_rsa(&r1);
|
||||||
|
|
||||||
assert!(k1.can(super::Role::Encrypt));
|
assert!(k1.can(super::Role::Encrypt));
|
||||||
|
|
@ -847,12 +847,13 @@ mod tests {
|
||||||
fn test_pkey_clone_creates_copy() {
|
fn test_pkey_clone_creates_copy() {
|
||||||
let mut pkey = super::PKey::new();
|
let mut pkey = super::PKey::new();
|
||||||
pkey.gen(512);
|
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();
|
let mut pkey2 = pkey.clone();
|
||||||
pkey2.gen(512);
|
pkey2.gen(512);
|
||||||
|
|
||||||
assert!(old_pkey_n == pkey.get_rsa().n().unwrap());
|
assert!(old_pkey_n == rsa.n());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -862,7 +863,7 @@ mod tests {
|
||||||
|
|
||||||
let pkey2 = pkey.clone();
|
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]
|
#[test]
|
||||||
|
|
@ -874,6 +875,6 @@ mod tests {
|
||||||
|
|
||||||
let pub_key2 = pub_key.clone();
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use std::fmt;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use libc::{c_int, c_void, c_char};
|
use libc::{c_int, c_void, c_char};
|
||||||
|
|
||||||
use bn::BigNum;
|
use bn::{BigNum, BigNumRef};
|
||||||
use bio::{MemBio, MemBioSlice};
|
use bio::{MemBio, MemBioSlice};
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use crypto::HashTypeInternals;
|
use crypto::HashTypeInternals;
|
||||||
|
|
@ -171,43 +171,49 @@ impl RSA {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
// The following getters are unsafe, since BigNum::new_from_ffi fails upon null pointers
|
pub fn n<'a>(&'a self) -> BigNumRef<'a> {
|
||||||
pub fn n(&self) -> Result<BigNum, ErrorStack> {
|
assert!(self.has_n());
|
||||||
unsafe {
|
unsafe { BigNumRef::from_handle((*self.0).n) }
|
||||||
BigNum::new_from_ffi((*self.0).n)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_n(&self) -> bool {
|
pub fn has_n(&self) -> bool {
|
||||||
unsafe { !(*self.0).n.is_null() }
|
unsafe { !(*self.0).n.is_null() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn d(&self) -> Result<BigNum, ErrorStack> {
|
pub fn d<'a>(&self) -> BigNumRef<'a> {
|
||||||
unsafe {
|
assert!(self.has_d());
|
||||||
BigNum::new_from_ffi((*self.0).d)
|
unsafe { BigNumRef::from_handle((*self.0).d) }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn e(&self) -> Result<BigNum, ErrorStack> {
|
pub fn has_d(&self) -> bool {
|
||||||
unsafe {
|
unsafe { !(*self.0).d.is_null() }
|
||||||
BigNum::new_from_ffi((*self.0).e)
|
}
|
||||||
}
|
|
||||||
|
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 {
|
pub fn has_e(&self) -> bool {
|
||||||
unsafe { !(*self.0).e.is_null() }
|
unsafe { !(*self.0).e.is_null() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn p(&self) -> Result<BigNum, ErrorStack> {
|
pub fn p<'a>(&'a self) -> BigNumRef<'a> {
|
||||||
unsafe {
|
assert!(self.has_p());
|
||||||
BigNum::new_from_ffi((*self.0).p)
|
unsafe { BigNumRef::from_handle((*self.0).p) }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn q(&self) -> Result<BigNum, ErrorStack> {
|
pub fn has_p(&self) -> bool {
|
||||||
unsafe {
|
unsafe { !(*self.0).p.is_null() }
|
||||||
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() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue