diff options
| author | Steven Fackler <[email protected]> | 2016-11-13 20:46:01 +0000 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-11-13 20:46:01 +0000 |
| commit | 1a52649516e5b3924917314ee503523d59ed528b (patch) | |
| tree | df1962722ab8f48d5ec12388b9cc2fd38735e705 /openssl/src | |
| parent | Public keys are not always present (diff) | |
| download | rust-openssl-1a52649516e5b3924917314ee503523d59ed528b.tar.xz rust-openssl-1a52649516e5b3924917314ee503523d59ed528b.zip | |
More functionality
Diffstat (limited to 'openssl/src')
| -rw-r--r-- | openssl/src/ec_key.rs | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/openssl/src/ec_key.rs b/openssl/src/ec_key.rs index 22082b42..e7e92d7a 100644 --- a/openssl/src/ec_key.rs +++ b/openssl/src/ec_key.rs @@ -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(); + } } |