Changes as per PR feedback

This commit is contained in:
Rohit Aggarwal 2018-03-08 17:42:15 +00:00
parent 9e2755abae
commit 8461129456
1 changed files with 21 additions and 9 deletions

View File

@ -4,7 +4,7 @@
use bn::{BigNum, BigNumRef}; use bn::{BigNum, BigNumRef};
use {cvt, cvt_n, cvt_p}; use {cvt, cvt_n, cvt_p};
use ec::EcKey; use ec::EcKeyRef;
use error::ErrorStack; use error::ErrorStack;
use ffi; use ffi;
use foreign_types::{ForeignType, ForeignTypeRef}; use foreign_types::{ForeignType, ForeignTypeRef};
@ -36,7 +36,7 @@ impl EcdsaSig {
/// OpenSSL documentation at [`ECDSA_do_sign`] /// OpenSSL documentation at [`ECDSA_do_sign`]
/// ///
/// [`ECDSA_do_sign`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_do_sign.html /// [`ECDSA_do_sign`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_do_sign.html
pub fn sign(data: &[u8], dgstlen: i32, eckey: &EcKey<Private>) -> Result<EcdsaSig, ErrorStack> { pub fn sign(data: &[u8], dgstlen: i32, eckey: &EcKeyRef<Private>) -> Result<EcdsaSig, ErrorStack> {
unsafe { unsafe {
let sig = cvt_p(ffi::ECDSA_do_sign(data.as_ptr(), dgstlen, eckey.as_ptr()))?; let sig = cvt_p(ffi::ECDSA_do_sign(data.as_ptr(), dgstlen, eckey.as_ptr()))?;
Ok(EcdsaSig::from_ptr(sig as *mut _)) Ok(EcdsaSig::from_ptr(sig as *mut _))
@ -63,24 +63,36 @@ impl EcdsaSig {
/// OpenSSL documentation at [`ECDSA_do_verify`] /// OpenSSL documentation at [`ECDSA_do_verify`]
/// ///
/// [`ECDSA_do_verify`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_do_verify.html /// [`ECDSA_do_verify`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_do_verify.html
pub fn verify(&self, data: &[u8], dgstlen: i32, eckey: &EcKey<Public>) -> Result<bool, ErrorStack> { pub fn verify(&self, data: &[u8], dgstlen: i32, eckey: &EcKeyRef<Public>) -> Result<bool, ErrorStack> {
unsafe { unsafe {
let x = cvt_n(ffi::ECDSA_do_verify(data.as_ptr(), dgstlen, self.as_ptr(), eckey.as_ptr()))?; let x = cvt_n(ffi::ECDSA_do_verify(data.as_ptr(), dgstlen, self.as_ptr(), eckey.as_ptr()))?;
Ok(x == 1) Ok(x == 1)
} }
} }
/// Returns internal components: `r` and `s` of a `EcdsaSig`. (See X9.62 or FIPS 186-2) /// Returns internal component: `r` of a `EcdsaSig`. (See X9.62 or FIPS 186-2)
/// ///
/// OpenSSL documentation at [`ECDSA_SIG_get0`] /// OpenSSL documentation at [`ECDSA_SIG_get0`]
/// ///
/// [`ECDSA_SIG_get0`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_SIG_get0.html /// [`ECDSA_SIG_get0`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_SIG_get0.html
pub fn private_components(&self) -> (Option<&BigNumRef>, Option<&BigNumRef>) { pub fn private_component_r(&self) -> Option<&BigNumRef> {
unsafe { unsafe {
let xs = compat::get_numbers(self.as_ptr()); let xs = compat::get_numbers(self.as_ptr());
let r = if xs[0].is_null() { None } else { Some(BigNumRef::from_ptr(xs[0] as *mut _)) }; let r = if xs[0].is_null() { None } else { Some(BigNumRef::from_ptr(xs[0] as *mut _)) };
r
}
}
/// Returns internal components: `s` of a `EcdsaSig`. (See X9.62 or FIPS 186-2)
///
/// OpenSSL documentation at [`ECDSA_SIG_get0`]
///
/// [`ECDSA_SIG_get0`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_SIG_get0.html
pub fn private_component_s(&self) -> Option<&BigNumRef> {
unsafe {
let xs = compat::get_numbers(self.as_ptr());
let s = if xs[1].is_null() { None } else { Some(BigNumRef::from_ptr(xs[1] as *mut _)) }; let s = if xs[1].is_null() { None } else { Some(BigNumRef::from_ptr(xs[1] as *mut _)) };
(r, s) s
} }
} }
@ -125,6 +137,7 @@ mod compat {
mod test { mod test {
use nid::Nid; use nid::Nid;
use ec::EcGroup; use ec::EcGroup;
use ec::EcKey;
use super::*; use super::*;
static DGST_LEN: i32 = 20; static DGST_LEN: i32 = 20;
@ -176,9 +189,8 @@ mod test {
let verification = res.verify(data.as_bytes(), DGST_LEN, &public_key).unwrap(); let verification = res.verify(data.as_bytes(), DGST_LEN, &public_key).unwrap();
assert!(verification); assert!(verification);
let x = res.private_components(); let r = res.private_component_r().unwrap().to_owned().unwrap();
let r = x.0.unwrap().to_owned().unwrap(); let s = res.private_component_s().unwrap().to_owned().unwrap();
let s = x.1.unwrap().to_owned().unwrap();
let res2 = EcdsaSig::from_private_components(r, s).unwrap(); let res2 = EcdsaSig::from_private_components(r, s).unwrap();
let verification2 = res2.verify(data.as_bytes(), DGST_LEN, &public_key).unwrap(); let verification2 = res2.verify(data.as_bytes(), DGST_LEN, &public_key).unwrap();