summaryrefslogtreecommitdiff
path: root/openssl
diff options
context:
space:
mode:
authorMichael Rossberg <michael.rossberg@tu-ilmenau.de>2021-02-15 11:47:07 +0100
committerMichael Rossberg <michael.rossberg@tu-ilmenau.de>2021-02-15 11:54:03 +0100
commit687f0d26c4d3199179bb0b11485992e004038dba (patch)
tree1ba1cd903caa91ceadeeb10ec4dbc948f4ba5a3c /openssl
parent71b6e3f81f9cf0d1772d00577873cc21ee8f86bf (diff)
downloadrust-openssl-687f0d26c4d3199179bb0b11485992e004038dba.zip
Add ec point validation functions
Diffstat (limited to 'openssl')
-rw-r--r--openssl/src/ec.rs49
1 files changed, 49 insertions, 0 deletions
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<bool, ErrorStack> {
+ 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<bool, ErrorStack> {
+ 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);
+ }
}