Skip to content

Commit

Permalink
Deprecate BigNum::from_u32()
Browse files Browse the repository at this point in the history
The Rust-idiomatic TryFrom implementations can be used instead.
  • Loading branch information
npmccallum committed Aug 24, 2020
1 parent ccbaa5a commit bb87e29
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 55 deletions.
6 changes: 4 additions & 2 deletions openssl/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,12 @@ mod tests {
assert_eq!(large.to_bn().unwrap(), bn);
}

use std::convert::TryFrom;

roundtrip(BigNum::from_dec_str("1000000000000000000000000000000000").unwrap());
roundtrip(-BigNum::from_dec_str("1000000000000000000000000000000000").unwrap());
roundtrip(BigNum::from_u32(1234).unwrap());
roundtrip(-BigNum::from_u32(1234).unwrap());
roundtrip(BigNum::try_from(1234).unwrap());
roundtrip(-BigNum::try_from(1234).unwrap());
}

#[test]
Expand Down
70 changes: 39 additions & 31 deletions openssl/src/bn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ foreign_type_and_impl_send_sync! {
///
/// # Examples
/// ```
/// use std::convert::TryFrom;
/// use openssl::bn::BigNum;
/// # use openssl::error::ErrorStack;
/// # fn bignums() -> Result< (), ErrorStack > {
/// let little_big = BigNum::from_u32(std::u32::MAX)?;
/// let little_big = BigNum::try_from(std::u32::MAX)?;
/// assert_eq!(*&little_big.num_bytes(), 4);
/// # Ok(())
/// # }
Expand Down Expand Up @@ -364,10 +365,11 @@ impl BigNumRef {
/// # Examples
///
/// ```
/// use std::convert::TryFrom;
/// # use openssl::bn::BigNum;
/// # use std::cmp::Ordering;
/// let s = -BigNum::from_u32(8).unwrap();
/// let o = BigNum::from_u32(8).unwrap();
/// let s = -BigNum::try_from(8).unwrap();
/// let o = BigNum::try_from(8).unwrap();
///
/// assert_eq!(s.ucmp(&o), Ordering::Equal);
/// ```
Expand Down Expand Up @@ -875,8 +877,9 @@ impl BigNumRef {
///
/// ```
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(4543).unwrap();
/// let r = BigNum::from_u32(4543).unwrap();
/// use std::convert::TryFrom;
/// let s = -BigNum::try_from(4543).unwrap();
/// let r = BigNum::try_from(4543).unwrap();
///
/// let s_vec = s.to_be_vec();
/// assert_eq!(BigNum::from_be_bytes(&s_vec).unwrap(), r);
Expand All @@ -897,8 +900,9 @@ impl BigNumRef {
///
/// ```
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(4543).unwrap();
/// let r = BigNum::from_u32(4543).unwrap();
/// use std::convert::TryFrom;
/// let s = -BigNum::try_from(4543).unwrap();
/// let r = BigNum::try_from(4543).unwrap();
///
/// let s_vec = s.to_le_vec();
/// assert_eq!(BigNum::from_le_bytes(&s_vec).unwrap(), r);
Expand All @@ -918,8 +922,9 @@ impl BigNumRef {
///
/// ```
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(4543).unwrap();
/// let r = BigNum::from_u32(4543).unwrap();
/// use std::convert::TryFrom;
/// let s = -BigNum::try_from(4543).unwrap();
/// let r = BigNum::try_from(4543).unwrap();
///
/// let s_vec = s.to_ne_vec();
/// assert_eq!(BigNum::from_ne_bytes(&s_vec).unwrap(), r);
Expand All @@ -941,8 +946,9 @@ impl BigNumRef {
///
/// ```
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(0x1234).unwrap();
/// let r = BigNum::from_u32(0x1234).unwrap();
/// use std::convert::TryFrom;
/// let s = -BigNum::try_from(0x1234).unwrap();
/// let r = BigNum::try_from(0x1234).unwrap();
///
/// let mut buf = [1u8; 4];
/// s.to_be_bytes(&mut buf).unwrap();
Expand Down Expand Up @@ -971,8 +977,9 @@ impl BigNumRef {
///
/// ```
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(0x1234).unwrap();
/// let r = BigNum::from_u32(0x1234).unwrap();
/// use std::convert::TryFrom;
/// let s = -BigNum::try_from(0x1234).unwrap();
/// let r = BigNum::try_from(0x1234).unwrap();
///
/// let mut buf = [1u8; 4];
/// s.to_le_bytes(&mut buf).unwrap();
Expand Down Expand Up @@ -1011,7 +1018,8 @@ impl BigNumRef {
///
/// ```
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(12345).unwrap();
/// use std::convert::TryFrom;
/// let s = -BigNum::try_from(12345).unwrap();
///
/// assert_eq!(&**s.to_dec_str().unwrap(), "-12345");
/// ```
Expand All @@ -1026,7 +1034,8 @@ impl BigNumRef {
///
/// ```
/// # use openssl::bn::BigNum;
/// let s = -BigNum::from_u32(0x99ff).unwrap();
/// use std::convert::TryFrom;
/// let s = -BigNum::try_from(0x99ff).unwrap();
///
/// assert_eq!(&**s.to_hex_str().unwrap(), "-99FF");
/// ```
Expand Down Expand Up @@ -1056,11 +1065,8 @@ impl BigNum {
}
}

/// Creates a new `BigNum` with the given value.
///
/// OpenSSL documentation at [`BN_set_word`]
///
/// [`BN_set_word`]: https://www.openssl.org/docs/man1.1.0/crypto/BN_set_word.html
/// Deprecated. Use the `TryFrom` interface for integer conversions.
#[deprecated]
pub fn from_u32(n: u32) -> Result<BigNum, ErrorStack> {
BigNum::new().and_then(|v| unsafe {
cvt(ffi::BN_set_word(v.as_ptr(), n as ffi::BN_ULONG)).map(|_| v)
Expand Down Expand Up @@ -1231,9 +1237,10 @@ impl BigNum {
///
/// ```
/// # use openssl::bn::BigNum;
/// use std::convert::TryFrom;
/// let bignum = BigNum::from_be_bytes(&[0x12, 0x00, 0x34]).unwrap();
///
/// assert_eq!(bignum, BigNum::from_u32(0x120034).unwrap());
/// assert_eq!(bignum, BigNum::try_from(0x120034).unwrap());
/// ```
pub fn from_be_bytes(n: &[u8]) -> Result<Self, ErrorStack> {
unsafe {
Expand All @@ -1256,9 +1263,10 @@ impl BigNum {
///
/// ```
/// # use openssl::bn::BigNum;
/// use std::convert::TryFrom;
/// let bignum = BigNum::from_le_bytes(&[0x34, 0x00, 0x12]).unwrap();
///
/// assert_eq!(bignum, BigNum::from_u32(0x120034).unwrap());
/// assert_eq!(bignum, BigNum::try_from(0x120034).unwrap());
/// ```
#[cfg(ossl110)]
pub fn from_le_bytes(n: &[u8]) -> Result<Self, ErrorStack> {
Expand Down Expand Up @@ -1543,7 +1551,6 @@ impl Neg for BigNum {
macro_rules! mkconvert {
($($t:ty)+) => {
$(
#[cfg(ossl110)]
impl core::convert::TryFrom<$t> for BigNum {
type Error = ErrorStack;

Expand Down Expand Up @@ -1587,10 +1594,11 @@ mkconvert! {
#[cfg(test)]
mod tests {
use bn::{BigNum, BigNumContext};
use std::convert::TryFrom;

#[test]
fn test_to_from_slice() {
let v0 = BigNum::from_u32(10_203_004).unwrap();
let v0 = BigNum::try_from(10_203_004u32).unwrap();
let vec = v0.to_be_vec();
let v1 = BigNum::from_be_bytes(&vec).unwrap();

Expand All @@ -1599,38 +1607,38 @@ mod tests {

#[test]
fn test_negation() {
let a = BigNum::from_u32(909_829_283).unwrap();
let a = BigNum::try_from(909_829_283u32).unwrap();

assert!(!a.is_negative());
assert!((-a).is_negative());
}

#[test]
fn test_shift() {
let a = BigNum::from_u32(909_829_283).unwrap();
let a = BigNum::try_from(909_829_283u32).unwrap();

assert_eq!(a, &(&a << 1) >> 1);
}

#[test]
fn test_rand_range() {
let range = BigNum::from_u32(909_829_283).unwrap();
let range = BigNum::try_from(909_829_283u32).unwrap();
let mut result = BigNum::from_dec_str(&range.to_dec_str().unwrap()).unwrap();
range.rand_range(&mut result).unwrap();
assert!(result >= BigNum::from_u32(0).unwrap() && result < range);
assert!(result >= BigNum::try_from(0).unwrap() && result < range);
}

#[test]
fn test_pseudo_rand_range() {
let range = BigNum::from_u32(909_829_283).unwrap();
let range = BigNum::try_from(909_829_283u32).unwrap();
let mut result = BigNum::from_dec_str(&range.to_dec_str().unwrap()).unwrap();
range.pseudo_rand_range(&mut result).unwrap();
assert!(result >= BigNum::from_u32(0).unwrap() && result < range);
assert!(result >= BigNum::try_from(0).unwrap() && result < range);
}

#[test]
fn test_prime_numbers() {
let a = BigNum::from_u32(19_029_017).unwrap();
let a = BigNum::try_from(19_029_017u32).unwrap();
let mut p = BigNum::new().unwrap();
p.generate_prime(128, true, None, Some(&a)).unwrap();

Expand Down
37 changes: 19 additions & 18 deletions openssl/src/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ mod test {
use hash::MessageDigest;
use pkey::PKey;
use sign::{Signer, Verifier};
use std::convert::TryFrom;

#[test]
pub fn test_generate() {
Expand All @@ -382,32 +383,32 @@ mod test {

#[test]
fn test_priv_key_from_parts() {
let p = BigNum::from_u32(283).unwrap();
let q = BigNum::from_u32(47).unwrap();
let g = BigNum::from_u32(60).unwrap();
let priv_key = BigNum::from_u32(15).unwrap();
let pub_key = BigNum::from_u32(207).unwrap();
let p = BigNum::try_from(283).unwrap();
let q = BigNum::try_from(47).unwrap();
let g = BigNum::try_from(60).unwrap();
let priv_key = BigNum::try_from(15).unwrap();
let pub_key = BigNum::try_from(207).unwrap();

let dsa = Dsa::from_private_components(p, q, g, priv_key, pub_key).unwrap();
assert_eq!(dsa.pub_key(), &BigNum::from_u32(207).unwrap());
assert_eq!(dsa.priv_key(), &BigNum::from_u32(15).unwrap());
assert_eq!(dsa.p(), &BigNum::from_u32(283).unwrap());
assert_eq!(dsa.q(), &BigNum::from_u32(47).unwrap());
assert_eq!(dsa.g(), &BigNum::from_u32(60).unwrap());
assert_eq!(dsa.pub_key(), &BigNum::try_from(207).unwrap());
assert_eq!(dsa.priv_key(), &BigNum::try_from(15).unwrap());
assert_eq!(dsa.p(), &BigNum::try_from(283).unwrap());
assert_eq!(dsa.q(), &BigNum::try_from(47).unwrap());
assert_eq!(dsa.g(), &BigNum::try_from(60).unwrap());
}

#[test]
fn test_pub_key_from_parts() {
let p = BigNum::from_u32(283).unwrap();
let q = BigNum::from_u32(47).unwrap();
let g = BigNum::from_u32(60).unwrap();
let pub_key = BigNum::from_u32(207).unwrap();
let p = BigNum::try_from(283).unwrap();
let q = BigNum::try_from(47).unwrap();
let g = BigNum::try_from(60).unwrap();
let pub_key = BigNum::try_from(207).unwrap();

let dsa = Dsa::from_public_components(p, q, g, pub_key).unwrap();
assert_eq!(dsa.pub_key(), &BigNum::from_u32(207).unwrap());
assert_eq!(dsa.p(), &BigNum::from_u32(283).unwrap());
assert_eq!(dsa.q(), &BigNum::from_u32(47).unwrap());
assert_eq!(dsa.g(), &BigNum::from_u32(60).unwrap());
assert_eq!(dsa.pub_key(), &BigNum::try_from(207).unwrap());
assert_eq!(dsa.p(), &BigNum::try_from(283).unwrap());
assert_eq!(dsa.q(), &BigNum::try_from(47).unwrap());
assert_eq!(dsa.g(), &BigNum::try_from(60).unwrap());
}

#[test]
Expand Down
5 changes: 3 additions & 2 deletions openssl/src/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ impl<T> fmt::Debug for EcKey<T> {
#[cfg(test)]
mod test {
use hex::FromHex;
use std::convert::TryFrom;

use super::*;
use bn::{BigNum, BigNumContext};
Expand All @@ -944,7 +945,7 @@ mod test {
let mut ctx = BigNumContext::new().unwrap();
let mut cofactor = BigNum::new().unwrap();
group.cofactor(&mut cofactor, &mut ctx).unwrap();
let one = BigNum::from_u32(1).unwrap();
let one = BigNum::try_from(1).unwrap();
assert_eq!(cofactor, one);
}

Expand Down Expand Up @@ -1001,7 +1002,7 @@ mod test {
fn generator() {
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
let gen = group.generator();
let one = BigNum::from_u32(1).unwrap();
let one = BigNum::try_from(1).unwrap();
let mut ctx = BigNumContext::new().unwrap();
let mut ecp = EcPoint::new(&group).unwrap();
ecp.mul_generator(&group, &one, &ctx).unwrap();
Expand Down
5 changes: 3 additions & 2 deletions openssl/src/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use ffi;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::c_int;
use std::convert::TryFrom;
use std::fmt;
use std::mem;
use std::ptr;
Expand Down Expand Up @@ -605,7 +606,7 @@ impl Rsa<Private> {
///
/// [`RSA_generate_key_ex`]: https://www.openssl.org/docs/man1.1.0/crypto/RSA_generate_key_ex.html
pub fn generate(bits: u32) -> Result<Rsa<Private>, ErrorStack> {
let e = BigNum::from_u32(ffi::RSA_F4 as u32)?;
let e = BigNum::try_from(ffi::RSA_F4)?;
Rsa::generate_with_e(bits, &e)
}

Expand Down Expand Up @@ -941,7 +942,7 @@ mod test {

#[test]
fn generate_with_e() {
let e = BigNum::from_u32(0x10001).unwrap();
let e = BigNum::try_from(0x10001u32).unwrap();
Rsa::generate_with_e(2048, &e).unwrap();
}
}

0 comments on commit bb87e29

Please sign in to comment.