Struct openssl::bn::BigNumRef

source ·
pub struct BigNumRef(/* private fields */);
Expand description

Reference to a BigNum

Implementations§

source§

impl BigNumRef

source

pub fn clear(&mut self)

Erases the memory used by this BigNum, resetting its value to 0.

This can be used to destroy sensitive data such as keys when they are no longer needed.

This corresponds to BN_clear.

source

pub fn add_word(&mut self, w: u32) -> Result<(), ErrorStack>

Adds a u32 to self.

This corresponds to BN_add_word.

source

pub fn sub_word(&mut self, w: u32) -> Result<(), ErrorStack>

Subtracts a u32 from self.

This corresponds to BN_sub_word.

source

pub fn mul_word(&mut self, w: u32) -> Result<(), ErrorStack>

Multiplies a u32 by self.

This corresponds to BN_mul_word.

source

pub fn div_word(&mut self, w: u32) -> Result<u64, ErrorStack>

Divides self by a u32, returning the remainder.

This corresponds to BN_div_word.

source

pub fn mod_word(&self, w: u32) -> Result<u64, ErrorStack>

Returns the result of self modulo w.

This corresponds to BN_mod_word.

source

pub fn rand_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack>

Places a cryptographically-secure pseudo-random nonnegative number less than self in rnd.

This corresponds to BN_rand_range.

source

pub fn pseudo_rand_range(&self, rnd: &mut BigNumRef) -> Result<(), ErrorStack>

The cryptographically weak counterpart to rand_in_range.

This corresponds to BN_pseudo_rand_range.

source

pub fn set_bit(&mut self, n: i32) -> Result<(), ErrorStack>

Sets bit n. Equivalent to self |= (1 << n).

When setting a bit outside of self, it is expanded.

This corresponds to BN_set_bit.

source

pub fn clear_bit(&mut self, n: i32) -> Result<(), ErrorStack>

Clears bit n, setting it to 0. Equivalent to self &= ~(1 << n).

When clearing a bit outside of self, an error is returned.

This corresponds to BN_clear_bit.

source

pub fn is_bit_set(&self, n: i32) -> bool

Returns true if the nth bit of self is set to 1, false otherwise.

This corresponds to BN_is_bit_set.

source

pub fn mask_bits(&mut self, n: i32) -> Result<(), ErrorStack>

Truncates self to the lowest n bits.

An error occurs if self is already shorter than n bits.

This corresponds to BN_mask_bits.

source

pub fn lshift1(&mut self, a: &BigNumRef) -> Result<(), ErrorStack>

Places a << 1 in self. Equivalent to self * 2.

This corresponds to BN_lshift1.

source

pub fn rshift1(&mut self, a: &BigNumRef) -> Result<(), ErrorStack>

Places a >> 1 in self. Equivalent to self / 2.

This corresponds to BN_rshift1.

source

pub fn checked_add( &mut self, a: &BigNumRef, b: &BigNumRef, ) -> Result<(), ErrorStack>

Places a + b in self. core::ops::Add is also implemented for BigNumRef.

This corresponds to BN_add.

source

pub fn checked_sub( &mut self, a: &BigNumRef, b: &BigNumRef, ) -> Result<(), ErrorStack>

Places a - b in self. core::ops::Sub is also implemented for BigNumRef.

This corresponds to BN_sub.

source

pub fn lshift(&mut self, a: &BigNumRef, n: i32) -> Result<(), ErrorStack>

Places a << n in self. Equivalent to a * 2 ^ n.

This corresponds to BN_lshift.

source

pub fn rshift(&mut self, a: &BigNumRef, n: i32) -> Result<(), ErrorStack>

Places a >> n in self. Equivalent to a / 2 ^ n.

This corresponds to BN_rshift.

source

pub fn to_owned(&self) -> Result<BigNum, ErrorStack>

Creates a new BigNum with the same value.

This corresponds to BN_dup.

source

pub fn set_negative(&mut self, negative: bool)

Sets the sign of self. Pass true to set self to a negative. False sets self positive.

This corresponds to BN_set_negative.

source

pub fn ucmp(&self, oth: &BigNumRef) -> Ordering

Compare the absolute values of self and oth.

§Examples
let s = -BigNum::from_u32(8).unwrap();
let o = BigNum::from_u32(8).unwrap();

assert_eq!(s.ucmp(&o), Ordering::Equal);

This corresponds to BN_ucmp.

source

pub fn is_negative(&self) -> bool

Returns true if self is negative.

This corresponds to BN_is_negative.

source

pub fn is_even(&self) -> bool

Returns true is self is even.

This corresponds to BN_is_even.

source

pub fn is_odd(&self) -> bool

Returns true is self is odd.

This corresponds to BN_is_odd.

source

pub fn num_bits(&self) -> i32

Returns the number of significant bits in self.

This corresponds to BN_num_bits.

source

pub fn num_bytes(&self) -> i32

Returns the size of self in bytes. Implemented natively.

source

pub fn rand( &mut self, bits: i32, msb: MsbOption, odd: bool, ) -> Result<(), ErrorStack>

Generates a cryptographically strong pseudo-random BigNum, placing it in self.

§Parameters
  • bits: Length of the number in bits.
  • msb: The desired properties of the most significant bit. See constants.
  • odd: If true, the generated number will be odd.
§Examples
use openssl::bn::{BigNum, MsbOption};
use openssl::error::ErrorStack;

fn generate_random() -> Result< BigNum, ErrorStack > {
   let mut big = BigNum::new()?;

   // Generates a 128-bit odd random number
   big.rand(128, MsbOption::MAYBE_ZERO, true);
   Ok((big))
}

This corresponds to BN_rand.

source

pub fn pseudo_rand( &mut self, bits: i32, msb: MsbOption, odd: bool, ) -> Result<(), ErrorStack>

The cryptographically weak counterpart to rand. Not suitable for key generation.

This corresponds to BN_pseudo_rand.

source

pub fn generate_prime( &mut self, bits: i32, safe: bool, add: Option<&BigNumRef>, rem: Option<&BigNumRef>, ) -> Result<(), ErrorStack>

Generates a prime number, placing it in self.

§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).
§Examples
use openssl::bn::BigNum;
use openssl::error::ErrorStack;

fn generate_weak_prime() -> Result< BigNum, ErrorStack > {
   let mut big = BigNum::new()?;

   // Generates a 128-bit simple prime number
   big.generate_prime(128, false, None, None);
   Ok((big))
}

This corresponds to BN_generate_prime_ex.

source

pub fn checked_mul( &mut self, a: &BigNumRef, b: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of a * b in self. core::ops::Mul is also implemented for BigNumRef.

This corresponds to BN_mul.

source

pub fn checked_div( &mut self, a: &BigNumRef, b: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of a / b in self. The remainder is discarded. core::ops::Div is also implemented for BigNumRef.

This corresponds to BN_div.

source

pub fn checked_rem( &mut self, a: &BigNumRef, b: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of a % b in self.

This corresponds to BN_div.

source

pub fn div_rem( &mut self, rem: &mut BigNumRef, a: &BigNumRef, b: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of a / b in self and a % b in rem.

This corresponds to BN_div.

source

pub fn sqr( &mut self, a: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of in self.

This corresponds to BN_sqr.

source

pub fn nnmod( &mut self, a: &BigNumRef, m: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of a mod m in self. As opposed to div_rem the result is non-negative.

This corresponds to BN_nnmod.

source

pub fn mod_add( &mut self, a: &BigNumRef, b: &BigNumRef, m: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of (a + b) mod m in self.

This corresponds to BN_mod_add.

source

pub fn mod_sub( &mut self, a: &BigNumRef, b: &BigNumRef, m: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of (a - b) mod m in self.

This corresponds to BN_mod_sub.

source

pub fn mod_mul( &mut self, a: &BigNumRef, b: &BigNumRef, m: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of (a * b) mod m in self.

This corresponds to BN_mod_mul.

source

pub fn mod_sqr( &mut self, a: &BigNumRef, m: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of a² mod m in self.

This corresponds to BN_mod_sqr.

source

pub fn mod_sqrt( &mut self, a: &BigNumRef, p: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places into self the modular square root of a such that self^2 = a (mod p)

This corresponds to BN_mod_sqrt.

source

pub fn exp( &mut self, a: &BigNumRef, p: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of a^p in self.

This corresponds to BN_exp.

source

pub fn mod_exp( &mut self, a: &BigNumRef, p: &BigNumRef, m: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the result of a^p mod m in self.

This corresponds to BN_mod_exp.

source

pub fn mod_inverse( &mut self, a: &BigNumRef, n: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the inverse of a modulo n in self.

This corresponds to BN_mod_inverse.

source

pub fn gcd( &mut self, a: &BigNumRef, b: &BigNumRef, ctx: &mut BigNumContextRef, ) -> Result<(), ErrorStack>

Places the greatest common denominator of a and b in self.

This corresponds to BN_gcd.

source

pub fn is_prime( &self, checks: i32, ctx: &mut BigNumContextRef, ) -> Result<bool, ErrorStack>

Checks whether self is prime.

Performs a Miller-Rabin probabilistic primality test with checks iterations.

§Return Value

Returns true if self is prime with an error probability of less than 0.25 ^ checks.

This corresponds to BN_is_prime_ex.

source

pub fn is_prime_fasttest( &self, checks: i32, ctx: &mut BigNumContextRef, do_trial_division: bool, ) -> Result<bool, ErrorStack>

Checks whether self is prime with optional trial division.

If do_trial_division is true, first performs trial division by a number of small primes. Then, like is_prime, performs a Miller-Rabin probabilistic primality test with checks iterations.

§Return Value

Returns true if self is prime with an error probability of less than 0.25 ^ checks.

This corresponds to BN_is_prime_fasttest_ex.

source

pub fn to_vec(&self) -> Vec<u8>

Returns a big-endian byte vector representation of the absolute value of self.

self can be recreated by using from_slice.

let s = -BigNum::from_u32(4543).unwrap();
let r = BigNum::from_u32(4543).unwrap();

let s_vec = s.to_vec();
assert_eq!(BigNum::from_slice(&s_vec).unwrap(), r);

This corresponds to BN_bn2bin.

source

pub fn to_vec_padded(&self, pad_to: i32) -> Result<Vec<u8>, ErrorStack>

Returns a big-endian byte vector representation of the absolute value of self padded to pad_to bytes.

If pad_to is less than self.num_bytes() then an error is returned.

self can be recreated by using from_slice.

let bn = BigNum::from_u32(0x4543).unwrap();

let bn_vec = bn.to_vec_padded(4).unwrap();
assert_eq!(&bn_vec, &[0, 0, 0x45, 0x43]);

let r = bn.to_vec_padded(1);
assert!(r.is_err());

let bn = -BigNum::from_u32(0x4543).unwrap();
let bn_vec = bn.to_vec_padded(4).unwrap();
assert_eq!(&bn_vec, &[0, 0, 0x45, 0x43]);

This corresponds to BN_bn2binpad.

source

pub fn to_dec_str(&self) -> Result<OpensslString, ErrorStack>

Returns a decimal string representation of self.

let s = -BigNum::from_u32(12345).unwrap();

assert_eq!(&**s.to_dec_str().unwrap(), "-12345");

This corresponds to BN_bn2dec.

source

pub fn to_hex_str(&self) -> Result<OpensslString, ErrorStack>

Returns a hexadecimal string representation of self.

let s = -BigNum::from_u32(0x99ff).unwrap();

assert_eq!(s.to_hex_str().unwrap().to_uppercase(), "-99FF");

This corresponds to BN_bn2hex.

source

pub fn to_asn1_integer(&self) -> Result<Asn1Integer, ErrorStack>

Returns an Asn1Integer containing the value of self.

This corresponds to BN_to_ASN1_INTEGER.

source

pub fn set_const_time(&mut self)

Force constant time computation on this value.

This corresponds to BN_set_flags.

source

pub fn is_const_time(&self) -> bool

Returns true if self is in const time mode.

This corresponds to BN_get_flags.

source

pub fn is_secure(&self) -> bool

Returns true if self was created with BigNum::new_secure.

This corresponds to BN_get_flags.

Trait Implementations§

source§

impl<'a, 'b> Add<&'b BigNum> for &'a BigNumRef

§

type Output = BigNum

The resulting type after applying the + operator.
source§

fn add(self, oth: &BigNum) -> BigNum

Performs the + operation. Read more
source§

impl<'a, 'b> Add<&'b BigNumRef> for &'a BigNum

§

type Output = BigNum

The resulting type after applying the + operator.
source§

fn add(self, oth: &BigNumRef) -> BigNum

Performs the + operation. Read more
source§

impl Add<&BigNumRef> for &BigNumRef

§

type Output = BigNum

The resulting type after applying the + operator.
source§

fn add(self, oth: &BigNumRef) -> BigNum

Performs the + operation. Read more
source§

impl AsRef<BigNumRef> for BigNum

source§

fn as_ref(&self) -> &BigNumRef

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<BigNumRef> for BigNum

source§

fn borrow(&self) -> &BigNumRef

Immutably borrows from an owned value. Read more
source§

impl Debug for BigNumRef

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for BigNumRef

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, 'b> Div<&'b BigNum> for &'a BigNumRef

§

type Output = BigNum

The resulting type after applying the / operator.
source§

fn div(self, oth: &BigNum) -> BigNum

Performs the / operation. Read more
source§

impl<'a, 'b> Div<&'b BigNumRef> for &'a BigNum

§

type Output = BigNum

The resulting type after applying the / operator.
source§

fn div(self, oth: &BigNumRef) -> BigNum

Performs the / operation. Read more
source§

impl<'b> Div<&'b BigNumRef> for &BigNumRef

§

type Output = BigNum

The resulting type after applying the / operator.
source§

fn div(self, oth: &'b BigNumRef) -> BigNum

Performs the / operation. Read more
source§

impl ForeignTypeRef for BigNumRef

§

type CType = BIGNUM

The raw C type.
source§

unsafe fn from_ptr<'a>(ptr: *mut Self::CType) -> &'a Self

Constructs a shared instance of this type from its raw type.
source§

unsafe fn from_ptr_mut<'a>(ptr: *mut Self::CType) -> &'a mut Self

Constructs a mutable reference of this type from its raw type.
source§

fn as_ptr(&self) -> *mut Self::CType

Returns a raw pointer to the wrapped value.
source§

impl<'a, 'b> Mul<&'b BigNum> for &'a BigNumRef

§

type Output = BigNum

The resulting type after applying the * operator.
source§

fn mul(self, oth: &BigNum) -> BigNum

Performs the * operation. Read more
source§

impl<'a, 'b> Mul<&'b BigNumRef> for &'a BigNum

§

type Output = BigNum

The resulting type after applying the * operator.
source§

fn mul(self, oth: &BigNumRef) -> BigNum

Performs the * operation. Read more
source§

impl Mul<&BigNumRef> for &BigNumRef

§

type Output = BigNum

The resulting type after applying the * operator.
source§

fn mul(self, oth: &BigNumRef) -> BigNum

Performs the * operation. Read more
source§

impl Neg for &BigNumRef

§

type Output = BigNum

The resulting type after applying the - operator.
source§

fn neg(self) -> BigNum

Performs the unary - operation. Read more
source§

impl Ord for BigNumRef

source§

fn cmp(&self, oth: &BigNumRef) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<BigNum> for BigNumRef

source§

fn eq(&self, oth: &BigNum) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<BigNumRef> for BigNum

source§

fn eq(&self, oth: &BigNumRef) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for BigNumRef

source§

fn eq(&self, oth: &BigNumRef) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<BigNum> for BigNumRef

source§

fn partial_cmp(&self, oth: &BigNum) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<BigNumRef> for BigNum

source§

fn partial_cmp(&self, oth: &BigNumRef) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd for BigNumRef

source§

fn partial_cmp(&self, oth: &BigNumRef) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a, 'b> Rem<&'b BigNum> for &'a BigNumRef

§

type Output = BigNum

The resulting type after applying the % operator.
source§

fn rem(self, oth: &BigNum) -> BigNum

Performs the % operation. Read more
source§

impl<'a, 'b> Rem<&'b BigNumRef> for &'a BigNum

§

type Output = BigNum

The resulting type after applying the % operator.
source§

fn rem(self, oth: &BigNumRef) -> BigNum

Performs the % operation. Read more
source§

impl<'b> Rem<&'b BigNumRef> for &BigNumRef

§

type Output = BigNum

The resulting type after applying the % operator.
source§

fn rem(self, oth: &'b BigNumRef) -> BigNum

Performs the % operation. Read more
source§

impl Shl<i32> for &BigNumRef

§

type Output = BigNum

The resulting type after applying the << operator.
source§

fn shl(self, n: i32) -> BigNum

Performs the << operation. Read more
source§

impl Shr<i32> for &BigNumRef

§

type Output = BigNum

The resulting type after applying the >> operator.
source§

fn shr(self, n: i32) -> BigNum

Performs the >> operation. Read more
source§

impl<'a, 'b> Sub<&'b BigNum> for &'a BigNumRef

§

type Output = BigNum

The resulting type after applying the - operator.
source§

fn sub(self, oth: &BigNum) -> BigNum

Performs the - operation. Read more
source§

impl<'a, 'b> Sub<&'b BigNumRef> for &'a BigNum

§

type Output = BigNum

The resulting type after applying the - operator.
source§

fn sub(self, oth: &BigNumRef) -> BigNum

Performs the - operation. Read more
source§

impl Sub<&BigNumRef> for &BigNumRef

§

type Output = BigNum

The resulting type after applying the - operator.
source§

fn sub(self, oth: &BigNumRef) -> BigNum

Performs the - operation. Read more
source§

impl Eq for BigNumRef

source§

impl Send for BigNumRef

source§

impl Sync for BigNumRef

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.