diff --git a/openssl-sys/src/ec.rs b/openssl-sys/src/ec.rs index 05faee27..5998e5bf 100644 --- a/openssl-sys/src/ec.rs +++ b/openssl-sys/src/ec.rs @@ -30,6 +30,12 @@ extern "C" { ctx: *mut BN_CTX, ) -> c_int; + pub fn EC_GROUP_get_cofactor( + group: *const EC_GROUP, + cofactor: *mut BIGNUM, + ctx: *mut BN_CTX, + ) -> c_int; + pub fn EC_GROUP_get0_generator(group: *const EC_GROUP) -> *const EC_POINT; pub fn EC_GROUP_get_curve_name(group: *const EC_GROUP) -> c_int; diff --git a/openssl/src/ec.rs b/openssl/src/ec.rs index 34b7f05e..73aedc71 100644 --- a/openssl/src/ec.rs +++ b/openssl/src/ec.rs @@ -199,6 +199,26 @@ impl EcGroupRef { } } + /// Places the cofactor of the group in the provided `BigNum`. + /// + /// OpenSSL documentation at [`EC_GROUP_get_cofactor`] + /// + /// [`EC_GROUP_get_cofactor`]: https://www.openssl.org/docs/man1.1.0/crypto/EC_GROUP_get_cofactor.html + pub fn cofactor( + &self, + cofactor: &mut BigNumRef, + ctx: &mut BigNumContextRef, + ) -> Result<(), ErrorStack> { + unsafe { + cvt(ffi::EC_GROUP_get_cofactor( + self.as_ptr(), + cofactor.as_ptr(), + ctx.as_ptr(), + )) + .map(|_| ()) + } + } + /// Returns the degree of the curve. /// /// OpenSSL documentation at [`EC_GROUP_get_degree`] @@ -328,7 +348,7 @@ impl EcPointRef { } } - /// Computes `generator * n`, storing the result ing `self`. + /// Computes `generator * n`, storing the result in `self`. pub fn mul_generator( &mut self, group: &EcGroupRef, @@ -863,6 +883,16 @@ mod test { EcKey::generate(&group).unwrap(); } + #[test] + fn cofactor() { + let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap(); + let mut ctx = BigNumContext::new().unwrap(); + let mut cofactor = BigNum::new().unwrap(); + group.cofactor(&mut cofactor, &mut ctx).unwrap(); + let one = BigNum::from_u32(1).unwrap(); + assert_eq!(cofactor, one); + } + #[test] fn dup() { let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();