Commit 1a526495 authored by Steven Fackler's avatar Steven Fackler
Browse files

More functionality

parent 3d31539b
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -1374,10 +1374,15 @@ extern {
    #[cfg(not(ossl101))]
    pub fn DH_get_2048_256() -> *mut DH;

    pub fn EC_KEY_new() -> *mut EC_KEY;
    pub fn EC_KEY_new_by_curve_name(nid: c_int) -> *mut EC_KEY;
    pub fn EC_KEY_set_group(key: *mut EC_KEY, group: *const EC_GROUP) -> c_int;
    pub fn EC_KEY_get0_group(key: *const EC_KEY) -> *const EC_GROUP;
    pub fn EC_KEY_set_public_key(key: *mut EC_KEY, key: *const EC_POINT) -> c_int;
    pub fn EC_KEY_get0_public_key(key: *const EC_KEY) -> *const EC_POINT;
    pub fn EC_KEY_set_private_key(key: *mut EC_KEY, key: *const BIGNUM) -> c_int;
    pub fn EC_KEY_get0_private_key(key: *const EC_KEY) -> *const BIGNUM;
    pub fn EC_KEY_generate_key(key: *mut EC_KEY) -> c_int;
    pub fn EC_KEY_free(key: *mut EC_KEY);

    pub fn EC_GFp_simple_method() -> *const EC_METHOD;
+26 −2
Original line number Diff line number Diff line
@@ -96,8 +96,11 @@ impl EcKeyRef {
    pub fn public_key(&self) -> Option<&EcPointRef> {
        unsafe {
            let ptr = ffi::EC_KEY_get0_public_key(self.as_ptr());
            assert!(!ptr.is_null());
            EcPointRef::from_ptr(ptr as *mut _)
            if ptr.is_null() {
                None
            } else {
                Some(EcPointRef::from_ptr(ptr as *mut _))
            }
        }
    }

@@ -114,6 +117,9 @@ impl EcKeyRef {
}

impl EcKey {
    /// Constructs an `EcKey` corresponding to a known curve.
    ///
    /// It will not have an associated public or private key.
    pub fn from_curve_name(nid: Nid) -> Result<EcKey, ErrorStack> {
        unsafe {
            init();
@@ -121,6 +127,16 @@ impl EcKey {
        }
    }

    /// Generates a new public/private key pair on the specified curve.
    pub fn generate(group: &EcGroupRef) -> Result<EcKey, ErrorStack> {
        unsafe {
            let key = EcKey(try!(cvt_p(ffi::EC_KEY_new())));
            try!(cvt(ffi::EC_KEY_set_group(key.as_ptr(), group.as_ptr())));
            try!(cvt(ffi::EC_KEY_generate_key(key.as_ptr())));
            Ok(key)
        }
    }

    #[deprecated(since = "0.9.2", note = "use from_curve_name")]
    pub fn new_by_curve_name(nid: Nid) -> Result<EcKey, ErrorStack> {
        EcKey::from_curve_name(nid)
@@ -151,4 +167,12 @@ mod test {
        group.components_gfp(&mut p, &mut a, &mut b, &mut ctx).unwrap();
        EcGroup::from_components_gfp(&p, &a, &b, &mut ctx).unwrap();
    }

    #[test]
    fn generate() {
        let group = EcGroup::from_curve_name(nid::X9_62_PRIME256V1).unwrap();
        let key = EcKey::generate(&group).unwrap();
        key.public_key().unwrap();
        key.private_key().unwrap();
    }
}