aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/rsa.rs
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2017-07-15 21:46:11 -0700
committerSteven Fackler <[email protected]>2017-07-15 21:46:11 -0700
commitbcd0dcafcba31b7239faf1d582871f8fa83d69e9 (patch)
treef090be453d289f0f17ca4f6a3f458881e6f7091a /openssl/src/rsa.rs
parentInit before creating ex indexes (diff)
downloadrust-openssl-bcd0dcafcba31b7239faf1d582871f8fa83d69e9.tar.xz
rust-openssl-bcd0dcafcba31b7239faf1d582871f8fa83d69e9.zip
Rustfmt
Diffstat (limited to 'openssl/src/rsa.rs')
-rw-r--r--openssl/src/rsa.rs216
1 files changed, 130 insertions, 86 deletions
diff --git a/openssl/src/rsa.rs b/openssl/src/rsa.rs
index 22a9c36f..e5126e5f 100644
--- a/openssl/src/rsa.rs
+++ b/openssl/src/rsa.rs
@@ -45,8 +45,11 @@ impl RsaRef {
private_key_to_der!(ffi::i2d_RSAPrivateKey);
public_key_to_der!(ffi::i2d_RSA_PUBKEY);
- to_der_inner!(/// Serializes the public key to DER-encoded PKCS#1.
- public_key_to_der_pkcs1, ffi::i2d_RSAPublicKey);
+ to_der_inner!(
+ /// Serializes the public key to DER-encoded PKCS#1.
+ public_key_to_der_pkcs1,
+ ffi::i2d_RSAPublicKey
+ );
// FIXME should return u32
pub fn size(&self) -> usize {
@@ -63,21 +66,24 @@ impl RsaRef {
///
/// Panics if `self` has no private components, or if `to` is smaller
/// than `self.size()`.
- pub fn private_decrypt(&self,
- from: &[u8],
- to: &mut [u8],
- padding: Padding)
- -> Result<usize, ErrorStack> {
+ pub fn private_decrypt(
+ &self,
+ from: &[u8],
+ to: &mut [u8],
+ padding: Padding,
+ ) -> Result<usize, ErrorStack> {
assert!(self.d().is_some(), "private components missing");
assert!(from.len() <= i32::max_value() as usize);
assert!(to.len() >= self.size());
unsafe {
- let len = try!(cvt_n(ffi::RSA_private_decrypt(from.len() as c_int,
- from.as_ptr(),
- to.as_mut_ptr(),
- self.as_ptr(),
- padding.0)));
+ let len = try!(cvt_n(ffi::RSA_private_decrypt(
+ from.len() as c_int,
+ from.as_ptr(),
+ to.as_mut_ptr(),
+ self.as_ptr(),
+ padding.0,
+ )));
Ok(len as usize)
}
}
@@ -88,21 +94,24 @@ impl RsaRef {
///
/// Panics if `self` has no private components, or if `to` is smaller
/// than `self.size()`.
- pub fn private_encrypt(&self,
- from: &[u8],
- to: &mut [u8],
- padding: Padding)
- -> Result<usize, ErrorStack> {
+ pub fn private_encrypt(
+ &self,
+ from: &[u8],
+ to: &mut [u8],
+ padding: Padding,
+ ) -> Result<usize, ErrorStack> {
assert!(self.d().is_some(), "private components missing");
assert!(from.len() <= i32::max_value() as usize);
assert!(to.len() >= self.size());
unsafe {
- let len = try!(cvt_n(ffi::RSA_private_encrypt(from.len() as c_int,
- from.as_ptr(),
- to.as_mut_ptr(),
- self.as_ptr(),
- padding.0)));
+ let len = try!(cvt_n(ffi::RSA_private_encrypt(
+ from.len() as c_int,
+ from.as_ptr(),
+ to.as_mut_ptr(),
+ self.as_ptr(),
+ padding.0,
+ )));
Ok(len as usize)
}
}
@@ -112,20 +121,23 @@ impl RsaRef {
/// # Panics
///
/// Panics if `to` is smaller than `self.size()`.
- pub fn public_decrypt(&self,
- from: &[u8],
- to: &mut [u8],
- padding: Padding)
- -> Result<usize, ErrorStack> {
+ pub fn public_decrypt(
+ &self,
+ from: &[u8],
+ to: &mut [u8],
+ padding: Padding,
+ ) -> Result<usize, ErrorStack> {
assert!(from.len() <= i32::max_value() as usize);
assert!(to.len() >= self.size());
unsafe {
- let len = try!(cvt_n(ffi::RSA_public_decrypt(from.len() as c_int,
- from.as_ptr(),
- to.as_mut_ptr(),
- self.as_ptr(),
- padding.0)));
+ let len = try!(cvt_n(ffi::RSA_public_decrypt(
+ from.len() as c_int,
+ from.as_ptr(),
+ to.as_mut_ptr(),
+ self.as_ptr(),
+ padding.0,
+ )));
Ok(len as usize)
}
}
@@ -135,20 +147,23 @@ impl RsaRef {
/// # Panics
///
/// Panics if `to` is smaller than `self.size()`.
- pub fn public_encrypt(&self,
- from: &[u8],
- to: &mut [u8],
- padding: Padding)
- -> Result<usize, ErrorStack> {
+ pub fn public_encrypt(
+ &self,
+ from: &[u8],
+ to: &mut [u8],
+ padding: Padding,
+ ) -> Result<usize, ErrorStack> {
assert!(from.len() <= i32::max_value() as usize);
assert!(to.len() >= self.size());
unsafe {
- let len = try!(cvt_n(ffi::RSA_public_encrypt(from.len() as c_int,
- from.as_ptr(),
- to.as_mut_ptr(),
- self.as_ptr(),
- padding.0)));
+ let len = try!(cvt_n(ffi::RSA_public_encrypt(
+ from.len() as c_int,
+ from.as_ptr(),
+ to.as_mut_ptr(),
+ self.as_ptr(),
+ padding.0,
+ )));
Ok(len as usize)
}
}
@@ -215,32 +230,41 @@ impl Rsa {
pub fn from_public_components(n: BigNum, e: BigNum) -> Result<Rsa, ErrorStack> {
unsafe {
let rsa = Rsa(try!(cvt_p(ffi::RSA_new())));
- try!(cvt(compat::set_key(rsa.0,
- n.as_ptr(),
- e.as_ptr(),
- ptr::null_mut())));
+ try!(cvt(compat::set_key(
+ rsa.0,
+ n.as_ptr(),
+ e.as_ptr(),
+ ptr::null_mut(),
+ )));
mem::forget((n, e));
Ok(rsa)
}
}
- pub fn from_private_components(n: BigNum,
- e: BigNum,
- d: BigNum,
- p: BigNum,
- q: BigNum,
- dp: BigNum,
- dq: BigNum,
- qi: BigNum)
- -> Result<Rsa, ErrorStack> {
+ pub fn from_private_components(
+ n: BigNum,
+ e: BigNum,
+ d: BigNum,
+ p: BigNum,
+ q: BigNum,
+ dp: BigNum,
+ dq: BigNum,
+ qi: BigNum,
+ ) -> Result<Rsa, ErrorStack> {
unsafe {
let rsa = Rsa(try!(cvt_p(ffi::RSA_new())));
- try!(cvt(compat::set_key(rsa.0, n.as_ptr(), e.as_ptr(), d.as_ptr())));
+ try!(cvt(
+ compat::set_key(rsa.0, n.as_ptr(), e.as_ptr(), d.as_ptr()),
+ ));
mem::forget((n, e, d));
try!(cvt(compat::set_factors(rsa.0, p.as_ptr(), q.as_ptr())));
mem::forget((p, q));
- try!(cvt(compat::set_crt_params(rsa.0, dp.as_ptr(), dq.as_ptr(),
- qi.as_ptr())));
+ try!(cvt(compat::set_crt_params(
+ rsa.0,
+ dp.as_ptr(),
+ dq.as_ptr(),
+ qi.as_ptr(),
+ )));
mem::forget((dp, dq, qi));
Ok(rsa)
}
@@ -254,7 +278,12 @@ impl Rsa {
unsafe {
let rsa = Rsa(try!(cvt_p(ffi::RSA_new())));
let e = try!(BigNum::from_u32(ffi::RSA_F4 as u32));
- try!(cvt(ffi::RSA_generate_key_ex(rsa.0, bits as c_int, e.as_ptr(), ptr::null_mut())));
+ try!(cvt(ffi::RSA_generate_key_ex(
+ rsa.0,
+ bits as c_int,
+ e.as_ptr(),
+ ptr::null_mut(),
+ )));
Ok(rsa)
}
}
@@ -265,12 +294,17 @@ impl Rsa {
public_key_from_pem!(Rsa, ffi::PEM_read_bio_RSA_PUBKEY);
public_key_from_der!(Rsa, ffi::d2i_RSA_PUBKEY);
- from_der_inner!(/// Deserializes a public key from DER-encoded PKCS#1 data.
- public_key_from_der_pkcs1, Rsa, ffi::d2i_RSAPublicKey);
+ from_der_inner!(
+ /// Deserializes a public key from DER-encoded PKCS#1 data.
+ public_key_from_der_pkcs1,
+ Rsa,
+ ffi::d2i_RSAPublicKey
+ );
#[deprecated(since = "0.9.2", note = "use private_key_from_pem_callback")]
pub fn private_key_from_pem_cb<F>(buf: &[u8], pass_cb: F) -> Result<Rsa, ErrorStack>
- where F: FnOnce(&mut [c_char]) -> usize
+ where
+ F: FnOnce(&mut [c_char]) -> usize,
{
ffi::init();
let mut cb = CallbackState::new(pass_cb);
@@ -278,10 +312,12 @@ impl Rsa {
unsafe {
let cb_ptr = &mut cb as *mut _ as *mut c_void;
- let rsa = try!(cvt_p(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.as_ptr(),
- ptr::null_mut(),
- Some(invoke_passwd_cb_old::<F>),
- cb_ptr)));
+ let rsa = try!(cvt_p(ffi::PEM_read_bio_RSAPrivateKey(
+ mem_bio.as_ptr(),
+ ptr::null_mut(),
+ Some(invoke_passwd_cb_old::<F>),
+ cb_ptr,
+ )));
Ok(Rsa(rsa))
}
}
@@ -320,11 +356,12 @@ mod compat {
ffi::RSA_set0_factors(r, p, q)
}
- pub unsafe fn set_crt_params(r: *mut RSA,
- dmp1: *mut BIGNUM,
- dmq1: *mut BIGNUM,
- iqmp: *mut BIGNUM)
- -> c_int {
+ pub unsafe fn set_crt_params(
+ r: *mut RSA,
+ dmp1: *mut BIGNUM,
+ dmq1: *mut BIGNUM,
+ iqmp: *mut BIGNUM,
+ ) -> c_int {
ffi::RSA_set0_crt_params(r, dmp1, dmq1, iqmp)
}
}
@@ -355,11 +392,12 @@ mod compat {
1 // TODO: is this right? should it be 0? what's success?
}
- pub unsafe fn set_crt_params(r: *mut RSA,
- dmp1: *mut BIGNUM,
- dmq1: *mut BIGNUM,
- iqmp: *mut BIGNUM)
- -> c_int {
+ pub unsafe fn set_crt_params(
+ r: *mut RSA,
+ dmp1: *mut BIGNUM,
+ dmq1: *mut BIGNUM,
+ iqmp: *mut BIGNUM,
+ ) -> c_int {
(*r).dmp1 = dmp1;
(*r).dmq1 = dmq1;
(*r).iqmp = iqmp;
@@ -384,11 +422,10 @@ mod test {
let mut password_queried = false;
let key = include_bytes!("../test/rsa-encrypted.pem");
Rsa::private_key_from_pem_callback(key, |password| {
- password_queried = true;
- password[..6].copy_from_slice(b"mypass");
- Ok(6)
- })
- .unwrap();
+ password_queried = true;
+ password[..6].copy_from_slice(b"mypass");
+ Ok(6)
+ }).unwrap();
assert!(password_queried);
}
@@ -396,7 +433,8 @@ mod test {
#[test]
fn test_to_password() {
let key = Rsa::generate(2048).unwrap();
- let pem = key.private_key_to_pem_passphrase(Cipher::aes_128_cbc(), b"foobar").unwrap();
+ let pem = key.private_key_to_pem_passphrase(Cipher::aes_128_cbc(), b"foobar")
+ .unwrap();
Rsa::private_key_from_pem_passphrase(&pem, b"foobar").unwrap();
assert!(Rsa::private_key_from_pem_passphrase(&pem, b"fizzbuzz").is_err());
}
@@ -408,13 +446,17 @@ mod test {
let mut result = vec![0; public_key.size()];
let original_data = b"This is test";
- let len = public_key.public_encrypt(original_data, &mut result, PKCS1_PADDING).unwrap();
+ let len = public_key
+ .public_encrypt(original_data, &mut result, PKCS1_PADDING)
+ .unwrap();
assert_eq!(len, 256);
let pkey = include_bytes!("../test/rsa.pem");
let private_key = Rsa::private_key_from_pem(pkey).unwrap();
let mut dec_result = vec![0; private_key.size()];
- let len = private_key.private_decrypt(&result, &mut dec_result, PKCS1_PADDING).unwrap();
+ let len = private_key
+ .private_decrypt(&result, &mut dec_result, PKCS1_PADDING)
+ .unwrap();
assert_eq!(&dec_result[..len], original_data);
}
@@ -430,7 +472,8 @@ mod test {
let mut emesg = vec![0; k0.size()];
k0.private_encrypt(&msg, &mut emesg, PKCS1_PADDING).unwrap();
let mut dmesg = vec![0; k1.size()];
- let len = k1.public_decrypt(&emesg, &mut dmesg, PKCS1_PADDING).unwrap();
+ let len = k1.public_decrypt(&emesg, &mut dmesg, PKCS1_PADDING)
+ .unwrap();
assert_eq!(msg, &dmesg[..len]);
}
@@ -445,7 +488,8 @@ mod test {
let mut emesg = vec![0; k0.size()];
k0.public_encrypt(&msg, &mut emesg, PKCS1_PADDING).unwrap();
let mut dmesg = vec![0; k1.size()];
- let len = k1.private_decrypt(&emesg, &mut dmesg, PKCS1_PADDING).unwrap();
+ let len = k1.private_decrypt(&emesg, &mut dmesg, PKCS1_PADDING)
+ .unwrap();
assert_eq!(msg, &dmesg[..len]);
}
}