Expose EC_POINT_dup as EcPoint::to_owned

This commit is contained in:
Mike Belopuhov 2019-06-19 21:34:48 +02:00
parent 39e692fac5
commit aef0517dcf
2 changed files with 29 additions and 0 deletions

View File

@ -82,6 +82,11 @@ extern "C" {
pub fn EC_POINT_free(point: *mut EC_POINT);
pub fn EC_POINT_dup(
p: *const EC_POINT,
group: *const EC_GROUP,
) -> *mut EC_POINT;
pub fn EC_POINT_get_affine_coordinates_GFp(
group: *const EC_GROUP,
p: *const EC_POINT,

View File

@ -446,6 +446,20 @@ impl EcPointRef {
}
}
/// Creates a new point on the specified curve with the same value.
///
/// OpenSSL documentation at [`EC_POINT_dup`]
///
/// [`EC_POINT_dup`]: https://www.openssl.org/docs/man1.1.0/crypto/EC_POINT_dup.html
pub fn to_owned(
&self,
group: &EcGroupRef,
) -> Result<EcPoint, ErrorStack> {
unsafe {
cvt_p(ffi::EC_POINT_dup(self.as_ptr(), group.as_ptr())).map(EcPoint)
}
}
/// Determines if this point is equal to another.
///
/// OpenSSL doucmentation at [`EC_POINT_cmp`]
@ -919,6 +933,16 @@ mod test {
assert!(point.eq(&group, &point2, &mut ctx).unwrap());
}
#[test]
fn point_owned() {
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
let key = EcKey::generate(&group).unwrap();
let point = key.public_key();
let owned = point.to_owned(&group).unwrap();
let mut ctx = BigNumContext::new().unwrap();
assert!(owned.eq(&group, point, &mut ctx).unwrap());
}
#[test]
fn mul_generator() {
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();