diff options
| author | Steven Fackler <[email protected]> | 2016-11-13 20:19:38 +0000 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-11-13 20:19:38 +0000 |
| commit | b2de36049a7687da00870e714c2e299ac5a903cd (patch) | |
| tree | d939d641703a6ddd711f6bdcd083bb0891fcfe78 /openssl/src | |
| parent | Remove some stray manual impls (diff) | |
| download | rust-openssl-b2de36049a7687da00870e714c2e299ac5a903cd.tar.xz rust-openssl-b2de36049a7687da00870e714c2e299ac5a903cd.zip | |
Add Some more elliptic curve functionality
Diffstat (limited to 'openssl/src')
| -rw-r--r-- | openssl/src/ec_key.rs | 114 |
1 files changed, 113 insertions, 1 deletions
diff --git a/openssl/src/ec_key.rs b/openssl/src/ec_key.rs index 268a6fd2..3b6349b5 100644 --- a/openssl/src/ec_key.rs +++ b/openssl/src/ec_key.rs @@ -2,15 +2,115 @@ use ffi; use std::ptr; use {cvt, cvt_p, init}; +use bn::{BigNumRef, BigNumContextRef}; use error::ErrorStack; use nid::Nid; use types::OpenSslTypeRef; +type_!(EcGroup, EcGroupRef, ffi::EC_GROUP, ffi::EC_GROUP_free); + +impl EcGroup { + /// Returns the group of a standard named curve. + pub fn from_curve_name(nid: Nid) -> Result<EcGroup, ErrorStack> { + unsafe { + init(); + cvt_p(ffi::EC_GROUP_new_by_curve_name(nid.as_raw())).map(EcGroup) + } + } + + /// Constructs a curve over a prime field from its components. + pub fn from_components_gfp(p: &BigNumRef, + a: &BigNumRef, + b: &BigNumRef, + ctx: &mut BigNumContextRef) + -> Result<EcGroup, ErrorStack> { + unsafe { + cvt_p(ffi::EC_GROUP_new_curve_GFp(p.as_ptr(), a.as_ptr(), b.as_ptr(), ctx.as_ptr())) + .map(EcGroup) + } + } + + /// Constructs a curve over a binary field from its components. + pub fn from_components_gf2m(p: &BigNumRef, + a: &BigNumRef, + b: &BigNumRef, + ctx: &mut BigNumContextRef) + -> Result<EcGroup, ErrorStack> { + unsafe { + cvt_p(ffi::EC_GROUP_new_curve_GF2m(p.as_ptr(), a.as_ptr(), b.as_ptr(), ctx.as_ptr())) + .map(EcGroup) + } + } + + /// Places the components of a curve over a prime field in the provided `BigNum`s. + pub fn components_gfp(&self, + p: &mut BigNumRef, + a: &mut BigNumRef, + b: &mut BigNumRef, + ctx: &mut BigNumContextRef) + -> Result<(), ErrorStack> { + unsafe { + cvt(ffi::EC_GROUP_get_curve_GFp(self.as_ptr(), + p.as_ptr(), + a.as_ptr(), + b.as_ptr(), + ctx.as_ptr())) + .map(|_| ()) + } + } + + /// Places the components of a curve over a binary field in the provided `BigNum`s. + pub fn components_gf2m(&self, + p: &mut BigNumRef, + a: &mut BigNumRef, + b: &mut BigNumRef, + ctx: &mut BigNumContextRef) + -> Result<(), ErrorStack> { + unsafe { + cvt(ffi::EC_GROUP_get_curve_GF2m(self.as_ptr(), + p.as_ptr(), + a.as_ptr(), + b.as_ptr(), + ctx.as_ptr())) + .map(|_| ()) + } + } +} + +type_!(EcPoint, EcPointRef, ffi::EC_POINT, ffi::EC_POINT_free); + type_!(EcKey, EcKeyRef, ffi::EC_KEY, ffi::EC_KEY_free); impl EcKeyRef { private_key_to_pem!(ffi::PEM_write_bio_ECPrivateKey); private_key_to_der!(ffi::i2d_ECPrivateKey); + + pub fn group(&self) -> &EcGroupRef { + unsafe { + let ptr = ffi::EC_KEY_get0_group(self.as_ptr()); + assert!(!ptr.is_null()); + EcGroupRef::from_ptr(ptr as *mut _) + } + } + + pub fn public_key(&self) -> &EcPointRef { + unsafe { + let ptr = ffi::EC_KEY_get0_public_key(self.as_ptr()); + assert!(!ptr.is_null()); + EcPointRef::from_ptr(ptr as *mut _) + } + } + + pub fn private_key(&self) -> Option<&BigNumRef> { + unsafe { + let ptr = ffi::EC_KEY_get0_private_key(self.as_ptr()); + if ptr.is_null() { + None + } else { + Some(BigNumRef::from_ptr(ptr as *mut _)) + } + } + } } impl EcKey { @@ -27,11 +127,23 @@ impl EcKey { #[cfg(test)] mod test { + use bn::{BigNum, BigNumContext}; use nid; use super::*; #[test] - fn new_by_curve_name() { + fn key_new_by_curve_name() { EcKey::new_by_curve_name(nid::X9_62_PRIME256V1).unwrap(); } + + #[test] + fn round_trip_prime256v1() { + let group = EcGroup::from_curve_name(nid::X9_62_PRIME256V1).unwrap(); + let mut p = BigNum::new().unwrap(); + let mut a = BigNum::new().unwrap(); + let mut b = BigNum::new().unwrap(); + let mut ctx = BigNumContext::new().unwrap(); + group.components_gfp(&mut p, &mut a, &mut b, &mut ctx).unwrap(); + EcGroup::from_components_gfp(&p, &a, &b, &mut ctx).unwrap(); + } } |