From 8c58ecc2fa63fb4234b9e48ef9ba0113628ce35f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 30 Oct 2016 13:17:20 -0700 Subject: Implement EcKey cc #499 --- openssl/src/ec_key.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 openssl/src/ec_key.rs (limited to 'openssl/src/ec_key.rs') diff --git a/openssl/src/ec_key.rs b/openssl/src/ec_key.rs new file mode 100644 index 00000000..5d634c5a --- /dev/null +++ b/openssl/src/ec_key.rs @@ -0,0 +1,62 @@ +use ffi; +use std::ops::Deref; + +use cvt_p; +use error::ErrorStack; +use nid::Nid; +use opaque::Opaque; + +pub struct EcKeyRef(Opaque); + +impl EcKeyRef { + pub unsafe fn from_ptr<'a>(ptr: *mut ffi::EC_KEY) -> &'a EcKeyRef { + &*(ptr as *mut _) + } + + pub fn as_ptr(&self) -> *mut ffi::EC_KEY { + self as *const _ as *mut _ + } +} + +pub struct EcKey(*mut ffi::EC_KEY); + +impl Drop for EcKey { + fn drop(&mut self) { + unsafe { + ffi::EC_KEY_free(self.0); + } + } +} + +impl EcKey { + pub fn new_by_curve_name(nid: Nid) -> Result { + unsafe { + cvt_p(ffi::EC_KEY_new_by_curve_name(nid.as_raw())).map(EcKey) + } + } + + pub unsafe fn from_ptr(ptr: *mut ffi::EC_KEY) -> EcKey { + EcKey(ptr) + } +} + +impl Deref for EcKey { + type Target = EcKeyRef; + + fn deref(&self) -> &EcKeyRef { + unsafe { + EcKeyRef::from_ptr(self.0) + } + } +} + +#[cfg(test)] +mod test { + use nid; + use super::*; + + #[test] + fn new_by_curve_name() { + EcKey::new_by_curve_name(nid::X9_62_PRIME256V1).unwrap(); + } +} -- cgit v1.2.3 From f75f82e466993848393c7a26ccb51dc31b4547fe Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 30 Oct 2016 16:37:45 -0700 Subject: Rustfmt --- openssl/src/ec_key.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'openssl/src/ec_key.rs') diff --git a/openssl/src/ec_key.rs b/openssl/src/ec_key.rs index 5d634c5a..81b790aa 100644 --- a/openssl/src/ec_key.rs +++ b/openssl/src/ec_key.rs @@ -30,9 +30,7 @@ impl Drop for EcKey { impl EcKey { pub fn new_by_curve_name(nid: Nid) -> Result { - unsafe { - cvt_p(ffi::EC_KEY_new_by_curve_name(nid.as_raw())).map(EcKey) - } + unsafe { cvt_p(ffi::EC_KEY_new_by_curve_name(nid.as_raw())).map(EcKey) } } pub unsafe fn from_ptr(ptr: *mut ffi::EC_KEY) -> EcKey { @@ -44,9 +42,7 @@ impl Deref for EcKey { type Target = EcKeyRef; fn deref(&self) -> &EcKeyRef { - unsafe { - EcKeyRef::from_ptr(self.0) - } + unsafe { EcKeyRef::from_ptr(self.0) } } } -- cgit v1.2.3 From d6579ab058a22b8e7e2d82cd0d707fb3e670d5ec Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 31 Oct 2016 20:06:06 -0700 Subject: Update EcKey --- openssl/src/ec_key.rs | 36 +----------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) (limited to 'openssl/src/ec_key.rs') diff --git a/openssl/src/ec_key.rs b/openssl/src/ec_key.rs index 81b790aa..95175eaa 100644 --- a/openssl/src/ec_key.rs +++ b/openssl/src/ec_key.rs @@ -1,49 +1,15 @@ use ffi; -use std::ops::Deref; use cvt_p; use error::ErrorStack; use nid::Nid; -use opaque::Opaque; -pub struct EcKeyRef(Opaque); - -impl EcKeyRef { - pub unsafe fn from_ptr<'a>(ptr: *mut ffi::EC_KEY) -> &'a EcKeyRef { - &*(ptr as *mut _) - } - - pub fn as_ptr(&self) -> *mut ffi::EC_KEY { - self as *const _ as *mut _ - } -} - -pub struct EcKey(*mut ffi::EC_KEY); - -impl Drop for EcKey { - fn drop(&mut self) { - unsafe { - ffi::EC_KEY_free(self.0); - } - } -} +type_!(EcKey, ffi::EC_KEY, ffi::EC_KEY_free); impl EcKey { pub fn new_by_curve_name(nid: Nid) -> Result { unsafe { cvt_p(ffi::EC_KEY_new_by_curve_name(nid.as_raw())).map(EcKey) } } - - pub unsafe fn from_ptr(ptr: *mut ffi::EC_KEY) -> EcKey { - EcKey(ptr) - } -} - -impl Deref for EcKey { - type Target = EcKeyRef; - - fn deref(&self) -> &EcKeyRef { - unsafe { EcKeyRef::from_ptr(self.0) } - } } #[cfg(test)] -- cgit v1.2.3 From 01ae978db0dc8620b2cc754c0d5cf94a68c1f549 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 4 Nov 2016 16:32:20 -0700 Subject: Get rid of Ref There's unfortunately a rustdoc bug that causes all methods implemented for any Ref to be inlined in the deref methods section :( --- openssl/src/ec_key.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'openssl/src/ec_key.rs') diff --git a/openssl/src/ec_key.rs b/openssl/src/ec_key.rs index 95175eaa..41501c14 100644 --- a/openssl/src/ec_key.rs +++ b/openssl/src/ec_key.rs @@ -4,7 +4,7 @@ use cvt_p; use error::ErrorStack; use nid::Nid; -type_!(EcKey, ffi::EC_KEY, ffi::EC_KEY_free); +type_!(EcKey, EcKeyRef, ffi::EC_KEY, ffi::EC_KEY_free); impl EcKey { pub fn new_by_curve_name(nid: Nid) -> Result { -- cgit v1.2.3