Commit 8c58ecc2 authored by Steven Fackler's avatar Steven Fackler
Browse files

Implement EcKey

cc #499
parent eb735f51
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ pub enum ASN1_TYPE {}
pub enum BN_CTX {}
pub enum BN_GENCB {}
pub enum COMP_METHOD {}
pub enum EC_KEY {}
pub enum ENGINE {}
pub enum EVP_CIPHER_CTX {}
pub enum EVP_MD {}
@@ -1341,8 +1342,10 @@ extern {
    #[cfg(not(ossl101))]
    pub fn DH_get_2048_256() -> *mut DH;

    pub fn ERR_get_error() -> c_ulong;
    pub fn EC_KEY_new_by_curve_name(nid: c_int) -> *mut EC_KEY;
    pub fn EC_KEY_free(key: *mut EC_KEY);

    pub fn ERR_get_error() -> c_ulong;
    pub fn ERR_lib_error_string(err: c_ulong) -> *const c_char;
    pub fn ERR_func_error_string(err: c_ulong) -> *const c_char;
    pub fn ERR_reason_error_string(err: c_ulong) -> *const c_char;

openssl/src/ec_key.rs

0 → 100644
+62 −0
Original line number Diff line number Diff line
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();
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -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;