pub struct BigNum(_);
Expand description

Dynamically sized large number implementation

Perform large number mathematics. Create a new BigNum with new. Perform standard mathematics on large numbers using methods from Dref<Target = BigNumRef>

OpenSSL documentation at BN_new.

Examples

use openssl::bn::BigNum;
let little_big = BigNum::from_u32(std::u32::MAX)?;
assert_eq!(*&little_big.num_bytes(), 4);

Implementations

Creates a new BigNum with the value 0.

This corresponds to BN_new.

Returns a new secure BigNum.

This corresponds to BN_secure_new.

Creates a new BigNum with the given value.

This corresponds to BN_set_word.

Creates a BigNum from a decimal string.

This corresponds to BN_dec2bn.

Creates a BigNum from a hexadecimal string.

This corresponds to BN_hex2bn.

Returns a constant used in IKE as defined in RFC 2409. This prime number is in the order of magnitude of 2 ^ 768. This number is used during calculated key exchanges such as Diffie-Hellman. This number is labeled Oakley group id 1.

This corresponds to BN_get_rfc2409_prime_768.

Returns a constant used in IKE as defined in RFC 2409. This prime number is in the order of magnitude of 2 ^ 1024. This number is used during calculated key exchanges such as Diffie-Hellman. This number is labeled Oakly group 2.

This corresponds to BN_get_rfc2409_prime_1024.

Returns a constant used in IKE as defined in RFC 3526. The prime is in the order of magnitude of 2 ^ 1536. This number is used during calculated key exchanges such as Diffie-Hellman. This number is labeled MODP group 5.

This corresponds to BN_get_rfc3526_prime_1536.

Returns a constant used in IKE as defined in RFC 3526. The prime is in the order of magnitude of 2 ^ 2048. This number is used during calculated key exchanges such as Diffie-Hellman. This number is labeled MODP group 14.

This corresponds to BN_get_rfc3526_prime_2048.

Returns a constant used in IKE as defined in RFC 3526. The prime is in the order of magnitude of 2 ^ 3072. This number is used during calculated key exchanges such as Diffie-Hellman. This number is labeled MODP group 15.

This corresponds to BN_get_rfc3526_prime_3072.

Returns a constant used in IKE as defined in RFC 3526. The prime is in the order of magnitude of 2 ^ 4096. This number is used during calculated key exchanges such as Diffie-Hellman. This number is labeled MODP group 16.

This corresponds to BN_get_rfc3526_prime_4096.

Returns a constant used in IKE as defined in RFC 3526. The prime is in the order of magnitude of 2 ^ 6144. This number is used during calculated key exchanges such as Diffie-Hellman. This number is labeled MODP group 17.

This corresponds to BN_get_rfc3526_prime_6114.

Returns a constant used in IKE as defined in RFC 3526. The prime is in the order of magnitude of 2 ^ 8192. This number is used during calculated key exchanges such as Diffie-Hellman. This number is labeled MODP group 18.

This corresponds to BN_get_rfc3526_prime_8192.

Creates a new BigNum from an unsigned, big-endian encoded number of arbitrary length.

OpenSSL documentation at BN_bin2bn

let bignum = BigNum::from_slice(&[0x12, 0x00, 0x34]).unwrap();

assert_eq!(bignum, BigNum::from_u32(0x120034).unwrap());

This corresponds to BN_bin2bn.

Copies data from a slice overwriting what was in the BigNum.

This function can be used to copy data from a slice to a secure BigNum.

Examples
let mut bignum = BigNum::new().unwrap();
bignum.copy_from_slice(&[0x12, 0x00, 0x34]).unwrap();

assert_eq!(bignum, BigNum::from_u32(0x120034).unwrap());

This corresponds to BN_bin2bn.

Methods from Deref<Target = BigNumRef>

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.

Adds a u32 to self.

This corresponds to BN_add_word.

Subtracts a u32 from self.

This corresponds to BN_sub_word.

Multiplies a u32 by self.

This corresponds to BN_mul_word.

Divides self by a u32, returning the remainder.

This corresponds to BN_div_word.

Returns the result of self modulo w.

This corresponds to BN_mod_word.

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

This corresponds to BN_rand_range.

The cryptographically weak counterpart to rand_in_range.

This corresponds to BN_pseudo_rand_range.

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

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

This corresponds to BN_set_bit.

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.

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

This corresponds to BN_is_bit_set.

Truncates self to the lowest n bits.

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

This corresponds to BN_mask_bits.

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

This corresponds to BN_lshift1.

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

This corresponds to BN_rshift1.

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

This corresponds to BN_add.

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

This corresponds to BN_sub.

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

This corresponds to BN_lshift.

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

This corresponds to BN_rshift.

Creates a new BigNum with the same value.

This corresponds to BN_dup.

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

This corresponds to BN_set_negative.

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.

Returns true if self is negative.

This corresponds to BN_is_negative.

Returns the number of significant bits in self.

This corresponds to BN_num_bits.

Returns the size of self in bytes. Implemented natively.

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.

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

This corresponds to BN_pseudo_rand.

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.

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

This corresponds to BN_mul.

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.

Places the result of a % b in self.

This corresponds to BN_div.

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

This corresponds to BN_div.

Places the result of in self.

This corresponds to BN_sqr.

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

This corresponds to BN_nnmod.

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

This corresponds to BN_mod_add.

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

This corresponds to BN_mod_sub.

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

This corresponds to BN_mod_mul.

Places the result of a² mod m in self.

This corresponds to BN_mod_sqr.

Places the result of a^p in self.

This corresponds to BN_exp.

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

This corresponds to BN_mod_exp.

Places the inverse of a modulo n in self.

This corresponds to BN_mod_inverse.

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

This corresponds to BN_gcd.

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.

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.

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.

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.

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.

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.

Returns an Asn1Integer containing the value of self.

This corresponds to BN_to_ASN1_INTEGER.

Force constant time computation on this value.

This corresponds to BN_set_flags.

Returns true if self is in const time mode.

This corresponds to BN_get_flags.

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

This corresponds to BN_get_flags.

Trait Implementations

The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Formats the value using the given formatter. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
Executes the destructor for this type. Read more
The raw C type.
The type representing a reference to this type.
Constructs an instance of this type from its raw type.
Returns a raw pointer to the wrapped value.
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
The resulting type after applying the << operator.
Performs the << operation. Read more
The resulting type after applying the >> operator.
Performs the >> operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.