diff options
| author | Bradley Beddoes <[email protected]> | 2017-08-09 12:02:58 +1000 |
|---|---|---|
| committer | Bradley Beddoes <[email protected]> | 2017-08-09 12:21:54 +1000 |
| commit | cfb4ea31d51c274a31f500cd1a5e8bdac571607c (patch) | |
| tree | e2571655d3105bfc29aa4ceda3040a19e2309b42 /openssl/src | |
| parent | Init in bn_ctx constructor (diff) | |
| download | rust-openssl-cfb4ea31d51c274a31f500cd1a5e8bdac571607c.tar.xz rust-openssl-cfb4ea31d51c274a31f500cd1a5e8bdac571607c.zip | |
Support for EcKey creation from affine coordinates
Sets the public key for an EcKey based on its affine co-ordinates,
i.e. it constructs an EC_POINT object based on the supplied x and y
values and sets the public key to be this EC_POINT.
The initial usecase here is creating EcKey instances from JWK
representations as defined within RFC 7517.
Diffstat (limited to 'openssl/src')
| -rw-r--r-- | openssl/src/ec.rs | 36 | ||||
| -rw-r--r-- | openssl/src/lib.rs | 2 |
2 files changed, 37 insertions, 1 deletions
diff --git a/openssl/src/ec.rs b/openssl/src/ec.rs index 95baa833..5ff9fd5b 100644 --- a/openssl/src/ec.rs +++ b/openssl/src/ec.rs @@ -459,12 +459,26 @@ impl EcKeyBuilderRef { pub fn generate_key(&mut self) -> Result<&mut EcKeyBuilderRef, ErrorStack> { unsafe { cvt(ffi::EC_KEY_generate_key(self.as_ptr())).map(|_| self) } } + + /// Sets the public key based on affine coordinates. + pub fn set_public_key_affine_coordinates(&mut self, + x: &BigNumRef, + y: &BigNumRef) + -> Result<&mut EcKeyBuilderRef, ErrorStack> { + unsafe { + cvt(ffi::EC_KEY_set_public_key_affine_coordinates(self.as_ptr(), + x.as_ptr(), + y.as_ptr()) + ).map(|_| self) + } + } } #[cfg(test)] mod test { - use bn::BigNumContext; + use bn::{BigNum, BigNumContext}; use nid; + use data_encoding; use super::*; #[test] @@ -539,4 +553,24 @@ mod test { assert!(ec_key.public_key().is_some()); assert!(ec_key.private_key().is_none()); } + + #[test] + fn key_from_affine_coordinates() { + let group = EcGroup::from_curve_name(nid::X9_62_PRIME256V1).unwrap(); + let x = data_encoding::base64url::decode_nopad("MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4".as_bytes()) + .unwrap(); + let y = data_encoding::base64url::decode_nopad("4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM".as_bytes()) + .unwrap(); + + let xbn = BigNum::from_slice(&x).unwrap(); + let ybn = BigNum::from_slice(&y).unwrap(); + + let mut builder = EcKeyBuilder::new().unwrap(); + builder.set_group(&group).unwrap(); + builder.set_public_key_affine_coordinates(&xbn, &ybn).unwrap(); + + let ec_key = builder.build(); + assert!(ec_key.check_key().is_ok()); + assert!(ec_key.public_key().is_some()); + } } diff --git a/openssl/src/lib.rs b/openssl/src/lib.rs index 44752dcc..a6d5e6a0 100644 --- a/openssl/src/lib.rs +++ b/openssl/src/lib.rs @@ -13,6 +13,8 @@ extern crate openssl_sys as ffi; extern crate hex; #[cfg(test)] extern crate tempdir; +#[cfg(test)] +extern crate data_encoding; #[doc(inline)] pub use ffi::init; |