Loading openssl/src/crypto/rsa.rs +22 −6 Original line number Diff line number Diff line Loading @@ -7,6 +7,24 @@ use std::io::{self, Read}; use bn::BigNum; use bio::MemBio; #[derive(Copy, Clone, Debug)] pub enum PKCSHashType { SHA256, SHA384, SHA512 } /// https://github.com/openssl/openssl/blob/master/include/openssl/obj_mac.h#L2790 impl Into<i32> for PKCSHashType { fn into(self) -> i32 { match self { PKCSHashType::SHA256 => 672, PKCSHashType::SHA384 => 673, PKCSHashType::SHA512 => 674 } } } pub struct RSA(*mut ffi::RSA); impl Drop for RSA { Loading Loading @@ -91,15 +109,13 @@ impl RSA { } } pub fn sign(&self, hash_id: i32, message: &[u8]) -> Result<Vec<u8>, SslError> { // RSA_sign(t: c_int, m: *const u8, mlen: c_uint, sig: *mut u8, siglen: *mut c_uint, k: *mut RSA) -> c_int pub fn sign(&self, hash_id: PKCSHashType, message: &[u8]) -> Result<Vec<u8>, SslError> { let k_len = try!(self.size()); let mut sig = vec![0;k_len as usize]; let mut sig_len = k_len; unsafe { let result = ffi::RSA_sign(hash_id, message.as_ptr(), message.len() as u32, sig.as_mut_ptr(), &mut sig_len, self.0); let result = ffi::RSA_sign(hash_id.into(), message.as_ptr(), message.len() as u32, sig.as_mut_ptr(), &mut sig_len, self.0); assert!(sig_len == k_len); if result == 1 { Loading @@ -110,9 +126,9 @@ impl RSA { } } pub fn verify(&self, hash_id: i32, message: &[u8], sig: &[u8]) -> Result<bool, SslError> { pub fn verify(&self, hash_id: PKCSHashType, message: &[u8], sig: &[u8]) -> Result<bool, SslError> { unsafe { let result = ffi::RSA_verify(hash_id, message.as_ptr(), message.len() as u32, sig.as_ptr(), sig.len() as u32, self.0); let result = ffi::RSA_verify(hash_id.into(), message.as_ptr(), message.len() as u32, sig.as_ptr(), sig.len() as u32, self.0); Ok(result == 1) } Loading Loading
openssl/src/crypto/rsa.rs +22 −6 Original line number Diff line number Diff line Loading @@ -7,6 +7,24 @@ use std::io::{self, Read}; use bn::BigNum; use bio::MemBio; #[derive(Copy, Clone, Debug)] pub enum PKCSHashType { SHA256, SHA384, SHA512 } /// https://github.com/openssl/openssl/blob/master/include/openssl/obj_mac.h#L2790 impl Into<i32> for PKCSHashType { fn into(self) -> i32 { match self { PKCSHashType::SHA256 => 672, PKCSHashType::SHA384 => 673, PKCSHashType::SHA512 => 674 } } } pub struct RSA(*mut ffi::RSA); impl Drop for RSA { Loading Loading @@ -91,15 +109,13 @@ impl RSA { } } pub fn sign(&self, hash_id: i32, message: &[u8]) -> Result<Vec<u8>, SslError> { // RSA_sign(t: c_int, m: *const u8, mlen: c_uint, sig: *mut u8, siglen: *mut c_uint, k: *mut RSA) -> c_int pub fn sign(&self, hash_id: PKCSHashType, message: &[u8]) -> Result<Vec<u8>, SslError> { let k_len = try!(self.size()); let mut sig = vec![0;k_len as usize]; let mut sig_len = k_len; unsafe { let result = ffi::RSA_sign(hash_id, message.as_ptr(), message.len() as u32, sig.as_mut_ptr(), &mut sig_len, self.0); let result = ffi::RSA_sign(hash_id.into(), message.as_ptr(), message.len() as u32, sig.as_mut_ptr(), &mut sig_len, self.0); assert!(sig_len == k_len); if result == 1 { Loading @@ -110,9 +126,9 @@ impl RSA { } } pub fn verify(&self, hash_id: i32, message: &[u8], sig: &[u8]) -> Result<bool, SslError> { pub fn verify(&self, hash_id: PKCSHashType, message: &[u8], sig: &[u8]) -> Result<bool, SslError> { unsafe { let result = ffi::RSA_verify(hash_id, message.as_ptr(), message.len() as u32, sig.as_ptr(), sig.len() as u32, self.0); let result = ffi::RSA_verify(hash_id.into(), message.as_ptr(), message.len() as u32, sig.as_ptr(), sig.len() as u32, self.0); Ok(result == 1) } Loading