From 6f410a25b2179b11183a56ca0d476a744bff91cd Mon Sep 17 00:00:00 2001 From: Chris Dawes Date: Tue, 3 May 2016 22:17:07 +0100 Subject: [PATCH] take enum instead of ints from openssl header file --- openssl/src/crypto/rsa.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/openssl/src/crypto/rsa.rs b/openssl/src/crypto/rsa.rs index 974f9ad9..ada5f1c1 100644 --- a/openssl/src/crypto/rsa.rs +++ b/openssl/src/crypto/rsa.rs @@ -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 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 { @@ -91,15 +109,13 @@ impl RSA { } } - pub fn sign(&self, hash_id: i32, message: &[u8]) -> Result, 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, 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 { @@ -110,9 +126,9 @@ impl RSA { } } - pub fn verify(&self, hash_id: i32, message: &[u8], sig: &[u8]) -> Result { + pub fn verify(&self, hash_id: PKCSHashType, message: &[u8], sig: &[u8]) -> Result { 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) }