From fa622326490e1dd27df4d42b4097ca574deedb3f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 30 Jan 2016 12:55:22 -0800 Subject: Error reform --- openssl/src/bn/mod.rs | 108 +++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 54 deletions(-) (limited to 'openssl/src/bn') diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index d548e9ef..eb697248 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -4,7 +4,7 @@ use std::cmp::Ordering; use std::{fmt, ptr, mem}; use ffi; -use ssl::error::SslError; +use error::ErrorStack; pub struct BigNum(*mut ffi::BIGNUM); @@ -20,7 +20,7 @@ macro_rules! with_ctx( ($name:ident, $action:block) => ({ let $name = ffi::BN_CTX_new(); if ($name).is_null() { - Err(SslError::get()) + Err(ErrorStack::get()) } else { let r = $action; ffi::BN_CTX_free($name); @@ -37,7 +37,7 @@ macro_rules! with_bn( if $action { Ok($name) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } }, Err(err) => Err(err), @@ -52,13 +52,13 @@ macro_rules! with_bn_in_ctx( Ok($name) => { let $ctx_name = ffi::BN_CTX_new(); if ($ctx_name).is_null() { - Err(SslError::get()) + Err(ErrorStack::get()) } else { let r = if $action { Ok($name) } else { - Err(SslError::get()) + Err(ErrorStack::get()) }; ffi::BN_CTX_free($ctx_name); r @@ -70,7 +70,7 @@ macro_rules! with_bn_in_ctx( ); impl BigNum { - pub fn new() -> Result { + pub fn new() -> Result { unsafe { ffi::init(); @@ -79,14 +79,14 @@ impl BigNum { } } - pub fn new_from(n: u64) -> Result { + pub fn new_from(n: u64) -> Result { BigNum::new().and_then(|v| unsafe { try_ssl!(ffi::BN_set_word(v.raw(), n as c_ulong)); Ok(v) }) } - pub fn from_dec_str(s: &str) -> Result { + pub fn from_dec_str(s: &str) -> Result { 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 _)); @@ -94,7 +94,7 @@ impl BigNum { }) } - pub fn from_hex_str(s: &str) -> Result { + pub fn from_hex_str(s: &str) -> Result { 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 _)); @@ -102,26 +102,26 @@ impl BigNum { }) } - pub unsafe fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result { + pub unsafe fn new_from_ffi(orig: *mut ffi::BIGNUM) -> Result { 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(SslError::get()) + Err(ErrorStack::get()) } else { Ok(BigNum(r)) } } - pub fn new_from_slice(n: &[u8]) -> Result { + pub fn new_from_slice(n: &[u8]) -> Result { BigNum::new().and_then(|v| unsafe { try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw())); Ok(v) }) } - pub fn checked_sqr(&self) -> Result { + pub fn checked_sqr(&self) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_sqr(r.raw(), self.raw(), ctx) == 1 @@ -129,7 +129,7 @@ impl BigNum { } } - pub fn checked_nnmod(&self, n: &BigNum) -> Result { + pub fn checked_nnmod(&self, n: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_nnmod(r.raw(), self.raw(), n.raw(), ctx) == 1 @@ -137,7 +137,7 @@ impl BigNum { } } - pub fn checked_mod_add(&self, a: &BigNum, n: &BigNum) -> Result { + pub fn checked_mod_add(&self, a: &BigNum, n: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_mod_add(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 @@ -145,7 +145,7 @@ impl BigNum { } } - pub fn checked_mod_sub(&self, a: &BigNum, n: &BigNum) -> Result { + pub fn checked_mod_sub(&self, a: &BigNum, n: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_mod_sub(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 @@ -153,7 +153,7 @@ impl BigNum { } } - pub fn checked_mod_mul(&self, a: &BigNum, n: &BigNum) -> Result { + pub fn checked_mod_mul(&self, a: &BigNum, n: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_mod_mul(r.raw(), self.raw(), a.raw(), n.raw(), ctx) == 1 @@ -161,7 +161,7 @@ impl BigNum { } } - pub fn checked_mod_sqr(&self, n: &BigNum) -> Result { + pub fn checked_mod_sqr(&self, n: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_mod_sqr(r.raw(), self.raw(), n.raw(), ctx) == 1 @@ -169,7 +169,7 @@ impl BigNum { } } - pub fn checked_exp(&self, p: &BigNum) -> Result { + pub fn checked_exp(&self, p: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_exp(r.raw(), self.raw(), p.raw(), ctx) == 1 @@ -177,7 +177,7 @@ impl BigNum { } } - pub fn checked_mod_exp(&self, p: &BigNum, n: &BigNum) -> Result { + pub fn checked_mod_exp(&self, p: &BigNum, n: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_mod_exp(r.raw(), self.raw(), p.raw(), n.raw(), ctx) == 1 @@ -185,7 +185,7 @@ impl BigNum { } } - pub fn checked_mod_inv(&self, n: &BigNum) -> Result { + pub fn checked_mod_inv(&self, n: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { !ffi::BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx).is_null() @@ -193,59 +193,59 @@ impl BigNum { } } - pub fn add_word(&mut self, w: c_ulong) -> Result<(), SslError> { + pub fn add_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { unsafe { if ffi::BN_add_word(self.raw(), w) == 1 { Ok(()) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } } } - pub fn sub_word(&mut self, w: c_ulong) -> Result<(), SslError> { + pub fn sub_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { unsafe { if ffi::BN_sub_word(self.raw(), w) == 1 { Ok(()) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } } } - pub fn mul_word(&mut self, w: c_ulong) -> Result<(), SslError> { + pub fn mul_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { unsafe { if ffi::BN_mul_word(self.raw(), w) == 1 { Ok(()) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } } } - pub fn div_word(&mut self, w: c_ulong) -> Result { + pub fn div_word(&mut self, w: c_ulong) -> Result { unsafe { let result = ffi::BN_div_word(self.raw(), w); if result != !0 as c_ulong { Ok(result) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } } } - pub fn mod_word(&self, w: c_ulong) -> Result { + pub fn mod_word(&self, w: c_ulong) -> Result { unsafe { let result = ffi::BN_mod_word(self.raw(), w); if result != !0 as c_ulong { Ok(result) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } } } - pub fn checked_gcd(&self, a: &BigNum) -> Result { + pub fn checked_gcd(&self, a: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1 @@ -257,7 +257,7 @@ impl BigNum { safe: bool, add: Option<&BigNum>, rem: Option<&BigNum>) - -> Result { + -> Result { unsafe { with_bn_in_ctx!(r, ctx, { let add_arg = add.map(|a| a.raw()).unwrap_or(ptr::null_mut()); @@ -273,7 +273,7 @@ impl BigNum { } } - pub fn is_prime(&self, checks: i32) -> Result { + pub fn is_prime(&self, checks: i32) -> Result { unsafe { with_ctx!(ctx, { Ok(ffi::BN_is_prime_ex(self.raw(), checks as c_int, ctx, ptr::null()) == 1) @@ -281,7 +281,7 @@ impl BigNum { } } - pub fn is_prime_fast(&self, checks: i32, do_trial_division: bool) -> Result { + pub fn is_prime_fast(&self, checks: i32, do_trial_division: bool) -> Result { unsafe { with_ctx!(ctx, { Ok(ffi::BN_is_prime_fasttest_ex(self.raw(), @@ -293,7 +293,7 @@ impl BigNum { } } - pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result { + pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result { 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 @@ -304,7 +304,7 @@ impl BigNum { pub fn checked_new_pseudo_random(bits: i32, prop: RNGProperty, odd: bool) - -> Result { + -> Result { 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 @@ -312,7 +312,7 @@ impl BigNum { } } - pub fn checked_rand_in_range(&self) -> Result { + pub fn checked_rand_in_range(&self) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_rand_range(r.raw(), self.raw()) == 1 @@ -320,7 +320,7 @@ impl BigNum { } } - pub fn checked_pseudo_rand_in_range(&self) -> Result { + pub fn checked_pseudo_rand_in_range(&self) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_pseudo_rand_range(r.raw(), self.raw()) == 1 @@ -328,22 +328,22 @@ impl BigNum { } } - pub fn set_bit(&mut self, n: i32) -> Result<(), SslError> { + pub fn set_bit(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { if ffi::BN_set_bit(self.raw(), n as c_int) == 1 { Ok(()) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } } } - pub fn clear_bit(&mut self, n: i32) -> Result<(), SslError> { + pub fn clear_bit(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { if ffi::BN_clear_bit(self.raw(), n as c_int) == 1 { Ok(()) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } } } @@ -352,17 +352,17 @@ impl BigNum { unsafe { ffi::BN_is_bit_set(self.raw(), n as c_int) == 1 } } - pub fn mask_bits(&mut self, n: i32) -> Result<(), SslError> { + pub fn mask_bits(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { if ffi::BN_mask_bits(self.raw(), n as c_int) == 1 { Ok(()) } else { - Err(SslError::get()) + Err(ErrorStack::get()) } } } - pub fn checked_shl1(&self) -> Result { + pub fn checked_shl1(&self) -> Result { unsafe { with_bn!(r, { ffi::BN_lshift1(r.raw(), self.raw()) == 1 @@ -370,7 +370,7 @@ impl BigNum { } } - pub fn checked_shr1(&self) -> Result { + pub fn checked_shr1(&self) -> Result { unsafe { with_bn!(r, { ffi::BN_rshift1(r.raw(), self.raw()) == 1 @@ -378,7 +378,7 @@ impl BigNum { } } - pub fn checked_add(&self, a: &BigNum) -> Result { + pub fn checked_add(&self, a: &BigNum) -> Result { unsafe { with_bn!(r, { ffi::BN_add(r.raw(), self.raw(), a.raw()) == 1 @@ -386,7 +386,7 @@ impl BigNum { } } - pub fn checked_sub(&self, a: &BigNum) -> Result { + pub fn checked_sub(&self, a: &BigNum) -> Result { unsafe { with_bn!(r, { ffi::BN_sub(r.raw(), self.raw(), a.raw()) == 1 @@ -394,7 +394,7 @@ impl BigNum { } } - pub fn checked_mul(&self, a: &BigNum) -> Result { + pub fn checked_mul(&self, a: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_mul(r.raw(), self.raw(), a.raw(), ctx) == 1 @@ -402,7 +402,7 @@ impl BigNum { } } - pub fn checked_div(&self, a: &BigNum) -> Result { + pub fn checked_div(&self, a: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_div(r.raw(), ptr::null_mut(), self.raw(), a.raw(), ctx) == 1 @@ -410,7 +410,7 @@ impl BigNum { } } - pub fn checked_mod(&self, a: &BigNum) -> Result { + pub fn checked_mod(&self, a: &BigNum) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { ffi::BN_div(ptr::null_mut(), r.raw(), self.raw(), a.raw(), ctx) == 1 @@ -418,7 +418,7 @@ impl BigNum { } } - pub fn checked_shl(&self, a: &i32) -> Result { + pub fn checked_shl(&self, a: &i32) -> Result { unsafe { with_bn!(r, { ffi::BN_lshift(r.raw(), self.raw(), *a as c_int) == 1 @@ -426,7 +426,7 @@ impl BigNum { } } - pub fn checked_shr(&self, a: &i32) -> Result { + pub fn checked_shr(&self, a: &i32) -> Result { unsafe { with_bn!(r, { ffi::BN_rshift(r.raw(), self.raw(), *a as c_int) == 1 -- cgit v1.2.3 From 635bdb45a812c1434cf94aea7e8ce5abe1321774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Mon, 1 Aug 2016 22:22:39 +0200 Subject: BigNum binary operators with different lifetimes. --- openssl/src/bn/mod.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'openssl/src/bn') diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index 5054f0ab..ab0e0f2e 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -730,42 +730,42 @@ pub mod unchecked { use ffi; use super::BigNum; - impl<'a> Add<&'a BigNum> for &'a BigNum { + impl<'a, 'b> Add<&'b BigNum> for &'a BigNum { type Output = BigNum; - fn add(self, oth: &'a BigNum) -> BigNum { + fn add(self, oth: &'b BigNum) -> BigNum { self.checked_add(oth).unwrap() } } - impl<'a> Sub<&'a BigNum> for &'a BigNum { + impl<'a, 'b> Sub<&'b BigNum> for &'a BigNum { type Output = BigNum; - fn sub(self, oth: &'a BigNum) -> BigNum { + fn sub(self, oth: &'b BigNum) -> BigNum { self.checked_sub(oth).unwrap() } } - impl<'a> Mul<&'a BigNum> for &'a BigNum { + impl<'a, 'b> Mul<&'b BigNum> for &'a BigNum { type Output = BigNum; - fn mul(self, oth: &'a BigNum) -> BigNum { + fn mul(self, oth: &'b BigNum) -> BigNum { self.checked_mul(oth).unwrap() } } - impl<'a> Div<&'a BigNum> for &'a BigNum { + impl<'a, 'b> Div<&'b BigNum> for &'a BigNum { type Output = BigNum; - fn div(self, oth: &'a BigNum) -> BigNum { + fn div(self, oth: &'b BigNum) -> BigNum { self.checked_div(oth).unwrap() } } - impl<'a> Rem<&'a BigNum> for &'a BigNum { + impl<'a, 'b> Rem<&'b BigNum> for &'a BigNum { type Output = BigNum; - fn rem(self, oth: &'a BigNum) -> BigNum { + fn rem(self, oth: &'b BigNum) -> BigNum { self.checked_mod(oth).unwrap() } } -- cgit v1.2.3 From 05089bacb3cde8fe3ec9f68b5682668d4f63383c Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 7 Aug 2016 14:19:44 -0700 Subject: Refactor BigNum --- openssl/src/bn/mod.rs | 650 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 420 insertions(+), 230 deletions(-) (limited to 'openssl/src/bn') diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index d6febe51..3a1efb4a 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -2,17 +2,12 @@ use libc::{c_int, c_ulong, c_void}; use std::ffi::{CStr, CString}; use std::cmp::Ordering; 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 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`. #[derive(Copy, Clone)] #[repr(C)] @@ -79,68 +74,14 @@ macro_rules! with_bn_in_ctx( }); ); -impl BigNum { - /// Creates a new `BigNum` with the value 0. - pub fn new() -> Result { - unsafe { - ffi::init(); - - let v = try_ssl_null!(ffi::BN_new()); - Ok(BigNum(v)) - } - } - - /// Creates a new `BigNum` with the given value. - pub fn new_from(n: u64) -> Result { - 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::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) - }) - } +/// A borrowed, signed, arbitrary-precision integer. +#[derive(Copy, Clone)] +pub struct BigNumRef<'a>(*mut ffi::BIGNUM, PhantomData<&'a ()>); - /// Creates a `BigNum` from a hexadecimal string. - pub fn from_hex_str(s: &str) -> Result { - 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 { - 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::new().and_then(|v| unsafe { - try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw())); - Ok(v) - }) +impl<'a> BigNumRef<'a> { + pub unsafe fn from_handle(handle: *mut ffi::BIGNUM) -> BigNumRef<'a> { + BigNumRef(handle, PhantomData) } /// Returns the square of `self`. @@ -162,7 +103,7 @@ impl BigNum { } /// Returns the unsigned remainder of the division `self / n`. - pub fn checked_nnmod(&self, n: &BigNum) -> Result { + pub fn checked_nnmod(&self, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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); /// ``` - pub fn checked_mod_add(&self, a: &BigNum, n: &BigNum) -> Result { + pub fn checked_mod_add(&self, a: &BigNumRef, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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`. - pub fn checked_mod_sub(&self, a: &BigNum, n: &BigNum) -> Result { + pub fn checked_mod_sub(&self, a: &BigNumRef, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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`. - pub fn checked_mod_mul(&self, a: &BigNum, n: &BigNum) -> Result { + pub fn checked_mod_mul(&self, a: &BigNumRef, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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`. - pub fn checked_mod_sqr(&self, n: &BigNum) -> Result { + pub fn checked_mod_sqr(&self, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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. - pub fn checked_exp(&self, p: &BigNum) -> Result { + pub fn checked_exp(&self, p: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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`. - pub fn checked_mod_exp(&self, p: &BigNum, n: &BigNum) -> Result { + pub fn checked_mod_exp(&self, p: &BigNumRef, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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` /// such that `(self * r) % n == 1`. - pub fn checked_mod_inv(&self, n: &BigNum) -> Result { + pub fn checked_mod_inv(&self, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { !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`. - pub fn checked_gcd(&self, a: &BigNum) -> Result { + pub fn checked_gcd(&self, a: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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 { - 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. /// /// 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 { - 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 { - 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 /// `0 <= r < self`. pub fn checked_rand_in_range(&self) -> Result { @@ -495,7 +381,7 @@ impl BigNum { } } - pub fn checked_add(&self, a: &BigNum) -> Result { + pub fn checked_add(&self, a: &BigNumRef) -> Result { unsafe { with_bn!(r, { ffi::BN_add(r.raw(), self.raw(), a.raw()) == 1 @@ -503,7 +389,7 @@ impl BigNum { } } - pub fn checked_sub(&self, a: &BigNum) -> Result { + pub fn checked_sub(&self, a: &BigNumRef) -> Result { unsafe { with_bn!(r, { ffi::BN_sub(r.raw(), self.raw(), a.raw()) == 1 @@ -511,7 +397,7 @@ impl BigNum { } } - pub fn checked_mul(&self, a: &BigNum) -> Result { + pub fn checked_mul(&self, a: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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 { + pub fn checked_div(&self, a: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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 { + pub fn checked_mod(&self, a: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { 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 { + unsafe { + let r = try_ssl_null!(ffi::BN_dup(self.raw())); + Ok(BigNum::from_handle(r)) + } + } + /// Inverts the sign of `self`. /// /// ``` @@ -574,9 +467,9 @@ impl BigNum { /// let s = -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 { let res = ffi::BN_ucmp(self.raw(), oth.raw()) as i32; if res < 0 { @@ -603,19 +496,12 @@ impl BigNum { (self.num_bits() + 7) / 8 } - pub unsafe fn raw(&self) -> *mut ffi::BIGNUM { - let BigNum(n) = *self; - n - } - - pub unsafe fn raw_ptr(&self) -> *const *mut ffi::BIGNUM { - let BigNum(ref n) = *self; - n + pub fn raw(&self) -> *mut ffi::BIGNUM { + self.0 } - pub fn into_raw(self) -> *mut ffi::BIGNUM { - let mut me = self; - mem::replace(&mut me.0, ptr::null_mut()) + pub fn raw_ptr(&self) -> *const *mut ffi::BIGNUM { + &self.0 } /// 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 { + 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::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::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::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::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 { + 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 { + 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 { + 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> 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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.to_dec_str()) } } -impl Eq for BigNum {} +impl<'a> fmt::Display for BigNumRef<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + 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> for BigNumRef<'a> { + fn eq(&self, oth: &BigNumRef) -> bool { + unsafe { ffi::BN_cmp(self.raw(), oth.raw()) == 0 } + } +} + +impl<'a> PartialEq for BigNumRef<'a> { + fn eq(&self, oth: &BigNum) -> bool { + self.eq(oth.deref()) + } +} + +impl<'a> Eq for BigNumRef<'a> {} + impl PartialEq for BigNum { fn eq(&self, oth: &BigNum) -> bool { - unsafe { ffi::BN_cmp(self.raw(), oth.raw()) == 0 } + self.deref().eq(oth) } } -impl Ord for BigNum { - fn cmp(&self, oth: &BigNum) -> Ordering { - self.partial_cmp(oth).unwrap() +impl<'a> PartialEq> for BigNum { + fn eq(&self, oth: &BigNumRef) -> bool { + self.deref().eq(oth) + } +} + +impl Eq for BigNum {} + +impl<'a, 'b> PartialOrd> for BigNumRef<'a> { + fn partial_cmp(&self, oth: &BigNumRef) -> Option { + Some(self.cmp(oth)) + } +} + +impl<'a> PartialOrd for BigNumRef<'a> { + fn partial_cmp(&self, oth: &BigNum) -> Option { + 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 { fn partial_cmp(&self, oth: &BigNum) -> Option { - unsafe { - 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) - } + self.deref().partial_cmp(oth.deref()) } } -impl Drop for BigNum { - fn drop(&mut self) { - unsafe { - if !self.raw().is_null() { - ffi::BN_clear_free(self.raw()); - } - } +impl<'a> PartialOrd> for BigNum { + fn partial_cmp(&self, oth: &BigNumRef) -> Option { + self.deref().partial_cmp(oth) } } -#[doc(hidden)] // This module only contains impls, so it's empty when generating docs -pub mod unchecked { - use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub}; - use ffi; - use super::BigNum; +impl Ord for BigNum { + fn cmp(&self, oth: &BigNum) -> Ordering { + self.deref().cmp(oth.deref()) + } +} - impl<'a, 'b> Add<&'b BigNum> for &'a BigNum { - type Output = BigNum; +impl<'a, 'b> Add<&'b BigNumRef<'b>> for &'a BigNumRef<'a> { + type Output = BigNum; - fn add(self, oth: &'b BigNum) -> BigNum { - self.checked_add(oth).unwrap() - } + fn add(self, oth: &BigNumRef) -> BigNum { + self.checked_add(oth).unwrap() } +} - impl<'a, 'b> Sub<&'b BigNum> for &'a BigNum { - type Output = BigNum; +impl<'a, 'b> Sub<&'b BigNumRef<'b>> for &'a BigNumRef<'a> { + type Output = BigNum; - fn sub(self, oth: &'b BigNum) -> BigNum { - self.checked_sub(oth).unwrap() - } + fn sub(self, oth: &BigNumRef) -> BigNum { + self.checked_sub(oth).unwrap() } +} - impl<'a, 'b> Mul<&'b BigNum> for &'a BigNum { - type Output = BigNum; +impl<'a, 'b> Sub<&'b BigNum> for &'a BigNumRef<'a> { + type Output = BigNum; - fn mul(self, oth: &'b BigNum) -> BigNum { - self.checked_mul(oth).unwrap() - } + fn sub(self, oth: &BigNum) -> BigNum { + self.checked_sub(oth).unwrap() } +} - impl<'a, 'b> Div<&'b BigNum> for &'a BigNum { - type Output = BigNum; +impl<'a, 'b> Sub<&'b BigNum> for &'a BigNum { + type Output = BigNum; - fn div(self, oth: &'b BigNum) -> BigNum { - self.checked_div(oth).unwrap() - } + fn sub(self, oth: &BigNum) -> BigNum { + self.checked_sub(oth).unwrap() } +} - impl<'a, 'b> Rem<&'b BigNum> for &'a BigNum { - type Output = BigNum; +impl<'a, 'b> Sub<&'b BigNumRef<'b>> for &'a BigNum { + type Output = BigNum; - fn rem(self, oth: &'b BigNum) -> BigNum { - self.checked_mod(oth).unwrap() - } + fn sub(self, oth: &BigNumRef) -> BigNum { + self.checked_sub(oth).unwrap() } +} - impl<'a> Shl for &'a BigNum { - type Output = BigNum; +impl<'a, 'b> Mul<&'b BigNumRef<'b>> for &'a BigNumRef<'a> { + type Output = BigNum; - fn shl(self, n: i32) -> BigNum { - self.checked_shl(&n).unwrap() - } + fn mul(self, oth: &BigNumRef) -> BigNum { + self.checked_mul(oth).unwrap() } +} - impl<'a> Shr for &'a BigNum { - type Output = BigNum; +impl<'a, 'b> Mul<&'b BigNum> for &'a BigNumRef<'a> { + type Output = BigNum; - fn shr(self, n: i32) -> BigNum { - self.checked_shr(&n).unwrap() - } + fn mul(self, oth: &BigNum) -> BigNum { + self.checked_mul(oth).unwrap() } +} - impl Clone for BigNum { - fn clone(&self) -> BigNum { - unsafe { - let r = ffi::BN_dup(self.raw()); - if r.is_null() { - panic!("Unexpected null pointer from BN_dup(..)") - } else { - BigNum(r) - } - } - } +impl<'a, 'b> Mul<&'b BigNum> for &'a BigNum { + type Output = BigNum; + + fn mul(self, oth: &BigNum) -> BigNum { + self.checked_mul(oth).unwrap() } +} - impl Neg for BigNum { - type Output = BigNum; +impl<'a, 'b> Mul<&'b BigNumRef<'b>> for &'a BigNum { + type Output = BigNum; - fn neg(self) -> BigNum { - let mut n = self.clone(); - n.negate(); - n - } + 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 for &'a BigNumRef<'a> { + type Output = BigNum; + + fn shl(self, n: i32) -> BigNum { + self.checked_shl(&n).unwrap() + } +} + +impl<'a> Shl for &'a BigNum { + type Output = BigNum; + + fn shl(self, n: i32) -> BigNum { + self.checked_shl(&n).unwrap() + } +} + +impl<'a> Shr for &'a BigNumRef<'a> { + type Output = BigNum; + + fn shr(self, n: i32) -> BigNum { + self.checked_shr(&n).unwrap() + } +} + +impl<'a> Shr 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 } } -- cgit v1.2.3 From b56908a3924f68c86ba4a5686c670182ef12c6e4 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 7 Aug 2016 17:48:18 -0700 Subject: Take a c_ulong directly in BN construction Closes #416 --- openssl/src/bn/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'openssl/src/bn') diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index 3a1efb4a..254e674e 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -584,9 +584,9 @@ impl BigNum { } /// Creates a new `BigNum` with the given value. - pub fn new_from(n: u64) -> Result { + pub fn new_from(n: c_ulong) -> Result { BigNum::new().and_then(|v| unsafe { - try_ssl!(ffi::BN_set_word(v.raw(), n as c_ulong)); + try_ssl!(ffi::BN_set_word(v.raw(), n)); Ok(v) }) } -- cgit v1.2.3 From 6091c674c99fb80baef79647d8e131064a4887a0 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 7 Aug 2016 17:52:13 -0700 Subject: Fix bn tests on 32 bit --- openssl/src/bn/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'openssl/src/bn') diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index 254e674e..a933157c 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -1006,7 +1006,7 @@ mod tests { #[test] fn test_to_from_slice() { - let v0 = BigNum::new_from(10203004_u64).unwrap(); + let v0 = BigNum::new_from(10203004).unwrap(); let vec = v0.to_vec(); let v1 = BigNum::new_from_slice(&vec).unwrap(); @@ -1015,7 +1015,7 @@ mod tests { #[test] fn test_negation() { - let a = BigNum::new_from(909829283_u64).unwrap(); + let a = BigNum::new_from(909829283).unwrap(); assert!(!a.is_negative()); assert!((-a).is_negative()); @@ -1024,7 +1024,7 @@ mod tests { #[test] fn test_prime_numbers() { - let a = BigNum::new_from(19029017_u64).unwrap(); + let a = BigNum::new_from(19029017).unwrap(); let p = BigNum::checked_generate_prime(128, true, None, Some(&a)).unwrap(); assert!(p.is_prime(100).unwrap()); -- cgit v1.2.3 From c4e7743c57bde2d25b8a14521c48bba85fe72e68 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 10 Aug 2016 20:51:06 -0700 Subject: Asn1 and Bignum renames --- openssl/src/bn/mod.rs | 122 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 63 deletions(-) (limited to 'openssl/src/bn') diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index a933157c..2f751037 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -80,7 +80,7 @@ pub struct BigNumRef<'a>(*mut ffi::BIGNUM, PhantomData<&'a ()>); impl<'a> BigNumRef<'a> { - pub unsafe fn from_handle(handle: *mut ffi::BIGNUM) -> BigNumRef<'a> { + pub unsafe fn from_ptr(handle: *mut ffi::BIGNUM) -> BigNumRef<'a> { BigNumRef(handle, PhantomData) } @@ -97,7 +97,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_sqr(&self) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_sqr(r.raw(), self.raw(), ctx) == 1 + ffi::BN_sqr(r.as_ptr(), self.as_ptr(), ctx) == 1 }) } } @@ -106,7 +106,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_nnmod(&self, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_nnmod(r.raw(), self.raw(), n.raw(), ctx) == 1 + ffi::BN_nnmod(r.as_ptr(), self.as_ptr(), n.as_ptr(), ctx) == 1 }) } } @@ -125,7 +125,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_mod_add(&self, a: &BigNumRef, n: &BigNumRef) -> Result { unsafe { 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.as_ptr(), self.as_ptr(), a.as_ptr(), n.as_ptr(), ctx) == 1 }) } } @@ -134,7 +134,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_mod_sub(&self, a: &BigNumRef, n: &BigNumRef) -> Result { unsafe { 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.as_ptr(), self.as_ptr(), a.as_ptr(), n.as_ptr(), ctx) == 1 }) } } @@ -143,7 +143,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_mod_mul(&self, a: &BigNumRef, n: &BigNumRef) -> Result { unsafe { 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.as_ptr(), self.as_ptr(), a.as_ptr(), n.as_ptr(), ctx) == 1 }) } } @@ -152,7 +152,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_mod_sqr(&self, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_mod_sqr(r.raw(), self.raw(), n.raw(), ctx) == 1 + ffi::BN_mod_sqr(r.as_ptr(), self.as_ptr(), n.as_ptr(), ctx) == 1 }) } } @@ -161,7 +161,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_exp(&self, p: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_exp(r.raw(), self.raw(), p.raw(), ctx) == 1 + ffi::BN_exp(r.as_ptr(), self.as_ptr(), p.as_ptr(), ctx) == 1 }) } } @@ -170,7 +170,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_mod_exp(&self, p: &BigNumRef, n: &BigNumRef) -> Result { unsafe { 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.as_ptr(), self.as_ptr(), p.as_ptr(), n.as_ptr(), ctx) == 1 }) } } @@ -180,7 +180,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_mod_inv(&self, n: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - !ffi::BN_mod_inverse(r.raw(), self.raw(), n.raw(), ctx).is_null() + !ffi::BN_mod_inverse(r.as_ptr(), self.as_ptr(), n.as_ptr(), ctx).is_null() }) } } @@ -188,7 +188,7 @@ impl<'a> BigNumRef<'a> { /// Add an `unsigned long` to `self`. This is more efficient than adding a `BigNum`. pub fn add_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_add_word(self.raw(), w) == 1 { + if ffi::BN_add_word(self.as_ptr(), w) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -198,7 +198,7 @@ impl<'a> BigNumRef<'a> { pub fn sub_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_sub_word(self.raw(), w) == 1 { + if ffi::BN_sub_word(self.as_ptr(), w) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -208,7 +208,7 @@ impl<'a> BigNumRef<'a> { pub fn mul_word(&mut self, w: c_ulong) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_mul_word(self.raw(), w) == 1 { + if ffi::BN_mul_word(self.as_ptr(), w) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -218,7 +218,7 @@ impl<'a> BigNumRef<'a> { pub fn div_word(&mut self, w: c_ulong) -> Result { unsafe { - let result = ffi::BN_div_word(self.raw(), w); + let result = ffi::BN_div_word(self.as_ptr(), w); if result != !0 as c_ulong { Ok(result) } else { @@ -229,7 +229,7 @@ impl<'a> BigNumRef<'a> { pub fn mod_word(&self, w: c_ulong) -> Result { unsafe { - let result = ffi::BN_mod_word(self.raw(), w); + let result = ffi::BN_mod_word(self.as_ptr(), w); if result != !0 as c_ulong { Ok(result) } else { @@ -242,7 +242,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_gcd(&self, a: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_gcd(r.raw(), self.raw(), a.raw(), ctx) == 1 + ffi::BN_gcd(r.as_ptr(), self.as_ptr(), a.as_ptr(), ctx) == 1 }) } } @@ -257,7 +257,7 @@ impl<'a> BigNumRef<'a> { pub fn is_prime(&self, checks: i32) -> Result { unsafe { with_ctx!(ctx, { - Ok(ffi::BN_is_prime_ex(self.raw(), checks as c_int, ctx, ptr::null()) == 1) + Ok(ffi::BN_is_prime_ex(self.as_ptr(), checks as c_int, ctx, ptr::null()) == 1) }) } } @@ -274,7 +274,7 @@ impl<'a> BigNumRef<'a> { pub fn is_prime_fast(&self, checks: i32, do_trial_division: bool) -> Result { unsafe { with_ctx!(ctx, { - Ok(ffi::BN_is_prime_fasttest_ex(self.raw(), + Ok(ffi::BN_is_prime_fasttest_ex(self.as_ptr(), checks as c_int, ctx, do_trial_division as c_int, @@ -288,7 +288,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_rand_in_range(&self) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_rand_range(r.raw(), self.raw()) == 1 + ffi::BN_rand_range(r.as_ptr(), self.as_ptr()) == 1 }) } } @@ -297,7 +297,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_pseudo_rand_in_range(&self) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_pseudo_rand_range(r.raw(), self.raw()) == 1 + ffi::BN_pseudo_rand_range(r.as_ptr(), self.as_ptr()) == 1 }) } } @@ -307,7 +307,7 @@ impl<'a> BigNumRef<'a> { /// When setting a bit outside of `self`, it is expanded. pub fn set_bit(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_set_bit(self.raw(), n as c_int) == 1 { + if ffi::BN_set_bit(self.as_ptr(), n as c_int) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -320,7 +320,7 @@ impl<'a> BigNumRef<'a> { /// When clearing a bit outside of `self`, an error is returned. pub fn clear_bit(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_clear_bit(self.raw(), n as c_int) == 1 { + if ffi::BN_clear_bit(self.as_ptr(), n as c_int) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -330,7 +330,7 @@ impl<'a> BigNumRef<'a> { /// Returns `true` if the `n`th bit of `self` is set to 1, `false` otherwise. pub fn is_bit_set(&self, n: i32) -> bool { - unsafe { ffi::BN_is_bit_set(self.raw(), n as c_int) == 1 } + unsafe { ffi::BN_is_bit_set(self.as_ptr(), n as c_int) == 1 } } /// Truncates `self` to the lowest `n` bits. @@ -338,7 +338,7 @@ impl<'a> BigNumRef<'a> { /// An error occurs if `self` is already shorter than `n` bits. pub fn mask_bits(&mut self, n: i32) -> Result<(), ErrorStack> { unsafe { - if ffi::BN_mask_bits(self.raw(), n as c_int) == 1 { + if ffi::BN_mask_bits(self.as_ptr(), n as c_int) == 1 { Ok(()) } else { Err(ErrorStack::get()) @@ -367,7 +367,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_shl1(&self) -> Result { unsafe { with_bn!(r, { - ffi::BN_lshift1(r.raw(), self.raw()) == 1 + ffi::BN_lshift1(r.as_ptr(), self.as_ptr()) == 1 }) } } @@ -376,7 +376,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_shr1(&self) -> Result { unsafe { with_bn!(r, { - ffi::BN_rshift1(r.raw(), self.raw()) == 1 + ffi::BN_rshift1(r.as_ptr(), self.as_ptr()) == 1 }) } } @@ -384,7 +384,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_add(&self, a: &BigNumRef) -> Result { unsafe { with_bn!(r, { - ffi::BN_add(r.raw(), self.raw(), a.raw()) == 1 + ffi::BN_add(r.as_ptr(), self.as_ptr(), a.as_ptr()) == 1 }) } } @@ -392,7 +392,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_sub(&self, a: &BigNumRef) -> Result { unsafe { with_bn!(r, { - ffi::BN_sub(r.raw(), self.raw(), a.raw()) == 1 + ffi::BN_sub(r.as_ptr(), self.as_ptr(), a.as_ptr()) == 1 }) } } @@ -400,7 +400,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_mul(&self, a: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_mul(r.raw(), self.raw(), a.raw(), ctx) == 1 + ffi::BN_mul(r.as_ptr(), self.as_ptr(), a.as_ptr(), ctx) == 1 }) } } @@ -408,7 +408,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_div(&self, a: &BigNumRef) -> Result { unsafe { with_bn_in_ctx!(r, ctx, { - ffi::BN_div(r.raw(), ptr::null_mut(), self.raw(), a.raw(), ctx) == 1 + ffi::BN_div(r.as_ptr(), ptr::null_mut(), self.as_ptr(), a.as_ptr(), ctx) == 1 }) } } @@ -416,7 +416,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_mod(&self, a: &BigNumRef) -> Result { unsafe { 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.as_ptr(), self.as_ptr(), a.as_ptr(), ctx) == 1 }) } } @@ -424,7 +424,7 @@ impl<'a> BigNumRef<'a> { pub fn checked_shl(&self, a: &i32) -> Result { unsafe { with_bn!(r, { - ffi::BN_lshift(r.raw(), self.raw(), *a as c_int) == 1 + ffi::BN_lshift(r.as_ptr(), self.as_ptr(), *a as c_int) == 1 }) } } @@ -432,15 +432,15 @@ impl<'a> BigNumRef<'a> { pub fn checked_shr(&self, a: &i32) -> Result { unsafe { with_bn!(r, { - ffi::BN_rshift(r.raw(), self.raw(), *a as c_int) == 1 + ffi::BN_rshift(r.as_ptr(), self.as_ptr(), *a as c_int) == 1 }) } } pub fn to_owned(&self) -> Result { unsafe { - let r = try_ssl_null!(ffi::BN_dup(self.raw())); - Ok(BigNum::from_handle(r)) + let r = try_ssl_null!(ffi::BN_dup(self.as_ptr())); + Ok(BigNum::from_ptr(r)) } } @@ -456,7 +456,7 @@ impl<'a> BigNumRef<'a> { /// assert_eq!(s, BigNum::new_from(8).unwrap()); /// ``` pub fn negate(&mut self) { - unsafe { ffi::BN_set_negative(self.raw(), !self.is_negative() as c_int) } + unsafe { ffi::BN_set_negative(self.as_ptr(), !self.is_negative() as c_int) } } /// Compare the absolute values of `self` and `oth`. @@ -471,7 +471,7 @@ impl<'a> BigNumRef<'a> { /// ``` pub fn abs_cmp(&self, oth: &BigNumRef) -> Ordering { unsafe { - let res = ffi::BN_ucmp(self.raw(), oth.raw()) as i32; + let res = ffi::BN_ucmp(self.as_ptr(), oth.as_ptr()) as i32; if res < 0 { Ordering::Less } else if res > 0 { @@ -483,12 +483,12 @@ impl<'a> BigNumRef<'a> { } pub fn is_negative(&self) -> bool { - unsafe { (*self.raw()).neg == 1 } + unsafe { (*self.as_ptr()).neg == 1 } } /// Returns the number of significant bits in `self`. pub fn num_bits(&self) -> i32 { - unsafe { ffi::BN_num_bits(self.raw()) as i32 } + unsafe { ffi::BN_num_bits(self.as_ptr()) as i32 } } /// Returns the size of `self` in bytes. @@ -496,14 +496,10 @@ impl<'a> BigNumRef<'a> { (self.num_bits() + 7) / 8 } - pub fn raw(&self) -> *mut ffi::BIGNUM { + pub fn as_ptr(&self) -> *mut ffi::BIGNUM { self.0 } - pub fn raw_ptr(&self) -> *const *mut ffi::BIGNUM { - &self.0 - } - /// Returns a big-endian byte vector representation of the absolute value of `self`. /// /// `self` can be recreated by using `new_from_slice`. @@ -520,7 +516,7 @@ impl<'a> BigNumRef<'a> { let size = self.num_bytes() as usize; let mut v = Vec::with_capacity(size); unsafe { - ffi::BN_bn2bin(self.raw(), v.as_mut_ptr()); + ffi::BN_bn2bin(self.as_ptr(), v.as_mut_ptr()); v.set_len(size); } v @@ -536,7 +532,7 @@ impl<'a> BigNumRef<'a> { /// ``` pub fn to_dec_str(&self) -> String { unsafe { - let buf = ffi::BN_bn2dec(self.raw()); + let buf = ffi::BN_bn2dec(self.as_ptr()); assert!(!buf.is_null()); let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec()) .unwrap(); @@ -555,7 +551,7 @@ impl<'a> BigNumRef<'a> { /// ``` pub fn to_hex_str(&self) -> String { unsafe { - let buf = ffi::BN_bn2hex(self.raw()); + let buf = ffi::BN_bn2hex(self.as_ptr()); assert!(!buf.is_null()); let str = String::from_utf8(CStr::from_ptr(buf as *const _).to_bytes().to_vec()) .unwrap(); @@ -579,14 +575,14 @@ impl BigNum { unsafe { ffi::init(); let v = try_ssl_null!(ffi::BN_new()); - Ok(BigNum::from_handle(v)) + Ok(BigNum::from_ptr(v)) } } /// Creates a new `BigNum` with the given value. pub fn new_from(n: c_ulong) -> Result { BigNum::new().and_then(|v| unsafe { - try_ssl!(ffi::BN_set_word(v.raw(), n)); + try_ssl!(ffi::BN_set_word(v.as_ptr(), n)); Ok(v) }) } @@ -595,7 +591,7 @@ impl BigNum { pub fn from_dec_str(s: &str) -> Result { 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 _)); + try_ssl!(ffi::BN_dec2bn(&(v.0).0, c_str.as_ptr() as *const _)); Ok(v) }) } @@ -604,13 +600,13 @@ impl BigNum { pub fn from_hex_str(s: &str) -> Result { 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 _)); + try_ssl!(ffi::BN_hex2bn(&(v.0).0, c_str.as_ptr() as *const _)); Ok(v) }) } - pub unsafe fn from_handle(handle: *mut ffi::BIGNUM) -> BigNum { - BigNum(BigNumRef::from_handle(handle)) + pub unsafe fn from_ptr(handle: *mut ffi::BIGNUM) -> BigNum { + BigNum(BigNumRef::from_ptr(handle)) } /// Creates a new `BigNum` from an unsigned, big-endian encoded number of arbitrary length. @@ -623,7 +619,7 @@ impl BigNum { /// ``` pub fn new_from_slice(n: &[u8]) -> Result { BigNum::new().and_then(|v| unsafe { - try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw())); + try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.as_ptr())); Ok(v) }) } @@ -642,10 +638,10 @@ impl BigNum { -> Result { 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()); + let add_arg = add.map(|a| a.as_ptr()).unwrap_or(ptr::null_mut()); + let rem_arg = rem.map(|r| r.as_ptr()).unwrap_or(ptr::null_mut()); - ffi::BN_generate_prime_ex(r.raw(), + ffi::BN_generate_prime_ex(r.as_ptr(), bits as c_int, safe as c_int, add_arg, @@ -665,7 +661,7 @@ impl BigNum { pub fn checked_new_random(bits: i32, prop: RNGProperty, odd: bool) -> Result { 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 + ffi::BN_rand(r.as_ptr(), bits as c_int, prop as c_int, odd as c_int) == 1 }) } } @@ -677,13 +673,13 @@ impl BigNum { -> Result { 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 + ffi::BN_pseudo_rand(r.as_ptr(), 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(); + let ptr = self.as_ptr(); mem::forget(self); ptr } @@ -691,7 +687,7 @@ impl BigNum { impl Drop for BigNum { fn drop(&mut self) { - unsafe { ffi::BN_clear_free(self.raw()); } + unsafe { ffi::BN_clear_free(self.as_ptr()); } } } @@ -741,7 +737,7 @@ impl fmt::Display for BigNum { impl<'a, 'b> PartialEq> for BigNumRef<'a> { fn eq(&self, oth: &BigNumRef) -> bool { - unsafe { ffi::BN_cmp(self.raw(), oth.raw()) == 0 } + unsafe { ffi::BN_cmp(self.as_ptr(), oth.as_ptr()) == 0 } } } @@ -781,7 +777,7 @@ impl<'a> PartialOrd for BigNumRef<'a> { impl<'a> Ord for BigNumRef<'a> { fn cmp(&self, oth: &BigNumRef) -> Ordering { - unsafe { ffi::BN_cmp(self.raw(), oth.raw()).cmp(&0) } + unsafe { ffi::BN_cmp(self.as_ptr(), oth.as_ptr()).cmp(&0) } } } -- cgit v1.2.3 From 5e6b8e68fdcc94c6d7a931925bb32b145caeb3db Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 10 Aug 2016 21:07:41 -0700 Subject: More API cleanup --- openssl/src/bn/mod.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'openssl/src/bn') diff --git a/openssl/src/bn/mod.rs b/openssl/src/bn/mod.rs index 2f751037..de9d0d2a 100644 --- a/openssl/src/bn/mod.rs +++ b/openssl/src/bn/mod.rs @@ -1,7 +1,7 @@ use libc::{c_int, c_ulong, c_void}; use std::ffi::{CStr, CString}; use std::cmp::Ordering; -use std::{fmt, ptr, mem}; +use std::{fmt, ptr}; use std::marker::PhantomData; use std::ops::{Add, Div, Mul, Neg, Rem, Shl, Shr, Sub, Deref, DerefMut}; @@ -677,12 +677,6 @@ impl BigNum { }) } } - - pub fn into_raw(self) -> *mut ffi::BIGNUM { - let ptr = self.as_ptr(); - mem::forget(self); - ptr - } } impl Drop for BigNum { -- cgit v1.2.3