From 687f0d26c4d3199179bb0b11485992e004038dba Mon Sep 17 00:00:00 2001 From: Michael Rossberg Date: Mon, 15 Feb 2021 11:47:07 +0100 Subject: Add ec point validation functions --- openssl-sys/src/ec.rs | 8 ++++++++ openssl/src/ec.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/openssl-sys/src/ec.rs b/openssl-sys/src/ec.rs index 98e233fb..82772fe8 100644 --- a/openssl-sys/src/ec.rs +++ b/openssl-sys/src/ec.rs @@ -81,6 +81,14 @@ extern "C" { pub fn EC_GROUP_new_by_curve_name(nid: c_int) -> *mut EC_GROUP; + pub fn EC_POINT_is_at_infinity(group: *const EC_GROUP, point: *const EC_POINT) -> c_int; + + pub fn EC_POINT_is_on_curve( + group: *const EC_GROUP, + point: *const EC_POINT, + ctx: *mut BN_CTX, + ) -> c_int; + pub fn EC_POINT_new(group: *const EC_GROUP) -> *mut EC_POINT; pub fn EC_POINT_free(point: *mut EC_POINT); diff --git a/openssl/src/ec.rs b/openssl/src/ec.rs index efbc26f6..1e266a0b 100644 --- a/openssl/src/ec.rs +++ b/openssl/src/ec.rs @@ -527,6 +527,30 @@ impl EcPointRef { .map(|_| ()) } } + + /// Checks if point is infinity + pub fn is_infinity(&self, group: &EcGroupRef) -> Result { + unsafe { + let res = cvt_n(ffi::EC_POINT_is_at_infinity(group.as_ptr(), self.as_ptr()))?; + Ok(res == 1) + } + } + + /// Checks if point is on a given curve + pub fn is_on_curve( + &self, + group: &EcGroupRef, + ctx: &mut BigNumContextRef, + ) -> Result { + unsafe { + let res = cvt_n(ffi::EC_POINT_is_on_curve( + group.as_ptr(), + self.as_ptr(), + ctx.as_ptr(), + ))?; + Ok(res == 1) + } + } } impl EcPoint { @@ -1074,4 +1098,29 @@ mod test { assert_eq!(xbn2, xbn); assert_eq!(ybn2, ybn); } + + #[test] + fn is_infinity() { + let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap(); + let mut ctx = BigNumContext::new().unwrap(); + let g = group.generator(); + assert_eq!(g.is_infinity(&group).unwrap(), false); + + let mut order = BigNum::new().unwrap(); + group.order(&mut order, &mut ctx).unwrap(); + let mut inf = EcPoint::new(&group).unwrap(); + inf.mul_generator(&group, &order, &ctx).unwrap(); + assert_eq!(inf.is_infinity(&group).unwrap(), true); + } + + #[test] + fn is_on_curve() { + let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap(); + let mut ctx = BigNumContext::new().unwrap(); + let g = group.generator(); + assert_eq!(g.is_on_curve(&group, &mut ctx).unwrap(), true); + + let group2 = EcGroup::from_curve_name(Nid::X9_62_PRIME239V3).unwrap(); + assert_eq!(g.is_on_curve(&group2, &mut ctx).unwrap(), false); + } } -- cgit v1.2.3 From 53a220bee6d19d3c70f6a60606eea353ad47e72a Mon Sep 17 00:00:00 2001 From: Michael Rossberg Date: Mon, 15 Feb 2021 15:34:10 +0100 Subject: fix sfackler's comments --- openssl/src/ec.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/openssl/src/ec.rs b/openssl/src/ec.rs index 1e266a0b..32751cd2 100644 --- a/openssl/src/ec.rs +++ b/openssl/src/ec.rs @@ -529,14 +529,22 @@ impl EcPointRef { } /// Checks if point is infinity - pub fn is_infinity(&self, group: &EcGroupRef) -> Result { + /// + /// OpenSSL documentation at [`EC_POINT_is_at_infinity`] + /// + /// [`EC_POINT_is_at_infinity`]: https://www.openssl.org/docs/man1.1.0/man3/EC_POINT_is_at_infinity.html + pub fn is_infinity(&self, group: &EcGroupRef) -> bool { unsafe { - let res = cvt_n(ffi::EC_POINT_is_at_infinity(group.as_ptr(), self.as_ptr()))?; - Ok(res == 1) + let res = ffi::EC_POINT_is_at_infinity(group.as_ptr(), self.as_ptr()); + res == 1 } } /// Checks if point is on a given curve + /// + /// OpenSSL documentation at [`EC_POINT_is_on_curve`] + /// + /// [`EC_POINT_is_on_curve`]: https://www.openssl.org/docs/man1.1.0/man3/EC_POINT_is_on_curve.html pub fn is_on_curve( &self, group: &EcGroupRef, @@ -1104,13 +1112,13 @@ mod test { let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap(); let mut ctx = BigNumContext::new().unwrap(); let g = group.generator(); - assert_eq!(g.is_infinity(&group).unwrap(), false); + assert_eq!(g.is_infinity(&group), false); let mut order = BigNum::new().unwrap(); group.order(&mut order, &mut ctx).unwrap(); let mut inf = EcPoint::new(&group).unwrap(); inf.mul_generator(&group, &order, &ctx).unwrap(); - assert_eq!(inf.is_infinity(&group).unwrap(), true); + assert_eq!(inf.is_infinity(&group), true); } #[test] -- cgit v1.2.3