Struct openssl::bn::BigNumRef [−][src]
pub struct BigNumRef(_);
Expand description
Reference to a BigNum
Implementations
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.
OpenSSL documentation at BN_clear
Adds a u32
to self
.
OpenSSL documentation at BN_add_word
Subtracts a u32
from self
.
OpenSSL documentation at BN_sub_word
Multiplies a u32
by self
.
OpenSSL documentation at BN_mul_word
Divides self
by a u32
, returning the remainder.
OpenSSL documentation at BN_div_word
Returns the result of self
modulo w
.
OpenSSL documentation at BN_mod_word
Places a cryptographically-secure pseudo-random nonnegative
number less than self
in rnd
.
OpenSSL documentation at BN_rand_range
The cryptographically weak counterpart to rand_in_range
.
OpenSSL documentation at BN_pseudo_rand_range
Sets bit n
. Equivalent to self |= (1 << n)
.
When setting a bit outside of self
, it is expanded.
OpenSSL documentation at 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.
OpenSSL documentation at BN_clear_bit
Returns true
if the n
th bit of self
is set to 1, false
otherwise.
OpenSSL documentation at BN_is_bit_set
Truncates self
to the lowest n
bits.
An error occurs if self
is already shorter than n
bits.
OpenSSL documentation at BN_mask_bits
Places a << 1
in self
. Equivalent to self * 2
.
OpenSSL documentation at BN_lshift1
Places a >> 1
in self
. Equivalent to self / 2
.
OpenSSL documentation at BN_rshift1
Places a + b
in self
. core::ops::Add
is also implemented for BigNumRef
.
OpenSSL documentation at BN_add
Places a - b
in self
. core::ops::Sub
is also implemented for BigNumRef
.
OpenSSL documentation at BN_sub
Places a << n
in self
. Equivalent to a * 2 ^ n
.
OpenSSL documentation at BN_lshift
Places a >> n
in self
. Equivalent to a / 2 ^ n
.
OpenSSL documentation at BN_rshift
Creates a new BigNum with the same value.
OpenSSL documentation at BN_dup
Sets the sign of self
. Pass true to set self
to a negative. False sets
self
positive.
Returns true
if self
is negative.
Returns the number of significant bits in self
.
OpenSSL documentation at BN_num_bits
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. Seeconstants
.odd
: Iftrue
, 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))
}
OpenSSL documentation at BN_rand
The cryptographically weak counterpart to rand
. Not suitable for key generation.
OpenSSL documentation at BN_psuedo_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” primep
so that(p-1)/2
is also prime.add
/rem
: Ifadd
is set toSome(add)
,p % add == rem
will hold, wherep
is the generated prime andrem
is1
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))
}
OpenSSL documentation at BN_generate_prime_ex
pub fn checked_mul(
&mut self,
a: &BigNumRef,
b: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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
.
OpenSSL documentation at BN_mul
pub fn checked_div(
&mut self,
a: &BigNumRef,
b: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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
.
OpenSSL documentation at BN_div
pub fn checked_rem(
&mut self,
a: &BigNumRef,
b: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
pub fn checked_rem(
&mut self,
a: &BigNumRef,
b: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
Places the result of a % b
in self
.
OpenSSL documentation at BN_div
pub fn div_rem(
&mut self,
rem: &mut BigNumRef,
a: &BigNumRef,
b: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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
.
OpenSSL documentation at BN_div
Places the result of a²
in self
.
OpenSSL documentation at BN_sqr
pub fn nnmod(
&mut self,
a: &BigNumRef,
m: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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.
OpenSSL documentation at BN_nnmod
pub fn mod_add(
&mut self,
a: &BigNumRef,
b: &BigNumRef,
m: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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
.
OpenSSL documentation at BN_mod_add
pub fn mod_sub(
&mut self,
a: &BigNumRef,
b: &BigNumRef,
m: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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
.
OpenSSL documentation at BN_mod_sub
pub fn mod_mul(
&mut self,
a: &BigNumRef,
b: &BigNumRef,
m: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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
.
OpenSSL documentation at BN_mod_mul
pub fn mod_sqr(
&mut self,
a: &BigNumRef,
m: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
pub fn mod_sqr(
&mut self,
a: &BigNumRef,
m: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
Places the result of a² mod m
in self
.
OpenSSL documentation at BN_mod_sqr
pub fn exp(
&mut self,
a: &BigNumRef,
p: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
pub fn exp(
&mut self,
a: &BigNumRef,
p: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
Places the result of a^p
in self
.
OpenSSL documentation at BN_exp
pub fn mod_exp(
&mut self,
a: &BigNumRef,
p: &BigNumRef,
m: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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
.
OpenSSL documentation at BN_mod_exp
pub fn mod_inverse(
&mut self,
a: &BigNumRef,
n: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
pub fn mod_inverse(
&mut self,
a: &BigNumRef,
n: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
Places the inverse of a
modulo n
in self
.
pub fn gcd(
&mut self,
a: &BigNumRef,
b: &BigNumRef,
ctx: &mut BigNumContextRef
) -> Result<(), ErrorStack>
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
.
OpenSSL documentation at BN_gcd
Checks whether self
is prime.
Performs a Miller-Rabin probabilistic primality test with checks
iterations.
OpenSSL documentation at BN_is_prime_ex
Return Value
Returns true
if self
is prime with an error probability of less than 0.25 ^ checks
.
pub fn is_prime_fasttest(
&self,
checks: i32,
ctx: &mut BigNumContextRef,
do_trial_division: bool
) -> Result<bool, ErrorStack>
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.
OpenSSL documentation at BN_is_prime_fasttest_ex
Return Value
Returns true
if self
is prime with an error probability of less than 0.25 ^ checks
.
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);
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]);
Returns a decimal string representation of self
.
let s = -BigNum::from_u32(12345).unwrap();
assert_eq!(&**s.to_dec_str().unwrap(), "-12345");
Returns a hexadecimal string representation of self
.
let s = -BigNum::from_u32(0x99ff).unwrap();
assert_eq!(&**s.to_hex_str().unwrap(), "-99FF");
Returns an Asn1Integer
containing the value of self
.
Force constant time computation on this value.
Returns true if self
is in const time mode.
Returns true if self
was created with BigNum::new_secure
.
Trait Implementations
Constructs a shared instance of this type from its raw type.
Constructs a mutable reference of this type from its raw type.
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 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 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