diff options
| author | Steven Fackler <[email protected]> | 2016-10-30 13:17:20 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-10-30 13:17:20 -0700 |
| commit | 8c58ecc2fa63fb4234b9e48ef9ba0113628ce35f (patch) | |
| tree | ae88b0bebd2431f91c142ebdaafa2526d8875a85 /openssl/src | |
| parent | Clean up generics a bit (diff) | |
| download | rust-openssl-8c58ecc2fa63fb4234b9e48ef9ba0113628ce35f.tar.xz rust-openssl-8c58ecc2fa63fb4234b9e48ef9ba0113628ce35f.zip | |
Implement EcKey
cc #499
Diffstat (limited to 'openssl/src')
| -rw-r--r-- | openssl/src/ec_key.rs | 62 | ||||
| -rw-r--r-- | openssl/src/lib.rs | 1 |
2 files changed, 63 insertions, 0 deletions
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<EcKey, ErrorStack> { + 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(); + } +} diff --git a/openssl/src/lib.rs b/openssl/src/lib.rs index acdc2ea8..4212e9de 100644 --- a/openssl/src/lib.rs +++ b/openssl/src/lib.rs @@ -28,6 +28,7 @@ pub mod bn; pub mod crypto; pub mod dh; pub mod dsa; +pub mod ec_key; pub mod error; pub mod hash; pub mod memcmp; |